1 |
|
---|
2 | // ================================================================
|
---|
3 | // $Id: lsds_kdtree.cpp,v 1.18 2005/04/16 09:34:21 bittner Exp $
|
---|
4 | // ****************************************************************
|
---|
5 | /**
|
---|
6 | The KD tree based LSDS
|
---|
7 | */
|
---|
8 | // Initial coding by
|
---|
9 | /**
|
---|
10 | @author Jiri Bittner
|
---|
11 | */
|
---|
12 |
|
---|
13 | // Standard headers
|
---|
14 | #include <stack>
|
---|
15 | #include <queue>
|
---|
16 | #include <algorithm>
|
---|
17 | #include <fstream>
|
---|
18 | #include <string>
|
---|
19 |
|
---|
20 | #include "VspKdTree.h"
|
---|
21 |
|
---|
22 | #include "Environment.h"
|
---|
23 | #include "VssRay.h"
|
---|
24 | #include "Intersectable.h"
|
---|
25 | #include "Ray.h"
|
---|
26 | #include "RayInfo.h"
|
---|
27 | #include "ViewCellsManager.h"
|
---|
28 | #include "ViewCellBsp.h"
|
---|
29 |
|
---|
30 | // Static variables
|
---|
31 | int VspKdLeaf::sMailId = 0;
|
---|
32 | int MergeCandidate::sMaxPvsSize = 150;
|
---|
33 | float MergeCandidate::sOverallCost = Limits::Small;
|
---|
34 | #define USE_FIXEDPOINT_T 0
|
---|
35 |
|
---|
36 | /// Adds object to the pvs of the front and back node
|
---|
37 | inline void AddObject2Pvs(Intersectable *object,
|
---|
38 | const int side,
|
---|
39 | int &pvsBack,
|
---|
40 | int &pvsFront)
|
---|
41 | {
|
---|
42 | if (!object)
|
---|
43 | return;
|
---|
44 |
|
---|
45 | if (side <= 0)
|
---|
46 | {
|
---|
47 | if (!object->Mailed() && !object->Mailed(2))
|
---|
48 | {
|
---|
49 | ++ pvsBack;
|
---|
50 |
|
---|
51 | if (object->Mailed(1))
|
---|
52 | object->Mail(2);
|
---|
53 | else
|
---|
54 | object->Mail();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (side >= 0)
|
---|
59 | {
|
---|
60 | if (!object->Mailed(1) && !object->Mailed(2))
|
---|
61 | {
|
---|
62 | ++ pvsFront;
|
---|
63 |
|
---|
64 | if (object->Mailed())
|
---|
65 | object->Mail(2);
|
---|
66 | else
|
---|
67 | object->Mail(1);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**************************************************************/
|
---|
73 | /* class VspKdNode implementation */
|
---|
74 | /**************************************************************/
|
---|
75 |
|
---|
76 | // Inline constructor
|
---|
77 | VspKdNode::VspKdNode(VspKdInterior *p):
|
---|
78 | mParent(p), mAxis(-1), mDepth(p ? p->mDepth + 1 : 0)
|
---|
79 | {}
|
---|
80 |
|
---|
81 | VspKdNode::~VspKdNode()
|
---|
82 | {};
|
---|
83 |
|
---|
84 | inline VspKdInterior *VspKdNode::GetParent() const
|
---|
85 | {
|
---|
86 | return mParent;
|
---|
87 | }
|
---|
88 |
|
---|
89 | inline void VspKdNode::SetParent(VspKdInterior *p)
|
---|
90 | {
|
---|
91 | mParent = p;
|
---|
92 | }
|
---|
93 |
|
---|
94 | bool VspKdNode::IsLeaf() const
|
---|
95 | {
|
---|
96 | return mAxis == -1;
|
---|
97 | }
|
---|
98 |
|
---|
99 | int VspKdNode::GetAccessTime()
|
---|
100 | {
|
---|
101 | return 0x7FFFFFF;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**************************************************************/
|
---|
105 | /* VspKdInterior implementation */
|
---|
106 | /**************************************************************/
|
---|
107 |
|
---|
108 | VspKdInterior::VspKdInterior(VspKdInterior *p):
|
---|
109 | VspKdNode(p), mBack(NULL), mFront(NULL), mAccesses(0), mLastAccessTime(-1)
|
---|
110 | {
|
---|
111 | }
|
---|
112 |
|
---|
113 | int VspKdInterior::GetAccessTime()
|
---|
114 | {
|
---|
115 | return mLastAccessTime;
|
---|
116 | }
|
---|
117 |
|
---|
118 | void VspKdInterior::SetupChildLinks(VspKdNode *b, VspKdNode *f)
|
---|
119 | {
|
---|
120 | mBack = b;
|
---|
121 | mFront = f;
|
---|
122 | b->SetParent(this);
|
---|
123 | f->SetParent(this);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void VspKdInterior::ReplaceChildLink(VspKdNode *oldChild,
|
---|
127 | VspKdNode *newChild)
|
---|
128 | {
|
---|
129 | if (mBack == oldChild)
|
---|
130 | mBack = newChild;
|
---|
131 | else
|
---|
132 | mFront = newChild;
|
---|
133 | }
|
---|
134 |
|
---|
135 | int VspKdInterior::Type() const
|
---|
136 | {
|
---|
137 | return EInterior;
|
---|
138 | }
|
---|
139 |
|
---|
140 | VspKdInterior::~VspKdInterior()
|
---|
141 | {
|
---|
142 | DEL_PTR(mBack);
|
---|
143 | DEL_PTR(mFront);
|
---|
144 | }
|
---|
145 |
|
---|
146 | void VspKdInterior::Print(ostream &s) const
|
---|
147 | {
|
---|
148 | switch (mAxis)
|
---|
149 | {
|
---|
150 | case 0:
|
---|
151 | s << "x "; break;
|
---|
152 | case 1:
|
---|
153 | s << "y "; break;
|
---|
154 | case 2:
|
---|
155 | s << "z "; break;
|
---|
156 | }
|
---|
157 |
|
---|
158 | s << mPosition << " ";
|
---|
159 |
|
---|
160 | mBack->Print(s);
|
---|
161 | mFront->Print(s);
|
---|
162 | }
|
---|
163 |
|
---|
164 | int VspKdInterior::ComputeRayIntersection(const RayInfo &rayData, float &t)
|
---|
165 | {
|
---|
166 | return rayData.ComputeRayIntersection(mAxis, mPosition, t);
|
---|
167 | }
|
---|
168 |
|
---|
169 | VspKdNode *VspKdInterior::GetBack() const
|
---|
170 | {
|
---|
171 | return mBack;
|
---|
172 | }
|
---|
173 |
|
---|
174 | VspKdNode *VspKdInterior::GetFront() const
|
---|
175 | {
|
---|
176 | return mFront;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**************************************************************/
|
---|
181 | /* class VspKdLeaf implementation */
|
---|
182 | /**************************************************************/
|
---|
183 |
|
---|
184 |
|
---|
185 | VspKdLeaf::VspKdLeaf(VspKdInterior *p,
|
---|
186 | const int nRays,
|
---|
187 | const int maxCostMisses):
|
---|
188 | VspKdNode(p), mRays(), mPvsSize(0), mValidPvs(false), mViewCell(NULL),
|
---|
189 | mMaxCostMisses(maxCostMisses)
|
---|
190 | {
|
---|
191 | mRays.reserve(nRays);
|
---|
192 | }
|
---|
193 |
|
---|
194 | VspKdLeaf::~VspKdLeaf()
|
---|
195 | {
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | int VspKdLeaf::GetMaxCostMisses()
|
---|
200 | {
|
---|
201 | return mMaxCostMisses;
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | int VspKdLeaf::Type() const
|
---|
206 | {
|
---|
207 | return ELeaf;
|
---|
208 | }
|
---|
209 |
|
---|
210 | void VspKdLeaf::Print(ostream &s) const
|
---|
211 | {
|
---|
212 | s << endl << "L: r = " << (int)mRays.size() << endl;
|
---|
213 | };
|
---|
214 |
|
---|
215 | void VspKdLeaf::AddRay(const RayInfo &data)
|
---|
216 | {
|
---|
217 | mValidPvs = false;
|
---|
218 | mRays.push_back(data);
|
---|
219 | data.mRay->Ref();
|
---|
220 | }
|
---|
221 |
|
---|
222 | int VspKdLeaf::GetPvsSize() const
|
---|
223 | {
|
---|
224 | return mPvsSize;
|
---|
225 | }
|
---|
226 |
|
---|
227 | void VspKdLeaf::SetPvsSize(const int s)
|
---|
228 | {
|
---|
229 | mPvsSize = s;
|
---|
230 | }
|
---|
231 |
|
---|
232 | void VspKdLeaf::Mail()
|
---|
233 | {
|
---|
234 | mMailbox = sMailId;
|
---|
235 | }
|
---|
236 |
|
---|
237 | void VspKdLeaf::NewMail()
|
---|
238 | {
|
---|
239 | ++ sMailId;
|
---|
240 | }
|
---|
241 |
|
---|
242 | bool VspKdLeaf::Mailed() const
|
---|
243 | {
|
---|
244 | return mMailbox == sMailId;
|
---|
245 | }
|
---|
246 |
|
---|
247 | bool VspKdLeaf::Mailed(const int mail) const
|
---|
248 | {
|
---|
249 | return mMailbox == mail;
|
---|
250 | }
|
---|
251 |
|
---|
252 | int VspKdLeaf::GetMailbox() const
|
---|
253 | {
|
---|
254 | return mMailbox;
|
---|
255 | }
|
---|
256 |
|
---|
257 | float VspKdLeaf::GetAvgRayContribution() const
|
---|
258 | {
|
---|
259 | return GetPvsSize() / ((float)mRays.size() + Limits::Small);
|
---|
260 | }
|
---|
261 |
|
---|
262 | float VspKdLeaf::GetSqrRayContribution() const
|
---|
263 | {
|
---|
264 | return sqr(GetAvgRayContribution());
|
---|
265 | }
|
---|
266 |
|
---|
267 | RayInfoContainer &VspKdLeaf::GetRays()
|
---|
268 | {
|
---|
269 | return mRays;
|
---|
270 | }
|
---|
271 |
|
---|
272 | void VspKdLeaf::ExtractPvs(ObjectContainer &objects) const
|
---|
273 | {
|
---|
274 | RayInfoContainer::const_iterator it, it_end = mRays.end();
|
---|
275 |
|
---|
276 | for (it = mRays.begin(); it != it_end; ++ it)
|
---|
277 | {
|
---|
278 | if ((*it).mRay->mTerminationObject)
|
---|
279 | objects.push_back((*it).mRay->mTerminationObject);
|
---|
280 | if ((*it).mRay->mOriginObject)
|
---|
281 | objects.push_back((*it).mRay->mOriginObject);
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | void VspKdLeaf::GetRays(VssRayContainer &rays)
|
---|
286 | {
|
---|
287 | RayInfoContainer::const_iterator it, it_end = mRays.end();
|
---|
288 |
|
---|
289 | for (it = mRays.begin(); it != mRays.end(); ++ it)
|
---|
290 | rays.push_back((*it).mRay);
|
---|
291 | }
|
---|
292 |
|
---|
293 | VspKdViewCell *VspKdLeaf::GetViewCell()
|
---|
294 | {
|
---|
295 | return mViewCell;
|
---|
296 | }
|
---|
297 |
|
---|
298 | void VspKdLeaf::SetViewCell(VspKdViewCell *viewCell)
|
---|
299 | {
|
---|
300 | mViewCell = viewCell;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | void VspKdLeaf::UpdatePvsSize()
|
---|
305 | {
|
---|
306 | if (!mValidPvs)
|
---|
307 | {
|
---|
308 | Intersectable::NewMail();
|
---|
309 | int pvsSize = 0;
|
---|
310 | for(RayInfoContainer::iterator ri = mRays.begin();
|
---|
311 | ri != mRays.end(); ++ ri)
|
---|
312 | {
|
---|
313 | if ((*ri).mRay->IsActive())
|
---|
314 | {
|
---|
315 | Intersectable *object;
|
---|
316 | #if BIDIRECTIONAL_RAY
|
---|
317 | object = (*ri).mRay->mOriginObject;
|
---|
318 |
|
---|
319 | if (object && !object->Mailed())
|
---|
320 | {
|
---|
321 | ++ pvsSize;
|
---|
322 | object->Mail();
|
---|
323 | }
|
---|
324 | #endif
|
---|
325 | object = (*ri).mRay->mTerminationObject;
|
---|
326 | if (object && !object->Mailed())
|
---|
327 | {
|
---|
328 | ++ pvsSize;
|
---|
329 | object->Mail();
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | mPvsSize = pvsSize;
|
---|
335 | mValidPvs = true;
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | /*********************************************************/
|
---|
340 | /* class VspKdTree implementation */
|
---|
341 | /*********************************************************/
|
---|
342 |
|
---|
343 |
|
---|
344 |
|
---|
345 | VspKdTree::VspKdTree(): mOnlyDrivingAxis(true)
|
---|
346 | {
|
---|
347 | environment->GetIntValue("VspKdTree.Termination.maxDepth", mTermMaxDepth);
|
---|
348 | environment->GetIntValue("VspKdTree.Termination.minPvs", mTermMinPvs);
|
---|
349 | environment->GetIntValue("VspKdTree.Termination.minRays", mTermMinRays);
|
---|
350 | environment->GetFloatValue("VspKdTree.Termination.maxRayContribution", mTermMaxRayContribution);
|
---|
351 | environment->GetFloatValue("VspKdTree.Termination.maxCostRatio", mTermMaxCostRatio);
|
---|
352 | environment->GetFloatValue("VspKdTree.Termination.minSize", mTermMinSize);
|
---|
353 |
|
---|
354 | environment->GetIntValue("VspKdTree.Termination.missTolerance", mTermMissTolerance);
|
---|
355 |
|
---|
356 | mTermMinSize = sqr(mTermMinSize);
|
---|
357 |
|
---|
358 | environment->GetFloatValue("VspKdTree.epsilon", mEpsilon);
|
---|
359 | environment->GetFloatValue("VspKdTree.ct_div_ci", mCtDivCi);
|
---|
360 |
|
---|
361 | environment->GetFloatValue("VspKdTree.maxTotalMemory", mMaxTotalMemory);
|
---|
362 | environment->GetFloatValue("VspKdTree.maxStaticMemory", mMaxStaticMemory);
|
---|
363 |
|
---|
364 | environment->GetIntValue("VspKdTree.accessTimeThreshold", mAccessTimeThreshold);
|
---|
365 | environment->GetIntValue("VspKdTree.minCollapseDepth", mMinCollapseDepth);
|
---|
366 |
|
---|
367 | //environment->GetIntValue("ViewCells.maxViewCells", mMaxViewCells);
|
---|
368 |
|
---|
369 | environment->GetIntValue("VspKdTree.PostProcess.minViewCells", mMergeMinViewCells);
|
---|
370 | environment->GetFloatValue("VspKdTree.PostProcess.maxCostRatio", mMergeMaxCostRatio);
|
---|
371 | environment->GetIntValue("VspKdTree.PostProcess.maxPvsSize",
|
---|
372 | MergeCandidate::sMaxPvsSize);
|
---|
373 |
|
---|
374 | // split type
|
---|
375 | char sname[128];
|
---|
376 | environment->GetStringValue("VspKdTree.splitType", sname);
|
---|
377 | string name(sname);
|
---|
378 |
|
---|
379 | Debug << "======= vsp kd tree options ========" << endl;
|
---|
380 | Debug << "max depth: "<< mTermMaxDepth << endl;
|
---|
381 | Debug << "min pvs: "<< mTermMinPvs << endl;
|
---|
382 | Debug << "min rays: "<< mTermMinRays << endl;
|
---|
383 | Debug << "max ray contribution: "<< mTermMaxRayContribution << endl;
|
---|
384 | Debug << "max cost ratio: " << mTermMaxCostRatio << endl;
|
---|
385 | Debug << "min size: " << mTermMinSize << endl;
|
---|
386 |
|
---|
387 | if (name.compare("regular") == 0)
|
---|
388 | {
|
---|
389 | Debug << "using regular split" << endl;
|
---|
390 | splitType = ESplitRegular;
|
---|
391 | }
|
---|
392 | else
|
---|
393 | {
|
---|
394 | if (name.compare("heuristic") == 0)
|
---|
395 | {
|
---|
396 | Debug << "using heuristic split" << endl;
|
---|
397 | splitType = ESplitHeuristic;
|
---|
398 | }
|
---|
399 | else
|
---|
400 | {
|
---|
401 | cerr << "Invalid VspKdTree split type " << name << endl;
|
---|
402 | exit(1);
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | mRoot = NULL;
|
---|
407 |
|
---|
408 | mSplitCandidates = new vector<SortableEntry>;
|
---|
409 | }
|
---|
410 |
|
---|
411 |
|
---|
412 | VspKdTree::~VspKdTree()
|
---|
413 | {
|
---|
414 | DEL_PTR(mRoot);
|
---|
415 | DEL_PTR(mSplitCandidates);
|
---|
416 | }
|
---|
417 |
|
---|
418 | void VspKdStatistics::Print(ostream &app) const
|
---|
419 | {
|
---|
420 | app << "===== VspKdTree statistics ===============\n";
|
---|
421 |
|
---|
422 | app << "#N_RAYS ( Number of rays )\n"
|
---|
423 | << rays << endl;
|
---|
424 |
|
---|
425 | app << "#N_INITPVS ( Initial PVS size )\n"
|
---|
426 | << initialPvsSize << endl;
|
---|
427 |
|
---|
428 | app << "#N_NODES ( Number of nodes )\n" << nodes << "\n";
|
---|
429 |
|
---|
430 | app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n";
|
---|
431 |
|
---|
432 | app << "#N_SPLITS ( Number of splits in axes x y z dx dy dz \n";
|
---|
433 |
|
---|
434 | for (int i=0; i<7; i++)
|
---|
435 | app << splits[i] <<" ";
|
---|
436 | app << endl;
|
---|
437 |
|
---|
438 | app << "#N_RAYREFS ( Number of rayRefs )\n" <<
|
---|
439 | rayRefs << "\n";
|
---|
440 |
|
---|
441 | app << "#N_RAYRAYREFS ( Number of rayRefs / ray )\n" <<
|
---|
442 | rayRefs / (double)rays << "\n";
|
---|
443 |
|
---|
444 | app << "#N_LEAFRAYREFS ( Number of rayRefs / leaf )\n" <<
|
---|
445 | rayRefs / (double)Leaves() << "\n";
|
---|
446 |
|
---|
447 | app << "#N_MAXRAYREFS ( Max number of rayRefs / leaf )\n" <<
|
---|
448 | maxRayRefs << "\n";
|
---|
449 |
|
---|
450 | // app << setprecision(4);
|
---|
451 |
|
---|
452 | app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maxdepth )\n" <<
|
---|
453 | maxDepthNodes * 100 / (double)Leaves() << endl;
|
---|
454 |
|
---|
455 | app << "#N_PMINPVSLEAVES ( Percentage of leaves with mininimal PVS )\n" <<
|
---|
456 | minPvsNodes * 100 / (double)Leaves() << endl;
|
---|
457 |
|
---|
458 | app << "#N_PMINRAYSLEAVES ( Percentage of leaves with minimal number of rays)\n" <<
|
---|
459 | minRaysNodes * 100 / (double)Leaves() << endl;
|
---|
460 |
|
---|
461 | app << "#N_PMINSIZELEAVES ( Percentage of leaves with minSize )\n"<<
|
---|
462 | minSizeNodes * 100 / (double)Leaves() << endl;
|
---|
463 |
|
---|
464 | app << "#N_PMAXRAYCONTRIBLEAVES ( Percentage of leaves with maximal ray contribution )\n" <<
|
---|
465 | maxRayContribNodes * 100 / (double)Leaves() << endl;
|
---|
466 |
|
---|
467 | app << "#N_ADDED_RAYREFS ( Number of dynamically added ray references )\n"<<
|
---|
468 | addedRayRefs << endl;
|
---|
469 |
|
---|
470 | app << "#N_REMOVED_RAYREFS ( Number of dynamically removed ray references )\n"<<
|
---|
471 | removedRayRefs << endl;
|
---|
472 |
|
---|
473 | // app << setprecision(4);
|
---|
474 |
|
---|
475 | app << "#N_( Maximal PVS size / leaf)\n"
|
---|
476 | << maxPvsSize << endl;
|
---|
477 |
|
---|
478 | app << "#N_CTIME ( Construction time [s] )\n"
|
---|
479 | << Time() << " \n";
|
---|
480 |
|
---|
481 | app << "===== END OF VspKdTree statistics ==========\n";
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | void VspKdTree::CollectViewCells(ViewCellContainer &viewCells) const
|
---|
486 | {
|
---|
487 | stack<VspKdNode *> nodeStack;
|
---|
488 | nodeStack.push(mRoot);
|
---|
489 |
|
---|
490 | ViewCell::NewMail();
|
---|
491 |
|
---|
492 | while (!nodeStack.empty())
|
---|
493 | {
|
---|
494 | VspKdNode *node = nodeStack.top();
|
---|
495 | nodeStack.pop();
|
---|
496 |
|
---|
497 | if (node->IsLeaf())
|
---|
498 | {
|
---|
499 | ViewCell *viewCell = dynamic_cast<VspKdLeaf *>(node)->mViewCell;
|
---|
500 |
|
---|
501 | if (!viewCell->Mailed())
|
---|
502 | {
|
---|
503 | viewCell->Mail();
|
---|
504 | viewCells.push_back(viewCell);
|
---|
505 | }
|
---|
506 | }
|
---|
507 | else
|
---|
508 | {
|
---|
509 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node);
|
---|
510 |
|
---|
511 | nodeStack.push(interior->GetFront());
|
---|
512 | nodeStack.push(interior->GetBack());
|
---|
513 | }
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | void VspKdTree::Construct(const VssRayContainer &rays,
|
---|
518 | AxisAlignedBox3 *forcedBoundingBox)
|
---|
519 | {
|
---|
520 | mStat.Start();
|
---|
521 |
|
---|
522 | mMaxMemory = mMaxStaticMemory;
|
---|
523 | DEL_PTR(mRoot);
|
---|
524 |
|
---|
525 | mRoot = new VspKdLeaf(NULL, (int)rays.size());
|
---|
526 |
|
---|
527 | // first construct a leaf that will get subdivided
|
---|
528 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(mRoot);
|
---|
529 |
|
---|
530 | mStat.nodes = 1;
|
---|
531 | mBox.Initialize();
|
---|
532 |
|
---|
533 | //-- compute bounding box
|
---|
534 | if (forcedBoundingBox)
|
---|
535 | mBox = *forcedBoundingBox;
|
---|
536 | else
|
---|
537 | for (VssRayContainer::const_iterator ri = rays.begin(); ri != rays.end(); ++ ri)
|
---|
538 | {
|
---|
539 | if ((*ri)->mOriginObject)
|
---|
540 | mBox.Include((*ri)->GetOrigin());
|
---|
541 | if ((*ri)->mTerminationObject)
|
---|
542 | mBox.Include((*ri)->GetTermination());
|
---|
543 | }
|
---|
544 |
|
---|
545 | Debug << "bbox = " << mBox << endl;
|
---|
546 |
|
---|
547 | for (VssRayContainer::const_iterator ri = rays.begin(); ri != rays.end(); ++ ri)
|
---|
548 | {
|
---|
549 | //leaf->AddRay(RayInfo(*ri));
|
---|
550 | VssRay *ray = *ri;
|
---|
551 |
|
---|
552 | float minT, maxT;
|
---|
553 |
|
---|
554 | // TODO: not very efficient to implictly cast between rays types ...
|
---|
555 | if (mBox.GetRaySegment(*ray, minT, maxT))
|
---|
556 | {
|
---|
557 | float len = ray->Length();
|
---|
558 |
|
---|
559 | if (!len)
|
---|
560 | len = Limits::Small;
|
---|
561 |
|
---|
562 | leaf->AddRay(RayInfo(ray, minT / len, maxT / len));
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | mStat.rays = (int)leaf->mRays.size();
|
---|
567 | leaf->UpdatePvsSize();
|
---|
568 |
|
---|
569 | mStat.initialPvsSize = leaf->GetPvsSize();
|
---|
570 |
|
---|
571 | // Subdivide();
|
---|
572 | mRoot = Subdivide(TraversalData(leaf, mBox, 0));
|
---|
573 |
|
---|
574 | if (mSplitCandidates)
|
---|
575 | {
|
---|
576 | // force release of this vector
|
---|
577 | delete mSplitCandidates;
|
---|
578 | mSplitCandidates = new vector<SortableEntry>;
|
---|
579 | }
|
---|
580 |
|
---|
581 | mStat.Stop();
|
---|
582 |
|
---|
583 | mStat.Print(cout);
|
---|
584 | Debug << "#Total memory=" << GetMemUsage() << endl;
|
---|
585 | }
|
---|
586 |
|
---|
587 |
|
---|
588 |
|
---|
589 | VspKdNode *VspKdTree::Subdivide(const TraversalData &tdata)
|
---|
590 | {
|
---|
591 | VspKdNode *result = NULL;
|
---|
592 |
|
---|
593 | priority_queue<TraversalData> tStack;
|
---|
594 | //stack<TraversalData> tStack;
|
---|
595 |
|
---|
596 | tStack.push(tdata);
|
---|
597 |
|
---|
598 | AxisAlignedBox3 backBox;
|
---|
599 | AxisAlignedBox3 frontBox;
|
---|
600 |
|
---|
601 | int lastMem = 0;
|
---|
602 |
|
---|
603 | while (!tStack.empty())
|
---|
604 | {
|
---|
605 | float mem = GetMemUsage();
|
---|
606 |
|
---|
607 | if (lastMem / 10 != ((int)mem) / 10)
|
---|
608 | {
|
---|
609 | Debug << mem << " MB" << endl;
|
---|
610 | }
|
---|
611 | lastMem = (int)mem;
|
---|
612 |
|
---|
613 | if (1 && mem > mMaxMemory)
|
---|
614 | {
|
---|
615 | Debug << "memory limit reached: " << mem << endl;
|
---|
616 | // count statistics on unprocessed leafs
|
---|
617 | while (!tStack.empty())
|
---|
618 | {
|
---|
619 | EvaluateLeafStats(tStack.top());
|
---|
620 | tStack.pop();
|
---|
621 | }
|
---|
622 | break;
|
---|
623 | }
|
---|
624 |
|
---|
625 | TraversalData data = tStack.top();
|
---|
626 | tStack.pop();
|
---|
627 |
|
---|
628 | VspKdNode *node = SubdivideNode(dynamic_cast<VspKdLeaf *>(data.mNode),
|
---|
629 | data.mBox, backBox, frontBox);
|
---|
630 | if (result == NULL)
|
---|
631 | result = node;
|
---|
632 |
|
---|
633 | if (!node->IsLeaf())
|
---|
634 | {
|
---|
635 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node);
|
---|
636 |
|
---|
637 | // push the children on the stack
|
---|
638 | tStack.push(TraversalData(interior->GetBack(), backBox, data.mDepth + 1));
|
---|
639 | tStack.push(TraversalData(interior->GetFront(), frontBox, data.mDepth + 1));
|
---|
640 | }
|
---|
641 | else
|
---|
642 | {
|
---|
643 | EvaluateLeafStats(data);
|
---|
644 | }
|
---|
645 | }
|
---|
646 |
|
---|
647 | return result;
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 | // returns selected plane for subdivision
|
---|
652 | int VspKdTree::SelectPlane(VspKdLeaf *leaf,
|
---|
653 | const AxisAlignedBox3 &box,
|
---|
654 | float &position,
|
---|
655 | int &raysBack,
|
---|
656 | int &raysFront,
|
---|
657 | int &pvsBack,
|
---|
658 | int &pvsFront)
|
---|
659 | {
|
---|
660 | int minDirDepth = 6;
|
---|
661 | int axis = 0;
|
---|
662 | float costRatio = 0;
|
---|
663 |
|
---|
664 | if (splitType == ESplitRegular)
|
---|
665 | {
|
---|
666 | costRatio = BestCostRatioRegular(leaf,
|
---|
667 | axis,
|
---|
668 | position,
|
---|
669 | raysBack,
|
---|
670 | raysFront,
|
---|
671 | pvsBack,
|
---|
672 | pvsFront);
|
---|
673 | }
|
---|
674 | else if (splitType == ESplitHeuristic)
|
---|
675 | {
|
---|
676 | costRatio = BestCostRatioHeuristic(leaf,
|
---|
677 | axis,
|
---|
678 | position,
|
---|
679 | raysBack,
|
---|
680 | raysFront,
|
---|
681 | pvsBack,
|
---|
682 | pvsFront);
|
---|
683 | }
|
---|
684 | else
|
---|
685 | {
|
---|
686 | cerr << "VspKdTree: Unknown split heuristics\n";
|
---|
687 | exit(1);
|
---|
688 | }
|
---|
689 |
|
---|
690 | if (costRatio > mTermMaxCostRatio)
|
---|
691 | {
|
---|
692 | Debug << "Too big cost ratio " << costRatio << endl;
|
---|
693 | return -1;
|
---|
694 | }
|
---|
695 |
|
---|
696 | if (0)
|
---|
697 | Debug << "pvs=" << leaf->mPvsSize
|
---|
698 | << " rays=" << (int)leaf->mRays.size()
|
---|
699 | << " rc=" << leaf->GetAvgRayContribution()
|
---|
700 | << " axis=" << axis << endl;
|
---|
701 |
|
---|
702 | return axis;
|
---|
703 | }
|
---|
704 |
|
---|
705 |
|
---|
706 |
|
---|
707 | float VspKdTree::EvalCostRatio(VspKdLeaf *leaf,
|
---|
708 | const int axis,
|
---|
709 | const float position,
|
---|
710 | int &raysBack,
|
---|
711 | int &raysFront,
|
---|
712 | int &pvsBack,
|
---|
713 | int &pvsFront)
|
---|
714 | {
|
---|
715 | raysBack = 0;
|
---|
716 | raysFront = 0;
|
---|
717 | pvsFront = 0;
|
---|
718 | pvsBack = 0;
|
---|
719 |
|
---|
720 | float newCost;
|
---|
721 |
|
---|
722 | Intersectable::NewMail(3);
|
---|
723 |
|
---|
724 | // eval pvs size
|
---|
725 | int pvsSize = leaf->GetPvsSize();
|
---|
726 |
|
---|
727 | Intersectable::NewMail(3);
|
---|
728 |
|
---|
729 | // this is the main ray classification loop!
|
---|
730 | for(RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
731 | ri != leaf->mRays.end(); ++ ri)
|
---|
732 | {
|
---|
733 | if (!(*ri).mRay->IsActive())
|
---|
734 | continue;
|
---|
735 |
|
---|
736 | // determine the side of this ray with respect to the plane
|
---|
737 | float t;
|
---|
738 | int side = (*ri).ComputeRayIntersection(axis, position, t);
|
---|
739 | // (*ri).mRay->mSide = side;
|
---|
740 |
|
---|
741 | if (side <= 0)
|
---|
742 | ++ raysBack;
|
---|
743 |
|
---|
744 | if (side >= 0)
|
---|
745 | ++ raysFront;
|
---|
746 |
|
---|
747 | AddObject2Pvs((*ri).mRay->mTerminationObject, side, pvsBack, pvsFront);
|
---|
748 | }
|
---|
749 |
|
---|
750 | if (0)
|
---|
751 | {
|
---|
752 | const float sum = float(pvsBack + pvsFront);
|
---|
753 | const float oldCost = (float)pvsSize * 2;
|
---|
754 |
|
---|
755 | return sum / oldCost;
|
---|
756 | }
|
---|
757 | else
|
---|
758 | {
|
---|
759 | AxisAlignedBox3 box = GetBBox(leaf);
|
---|
760 |
|
---|
761 | float minBox = box.Min(axis);
|
---|
762 | float maxBox = box.Max(axis);
|
---|
763 | float sizeBox = maxBox - minBox;
|
---|
764 |
|
---|
765 | // float sum = raysBack*(position - minBox) + raysFront*(maxBox - position);
|
---|
766 | const float sum = pvsBack * (position - minBox) + pvsFront * (maxBox - position);
|
---|
767 |
|
---|
768 | newCost = mCtDivCi + sum / sizeBox;
|
---|
769 |
|
---|
770 | //Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl;
|
---|
771 | // float oldCost = leaf->mRays.size();
|
---|
772 | const float oldCost = (float)pvsSize;
|
---|
773 |
|
---|
774 | return newCost / oldCost;
|
---|
775 | }
|
---|
776 | }
|
---|
777 |
|
---|
778 | float VspKdTree::BestCostRatioRegular(VspKdLeaf *leaf,
|
---|
779 | int &axis,
|
---|
780 | float &position,
|
---|
781 | int &raysBack,
|
---|
782 | int &raysFront,
|
---|
783 | int &pvsBack,
|
---|
784 | int &pvsFront)
|
---|
785 | {
|
---|
786 | int nRaysBack[3], nRaysFront[3];
|
---|
787 | int nPvsBack[3], nPvsFront[3];
|
---|
788 |
|
---|
789 | float nPosition[3];
|
---|
790 | float nCostRatio[3];
|
---|
791 | int bestAxis = -1;
|
---|
792 |
|
---|
793 | AxisAlignedBox3 sBox = GetBBox(leaf);
|
---|
794 |
|
---|
795 | const int sAxis = sBox.Size().DrivingAxis();
|
---|
796 |
|
---|
797 | for (axis = 0; axis < 3; ++ axis)
|
---|
798 | {
|
---|
799 | if (!mOnlyDrivingAxis || axis == sAxis)
|
---|
800 | {
|
---|
801 |
|
---|
802 | nPosition[axis] = (sBox.Min()[axis] + sBox.Max()[axis]) * 0.5f;
|
---|
803 |
|
---|
804 | nCostRatio[axis] = EvalCostRatio(leaf,
|
---|
805 | axis,
|
---|
806 | nPosition[axis],
|
---|
807 | nRaysBack[axis],
|
---|
808 | nRaysFront[axis],
|
---|
809 | nPvsBack[axis],
|
---|
810 | nPvsFront[axis]);
|
---|
811 | if (bestAxis == -1)
|
---|
812 | {
|
---|
813 | bestAxis = axis;
|
---|
814 | }
|
---|
815 | else if (nCostRatio[axis] < nCostRatio[bestAxis])
|
---|
816 | {
|
---|
817 | /*Debug << "pvs front " << nPvsBack[axis]
|
---|
818 | << " pvs back " << nPvsFront[axis]
|
---|
819 | << " overall pvs " << leaf->GetPvsSize() << endl;*/
|
---|
820 | bestAxis = axis;
|
---|
821 | }
|
---|
822 |
|
---|
823 | }
|
---|
824 | }
|
---|
825 |
|
---|
826 | //-- assign best axis
|
---|
827 | axis = bestAxis;
|
---|
828 | position = nPosition[bestAxis];
|
---|
829 |
|
---|
830 | raysBack = nRaysBack[bestAxis];
|
---|
831 | raysFront = nRaysFront[bestAxis];
|
---|
832 |
|
---|
833 | pvsBack = nPvsBack[bestAxis];
|
---|
834 | pvsFront = nPvsFront[bestAxis];
|
---|
835 |
|
---|
836 | return nCostRatio[bestAxis];
|
---|
837 | }
|
---|
838 |
|
---|
839 | float VspKdTree::BestCostRatioHeuristic(VspKdLeaf *leaf,
|
---|
840 | int &axis,
|
---|
841 | float &position,
|
---|
842 | int &raysBack,
|
---|
843 | int &raysFront,
|
---|
844 | int &pvsBack,
|
---|
845 | int &pvsFront)
|
---|
846 | {
|
---|
847 | AxisAlignedBox3 box = GetBBox(leaf);
|
---|
848 | // AxisAlignedBox3 dirBox = GetDirBBox(node);
|
---|
849 |
|
---|
850 | axis = box.Size().DrivingAxis();
|
---|
851 |
|
---|
852 | SortSplitCandidates(leaf, axis);
|
---|
853 |
|
---|
854 | // go through the lists, count the number of objects left and right
|
---|
855 | // and evaluate the following cost funcion:
|
---|
856 | // C = ct_div_ci + (ql*rl + qr*rr)/queries
|
---|
857 |
|
---|
858 | int rl = 0, rr = (int)leaf->mRays.size();
|
---|
859 | int pl = 0, pr = leaf->GetPvsSize();
|
---|
860 |
|
---|
861 | float minBox = box.Min(axis);
|
---|
862 | float maxBox = box.Max(axis);
|
---|
863 | float sizeBox = maxBox - minBox;
|
---|
864 |
|
---|
865 | float minBand = minBox + 0.1f*(maxBox - minBox);
|
---|
866 | float maxBand = minBox + 0.9f*(maxBox - minBox);
|
---|
867 |
|
---|
868 | float sum = rr*sizeBox;
|
---|
869 | float minSum = 1e20f;
|
---|
870 |
|
---|
871 | Intersectable::NewMail();
|
---|
872 |
|
---|
873 | // set all object as belonging to the fron pvs
|
---|
874 | for(RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
875 | ri != leaf->mRays.end(); ++ ri)
|
---|
876 | {
|
---|
877 | if ((*ri).mRay->IsActive())
|
---|
878 | {
|
---|
879 | Intersectable *object = (*ri).mRay->mTerminationObject;
|
---|
880 |
|
---|
881 | if (object)
|
---|
882 | if (!object->Mailed())
|
---|
883 | {
|
---|
884 | object->Mail();
|
---|
885 | object->mCounter = 1;
|
---|
886 | }
|
---|
887 | else
|
---|
888 | ++ object->mCounter;
|
---|
889 | }
|
---|
890 | }
|
---|
891 |
|
---|
892 | Intersectable::NewMail();
|
---|
893 |
|
---|
894 | for (vector<SortableEntry>::const_iterator ci = mSplitCandidates->begin();
|
---|
895 | ci < mSplitCandidates->end(); ++ ci)
|
---|
896 | {
|
---|
897 | VssRay *ray;
|
---|
898 |
|
---|
899 | switch ((*ci).type)
|
---|
900 | {
|
---|
901 | case SortableEntry::ERayMin:
|
---|
902 | {
|
---|
903 | ++ rl;
|
---|
904 | ray = (VssRay *) (*ci).data;
|
---|
905 | Intersectable *object = ray->mTerminationObject;
|
---|
906 | if (object && !object->Mailed())
|
---|
907 | {
|
---|
908 | object->Mail();
|
---|
909 | ++ pl;
|
---|
910 | }
|
---|
911 | break;
|
---|
912 | }
|
---|
913 | case SortableEntry::ERayMax:
|
---|
914 | {
|
---|
915 | -- rr;
|
---|
916 |
|
---|
917 | ray = (VssRay *) (*ci).data;
|
---|
918 | Intersectable *object = ray->mTerminationObject;
|
---|
919 |
|
---|
920 | if (object)
|
---|
921 | {
|
---|
922 | if (-- object->mCounter == 0)
|
---|
923 | -- pr;
|
---|
924 | }
|
---|
925 |
|
---|
926 | break;
|
---|
927 | }
|
---|
928 | }
|
---|
929 |
|
---|
930 | if ((*ci).value > minBand && (*ci).value < maxBand)
|
---|
931 | {
|
---|
932 | sum = pl*((*ci).value - minBox) + pr*(maxBox - (*ci).value);
|
---|
933 |
|
---|
934 | // cout<<"pos="<<(*ci).value<<"\t q=("<<ql<<","<<qr<<")\t r=("<<rl<<","<<rr<<")"<<endl;
|
---|
935 | // cout<<"cost= "<<sum<<endl;
|
---|
936 |
|
---|
937 | if (sum < minSum)
|
---|
938 | {
|
---|
939 | minSum = sum;
|
---|
940 | position = (*ci).value;
|
---|
941 |
|
---|
942 | raysBack = rl;
|
---|
943 | raysFront = rr;
|
---|
944 |
|
---|
945 | pvsBack = pl;
|
---|
946 | pvsFront = pr;
|
---|
947 |
|
---|
948 | }
|
---|
949 | }
|
---|
950 | }
|
---|
951 |
|
---|
952 | float oldCost = (float)leaf->GetPvsSize();
|
---|
953 | float newCost = mCtDivCi + minSum / sizeBox;
|
---|
954 | float ratio = newCost / oldCost;
|
---|
955 |
|
---|
956 | //Debug << "costRatio=" << ratio << " pos=" << position << " t=" << (position - minBox) / (maxBox - minBox)
|
---|
957 | // <<"\t q=(" << queriesBack << "," << queriesFront << ")\t r=(" << raysBack << "," << raysFront << ")" << endl;
|
---|
958 |
|
---|
959 | return ratio;
|
---|
960 | }
|
---|
961 |
|
---|
962 | void VspKdTree::SortSplitCandidates(VspKdLeaf *node,
|
---|
963 | const int axis)
|
---|
964 | {
|
---|
965 | mSplitCandidates->clear();
|
---|
966 |
|
---|
967 | int requestedSize = 2 * (int)(node->mRays.size());
|
---|
968 | // creates a sorted split candidates array
|
---|
969 | if (mSplitCandidates->capacity() > 500000 &&
|
---|
970 | requestedSize < (int)(mSplitCandidates->capacity()/10) )
|
---|
971 | {
|
---|
972 | DEL_PTR(mSplitCandidates);
|
---|
973 | mSplitCandidates = new vector<SortableEntry>;
|
---|
974 | }
|
---|
975 |
|
---|
976 | mSplitCandidates->reserve(requestedSize);
|
---|
977 |
|
---|
978 | // insert all queries
|
---|
979 | for(RayInfoContainer::const_iterator ri = node->mRays.begin();
|
---|
980 | ri < node->mRays.end(); ++ ri)
|
---|
981 | {
|
---|
982 | bool positive = (*ri).mRay->HasPosDir(axis);
|
---|
983 | mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax,
|
---|
984 | (*ri).ExtrapOrigin(axis), (void *)&*ri));
|
---|
985 |
|
---|
986 | mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin,
|
---|
987 | (*ri).ExtrapTermination(axis), (void *)&*ri));
|
---|
988 | }
|
---|
989 |
|
---|
990 | stable_sort(mSplitCandidates->begin(), mSplitCandidates->end());
|
---|
991 | }
|
---|
992 |
|
---|
993 |
|
---|
994 | void VspKdTree::EvaluateLeafStats(const TraversalData &data)
|
---|
995 | {
|
---|
996 | // the node became a leaf -> evaluate stats for leafs
|
---|
997 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(data.mNode);
|
---|
998 |
|
---|
999 | if (leaf->GetPvsSize() > mStat.maxPvsSize)
|
---|
1000 | mStat.maxPvsSize = leaf->GetPvsSize();
|
---|
1001 |
|
---|
1002 | if ((int)(leaf->mRays.size()) > mStat.maxRayRefs)
|
---|
1003 | mStat.maxRayRefs = (int)leaf->mRays.size();
|
---|
1004 |
|
---|
1005 |
|
---|
1006 | if (data.mDepth >= mTermMaxDepth)
|
---|
1007 | ++ mStat.maxDepthNodes;
|
---|
1008 |
|
---|
1009 | if (leaf->GetPvsSize() < mTermMinPvs)
|
---|
1010 | ++ mStat.minPvsNodes;
|
---|
1011 |
|
---|
1012 | if ((int)leaf->GetRays().size() < mTermMinRays)
|
---|
1013 | ++ mStat.minRaysNodes;
|
---|
1014 |
|
---|
1015 | if (leaf->GetAvgRayContribution() > mTermMaxRayContribution)
|
---|
1016 | ++ mStat.maxRayContribNodes;
|
---|
1017 |
|
---|
1018 | if (SqrMagnitude(data.mBox.Size()) <= mTermMinSize)
|
---|
1019 | ++ mStat.minSizeNodes;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 |
|
---|
1023 | inline bool VspKdTree::TerminationCriteriaMet(const VspKdLeaf *leaf,
|
---|
1024 | const AxisAlignedBox3 &box) const
|
---|
1025 | {
|
---|
1026 | return ((leaf->GetPvsSize() < mTermMinPvs) ||
|
---|
1027 | (leaf->mRays.size() < mTermMinRays) ||
|
---|
1028 | //(leaf->GetAvgRayContribution() > mTermMaxRayContribution ) ||
|
---|
1029 | (leaf->mDepth >= mTermMaxDepth) ||
|
---|
1030 | (SqrMagnitude(box.Size()) <= mTermMinSize));
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 |
|
---|
1034 | VspKdNode *VspKdTree::SubdivideNode(VspKdLeaf *leaf,
|
---|
1035 | const AxisAlignedBox3 &box,
|
---|
1036 | AxisAlignedBox3 &backBBox,
|
---|
1037 | AxisAlignedBox3 &frontBBox)
|
---|
1038 | {
|
---|
1039 | if (TerminationCriteriaMet(leaf, box))
|
---|
1040 | {
|
---|
1041 | if (1 && leaf->mDepth >= mTermMaxDepth)
|
---|
1042 | {
|
---|
1043 | Debug << "Warning: max depth reached depth=" << (int)leaf->mDepth<<" rays=" << (int)leaf->mRays.size() << endl;
|
---|
1044 | Debug << "Bbox: " << GetBBox(leaf) << endl;
|
---|
1045 | }
|
---|
1046 | //Debug << "depth: " << (int)leaf->mDepth << " pvs: " << leaf->GetPvsSize() << " rays: " << leaf->mRays.size() << endl;
|
---|
1047 |
|
---|
1048 | return leaf;
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | float position;
|
---|
1052 | // first count ray sides
|
---|
1053 | int raysBack;
|
---|
1054 | int raysFront;
|
---|
1055 | int pvsBack;
|
---|
1056 | int pvsFront;
|
---|
1057 |
|
---|
1058 | // select subdivision axis
|
---|
1059 | const int axis = SelectPlane(leaf, box, position, raysBack, raysFront, pvsBack, pvsFront);
|
---|
1060 |
|
---|
1061 | //Debug << "rays back=" << raysBack << " rays front=" << raysFront << " pvs back=" << pvsBack << " pvs front=" << pvsFront << endl;
|
---|
1062 |
|
---|
1063 | int maxCostMisses = leaf->GetMaxCostMisses();
|
---|
1064 |
|
---|
1065 | if (axis == -1)
|
---|
1066 | {
|
---|
1067 | ++ maxCostMisses;
|
---|
1068 |
|
---|
1069 | if (maxCostMisses >= mTermMissTolerance)
|
---|
1070 | return leaf;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | mStat.nodes += 2;
|
---|
1074 | //++ mStat.splits[axis];
|
---|
1075 |
|
---|
1076 | // add the new nodes to the tree
|
---|
1077 | VspKdInterior *node = new VspKdInterior(leaf->mParent);
|
---|
1078 |
|
---|
1079 | node->mAxis = axis;
|
---|
1080 | node->mPosition = position;
|
---|
1081 | node->mBox = box;
|
---|
1082 |
|
---|
1083 | backBBox = box;
|
---|
1084 | frontBBox = box;
|
---|
1085 |
|
---|
1086 | VspKdLeaf *back = new VspKdLeaf(node, raysBack, maxCostMisses);
|
---|
1087 | back->SetPvsSize(pvsBack);
|
---|
1088 | VspKdLeaf *front = new VspKdLeaf(node, raysFront, maxCostMisses);
|
---|
1089 | front->SetPvsSize(pvsFront);
|
---|
1090 |
|
---|
1091 | // replace a link from node's parent
|
---|
1092 | if (leaf->mParent)
|
---|
1093 | leaf->mParent->ReplaceChildLink(leaf, node);
|
---|
1094 | // and setup child links
|
---|
1095 | node->SetupChildLinks(back, front);
|
---|
1096 |
|
---|
1097 | if (axis <= VspKdNode::SPLIT_Z)
|
---|
1098 | {
|
---|
1099 | backBBox.SetMax(axis, position);
|
---|
1100 | frontBBox.SetMin(axis, position);
|
---|
1101 |
|
---|
1102 | for(RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
1103 | ri != leaf->mRays.end(); ++ ri)
|
---|
1104 | {
|
---|
1105 | if ((*ri).mRay->IsActive())
|
---|
1106 | {
|
---|
1107 | // first unref ray from the former leaf
|
---|
1108 | (*ri).mRay->Unref();
|
---|
1109 | float t;
|
---|
1110 | // determine the side of this ray with respect to the plane
|
---|
1111 | int side = node->ComputeRayIntersection(*ri, t);
|
---|
1112 |
|
---|
1113 | if (side == 0)
|
---|
1114 | {
|
---|
1115 | if ((*ri).mRay->HasPosDir(axis))
|
---|
1116 | {
|
---|
1117 | back->AddRay(RayInfo((*ri).mRay,
|
---|
1118 | (*ri).mMinT,
|
---|
1119 | t));
|
---|
1120 | front->AddRay(RayInfo((*ri).mRay,
|
---|
1121 | t,
|
---|
1122 | (*ri).mMaxT));
|
---|
1123 | }
|
---|
1124 | else
|
---|
1125 | {
|
---|
1126 | back->AddRay(RayInfo((*ri).mRay,
|
---|
1127 | t,
|
---|
1128 | (*ri).mMaxT));
|
---|
1129 | front->AddRay(RayInfo((*ri).mRay,
|
---|
1130 | (*ri).mMinT,
|
---|
1131 | t));
|
---|
1132 | }
|
---|
1133 | }
|
---|
1134 | else
|
---|
1135 | if (side == 1)
|
---|
1136 | front->AddRay(*ri);
|
---|
1137 | else
|
---|
1138 | back->AddRay(*ri);
|
---|
1139 | } else
|
---|
1140 | (*ri).mRay->Unref();
|
---|
1141 | }
|
---|
1142 | }
|
---|
1143 | else
|
---|
1144 | {
|
---|
1145 | // rays front/back
|
---|
1146 |
|
---|
1147 | for (RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
1148 | ri != leaf->mRays.end(); ++ ri)
|
---|
1149 | {
|
---|
1150 | if ((*ri).mRay->IsActive())
|
---|
1151 | {
|
---|
1152 | // first unref ray from the former leaf
|
---|
1153 | (*ri).mRay->Unref();
|
---|
1154 |
|
---|
1155 | int side;
|
---|
1156 | if ((*ri).mRay->GetDirParametrization(axis - 3) > position)
|
---|
1157 | side = 1;
|
---|
1158 | else
|
---|
1159 | side = -1;
|
---|
1160 |
|
---|
1161 | if (side == 1)
|
---|
1162 | front->AddRay(*ri);
|
---|
1163 | else
|
---|
1164 | back->AddRay(*ri);
|
---|
1165 | }
|
---|
1166 | else
|
---|
1167 | (*ri).mRay->Unref();
|
---|
1168 | }
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | // update stats
|
---|
1172 | mStat.rayRefs -= (int)leaf->mRays.size();
|
---|
1173 | mStat.rayRefs += raysBack + raysFront;
|
---|
1174 |
|
---|
1175 | DEL_PTR(leaf);
|
---|
1176 |
|
---|
1177 | return node;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | int VspKdTree::ReleaseMemory(const int time)
|
---|
1181 | {
|
---|
1182 | stack<VspKdNode *> tstack;
|
---|
1183 |
|
---|
1184 | // find a node in the tree which subtree will be collapsed
|
---|
1185 | int maxAccessTime = time - mAccessTimeThreshold;
|
---|
1186 | int released = 0;
|
---|
1187 |
|
---|
1188 | tstack.push(mRoot);
|
---|
1189 |
|
---|
1190 | while (!tstack.empty())
|
---|
1191 | {
|
---|
1192 | VspKdNode *node = tstack.top();
|
---|
1193 | tstack.pop();
|
---|
1194 |
|
---|
1195 | if (!node->IsLeaf())
|
---|
1196 | {
|
---|
1197 | VspKdInterior *in = dynamic_cast<VspKdInterior *>(node);
|
---|
1198 | // cout<<"depth="<<(int)in->depth<<" time="<<in->lastAccessTime<<endl;
|
---|
1199 | if (in->mDepth >= mMinCollapseDepth && in->mLastAccessTime <= maxAccessTime)
|
---|
1200 | {
|
---|
1201 | released = CollapseSubtree(node, time);
|
---|
1202 | break;
|
---|
1203 | }
|
---|
1204 | if (in->GetBack()->GetAccessTime() < in->GetFront()->GetAccessTime())
|
---|
1205 | {
|
---|
1206 | tstack.push(in->GetFront());
|
---|
1207 | tstack.push(in->GetBack());
|
---|
1208 | }
|
---|
1209 | else
|
---|
1210 | {
|
---|
1211 | tstack.push(in->GetBack());
|
---|
1212 | tstack.push(in->GetFront());
|
---|
1213 | }
|
---|
1214 | }
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | while (tstack.empty())
|
---|
1218 | {
|
---|
1219 | // could find node to collaps...
|
---|
1220 | // cout<<"Could not find a node to release "<<endl;
|
---|
1221 | break;
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | return released;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 |
|
---|
1228 | VspKdNode *VspKdTree::SubdivideLeaf(VspKdLeaf *leaf,
|
---|
1229 | const float sizeThreshold)
|
---|
1230 | {
|
---|
1231 | VspKdNode *node = leaf;
|
---|
1232 |
|
---|
1233 | AxisAlignedBox3 leafBBox = GetBBox(leaf);
|
---|
1234 |
|
---|
1235 | static int pass = 0;
|
---|
1236 | ++ pass;
|
---|
1237 |
|
---|
1238 | // check if we should perform a dynamic subdivision of the leaf
|
---|
1239 | if (// leaf->mRays.size() > (unsigned)termMinCost &&
|
---|
1240 | (leaf->GetPvsSize() >= mTermMinPvs) &&
|
---|
1241 | (SqrMagnitude(leafBBox.Size()) > sizeThreshold) )
|
---|
1242 | {
|
---|
1243 | // memory check and release
|
---|
1244 | if (GetMemUsage() > mMaxTotalMemory)
|
---|
1245 | ReleaseMemory(pass);
|
---|
1246 |
|
---|
1247 | AxisAlignedBox3 backBBox, frontBBox;
|
---|
1248 |
|
---|
1249 | // subdivide the node
|
---|
1250 | node = SubdivideNode(leaf, leafBBox, backBBox, frontBBox);
|
---|
1251 | }
|
---|
1252 | return node;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 |
|
---|
1256 |
|
---|
1257 | void VspKdTree::UpdateRays(VssRayContainer &remove,
|
---|
1258 | VssRayContainer &add)
|
---|
1259 | {
|
---|
1260 | VspKdLeaf::NewMail();
|
---|
1261 |
|
---|
1262 | // schedule rays for removal
|
---|
1263 | for(VssRayContainer::const_iterator ri = remove.begin();
|
---|
1264 | ri != remove.end(); ++ ri)
|
---|
1265 | {
|
---|
1266 | (*ri)->ScheduleForRemoval();
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | int inactive = 0;
|
---|
1270 |
|
---|
1271 | for (VssRayContainer::const_iterator ri = remove.begin(); ri != remove.end(); ++ ri)
|
---|
1272 | {
|
---|
1273 | if ((*ri)->ScheduledForRemoval())
|
---|
1274 | // RemoveRay(*ri, NULL, false);
|
---|
1275 | // !!! BUG - with true it does not work correctly - aggreated delete
|
---|
1276 | RemoveRay(*ri, NULL, true);
|
---|
1277 | else
|
---|
1278 | ++ inactive;
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | // cout<<"all/inactive"<<remove.size()<<"/"<<inactive<<endl;
|
---|
1282 | for (VssRayContainer::const_iterator ri = add.begin(); ri != add.end(); ++ ri)
|
---|
1283 | {
|
---|
1284 | AddRay(*ri);
|
---|
1285 | }
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 |
|
---|
1289 | void VspKdTree::RemoveRay(VssRay *ray,
|
---|
1290 | vector<VspKdLeaf *> *affectedLeaves,
|
---|
1291 | const bool removeAllScheduledRays)
|
---|
1292 | {
|
---|
1293 | stack<RayTraversalData> tstack;
|
---|
1294 |
|
---|
1295 | tstack.push(RayTraversalData(mRoot, RayInfo(ray)));
|
---|
1296 |
|
---|
1297 | RayTraversalData data;
|
---|
1298 |
|
---|
1299 | // cout<<"Number of ray refs = "<<ray->RefCount()<<endl;
|
---|
1300 | while (!tstack.empty())
|
---|
1301 | {
|
---|
1302 | data = tstack.top();
|
---|
1303 | tstack.pop();
|
---|
1304 |
|
---|
1305 | if (!data.mNode->IsLeaf())
|
---|
1306 | {
|
---|
1307 | // split the set of rays in two groups intersecting the
|
---|
1308 | // two subtrees
|
---|
1309 | TraverseInternalNode(data, tstack);
|
---|
1310 | }
|
---|
1311 | else
|
---|
1312 | {
|
---|
1313 | // remove the ray from the leaf
|
---|
1314 | // find the ray in the leaf and swap it with the last ray...
|
---|
1315 | VspKdLeaf *leaf = (VspKdLeaf *)data.mNode;
|
---|
1316 |
|
---|
1317 | if (!leaf->Mailed())
|
---|
1318 | {
|
---|
1319 | leaf->Mail();
|
---|
1320 |
|
---|
1321 | if (affectedLeaves)
|
---|
1322 | affectedLeaves->push_back(leaf);
|
---|
1323 |
|
---|
1324 | if (removeAllScheduledRays)
|
---|
1325 | {
|
---|
1326 | int tail = (int)leaf->mRays.size() - 1;
|
---|
1327 |
|
---|
1328 | for (int i=0; i < (int)(leaf->mRays.size()); ++ i)
|
---|
1329 | {
|
---|
1330 | if (leaf->mRays[i].mRay->ScheduledForRemoval())
|
---|
1331 | {
|
---|
1332 | // find a ray to replace it with
|
---|
1333 | while (tail >= i && leaf->mRays[tail].mRay->ScheduledForRemoval())
|
---|
1334 | {
|
---|
1335 | ++ mStat.removedRayRefs;
|
---|
1336 | leaf->mRays[tail].mRay->Unref();
|
---|
1337 | leaf->mRays.pop_back();
|
---|
1338 |
|
---|
1339 | -- tail;
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | if (tail < i)
|
---|
1343 | break;
|
---|
1344 |
|
---|
1345 | ++ mStat.removedRayRefs;
|
---|
1346 |
|
---|
1347 | leaf->mRays[i].mRay->Unref();
|
---|
1348 | leaf->mRays[i] = leaf->mRays[tail];
|
---|
1349 | leaf->mRays.pop_back();
|
---|
1350 | -- tail;
|
---|
1351 | }
|
---|
1352 | }
|
---|
1353 | }
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | if (!removeAllScheduledRays)
|
---|
1357 | for (int i=0; i < (int)leaf->mRays.size(); i++)
|
---|
1358 | {
|
---|
1359 | if (leaf->mRays[i].mRay == ray)
|
---|
1360 | {
|
---|
1361 | ++ mStat.removedRayRefs;
|
---|
1362 | ray->Unref();
|
---|
1363 | leaf->mRays[i] = leaf->mRays[leaf->mRays.size() - 1];
|
---|
1364 | leaf->mRays.pop_back();
|
---|
1365 | // check this ray again
|
---|
1366 | break;
|
---|
1367 | }
|
---|
1368 | }
|
---|
1369 | }
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | if (ray->RefCount() != 0)
|
---|
1373 | {
|
---|
1374 | cerr << "Error: Number of remaining refs = " << ray->RefCount() << endl;
|
---|
1375 | exit(1);
|
---|
1376 | }
|
---|
1377 |
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | void VspKdTree::AddRay(VssRay *ray)
|
---|
1381 | {
|
---|
1382 | stack<RayTraversalData> tstack;
|
---|
1383 |
|
---|
1384 | tstack.push(RayTraversalData(mRoot, RayInfo(ray)));
|
---|
1385 |
|
---|
1386 | RayTraversalData data;
|
---|
1387 |
|
---|
1388 | while (!tstack.empty())
|
---|
1389 | {
|
---|
1390 | data = tstack.top();
|
---|
1391 | tstack.pop();
|
---|
1392 |
|
---|
1393 | if (!data.mNode->IsLeaf())
|
---|
1394 | {
|
---|
1395 | TraverseInternalNode(data, tstack);
|
---|
1396 | }
|
---|
1397 | else
|
---|
1398 | {
|
---|
1399 | // remove the ray from the leaf
|
---|
1400 | // find the ray in the leaf and swap it with the last ray
|
---|
1401 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(data.mNode);
|
---|
1402 |
|
---|
1403 | leaf->AddRay(data.mRayData);
|
---|
1404 | ++ mStat.addedRayRefs;
|
---|
1405 | }
|
---|
1406 | }
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | void VspKdTree::TraverseInternalNode(RayTraversalData &data,
|
---|
1410 | stack<RayTraversalData> &tstack)
|
---|
1411 | {
|
---|
1412 | VspKdInterior *in = dynamic_cast<VspKdInterior *>(data.mNode);
|
---|
1413 |
|
---|
1414 | if (in->mAxis <= VspKdNode::SPLIT_Z)
|
---|
1415 | {
|
---|
1416 | // determine the side of this ray with respect to the plane
|
---|
1417 | float t;
|
---|
1418 | int side = in->ComputeRayIntersection(data.mRayData, t);
|
---|
1419 |
|
---|
1420 | if (side == 0)
|
---|
1421 | {
|
---|
1422 | if (data.mRayData.mRay->HasPosDir(in->mAxis))
|
---|
1423 | {
|
---|
1424 | tstack.push(RayTraversalData(in->GetBack(),
|
---|
1425 | RayInfo(data.mRayData.mRay, data.mRayData.mMinT, t)));
|
---|
1426 |
|
---|
1427 | tstack.push(RayTraversalData(in->GetFront(),
|
---|
1428 | RayInfo(data.mRayData.mRay, t, data.mRayData.mMaxT)));
|
---|
1429 |
|
---|
1430 | }
|
---|
1431 | else
|
---|
1432 | {
|
---|
1433 | tstack.push(RayTraversalData(in->GetBack(),
|
---|
1434 | RayInfo(data.mRayData.mRay,
|
---|
1435 | t,
|
---|
1436 | data.mRayData.mMaxT)));
|
---|
1437 | tstack.push(RayTraversalData(in->GetFront(),
|
---|
1438 | RayInfo(data.mRayData.mRay,
|
---|
1439 | data.mRayData.mMinT,
|
---|
1440 | t)));
|
---|
1441 | }
|
---|
1442 | }
|
---|
1443 | else
|
---|
1444 | if (side == 1)
|
---|
1445 | tstack.push(RayTraversalData(in->GetFront(), data.mRayData));
|
---|
1446 | else
|
---|
1447 | tstack.push(RayTraversalData(in->GetBack(), data.mRayData));
|
---|
1448 | }
|
---|
1449 | else
|
---|
1450 | {
|
---|
1451 | // directional split
|
---|
1452 | if (data.mRayData.mRay->GetDirParametrization(in->mAxis - 3) > in->mPosition)
|
---|
1453 | tstack.push(RayTraversalData(in->GetFront(), data.mRayData));
|
---|
1454 | else
|
---|
1455 | tstack.push(RayTraversalData(in->GetBack(), data.mRayData));
|
---|
1456 | }
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 |
|
---|
1460 | int VspKdTree::CollapseSubtree(VspKdNode *sroot, const int time)
|
---|
1461 | {
|
---|
1462 | // first count all rays in the subtree
|
---|
1463 | // use mail 1 for this purpose
|
---|
1464 | stack<VspKdNode *> tstack;
|
---|
1465 |
|
---|
1466 | int rayCount = 0;
|
---|
1467 | int totalRayCount = 0;
|
---|
1468 | int collapsedNodes = 0;
|
---|
1469 |
|
---|
1470 | #if DEBUG_COLLAPSE
|
---|
1471 | cout << "Collapsing subtree" << endl;
|
---|
1472 | cout << "accessTime=" << sroot->GetAccessTime() << endl;
|
---|
1473 | cout << "depth=" << (int)sroot->depth << endl;
|
---|
1474 | #endif
|
---|
1475 |
|
---|
1476 | // tstat.collapsedSubtrees++;
|
---|
1477 | // tstat.collapseDepths += (int)sroot->depth;
|
---|
1478 | // tstat.collapseAccessTimes += time - sroot->GetAccessTime();
|
---|
1479 | tstack.push(sroot);
|
---|
1480 | VssRay::NewMail();
|
---|
1481 |
|
---|
1482 | while (!tstack.empty())
|
---|
1483 | {
|
---|
1484 | ++ collapsedNodes;
|
---|
1485 |
|
---|
1486 | VspKdNode *node = tstack.top();
|
---|
1487 | tstack.pop();
|
---|
1488 | if (node->IsLeaf())
|
---|
1489 | {
|
---|
1490 | VspKdLeaf *leaf = (VspKdLeaf *) node;
|
---|
1491 |
|
---|
1492 | for(RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
1493 | ri != leaf->mRays.end(); ++ ri)
|
---|
1494 | {
|
---|
1495 | ++ totalRayCount;
|
---|
1496 |
|
---|
1497 | if ((*ri).mRay->IsActive() && !(*ri).mRay->Mailed())
|
---|
1498 | {
|
---|
1499 | (*ri).mRay->Mail();
|
---|
1500 | ++ rayCount;
|
---|
1501 | }
|
---|
1502 | }
|
---|
1503 | }
|
---|
1504 | else
|
---|
1505 | {
|
---|
1506 | tstack.push(dynamic_cast<VspKdInterior *>(node)->GetFront());
|
---|
1507 | tstack.push(dynamic_cast<VspKdInterior *>(node)->GetBack());
|
---|
1508 | }
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | VssRay::NewMail();
|
---|
1512 |
|
---|
1513 | // create a new node that will hold the rays
|
---|
1514 | VspKdLeaf *newLeaf = new VspKdLeaf(sroot->mParent, rayCount);
|
---|
1515 |
|
---|
1516 | if (newLeaf->mParent)
|
---|
1517 | {
|
---|
1518 | newLeaf->mParent->ReplaceChildLink(sroot, newLeaf);
|
---|
1519 | }
|
---|
1520 | tstack.push(sroot);
|
---|
1521 |
|
---|
1522 | while (!tstack.empty())
|
---|
1523 | {
|
---|
1524 | VspKdNode *node = tstack.top();
|
---|
1525 | tstack.pop();
|
---|
1526 |
|
---|
1527 | if (node->IsLeaf())
|
---|
1528 | {
|
---|
1529 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node);
|
---|
1530 |
|
---|
1531 | for(RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
1532 | ri != leaf->mRays.end(); ++ ri)
|
---|
1533 | {
|
---|
1534 | // unref this ray from the old node
|
---|
1535 | if ((*ri).mRay->IsActive())
|
---|
1536 | {
|
---|
1537 | (*ri).mRay->Unref();
|
---|
1538 | if (!(*ri).mRay->Mailed())
|
---|
1539 | {
|
---|
1540 | (*ri).mRay->Mail();
|
---|
1541 | newLeaf->AddRay(*ri);
|
---|
1542 | }
|
---|
1543 | }
|
---|
1544 | else
|
---|
1545 | (*ri).mRay->Unref();
|
---|
1546 | }
|
---|
1547 | }
|
---|
1548 | else
|
---|
1549 | {
|
---|
1550 | VspKdInterior *interior =
|
---|
1551 | dynamic_cast<VspKdInterior *>(node);
|
---|
1552 | tstack.push(interior->GetBack());
|
---|
1553 | tstack.push(interior->GetFront());
|
---|
1554 | }
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 | // delete the node and all its children
|
---|
1558 | DEL_PTR(sroot);
|
---|
1559 |
|
---|
1560 | // for(VspKdNode::SRayContainer::iterator ri = newleaf->mRays.begin();
|
---|
1561 | // ri != newleaf->mRays.end(); ++ ri)
|
---|
1562 | // (*ri).ray->UnMail(2);
|
---|
1563 |
|
---|
1564 | #if DEBUG_COLLAPSE
|
---|
1565 | cout << "Total memory before=" << GetMemUsage() << endl;
|
---|
1566 | #endif
|
---|
1567 |
|
---|
1568 | mStat.nodes -= collapsedNodes - 1;
|
---|
1569 | mStat.rayRefs -= totalRayCount - rayCount;
|
---|
1570 |
|
---|
1571 | #if DEBUG_COLLAPSE
|
---|
1572 | cout << "collapsed nodes" << collapsedNodes << endl;
|
---|
1573 | cout << "collapsed rays" << totalRayCount - rayCount << endl;
|
---|
1574 | cout << "Total memory after=" << GetMemUsage() << endl;
|
---|
1575 | cout << "================================" << endl;
|
---|
1576 | #endif
|
---|
1577 |
|
---|
1578 | // tstat.collapsedNodes += collapsedNodes;
|
---|
1579 | // tstat.collapsedRays += totalRayCount - rayCount;
|
---|
1580 | return totalRayCount - rayCount;
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 |
|
---|
1584 | int VspKdTree::GetPvsSize(VspKdNode *node, const AxisAlignedBox3 &box) const
|
---|
1585 | {
|
---|
1586 | stack<VspKdNode *> tstack;
|
---|
1587 | tstack.push(mRoot);
|
---|
1588 |
|
---|
1589 | Intersectable::NewMail();
|
---|
1590 | int pvsSize = 0;
|
---|
1591 |
|
---|
1592 | while (!tstack.empty())
|
---|
1593 | {
|
---|
1594 | VspKdNode *node = tstack.top();
|
---|
1595 | tstack.pop();
|
---|
1596 |
|
---|
1597 | if (node->IsLeaf())
|
---|
1598 | {
|
---|
1599 | VspKdLeaf *leaf = (VspKdLeaf *)node;
|
---|
1600 | for (RayInfoContainer::iterator ri = leaf->mRays.begin();
|
---|
1601 | ri != leaf->mRays.end(); ++ ri)
|
---|
1602 | {
|
---|
1603 | if ((*ri).mRay->IsActive())
|
---|
1604 | {
|
---|
1605 | Intersectable *object;
|
---|
1606 | #if BIDIRECTIONAL_RAY
|
---|
1607 | object = (*ri).mRay->mOriginObject;
|
---|
1608 |
|
---|
1609 | if (object && !object->Mailed())
|
---|
1610 | {
|
---|
1611 | ++ pvsSize;
|
---|
1612 | object->Mail();
|
---|
1613 | }
|
---|
1614 | #endif
|
---|
1615 | object = (*ri).mRay->mTerminationObject;
|
---|
1616 | if (object && !object->Mailed())
|
---|
1617 | {
|
---|
1618 | ++ pvsSize;
|
---|
1619 | object->Mail();
|
---|
1620 | }
|
---|
1621 | }
|
---|
1622 | }
|
---|
1623 | }
|
---|
1624 | else
|
---|
1625 | {
|
---|
1626 | VspKdInterior *in = dynamic_cast<VspKdInterior *>(node);
|
---|
1627 |
|
---|
1628 | if (in->mAxis < 3)
|
---|
1629 | {
|
---|
1630 | if (box.Max(in->mAxis) >= in->mPosition)
|
---|
1631 | tstack.push(in->GetFront());
|
---|
1632 |
|
---|
1633 | if (box.Min(in->mAxis) <= in->mPosition)
|
---|
1634 | tstack.push(in->GetBack());
|
---|
1635 | }
|
---|
1636 | else
|
---|
1637 | {
|
---|
1638 | // both nodes for directional splits
|
---|
1639 | tstack.push(in->GetFront());
|
---|
1640 | tstack.push(in->GetBack());
|
---|
1641 | }
|
---|
1642 | }
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | return pvsSize;
|
---|
1646 | }
|
---|
1647 |
|
---|
1648 | void VspKdTree::GetRayContributionStatistics(float &minRayContribution,
|
---|
1649 | float &maxRayContribution,
|
---|
1650 | float &avgRayContribution)
|
---|
1651 | {
|
---|
1652 | stack<VspKdNode *> tstack;
|
---|
1653 | tstack.push(mRoot);
|
---|
1654 |
|
---|
1655 | minRayContribution = 1.0f;
|
---|
1656 | maxRayContribution = 0.0f;
|
---|
1657 |
|
---|
1658 | float sumRayContribution = 0.0f;
|
---|
1659 | int leaves = 0;
|
---|
1660 |
|
---|
1661 | while (!tstack.empty())
|
---|
1662 | {
|
---|
1663 | VspKdNode *node = tstack.top();
|
---|
1664 | tstack.pop();
|
---|
1665 | if (node->IsLeaf())
|
---|
1666 | {
|
---|
1667 | leaves++;
|
---|
1668 | VspKdLeaf *leaf = (VspKdLeaf *)node;
|
---|
1669 | float c = leaf->GetAvgRayContribution();
|
---|
1670 |
|
---|
1671 | if (c > maxRayContribution)
|
---|
1672 | maxRayContribution = c;
|
---|
1673 | if (c < minRayContribution)
|
---|
1674 | minRayContribution = c;
|
---|
1675 | sumRayContribution += c;
|
---|
1676 |
|
---|
1677 | }
|
---|
1678 | else {
|
---|
1679 | VspKdInterior *in = (VspKdInterior *)node;
|
---|
1680 | // both nodes for directional splits
|
---|
1681 | tstack.push(in->GetFront());
|
---|
1682 | tstack.push(in->GetBack());
|
---|
1683 | }
|
---|
1684 | }
|
---|
1685 |
|
---|
1686 | Debug << "sum=" << sumRayContribution << endl;
|
---|
1687 | Debug << "leaves=" << leaves << endl;
|
---|
1688 | avgRayContribution = sumRayContribution / (float)leaves;
|
---|
1689 | }
|
---|
1690 |
|
---|
1691 |
|
---|
1692 | int VspKdTree::GenerateRays(const float ratioPerLeaf,
|
---|
1693 | SimpleRayContainer &rays)
|
---|
1694 | {
|
---|
1695 | stack<VspKdNode *> tstack;
|
---|
1696 | tstack.push(mRoot);
|
---|
1697 |
|
---|
1698 | while (!tstack.empty())
|
---|
1699 | {
|
---|
1700 | VspKdNode *node = tstack.top();
|
---|
1701 | tstack.pop();
|
---|
1702 |
|
---|
1703 | if (node->IsLeaf())
|
---|
1704 | {
|
---|
1705 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node);
|
---|
1706 |
|
---|
1707 | float c = leaf->GetAvgRayContribution();
|
---|
1708 | int num = (int)(c*ratioPerLeaf + 0.5);
|
---|
1709 | // cout<<num<<" ";
|
---|
1710 |
|
---|
1711 | for (int i=0; i < num; i++)
|
---|
1712 | {
|
---|
1713 | Vector3 origin = GetBBox(leaf).GetRandomPoint();
|
---|
1714 | /*Vector3 dirVector = GetDirBBox(leaf).GetRandomPoint();
|
---|
1715 | Vector3 direction = Vector3(sin(dirVector.x), sin(dirVector.y), cos(dirVector.x));
|
---|
1716 | //cout<<"dir vector.x="<<dirVector.x<<"direction'.x="<<atan2(direction.x, direction.y)<<endl;
|
---|
1717 | rays.push_back(SimpleRay(origin, direction));*/
|
---|
1718 | }
|
---|
1719 |
|
---|
1720 | }
|
---|
1721 | else
|
---|
1722 | {
|
---|
1723 | VspKdInterior *in =
|
---|
1724 | dynamic_cast<VspKdInterior *>(node);
|
---|
1725 | // both nodes for directional splits
|
---|
1726 | tstack.push(in->GetFront());
|
---|
1727 | tstack.push(in->GetBack());
|
---|
1728 | }
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 | return (int)rays.size();
|
---|
1732 | }
|
---|
1733 |
|
---|
1734 |
|
---|
1735 | float VspKdTree::GetAvgPvsSize()
|
---|
1736 | {
|
---|
1737 | stack<VspKdNode *> tstack;
|
---|
1738 | tstack.push(mRoot);
|
---|
1739 |
|
---|
1740 | int sumPvs = 0;
|
---|
1741 | int leaves = 0;
|
---|
1742 |
|
---|
1743 | while (!tstack.empty())
|
---|
1744 | {
|
---|
1745 | VspKdNode *node = tstack.top();
|
---|
1746 | tstack.pop();
|
---|
1747 |
|
---|
1748 | if (node->IsLeaf())
|
---|
1749 | {
|
---|
1750 | VspKdLeaf *leaf = (VspKdLeaf *)node;
|
---|
1751 | // update pvs size
|
---|
1752 | leaf->UpdatePvsSize();
|
---|
1753 | sumPvs += leaf->GetPvsSize();
|
---|
1754 | leaves++;
|
---|
1755 | }
|
---|
1756 | else
|
---|
1757 | {
|
---|
1758 | VspKdInterior *in = (VspKdInterior *)node;
|
---|
1759 | // both nodes for directional splits
|
---|
1760 | tstack.push(in->GetFront());
|
---|
1761 | tstack.push(in->GetBack());
|
---|
1762 | }
|
---|
1763 | }
|
---|
1764 |
|
---|
1765 | return sumPvs / (float)leaves;
|
---|
1766 | }
|
---|
1767 |
|
---|
1768 | VspKdNode *VspKdTree::GetRoot() const
|
---|
1769 | {
|
---|
1770 | return mRoot;
|
---|
1771 | }
|
---|
1772 |
|
---|
1773 | AxisAlignedBox3 VspKdTree::GetBBox(VspKdNode *node) const
|
---|
1774 | {
|
---|
1775 | if (node->mParent == NULL)
|
---|
1776 | return mBox;
|
---|
1777 |
|
---|
1778 | if (!node->IsLeaf())
|
---|
1779 | return (dynamic_cast<VspKdInterior *>(node))->mBox;
|
---|
1780 |
|
---|
1781 | if (node->mParent->mAxis >= 3)
|
---|
1782 | return node->mParent->mBox;
|
---|
1783 |
|
---|
1784 | AxisAlignedBox3 box(node->mParent->mBox);
|
---|
1785 | if (node->mParent->GetFront() == node)
|
---|
1786 | box.SetMin(node->mParent->mAxis, node->mParent->mPosition);
|
---|
1787 | else
|
---|
1788 | box.SetMax(node->mParent->mAxis, node->mParent->mPosition);
|
---|
1789 |
|
---|
1790 | return box;
|
---|
1791 | }
|
---|
1792 |
|
---|
1793 | int VspKdTree::GetRootPvsSize() const
|
---|
1794 | {
|
---|
1795 | return GetPvsSize(mRoot, mBox);
|
---|
1796 | }
|
---|
1797 |
|
---|
1798 | const VspKdStatistics &VspKdTree::GetStatistics() const
|
---|
1799 | {
|
---|
1800 | return mStat;
|
---|
1801 | }
|
---|
1802 |
|
---|
1803 | void VspKdTree::AddRays(VssRayContainer &add)
|
---|
1804 | {
|
---|
1805 | VssRayContainer remove;
|
---|
1806 | UpdateRays(remove, add);
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 | // return memory usage in MB
|
---|
1810 | float VspKdTree::GetMemUsage() const
|
---|
1811 | {
|
---|
1812 | return
|
---|
1813 | (sizeof(VspKdTree) +
|
---|
1814 | mStat.Leaves() * sizeof(VspKdLeaf) +
|
---|
1815 | mStat.Interior() * sizeof(VspKdInterior) +
|
---|
1816 | mStat.rayRefs * sizeof(RayInfo)) / (1024.0f * 1024.0f);
|
---|
1817 | }
|
---|
1818 |
|
---|
1819 | float VspKdTree::GetRayMemUsage() const
|
---|
1820 | {
|
---|
1821 | return mStat.rays * (sizeof(VssRay))/(1024.0f * 1024.0f);
|
---|
1822 | }
|
---|
1823 |
|
---|
1824 | int VspKdTree::ComputePvsSize(VspKdNode *node,
|
---|
1825 | const RayInfoContainer &globalRays) const
|
---|
1826 | {
|
---|
1827 | int pvsSize = 0;
|
---|
1828 |
|
---|
1829 | const AxisAlignedBox3 box = GetBBox(node);
|
---|
1830 |
|
---|
1831 | RayInfoContainer::const_iterator it, it_end = globalRays.end();
|
---|
1832 |
|
---|
1833 | // warning: implicit conversion from VssRay to Ray
|
---|
1834 | for (it = globalRays.begin(); it != globalRays.end(); ++ it)
|
---|
1835 | pvsSize += box.GetMinMaxT(*(*it).mRay, NULL, NULL);
|
---|
1836 |
|
---|
1837 | return pvsSize;
|
---|
1838 | }
|
---|
1839 |
|
---|
1840 | void VspKdTree::CollectLeaves(vector<VspKdLeaf *> &leaves) const
|
---|
1841 | {
|
---|
1842 | stack<VspKdNode *> nodeStack;
|
---|
1843 | nodeStack.push(mRoot);
|
---|
1844 |
|
---|
1845 | while (!nodeStack.empty())
|
---|
1846 | {
|
---|
1847 | VspKdNode *node = nodeStack.top();
|
---|
1848 |
|
---|
1849 | nodeStack.pop();
|
---|
1850 |
|
---|
1851 | if (node->IsLeaf())
|
---|
1852 | {
|
---|
1853 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node);
|
---|
1854 | leaves.push_back(leaf);
|
---|
1855 | }
|
---|
1856 | else
|
---|
1857 | {
|
---|
1858 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node);
|
---|
1859 |
|
---|
1860 | nodeStack.push(interior->GetBack());
|
---|
1861 | nodeStack.push(interior->GetFront());
|
---|
1862 | }
|
---|
1863 | }
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | int VspKdTree::FindNeighbors(VspKdLeaf *n,
|
---|
1867 | vector<VspKdLeaf *> &neighbors,
|
---|
1868 | bool onlyUnmailed)
|
---|
1869 | {
|
---|
1870 | stack<VspKdNode *> nodeStack;
|
---|
1871 |
|
---|
1872 | nodeStack.push(mRoot);
|
---|
1873 |
|
---|
1874 | AxisAlignedBox3 box = GetBBox(n);
|
---|
1875 |
|
---|
1876 | while (!nodeStack.empty())
|
---|
1877 | {
|
---|
1878 | VspKdNode *node = nodeStack.top();
|
---|
1879 | nodeStack.pop();
|
---|
1880 |
|
---|
1881 | if (node->IsLeaf())
|
---|
1882 | {
|
---|
1883 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node);
|
---|
1884 |
|
---|
1885 | if (leaf != n && (!onlyUnmailed || !leaf->Mailed()))
|
---|
1886 | neighbors.push_back(leaf);
|
---|
1887 | }
|
---|
1888 | else
|
---|
1889 | {
|
---|
1890 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node);
|
---|
1891 |
|
---|
1892 | if (interior->mPosition > box.Max(interior->mAxis))
|
---|
1893 | nodeStack.push(interior->mBack);
|
---|
1894 | else
|
---|
1895 | {
|
---|
1896 | if (interior->mPosition < box.Min(interior->mAxis))
|
---|
1897 | nodeStack.push(interior->mFront);
|
---|
1898 | else
|
---|
1899 | {
|
---|
1900 | // random decision
|
---|
1901 | nodeStack.push(interior->mBack);
|
---|
1902 | nodeStack.push(interior->mFront);
|
---|
1903 | }
|
---|
1904 | }
|
---|
1905 | }
|
---|
1906 | }
|
---|
1907 |
|
---|
1908 | return (int)neighbors.size();
|
---|
1909 | }
|
---|
1910 |
|
---|
1911 |
|
---|
1912 | void VspKdTree::SetViewCellsManager(ViewCellsManager *vcm)
|
---|
1913 | {
|
---|
1914 | mViewCellsManager = vcm;
|
---|
1915 | }
|
---|
1916 |
|
---|
1917 |
|
---|
1918 | int VspKdTree::CastLineSegment(const Vector3 &origin,
|
---|
1919 | const Vector3 &termination,
|
---|
1920 | vector<ViewCell *> &viewcells)
|
---|
1921 | {
|
---|
1922 | int hits = 0;
|
---|
1923 |
|
---|
1924 | float mint = 0.0f, maxt = 1.0f;
|
---|
1925 | const Vector3 dir = termination - origin;
|
---|
1926 |
|
---|
1927 | stack<LineSegmentTraversalData > tStack;
|
---|
1928 |
|
---|
1929 | Intersectable::NewMail();
|
---|
1930 |
|
---|
1931 | Vector3 entp = origin;
|
---|
1932 | Vector3 extp = termination;
|
---|
1933 |
|
---|
1934 | VspKdNode *node = mRoot;
|
---|
1935 | VspKdNode *farChild;
|
---|
1936 |
|
---|
1937 | float position;
|
---|
1938 | int axis;
|
---|
1939 |
|
---|
1940 | while (1)
|
---|
1941 | {
|
---|
1942 | if (!node->IsLeaf())
|
---|
1943 | {
|
---|
1944 | VspKdInterior *in = dynamic_cast<VspKdInterior *>(node);
|
---|
1945 | position = in->mPosition;
|
---|
1946 | axis = in->mAxis;
|
---|
1947 |
|
---|
1948 | if (entp[axis] <= position)
|
---|
1949 | {
|
---|
1950 | if (extp[axis] <= position)
|
---|
1951 | {
|
---|
1952 | node = in->mBack;
|
---|
1953 | // cases N1,N2,N3,P5,Z2,Z3
|
---|
1954 | continue;
|
---|
1955 | } else
|
---|
1956 | {
|
---|
1957 | // case N4
|
---|
1958 | node = in->mBack;
|
---|
1959 | farChild = in->mFront;
|
---|
1960 | }
|
---|
1961 | }
|
---|
1962 | else
|
---|
1963 | {
|
---|
1964 | if (position <= extp[axis])
|
---|
1965 | {
|
---|
1966 | node = in->mFront;
|
---|
1967 | // cases P1,P2,P3,N5,Z1
|
---|
1968 | continue;
|
---|
1969 | }
|
---|
1970 | else
|
---|
1971 | {
|
---|
1972 | node = in->mFront;
|
---|
1973 | farChild = in->mBack;
|
---|
1974 | // case P4
|
---|
1975 | }
|
---|
1976 | }
|
---|
1977 |
|
---|
1978 | // $$ modification 3.5.2004 - hints from Kamil Ghais
|
---|
1979 | // case N4 or P4
|
---|
1980 | float tdist = (position - origin[axis]) / dir[axis];
|
---|
1981 | //tStack.push(RayTraversalData(farChild, extp, maxt)); //TODO
|
---|
1982 | extp = origin + dir * tdist;
|
---|
1983 | maxt = tdist;
|
---|
1984 | }
|
---|
1985 | else
|
---|
1986 | {
|
---|
1987 | // compute intersection with all objects in this leaf
|
---|
1988 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node);
|
---|
1989 | ViewCell *vc = leaf->GetViewCell();
|
---|
1990 |
|
---|
1991 | if (!vc->Mailed())
|
---|
1992 | {
|
---|
1993 | vc->Mail();
|
---|
1994 | viewcells.push_back(vc);
|
---|
1995 | ++ hits;
|
---|
1996 | }
|
---|
1997 |
|
---|
1998 | // get the next node from the stack
|
---|
1999 | if (tStack.empty())
|
---|
2000 | break;
|
---|
2001 |
|
---|
2002 | entp = extp;
|
---|
2003 | mint = maxt;
|
---|
2004 |
|
---|
2005 | LineSegmentTraversalData &s = tStack.top();
|
---|
2006 | node = s.mNode;
|
---|
2007 | extp = s.mExitPoint;
|
---|
2008 | maxt = s.mMaxT;
|
---|
2009 | tStack.pop();
|
---|
2010 | }
|
---|
2011 | }
|
---|
2012 |
|
---|
2013 | return hits;
|
---|
2014 | }
|
---|
2015 |
|
---|
2016 |
|
---|
2017 | void VspKdTree::GenerateViewCell(VspKdLeaf *leaf)
|
---|
2018 | {
|
---|
2019 | RayInfoContainer::const_iterator it, it_end = leaf->GetRays().end();
|
---|
2020 |
|
---|
2021 | VspKdViewCell *vc = dynamic_cast<VspKdViewCell *>(mViewCellsManager->GenerateViewCell());
|
---|
2022 | leaf->SetViewCell(vc);
|
---|
2023 |
|
---|
2024 | vc->SetArea(GetBBox(leaf).GetVolume());
|
---|
2025 | vc->mLeaves.push_back(leaf);
|
---|
2026 |
|
---|
2027 | for (it = leaf->GetRays().begin(); it != it_end; ++ it)
|
---|
2028 | {
|
---|
2029 | VssRay *ray = (*it).mRay;
|
---|
2030 |
|
---|
2031 | if (ray->mTerminationObject)
|
---|
2032 | vc->GetPvs().AddSample(ray->mTerminationObject);
|
---|
2033 |
|
---|
2034 | if (ray->mOriginObject)
|
---|
2035 | vc->GetPvs().AddSample(ray->mOriginObject);
|
---|
2036 | }
|
---|
2037 | }
|
---|
2038 |
|
---|
2039 |
|
---|
2040 | void VspKdTree::EvaluateViewCellsStats(ViewCellsStatistics &vcStat) const
|
---|
2041 | {
|
---|
2042 | vcStat.Reset();
|
---|
2043 |
|
---|
2044 | stack<VspKdNode *> nodeStack;
|
---|
2045 | nodeStack.push(mRoot);
|
---|
2046 |
|
---|
2047 | ViewCell::NewMail();
|
---|
2048 |
|
---|
2049 | while (!nodeStack.empty())
|
---|
2050 | {
|
---|
2051 | VspKdNode *node = nodeStack.top();
|
---|
2052 | nodeStack.pop();
|
---|
2053 |
|
---|
2054 | if (node->IsLeaf())
|
---|
2055 | {
|
---|
2056 | ++ vcStat.leaves;
|
---|
2057 |
|
---|
2058 | VspKdViewCell *viewCell = dynamic_cast<VspKdLeaf *>(node)->mViewCell;
|
---|
2059 |
|
---|
2060 | if (!viewCell->Mailed())
|
---|
2061 | {
|
---|
2062 | viewCell->Mail();
|
---|
2063 |
|
---|
2064 | ++ vcStat.viewCells;
|
---|
2065 | const int pvsSize = viewCell->GetPvs().GetSize();
|
---|
2066 |
|
---|
2067 | vcStat.pvs += pvsSize;
|
---|
2068 |
|
---|
2069 | if (pvsSize < 1)
|
---|
2070 | ++ vcStat.emptyPvs;
|
---|
2071 |
|
---|
2072 | if (pvsSize > vcStat.maxPvs)
|
---|
2073 | vcStat.maxPvs = pvsSize;
|
---|
2074 |
|
---|
2075 | if (pvsSize < vcStat.minPvs)
|
---|
2076 | vcStat.minPvs = pvsSize;
|
---|
2077 |
|
---|
2078 | if ((int)viewCell->mLeaves.size() > vcStat.maxLeaves)
|
---|
2079 | vcStat.maxLeaves = (int)viewCell->mLeaves.size();
|
---|
2080 | }
|
---|
2081 | }
|
---|
2082 | else
|
---|
2083 | {
|
---|
2084 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node);
|
---|
2085 |
|
---|
2086 | nodeStack.push(interior->GetFront());
|
---|
2087 | nodeStack.push(interior->GetBack());
|
---|
2088 | }
|
---|
2089 | }
|
---|
2090 | }
|
---|
2091 |
|
---|
2092 |
|
---|
2093 |
|
---|
2094 | bool VspKdTree::MergeViewCells(VspKdLeaf *l1, VspKdLeaf *l2) |
---|
2095 | { |
---|
2096 | //-- change pointer to view cells of all leaves associated |
---|
2097 | //-- with the previous view cells |
---|
2098 | VspKdViewCell *fVc = l1->GetViewCell(); |
---|
2099 | VspKdViewCell *bVc = l2->GetViewCell(); |
---|
2100 | |
---|
2101 | VspKdViewCell *vc = dynamic_cast<VspKdViewCell *>( |
---|
2102 | mViewCellsManager->MergeViewCells(*fVc, *bVc)); |
---|
2103 | |
---|
2104 | // if merge was unsuccessful |
---|
2105 | if (!vc) return false; |
---|
2106 | |
---|
2107 | // set new size of view cell |
---|
2108 | vc->SetArea(fVc->GetVolume() + bVc->GetVolume()); |
---|
2109 | |
---|
2110 | vector<VspKdLeaf *> fLeaves = fVc->mLeaves; |
---|
2111 | vector<VspKdLeaf *> bLeaves = bVc->mLeaves; |
---|
2112 | |
---|
2113 | vector<VspKdLeaf *>::const_iterator it; |
---|
2114 | |
---|
2115 | //-- change view cells of all the other leaves the view cell belongs to |
---|
2116 | for (it = fLeaves.begin(); it != fLeaves.end(); ++ it) |
---|
2117 | { |
---|
2118 | (*it)->SetViewCell(vc); |
---|
2119 | vc->mLeaves.push_back(*it); |
---|
2120 | } |
---|
2121 | |
---|
2122 | for (it = bLeaves.begin(); it != bLeaves.end(); ++ it) |
---|
2123 | { |
---|
2124 | (*it)->SetViewCell(vc); |
---|
2125 | vc->mLeaves.push_back(*it); |
---|
2126 | } |
---|
2127 | |
---|
2128 | vc->Mail();
|
---|
2129 | |
---|
2130 | // clean up old view cells |
---|
2131 | DEL_PTR(fVc); |
---|
2132 | DEL_PTR(bVc); |
---|
2133 | |
---|
2134 | return true;
|
---|
2135 | }
|
---|
2136 |
|
---|
2137 |
|
---|
2138 |
|
---|
2139 | int VspKdTree::MergeLeaves()
|
---|
2140 | {
|
---|
2141 | vector<VspKdLeaf *> leaves;
|
---|
2142 | priority_queue<MergeCandidate> mergeQueue;
|
---|
2143 |
|
---|
2144 | // collect the leaves, e.g., the "voxels" that will build the view cells
|
---|
2145 | CollectLeaves(leaves);
|
---|
2146 |
|
---|
2147 | int vcSize = (int)leaves.size();
|
---|
2148 | int savedVcSize = vcSize;
|
---|
2149 |
|
---|
2150 | VspKdLeaf::NewMail();
|
---|
2151 |
|
---|
2152 | vector<VspKdLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
2153 |
|
---|
2154 | // generate view cells
|
---|
2155 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
2156 | GenerateViewCell(*it);
|
---|
2157 |
|
---|
2158 | // find merge candidates and push them into queue
|
---|
2159 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
2160 | {
|
---|
2161 | VspKdLeaf *leaf = *it;
|
---|
2162 |
|
---|
2163 | // no leaf is part of two merge candidates
|
---|
2164 | if (!leaf->Mailed())
|
---|
2165 | {
|
---|
2166 | leaf->Mail();
|
---|
2167 |
|
---|
2168 | vector<VspKdLeaf *> neighbors;
|
---|
2169 | FindNeighbors(leaf, neighbors, true);
|
---|
2170 |
|
---|
2171 | vector<VspKdLeaf *>::const_iterator nit,
|
---|
2172 | nit_end = neighbors.end();
|
---|
2173 |
|
---|
2174 | for (nit = neighbors.begin(); nit != nit_end; ++ nit)
|
---|
2175 | {
|
---|
2176 | MergeCandidate mc = MergeCandidate(leaf, *nit);
|
---|
2177 | mergeQueue.push(mc);
|
---|
2178 |
|
---|
2179 | MergeCandidate::sOverallCost += mc.GetLeaf1Cost();
|
---|
2180 | MergeCandidate::sOverallCost += mc.GetLeaf2Cost();
|
---|
2181 | }
|
---|
2182 | }
|
---|
2183 | }
|
---|
2184 |
|
---|
2185 | int merged = 0;
|
---|
2186 |
|
---|
2187 | //Debug << mMinViewCells << " " << mMaxCostRatio << endl;
|
---|
2188 |
|
---|
2189 | // use priority queue to merge leaves
|
---|
2190 | while (!mergeQueue.empty() &&
|
---|
2191 | (vcSize > mMergeMinViewCells) &&
|
---|
2192 | (mergeQueue.top().GetMergeCost() <
|
---|
2193 | mMergeMaxCostRatio / MergeCandidate::sOverallCost))
|
---|
2194 | {
|
---|
2195 | //Debug << mergeQueue.top().GetMergeCost() << " " << mMaxCostRatio << endl;
|
---|
2196 | MergeCandidate mc = mergeQueue.top();
|
---|
2197 | mergeQueue.pop();
|
---|
2198 |
|
---|
2199 | // both view cells equal!
|
---|
2200 | if (mc.GetLeaf1()->GetViewCell() == mc.GetLeaf2()->GetViewCell())
|
---|
2201 | continue;
|
---|
2202 |
|
---|
2203 | if (mc.Valid())
|
---|
2204 | {
|
---|
2205 | ViewCell::NewMail();
|
---|
2206 | MergeViewCells(mc.GetLeaf1(), mc.GetLeaf2());
|
---|
2207 |
|
---|
2208 | ++ merged;
|
---|
2209 | -- vcSize;
|
---|
2210 | // increase absolute merge cost
|
---|
2211 | MergeCandidate::sOverallCost += mc.GetMergeCost();
|
---|
2212 | }
|
---|
2213 | // merge candidate not valid, because one of the leaves was already
|
---|
2214 | // merged with another one
|
---|
2215 | else
|
---|
2216 | {
|
---|
2217 | // validate and reinsert into queue
|
---|
2218 | mc.SetValid();
|
---|
2219 | mergeQueue.push(mc);
|
---|
2220 | //Debug << "validate " << mc.GetMergeCost() << endl;
|
---|
2221 | }
|
---|
2222 | }
|
---|
2223 |
|
---|
2224 | // collapse tree according to view cell partition
|
---|
2225 | CollapseTree(mRoot);
|
---|
2226 |
|
---|
2227 | // revalidate leaves
|
---|
2228 | ValidateViewCellLeaves();
|
---|
2229 |
|
---|
2230 | //Debug << "merged " << merged << " of " << savedVcSize << " leaves" << endl;
|
---|
2231 |
|
---|
2232 | //TODO: should return sample contributions
|
---|
2233 | return merged;
|
---|
2234 | }
|
---|
2235 |
|
---|
2236 |
|
---|
2237 | void VspKdTree::ValidateViewCellLeaves()
|
---|
2238 | { |
---|
2239 | // list not valid anymore => clear
|
---|
2240 | stack<VspKdNode *> nodeStack; |
---|
2241 | nodeStack.push(mRoot); |
---|
2242 | |
---|
2243 | ViewCell::NewMail(); |
---|
2244 | |
---|
2245 | while (!nodeStack.empty()) |
---|
2246 | { |
---|
2247 | VspKdNode *node = nodeStack.top(); |
---|
2248 | nodeStack.pop(); |
---|
2249 | |
---|
2250 | if (node->IsLeaf()) |
---|
2251 | { |
---|
2252 | VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node); |
---|
2253 | |
---|
2254 | VspKdViewCell *viewCell = leaf->GetViewCell(); |
---|
2255 | |
---|
2256 | if (!viewCell->Mailed()) |
---|
2257 | { |
---|
2258 | viewCell->mLeaves.clear(); |
---|
2259 | viewCell->Mail(); |
---|
2260 | } |
---|
2261 | |
---|
2262 | viewCell->mLeaves.push_back(leaf); |
---|
2263 | } |
---|
2264 | else |
---|
2265 | { |
---|
2266 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node); |
---|
2267 | |
---|
2268 | nodeStack.push(interior->GetFront()); |
---|
2269 | nodeStack.push(interior->GetBack()); |
---|
2270 | } |
---|
2271 | }
|
---|
2272 | }
|
---|
2273 |
|
---|
2274 | VspKdNode *VspKdTree::CollapseTree(VspKdNode *node)
|
---|
2275 | {
|
---|
2276 | if (node->IsLeaf())
|
---|
2277 | return node;
|
---|
2278 |
|
---|
2279 | VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node); |
---|
2280 | |
---|
2281 | VspKdNode *front = CollapseTree(interior->GetFront()); |
---|
2282 | VspKdNode *back = CollapseTree(interior->GetBack()); |
---|
2283 | |
---|
2284 | if (front->IsLeaf() && back->IsLeaf())
|
---|
2285 | {
|
---|
2286 | VspKdLeaf *frontLeaf = dynamic_cast<VspKdLeaf *>(front);
|
---|
2287 | VspKdLeaf *backLeaf = dynamic_cast<VspKdLeaf *>(back);
|
---|
2288 |
|
---|
2289 | //-- collapse tree
|
---|
2290 | if (frontLeaf->GetViewCell() == backLeaf->GetViewCell())
|
---|
2291 | {
|
---|
2292 | VspKdViewCell *vc = frontLeaf->GetViewCell();
|
---|
2293 |
|
---|
2294 | VspKdLeaf *leaf = new VspKdLeaf(interior->GetParent(), 0);
|
---|
2295 | leaf->SetViewCell(vc);
|
---|
2296 |
|
---|
2297 | // replace a link from node's parent
|
---|
2298 | if (leaf->mParent)
|
---|
2299 | leaf->mParent->ReplaceChildLink(node, leaf);
|
---|
2300 |
|
---|
2301 | delete interior;
|
---|
2302 |
|
---|
2303 | return leaf;
|
---|
2304 | }
|
---|
2305 | }
|
---|
2306 |
|
---|
2307 | return node;
|
---|
2308 | }
|
---|
2309 |
|
---|
2310 |
|
---|
2311 | void VspKdTree::RefineViewCells()
|
---|
2312 | {
|
---|
2313 | }
|
---|
2314 |
|
---|
2315 |
|
---|
2316 | /*********************************************************************/
|
---|
2317 | /* MergeCandidate implementation */
|
---|
2318 | /*********************************************************************/
|
---|
2319 |
|
---|
2320 |
|
---|
2321 | MergeCandidate::MergeCandidate(VspKdLeaf *l1, VspKdLeaf *l2):
|
---|
2322 | mMergeCost(0),
|
---|
2323 | mLeaf1(l1),
|
---|
2324 | mLeaf2(l2),
|
---|
2325 | mLeaf1Id(l1->GetViewCell()->mMailbox),
|
---|
2326 | mLeaf2Id(l2->GetViewCell()->mMailbox)
|
---|
2327 | {
|
---|
2328 | EvalMergeCost();
|
---|
2329 | }
|
---|
2330 |
|
---|
2331 | float MergeCandidate::GetLeaf1Cost() const
|
---|
2332 | {
|
---|
2333 | VspKdViewCell *vc = mLeaf1->GetViewCell();
|
---|
2334 | return vc->GetPvs().GetSize() * vc->GetVolume();
|
---|
2335 | }
|
---|
2336 |
|
---|
2337 | float MergeCandidate::GetLeaf2Cost() const
|
---|
2338 | {
|
---|
2339 | VspKdViewCell *vc = mLeaf2->GetViewCell();
|
---|
2340 | return vc->GetPvs().GetSize() * vc->GetVolume();
|
---|
2341 | }
|
---|
2342 |
|
---|
2343 | void MergeCandidate::EvalMergeCost()
|
---|
2344 | {
|
---|
2345 | //-- compute pvs difference
|
---|
2346 | VspKdViewCell *vc1 = mLeaf1->GetViewCell();
|
---|
2347 | VspKdViewCell *vc2 = mLeaf2->GetViewCell();
|
---|
2348 |
|
---|
2349 | const int diff1 = vc1->GetPvs().Diff(vc2->GetPvs());
|
---|
2350 | const int vcPvs = diff1 + vc1->GetPvs().GetSize();
|
---|
2351 |
|
---|
2352 | //-- compute ratio of old cost
|
---|
2353 | //-- (added size of left and right view cell times pvs size)
|
---|
2354 | //-- to new rendering cost (size of merged view cell times pvs size)
|
---|
2355 | const float oldCost = GetLeaf1Cost() + GetLeaf2Cost();
|
---|
2356 |
|
---|
2357 | const float newCost =
|
---|
2358 | (float)vcPvs * (vc1->GetVolume() + vc2->GetVolume());
|
---|
2359 |
|
---|
2360 | mMergeCost = newCost - oldCost;
|
---|
2361 |
|
---|
2362 | // if (vcPvs > sMaxPvsSize) // strong penalty if pvs size too large
|
---|
2363 | // mMergeCost += 1.0;
|
---|
2364 | }
|
---|
2365 |
|
---|
2366 | void MergeCandidate::SetLeaf1(VspKdLeaf *l)
|
---|
2367 | {
|
---|
2368 | mLeaf1 = l;
|
---|
2369 | }
|
---|
2370 |
|
---|
2371 | void MergeCandidate::SetLeaf2(VspKdLeaf *l)
|
---|
2372 | {
|
---|
2373 | mLeaf2 = l;
|
---|
2374 | }
|
---|
2375 |
|
---|
2376 | VspKdLeaf *MergeCandidate::GetLeaf1()
|
---|
2377 | {
|
---|
2378 | return mLeaf1;
|
---|
2379 | }
|
---|
2380 |
|
---|
2381 | VspKdLeaf *MergeCandidate::GetLeaf2()
|
---|
2382 | {
|
---|
2383 | return mLeaf2;
|
---|
2384 | }
|
---|
2385 |
|
---|
2386 | bool MergeCandidate::Valid() const
|
---|
2387 | {
|
---|
2388 | return
|
---|
2389 | (mLeaf1->GetViewCell()->mMailbox == mLeaf1Id) &&
|
---|
2390 | (mLeaf2->GetViewCell()->mMailbox == mLeaf2Id);
|
---|
2391 | }
|
---|
2392 |
|
---|
2393 | float MergeCandidate::GetMergeCost() const
|
---|
2394 | {
|
---|
2395 | return mMergeCost;
|
---|
2396 | }
|
---|
2397 |
|
---|
2398 | void MergeCandidate::SetValid()
|
---|
2399 | {
|
---|
2400 | mLeaf1Id = mLeaf1->GetViewCell()->mMailbox;
|
---|
2401 | mLeaf2Id = mLeaf2->GetViewCell()->mMailbox;
|
---|
2402 |
|
---|
2403 | EvalMergeCost();
|
---|
2404 | }
|
---|