-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBallTree.cpp
387 lines (323 loc) · 12.2 KB
/
BallTree.cpp
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* $Id: BallTree.cpp 298 2015-03-25 13:00:40Z bernardt.duvenhage $
*/
/*
* BallTree.cpp
* StitchEngine
*
* Created by Bernardt Duvenhage on 2011/02/22.
* Copyright $Date: 2015-03-25 15:00:40 +0200 (Wed, 25 Mar 2015) $ Bernardt Duvenhage. All rights reserved.
*
*
* This file is part of StitchEngine.
* StitchEngine is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* StitchEngine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with StitchEngine. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "BallTree.h"
#include "Math/Plane.h"
#include <iostream>
stitch::BallTree::BallTree() :
BoundingVolume()
{
}
stitch::BallTree::BallTree(const BallTree &lValue) :
BoundingVolume(lValue)
{
std::vector<stitch::BoundingVolume *>::const_iterator itemIter=lValue.itemVector_.begin();
for (; itemIter!=lValue.itemVector_.end(); ++itemIter)
{
itemVector_.push_back( (*itemIter)->clone() );
}
std::vector<stitch::BallTree *>::const_iterator ballTreeIter=lValue.ballTreeVector_.begin();
for (; ballTreeIter!=lValue.ballTreeVector_.end(); ++ballTreeIter)
{
ballTreeVector_.push_back(new BallTree(*(*ballTreeIter)));
}
}
stitch::BallTree::~BallTree()
{
clear();
}
size_t stitch::BallTree::getNumItems() const
{
size_t numItems=itemVector_.size();
std::vector<stitch::BallTree *>::const_iterator ballTreeIter=ballTreeVector_.begin();
for (; ballTreeIter!=ballTreeVector_.end(); ++ballTreeIter)
{
numItems+=(*ballTreeIter)->getNumItems();
}
return numItems;
}
void stitch::BallTree::clear()
{
centre_=Vec3(0.0f, 0.0f, 0.0f);
radiusBV_=((float)FLT_MAX);
std::vector<stitch::BoundingVolume *>::const_iterator itemIter=itemVector_.begin();
for (; itemIter!=itemVector_.end(); ++itemIter)
{
delete (*itemIter);
}
itemVector_.clear();
std::vector<stitch::BallTree *>::const_iterator ballTreeIter=ballTreeVector_.begin();
for (; ballTreeIter!=ballTreeVector_.end(); ++ballTreeIter)
{
(*ballTreeIter)->clear();
delete (*ballTreeIter);
}
ballTreeVector_.clear();
}
void stitch::BallTree::linearise()
{//Collect all items into a linear list and delete the tree structure...
std::cout << "BallTree::linearise not implemented yet!\n";
std::cout.flush();
exit(-1);
}
void stitch::BallTree::build(const size_t chunkSize, const uint8_t splitAxis)
{
//New potential child trees.
BallTree *tree0=new BallTree;
BallTree *tree1=new BallTree;
//=== Find centre of items ===
Vec3 c;//Initially set to zero by vector implementation.
float dx=0.0f, dy=0.0f, dz=0.0f;
if (itemVector_.size()>0)
{
{
std::vector<stitch::BoundingVolume *>::const_iterator constItemIter=itemVector_.begin();
for (; constItemIter!=itemVector_.end(); ++constItemIter)
{//Do a linear search through the items.
c+=(*constItemIter)->centre_;
}
}
c*=1.0f/itemVector_.size();
float minx, maxx;
float miny, maxy;
float minz, maxz;
minx=maxx=c.x();
miny=maxy=c.y();
minz=maxz=c.z();
{
std::vector<stitch::BoundingVolume *>::const_iterator constItemIter=itemVector_.begin();
for (; constItemIter!=itemVector_.end(); ++constItemIter)
{//Do a linear search through the items.
if ((*constItemIter)->centre_.x()<minx)
{
minx=(*constItemIter)->centre_.x();
} else
if ((*constItemIter)->centre_.x()>maxx)
{
maxx=(*constItemIter)->centre_.x();
}
if ((*constItemIter)->centre_.y()<miny)
{
miny=(*constItemIter)->centre_.y();
} else
if ((*constItemIter)->centre_.y()>maxy)
{
maxy=(*constItemIter)->centre_.y();
}
if ((*constItemIter)->centre_.z()<minz)
{
minz=(*constItemIter)->centre_.z();
} else
if ((*constItemIter)->centre_.z()>maxz)
{
maxz=(*constItemIter)->centre_.z();
}
}
}
dx=maxx-minx;
dy=maxy-miny;
dz=maxz-minz;
}
//==============================
Plane binarySpacePartition(Vec3(0.0f, 0.0f, 0.0f), 0.0f);
if ((dx>=dy)&&(dx>=dz))
{
binarySpacePartition=Plane(Vec3(1.0f, 0.0f, 0.0f), c.x());//right-hand space.
} else
if (dy>=dz)
{
binarySpacePartition=Plane(Vec3(0.0f, 1.0f, 0.0f), c.y());//right-hand space.
} else
{
binarySpacePartition=Plane(Vec3(0.0f, 0.0f, 1.0f), c.z());//right-hand space.
}
//OR
/*
if (splitAxis==0)
{
binarySpacePartition=Plane(Vec3(1.0f, 0.0f, 0.0f), c.x());//right-hand space.
} else
if (splitAxis==1)
{
binarySpacePartition=Plane(Vec3(0.0f, 1.0f, 0.0f), c.y());//right-hand space.
} else
{
binarySpacePartition=Plane(Vec3(0.0f, 0.0f, 1.0f), c.z());//right-hand space.
}
*/
//=== Split items into two brances according to the binary space partition plane.
{
std::vector<BoundingVolume *>::const_iterator itemIter=itemVector_.begin();
size_t count=0;
for (; itemIter!=itemVector_.end(); ++itemIter)
{
BoundingVolume *item=(*itemIter);
if (item->centre_*binarySpacePartition.normal_ < binarySpacePartition.d_)
{//Item is in space of tree0.
tree0->addItem(item);
}
else if (item->centre_*binarySpacePartition.normal_ > binarySpacePartition.d_)
{//item is in space of tree1.
tree1->addItem(item);
} else
{//Place item in one of the two child trees.
if (count&1)
{
tree0->addItem(item);
} else
{
tree1->addItem(item);
}
}
++count;
}
//itemVector_.clear();//Does not delete the items pointed to by the vector entries.
std::vector<stitch::BoundingVolume *>().swap(itemVector_);//swap with new empty vector.
}
//===
//=== Add child trees to this parent and build the child hierarchy ===
{
if (tree0->itemVector_.size()>0)
{
ballTreeVector_.push_back(tree0);//Add child tree. There can be more than two child trees if the build mehod is called multiple times.
}
if (tree0->itemVector_.size()>chunkSize)
{
tree0->build(chunkSize, (splitAxis+1)%3);//Recursively build the tree.
}
if (tree1->itemVector_.size()>0)
{
ballTreeVector_.push_back(tree1);//Add child tree. There can be more than two child trees if the build mehod is called multiple times.
}
if (tree1->itemVector_.size()>chunkSize)
{
tree1->build(chunkSize, (splitAxis+1)%3);//Recursively build the tree.
}
}
//====================================================================
}
void stitch::BallTree::updateBV()
{//! @todo Should be done in one traversal of the tree!
std::vector<stitch::BoundingVolume *>::const_iterator constItemIter=itemVector_.begin();
centre_.setZeros();
radiusBV_=0.0f;
size_t numBoundingSpheres=0;
//=== Traversal 1: Find boundingSphereCentre_ ===//
std::vector<stitch::BallTree *>::const_iterator constBallTreeIter=ballTreeVector_.begin();
for (; constBallTreeIter!=ballTreeVector_.end(); ++constBallTreeIter)
{//Do a linear search through the ballTrees.
stitch::BallTree *ballTree=*constBallTreeIter;
ballTree->updateBV();
centre_+=ballTree->centre_;
++numBoundingSpheres;
}
for (; constItemIter!=itemVector_.end(); ++constItemIter)
{//Do a linear search through the items.
stitch::BoundingVolume *item=*constItemIter;
centre_+=item->centre_;
++numBoundingSpheres;
}
centre_*=1.0f/numBoundingSpheres;
//=================================//
//=== Traversal 2: Find boundingSphereRadius_ ===//
constBallTreeIter=ballTreeVector_.begin();
for (; constBallTreeIter!=ballTreeVector_.end(); ++constBallTreeIter)
{//Do a linear search through the ballTrees.
stitch::BallTree *ballTree=*constBallTreeIter;
float r=Vec3::calcDistToPoint(ballTree->centre_, centre_)+ballTree->radiusBV_;
if (r>radiusBV_)
{
radiusBV_=r;
}
}
constItemIter=itemVector_.begin();
for (; constItemIter!=itemVector_.end(); ++constItemIter)
{//Do a linear search through the items.
stitch::BoundingVolume *item=*constItemIter;
float r=Vec3::calcDistToPoint(item->centre_, centre_)+item->radiusBV_;
if (r>radiusBV_)
{
radiusBV_=r;
}
}
//==================================//
}
void stitch::BallTree::calcIntersection(const Ray &ray, Intersection &intersect) const
{
//if (BVIntersected(orig, normDir)) This is currently executed by the calling code!
{
//=== 1) Find closest ray-item intersection. ===
for (const auto itemPtr : itemVector_)
{//Do a linear search through the items stored in this tree node.
if (itemPtr->BVIntersected(ray))
{
itemPtr->calcIntersection(ray, intersect);
}
}
//============================================
//=== 2) Continue closest ray-item intersection to the items stored in the tree children. ===
for (const auto balltreePtr : ballTreeVector_)
{//Do a linear search through the child trees.
if (balltreePtr->BVIntersected(ray))
{
balltreePtr->calcIntersection(ray, intersect);
}
}
//============================================
}
}
#ifdef USE_OSG
osg::ref_ptr<osg::Node> stitch::BallTree::constructOSGNode(const bool createOSGLineGeometry, const bool createOSGNormalGeometry, const bool wireframe, const uintptr_t key) const
{
osg::ref_ptr<osg::Group> osgGroup=new osg::Group();
for (auto item : itemVector_)
{
osgGroup->addChild(item->constructOSGNode(createOSGLineGeometry, createOSGNormalGeometry, wireframe, key));
}
for (auto ballTree : ballTreeVector_)
{
osgGroup->addChild(ballTree->constructOSGNode(createOSGLineGeometry, createOSGNormalGeometry, wireframe, key));
}
return osgGroup;
}
osg::ref_ptr<osg::Node> stitch::BallTree::constructOSGBVNode() const
{
osg::ref_ptr<osg::Group> osgGroup=new osg::Group();
std::vector<stitch::BoundingVolume *>::const_iterator itemIter=itemVector_.begin();
for (; itemIter!=itemVector_.end(); ++itemIter)
{
osgGroup->addChild(constructOSGNode_Sphere(VecN((*itemIter)->centre_), (*itemIter)->radiusBV_,
true,
uintptr_t (*itemIter)));
}
std::vector<stitch::BallTree *>::const_iterator ballTreeIter=ballTreeVector_.begin();
for (; ballTreeIter!=ballTreeVector_.end(); ++ballTreeIter)
{
osgGroup->addChild(constructOSGNode_Sphere(VecN((*ballTreeIter)->centre_), (*ballTreeIter)->radiusBV_,
true));
osgGroup->addChild((*ballTreeIter)->constructOSGBVNode());
}
return osgGroup;
}
#endif// USE_OSG