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 | splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MAX, box.Max(axis), *mi));
|
---|
483 | }
|
---|
484 |
|
---|
485 | // insert all queries
|
---|
486 | for(ViewCellContainer::const_iterator mi = node->mViewCells.begin();
|
---|
487 | mi != node->mViewCells.end();
|
---|
488 | mi++)
|
---|
489 | {
|
---|
490 | AxisAlignedBox3 box = (*mi)->GetBox();
|
---|
491 |
|
---|
492 | splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MIN,
|
---|
493 | box.Min(axis),
|
---|
494 | *mi)
|
---|
495 | );
|
---|
496 | /*splitCandidates->push_back(new SortableEntry(SortableEntry::BOX_MAX,
|
---|
497 | box.Max(axis),
|
---|
498 | *mi)
|
---|
499 | );*/
|
---|
500 | }
|
---|
501 |
|
---|
502 | stable_sort(splitCandidates->begin(), splitCandidates->end(), iltS);
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 | float
|
---|
507 | TraversalTree::BestCostRatio(TraversalLeaf *node,
|
---|
508 | const AxisAlignedBox3 &box,
|
---|
509 | const int axis,
|
---|
510 | float &position,
|
---|
511 | int &objectsBack,
|
---|
512 | int &objectsFront
|
---|
513 | )
|
---|
514 | {
|
---|
515 |
|
---|
516 | #define DEBUG_COST 1
|
---|
517 |
|
---|
518 | #if DEBUG_COST
|
---|
519 | static int nodeId = -1;
|
---|
520 | char filename[256];
|
---|
521 |
|
---|
522 | static int lastAxis = 100;
|
---|
523 |
|
---|
524 | if (axis <= lastAxis)
|
---|
525 | ++ nodeId;
|
---|
526 |
|
---|
527 | lastAxis = axis;
|
---|
528 |
|
---|
529 | sprintf(filename, "sah-cost%d-%d.log", nodeId, axis);
|
---|
530 | ofstream costStream;
|
---|
531 |
|
---|
532 | if (nodeId < 100)
|
---|
533 | costStream.open(filename);
|
---|
534 |
|
---|
535 | #endif
|
---|
536 |
|
---|
537 | SortSubdivisionCandidates(node, axis);
|
---|
538 |
|
---|
539 | // go through the lists, count the number of objects left and right
|
---|
540 | // and evaluate the following cost funcion:
|
---|
541 | // C = ct_div_ci + (ol + or)/queries
|
---|
542 |
|
---|
543 | vector<SortableEntry *>::const_iterator ci;
|
---|
544 |
|
---|
545 | int objectsLeft = 0, objectsRight = (int)node->mViewCells.size();
|
---|
546 |
|
---|
547 | int dummy1 = objectsLeft, dummy2 = objectsRight;
|
---|
548 |
|
---|
549 | float minBox = box.Min(axis);
|
---|
550 | float maxBox = box.Max(axis);
|
---|
551 | float boxArea = box.SurfaceArea();
|
---|
552 |
|
---|
553 | float minBand = minBox + mSplitBorder*(maxBox - minBox);
|
---|
554 | float maxBand = minBox + (1.0f - mSplitBorder)*(maxBox - minBox);
|
---|
555 |
|
---|
556 | float minSum = 1e20f;
|
---|
557 |
|
---|
558 | int openBoxes = 0;
|
---|
559 |
|
---|
560 | for(ci = splitCandidates->begin(); ci < splitCandidates->end(); ++ ci)
|
---|
561 | {
|
---|
562 | switch ((*ci)->type)
|
---|
563 | {
|
---|
564 | case SortableEntry::BOX_MIN:
|
---|
565 | ++ objectsLeft;
|
---|
566 | ++ openBoxes;
|
---|
567 | break;
|
---|
568 | case SortableEntry::BOX_MAX:
|
---|
569 | -- objectsRight;
|
---|
570 | -- openBoxes;
|
---|
571 | break;
|
---|
572 | }
|
---|
573 |
|
---|
574 | if ((*ci)->value >= minBand && (*ci)->value <= maxBand)
|
---|
575 | {
|
---|
576 | AxisAlignedBox3 lbox = box;
|
---|
577 | AxisAlignedBox3 rbox = box;
|
---|
578 |
|
---|
579 | lbox.SetMax(axis, (*ci)->value);
|
---|
580 | rbox.SetMin(axis, (*ci)->value);
|
---|
581 |
|
---|
582 | const float sum = objectsLeft * lbox.SurfaceArea() + objectsRight * rbox.SurfaceArea();
|
---|
583 |
|
---|
584 | // cout<<"pos="<<(*ci).value<<"\t q=("<<ql<<","<<qr<<")\t r=("<<rl<<","<<rr<<")"<<endl;
|
---|
585 | // cout<<"cost= "<<sum<<endl;
|
---|
586 |
|
---|
587 | #if DEBUG_COST
|
---|
588 | if (nodeId < 100)
|
---|
589 | {
|
---|
590 | float oldCost = (float)node->mViewCells.size();
|
---|
591 | float newCost = mCt_div_ci + sum / boxArea;
|
---|
592 | float ratio = newCost / oldCost;
|
---|
593 |
|
---|
594 | costStream << (*ci)->value << " " << ratio << " open: " << openBoxes;
|
---|
595 |
|
---|
596 | if ((*ci)->type == SortableEntry::BOX_MAX)
|
---|
597 | costStream << " max event" << endl;
|
---|
598 | else
|
---|
599 | costStream << " min event" << endl;
|
---|
600 | }
|
---|
601 | #endif
|
---|
602 |
|
---|
603 | if (sum < minSum)
|
---|
604 | {
|
---|
605 | minSum = sum;
|
---|
606 | position = (*ci)->value;
|
---|
607 |
|
---|
608 | objectsBack = objectsLeft;
|
---|
609 | objectsFront = objectsRight;
|
---|
610 | }
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | const float oldCost = (float)node->mViewCells.size();
|
---|
615 | const float newCost = mCt_div_ci + minSum / boxArea;
|
---|
616 | const float ratio = newCost / oldCost;
|
---|
617 |
|
---|
618 | //if (boxArea == 0)
|
---|
619 | // cout << "error: " << boxArea << endl;
|
---|
620 | if (ratio > 2)
|
---|
621 | {
|
---|
622 | cout << "costratio: " << ratio << " oldcost: " << oldCost << " box area: " << boxArea << " new: " << newCost << endl;
|
---|
623 | cout << "obj left: " <<objectsBack<< " obj right: " << objectsFront << endl;
|
---|
624 | cout << "dummy1: " << dummy1 << " dummy2: " << dummy2 << endl;
|
---|
625 | }
|
---|
626 | #if 0
|
---|
627 | cout<<"===================="<<endl;
|
---|
628 | cout<<"costRatio="<<ratio<<" pos="<<position<<" t="<<(position - minBox)/(maxBox - minBox)
|
---|
629 | <<"\t o=("<<objectsBack<<","<<objectsFront<<")"<<endl;
|
---|
630 | #endif
|
---|
631 |
|
---|
632 | return ratio;
|
---|
633 | }
|
---|
634 |
|
---|
635 |
|
---|
636 | int TraversalTree::FindViewCellIntersections(const Vector3 &lStart,
|
---|
637 | const Vector3 &lEnd,
|
---|
638 | const ViewCellContainer &viewCells,
|
---|
639 | ViewCellContainer &hitViewCells,
|
---|
640 | const bool useMailboxing)
|
---|
641 | {
|
---|
642 | int hits = 0;
|
---|
643 | ViewCellContainer::const_iterator vit, vit_end = viewCells.end();
|
---|
644 |
|
---|
645 | for (vit = viewCells.begin(); vit != vit_end; ++ vit)
|
---|
646 | {
|
---|
647 | ViewCell *viewCell = *vit;
|
---|
648 | // don't have to mail if each view cell belongs to exactly one leaf
|
---|
649 | if (!useMailboxing || !viewCell->Mailed())
|
---|
650 | {
|
---|
651 | if (useMailboxing)
|
---|
652 | viewCell->Mail();
|
---|
653 |
|
---|
654 | // hack: assume that we use vsp tree,
|
---|
655 | //P so we can just test intersection with bounding boxes
|
---|
656 | if (viewCell->GetBox().Intersects(lStart, lEnd))
|
---|
657 | {
|
---|
658 | hitViewCells.push_back(viewCell);
|
---|
659 | ++ hits;
|
---|
660 | }
|
---|
661 | }
|
---|
662 | }
|
---|
663 |
|
---|
664 | return hits;
|
---|
665 | }
|
---|
666 |
|
---|
667 |
|
---|
668 | int TraversalTree::CastLineSegment(const Vector3 &origin,
|
---|
669 | const Vector3 &termination,
|
---|
670 | ViewCellContainer &viewCells,
|
---|
671 | const bool useMailboxing)
|
---|
672 | {
|
---|
673 | int hits = 0;
|
---|
674 |
|
---|
675 | float mint = 0.0f, maxt = 1.0f;
|
---|
676 | const Vector3 dir = termination - origin;
|
---|
677 |
|
---|
678 | stack<LineTraversalData> tStack;
|
---|
679 |
|
---|
680 | Vector3 entp = origin;
|
---|
681 | Vector3 extp = termination;
|
---|
682 |
|
---|
683 | TraversalNode *node = mRoot;
|
---|
684 | TraversalNode *farChild;
|
---|
685 |
|
---|
686 | float position;
|
---|
687 | int axis;
|
---|
688 |
|
---|
689 | while (1)
|
---|
690 | {
|
---|
691 | if (!node->IsLeaf())
|
---|
692 | {
|
---|
693 | TraversalInterior *in = static_cast<TraversalInterior *>(node);
|
---|
694 | position = in->mPosition;
|
---|
695 | axis = in->mAxis;
|
---|
696 |
|
---|
697 | if (entp[axis] <= position)
|
---|
698 | {
|
---|
699 | if (extp[axis] <= position)
|
---|
700 | {
|
---|
701 | node = in->mBack;
|
---|
702 | // cases N1,N2,N3,P5,Z2,Z3
|
---|
703 | continue;
|
---|
704 | } else
|
---|
705 | {
|
---|
706 | // case N4
|
---|
707 | node = in->mBack;
|
---|
708 | farChild = in->mFront;
|
---|
709 | }
|
---|
710 | }
|
---|
711 | else
|
---|
712 | {
|
---|
713 | if (position <= extp[axis])
|
---|
714 | {
|
---|
715 | node = in->mFront;
|
---|
716 | // cases P1,P2,P3,N5,Z1
|
---|
717 | continue;
|
---|
718 | }
|
---|
719 | else
|
---|
720 | {
|
---|
721 | node = in->mFront;
|
---|
722 | farChild = in->mBack;
|
---|
723 | // case P4
|
---|
724 | }
|
---|
725 | }
|
---|
726 |
|
---|
727 | // $$ modification 3.5.2004 - hints from Kamil Ghais
|
---|
728 | // case N4 or P4
|
---|
729 | const float tdist = (position - origin[axis]) / dir[axis];
|
---|
730 | tStack.push(LineTraversalData(farChild, extp, maxt)); //TODO
|
---|
731 |
|
---|
732 | extp = origin + dir * tdist;
|
---|
733 | maxt = tdist;
|
---|
734 | }
|
---|
735 | else
|
---|
736 | {
|
---|
737 | // compute intersection with all objects in this leaf
|
---|
738 | TraversalLeaf *leaf = static_cast<TraversalLeaf *>(node);
|
---|
739 |
|
---|
740 | hits += FindViewCellIntersections(origin,
|
---|
741 | termination,
|
---|
742 | leaf->mViewCells,
|
---|
743 | viewCells,
|
---|
744 | useMailboxing);
|
---|
745 |
|
---|
746 | // get the next node from the stack
|
---|
747 | if (tStack.empty())
|
---|
748 | break;
|
---|
749 |
|
---|
750 | entp = extp;
|
---|
751 | mint = maxt;
|
---|
752 |
|
---|
753 | LineTraversalData &s = tStack.top();
|
---|
754 | node = s.mNode;
|
---|
755 | extp = s.mExitPoint;
|
---|
756 | maxt = s.mMaxT;
|
---|
757 |
|
---|
758 | tStack.pop();
|
---|
759 | }
|
---|
760 | }
|
---|
761 |
|
---|
762 | return hits;
|
---|
763 | }
|
---|
764 |
|
---|
765 |
|
---|
766 | void TraversalTree::CollectLeaves(vector<TraversalLeaf *> &leaves)
|
---|
767 | {
|
---|
768 | stack<TraversalNode *> nodeStack;
|
---|
769 | nodeStack.push(mRoot);
|
---|
770 |
|
---|
771 | while (!nodeStack.empty())
|
---|
772 | {
|
---|
773 | TraversalNode *node = nodeStack.top();
|
---|
774 | nodeStack.pop();
|
---|
775 |
|
---|
776 | if (node->IsLeaf())
|
---|
777 | {
|
---|
778 | TraversalLeaf *leaf = (TraversalLeaf *)node;
|
---|
779 | leaves.push_back(leaf);
|
---|
780 | }
|
---|
781 | else
|
---|
782 | {
|
---|
783 | TraversalInterior *interior = (TraversalInterior *)node;
|
---|
784 | nodeStack.push(interior->mBack);
|
---|
785 | nodeStack.push(interior->mFront);
|
---|
786 | }
|
---|
787 | }
|
---|
788 | }
|
---|
789 |
|
---|
790 |
|
---|
791 | AxisAlignedBox3 TraversalTree::GetBox(const TraversalNode *node) const
|
---|
792 | {
|
---|
793 | TraversalInterior *parent = node->mParent;
|
---|
794 |
|
---|
795 | if (parent == NULL)
|
---|
796 | return mBox;
|
---|
797 |
|
---|
798 | if (!node->IsLeaf())
|
---|
799 | return ((TraversalInterior *)node)->mBox;
|
---|
800 |
|
---|
801 | AxisAlignedBox3 box(parent->mBox);
|
---|
802 |
|
---|
803 | if (parent->mFront == node)
|
---|
804 | box.SetMin(parent->mAxis, parent->mPosition);
|
---|
805 | else
|
---|
806 | box.SetMax(parent->mAxis, parent->mPosition);
|
---|
807 | return box;
|
---|
808 | }
|
---|
809 |
|
---|
810 | }
|
---|