1 | #include "Plane3.h" |
---|
2 | #include "ViewCellBsp.h" |
---|
3 | #include "Mesh.h" |
---|
4 | #include "common.h" |
---|
5 | #include "ViewCell.h" |
---|
6 | #include "Environment.h" |
---|
7 | #include "Polygon3.h" |
---|
8 | #include "Ray.h" |
---|
9 | #include "AxisAlignedBox3.h" |
---|
10 | #include <stack> |
---|
11 | #include <time.h> |
---|
12 | #include <iomanip> |
---|
13 | #include "Exporter.h" |
---|
14 | |
---|
15 | #define INITIAL_COST 999999// unreachable high initial cost for heuristic evaluation |
---|
16 | |
---|
17 | int BspTree::sTermMaxPolygons = 10; |
---|
18 | int BspTree::sTermMaxDepth = 20; |
---|
19 | int BspTree::sSplitPlaneStrategy = NEXT_POLYGON; |
---|
20 | int BspTree::sMaxCandidates = 10; |
---|
21 | int BspTree::sConstructionMethod = VIEW_CELLS; |
---|
22 | |
---|
23 | /** Evaluates split plane classification with respect to the plane's |
---|
24 | contribution for a balanced tree. |
---|
25 | */ |
---|
26 | int BspTree::sLeastSplitsTable[4] = {0, 0, 1, 0}; |
---|
27 | /** Evaluates split plane classification with respect to the plane's |
---|
28 | contribution for a minimum number splits in the tree. |
---|
29 | */ |
---|
30 | int BspTree::sBalancedTreeTable[4] = {-1, 1, 0, 0}; |
---|
31 | |
---|
32 | /****************************************************************/ |
---|
33 | /* class BspNode implementation */ |
---|
34 | /****************************************************************/ |
---|
35 | |
---|
36 | BspNode::BspNode(): mParent(NULL) |
---|
37 | {} |
---|
38 | |
---|
39 | BspNode::BspNode(BspInterior *parent): mParent(parent) |
---|
40 | {} |
---|
41 | |
---|
42 | |
---|
43 | bool BspNode::IsRoot() const |
---|
44 | { |
---|
45 | return mParent == NULL; |
---|
46 | } |
---|
47 | |
---|
48 | BspInterior *BspNode::GetParent() |
---|
49 | { |
---|
50 | return mParent; |
---|
51 | } |
---|
52 | |
---|
53 | void BspNode::SetParent(BspInterior *parent) |
---|
54 | { |
---|
55 | mParent = parent; |
---|
56 | } |
---|
57 | |
---|
58 | PolygonContainer *BspNode::GetPolygons() |
---|
59 | { |
---|
60 | return &mPolygons; |
---|
61 | } |
---|
62 | |
---|
63 | void BspNode::AddPolygons(PolygonContainer *polys) |
---|
64 | { |
---|
65 | while (!polys->empty()) |
---|
66 | { |
---|
67 | mPolygons.push_back(polys->back()); |
---|
68 | polys->pop_back(); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | /****************************************************************/ |
---|
74 | /* class BspInterior implementation */ |
---|
75 | /****************************************************************/ |
---|
76 | |
---|
77 | |
---|
78 | BspInterior::BspInterior(const Plane3 &plane): |
---|
79 | mPlane(plane), mFront(NULL), mBack(NULL) |
---|
80 | {} |
---|
81 | |
---|
82 | bool BspInterior::IsLeaf() const |
---|
83 | { |
---|
84 | return false; |
---|
85 | } |
---|
86 | |
---|
87 | BspNode *BspInterior::GetBack() |
---|
88 | { |
---|
89 | return mBack; |
---|
90 | } |
---|
91 | |
---|
92 | BspNode *BspInterior::GetFront() |
---|
93 | { |
---|
94 | return mFront; |
---|
95 | } |
---|
96 | |
---|
97 | Plane3 *BspInterior::GetPlane() |
---|
98 | { |
---|
99 | return &mPlane; |
---|
100 | } |
---|
101 | |
---|
102 | void BspInterior::ReplaceChildLink(BspNode *oldChild, BspNode *newChild) |
---|
103 | { |
---|
104 | if (mBack == oldChild) |
---|
105 | { |
---|
106 | mBack = newChild; |
---|
107 | } |
---|
108 | else |
---|
109 | { |
---|
110 | mFront = newChild; |
---|
111 | } |
---|
112 | }
|
---|
113 |
|
---|
114 | void BspInterior::SetupChildLinks(BspNode *b, BspNode *f)
|
---|
115 | {
|
---|
116 | mBack = b;
|
---|
117 | mFront = f;
|
---|
118 | }
|
---|
119 | |
---|
120 | void BspInterior::ProcessPolygon(Polygon3 *poly, bool storePolys) |
---|
121 | { |
---|
122 | if (storePolys)
|
---|
123 | mPolygons.push_back(poly);
|
---|
124 | else
|
---|
125 | delete poly; |
---|
126 | } |
---|
127 | |
---|
128 | void BspInterior::SplitPolygons(PolygonContainer *polys, |
---|
129 | PolygonContainer *frontPolys, |
---|
130 | PolygonContainer *backPolys, |
---|
131 | int &splits, bool storePolys) |
---|
132 | {
|
---|
133 | while (!polys->empty())
|
---|
134 | {
|
---|
135 | Polygon3 *poly = polys->back();
|
---|
136 | polys->pop_back();
|
---|
137 |
|
---|
138 | // test if split is neccessary
|
---|
139 | int result = poly->ClassifyPlane(mPlane);
|
---|
140 |
|
---|
141 | Polygon3 *front_piece = NULL;
|
---|
142 | Polygon3 *back_piece = NULL;
|
---|
143 |
|
---|
144 | switch (result)
|
---|
145 | {
|
---|
146 | case Polygon3::COINCIDENT:
|
---|
147 | // discard polygons or saves them in node
|
---|
148 | ProcessPolygon(poly, storePolys);
|
---|
149 | break;
|
---|
150 | case Polygon3::FRONT_SIDE:
|
---|
151 | frontPolys->push_back(poly);
|
---|
152 | break;
|
---|
153 | case Polygon3::BACK_SIDE:
|
---|
154 | backPolys->push_back(poly);
|
---|
155 | break;
|
---|
156 | case Polygon3::SPLIT:
|
---|
157 | front_piece = new Polygon3();
|
---|
158 | back_piece = new Polygon3();
|
---|
159 |
|
---|
160 | //-- split polygon
|
---|
161 | poly->Split(&mPlane, front_piece, back_piece, splits);
|
---|
162 |
|
---|
163 | frontPolys->push_back(front_piece);
|
---|
164 | backPolys->push_back(back_piece);
|
---|
165 |
|
---|
166 | #ifdef _DEBUG |
---|
167 | Debug << "split " << *poly << endl << *front_piece << endl << *back_piece << endl; |
---|
168 | #endif
|
---|
169 | // save or discard polygons
|
---|
170 | ProcessPolygon(poly, storePolys);
|
---|
171 |
|
---|
172 | break;
|
---|
173 | default:
|
---|
174 | Debug << "SHOULD NEVER COME HERE\n";
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | } |
---|
178 | |
---|
179 | // contains nothing |
---|
180 | delete polys; |
---|
181 | } |
---|
182 | |
---|
183 | /****************************************************************/ |
---|
184 | /* class BspLeaf implementation */ |
---|
185 | /****************************************************************/ |
---|
186 | BspLeaf::BspLeaf(): mViewCell(NULL) |
---|
187 | { |
---|
188 | } |
---|
189 | |
---|
190 | BspLeaf::BspLeaf(ViewCell *viewCell): mViewCell(viewCell) |
---|
191 | { |
---|
192 | } |
---|
193 | |
---|
194 | BspLeaf::BspLeaf(BspInterior *parent): BspNode(parent), mViewCell(NULL) |
---|
195 | {} |
---|
196 | |
---|
197 | ViewCell *BspLeaf::GetViewCell() |
---|
198 | { |
---|
199 | return mViewCell; |
---|
200 | } |
---|
201 | |
---|
202 | void BspLeaf::SetViewCell(ViewCell *viewCell) |
---|
203 | { |
---|
204 | mViewCell = viewCell; |
---|
205 | } |
---|
206 | |
---|
207 | bool BspLeaf::IsLeaf() const |
---|
208 | { |
---|
209 | return true; |
---|
210 | } |
---|
211 | |
---|
212 | /****************************************************************/ |
---|
213 | /* class BspTree implementation */ |
---|
214 | /****************************************************************/ |
---|
215 | |
---|
216 | BspTree::BspTree(): |
---|
217 | mTermMaxPolygons(0), |
---|
218 | mTermMaxDepth(0), |
---|
219 | mRoot(NULL), |
---|
220 | mIsIncremential(false), |
---|
221 | mStorePolys(true) |
---|
222 | { |
---|
223 | Randomize(); // initialise random generator for heuristics |
---|
224 | }
|
---|
225 |
|
---|
226 | const BspTreeStatistics &BspTree::GetStatistics() const |
---|
227 | { |
---|
228 | return mStat;
|
---|
229 | }
|
---|
230 |
|
---|
231 | void BspTreeStatistics::Print(ostream &app) const |
---|
232 | { |
---|
233 | app << "===== BspTree statistics ===============\n"; |
---|
234 | |
---|
235 | app << setprecision(4); |
---|
236 | |
---|
237 | app << "#N_RAYS Number of rays )\n" |
---|
238 | << rays << endl; |
---|
239 | app << "#N_DOMAINS ( Number of query domains )\n" |
---|
240 | << queryDomains <<endl; |
---|
241 | |
---|
242 | app << "#N_NODES ( Number of nodes )\n" << nodes << "\n"; |
---|
243 | |
---|
244 | app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n"; |
---|
245 | |
---|
246 | app << "#N_SPLITS ( Number of splits)\n" << splits << "\n"; |
---|
247 | |
---|
248 | app << "#N_RAYREFS ( Number of rayRefs )\n" << |
---|
249 | rayRefs << "\n"; |
---|
250 | |
---|
251 | app << "#N_RAYRAYREFS ( Number of rayRefs / ray )\n" << |
---|
252 | rayRefs/(double)rays << "\n"; |
---|
253 | |
---|
254 | app << "#N_LEAFRAYREFS ( Number of rayRefs / leaf )\n" << |
---|
255 | rayRefs/(double)Leaves() << "\n"; |
---|
256 | |
---|
257 | app << "#N_MAXOBJECTREFS ( Max number of rayRefs / leaf )\n" << |
---|
258 | maxObjectRefs << "\n"; |
---|
259 | |
---|
260 | app << "#N_NONEMPTYRAYREFS ( Number of rayRefs in nonEmpty leaves / non empty leaf )\n" << |
---|
261 | rayRefsNonZeroQuery/(double)(Leaves() - zeroQueryNodes) << "\n"; |
---|
262 | |
---|
263 | app << "#N_LEAFDOMAINREFS ( Number of query domain Refs / leaf )\n" << |
---|
264 | objectRefs/(double)Leaves() << "\n"; |
---|
265 | |
---|
266 | app << "#N_PEMPTYLEAVES ( Percentage of leaves with zero query domains )\n"<< |
---|
267 | zeroQueryNodes*100/(double)Leaves() << endl; |
---|
268 | |
---|
269 | app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maxdepth )\n"<< |
---|
270 | maxDepthNodes * 100 / (double)Leaves() << endl; |
---|
271 | |
---|
272 | app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << maxDepth <<endl; |
---|
273 | |
---|
274 | app << "#N_ADDED_RAYREFS (Number of dynamically added ray references )\n"<< |
---|
275 | addedRayRefs<<endl; |
---|
276 | |
---|
277 | app << "#N_REMOVED_RAYREFS (Number of dynamically removed ray references )\n"<< |
---|
278 | removedRayRefs<<endl; |
---|
279 | |
---|
280 | // app << setprecision(4); |
---|
281 | |
---|
282 | // app << "#N_CTIME ( Construction time [s] )\n" |
---|
283 | // << Time() << " \n"; |
---|
284 | |
---|
285 | app << "===== END OF BspTree statistics ==========\n"; |
---|
286 | }
|
---|
287 |
|
---|
288 | BspTree::~BspTree()
|
---|
289 | {
|
---|
290 | std::stack<BspNode *> tStack;
|
---|
291 |
|
---|
292 | tStack.push(mRoot);
|
---|
293 |
|
---|
294 | while (!tStack.empty())
|
---|
295 | {
|
---|
296 | BspNode *node = tStack.top();
|
---|
297 |
|
---|
298 | tStack.pop();
|
---|
299 |
|
---|
300 | /// if we stored the polygons
|
---|
301 | CLEAR_CONTAINER(node->mPolygons);
|
---|
302 |
|
---|
303 | if (!node->IsLeaf())
|
---|
304 | {
|
---|
305 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
306 |
|
---|
307 | // push the children on the stack (there are always two children)
|
---|
308 | tStack.push(interior->GetBack());
|
---|
309 | tStack.push(interior->GetFront());
|
---|
310 | }
|
---|
311 |
|
---|
312 | DEL_PTR(node);
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | void BspTree::InsertViewCell(ViewCell *viewCell)
|
---|
318 | {
|
---|
319 | std::stack<BspTraversalData> tStack;
|
---|
320 | Debug << "getting polygons" << endl; Debug.flush();
|
---|
321 | PolygonContainer *polys = new PolygonContainer();
|
---|
322 |
|
---|
323 | // extract polygons that guide the split process
|
---|
324 | AddMesh2Polygons(viewCell->GetMesh(), *polys);
|
---|
325 | mBox.Include(viewCell->GetBox()); // add to BSP aabb
|
---|
326 |
|
---|
327 | // traverse tree or create new one
|
---|
328 | BspNode *firstNode = mRoot ? mRoot : new BspLeaf();
|
---|
329 |
|
---|
330 | tStack.push(BspTraversalData(firstNode, NULL, polys, 0));
|
---|
331 |
|
---|
332 | Debug << "starting traversal" << endl; Debug.flush();
|
---|
333 | while (!tStack.empty())
|
---|
334 | {
|
---|
335 | Debug << "new traversal step" << endl; Debug.flush();
|
---|
336 |
|
---|
337 | // filter polygons donw the tree
|
---|
338 | BspTraversalData tData = tStack.top();
|
---|
339 | tStack.pop();
|
---|
340 |
|
---|
341 | Debug << "popped stack" << endl; Debug.flush();
|
---|
342 | if (!tData.mNode)
|
---|
343 | Debug << "ERROR NO NODE\n"; Debug.flush();
|
---|
344 | if (!tData.mNode->IsLeaf())
|
---|
345 | {
|
---|
346 | Debug << "no leaf" << endl; Debug.flush();
|
---|
347 | BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
|
---|
348 |
|
---|
349 | //-- filter view cell polygons down the tree until a leaf is reached
|
---|
350 | PolygonContainer *frontPolys = new PolygonContainer();
|
---|
351 | PolygonContainer *backPolys = new PolygonContainer();
|
---|
352 |
|
---|
353 | int splits = 0;
|
---|
354 |
|
---|
355 | if ((int)tData.mPolygons->size() > 0)
|
---|
356 | {
|
---|
357 | Debug << "traversing down tree" << endl; Debug.flush();
|
---|
358 | // split viecell polygons with respect to split plane
|
---|
359 | interior->SplitPolygons(tData.mPolygons, frontPolys, backPolys, splits, mStorePolys);
|
---|
360 | Debug << "polygon splitted" << endl; Debug.flush();
|
---|
361 | //Debug << "Reached level " << tData.mDepth << " bk: " << backPolys->size() << " frnt: " << frontPolys->size() << endl;
|
---|
362 | mStat.splits += splits;
|
---|
363 |
|
---|
364 | // push the children on the stack
|
---|
365 | tStack.push(BspTraversalData(interior->GetFront(), interior->GetParent(),
|
---|
366 | frontPolys, tData.mDepth + 1));
|
---|
367 |
|
---|
368 | tStack.push(BspTraversalData(interior->GetBack(), interior->GetParent(),
|
---|
369 | backPolys, tData.mDepth + 1));
|
---|
370 |
|
---|
371 | }
|
---|
372 | else
|
---|
373 | {
|
---|
374 | Debug << "deleting data" << endl; Debug.flush();
|
---|
375 | DEL_PTR(tData.mPolygons);
|
---|
376 | Debug << "deleted data" << endl; Debug.flush();
|
---|
377 | }
|
---|
378 | }
|
---|
379 | else // reached leaf => subdivide current viewcell
|
---|
380 | {
|
---|
381 | Debug << "reached leaf" << endl; Debug.flush();
|
---|
382 | BspNode *root = Subdivide(tStack, tData, viewCell);
|
---|
383 |
|
---|
384 | // tree empty => new root
|
---|
385 | if (!mRoot)
|
---|
386 | mRoot = root;
|
---|
387 | Debug << "subdivision done" << endl; Debug.flush();
|
---|
388 | }
|
---|
389 | Debug << "ended one traversal step" << endl; Debug.flush();
|
---|
390 | }
|
---|
391 | Debug << "ended traversal" << endl; Debug.flush();
|
---|
392 | }
|
---|
393 |
|
---|
394 | void BspTree::InitTree(int maxPolygons, int maxDepth)
|
---|
395 | {
|
---|
396 | mTermMaxPolygons = maxPolygons;
|
---|
397 | mTermMaxDepth = maxDepth;
|
---|
398 | mStat.nodes = 1;
|
---|
399 |
|
---|
400 | mBox.Initialize(); // initialise bsp tree bounding box
|
---|
401 | }
|
---|
402 |
|
---|
403 |
|
---|
404 |
|
---|
405 | void BspTree::AddMesh2Polygons(Mesh *mesh, PolygonContainer &polys)
|
---|
406 | {
|
---|
407 | FaceContainer::const_iterator fi;
|
---|
408 |
|
---|
409 | // copy the face data to polygons
|
---|
410 | for (fi = mesh->mFaces.begin(); fi != mesh->mFaces.end(); ++ fi)
|
---|
411 | {
|
---|
412 | Polygon3 *poly = new Polygon3((*fi), mesh);
|
---|
413 | polys.push_back(poly);
|
---|
414 | }
|
---|
415 | }
|
---|
416 |
|
---|
417 | void BspTree::Copy2PolygonSoup(const ViewCellContainer &viewCells, PolygonContainer &polys, int maxObjects)
|
---|
418 | {
|
---|
419 | int limit = (maxObjects > 0) ? Min((int)viewCells.size(), maxObjects) : (int)viewCells.size(); |
---|
420 | |
---|
421 | for (int i = 0; i < limit; ++i)
|
---|
422 | {
|
---|
423 | if (viewCells[i]->GetMesh()) // copy the mesh data to polygons
|
---|
424 | {
|
---|
425 | mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb
|
---|
426 | AddMesh2Polygons(viewCells[i]->GetMesh(), polys);
|
---|
427 | }
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | void BspTree::Copy2PolygonSoup(const ObjectContainer &objects, PolygonContainer &polys, int maxObjects)
|
---|
432 | {
|
---|
433 | int limit = (maxObjects > 0) ? Min((int)objects.size(), maxObjects) : (int)objects.size(); |
---|
434 | |
---|
435 | for (int i = 0; i < limit; ++i)
|
---|
436 | {
|
---|
437 | Intersectable *object = objects[i];//*it;
|
---|
438 | Mesh *mesh = NULL;
|
---|
439 |
|
---|
440 | switch (object->Type()) // extract the meshes
|
---|
441 | {
|
---|
442 | case Intersectable::MESH_INSTANCE:
|
---|
443 | mesh = dynamic_cast<MeshInstance *>(object)->GetMesh();
|
---|
444 | break;
|
---|
445 | case Intersectable::VIEWCELL:
|
---|
446 | mesh = dynamic_cast<ViewCell *>(object)->GetMesh();
|
---|
447 | break;
|
---|
448 | // TODO: handle transformed mesh instances
|
---|
449 | default:
|
---|
450 | break;
|
---|
451 | }
|
---|
452 |
|
---|
453 | if (mesh) // copy the mesh data to polygons
|
---|
454 | {
|
---|
455 | mBox.Include(object->GetBox()); // add to BSP tree aabb
|
---|
456 | AddMesh2Polygons(mesh, polys);
|
---|
457 | }
|
---|
458 | }
|
---|
459 |
|
---|
460 | Debug << "Number of polygons: " << (int)polys.size() << ", BSP root box: " << mBox << endl;
|
---|
461 | }
|
---|
462 |
|
---|
463 | void BspTree::Construct(const ViewCellContainer &viewCells)
|
---|
464 | {
|
---|
465 | //-- Construct tree using the given viewcells
|
---|
466 |
|
---|
467 | /* for this type of construction we filter all viewcells down the
|
---|
468 | * tree. If there is no polygon left, the last split plane
|
---|
469 | * decides inside or outside of the viewcell.
|
---|
470 | */
|
---|
471 | InitTree(sTermMaxPolygons, sTermMaxDepth);
|
---|
472 |
|
---|
473 | bool savedStorePolys = mStorePolys;
|
---|
474 |
|
---|
475 | // tree is completely constructed once before
|
---|
476 | // view cells are inserted one after another =>
|
---|
477 | // global tree optimization possible
|
---|
478 | if (!mIsIncremential)
|
---|
479 | {
|
---|
480 | // copy view cell meshes into one big polygon soup
|
---|
481 | PolygonContainer *polys = new PolygonContainer();
|
---|
482 | Copy2PolygonSoup(viewCells, *polys, sMaxCandidates);
|
---|
483 |
|
---|
484 | // polygons are stored only during view cell insertion
|
---|
485 | mStorePolys = false;
|
---|
486 |
|
---|
487 | // construct tree from viewcell polygons
|
---|
488 | Construct(polys);
|
---|
489 | //Export("bsp.x3d");
|
---|
490 | }
|
---|
491 |
|
---|
492 | //-- insert all viewcells
|
---|
493 | ViewCellContainer::const_iterator it;
|
---|
494 |
|
---|
495 | mStorePolys = savedStorePolys;
|
---|
496 |
|
---|
497 | int counter = 0;
|
---|
498 |
|
---|
499 | Debug << "\n**** Started view cells insertion ****\n\n";
|
---|
500 | for (it = viewCells.begin(); it != viewCells.end(); ++ it)
|
---|
501 | {
|
---|
502 | Debug << "*** Inserting view cell " << counter << " ***" << endl;
|
---|
503 | InsertViewCell(*it);
|
---|
504 | Debug << "*** view cell " << ++ counter << " inserted ***" << endl;
|
---|
505 | }
|
---|
506 | Debug << "**** finished view cells insertion ****" << endl; Debug.flush();
|
---|
507 | }
|
---|
508 |
|
---|
509 |
|
---|
510 | void BspTree::Construct(const ObjectContainer &objects, ViewCellContainer *viewCells)
|
---|
511 | {
|
---|
512 | // take termination criteria from globals
|
---|
513 | InitTree(sTermMaxPolygons, sTermMaxDepth);
|
---|
514 |
|
---|
515 | PolygonContainer *polys = new PolygonContainer();
|
---|
516 |
|
---|
517 | // copy mesh instance polygons into one big polygon soup
|
---|
518 | Copy2PolygonSoup(objects, *polys, sMaxCandidates); |
---|
519 | |
---|
520 | // construct tree from polygon soup |
---|
521 | Construct(polys); |
---|
522 | |
---|
523 | // TODO: generate view cells |
---|
524 | } |
---|
525 | |
---|
526 | void BspTree::Construct(const RayContainer &rays, ViewCellContainer *viewCells) |
---|
527 | { |
---|
528 | // TODO |
---|
529 | } |
---|
530 | |
---|
531 | void BspTree::Construct(PolygonContainer *polys)
|
---|
532 | {
|
---|
533 | std::stack<BspTraversalData> tStack;
|
---|
534 |
|
---|
535 | BspTraversalData tData(new BspLeaf(), NULL, polys, 0);
|
---|
536 |
|
---|
537 | tStack.push(tData);
|
---|
538 |
|
---|
539 | Debug << "**** Contructing tree using objects ****\n";
|
---|
540 | while (!tStack.empty())
|
---|
541 | {
|
---|
542 | tData = tStack.top();
|
---|
543 | tStack.pop();
|
---|
544 |
|
---|
545 | // subdivide leaf node
|
---|
546 | BspNode *root = Subdivide(tStack, tData); |
---|
547 | |
---|
548 | // empty tree => new root corresponding to unbounded space |
---|
549 | if (!mRoot) |
---|
550 | mRoot = root; |
---|
551 | }
|
---|
552 |
|
---|
553 | Debug << "**** tree construction finished ****\n";
|
---|
554 | }
|
---|
555 | |
---|
556 | |
---|
557 | BspNode * BspTree::Subdivide(BspTraversalStack &tStack, |
---|
558 | BspTraversalData &tData, |
---|
559 | ViewCell *viewCell) |
---|
560 | {
|
---|
561 | Debug << "subdividing" << endl; Debug.flush();
|
---|
562 | PolygonContainer *backPolys = new PolygonContainer();
|
---|
563 | PolygonContainer *frontPolys = new PolygonContainer();
|
---|
564 |
|
---|
565 | BspNode *node = SubdivideNode(dynamic_cast<BspLeaf *>(tData.mNode),
|
---|
566 | tData.mParent,
|
---|
567 | tData.mPolygons,
|
---|
568 | tData.mDepth,
|
---|
569 | frontPolys,
|
---|
570 | backPolys);
|
---|
571 |
|
---|
572 | if (!node->IsLeaf()) // node was subdivided
|
---|
573 | {
|
---|
574 | Debug << "node was subdivided" << endl; Debug.flush();
|
---|
575 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
576 |
|
---|
577 | // push the children on the stack (there are always two children)
|
---|
578 | tStack.push(BspTraversalData(interior->GetBack(), interior, backPolys, tData.mDepth + 1));
|
---|
579 | tStack.push(BspTraversalData(interior->GetFront(), interior, frontPolys, tData.mDepth + 1));
|
---|
580 | }
|
---|
581 | else // traversal terminates
|
---|
582 | {
|
---|
583 | Debug << "eval stats" << endl; Debug.flush();
|
---|
584 | EvaluateLeafStats(tData);
|
---|
585 |
|
---|
586 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
587 |
|
---|
588 | // add view cell if in negative halfspace (== inside object)
|
---|
589 | // or if there is still geometry left
|
---|
590 | if (viewCell &&
|
---|
591 | (!node->GetParent() || (node == node->GetParent()->GetBack()) ||
|
---|
592 | (tData.mPolygons->size() > 0)))
|
---|
593 | {
|
---|
594 | Debug << "** view cell inserted **\n";
|
---|
595 | leaf->SetViewCell(viewCell);
|
---|
596 | }
|
---|
597 |
|
---|
598 | Debug << "storing or deleting polygons: " << mStorePolys << endl; Debug.flush();
|
---|
599 | // add or delete remaining polygons |
---|
600 | if (mStorePolys) |
---|
601 | leaf->AddPolygons(tData.mPolygons); |
---|
602 | else |
---|
603 | CLEAR_CONTAINER(*tData.mPolygons); |
---|
604 | |
---|
605 | DEL_PTR(tData.mPolygons);
|
---|
606 | Debug << "deleted polygon container" << endl; Debug.flush();
|
---|
607 | }
|
---|
608 |
|
---|
609 | return node;
|
---|
610 | } |
---|
611 | |
---|
612 | |
---|
613 | BspNode *BspTree::SubdivideNode(BspLeaf *leaf,
|
---|
614 | BspInterior *parent,
|
---|
615 | PolygonContainer *polys,
|
---|
616 | const int depth,
|
---|
617 | PolygonContainer *frontPolys,
|
---|
618 | PolygonContainer *backPolys)
|
---|
619 | {
|
---|
620 | if (leaf->GetViewCell() && (polys->size() == 0))
|
---|
621 | Debug << "error: no distinct view cells!!!!!!!!!!!\n";
|
---|
622 |
|
---|
623 | // terminate traversal
|
---|
624 | if (//!leaf->GetViewCell() &&
|
---|
625 | ((polys->size() <= mTermMaxPolygons) || (depth >= mTermMaxDepth)))
|
---|
626 | {Debug << "subdividing node termination" << endl; Debug.flush();
|
---|
627 | return leaf;
|
---|
628 | }
|
---|
629 |
|
---|
630 | mStat.nodes += 2;
|
---|
631 |
|
---|
632 | // add the new nodes to the tree + select subdivision plane
|
---|
633 | BspInterior *node = new BspInterior(SelectPlane(polys));
|
---|
634 |
|
---|
635 | #ifdef _DEBUG
|
---|
636 | Debug << node << endl;
|
---|
637 | #endif
|
---|
638 |
|
---|
639 | // split polygon according to current plane
|
---|
640 | int splits = 0;
|
---|
641 |
|
---|
642 | Debug << "splipoly" << endl; Debug.flush();
|
---|
643 | node->SplitPolygons(polys, frontPolys, backPolys, splits, mStorePolys);
|
---|
644 |
|
---|
645 | mStat.splits += splits;
|
---|
646 |
|
---|
647 | // two new leaves
|
---|
648 | BspLeaf *back = new BspLeaf(node);
|
---|
649 | BspLeaf *front = new BspLeaf(node);
|
---|
650 |
|
---|
651 | // replace a link from node's parent
|
---|
652 | if (parent)
|
---|
653 | parent->ReplaceChildLink(leaf, node);
|
---|
654 |
|
---|
655 | // and setup child links
|
---|
656 | node->SetupChildLinks(back, front);
|
---|
657 |
|
---|
658 | DEL_PTR(leaf); // leaf not member of tree anymore
|
---|
659 |
|
---|
660 | return node;
|
---|
661 | } |
---|
662 | |
---|
663 | Plane3 BspTree::SelectPlane(PolygonContainer *polys) const |
---|
664 | { |
---|
665 | if (polys->size() == 0) |
---|
666 | { |
---|
667 | Debug << "Warning: No split candidate available\n"; |
---|
668 | return Plane3(); |
---|
669 | } |
---|
670 | |
---|
671 | // simple strategy: just take next polygon |
---|
672 | if (sSplitPlaneStrategy == NEXT_POLYGON) |
---|
673 | { |
---|
674 | return polys->front()->GetSupportingPlane(); |
---|
675 | } |
---|
676 | |
---|
677 | // use heuristics to find appropriate plane |
---|
678 | return SelectPlaneHeuristics(polys, sMaxCandidates); |
---|
679 | } |
---|
680 | |
---|
681 | Plane3 BspTree::SelectPlaneHeuristics(PolygonContainer *polygons, int maxTests) const |
---|
682 | { |
---|
683 | int lowestCost = INITIAL_COST; |
---|
684 | Plane3 *bestPlane = NULL; |
---|
685 | |
---|
686 | int limit = Min((int)polygons->size(), maxTests); |
---|
687 | |
---|
688 | for (int i = 0; i < limit; ++i) |
---|
689 | { |
---|
690 | int candidateIdx = Random((int)polygons->size()); |
---|
691 | Plane3 candidatePlane = (*polygons)[candidateIdx]->GetSupportingPlane(); |
---|
692 | |
---|
693 | // evaluate current candidate |
---|
694 | int candidateCost = EvalSplitPlane(polygons, candidatePlane); |
---|
695 | |
---|
696 | if (candidateCost < lowestCost) |
---|
697 | { |
---|
698 | bestPlane = &candidatePlane; |
---|
699 | lowestCost = candidateCost; |
---|
700 | //Debug << "new plane cost " << lowestCost << endl; |
---|
701 | } |
---|
702 | } |
---|
703 | |
---|
704 | return *bestPlane; |
---|
705 | } |
---|
706 | |
---|
707 | int BspTree::EvalSplitPlane(PolygonContainer *polygons, const Plane3 &candidatePlane) const |
---|
708 | { |
---|
709 | PolygonContainer::const_iterator it; |
---|
710 | |
---|
711 | int sum = 0, sum2 = 0; |
---|
712 | |
---|
713 | for (it = polygons->begin(); it < polygons->end(); ++ it) |
---|
714 | { |
---|
715 | int classification = (*it)->ClassifyPlane(candidatePlane); |
---|
716 | |
---|
717 | if ((sSplitPlaneStrategy == BALANCED_TREE) || (sSplitPlaneStrategy == COMBINED)) |
---|
718 | { |
---|
719 | sum += sBalancedTreeTable[classification]; |
---|
720 | } |
---|
721 | |
---|
722 | if ((sSplitPlaneStrategy == LEAST_SPLITS) || (sSplitPlaneStrategy == COMBINED)) |
---|
723 | { |
---|
724 | sum2 += sLeastSplitsTable[classification]; |
---|
725 | } |
---|
726 | } |
---|
727 | |
---|
728 | // return linear combination of both sums |
---|
729 | return abs(sum) + abs(sum2); |
---|
730 | } |
---|
731 | |
---|
732 | void BspTree::ParseEnvironment()
|
---|
733 | {
|
---|
734 | environment->GetIntValue("BspTree.Termination.maxDepth", sTermMaxDepth);
|
---|
735 | environment->GetIntValue("BspTree.Termination.maxPolygons", sTermMaxPolygons);
|
---|
736 | environment->GetIntValue("BspTree.maxCandidates", sMaxCandidates);
|
---|
737 |
|
---|
738 | //-- extract strategy to choose the next split plane
|
---|
739 | char splitPlaneStrategyStr[60];
|
---|
740 |
|
---|
741 | environment->GetStringValue("BspTree.splitPlaneStrategy", splitPlaneStrategyStr);
|
---|
742 | |
---|
743 | sSplitPlaneStrategy = BspTree::NEXT_POLYGON; |
---|
744 | |
---|
745 | if (strcmp(splitPlaneStrategyStr, "nextPolygon") == 0) |
---|
746 | sSplitPlaneStrategy = BspTree::NEXT_POLYGON; |
---|
747 | else if (strcmp(splitPlaneStrategyStr, "leastSplits") == 0) |
---|
748 | sSplitPlaneStrategy = BspTree::LEAST_SPLITS; |
---|
749 | else if (strcmp(splitPlaneStrategyStr, "balancedTree") == 0) |
---|
750 | sSplitPlaneStrategy = BspTree::BALANCED_TREE; |
---|
751 | else |
---|
752 | { |
---|
753 | cerr << "Wrong BSP split plane strategy " << splitPlaneStrategyStr << endl; |
---|
754 | exit(1); |
---|
755 | }
|
---|
756 |
|
---|
757 | //-- parse BSP tree construction method |
---|
758 | char constructionMethodStr[64]; |
---|
759 | |
---|
760 | environment->GetStringValue("BspTree.constructionMethod", constructionMethodStr); |
---|
761 | |
---|
762 | sConstructionMethod = BspTree::VIEW_CELLS; |
---|
763 | |
---|
764 | if (strcmp(constructionMethodStr, "viewCells") == 0) |
---|
765 | sConstructionMethod = BspTree::VIEW_CELLS; |
---|
766 | else if (strcmp(constructionMethodStr, "sceneGeometry") == 0) |
---|
767 | sConstructionMethod = BspTree::SCENE_GEOMETRY; |
---|
768 | else if (strcmp(constructionMethodStr, "rays") == 0) |
---|
769 | sConstructionMethod = BspTree::RAYS; |
---|
770 | else |
---|
771 | { |
---|
772 | cerr << "Wrong bsp construction method " << constructionMethodStr << endl; |
---|
773 | exit(1); |
---|
774 | } |
---|
775 |
|
---|
776 | }
|
---|
777 |
|
---|
778 | void BspTree::CollectLeaves(vector<BspLeaf *> &leaves) |
---|
779 | { |
---|
780 | stack<BspNode *> nodeStack; |
---|
781 | nodeStack.push(mRoot); |
---|
782 | |
---|
783 | while (!nodeStack.empty()) |
---|
784 | { |
---|
785 | BspNode *node = nodeStack.top(); |
---|
786 | |
---|
787 | nodeStack.pop(); |
---|
788 | |
---|
789 | if (node->IsLeaf()) |
---|
790 | { |
---|
791 | BspLeaf *leaf = (BspLeaf *)node; |
---|
792 | |
---|
793 | leaves.push_back(leaf); |
---|
794 | } else |
---|
795 | { |
---|
796 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
797 | nodeStack.push(interior->GetBack()); |
---|
798 | nodeStack.push(interior->GetFront()); |
---|
799 | } |
---|
800 | } |
---|
801 | }
|
---|
802 |
|
---|
803 | int BspTree::CollectLeafPvs() |
---|
804 | { |
---|
805 | int totalPvsSize = 0; |
---|
806 | |
---|
807 | stack<BspNode *> nodeStack; |
---|
808 | |
---|
809 | nodeStack.push(mRoot); |
---|
810 | |
---|
811 | while (!nodeStack.empty()) |
---|
812 | { |
---|
813 | BspNode *node = nodeStack.top(); |
---|
814 | |
---|
815 | nodeStack.pop(); |
---|
816 | |
---|
817 | if (node->IsLeaf()) |
---|
818 | { |
---|
819 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); |
---|
820 | |
---|
821 | ViewCell *viewcell = leaf->GetViewCell(); |
---|
822 | |
---|
823 | if (!viewcell->Mailed()) |
---|
824 | { |
---|
825 | viewcell->Mail(); // what does mail mean? |
---|
826 | |
---|
827 | // add this node to pvs of all nodes it can see |
---|
828 | BspPvsMap::iterator ni; |
---|
829 | |
---|
830 | /* for (ni = object->mBspPvs.mEntries.begin(); ni != object->mKdPvs.mEntries.end(); ni++) |
---|
831 | { |
---|
832 | BspNode *node = (*ni).first; |
---|
833 | |
---|
834 | // $$ JB TEMPORARY solution -> should add object PVS or explictly computed |
---|
835 | // BSP tree PVS |
---|
836 | if (leaf->mBspPvs.AddNodeSample(node)) |
---|
837 | totalPvsSize++; |
---|
838 | }*/ |
---|
839 | } |
---|
840 | } else |
---|
841 | { |
---|
842 | // traverse tree |
---|
843 | BspInterior *interior = (BspInterior *)node; |
---|
844 | |
---|
845 | nodeStack.push(interior->GetFront()); |
---|
846 | nodeStack.push(interior->GetBack()); |
---|
847 | } |
---|
848 | } |
---|
849 | |
---|
850 | return totalPvsSize; |
---|
851 | }
|
---|
852 |
|
---|
853 | AxisAlignedBox3 BspTree::GetBoundingBox() const
|
---|
854 | {
|
---|
855 | return mBox;
|
---|
856 | }
|
---|
857 |
|
---|
858 | BspNode *BspTree::GetRoot() const
|
---|
859 | {
|
---|
860 | return mRoot;
|
---|
861 | }
|
---|
862 |
|
---|
863 | bool BspTree::StorePolys() const
|
---|
864 | {
|
---|
865 | return mStorePolys;
|
---|
866 | }
|
---|
867 |
|
---|
868 | void BspTree::EvaluateLeafStats(const BspTraversalData &data) |
---|
869 | { |
---|
870 | // the node became a leaf -> evaluate stats for leafs |
---|
871 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode); |
---|
872 | |
---|
873 | if (data.mDepth >= mTermMaxDepth) |
---|
874 | { |
---|
875 | ++ mStat.maxDepthNodes; |
---|
876 | } |
---|
877 | |
---|
878 | // store maximal depth |
---|
879 | if (data.mDepth > mStat.maxDepth) |
---|
880 | mStat.maxDepth = data.mDepth; |
---|
881 | |
---|
882 | #ifdef _DEBUG |
---|
883 | Debug << "BSP Traversal data. Depth: " << data.mDepth << " (max: " << mTermMaxDepth<< "), #polygons: " << |
---|
884 | data.mPolygons->size() << " (max: " << mTermMaxPolygons << ")" << endl; |
---|
885 | #endif |
---|
886 | } |
---|
887 | |
---|
888 | int BspTree::CastRay(Ray &ray) |
---|
889 | { |
---|
890 | int hits = 0; |
---|
891 | |
---|
892 | stack<BspRayTraversalData> tStack; |
---|
893 | |
---|
894 | float maxt = 1e6; |
---|
895 | float mint = 0; |
---|
896 | |
---|
897 | Intersectable::NewMail(); |
---|
898 | |
---|
899 | // test with tree bounding box |
---|
900 | if (!mBox.GetMinMaxT(ray, &mint, &maxt)) |
---|
901 | return 0; |
---|
902 | |
---|
903 | if (mint < 0) |
---|
904 | mint = 0; |
---|
905 | |
---|
906 | maxt += Limits::Threshold; |
---|
907 | |
---|
908 | Vector3 entp = ray.Extrap(mint); |
---|
909 | Vector3 extp = ray.Extrap(maxt); |
---|
910 | |
---|
911 | BspNode *node = mRoot; |
---|
912 | BspNode *farChild; |
---|
913 | |
---|
914 | while (1) |
---|
915 | { |
---|
916 | if (!node->IsLeaf()) |
---|
917 | { |
---|
918 | BspInterior *in = (BspInterior *) node; |
---|
919 | |
---|
920 | Plane3 *splitPlane = in->GetPlane(); |
---|
921 | |
---|
922 | float t = 0; |
---|
923 | bool coplanar = false; |
---|
924 | |
---|
925 | int entSide = splitPlane->Side(entp); |
---|
926 | int extSide = splitPlane->Side(extp); |
---|
927 | |
---|
928 | Vector3 intersection; |
---|
929 | |
---|
930 | if (entSide < 0) |
---|
931 | { |
---|
932 | node = in->GetBack(); |
---|
933 | |
---|
934 | if(extSide <= 0) |
---|
935 | continue; |
---|
936 | |
---|
937 | farChild = in->GetFront(); // plane splits ray |
---|
938 | |
---|
939 | } else if (entSide > 0) |
---|
940 | { |
---|
941 | node = in->GetFront(); |
---|
942 | |
---|
943 | if (extSide >= 0) |
---|
944 | continue; |
---|
945 | |
---|
946 | farChild = in->GetBack(); // plane splits ray |
---|
947 | } |
---|
948 | else // ray and plane are coincident // WHAT TO DO IN THIS CASE ? |
---|
949 | { |
---|
950 | node = in->GetFront(); |
---|
951 | continue; |
---|
952 | } |
---|
953 | |
---|
954 | //-- split |
---|
955 | |
---|
956 | // push data for far child |
---|
957 | tStack.push(BspRayTraversalData(farChild, extp, maxt)); |
---|
958 | |
---|
959 | // find intersection with plane between ray origin and exit point |
---|
960 | extp = splitPlane->FindIntersection(ray.GetLoc(), extp, &maxt); |
---|
961 | |
---|
962 | } else // compute intersections with objects in leaf |
---|
963 | { |
---|
964 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); |
---|
965 | //ray.leaves.push_back(leaf); |
---|
966 | // TODO |
---|
967 | /*ObjectContainer::const_iterator mi; |
---|
968 | for (mi = leaf->mObjects.begin(); mi != leaf->mObjects.end(); ++mi) |
---|
969 | { |
---|
970 | Intersectable *object = *mi; |
---|
971 | if (!object->Mailed()) |
---|
972 | { |
---|
973 | object->Mail(); |
---|
974 | //ray.meshes.push_back(mesh); |
---|
975 | hits += object->CastRay(ray); |
---|
976 | } |
---|
977 | }*/ |
---|
978 | |
---|
979 | if (hits && ray.GetType() == Ray::LOCAL_RAY) |
---|
980 | if (ray.intersections[0].mT <= maxt) |
---|
981 | break; |
---|
982 | |
---|
983 | // get the next node from the stack |
---|
984 | if (tStack.empty()) |
---|
985 | break; |
---|
986 | |
---|
987 | entp = extp; |
---|
988 | mint = maxt; |
---|
989 | BspRayTraversalData &s = tStack.top(); |
---|
990 | |
---|
991 | node = s.mNode; |
---|
992 | extp = s.mExitPoint; |
---|
993 | maxt = s.mMaxT; |
---|
994 | |
---|
995 | tStack.pop(); |
---|
996 | } |
---|
997 | } |
---|
998 | |
---|
999 | return hits; |
---|
1000 | } |
---|
1001 | |
---|
1002 | bool BspTree::Export(const string filename) |
---|
1003 | { |
---|
1004 | Exporter *exporter = Exporter::GetExporter(filename); |
---|
1005 | |
---|
1006 | if (exporter) |
---|
1007 | { |
---|
1008 | exporter->ExportBspTree(*this); |
---|
1009 | delete exporter; |
---|
1010 | |
---|
1011 | return true; |
---|
1012 | } |
---|
1013 | |
---|
1014 | return false; |
---|
1015 | } |
---|
1016 | //} // GtpVisibilityPreprocessor |
---|