-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOctTree.h
282 lines (252 loc) · 5.76 KB
/
OctTree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* OctTree.h
*
* Created on: Feb 27, 2012
* Author: TF
* \copyright
* Copyright (c) 2015, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*/
#ifndef OCTTREE_H_
#define OCTTREE_H_
#include <limits>
// MathLib
#include "MathTools.h"
namespace GEOLIB
{
template <typename POINT>
class OctTree
{
public:
static OctTree<POINT>* createOctTree(POINT& ll, POINT& ur, size_t max_points_per_node)
{
const double dx(ur[0] - ll[0]);
const double dy(ur[1] - ll[1]);
const double dz(ur[2] - ll[2]);
if (dx >= dy && dx >= dz)
{
ll[1] -= (dx - dy) / 2.0;
ur[1] += (dx - dy) / 2.0;
ll[2] -= (dx - dz) / 2.0;
ur[2] += (dx - dz) / 2.0;
}
else
{
if (dy >= dx && dy >= dz)
{
ll[0] -= (dy - dx) / 2.0;
ur[0] += (dy - dx) / 2.0;
ll[2] -= (dy - dz) / 2.0;
ur[2] += (dy - dz) / 2.0;
}
else
{
ll[0] -= (dz - dx) / 2.0;
ur[0] += (dz - dx) / 2.0;
ll[1] -= (dz - dy) / 2.0;
ur[1] += (dz - dy) / 2.0;
}
}
OctTree<POINT>::_max_points_per_node = max_points_per_node;
return new OctTree<POINT>(ll, ur);
}
virtual ~OctTree()
{
for (size_t k(0); k < 8; k++)
delete _childs[k];
}
/**
* This method adds the given point to the OctTree. If necessary,
* the OctTree will be extended.
* @param pnt the point
* @return If the point can be inserted the method returns true, else false.
*/
bool addPoint(POINT* pnt)
{
if ((*pnt)[0] < _ll[0])
return false;
if ((*pnt)[0] > _ur[0])
return false;
if ((*pnt)[1] < _ll[1])
return false;
if ((*pnt)[1] > _ur[1])
return false;
if ((*pnt)[2] < _ll[2])
return false;
if ((*pnt)[2] > _ur[2])
return false;
if (!_is_leaf)
{
for (size_t k(0); k < 8; k++)
{
if (_childs[k]->addPoint(pnt))
{
return true;
}
}
}
// check if point is already in OctTree
bool pnt_in_tree(false);
for (size_t k(0); k < _pnts.size() && !pnt_in_tree; k++)
{
const double sqr_dist(MathLib::sqrDist((_pnts[k])->getData(), pnt->getData()));
if (sqr_dist < std::numeric_limits<double>::epsilon())
pnt_in_tree = true;
}
if (!pnt_in_tree)
_pnts.push_back(pnt);
else
return false;
if (_pnts.size() > OctTree<POINT>::_max_points_per_node)
splitNode();
return true;
}
/**
* range query - returns all points inside the range (min[0], max[0]) x (min[1], max[1]) x (min[2], max[2])
* @param min
* @param max
* @param pnts
*/
void getPointsInRange(POINT const& min, POINT const& max, std::vector<POINT*>& pnts) const
{
if (_ur[0] < min[0])
return;
if (_ur[1] < min[1])
return;
if (_ur[2] < min[2])
return;
if (max[0] < _ll[0])
return;
if (max[1] < _ll[1])
return;
if (max[2] < _ll[2])
return;
if (_is_leaf)
{
typename std::vector<POINT*>::const_iterator it;
for (it = (_pnts.begin()); it != _pnts.end(); it++)
{
pnts.push_back(*it);
}
}
else
{
for (size_t k(0); k < 8; k++)
{
_childs[k]->getPointsInRange(min, max, pnts);
}
}
}
private:
enum OctTreeQuadrant
{
NEL = 0, //!< north east lower
NWL, //!< north west lower
SWL, //!< south west lower
SEL, //!< south east lower
NEU, //!< south west upper
NWU, //!< south west upper
SWU, //!< south west upper
SEU //!< south east upper
};
/**
* private constructor
* @param ll lower left point
* @param ur upper right point
* @return
*/
OctTree(POINT const& ll, POINT const& ur) : _ll(ll), _ur(ur), _is_leaf(true)
{
// init childs
for (size_t k(0); k < 8; k++)
_childs[k] = NULL;
}
void splitNode()
{
const double x_mid((_ur[0] + _ll[0]) / 2.0);
const double y_mid((_ur[1] + _ll[1]) / 2.0);
const double z_mid((_ur[2] + _ll[2]) / 2.0);
POINT p0(x_mid, y_mid, _ll[2]), p1(_ur[0], _ur[1], z_mid);
// create child NEL
_childs[NEL] = new OctTree<POINT>(p0, p1);
// create child NWL
p0[0] = _ll[0];
p1[0] = x_mid;
_childs[NWL] = new OctTree<POINT>(p0, p1);
// create child SWL
p0[1] = _ll[1];
p1[1] = y_mid;
_childs[SWL] = new OctTree<POINT>(_ll, p1);
// create child NEU
_childs[NEU] = new OctTree<POINT>(p1, _ur);
// create child SEL
p0[0] = x_mid;
p1[0] = _ur[0];
_childs[SEL] = new OctTree<POINT>(p0, p1);
// create child NWU
p0[0] = _ll[0];
p0[1] = y_mid;
p0[2] = z_mid;
p1[0] = x_mid;
p1[1] = _ur[1];
p1[2] = _ur[2];
_childs[NWU] = new OctTree<POINT>(p0, p1);
// create child SWU
p0[1] = _ll[1];
p1[1] = y_mid;
_childs[SWU] = new OctTree<POINT>(p0, p1);
// create child SEU
p0[0] = x_mid;
p1[0] = _ur[0];
p1[1] = y_mid;
p1[2] = _ur[2];
_childs[SEU] = new OctTree<POINT>(p0, p1);
// distribute points to sub quadtrees
const size_t n_pnts(_pnts.size());
for (size_t j(0); j < n_pnts; j++)
{
bool nfound(true);
for (size_t k(0); k < 8 && nfound; k++)
{
if (_childs[k]->addPoint(_pnts[j]))
{
nfound = false;
}
}
}
_pnts.clear();
_is_leaf = false;
}
/**
* childs are sorted:
* _childs[0] is north east lower child
* _childs[1] is north west lower child
* _childs[2] is south west lower child
* _childs[3] is south east lower child
* _childs[4] is north east upper child
* _childs[5] is north west upper child
* _childs[6] is south west upper child
* _childs[7] is south east upper child
*/
OctTree<POINT>* _childs[8];
/**
* lower left front face point of the cube
*/
POINT const _ll;
/**
* upper right back face point of the cube
*/
POINT const _ur;
std::vector<POINT*> _pnts;
bool _is_leaf;
/**
* maximum number of points per leaf
*/
static size_t _max_points_per_node;
};
template <typename POINT>
size_t OctTree<POINT>::_max_points_per_node = 0;
}
#endif /* OCTTREE_H_ */