-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathboundaryvolumehierarchy.h
339 lines (302 loc) · 9.93 KB
/
boundaryvolumehierarchy.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#ifndef BOUNDARYVOLUMEHIERARCHY_H
#define BOUNDARYVOLUMEHIERARCHY_H
#include <algorithm>
#include <set>
#include "partitioner.h"
template <size_t DIMENSION>
class BoundaryVolumeHierarchy : public Partitioner<DIMENSION>
{
public:
typedef typename Point<DIMENSION>::Vector Vector;
static const size_t NUM_CHILDREN = 1 << DIMENSION;
BoundaryVolumeHierarchy(const PointCloud<DIMENSION> *pointCloud)
: Partitioner<DIMENSION>(pointCloud)
, mRoot(this)
, mParent(this)
, mLeaf(true)
, mLevel(0)
{
Rect<DIMENSION> extension = pointCloud->extension();
mCenter = extension.center();
mSize = extension.maxSize() / 2;
mIndices = std::vector<size_t>(pointCloud->size());
std::iota(mIndices.begin(), mIndices.end(), 0);
mLeafTable = std::vector<BoundaryVolumeHierarchy<DIMENSION>*>(pointCloud->size(), this);
}
BoundaryVolumeHierarchy(const BoundaryVolumeHierarchy &bvh) = delete;
~BoundaryVolumeHierarchy()
{
if (!isLeaf())
{
for (size_t i = 0; i < NUM_CHILDREN; i++)
{
if (mChildren[i] != NULL)
{
delete mChildren[i];
}
}
}
}
void partition(size_t levels = 1, size_t minNumPoints = 1, float minSize = 0.0f) override
{
if (!isLeaf())
{
for (size_t i = 0; i < NUM_CHILDREN; i++)
{
if (mChildren[i] != NULL)
mChildren[i]->partition(levels - 1, minNumPoints, minSize);
}
}
else
{
if (levels <= 0 || mIndices.size() <= minNumPoints || mIndices.size() <= 1 || mSize < minSize) return;
// create new centers
Vector newCenters[NUM_CHILDREN];
calculateNewCenters(newCenters);
mLeaf = false;
// create children
float newSize = mSize / 2;
for (size_t i = 0; i < NUM_CHILDREN; i++)
{
mChildren[i] = NULL;
}
// split points
for (const size_t &index : mIndices)
{
// calculate child index comparing position to child center
size_t childIndex = calculateChildIndex(this->pointCloud()->at(index).position());
if (mChildren[childIndex] == NULL)
{
mChildren[childIndex] = new BoundaryVolumeHierarchy<DIMENSION>(this, newCenters[childIndex], newSize);
mChildren[childIndex]->mIndices.reserve(mIndices.size());
}
mChildren[childIndex]->mIndices.push_back(index);
// update current leaf where point is stored
mRoot->mLeafTable[index] = mChildren[childIndex];
}
mIndices.clear();
// partition recursively
for (size_t i = 0; i < NUM_CHILDREN; i++)
{
if (mChildren[i] != NULL) {
mChildren[i]->partition(levels - 1, minNumPoints, minSize);
}
}
}
}
const Partitioner<DIMENSION>* getContainingLeaf(size_t index) const override
{
return mRoot->mLeafTable[index];
}
BoundaryVolumeHierarchy<DIMENSION>* child(size_t index)
{
return mChildren[index];
}
std::vector<const Partitioner<DIMENSION>*> children() const override
{
if (isLeaf()) return std::vector<const Partitioner<DIMENSION>*>();
std::vector<const Partitioner<DIMENSION>*> children;
for (size_t i = 0; i < NUM_CHILDREN; i++)
{
if (mChildren[i] != NULL)
children.push_back(mChildren[i]);
}
return children;
}
const Partitioner<DIMENSION>* parent() const override
{
return mParent;
}
const Vector& center() const
{
return mCenter;
}
float cellSize() const
{
return mSize;
}
bool isRoot() const override
{
return this == mRoot;
}
bool isLeaf() const override
{
return mLeaf;
}
size_t octreeLevel() const
{
return mLevel;
}
size_t numPoints() const override
{
if (isLeaf())
{
return mIndices.size();
}
else
{
size_t numPoints = 0;
for (size_t i = 0; i < NUM_CHILDREN; i++)
{
if (mChildren[i] != NULL)
numPoints += mChildren[i]->numPoints();
}
return numPoints;
}
}
Rect<DIMENSION> extension() const override
{
return Rect<DIMENSION>(mCenter - Vector::Constant(mSize),
mCenter + Vector::Constant(mSize));
}
std::vector<size_t> points() const override
{
if (isLeaf())
{
return mIndices;
}
else
{
std::vector<size_t> points;
for (size_t i = 0; i < NUM_CHILDREN; i++)
{
if (mChildren[i] != NULL)
{
std::vector<size_t> childPoints = mChildren[i]->points();
points.insert(points.end(), childPoints.begin(), childPoints.end());
}
}
return points;
}
}
void getNeighborCells(std::vector<BoundaryVolumeHierarchy<DIMENSION>*> &neighbors)
{
BoundaryVolumeHierarchy<DIMENSION>* neighbor;
for (size_t i = 0; i < DIMENSION; i++)
{
neighbor = getNeighborCellsGreaterOrEqual(i, false);
getNeighborCellsSmaller(i, false, neighbor, neighbors);
neighbor = getNeighborCellsGreaterOrEqual(i, true);
getNeighborCellsSmaller(i, true, neighbor, neighbors);
}
}
private:
BoundaryVolumeHierarchy *mRoot;
BoundaryVolumeHierarchy *mParent;
BoundaryVolumeHierarchy *mChildren[NUM_CHILDREN];
Vector mCenter;
float mSize;
bool mLeaf;
size_t mLevel;
std::vector<size_t> mIndices;
std::vector<BoundaryVolumeHierarchy<DIMENSION>*> mLeafTable;
BoundaryVolumeHierarchy(BoundaryVolumeHierarchy *parent, const Vector ¢er, float size)
: Partitioner<DIMENSION>(parent->pointCloud())
, mRoot(parent->mRoot)
, mParent(parent)
, mCenter(center)
, mSize(size)
, mLeaf(true)
, mLevel(parent->mLevel + 1)
{
for (size_t i = 0; i < NUM_CHILDREN; i++)
{
mChildren[i] = NULL;
}
}
void calculateNewCenters(Vector centers[NUM_CHILDREN])
{
float newSize = mSize / 2;
for (size_t dim = 0; dim < DIMENSION; dim++)
{
for (size_t i = 0; i < NUM_CHILDREN; i++)
{
int signal = (((i & (1 << (DIMENSION - dim - 1))) >> (DIMENSION - dim - 1)) << 1) - 1;
centers[i](dim) = mCenter(dim) + newSize * signal;
}
}
}
size_t calculateChildIndex(const Vector &position)
{
size_t childIndex = 0;
for (size_t dim = 0; dim < DIMENSION; dim++)
{
childIndex |= (position(dim) > mCenter(dim)) << (DIMENSION - dim - 1);
}
return childIndex;
}
size_t getIndexOnParent()
{
for (size_t i = 0; i < NUM_CHILDREN; i++)
{
if (mParent->mChildren[i] == this)
{
return i;
}
}
throw "Could not find node on parent";
}
size_t getIncrement(size_t direction, bool greaterThan)
{
int increment = 1 << (DIMENSION - direction - 1);
if (!greaterThan)
{
increment = NUM_CHILDREN - increment;
}
return increment;
}
BoundaryVolumeHierarchy<DIMENSION>* getNeighborCellsGreaterOrEqual(size_t direction, bool greaterThan)
{
if (isRoot())
{
return NULL;
}
else if (greaterThan && mParent->mCenter(direction) > mCenter(direction) ||
!greaterThan && mParent->mCenter(direction) < mCenter(direction))
{
size_t index = getIndexOnParent();
size_t increment = getIncrement(direction, greaterThan);
return mParent->mChildren[(index + increment) % NUM_CHILDREN];
}
BoundaryVolumeHierarchy<DIMENSION>* node = mParent->getNeighborCellsGreaterOrEqual(direction, greaterThan);
if (node == NULL || node->isLeaf())
{
return node;
}
size_t index = getIndexOnParent();
size_t increment = getIncrement(direction, !greaterThan);
return node->mChildren[(index + increment) % NUM_CHILDREN];
}
void getNeighborCellsSmaller(size_t direction, bool greaterThan, BoundaryVolumeHierarchy<DIMENSION>* neighbor,
std::vector<BoundaryVolumeHierarchy<DIMENSION>*> &neighbors)
{
std::queue<BoundaryVolumeHierarchy<DIMENSION>*> candidates;
if (neighbor != NULL) candidates.push(neighbor);
while (candidates.size() > 0)
{
BoundaryVolumeHierarchy<DIMENSION>* front = candidates.front();
candidates.pop();
if (front->isLeaf())
{
neighbors.push_back(front);
}
else
{
for (size_t i = 0; i < NUM_CHILDREN; i++)
{
if (front->mChildren[i] == NULL) continue;
if ((greaterThan && front->mCenter(direction) > front->mChildren[i]->mCenter(direction)) ||
(!greaterThan && front->mCenter(direction) < front->mChildren[i]->mCenter(direction)))
{
candidates.push(front->mChildren[i]);
}
}
}
}
}
};
template class BoundaryVolumeHierarchy<2>;
template class BoundaryVolumeHierarchy<3>;
typedef BoundaryVolumeHierarchy<2> Quadtree;
typedef BoundaryVolumeHierarchy<3> Octree;
#endif // BOUNDARYVOLUMEHIERARCHY_H