1 | #include <stack>
|
---|
2 | #include <algorithm>
|
---|
3 | #include <queue>
|
---|
4 | #include "Environment.h"
|
---|
5 | #include "Mesh.h"
|
---|
6 | #include "TraversalTree.h"
|
---|
7 | #include "ViewCell.h"
|
---|
8 | #include "Beam.h"
|
---|
9 | #include "Exporter.h"
|
---|
10 |
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | // $$JB HACK
|
---|
15 | #define KD_PVS_AREA (1e-5f)
|
---|
16 |
|
---|
17 | namespace GtpVisibilityPreprocessor {
|
---|
18 |
|
---|
19 | int TraversalNode::sMailId = 1;
|
---|
20 | int TraversalNode::sReservedMailboxes = 1;
|
---|
21 |
|
---|
22 |
|
---|
23 | inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
24 | {
|
---|
25 | return obj1->mId < obj2->mId;
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | TraversalNode::TraversalNode(TraversalInterior *parent):
|
---|
30 | mParent(parent), mMailbox(0)
|
---|
31 | {
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | TraversalInterior::TraversalInterior(TraversalInterior *parent):
|
---|
36 | TraversalNode(parent), mBack(NULL), mFront(NULL)
|
---|
37 | {
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | TraversalInterior::~TraversalInterior()
|
---|
42 | {
|
---|
43 | // recursivly destroy children
|
---|
44 | DEL_PTR(mFront);
|
---|
45 | DEL_PTR(mBack);
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | bool TraversalInterior::IsLeaf() const
|
---|
50 | {
|
---|
51 | return false;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | void TraversalInterior::SetupChildLinks(TraversalNode *b, TraversalNode *f)
|
---|
56 | {
|
---|
57 | mBack = b;
|
---|
58 | mFront = f;
|
---|
59 |
|
---|
60 | b->mParent = f->mParent = this;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | void TraversalInterior::ReplaceChildLink(TraversalNode *oldChild,
|
---|
65 | TraversalNode *newChild)
|
---|
66 | {
|
---|
67 | if (mBack == oldChild)
|
---|
68 | mBack = newChild;
|
---|
69 | else
|
---|
70 | mFront = newChild;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | TraversalLeaf::TraversalLeaf(TraversalInterior *parent, const int objects):
|
---|
75 | TraversalNode(parent)
|
---|
76 | {
|
---|
77 | mViewCells.reserve(objects);
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | bool TraversalLeaf::IsLeaf() const
|
---|
82 | {
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | TraversalLeaf::~TraversalLeaf()
|
---|
88 | {
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | TraversalTree::TraversalTree()
|
---|
93 | {
|
---|
94 | TraversalLeaf *leaf = new TraversalLeaf(NULL, 0);
|
---|
95 | leaf->mDepth = 0;
|
---|
96 | mRoot = leaf;
|
---|
97 |
|
---|
98 | Environment::GetSingleton()->GetIntValue("TraversalTree.Termination.maxNodes", mTermMaxNodes);
|
---|
99 | Environment::GetSingleton()->GetIntValue("TraversalTree.Termination.maxDepth", mTermMaxDepth);
|
---|
100 | Environment::GetSingleton()->GetIntValue("TraversalTree.Termination.minCost", mTermMinCost);
|
---|
101 | Environment::GetSingleton()->GetFloatValue("TraversalTree.Termination.maxCostRatio", mMaxCostRatio);
|
---|
102 | Environment::GetSingleton()->GetFloatValue("TraversalTree.Termination.ct_div_ci", mCt_div_ci);
|
---|
103 | Environment::GetSingleton()->GetFloatValue("TraversalTree.splitBorder", mSplitBorder);
|
---|
104 |
|
---|
105 | Environment::GetSingleton()->GetBoolValue("TraversalTree.sahUseFaces", mSahUseFaces);
|
---|
106 |
|
---|
107 | char splitType[64];
|
---|
108 | Environment::GetSingleton()->GetStringValue("TraversalTree.splitMethod", splitType);
|
---|
109 |
|
---|
110 | splitCandidates = NULL;
|
---|
111 | mSplitMethod = SPLIT_SPATIAL_MEDIAN;
|
---|
112 |
|
---|
113 | if (strcmp(splitType, "spatialMedian") == 0)
|
---|
114 | {
|
---|
115 | mSplitMethod = SPLIT_SPATIAL_MEDIAN;
|
---|
116 | }
|
---|
117 | else
|
---|
118 | {
|
---|
119 | if (strcmp(splitType, "objectMedian") == 0)
|
---|
120 | {
|
---|
121 | mSplitMethod = SPLIT_OBJECT_MEDIAN;
|
---|
122 | }
|
---|
123 | else
|
---|
124 | {
|
---|
125 | if (strcmp(splitType, "SAH") == 0)
|
---|
126 | {
|
---|
127 | mSplitMethod = SPLIT_SAH;
|
---|
128 | }
|
---|
129 | else
|
---|
130 | {
|
---|
131 | cerr << "Wrong kd split type " << splitType << endl;
|
---|
132 | exit(1);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | cout << "Traversal Tree Split method: " << mSplitMethod << endl;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | TraversalTree::~TraversalTree()
|
---|
141 | {
|
---|
142 | DEL_PTR(mRoot);
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | bool TraversalTree::Construct(const ViewCellContainer &viewCells)
|
---|
147 | {
|
---|
148 | if (!splitCandidates)
|
---|
149 | {
|
---|
150 | splitCandidates = new vector<SortableEntry *>;
|
---|
151 | }
|
---|
152 |
|
---|
153 | // first construct a leaf that will get subdivide
|
---|
154 | TraversalLeaf *leaf = static_cast<TraversalLeaf *>(mRoot);
|
---|
155 |
|
---|
156 | leaf->mViewCells = viewCells;
|
---|
157 | mStat.nodes = 1;
|
---|
158 | mBox.Initialize();
|
---|
159 |
|
---|
160 | ViewCellContainer::const_iterator mi;
|
---|
161 |
|
---|
162 | for ( mi = leaf->mViewCells.begin(); mi != leaf->mViewCells.end(); ++ mi)
|
---|
163 | {
|
---|
164 | mBox.Include((*mi)->GetBox());
|
---|
165 | }
|
---|
166 |
|
---|
167 | cout << "TraversalTree Root Box:" << mBox << endl;
|
---|
168 | mRoot = Subdivide(TraversalData(leaf, mBox, 0));
|
---|
169 |
|
---|
170 | // remove the allocated array
|
---|
171 | if (splitCandidates)
|
---|
172 | {
|
---|
173 | CLEAR_CONTAINER(*splitCandidates);
|
---|
174 | delete splitCandidates;
|
---|
175 | }
|
---|
176 |
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | TraversalNode *TraversalTree::Subdivide(const TraversalData &tdata)
|
---|
182 | {
|
---|
183 | TraversalNode *result = NULL;
|
---|
184 |
|
---|
185 | //priority_queue<TraversalData> tStack;
|
---|
186 | stack<TraversalData> tStack;
|
---|
187 |
|
---|
188 | tStack.push(tdata);
|
---|
189 | AxisAlignedBox3 backBox, frontBox;
|
---|
190 |
|
---|
191 | while (!tStack.empty())
|
---|
192 | {
|
---|
193 | if (mStat.Nodes() > mTermMaxNodes)
|
---|
194 | {
|
---|
195 | while (!tStack.empty())
|
---|
196 | {
|
---|
197 | EvaluateLeafStats(tStack.top());
|
---|
198 | tStack.pop();
|
---|
199 | }
|
---|
200 | break;
|
---|
201 | }
|
---|
202 |
|
---|
203 | TraversalData data = tStack.top();
|
---|
204 | tStack.pop();
|
---|
205 |
|
---|
206 | TraversalLeaf *tLeaf = static_cast<TraversalLeaf *> (data.mNode);
|
---|
207 | TraversalNode *node = SubdivideNode(tLeaf,
|
---|
208 | data.mBox,
|
---|
209 | backBox,
|
---|
210 | frontBox);
|
---|
211 |
|
---|
212 | if (result == NULL)
|
---|
213 | result = node;
|
---|
214 |
|
---|
215 | if (!node->IsLeaf())
|
---|
216 | {
|
---|
217 | TraversalInterior *interior = static_cast<TraversalInterior *>(node);
|
---|
218 |
|
---|
219 | // push the children on the stack
|
---|
220 | tStack.push(TraversalData(interior->mBack, backBox, data.mDepth + 1));
|
---|
221 | tStack.push(TraversalData(interior->mFront, frontBox, data.mDepth + 1));
|
---|
222 |
|
---|
223 | }
|
---|
224 | else
|
---|
225 | {
|
---|
226 | EvaluateLeafStats(data);
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | return result;
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | bool TraversalTree::TerminationCriteriaMet(const TraversalLeaf *leaf)
|
---|
235 | {
|
---|
236 | const bool criteriaMet = (
|
---|
237 | ((int)leaf->mViewCells.size() <= mTermMinCost) ||
|
---|
238 | (leaf->mDepth >= mTermMaxDepth)
|
---|
239 | || (GetBox(leaf).SurfaceArea() < 0.00001f)
|
---|
240 | );
|
---|
241 |
|
---|
242 | if (criteriaMet)
|
---|
243 | {
|
---|
244 | cerr << "\nOBJECTS=" << (int)leaf->mViewCells.size() << endl;
|
---|
245 | cerr << "\nDEPTH=" << (int)leaf->mDepth << endl;
|
---|
246 | }
|
---|
247 |
|
---|
248 | return criteriaMet;
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | int TraversalTree::SelectPlane(TraversalLeaf *leaf,
|
---|
253 | const AxisAlignedBox3 &box,
|
---|
254 | float &position)
|
---|
255 | {
|
---|
256 | int axis = -1;
|
---|
257 |
|
---|
258 | switch (mSplitMethod)
|
---|
259 | {
|
---|
260 | case SPLIT_SPATIAL_MEDIAN:
|
---|
261 | {
|
---|
262 | axis = box.Size().DrivingAxis();
|
---|
263 | position = (box.Min()[axis] + box.Max()[axis]) * 0.5f;
|
---|
264 | break;
|
---|
265 | }
|
---|
266 | case SPLIT_SAH:
|
---|
267 | {
|
---|
268 | int objectsBack, objectsFront;
|
---|
269 | float costRatio = 99999;//MAX_FLOAT;
|
---|
270 |
|
---|
271 | for (int i=0; i < 3; ++ i)
|
---|
272 | {
|
---|
273 | float p;
|
---|
274 | float r = BestCostRatio(leaf,
|
---|
275 | box,
|
---|
276 | i,
|
---|
277 | p,
|
---|
278 | objectsBack,
|
---|
279 | objectsFront);
|
---|
280 |
|
---|
281 | if (r < costRatio)
|
---|
282 | {
|
---|
283 | costRatio = r;
|
---|
284 | axis = i;
|
---|
285 | position = p;
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | if (costRatio > mMaxCostRatio)
|
---|
290 | {
|
---|
291 | cout << "Too big cost ratio " << costRatio << endl;
|
---|
292 | axis = -1;
|
---|
293 | }
|
---|
294 | break;
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | return axis;
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 | TraversalNode *TraversalTree::SubdivideNode(TraversalLeaf *leaf,
|
---|
303 | const AxisAlignedBox3 &box,
|
---|
304 | AxisAlignedBox3 &backBBox,
|
---|
305 | AxisAlignedBox3 &frontBBox)
|
---|
306 | {
|
---|
307 |
|
---|
308 | if (TerminationCriteriaMet(leaf))
|
---|
309 | return leaf;
|
---|
310 |
|
---|
311 | float position;
|
---|
312 |
|
---|
313 | // select subdivision axis
|
---|
314 | int axis = SelectPlane( leaf, box, position );
|
---|
315 |
|
---|
316 | if (axis == -1) {
|
---|
317 | cout << "terminate on cost ratio" << endl;
|
---|
318 | ++ mStat.costRatioNodes;
|
---|
319 | return leaf;
|
---|
320 | }
|
---|
321 |
|
---|
322 | mStat.nodes+=2;
|
---|
323 | mStat.splits[axis]++;
|
---|
324 |
|
---|
325 | // add the new nodes to the tree
|
---|
326 | TraversalInterior *node = new TraversalInterior(leaf->mParent);
|
---|
327 |
|
---|
328 | node->mAxis = axis;
|
---|
329 | node->mPosition = position;
|
---|
330 | node->mBox = box;
|
---|
331 |
|
---|
332 | backBBox = box;
|
---|
333 | frontBBox = box;
|
---|
334 |
|
---|
335 | // first count ray sides
|
---|
336 | int objectsBack = 0;
|
---|
337 | int objectsFront = 0;
|
---|
338 |
|
---|
339 | backBBox.SetMax(axis, position);
|
---|
340 | frontBBox.SetMin(axis, position);
|
---|
341 |
|
---|
342 | ViewCellContainer::const_iterator mi, mi_end = leaf->mViewCells.end();
|
---|
343 |
|
---|
344 | for (mi = leaf->mViewCells.begin(); mi != mi_end; ++ mi)
|
---|
345 | {
|
---|
346 | // determine the side of this ray with respect to the plane
|
---|
347 | AxisAlignedBox3 box = (*mi)->GetBox();
|
---|
348 | if (box.Max(axis) > position)
|
---|
349 | ++ objectsFront;
|
---|
350 |
|
---|
351 | if (box.Min(axis) < position)
|
---|
352 | ++ objectsBack;
|
---|
353 | }
|
---|
354 |
|
---|
355 | TraversalLeaf *back = new TraversalLeaf(node, objectsBack);
|
---|
356 | TraversalLeaf *front = new TraversalLeaf(node, objectsFront);
|
---|
357 |
|
---|
358 | back->mDepth = front->mDepth = leaf->mDepth + 1;
|
---|
359 |
|
---|
360 | // replace a link from node's parent
|
---|
361 | if (leaf->mParent)
|
---|
362 | {
|
---|
363 | leaf->mParent->ReplaceChildLink(leaf, node);
|
---|
364 | }
|
---|
365 |
|
---|
366 | // and setup child links
|
---|
367 | node->SetupChildLinks(back, front);
|
---|
368 |
|
---|
369 | for (mi = leaf->mViewCells.begin(); mi != mi_end; ++ mi)
|
---|
370 | {
|
---|
371 | // determine the side of this ray with respect to the plane
|
---|
372 | AxisAlignedBox3 box = (*mi)->GetBox();
|
---|
373 |
|
---|
374 | if (box.Max(axis) >= position )
|
---|
375 | {
|
---|
376 | front->mViewCells.push_back(*mi);
|
---|
377 | }
|
---|
378 |
|
---|
379 | if (box.Min(axis) < position )
|
---|
380 | {
|
---|
381 | back->mViewCells.push_back(*mi);
|
---|
382 | }
|
---|
383 |
|
---|
384 | mStat.objectRefs -= (int)leaf->mViewCells.size();
|
---|
385 | mStat.objectRefs += objectsBack + objectsFront;
|
---|
386 | }
|
---|
387 |
|
---|
388 | delete leaf;
|
---|
389 |
|
---|
390 | return node;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | void TraversalTreeStatistics::Print(ostream &app) const
|
---|
395 | {
|
---|
396 | app << "===== TraversalTree statistics ===============\n";
|
---|
397 |
|
---|
398 | app << "#N_NODES ( Number of nodes )\n" << nodes << "\n";
|
---|
399 |
|
---|
400 | app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n";
|
---|
401 |
|
---|
402 | app << "#N_SPLITS ( Number of splits in axes x y z dx dy dz)\n";
|
---|
403 |
|
---|
404 | for (int i = 0; i < 7; ++ i)
|
---|
405 | app << splits[i] << " ";
|
---|
406 | app << endl;
|
---|
407 |
|
---|
408 | app << "#N_MAXOBJECTREFS ( Max number of object refs / leaf )\n" <<
|
---|
409 | maxObjectRefs << "\n";
|
---|
410 |
|
---|
411 | app << "#N_AVGOBJECTREFS ( Avg number of object refs / leaf )\n" <<
|
---|
412 | totalObjectRefs / (double)Leaves() << "\n";
|
---|
413 |
|
---|
414 | app << "#N_LEAFDOMAINREFS ( Number of query domain Refs / leaf )\n" <<
|
---|
415 | objectRefs / (double)Leaves() << "\n";
|
---|
416 |
|
---|
417 | app << "#N_PEMPTYLEAVES ( Percentage of leaves with zero query domains )\n"<<
|
---|
418 | zeroQueryNodes * 100 / (double)Leaves() << endl;
|
---|
419 |
|
---|
420 | app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maxdepth )\n"<<
|
---|
421 | maxDepthNodes * 100 / (double)Leaves() << endl;
|
---|
422 |
|
---|
423 | app << "#N_PMINCOSTLEAVES ( Percentage of leaves with minCost )\n"<<
|
---|
424 | minCostNodes * 100 / (double)Leaves() << endl;
|
---|
425 |
|
---|
426 | app << "#N_COSTRATIOLEAVES ( Percentage of leaves with cost ratio termination )\n"<<
|
---|
427 | costRatioNodes * 100 / (double)Leaves() << endl;
|
---|
428 |
|
---|
429 | // app << setprecision(4);
|
---|
430 | // app << "#N_CTIME ( Construction time [s] )\n"
|
---|
431 | // << Time() << " \n";
|
---|
432 |
|
---|
433 | app << "======= END OF TraversalTree statistics ========\n";
|
---|
434 | }
|
---|
435 |
|
---|
436 |
|
---|
437 | void TraversalTree::EvaluateLeafStats(const TraversalData &data)
|
---|
438 | {
|
---|
439 | // the node became a leaf -> evaluate stats for leafs
|
---|
440 | TraversalLeaf *leaf = (TraversalLeaf *)data.mNode;
|
---|
441 |
|
---|
442 | if (data.mDepth >= mTermMaxDepth)
|
---|
443 | ++ mStat.maxDepthNodes;
|
---|
444 |
|
---|
445 | if ((int)(leaf->mViewCells.size()) <= mTermMinCost)
|
---|
446 | ++ mStat.minCostNodes;
|
---|
447 |
|
---|
448 | mStat.totalObjectRefs += (int)leaf->mViewCells.size();
|
---|
449 |
|
---|
450 | if ((int)(leaf->mViewCells.size()) > mStat.maxObjectRefs)
|
---|
451 | mStat.maxObjectRefs = (int)leaf->mViewCells.size();
|
---|
452 | }
|
---|
453 |
|
---|
454 |
|
---|
455 | void
|
---|
456 | TraversalTree::SortSubdivisionCandidates(TraversalLeaf *node,
|
---|
457 | const int axis)
|
---|
458 | {
|
---|
459 | CLEAR_CONTAINER(*splitCandidates);
|
---|
460 | //splitCandidates->clear();
|
---|
461 |
|
---|
462 | int requestedSize = 2 * (int)node->mViewCells.size();
|
---|
463 |
|
---|
464 | // creates a sorted split candidates array
|
---|
465 | if (splitCandidates->capacity() > 500000 &&
|
---|
466 | requestedSize < (int)(splitCandidates->capacity() / 10) )
|
---|
467 | {
|
---|
468 | delete splitCandidates;
|
---|
469 | splitCandidates = new vector<SortableEntry *>;
|
---|
470 | }
|
---|
471 |
|
---|
472 | splitCandidates->reserve(requestedSize);
|
---|
473 |
|
---|
474 |
|
---|
475 | // insert all queries
|
---|
476 | for(ViewCellContainer::const_iterator mi = node->mViewCells.begin();
|
---|
477 | mi != node->mViewCells.end();
|
---|
478 | mi++)
|
---|
479 | {
|
---|
480 | AxisAlignedBox3 box = (*mi)->GetBox();
|
---|
481 |
|
---|
482 | //cout << "box: " << box << endl;
|
---|
483 | splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MAX,
|
---|
484 | box.Max(axis),
|
---|
485 | *mi)
|
---|
486 | );
|
---|
487 | }
|
---|
488 |
|
---|
489 | // insert all queries
|
---|
490 | for(ViewCellContainer::const_iterator mi = node->mViewCells.begin();
|
---|
491 | mi != node->mViewCells.end();
|
---|
492 | mi++)
|
---|
493 | {
|
---|
494 | AxisAlignedBox3 box = (*mi)->GetBox();
|
---|
495 |
|
---|
496 | splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MIN,
|
---|
497 | box.Min(axis),
|
---|
498 | *mi)
|
---|
499 | );
|
---|
500 | /*splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MAX,
|
---|
501 | box.Max(axis),
|
---|
502 | *mi)
|
---|
503 | );*/
|
---|
504 | }
|
---|
505 |
|
---|
506 | stable_sort(splitCandidates->begin(), splitCandidates->end(), iltS);
|
---|
507 | }
|
---|
508 |
|
---|
509 |
|
---|
510 | float
|
---|
511 | TraversalTree::BestCostRatio(TraversalLeaf *node,
|
---|
512 | const AxisAlignedBox3 &box,
|
---|
513 | const int axis,
|
---|
514 | float &position,
|
---|
515 | int &objectsBack,
|
---|
516 | int &objectsFront
|
---|
517 | )
|
---|
518 | {
|
---|
519 |
|
---|
520 | #define DEBUG_COST 1
|
---|
521 |
|
---|
522 | #if DEBUG_COST
|
---|
523 | static int nodeId = -1;
|
---|
524 | char filename[256];
|
---|
525 |
|
---|
526 | static int lastAxis = 100;
|
---|
527 |
|
---|
528 | if (axis <= lastAxis)
|
---|
529 | ++ nodeId;
|
---|
530 |
|
---|
531 | lastAxis = axis;
|
---|
532 |
|
---|
533 | sprintf(filename, "sah-cost%d-%d.log", nodeId, axis);
|
---|
534 | ofstream costStream;
|
---|
535 |
|
---|
536 | if (nodeId < 100)
|
---|
537 | costStream.open(filename);
|
---|
538 |
|
---|
539 | #endif
|
---|
540 |
|
---|
541 | SortSubdivisionCandidates(node, axis);
|
---|
542 |
|
---|
543 | // go through the lists, count the number of objects left and right
|
---|
544 | // and evaluate the following cost funcion:
|
---|
545 | // C = ct_div_ci + (ol + or)/queries
|
---|
546 |
|
---|
547 | vector<SortableEntry *>::const_iterator ci;
|
---|
548 |
|
---|
549 | int objectsLeft = 0, objectsRight = (int)node->mViewCells.size();
|
---|
550 |
|
---|
551 | int dummy1 = objectsLeft, dummy2 = objectsRight;
|
---|
552 |
|
---|
553 | float minBox = box.Min(axis);
|
---|
554 | float maxBox = box.Max(axis);
|
---|
555 | float boxArea = box.SurfaceArea();
|
---|
556 |
|
---|
557 | float minBand = minBox + mSplitBorder*(maxBox - minBox);
|
---|
558 | float maxBand = minBox + (1.0f - mSplitBorder)*(maxBox - minBox);
|
---|
559 |
|
---|
560 | float minSum = 1e20f;
|
---|
561 |
|
---|
562 | int openBoxes = 0;
|
---|
563 |
|
---|
564 | for(ci = splitCandidates->begin(); ci < splitCandidates->end(); ++ ci)
|
---|
565 | {
|
---|
566 | switch ((*ci)->type)
|
---|
567 | {
|
---|
568 | case SortableEntry::BOX_MIN:
|
---|
569 | ++ objectsLeft;
|
---|
570 | ++ openBoxes;
|
---|
571 | break;
|
---|
572 | case SortableEntry::BOX_MAX:
|
---|
573 | -- objectsRight;
|
---|
574 | -- openBoxes;
|
---|
575 | break;
|
---|
576 | }
|
---|
577 |
|
---|
578 | if ((*ci)->value >= minBand && (*ci)->value <= maxBand)
|
---|
579 | {
|
---|
580 | AxisAlignedBox3 lbox = box;
|
---|
581 | AxisAlignedBox3 rbox = box;
|
---|
582 |
|
---|
583 | lbox.SetMax(axis, (*ci)->value);
|
---|
584 | rbox.SetMin(axis, (*ci)->value);
|
---|
585 |
|
---|
586 | const float sum = objectsLeft * lbox.SurfaceArea() + objectsRight * rbox.SurfaceArea();
|
---|
587 |
|
---|
588 | // cout<<"pos="<<(*ci).value<<"\t q=("<<ql<<","<<qr<<")\t r=("<<rl<<","<<rr<<")"<<endl;
|
---|
589 | // cout<<"cost= "<<sum<<endl;
|
---|
590 |
|
---|
591 | #if DEBUG_COST
|
---|
592 | if (nodeId < 100)
|
---|
593 | {
|
---|
594 | float oldCost = (float)node->mViewCells.size();
|
---|
595 | float newCost = mCt_div_ci + sum / boxArea;
|
---|
596 | float ratio = newCost / oldCost;
|
---|
597 |
|
---|
598 | costStream << (*ci)->value << " " << ratio << " open: " << openBoxes;
|
---|
599 |
|
---|
600 | if ((*ci)->type == SortableEntry::BOX_MAX)
|
---|
601 | costStream << " max event" << endl;
|
---|
602 | else
|
---|
603 | costStream << " min event" << endl;
|
---|
604 | }
|
---|
605 | #endif
|
---|
606 |
|
---|
607 | if (sum < minSum)
|
---|
608 | {
|
---|
609 | minSum = sum;
|
---|
610 | position = (*ci)->value;
|
---|
611 |
|
---|
612 | objectsBack = objectsLeft;
|
---|
613 | objectsFront = objectsRight;
|
---|
614 | }
|
---|
615 | }
|
---|
616 | }
|
---|
617 |
|
---|
618 | const float oldCost = (float)node->mViewCells.size();
|
---|
619 | const float newCost = mCt_div_ci + minSum / boxArea;
|
---|
620 | const float ratio = newCost / oldCost;
|
---|
621 |
|
---|
622 | //if (boxArea == 0)
|
---|
623 | // cout << "error: " << boxArea << endl;
|
---|
624 | if (ratio > 2)
|
---|
625 | {
|
---|
626 | cout << "costratio: " << ratio << " oldcost: " << oldCost << " box area: " << boxArea << " new: " << newCost << endl;
|
---|
627 | cout << "obj left: " <<objectsBack<< " obj right: " << objectsFront << endl;
|
---|
628 | cout << "dummy1: " << dummy1 << " dummy2: " << dummy2 << endl;
|
---|
629 | }
|
---|
630 | #if 0
|
---|
631 | cout<<"===================="<<endl;
|
---|
632 | cout<<"costRatio="<<ratio<<" pos="<<position<<" t="<<(position - minBox)/(maxBox - minBox)
|
---|
633 | <<"\t o=("<<objectsBack<<","<<objectsFront<<")"<<endl;
|
---|
634 | #endif
|
---|
635 |
|
---|
636 | return ratio;
|
---|
637 | }
|
---|
638 |
|
---|
639 |
|
---|
640 | int TraversalTree::FindViewCellIntersections(const Vector3 &lStart,
|
---|
641 | const Vector3 &lEnd,
|
---|
642 | const ViewCellContainer &viewCells,
|
---|
643 | ViewCellContainer &hitViewCells,
|
---|
644 | const bool useMailboxing)
|
---|
645 | {
|
---|
646 | int hits = 0;
|
---|
647 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
648 |
|
---|
649 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
650 | {
|
---|
651 | ViewCell *viewCell = *vit;
|
---|
652 | // don't have to mail if each view cell belongs to exactly one leaf
|
---|
653 | if (!useMailboxing || !viewCell->Mailed())
|
---|
654 | {
|
---|
655 | if (useMailboxing)
|
---|
656 | viewCell->Mail();
|
---|
657 |
|
---|
658 | // hack: assume that we use vsp tree,
|
---|
659 | //P so we can just test intersection with bounding boxes
|
---|
660 | if (viewCell->GetBox().Intersects(lStart, lEnd))
|
---|
661 | {
|
---|
662 | hitViewCells.push_back(viewCell);
|
---|
663 | ++ hits;
|
---|
664 | }
|
---|
665 | }
|
---|
666 | }
|
---|
667 |
|
---|
668 | return hits;
|
---|
669 | }
|
---|
670 |
|
---|
671 |
|
---|
672 | int TraversalTree::CastLineSegment(const Vector3 &origin,
|
---|
673 | const Vector3 &termination,
|
---|
674 | ViewCellContainer &viewCells,
|
---|
675 | const bool useMailboxing)
|
---|
676 | {
|
---|
677 | int hits = 0;
|
---|
678 |
|
---|
679 | float mint = 0.0f, maxt = 1.0f;
|
---|
680 | const Vector3 dir = termination - origin;
|
---|
681 |
|
---|
682 | stack<LineTraversalData> tStack;
|
---|
683 |
|
---|
684 | Vector3 entp = origin;
|
---|
685 | Vector3 extp = termination;
|
---|
686 |
|
---|
687 | TraversalNode *node = mRoot;
|
---|
688 | TraversalNode *farChild;
|
---|
689 |
|
---|
690 | float position;
|
---|
691 | int axis;
|
---|
692 |
|
---|
693 | while (1)
|
---|
694 | {
|
---|
695 | if (!node->IsLeaf())
|
---|
696 | {
|
---|
697 | TraversalInterior *in = static_cast<TraversalInterior *>(node);
|
---|
698 | position = in->mPosition;
|
---|
699 | axis = in->mAxis;
|
---|
700 |
|
---|
701 | if (entp[axis] <= position)
|
---|
702 | {
|
---|
703 | if (extp[axis] <= position)
|
---|
704 | {
|
---|
705 | node = in->mBack;
|
---|
706 | // cases N1,N2,N3,P5,Z2,Z3
|
---|
707 | continue;
|
---|
708 | } else
|
---|
709 | {
|
---|
710 | // case N4
|
---|
711 | node = in->mBack;
|
---|
712 | farChild = in->mFront;
|
---|
713 | }
|
---|
714 | }
|
---|
715 | else
|
---|
716 | {
|
---|
717 | if (position <= extp[axis])
|
---|
718 | {
|
---|
719 | node = in->mFront;
|
---|
720 | // cases P1,P2,P3,N5,Z1
|
---|
721 | continue;
|
---|
722 | }
|
---|
723 | else
|
---|
724 | {
|
---|
725 | node = in->mFront;
|
---|
726 | farChild = in->mBack;
|
---|
727 | // case P4
|
---|
728 | }
|
---|
729 | }
|
---|
730 |
|
---|
731 | // $$ modification 3.5.2004 - hints from Kamil Ghais
|
---|
732 | // case N4 or P4
|
---|
733 | const float tdist = (position - origin[axis]) / dir[axis];
|
---|
734 | tStack.push(LineTraversalData(farChild, extp, maxt)); //TODO
|
---|
735 |
|
---|
736 | extp = origin + dir * tdist;
|
---|
737 | maxt = tdist;
|
---|
738 | }
|
---|
739 | else
|
---|
740 | {
|
---|
741 | // compute intersection with all objects in this leaf
|
---|
742 | TraversalLeaf *leaf = static_cast<TraversalLeaf *>(node);
|
---|
743 |
|
---|
744 | hits += FindViewCellIntersections(origin,
|
---|
745 | termination,
|
---|
746 | leaf->mViewCells,
|
---|
747 | viewCells,
|
---|
748 | useMailboxing);
|
---|
749 |
|
---|
750 | // get the next node from the stack
|
---|
751 | if (tStack.empty())
|
---|
752 | break;
|
---|
753 |
|
---|
754 | entp = extp;
|
---|
755 | mint = maxt;
|
---|
756 |
|
---|
757 | LineTraversalData &s = tStack.top();
|
---|
758 | node = s.mNode;
|
---|
759 | extp = s.mExitPoint;
|
---|
760 | maxt = s.mMaxT;
|
---|
761 |
|
---|
762 | tStack.pop();
|
---|
763 | }
|
---|
764 | }
|
---|
765 |
|
---|
766 | return hits;
|
---|
767 | }
|
---|
768 |
|
---|
769 |
|
---|
770 | void TraversalTree::CollectLeaves(vector<TraversalLeaf *> &leaves)
|
---|
771 | {
|
---|
772 | stack<TraversalNode *> nodeStack;
|
---|
773 | nodeStack.push(mRoot);
|
---|
774 |
|
---|
775 | while (!nodeStack.empty())
|
---|
776 | {
|
---|
777 | TraversalNode *node = nodeStack.top();
|
---|
778 | nodeStack.pop();
|
---|
779 |
|
---|
780 | if (node->IsLeaf())
|
---|
781 | {
|
---|
782 | TraversalLeaf *leaf = (TraversalLeaf *)node;
|
---|
783 | leaves.push_back(leaf);
|
---|
784 | }
|
---|
785 | else
|
---|
786 | {
|
---|
787 | TraversalInterior *interior = (TraversalInterior *)node;
|
---|
788 | nodeStack.push(interior->mBack);
|
---|
789 | nodeStack.push(interior->mFront);
|
---|
790 | }
|
---|
791 | }
|
---|
792 | }
|
---|
793 |
|
---|
794 |
|
---|
795 | AxisAlignedBox3 TraversalTree::GetBox(const TraversalNode *node) const
|
---|
796 | {
|
---|
797 | TraversalInterior *parent = node->mParent;
|
---|
798 |
|
---|
799 | if (parent == NULL)
|
---|
800 | return mBox;
|
---|
801 |
|
---|
802 | if (!node->IsLeaf())
|
---|
803 | return ((TraversalInterior *)node)->mBox;
|
---|
804 |
|
---|
805 | AxisAlignedBox3 box(parent->mBox);
|
---|
806 |
|
---|
807 | if (parent->mFront == node)
|
---|
808 | box.SetMin(parent->mAxis, parent->mPosition);
|
---|
809 | else
|
---|
810 | box.SetMax(parent->mAxis, parent->mPosition);
|
---|
811 | return box;
|
---|
812 | }
|
---|
813 |
|
---|
814 | }
|
---|