1 | #include <stack>
|
---|
2 | #include <time.h>
|
---|
3 | #include <iomanip>
|
---|
4 |
|
---|
5 | #include "Plane3.h"
|
---|
6 | #include "VspBspTree.h"
|
---|
7 | #include "Mesh.h"
|
---|
8 | #include "common.h"
|
---|
9 | #include "ViewCell.h"
|
---|
10 | #include "Environment.h"
|
---|
11 | #include "Polygon3.h"
|
---|
12 | #include "Ray.h"
|
---|
13 | #include "AxisAlignedBox3.h"
|
---|
14 | #include "Exporter.h"
|
---|
15 | #include "Plane3.h"
|
---|
16 | #include "ViewCellBsp.h"
|
---|
17 | #include "ViewCellsManager.h"
|
---|
18 | #include "Beam.h"
|
---|
19 |
|
---|
20 |
|
---|
21 | //-- static members
|
---|
22 |
|
---|
23 | /** Evaluates split plane classification with respect to the plane's
|
---|
24 | contribution for a minimum number of ray splits.
|
---|
25 | */
|
---|
26 | const float VspBspTree::sLeastRaySplitsTable[] = {0, 0, 1, 1, 0};
|
---|
27 | /** Evaluates split plane classification with respect to the plane's
|
---|
28 | contribution for balanced rays.
|
---|
29 | */
|
---|
30 | const float VspBspTree::sBalancedRaysTable[] = {1, -1, 0, 0, 0};
|
---|
31 |
|
---|
32 |
|
---|
33 | int VspBspTree::sFrontId = 0;
|
---|
34 | int VspBspTree::sBackId = 0;
|
---|
35 | int VspBspTree::sFrontAndBackId = 0;
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 | // pvs penalty can be different from pvs size
|
---|
41 | inline float EvalPvsPenalty(const int pvs,
|
---|
42 | const int lower,
|
---|
43 | const int upper)
|
---|
44 | {
|
---|
45 | // clamp to minmax values
|
---|
46 | if (pvs < lower)
|
---|
47 | return (float)lower;
|
---|
48 | if (pvs > upper)
|
---|
49 | return (float)upper;
|
---|
50 |
|
---|
51 | return (float)pvs;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | /******************************************************************************/
|
---|
58 | /* class VspBspTree implementation */
|
---|
59 | /******************************************************************************/
|
---|
60 |
|
---|
61 |
|
---|
62 | VspBspTree::VspBspTree():
|
---|
63 | mRoot(NULL),
|
---|
64 | mUseAreaForPvs(false),
|
---|
65 | mCostNormalizer(Limits::Small),
|
---|
66 | mViewCellsManager(NULL),
|
---|
67 | mOutOfBoundsCell(NULL),
|
---|
68 | mStoreRays(false),
|
---|
69 | mRenderCostWeight(0.5),
|
---|
70 | mUseRandomAxis(false)
|
---|
71 | {
|
---|
72 | bool randomize = false;
|
---|
73 | environment->GetBoolValue("VspBspTree.Construction.randomize", randomize);
|
---|
74 | if (randomize)
|
---|
75 | Randomize(); // initialise random generator for heuristics
|
---|
76 |
|
---|
77 | //-- termination criteria for autopartition
|
---|
78 | environment->GetIntValue("VspBspTree.Termination.maxDepth", mTermMaxDepth);
|
---|
79 | environment->GetIntValue("VspBspTree.Termination.minPvs", mTermMinPvs);
|
---|
80 | environment->GetIntValue("VspBspTree.Termination.minRays", mTermMinRays);
|
---|
81 | environment->GetFloatValue("VspBspTree.Termination.minProbability", mTermMinProbability);
|
---|
82 | environment->GetFloatValue("VspBspTree.Termination.maxRayContribution", mTermMaxRayContribution);
|
---|
83 | environment->GetFloatValue("VspBspTree.Termination.minAccRayLenght", mTermMinAccRayLength);
|
---|
84 | environment->GetFloatValue("VspBspTree.Termination.maxCostRatio", mTermMaxCostRatio);
|
---|
85 | environment->GetIntValue("VspBspTree.Termination.missTolerance", mTermMissTolerance);
|
---|
86 | environment->GetIntValue("VspBspTree.Termination.maxViewCells", mMaxViewCells);
|
---|
87 | //-- max cost ratio for early tree termination
|
---|
88 | environment->GetFloatValue("VspBspTree.Termination.maxCostRatio", mTermMaxCostRatio);
|
---|
89 | mTermMinPolygons = 25;
|
---|
90 | //-- factors for bsp tree split plane heuristics
|
---|
91 | environment->GetFloatValue("VspBspTree.Factor.balancedRays", mBalancedRaysFactor);
|
---|
92 | environment->GetFloatValue("VspBspTree.Factor.pvs", mPvsFactor);
|
---|
93 | environment->GetFloatValue("VspBspTree.Termination.ct_div_ci", mCtDivCi);
|
---|
94 |
|
---|
95 |
|
---|
96 | //-- partition criteria
|
---|
97 | environment->GetIntValue("VspBspTree.maxPolyCandidates", mMaxPolyCandidates);
|
---|
98 | environment->GetIntValue("VspBspTree.maxRayCandidates", mMaxRayCandidates);
|
---|
99 | environment->GetIntValue("VspBspTree.splitPlaneStrategy", mSplitPlaneStrategy);
|
---|
100 |
|
---|
101 | environment->GetFloatValue("VspBspTree.Construction.epsilon", mEpsilon);
|
---|
102 | environment->GetIntValue("VspBspTree.maxTests", mMaxTests);
|
---|
103 |
|
---|
104 | // if only the driving axis is used for axis aligned split
|
---|
105 | environment->GetBoolValue("VspBspTree.splitUseOnlyDrivingAxis", mOnlyDrivingAxis);
|
---|
106 |
|
---|
107 | //-- termination criteria for axis aligned split
|
---|
108 | environment->GetFloatValue("VspBspTree.Termination.AxisAligned.maxRayContribution",
|
---|
109 | mTermMaxRayContriForAxisAligned);
|
---|
110 | environment->GetIntValue("VspBspTree.Termination.AxisAligned.minRays",
|
---|
111 | mTermMinRaysForAxisAligned);
|
---|
112 |
|
---|
113 | //environment->GetFloatValue("VspBspTree.maxTotalMemory", mMaxTotalMemory);
|
---|
114 | environment->GetFloatValue("VspBspTree.maxStaticMemory", mMaxMemory);
|
---|
115 |
|
---|
116 | environment->GetFloatValue("VspBspTree.Construction.renderCostWeight", mRenderCostWeight);
|
---|
117 |
|
---|
118 |
|
---|
119 | mSubdivisionStats.open("subdivisionStats.log");
|
---|
120 |
|
---|
121 |
|
---|
122 | //-- debug output
|
---|
123 |
|
---|
124 | Debug << "******* VSP BSP options ******** " << endl;
|
---|
125 | Debug << "max depth: " << mTermMaxDepth << endl;
|
---|
126 | Debug << "min PVS: " << mTermMinPvs << endl;
|
---|
127 | Debug << "min probabiliy: " << mTermMinProbability << endl;
|
---|
128 | Debug << "min rays: " << mTermMinRays << endl;
|
---|
129 | Debug << "max ray contri: " << mTermMaxRayContribution << endl;
|
---|
130 | //Debug << "VSP BSP mininam accumulated ray lenght: ", mTermMinAccRayLength) << endl;
|
---|
131 | Debug << "max cost ratio: " << mTermMaxCostRatio << endl;
|
---|
132 | Debug << "miss tolerance: " << mTermMissTolerance << endl;
|
---|
133 | Debug << "max view cells: " << mMaxViewCells << endl;
|
---|
134 | Debug << "max polygon candidates: " << mMaxPolyCandidates << endl;
|
---|
135 | Debug << "max plane candidates: " << mMaxRayCandidates << endl;
|
---|
136 | Debug << "randomize: " << randomize << endl;
|
---|
137 |
|
---|
138 | Debug << "using area for pvs: " << mUseAreaForPvs << endl;
|
---|
139 | Debug << "render cost weight: " << mRenderCostWeight << endl;
|
---|
140 | Debug << "Split plane strategy: ";
|
---|
141 |
|
---|
142 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
143 | {
|
---|
144 | Debug << "random polygon ";
|
---|
145 | }
|
---|
146 | if (mSplitPlaneStrategy & AXIS_ALIGNED)
|
---|
147 | {
|
---|
148 | Debug << "axis aligned ";
|
---|
149 | }
|
---|
150 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
151 | {
|
---|
152 | mCostNormalizer += mLeastRaySplitsFactor;
|
---|
153 | Debug << "least ray splits ";
|
---|
154 | }
|
---|
155 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
156 | {
|
---|
157 | mCostNormalizer += mBalancedRaysFactor;
|
---|
158 | Debug << "balanced rays ";
|
---|
159 | }
|
---|
160 | if (mSplitPlaneStrategy & PVS)
|
---|
161 | {
|
---|
162 | mCostNormalizer += mPvsFactor;
|
---|
163 | Debug << "pvs";
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | mSplitCandidates = new vector<SortableEntry>;
|
---|
168 |
|
---|
169 | Debug << endl;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | BspViewCell *VspBspTree::GetOutOfBoundsCell()
|
---|
174 | {
|
---|
175 | return mOutOfBoundsCell;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | BspViewCell *VspBspTree::GetOrCreateOutOfBoundsCell()
|
---|
180 | {
|
---|
181 | if (!mOutOfBoundsCell)
|
---|
182 | {
|
---|
183 | mOutOfBoundsCell = new BspViewCell();
|
---|
184 | mOutOfBoundsCell->SetId(-1);
|
---|
185 | mOutOfBoundsCell->SetValid(false);
|
---|
186 | }
|
---|
187 |
|
---|
188 | return mOutOfBoundsCell;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | const BspTreeStatistics &VspBspTree::GetStatistics() const
|
---|
193 | {
|
---|
194 | return mBspStats;
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | VspBspTree::~VspBspTree()
|
---|
199 | {
|
---|
200 | DEL_PTR(mRoot);
|
---|
201 | DEL_PTR(mSplitCandidates);
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | int VspBspTree::AddMeshToPolygons(Mesh *mesh,
|
---|
206 | PolygonContainer &polys,
|
---|
207 | MeshInstance *parent)
|
---|
208 | {
|
---|
209 | FaceContainer::const_iterator fi;
|
---|
210 |
|
---|
211 | // copy the face data to polygons
|
---|
212 | for (fi = mesh->mFaces.begin(); fi != mesh->mFaces.end(); ++ fi)
|
---|
213 | {
|
---|
214 | Polygon3 *poly = new Polygon3((*fi), mesh);
|
---|
215 |
|
---|
216 | if (poly->Valid(mEpsilon))
|
---|
217 | {
|
---|
218 | poly->mParent = parent; // set parent intersectable
|
---|
219 | polys.push_back(poly);
|
---|
220 | }
|
---|
221 | else
|
---|
222 | DEL_PTR(poly);
|
---|
223 | }
|
---|
224 | return (int)mesh->mFaces.size();
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | int VspBspTree::AddToPolygonSoup(const ViewCellContainer &viewCells,
|
---|
229 | PolygonContainer &polys,
|
---|
230 | int maxObjects)
|
---|
231 | {
|
---|
232 | int limit = (maxObjects > 0) ?
|
---|
233 | Min((int)viewCells.size(), maxObjects) : (int)viewCells.size();
|
---|
234 |
|
---|
235 | int polysSize = 0;
|
---|
236 |
|
---|
237 | for (int i = 0; i < limit; ++ i)
|
---|
238 | {
|
---|
239 | if (viewCells[i]->GetMesh()) // copy the mesh data to polygons
|
---|
240 | {
|
---|
241 | mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb
|
---|
242 | polysSize +=
|
---|
243 | AddMeshToPolygons(viewCells[i]->GetMesh(),
|
---|
244 | polys,
|
---|
245 | viewCells[i]);
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | return polysSize;
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | int VspBspTree::AddToPolygonSoup(const ObjectContainer &objects,
|
---|
254 | PolygonContainer &polys,
|
---|
255 | int maxObjects)
|
---|
256 | {
|
---|
257 | int limit = (maxObjects > 0) ?
|
---|
258 | Min((int)objects.size(), maxObjects) : (int)objects.size();
|
---|
259 |
|
---|
260 | for (int i = 0; i < limit; ++i)
|
---|
261 | {
|
---|
262 | Intersectable *object = objects[i];//*it;
|
---|
263 | Mesh *mesh = NULL;
|
---|
264 |
|
---|
265 | switch (object->Type()) // extract the meshes
|
---|
266 | {
|
---|
267 | case Intersectable::MESH_INSTANCE:
|
---|
268 | mesh = dynamic_cast<MeshInstance *>(object)->GetMesh();
|
---|
269 | break;
|
---|
270 | case Intersectable::VIEW_CELL:
|
---|
271 | mesh = dynamic_cast<ViewCell *>(object)->GetMesh();
|
---|
272 | break;
|
---|
273 | // TODO: handle transformed mesh instances
|
---|
274 | default:
|
---|
275 | Debug << "intersectable type not supported" << endl;
|
---|
276 | break;
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (mesh) // copy the mesh data to polygons
|
---|
280 | {
|
---|
281 | mBox.Include(object->GetBox()); // add to BSP tree aabb
|
---|
282 | AddMeshToPolygons(mesh, polys, NULL);
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | return (int)polys.size();
|
---|
287 | }
|
---|
288 |
|
---|
289 |
|
---|
290 | void VspBspTree::Construct(const VssRayContainer &sampleRays,
|
---|
291 | AxisAlignedBox3 *forcedBoundingBox)
|
---|
292 | {
|
---|
293 | mBspStats.nodes = 1;
|
---|
294 | mBox.Initialize(); // initialise BSP tree bounding box
|
---|
295 |
|
---|
296 | if (forcedBoundingBox)
|
---|
297 | mBox = *forcedBoundingBox;
|
---|
298 |
|
---|
299 | PolygonContainer polys;
|
---|
300 | RayInfoContainer *rays = new RayInfoContainer();
|
---|
301 |
|
---|
302 | VssRayContainer::const_iterator rit, rit_end = sampleRays.end();
|
---|
303 |
|
---|
304 | long startTime = GetTime();
|
---|
305 |
|
---|
306 | cout << "Extracting polygons from rays ... ";
|
---|
307 |
|
---|
308 | Intersectable::NewMail();
|
---|
309 |
|
---|
310 | int numObj = 0;
|
---|
311 |
|
---|
312 | //-- extract polygons intersected by the rays
|
---|
313 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
314 | {
|
---|
315 | VssRay *ray = *rit;
|
---|
316 |
|
---|
317 | if ((mBox.IsInside(ray->mTermination) || !forcedBoundingBox) &&
|
---|
318 | ray->mTerminationObject &&
|
---|
319 | !ray->mTerminationObject->Mailed())
|
---|
320 | {
|
---|
321 | ray->mTerminationObject->Mail();
|
---|
322 | MeshInstance *obj = dynamic_cast<MeshInstance *>(ray->mTerminationObject);
|
---|
323 | AddMeshToPolygons(obj->GetMesh(), polys, obj);
|
---|
324 | ++ numObj;
|
---|
325 | //-- compute bounding box
|
---|
326 | if (!forcedBoundingBox)
|
---|
327 | mBox.Include(ray->mTermination);
|
---|
328 | }
|
---|
329 |
|
---|
330 | if ((mBox.IsInside(ray->mOrigin) || !forcedBoundingBox) &&
|
---|
331 | ray->mOriginObject &&
|
---|
332 | !ray->mOriginObject->Mailed())
|
---|
333 | {
|
---|
334 | ray->mOriginObject->Mail();
|
---|
335 | MeshInstance *obj = dynamic_cast<MeshInstance *>(ray->mOriginObject);
|
---|
336 | AddMeshToPolygons(obj->GetMesh(), polys, obj);
|
---|
337 | ++ numObj;
|
---|
338 |
|
---|
339 | //-- compute bounding box
|
---|
340 | if (!forcedBoundingBox)
|
---|
341 | mBox.Include(ray->mOrigin);
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | Debug << "maximal pvs (i.e., pvs still considered as valid) : " << mViewCellsManager->GetMaxPvsSize() << endl;
|
---|
346 |
|
---|
347 | //-- store rays
|
---|
348 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
349 | {
|
---|
350 | VssRay *ray = *rit;
|
---|
351 |
|
---|
352 | float minT, maxT;
|
---|
353 |
|
---|
354 | static Ray hray;
|
---|
355 | hray.Init(*ray);
|
---|
356 |
|
---|
357 | // TODO: not very efficient to implictly cast between rays types
|
---|
358 | if (mBox.GetRaySegment(hray, minT, maxT))
|
---|
359 | {
|
---|
360 | float len = ray->Length();
|
---|
361 |
|
---|
362 | if (!len)
|
---|
363 | len = Limits::Small;
|
---|
364 |
|
---|
365 | rays->push_back(RayInfo(ray, minT / len, maxT / len));
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | // normalize
|
---|
370 | if (mUseAreaForPvs)
|
---|
371 | mTermMinProbability *= mBox.SurfaceArea();
|
---|
372 | else
|
---|
373 | mTermMinProbability *= mBox.GetVolume();
|
---|
374 |
|
---|
375 | mBspStats.polys = (int)polys.size();
|
---|
376 |
|
---|
377 | cout << "finished" << endl;
|
---|
378 |
|
---|
379 | Debug << "\nPolygon extraction: " << (int)polys.size() << " polys extracted from "
|
---|
380 | << (int)sampleRays.size() << " rays in "
|
---|
381 | << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl << endl;
|
---|
382 |
|
---|
383 | Construct(polys, rays);
|
---|
384 |
|
---|
385 | // clean up polygons
|
---|
386 | CLEAR_CONTAINER(polys);
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | // return memory usage in MB
|
---|
391 | float VspBspTree::GetMemUsage(/*const VspBspTraversalQueue &tstack*/) const
|
---|
392 | {
|
---|
393 | return
|
---|
394 | (sizeof(VspBspTree) +
|
---|
395 | (float)mBspStats.Leaves() * sizeof(BspLeaf) +
|
---|
396 | // the nodes in the stack is the minimal additional number of leaves
|
---|
397 | //(float)tstack.size() * sizeof(BspLeaf) +
|
---|
398 | mBspStats.Interior() * sizeof(BspInterior) +
|
---|
399 | mBspStats.accumRays * sizeof(RayInfo)) / (1024.0f * 1024.0f);
|
---|
400 | }
|
---|
401 |
|
---|
402 |
|
---|
403 |
|
---|
404 | void VspBspTree::Construct(const PolygonContainer &polys, RayInfoContainer *rays)
|
---|
405 | {
|
---|
406 | VspBspTraversalQueue tQueue;
|
---|
407 |
|
---|
408 | mRoot = new BspLeaf();
|
---|
409 |
|
---|
410 | // constrruct root node geometry
|
---|
411 | BspNodeGeometry *geom = new BspNodeGeometry();
|
---|
412 | ConstructGeometry(mRoot, *geom);
|
---|
413 |
|
---|
414 | const float prop = mUseAreaForPvs ? geom->GetArea() : geom->GetVolume();
|
---|
415 |
|
---|
416 | VspBspTraversalData tData(mRoot,
|
---|
417 | new PolygonContainer(polys),
|
---|
418 | 0,
|
---|
419 | rays,
|
---|
420 | ComputePvsSize(*rays),
|
---|
421 | prop,
|
---|
422 | geom);
|
---|
423 |
|
---|
424 | // first node is kd node, i.e. an axis aligned box
|
---|
425 | if (1)
|
---|
426 | tData.mIsKdNode = true;
|
---|
427 | else
|
---|
428 | tData.mIsKdNode = false;
|
---|
429 |
|
---|
430 | tQueue.push(tData);
|
---|
431 |
|
---|
432 | mTotalCost = tData.mPvs * tData.mProbability / mBox.GetVolume();
|
---|
433 |
|
---|
434 | mSubdivisionStats
|
---|
435 | << "#ViewCells\n1\n" << endl
|
---|
436 | << "#RenderCostDecrease\n0\n" << endl
|
---|
437 | << "#TotalRenderCost\n" << mTotalCost << endl;
|
---|
438 |
|
---|
439 | Debug << "total cost: " << mTotalCost << endl;
|
---|
440 |
|
---|
441 |
|
---|
442 | mBspStats.Start();
|
---|
443 | cout << "Contructing vsp bsp tree ... \n";
|
---|
444 |
|
---|
445 | long startTime = GetTime();
|
---|
446 |
|
---|
447 | // used for intermediate time measurements and progress
|
---|
448 | long interTime = GetTime();
|
---|
449 |
|
---|
450 | mOutOfMemory = false;
|
---|
451 |
|
---|
452 | int nleaves = 500;
|
---|
453 |
|
---|
454 | while (!tQueue.empty())
|
---|
455 | {
|
---|
456 | tData = tQueue.top();
|
---|
457 | tQueue.pop();
|
---|
458 |
|
---|
459 | if (0 && !mOutOfMemory)
|
---|
460 | {
|
---|
461 | float mem = GetMemUsage();
|
---|
462 |
|
---|
463 | if (mem > mMaxMemory)
|
---|
464 | {
|
---|
465 | mOutOfMemory = true;
|
---|
466 | Debug << "memory limit reached: " << mem << endl;
|
---|
467 | }
|
---|
468 | }
|
---|
469 |
|
---|
470 | // subdivide leaf node
|
---|
471 | BspNode *r = Subdivide(tQueue, tData);
|
---|
472 |
|
---|
473 | if (r == mRoot)
|
---|
474 | Debug << "VSP BSP tree construction time spent at root: "
|
---|
475 | << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
476 |
|
---|
477 | if (mBspStats.Leaves() >= nleaves)
|
---|
478 | {
|
---|
479 | nleaves += 500;
|
---|
480 |
|
---|
481 | cout << "leaves=" << mBspStats.Leaves() << endl;
|
---|
482 | Debug << "needed "
|
---|
483 | << TimeDiff(interTime, GetTime())*1e-3
|
---|
484 | << " secs to create 500 leaves" << endl;
|
---|
485 | interTime = GetTime();
|
---|
486 | }
|
---|
487 | }
|
---|
488 |
|
---|
489 | Debug << "Used Memory: " << GetMemUsage() << " MB" << endl << endl;
|
---|
490 | cout << "finished\n";
|
---|
491 |
|
---|
492 | mBspStats.Stop();
|
---|
493 | }
|
---|
494 |
|
---|
495 |
|
---|
496 | bool VspBspTree::TerminationCriteriaMet(const VspBspTraversalData &data) const
|
---|
497 | {
|
---|
498 | return
|
---|
499 | (((int)data.mRays->size() <= mTermMinRays) ||
|
---|
500 | (data.mPvs <= mTermMinPvs) ||
|
---|
501 | (data.mProbability <= mTermMinProbability) ||
|
---|
502 | (mBspStats.Leaves() >= mMaxViewCells) ||
|
---|
503 | #if 0
|
---|
504 | (((int)data.mPolygons->size() <= mTermMinPolygons) && !data.mPolygons->empty())||
|
---|
505 | #endif
|
---|
506 | (data.GetAvgRayContribution() > mTermMaxRayContribution) ||
|
---|
507 | (data.mDepth >= mTermMaxDepth));
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 | BspNode *VspBspTree::Subdivide(VspBspTraversalQueue &tQueue,
|
---|
512 | VspBspTraversalData &tData)
|
---|
513 | {
|
---|
514 | BspNode *newNode = tData.mNode;
|
---|
515 |
|
---|
516 | if (!mOutOfMemory && !TerminationCriteriaMet(tData))
|
---|
517 | {
|
---|
518 | PolygonContainer coincident;
|
---|
519 |
|
---|
520 | VspBspTraversalData tFrontData;
|
---|
521 | VspBspTraversalData tBackData;
|
---|
522 |
|
---|
523 | // create new interior node and two leaf nodes
|
---|
524 | // or return leaf as it is (if maxCostRatio missed)
|
---|
525 | newNode = SubdivideNode(tData, tFrontData, tBackData, coincident);
|
---|
526 |
|
---|
527 | if (!newNode->IsLeaf()) //-- continue subdivision
|
---|
528 | {
|
---|
529 | if (1)
|
---|
530 | {
|
---|
531 | float cFront = tFrontData.mPvs * tFrontData.mProbability;
|
---|
532 | float cBack = tBackData.mPvs * tBackData.mProbability;
|
---|
533 | float cData = tData.mPvs * tData.mProbability;;
|
---|
534 |
|
---|
535 | float costDecr =
|
---|
536 | (cFront + cBack - cData) / mBox.GetVolume();
|
---|
537 | mTotalCost += costDecr;
|
---|
538 |
|
---|
539 | mSubdivisionStats
|
---|
540 | << "#ViewCells\n" << mBspStats.Leaves() << endl
|
---|
541 | << "#RenderCostDecrease\n" << -costDecr << endl
|
---|
542 | << "#TotalRenderCost\n" << mTotalCost << endl;
|
---|
543 | }
|
---|
544 |
|
---|
545 | // push the children on the stack
|
---|
546 | tQueue.push(tFrontData);
|
---|
547 | tQueue.push(tBackData);
|
---|
548 |
|
---|
549 | // delete old leaf node
|
---|
550 | DEL_PTR(tData.mNode);
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 | //-- terminate traversal and create new view cell
|
---|
555 | if (newNode->IsLeaf())
|
---|
556 | {
|
---|
557 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(newNode);
|
---|
558 | BspViewCell *viewCell = new BspViewCell();
|
---|
559 |
|
---|
560 | leaf->SetViewCell(viewCell);
|
---|
561 |
|
---|
562 | //-- update pvs
|
---|
563 | int conSamp = 0;
|
---|
564 | float sampCon = 0.0f;
|
---|
565 | AddToPvs(leaf, *tData.mRays, sampCon, conSamp);
|
---|
566 |
|
---|
567 | mBspStats.contributingSamples += conSamp;
|
---|
568 | mBspStats.sampleContributions +=(int) sampCon;
|
---|
569 |
|
---|
570 | //-- store additional info
|
---|
571 | if (mStoreRays)
|
---|
572 | {
|
---|
573 | RayInfoContainer::const_iterator it, it_end = tData.mRays->end();
|
---|
574 | for (it = tData.mRays->begin(); it != it_end; ++ it)
|
---|
575 | leaf->mVssRays.push_back((*it).mRay);
|
---|
576 | }
|
---|
577 | // should I check here?
|
---|
578 | if (0 && !mViewCellsManager->CheckValidity(viewCell, 0, mViewCellsManager->GetMaxPvsSize()))
|
---|
579 | {
|
---|
580 | viewCell->SetValid(false);
|
---|
581 | leaf->SetTreeValid(false);
|
---|
582 | PropagateUpValidity(leaf);
|
---|
583 |
|
---|
584 | ++ mBspStats.invalidLeaves;
|
---|
585 | }
|
---|
586 |
|
---|
587 | viewCell->mLeaf = leaf;
|
---|
588 |
|
---|
589 | if (mUseAreaForPvs)
|
---|
590 | viewCell->SetArea(tData.mProbability);
|
---|
591 | else
|
---|
592 | viewCell->SetVolume(tData.mProbability);
|
---|
593 |
|
---|
594 | leaf->mProbability = tData.mProbability;
|
---|
595 |
|
---|
596 | EvaluateLeafStats(tData);
|
---|
597 | }
|
---|
598 |
|
---|
599 | //-- cleanup
|
---|
600 | tData.Clear();
|
---|
601 |
|
---|
602 | return newNode;
|
---|
603 | }
|
---|
604 |
|
---|
605 |
|
---|
606 | BspNode *VspBspTree::SubdivideNode(VspBspTraversalData &tData,
|
---|
607 | VspBspTraversalData &frontData,
|
---|
608 | VspBspTraversalData &backData,
|
---|
609 | PolygonContainer &coincident)
|
---|
610 | {
|
---|
611 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
|
---|
612 |
|
---|
613 | // select subdivision plane
|
---|
614 | Plane3 splitPlane;
|
---|
615 |
|
---|
616 | int maxCostMisses = tData.mMaxCostMisses;
|
---|
617 |
|
---|
618 | const bool success =
|
---|
619 | SelectPlane(splitPlane, leaf, tData, frontData, backData);
|
---|
620 |
|
---|
621 | if (!success)
|
---|
622 | {
|
---|
623 | ++ maxCostMisses;
|
---|
624 |
|
---|
625 | if (maxCostMisses > mTermMissTolerance)
|
---|
626 | {
|
---|
627 | // terminate branch because of max cost
|
---|
628 | ++ mBspStats.maxCostNodes;
|
---|
629 | return leaf;
|
---|
630 | }
|
---|
631 | }
|
---|
632 |
|
---|
633 | mBspStats.nodes += 2;
|
---|
634 |
|
---|
635 | //-- subdivide further
|
---|
636 | BspInterior *interior = new BspInterior(splitPlane);
|
---|
637 |
|
---|
638 | #ifdef _DEBUG
|
---|
639 | Debug << interior << endl;
|
---|
640 | #endif
|
---|
641 |
|
---|
642 | //-- the front and back traversal data is filled with the new values
|
---|
643 | frontData.mDepth = tData.mDepth + 1;
|
---|
644 | frontData.mPolygons = new PolygonContainer();
|
---|
645 | frontData.mRays = new RayInfoContainer();
|
---|
646 |
|
---|
647 | backData.mDepth = tData.mDepth + 1;
|
---|
648 | backData.mPolygons = new PolygonContainer();
|
---|
649 | backData.mRays = new RayInfoContainer();
|
---|
650 |
|
---|
651 | // subdivide rays
|
---|
652 | SplitRays(interior->GetPlane(),
|
---|
653 | *tData.mRays,
|
---|
654 | *frontData.mRays,
|
---|
655 | *backData.mRays);
|
---|
656 |
|
---|
657 | // subdivide polygons
|
---|
658 | SplitPolygons(interior->GetPlane(),
|
---|
659 | *tData.mPolygons,
|
---|
660 | *frontData.mPolygons,
|
---|
661 | *backData.mPolygons,
|
---|
662 | coincident);
|
---|
663 |
|
---|
664 |
|
---|
665 | // how often was max cost ratio missed in this branch?
|
---|
666 | frontData.mMaxCostMisses = maxCostMisses;
|
---|
667 | backData.mMaxCostMisses = maxCostMisses;
|
---|
668 |
|
---|
669 | // compute pvs
|
---|
670 | frontData.mPvs = ComputePvsSize(*frontData.mRays);
|
---|
671 | backData.mPvs = ComputePvsSize(*backData.mRays);
|
---|
672 |
|
---|
673 | // split front and back node geometry and compute area
|
---|
674 |
|
---|
675 | // if geometry was not already computed
|
---|
676 | if (!frontData.mGeometry && !backData.mGeometry)
|
---|
677 | {
|
---|
678 | frontData.mGeometry = new BspNodeGeometry();
|
---|
679 | backData.mGeometry = new BspNodeGeometry();
|
---|
680 |
|
---|
681 | tData.mGeometry->SplitGeometry(*frontData.mGeometry,
|
---|
682 | *backData.mGeometry,
|
---|
683 | interior->GetPlane(),
|
---|
684 | mBox,
|
---|
685 | mEpsilon);
|
---|
686 |
|
---|
687 | if (mUseAreaForPvs)
|
---|
688 | {
|
---|
689 | frontData.mProbability = frontData.mGeometry->GetArea();
|
---|
690 | backData.mProbability = backData.mGeometry->GetArea();
|
---|
691 | }
|
---|
692 | else
|
---|
693 | {
|
---|
694 | frontData.mProbability = frontData.mGeometry->GetVolume();
|
---|
695 | backData.mProbability = tData.mProbability - frontData.mProbability;
|
---|
696 | }
|
---|
697 | }
|
---|
698 |
|
---|
699 |
|
---|
700 | //-- create front and back leaf
|
---|
701 |
|
---|
702 | BspInterior *parent = leaf->GetParent();
|
---|
703 |
|
---|
704 | // replace a link from node's parent
|
---|
705 | if (parent)
|
---|
706 | {
|
---|
707 | parent->ReplaceChildLink(leaf, interior);
|
---|
708 | interior->SetParent(parent);
|
---|
709 | }
|
---|
710 | else // new root
|
---|
711 | {
|
---|
712 | mRoot = interior;
|
---|
713 | }
|
---|
714 |
|
---|
715 | // and setup child links
|
---|
716 | interior->SetupChildLinks(new BspLeaf(interior), new BspLeaf(interior));
|
---|
717 |
|
---|
718 | frontData.mNode = interior->GetFront();
|
---|
719 | backData.mNode = interior->GetBack();
|
---|
720 |
|
---|
721 | //DEL_PTR(leaf);
|
---|
722 | return interior;
|
---|
723 | }
|
---|
724 |
|
---|
725 |
|
---|
726 | void VspBspTree::AddToPvs(BspLeaf *leaf,
|
---|
727 | const RayInfoContainer &rays,
|
---|
728 | float &sampleContributions,
|
---|
729 | int &contributingSamples)
|
---|
730 | {
|
---|
731 | sampleContributions = 0;
|
---|
732 | contributingSamples = 0;
|
---|
733 |
|
---|
734 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
735 |
|
---|
736 | ViewCell *vc = leaf->GetViewCell();
|
---|
737 |
|
---|
738 | // add contributions from samples to the PVS
|
---|
739 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
740 | {
|
---|
741 | float sc = 0.0f;
|
---|
742 | VssRay *ray = (*it).mRay;
|
---|
743 | bool madeContrib = false;
|
---|
744 | float contribution;
|
---|
745 | if (ray->mTerminationObject) {
|
---|
746 | if (vc->GetPvs().AddSample(ray->mTerminationObject, ray->mPdf, contribution))
|
---|
747 | madeContrib = true;
|
---|
748 | sc += contribution;
|
---|
749 | }
|
---|
750 |
|
---|
751 | if (ray->mOriginObject) {
|
---|
752 | if (vc->GetPvs().AddSample(ray->mOriginObject, ray->mPdf, contribution))
|
---|
753 | madeContrib = true;
|
---|
754 | sc += contribution;
|
---|
755 | }
|
---|
756 |
|
---|
757 | sampleContributions += sc;
|
---|
758 | if (madeContrib)
|
---|
759 | ++ contributingSamples;
|
---|
760 |
|
---|
761 | //leaf->mVssRays.push_back(ray);
|
---|
762 | }
|
---|
763 | }
|
---|
764 |
|
---|
765 |
|
---|
766 | void VspBspTree::SortSplitCandidates(const RayInfoContainer &rays, const int axis)
|
---|
767 | {
|
---|
768 | mSplitCandidates->clear();
|
---|
769 |
|
---|
770 | int requestedSize = 2 * (int)(rays.size());
|
---|
771 | // creates a sorted split candidates array
|
---|
772 | if (mSplitCandidates->capacity() > 500000 &&
|
---|
773 | requestedSize < (int)(mSplitCandidates->capacity()/10) )
|
---|
774 | {
|
---|
775 | delete mSplitCandidates;
|
---|
776 | mSplitCandidates = new vector<SortableEntry>;
|
---|
777 | }
|
---|
778 |
|
---|
779 | mSplitCandidates->reserve(requestedSize);
|
---|
780 |
|
---|
781 | // insert all queries
|
---|
782 | for(RayInfoContainer::const_iterator ri = rays.begin(); ri < rays.end(); ++ ri)
|
---|
783 | {
|
---|
784 | bool positive = (*ri).mRay->HasPosDir(axis);
|
---|
785 | mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax,
|
---|
786 | (*ri).ExtrapOrigin(axis), (*ri).mRay));
|
---|
787 |
|
---|
788 | mSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin,
|
---|
789 | (*ri).ExtrapTermination(axis), (*ri).mRay));
|
---|
790 | }
|
---|
791 |
|
---|
792 | stable_sort(mSplitCandidates->begin(), mSplitCandidates->end());
|
---|
793 | }
|
---|
794 |
|
---|
795 |
|
---|
796 | float VspBspTree::BestCostRatioHeuristics(const RayInfoContainer &rays,
|
---|
797 | const AxisAlignedBox3 &box,
|
---|
798 | const int pvsSize,
|
---|
799 | const int &axis,
|
---|
800 | float &position)
|
---|
801 | {
|
---|
802 | int raysBack;
|
---|
803 | int raysFront;
|
---|
804 | int pvsBack;
|
---|
805 | int pvsFront;
|
---|
806 |
|
---|
807 | SortSplitCandidates(rays, axis);
|
---|
808 |
|
---|
809 | // go through the lists, count the number of objects left and right
|
---|
810 | // and evaluate the following cost funcion:
|
---|
811 | // C = ct_div_ci + (ql*rl + qr*rr)/queries
|
---|
812 |
|
---|
813 | int rl = 0, rr = (int)rays.size();
|
---|
814 | int pl = 0, pr = pvsSize;
|
---|
815 |
|
---|
816 | float minBox = box.Min(axis);
|
---|
817 | float maxBox = box.Max(axis);
|
---|
818 | float sizeBox = maxBox - minBox;
|
---|
819 |
|
---|
820 | float minBand = minBox + 0.1f*(maxBox - minBox);
|
---|
821 | float maxBand = minBox + 0.9f*(maxBox - minBox);
|
---|
822 |
|
---|
823 | float sum = rr*sizeBox;
|
---|
824 | float minSum = 1e20f;
|
---|
825 |
|
---|
826 | Intersectable::NewMail();
|
---|
827 |
|
---|
828 | // set all object as belonging to the fron pvs
|
---|
829 | for(RayInfoContainer::const_iterator ri = rays.begin(); ri != rays.end(); ++ ri)
|
---|
830 | {
|
---|
831 | if ((*ri).mRay->IsActive())
|
---|
832 | {
|
---|
833 | Intersectable *object = (*ri).mRay->mTerminationObject;
|
---|
834 |
|
---|
835 | if (object)
|
---|
836 | {
|
---|
837 | if (!object->Mailed())
|
---|
838 | {
|
---|
839 | object->Mail();
|
---|
840 | object->mCounter = 1;
|
---|
841 | }
|
---|
842 | else
|
---|
843 | ++ object->mCounter;
|
---|
844 | }
|
---|
845 | }
|
---|
846 | }
|
---|
847 |
|
---|
848 | Intersectable::NewMail();
|
---|
849 |
|
---|
850 | for (vector<SortableEntry>::const_iterator ci = mSplitCandidates->begin();
|
---|
851 | ci < mSplitCandidates->end(); ++ ci)
|
---|
852 | {
|
---|
853 | VssRay *ray;
|
---|
854 |
|
---|
855 | switch ((*ci).type)
|
---|
856 | {
|
---|
857 | case SortableEntry::ERayMin:
|
---|
858 | {
|
---|
859 | ++ rl;
|
---|
860 | ray = (VssRay *) (*ci).ray;
|
---|
861 |
|
---|
862 | Intersectable *object = ray->mTerminationObject;
|
---|
863 |
|
---|
864 | if (object && !object->Mailed())
|
---|
865 | {
|
---|
866 | object->Mail();
|
---|
867 | ++ pl;
|
---|
868 | }
|
---|
869 | break;
|
---|
870 | }
|
---|
871 | case SortableEntry::ERayMax:
|
---|
872 | {
|
---|
873 | -- rr;
|
---|
874 | ray = (VssRay *) (*ci).ray;
|
---|
875 |
|
---|
876 | Intersectable *object = ray->mTerminationObject;
|
---|
877 |
|
---|
878 | if (object)
|
---|
879 | {
|
---|
880 | if (-- object->mCounter == 0)
|
---|
881 | -- pr;
|
---|
882 | }
|
---|
883 |
|
---|
884 | break;
|
---|
885 | }
|
---|
886 | }
|
---|
887 |
|
---|
888 | // Note: sufficient to compare size of bounding boxes of front and back side?
|
---|
889 | if ((*ci).value > minBand && (*ci).value < maxBand)
|
---|
890 | {
|
---|
891 | sum = pl*((*ci).value - minBox) + pr*(maxBox - (*ci).value);
|
---|
892 |
|
---|
893 | // cout<<"pos="<<(*ci).value<<"\t q=("<<ql<<","<<qr<<")\t r=("<<rl<<","<<rr<<")"<<endl;
|
---|
894 | // cout<<"cost= "<<sum<<endl;
|
---|
895 |
|
---|
896 | if (sum < minSum)
|
---|
897 | {
|
---|
898 | minSum = sum;
|
---|
899 | position = (*ci).value;
|
---|
900 |
|
---|
901 | raysBack = rl;
|
---|
902 | raysFront = rr;
|
---|
903 |
|
---|
904 | pvsBack = pl;
|
---|
905 | pvsFront = pr;
|
---|
906 |
|
---|
907 | }
|
---|
908 | }
|
---|
909 | }
|
---|
910 |
|
---|
911 | float oldCost = (float)pvsSize;
|
---|
912 | float newCost = mCtDivCi + minSum / sizeBox;
|
---|
913 | float ratio = newCost / oldCost;
|
---|
914 |
|
---|
915 | //Debug << "costRatio=" << ratio << " pos=" << position << " t=" << (position - minBox) / (maxBox - minBox)
|
---|
916 | // <<"\t q=(" << queriesBack << "," << queriesFront << ")\t r=(" << raysBack << "," << raysFront << ")" << endl;
|
---|
917 |
|
---|
918 | return ratio;
|
---|
919 | }
|
---|
920 |
|
---|
921 |
|
---|
922 | float VspBspTree::SelectAxisAlignedPlane(Plane3 &plane,
|
---|
923 | const VspBspTraversalData &tData,
|
---|
924 | int &axis,
|
---|
925 | BspNodeGeometry **frontGeom,
|
---|
926 | BspNodeGeometry **backGeom,
|
---|
927 | float &pFront,
|
---|
928 | float &pBack,
|
---|
929 | const bool useKdSplit)
|
---|
930 | {
|
---|
931 | const bool useCostHeuristics = false;
|
---|
932 |
|
---|
933 | //-- regular split
|
---|
934 | float nPosition[3];
|
---|
935 | float nCostRatio[3];
|
---|
936 | float nProbFront[3];
|
---|
937 | float nProbBack[3];
|
---|
938 |
|
---|
939 | BspNodeGeometry *nFrontGeom[3];
|
---|
940 | BspNodeGeometry *nBackGeom[3];
|
---|
941 |
|
---|
942 | int bestAxis = -1;
|
---|
943 |
|
---|
944 | // create bounding box of node geometry
|
---|
945 | AxisAlignedBox3 box;
|
---|
946 | box.Initialize();
|
---|
947 |
|
---|
948 | //TODO: for kd split geometry already is box => only take minmax vertices
|
---|
949 | if (1)
|
---|
950 | {
|
---|
951 | PolygonContainer::const_iterator it, it_end = tData.mGeometry->mPolys.end();
|
---|
952 |
|
---|
953 | for(it = tData.mGeometry->mPolys.begin(); it < it_end; ++ it)
|
---|
954 | box.Include(*(*it));
|
---|
955 | }
|
---|
956 | else
|
---|
957 | {
|
---|
958 | RayInfoContainer::const_iterator ri, ri_end = tData.mRays->end();
|
---|
959 |
|
---|
960 | for(ri = tData.mRays->begin(); ri < ri_end; ++ ri)
|
---|
961 | box.Include((*ri).ExtrapTermination());
|
---|
962 | }
|
---|
963 |
|
---|
964 | const int sAxis = mUseRandomAxis ? Random(3) : box.Size().DrivingAxis();
|
---|
965 |
|
---|
966 | for (axis = 0; axis < 3; ++ axis)
|
---|
967 | {
|
---|
968 | nFrontGeom[axis] = new BspNodeGeometry();
|
---|
969 | nBackGeom[axis] = new BspNodeGeometry();
|
---|
970 |
|
---|
971 | if (!mOnlyDrivingAxis || (axis == sAxis))
|
---|
972 | {
|
---|
973 | if (!useCostHeuristics)
|
---|
974 | {
|
---|
975 | nPosition[axis] = (box.Min()[axis] + box.Max()[axis]) * 0.5f;
|
---|
976 | Vector3 normal(0,0,0); normal[axis] = 1.0f;
|
---|
977 |
|
---|
978 | // allows faster split because we have axis aligned kd tree boxes
|
---|
979 | if (useKdSplit)
|
---|
980 | {
|
---|
981 | nCostRatio[axis] = EvalAxisAlignedSplitCost(tData,
|
---|
982 | box,
|
---|
983 | axis,
|
---|
984 | nPosition[axis],
|
---|
985 | nProbFront[axis],
|
---|
986 | nProbBack[axis]);
|
---|
987 |
|
---|
988 | Vector3 pos;
|
---|
989 |
|
---|
990 | pos = box.Max(); pos[axis] = nPosition[axis];
|
---|
991 | AxisAlignedBox3 bBox(box.Min(), pos);
|
---|
992 | bBox.ExtractPolys(nBackGeom[axis]->mPolys);
|
---|
993 |
|
---|
994 | pos = box.Min(); pos[axis] = nPosition[axis];
|
---|
995 | AxisAlignedBox3 fBox(pos, box.Max());
|
---|
996 | fBox.ExtractPolys(nFrontGeom[axis]->mPolys);
|
---|
997 | }
|
---|
998 | else
|
---|
999 | {
|
---|
1000 | nCostRatio[axis] =
|
---|
1001 | EvalSplitPlaneCost(Plane3(normal, nPosition[axis]),
|
---|
1002 | tData, *nFrontGeom[axis], *nBackGeom[axis],
|
---|
1003 | nProbFront[axis], nProbBack[axis]);
|
---|
1004 | }
|
---|
1005 | }
|
---|
1006 | else
|
---|
1007 | {
|
---|
1008 | nCostRatio[axis] =
|
---|
1009 | BestCostRatioHeuristics(*tData.mRays,
|
---|
1010 | box,
|
---|
1011 | tData.mPvs,
|
---|
1012 | axis,
|
---|
1013 | nPosition[axis]);
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | if (bestAxis == -1)
|
---|
1017 | {
|
---|
1018 | bestAxis = axis;
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | else if (nCostRatio[axis] < nCostRatio[bestAxis])
|
---|
1022 | {
|
---|
1023 | bestAxis = axis;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | }
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | //-- assign values
|
---|
1030 | axis = bestAxis;
|
---|
1031 | pFront = nProbFront[bestAxis];
|
---|
1032 | pBack = nProbBack[bestAxis];
|
---|
1033 |
|
---|
1034 | // assign best split nodes geometry
|
---|
1035 | *frontGeom = nFrontGeom[bestAxis];
|
---|
1036 | *backGeom = nBackGeom[bestAxis];
|
---|
1037 |
|
---|
1038 | // and delete other geometry
|
---|
1039 | delete nFrontGeom[(bestAxis + 1) % 3];
|
---|
1040 | delete nBackGeom[(bestAxis + 2) % 3];
|
---|
1041 |
|
---|
1042 | //-- split plane
|
---|
1043 | Vector3 normal(0,0,0); normal[bestAxis] = 1;
|
---|
1044 | plane = Plane3(normal, nPosition[bestAxis]);
|
---|
1045 |
|
---|
1046 | return nCostRatio[bestAxis];
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 |
|
---|
1050 | bool VspBspTree::SelectPlane(Plane3 &bestPlane,
|
---|
1051 | BspLeaf *leaf,
|
---|
1052 | VspBspTraversalData &data,
|
---|
1053 | VspBspTraversalData &frontData,
|
---|
1054 | VspBspTraversalData &backData)
|
---|
1055 | {
|
---|
1056 | // simplest strategy: just take next polygon
|
---|
1057 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
1058 | {
|
---|
1059 | if (!data.mPolygons->empty())
|
---|
1060 | {
|
---|
1061 | const int randIdx =
|
---|
1062 | (int)RandomValue(0, (Real)((int)data.mPolygons->size() - 1));
|
---|
1063 | Polygon3 *nextPoly = (*data.mPolygons)[randIdx];
|
---|
1064 |
|
---|
1065 | bestPlane = nextPoly->GetSupportingPlane();
|
---|
1066 | return true;
|
---|
1067 | }
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | //-- use heuristics to find appropriate plane
|
---|
1071 |
|
---|
1072 | // intermediate plane
|
---|
1073 | Plane3 plane;
|
---|
1074 | float lowestCost = MAX_FLOAT;
|
---|
1075 |
|
---|
1076 | // decides if the first few splits should be only axisAligned
|
---|
1077 | const bool onlyAxisAligned =
|
---|
1078 | (mSplitPlaneStrategy & AXIS_ALIGNED) &&
|
---|
1079 | ((int)data.mRays->size() > mTermMinRaysForAxisAligned) &&
|
---|
1080 | ((int)data.GetAvgRayContribution() < mTermMaxRayContriForAxisAligned);
|
---|
1081 |
|
---|
1082 | const int limit = onlyAxisAligned ? 0 :
|
---|
1083 | Min((int)data.mPolygons->size(), mMaxPolyCandidates);
|
---|
1084 |
|
---|
1085 | float candidateCost;
|
---|
1086 |
|
---|
1087 | int maxIdx = (int)data.mPolygons->size();
|
---|
1088 |
|
---|
1089 | for (int i = 0; i < limit; ++ i)
|
---|
1090 | {
|
---|
1091 | // the already taken candidates are stored behind maxIdx
|
---|
1092 | // => assure that no index is taken twice
|
---|
1093 | const int candidateIdx = (int)RandomValue(0, (Real)(-- maxIdx));
|
---|
1094 | Polygon3 *poly = (*data.mPolygons)[candidateIdx];
|
---|
1095 |
|
---|
1096 | // swap candidate to the end to avoid testing same plane
|
---|
1097 | std::swap((*data.mPolygons)[maxIdx], (*data.mPolygons)[candidateIdx]);
|
---|
1098 | //Polygon3 *poly = (*data.mPolygons)[(int)RandomValue(0, (int)polys.size() - 1)];
|
---|
1099 |
|
---|
1100 | // evaluate current candidate
|
---|
1101 | BspNodeGeometry fGeom, bGeom;
|
---|
1102 | float fArea, bArea;
|
---|
1103 | plane = poly->GetSupportingPlane();
|
---|
1104 | candidateCost = EvalSplitPlaneCost(plane, data, fGeom, bGeom, fArea, bArea);
|
---|
1105 |
|
---|
1106 | if (candidateCost < lowestCost)
|
---|
1107 | {
|
---|
1108 | bestPlane = plane;
|
---|
1109 | lowestCost = candidateCost;
|
---|
1110 | }
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 | //-- evaluate axis aligned splits
|
---|
1114 | int axis;
|
---|
1115 | BspNodeGeometry *fGeom, *bGeom;
|
---|
1116 | float pFront, pBack;
|
---|
1117 |
|
---|
1118 | candidateCost = SelectAxisAlignedPlane(plane, data, axis,
|
---|
1119 | &fGeom, &bGeom,
|
---|
1120 | pFront, pBack,
|
---|
1121 | data.mIsKdNode);
|
---|
1122 |
|
---|
1123 | bool isAxisAlignedSplit = false;
|
---|
1124 |
|
---|
1125 | if (candidateCost < lowestCost)
|
---|
1126 | {
|
---|
1127 | bestPlane = plane;
|
---|
1128 | lowestCost = candidateCost;
|
---|
1129 |
|
---|
1130 | // assign already computed values
|
---|
1131 | // we can do this because we always save the
|
---|
1132 | // computed values from the axis aligned splits
|
---|
1133 | frontData.mGeometry = fGeom;
|
---|
1134 | backData.mGeometry = bGeom;
|
---|
1135 |
|
---|
1136 | frontData.mProbability = pFront;
|
---|
1137 | backData.mProbability = pBack;
|
---|
1138 |
|
---|
1139 | //! error also computed if cost ratio is missed
|
---|
1140 | ++ mBspStats.splits[axis];
|
---|
1141 | isAxisAlignedSplit = true;
|
---|
1142 | }
|
---|
1143 | else
|
---|
1144 | {
|
---|
1145 | DEL_PTR(fGeom);
|
---|
1146 | DEL_PTR(bGeom);
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 | frontData.mIsKdNode = backData.mIsKdNode = (data.mIsKdNode && isAxisAlignedSplit);
|
---|
1150 |
|
---|
1151 | #ifdef _DEBUG
|
---|
1152 | Debug << "plane lowest cost: " << lowestCost << endl;
|
---|
1153 | #endif
|
---|
1154 |
|
---|
1155 | // cost ratio miss
|
---|
1156 | if (lowestCost > mTermMaxCostRatio)
|
---|
1157 | return false;
|
---|
1158 |
|
---|
1159 | return true;
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 |
|
---|
1163 | Plane3 VspBspTree::ChooseCandidatePlane(const RayInfoContainer &rays) const
|
---|
1164 | {
|
---|
1165 | const int candidateIdx = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
1166 |
|
---|
1167 | const Vector3 minPt = rays[candidateIdx].ExtrapOrigin();
|
---|
1168 | const Vector3 maxPt = rays[candidateIdx].ExtrapTermination();
|
---|
1169 |
|
---|
1170 | const Vector3 pt = (maxPt + minPt) * 0.5;
|
---|
1171 | const Vector3 normal = Normalize(rays[candidateIdx].mRay->GetDir());
|
---|
1172 |
|
---|
1173 | return Plane3(normal, pt);
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 |
|
---|
1177 | Plane3 VspBspTree::ChooseCandidatePlane2(const RayInfoContainer &rays) const
|
---|
1178 | {
|
---|
1179 | Vector3 pt[3];
|
---|
1180 |
|
---|
1181 | int idx[3];
|
---|
1182 | int cmaxT = 0;
|
---|
1183 | int cminT = 0;
|
---|
1184 | bool chooseMin = false;
|
---|
1185 |
|
---|
1186 | for (int j = 0; j < 3; ++ j)
|
---|
1187 | {
|
---|
1188 | idx[j] = (int)RandomValue(0, (Real)((int)rays.size() * 2 - 1));
|
---|
1189 |
|
---|
1190 | if (idx[j] >= (int)rays.size())
|
---|
1191 | {
|
---|
1192 | idx[j] -= (int)rays.size();
|
---|
1193 |
|
---|
1194 | chooseMin = (cminT < 2);
|
---|
1195 | }
|
---|
1196 | else
|
---|
1197 | chooseMin = (cmaxT < 2);
|
---|
1198 |
|
---|
1199 | RayInfo rayInf = rays[idx[j]];
|
---|
1200 | pt[j] = chooseMin ? rayInf.ExtrapOrigin() : rayInf.ExtrapTermination();
|
---|
1201 | }
|
---|
1202 |
|
---|
1203 | return Plane3(pt[0], pt[1], pt[2]);
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 |
|
---|
1207 | Plane3 VspBspTree::ChooseCandidatePlane3(const RayInfoContainer &rays) const
|
---|
1208 | {
|
---|
1209 | Vector3 pt[3];
|
---|
1210 |
|
---|
1211 | int idx1 = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
1212 | int idx2 = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
1213 |
|
---|
1214 | // check if rays different
|
---|
1215 | if (idx1 == idx2)
|
---|
1216 | idx2 = (idx2 + 1) % (int)rays.size();
|
---|
1217 |
|
---|
1218 | const RayInfo ray1 = rays[idx1];
|
---|
1219 | const RayInfo ray2 = rays[idx2];
|
---|
1220 |
|
---|
1221 | // normal vector of the plane parallel to both lines
|
---|
1222 | const Vector3 norm = Normalize(CrossProd(ray1.mRay->GetDir(), ray2.mRay->GetDir()));
|
---|
1223 |
|
---|
1224 | // vector from line 1 to line 2
|
---|
1225 | const Vector3 vd = ray2.ExtrapOrigin() - ray1.ExtrapOrigin();
|
---|
1226 |
|
---|
1227 | // project vector on normal to get distance
|
---|
1228 | const float dist = DotProd(vd, norm);
|
---|
1229 |
|
---|
1230 | // point on plane lies halfway between the two planes
|
---|
1231 | const Vector3 planePt = ray1.ExtrapOrigin() + norm * dist * 0.5;
|
---|
1232 |
|
---|
1233 | return Plane3(norm, planePt);
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 |
|
---|
1237 | inline void VspBspTree::GenerateUniqueIdsForPvs()
|
---|
1238 | {
|
---|
1239 | Intersectable::NewMail(); sBackId = Intersectable::sMailId;
|
---|
1240 | Intersectable::NewMail(); sFrontId = Intersectable::sMailId;
|
---|
1241 | Intersectable::NewMail(); sFrontAndBackId = Intersectable::sMailId;
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 |
|
---|
1245 | float VspBspTree::EvalSplitPlaneCost(const Plane3 &candidatePlane,
|
---|
1246 | const VspBspTraversalData &data,
|
---|
1247 | BspNodeGeometry &geomFront,
|
---|
1248 | BspNodeGeometry &geomBack,
|
---|
1249 | float &pFront,
|
---|
1250 | float &pBack) const
|
---|
1251 | {
|
---|
1252 | float cost = 0;
|
---|
1253 |
|
---|
1254 | float sumBalancedRays = 0;
|
---|
1255 | float sumRaySplits = 0;
|
---|
1256 |
|
---|
1257 | int pvsFront = 0;
|
---|
1258 | int pvsBack = 0;
|
---|
1259 |
|
---|
1260 | // probability that view point lies in back / front node
|
---|
1261 | float pOverall = 0;
|
---|
1262 | pFront = 0;
|
---|
1263 | pBack = 0;
|
---|
1264 |
|
---|
1265 | int raysFront = 0;
|
---|
1266 | int raysBack = 0;
|
---|
1267 | int totalPvs = 0;
|
---|
1268 |
|
---|
1269 | int limit;
|
---|
1270 | bool useRand;
|
---|
1271 |
|
---|
1272 | // choose test rays randomly if too much
|
---|
1273 | if ((int)data.mRays->size() > mMaxTests)
|
---|
1274 | {
|
---|
1275 | useRand = true;
|
---|
1276 | limit = mMaxTests;
|
---|
1277 | }
|
---|
1278 | else
|
---|
1279 | {
|
---|
1280 | useRand = false;
|
---|
1281 | limit = (int)data.mRays->size();
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | for (int i = 0; i < limit; ++ i)
|
---|
1285 | {
|
---|
1286 | const int testIdx = useRand ?
|
---|
1287 | (int)RandomValue(0, (Real)((int)data.mRays->size() - 1)) : i;
|
---|
1288 | RayInfo rayInf = (*data.mRays)[testIdx];
|
---|
1289 |
|
---|
1290 | float t;
|
---|
1291 | VssRay *ray = rayInf.mRay;
|
---|
1292 | const int cf = rayInf.ComputeRayIntersection(candidatePlane, t);
|
---|
1293 |
|
---|
1294 | if (0)
|
---|
1295 | {
|
---|
1296 | if (cf >= 0)
|
---|
1297 | ++ raysFront;
|
---|
1298 | if (cf <= 0)
|
---|
1299 | ++ raysBack;
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
1303 | {
|
---|
1304 | sumBalancedRays += cf;
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
1308 | {
|
---|
1309 | if (cf == 0)
|
---|
1310 | ++ sumRaySplits;
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 | if (mSplitPlaneStrategy & PVS)
|
---|
1314 | {
|
---|
1315 | // find front and back pvs for origing and termination object
|
---|
1316 | AddObjToPvs(ray->mTerminationObject, cf, pvsFront, pvsBack, totalPvs);
|
---|
1317 | AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs);
|
---|
1318 | }
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | const float raysSize = (float)data.mRays->size() + Limits::Small;
|
---|
1322 |
|
---|
1323 | if (mSplitPlaneStrategy & PVS)
|
---|
1324 | {
|
---|
1325 | // create unique ids for pvs heuristics
|
---|
1326 | GenerateUniqueIdsForPvs();
|
---|
1327 |
|
---|
1328 | // construct child geometry with regard to the candidate split plane
|
---|
1329 | data.mGeometry->SplitGeometry(geomFront,
|
---|
1330 | geomBack,
|
---|
1331 | candidatePlane,
|
---|
1332 | mBox,
|
---|
1333 | mEpsilon);
|
---|
1334 |
|
---|
1335 | pOverall = data.mProbability;
|
---|
1336 |
|
---|
1337 | if (!mUseAreaForPvs) // use front and back cell areas to approximate volume
|
---|
1338 | {
|
---|
1339 | pFront = geomFront.GetVolume();
|
---|
1340 | pBack = pOverall - pFront;
|
---|
1341 | }
|
---|
1342 | else
|
---|
1343 | {
|
---|
1344 | pFront = geomFront.GetArea();
|
---|
1345 | pBack = geomBack.GetArea();
|
---|
1346 | }
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 |
|
---|
1350 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
1351 | cost += mLeastRaySplitsFactor * sumRaySplits / raysSize;
|
---|
1352 |
|
---|
1353 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
1354 | cost += mBalancedRaysFactor * fabs(sumBalancedRays) / raysSize;
|
---|
1355 |
|
---|
1356 | // -- pvs rendering heuristics
|
---|
1357 | if (mSplitPlaneStrategy & PVS)
|
---|
1358 | {
|
---|
1359 | const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize();
|
---|
1360 | const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize();
|
---|
1361 |
|
---|
1362 | // only render cost heuristics or combined with standard deviation
|
---|
1363 | if (1)
|
---|
1364 | {
|
---|
1365 | const float penaltyOld = EvalPvsPenalty(totalPvs, lowerPvsLimit, upperPvsLimit);
|
---|
1366 | const float penaltyFront = EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit);
|
---|
1367 | const float penaltyBack = EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit);
|
---|
1368 |
|
---|
1369 | const float oldRenderCost = pOverall * (float)penaltyOld + Limits::Small;
|
---|
1370 | const float newRenderCost = penaltyFront * pFront + penaltyBack * pBack;
|
---|
1371 |
|
---|
1372 | float oldCost, newCost;
|
---|
1373 |
|
---|
1374 | // only render cost
|
---|
1375 | if (1)
|
---|
1376 | {
|
---|
1377 | oldCost = oldRenderCost;
|
---|
1378 | newCost = newRenderCost;
|
---|
1379 | }
|
---|
1380 | else // also considering standard deviation
|
---|
1381 | {
|
---|
1382 | // standard deviation is difference of back and front pvs
|
---|
1383 | const float expectedCost = 0.5f * (penaltyFront + penaltyBack);
|
---|
1384 |
|
---|
1385 | const float newDeviation = 0.5f *
|
---|
1386 | fabs(penaltyFront - expectedCost) + fabs(penaltyBack - expectedCost);
|
---|
1387 |
|
---|
1388 | const float oldDeviation = penaltyOld + Limits::Small;
|
---|
1389 |
|
---|
1390 | newCost = mRenderCostWeight * newRenderCost + (1.0f - mRenderCostWeight) * newDeviation;
|
---|
1391 | oldCost = mRenderCostWeight * oldRenderCost + (1.0f - mRenderCostWeight) * oldDeviation;
|
---|
1392 | }
|
---|
1393 |
|
---|
1394 | cost += mPvsFactor * newCost / oldCost;
|
---|
1395 | }
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | #ifdef _DEBUG
|
---|
1399 | Debug << "totalpvs: " << data.mPvs << " ptotal: " << pOverall
|
---|
1400 | << " frontpvs: " << pvsFront << " pFront: " << pFront
|
---|
1401 | << " backpvs: " << pvsBack << " pBack: " << pBack << endl << endl;
|
---|
1402 | Debug << "cost: " << cost << endl;
|
---|
1403 | #endif
|
---|
1404 |
|
---|
1405 |
|
---|
1406 | // normalize cost by sum of linear factors
|
---|
1407 | if(0)
|
---|
1408 | return cost / (float)mCostNormalizer;
|
---|
1409 | else
|
---|
1410 | return cost;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 |
|
---|
1414 | float VspBspTree::EvalAxisAlignedSplitCost(const VspBspTraversalData &data,
|
---|
1415 | const AxisAlignedBox3 &box,
|
---|
1416 | const int axis,
|
---|
1417 | const float &position,
|
---|
1418 | float &pFront,
|
---|
1419 | float &pBack) const
|
---|
1420 | {
|
---|
1421 | int pvsTotal = 0;
|
---|
1422 | int pvsFront = 0;
|
---|
1423 | int pvsBack = 0;
|
---|
1424 |
|
---|
1425 | // create unique ids for pvs heuristics
|
---|
1426 | GenerateUniqueIdsForPvs();
|
---|
1427 |
|
---|
1428 | const int pvsSize = data.mPvs;
|
---|
1429 |
|
---|
1430 | RayInfoContainer::const_iterator rit, rit_end = data.mRays->end();
|
---|
1431 |
|
---|
1432 | // this is the main ray classification loop!
|
---|
1433 | for(rit = data.mRays->begin(); rit != rit_end; ++ rit)
|
---|
1434 | {
|
---|
1435 | //if (!(*rit).mRay->IsActive()) continue;
|
---|
1436 |
|
---|
1437 | // determine the side of this ray with respect to the plane
|
---|
1438 | float t;
|
---|
1439 | const int side = (*rit).ComputeRayIntersection(axis, position, t);
|
---|
1440 |
|
---|
1441 | AddObjToPvs((*rit).mRay->mTerminationObject, side, pvsFront, pvsBack, pvsTotal);
|
---|
1442 | AddObjToPvs((*rit).mRay->mOriginObject, side, pvsFront, pvsBack, pvsTotal);
|
---|
1443 | }
|
---|
1444 |
|
---|
1445 | //-- pvs heuristics
|
---|
1446 | float pOverall;
|
---|
1447 |
|
---|
1448 | //-- compute heurstics
|
---|
1449 | // we take simplified computation for mid split
|
---|
1450 |
|
---|
1451 | pOverall = data.mProbability;
|
---|
1452 |
|
---|
1453 | if (!mUseAreaForPvs)
|
---|
1454 | { // volume
|
---|
1455 | pBack = pFront = pOverall * 0.5f;
|
---|
1456 |
|
---|
1457 | #if 0
|
---|
1458 | // box length substitute for probability
|
---|
1459 | const float minBox = box.Min(axis);
|
---|
1460 | const float maxBox = box.Max(axis);
|
---|
1461 |
|
---|
1462 | pBack = position - minBox;
|
---|
1463 | pFront = maxBox - position;
|
---|
1464 | pOverall = maxBox - minBox;
|
---|
1465 | #endif
|
---|
1466 | }
|
---|
1467 | else //-- area substitute for probability
|
---|
1468 | {
|
---|
1469 | const int axis2 = (axis + 1) % 3;
|
---|
1470 | const int axis3 = (axis + 2) % 3;
|
---|
1471 |
|
---|
1472 | const float faceArea =
|
---|
1473 | (box.Max(axis2) - box.Min(axis2)) *
|
---|
1474 | (box.Max(axis3) - box.Min(axis3));
|
---|
1475 |
|
---|
1476 | pBack = pFront = pOverall * 0.5f + faceArea;
|
---|
1477 | }
|
---|
1478 |
|
---|
1479 | #ifdef _DEBUG
|
---|
1480 | Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl;
|
---|
1481 | Debug << pFront << " " << pBack << " " << pOverall << endl;
|
---|
1482 | #endif
|
---|
1483 |
|
---|
1484 |
|
---|
1485 | const float newCost = pvsBack * pBack + pvsFront * pFront;
|
---|
1486 | const float oldCost = (float)pvsSize * pOverall + Limits::Small;
|
---|
1487 |
|
---|
1488 | return (mCtDivCi + newCost) / oldCost;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 |
|
---|
1492 | void VspBspTree::AddObjToPvs(Intersectable *obj,
|
---|
1493 | const int cf,
|
---|
1494 | int &frontPvs,
|
---|
1495 | int &backPvs,
|
---|
1496 | int &totalPvs) const
|
---|
1497 | {
|
---|
1498 | if (!obj)
|
---|
1499 | return;
|
---|
1500 |
|
---|
1501 | if ((obj->mMailbox != sFrontId) &&
|
---|
1502 | (obj->mMailbox != sBackId) &&
|
---|
1503 | (obj->mMailbox != sFrontAndBackId))
|
---|
1504 | {
|
---|
1505 | ++ totalPvs;
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | // TODO: does this really belong to no pvs?
|
---|
1509 | //if (cf == Ray::COINCIDENT) return;
|
---|
1510 |
|
---|
1511 | // object belongs to both PVS
|
---|
1512 | if (cf >= 0)
|
---|
1513 | {
|
---|
1514 | if ((obj->mMailbox != sFrontId) &&
|
---|
1515 | (obj->mMailbox != sFrontAndBackId))
|
---|
1516 | {
|
---|
1517 | ++ frontPvs;
|
---|
1518 |
|
---|
1519 | if (obj->mMailbox == sBackId)
|
---|
1520 | obj->mMailbox = sFrontAndBackId;
|
---|
1521 | else
|
---|
1522 | obj->mMailbox = sFrontId;
|
---|
1523 | }
|
---|
1524 | }
|
---|
1525 |
|
---|
1526 | if (cf <= 0)
|
---|
1527 | {
|
---|
1528 | if ((obj->mMailbox != sBackId) &&
|
---|
1529 | (obj->mMailbox != sFrontAndBackId))
|
---|
1530 | {
|
---|
1531 | ++ backPvs;
|
---|
1532 |
|
---|
1533 | if (obj->mMailbox == sFrontId)
|
---|
1534 | obj->mMailbox = sFrontAndBackId;
|
---|
1535 | else
|
---|
1536 | obj->mMailbox = sBackId;
|
---|
1537 | }
|
---|
1538 | }
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 |
|
---|
1542 | void VspBspTree::CollectLeaves(vector<BspLeaf *> &leaves,
|
---|
1543 | const bool onlyUnmailed,
|
---|
1544 | const int maxPvsSize) const
|
---|
1545 | {
|
---|
1546 | stack<BspNode *> nodeStack;
|
---|
1547 | nodeStack.push(mRoot);
|
---|
1548 |
|
---|
1549 | while (!nodeStack.empty())
|
---|
1550 | {
|
---|
1551 | BspNode *node = nodeStack.top();
|
---|
1552 | nodeStack.pop();
|
---|
1553 |
|
---|
1554 | if (node->IsLeaf())
|
---|
1555 | {
|
---|
1556 | // test if this leaf is in valid view space
|
---|
1557 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
1558 | if (leaf->TreeValid() &&
|
---|
1559 | (!onlyUnmailed || !leaf->Mailed()) &&
|
---|
1560 | ((maxPvsSize < 0) || (leaf->GetViewCell()->GetPvs().GetSize() <= maxPvsSize)))
|
---|
1561 | {
|
---|
1562 | leaves.push_back(leaf);
|
---|
1563 | }
|
---|
1564 | }
|
---|
1565 | else
|
---|
1566 | {
|
---|
1567 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1568 |
|
---|
1569 | nodeStack.push(interior->GetBack());
|
---|
1570 | nodeStack.push(interior->GetFront());
|
---|
1571 | }
|
---|
1572 | }
|
---|
1573 | }
|
---|
1574 |
|
---|
1575 |
|
---|
1576 | AxisAlignedBox3 VspBspTree::GetBoundingBox() const
|
---|
1577 | {
|
---|
1578 | return mBox;
|
---|
1579 | }
|
---|
1580 |
|
---|
1581 |
|
---|
1582 | BspNode *VspBspTree::GetRoot() const
|
---|
1583 | {
|
---|
1584 | return mRoot;
|
---|
1585 | }
|
---|
1586 |
|
---|
1587 |
|
---|
1588 | void VspBspTree::EvaluateLeafStats(const VspBspTraversalData &data)
|
---|
1589 | {
|
---|
1590 | // the node became a leaf -> evaluate stats for leafs
|
---|
1591 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode);
|
---|
1592 |
|
---|
1593 | // store maximal and minimal depth
|
---|
1594 | if (data.mDepth > mBspStats.maxDepth)
|
---|
1595 | mBspStats.maxDepth = data.mDepth;
|
---|
1596 |
|
---|
1597 | if (data.mPvs > mBspStats.maxPvs)
|
---|
1598 | mBspStats.maxPvs = data.mPvs;
|
---|
1599 |
|
---|
1600 | if (data.mDepth < mBspStats.minDepth)
|
---|
1601 | mBspStats.minDepth = data.mDepth;
|
---|
1602 |
|
---|
1603 | if (data.mDepth >= mTermMaxDepth)
|
---|
1604 | ++ mBspStats.maxDepthNodes;
|
---|
1605 | // accumulate rays to compute rays / leaf
|
---|
1606 | mBspStats.accumRays += (int)data.mRays->size();
|
---|
1607 |
|
---|
1608 | if (data.mPvs < mTermMinPvs)
|
---|
1609 | ++ mBspStats.minPvsNodes;
|
---|
1610 |
|
---|
1611 | if ((int)data.mRays->size() < mTermMinRays)
|
---|
1612 | ++ mBspStats.minRaysNodes;
|
---|
1613 |
|
---|
1614 | if (data.GetAvgRayContribution() > mTermMaxRayContribution)
|
---|
1615 | ++ mBspStats.maxRayContribNodes;
|
---|
1616 |
|
---|
1617 | if (data.mProbability <= mTermMinProbability)
|
---|
1618 | ++ mBspStats.minProbabilityNodes;
|
---|
1619 |
|
---|
1620 | // accumulate depth to compute average depth
|
---|
1621 | mBspStats.accumDepth += data.mDepth;
|
---|
1622 |
|
---|
1623 | #ifdef _DEBUG
|
---|
1624 | Debug << "BSP stats: "
|
---|
1625 | << "Depth: " << data.mDepth << " (max: " << mTermMaxDepth << "), "
|
---|
1626 | << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), "
|
---|
1627 | // << "Area: " << data.mProbability << " (min: " << mTermMinProbability << "), "
|
---|
1628 | << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "
|
---|
1629 | << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "=, "
|
---|
1630 | << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl;
|
---|
1631 | #endif
|
---|
1632 | }
|
---|
1633 |
|
---|
1634 | int VspBspTree::CastRay(Ray &ray)
|
---|
1635 | {
|
---|
1636 | int hits = 0;
|
---|
1637 |
|
---|
1638 | stack<BspRayTraversalData> tQueue;
|
---|
1639 |
|
---|
1640 | float maxt, mint;
|
---|
1641 |
|
---|
1642 | if (!mBox.GetRaySegment(ray, mint, maxt))
|
---|
1643 | return 0;
|
---|
1644 |
|
---|
1645 | Intersectable::NewMail();
|
---|
1646 | ViewCell::NewMail();
|
---|
1647 | Vector3 entp = ray.Extrap(mint);
|
---|
1648 | Vector3 extp = ray.Extrap(maxt);
|
---|
1649 |
|
---|
1650 | BspNode *node = mRoot;
|
---|
1651 | BspNode *farChild = NULL;
|
---|
1652 |
|
---|
1653 | while (1)
|
---|
1654 | {
|
---|
1655 | if (!node->IsLeaf())
|
---|
1656 | {
|
---|
1657 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
1658 |
|
---|
1659 | Plane3 splitPlane = in->GetPlane();
|
---|
1660 | const int entSide = splitPlane.Side(entp);
|
---|
1661 | const int extSide = splitPlane.Side(extp);
|
---|
1662 |
|
---|
1663 | if (entSide < 0)
|
---|
1664 | {
|
---|
1665 | node = in->GetBack();
|
---|
1666 |
|
---|
1667 | if(extSide <= 0) // plane does not split ray => no far child
|
---|
1668 | continue;
|
---|
1669 |
|
---|
1670 | farChild = in->GetFront(); // plane splits ray
|
---|
1671 |
|
---|
1672 | } else if (entSide > 0)
|
---|
1673 | {
|
---|
1674 | node = in->GetFront();
|
---|
1675 |
|
---|
1676 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
1677 | continue;
|
---|
1678 |
|
---|
1679 | farChild = in->GetBack(); // plane splits ray
|
---|
1680 | }
|
---|
1681 | else // ray and plane are coincident
|
---|
1682 | {
|
---|
1683 | // WHAT TO DO IN THIS CASE ?
|
---|
1684 | //break;
|
---|
1685 | node = in->GetFront();
|
---|
1686 | continue;
|
---|
1687 | }
|
---|
1688 |
|
---|
1689 | // push data for far child
|
---|
1690 | tQueue.push(BspRayTraversalData(farChild, extp, maxt));
|
---|
1691 |
|
---|
1692 | // find intersection of ray segment with plane
|
---|
1693 | float t;
|
---|
1694 | extp = splitPlane.FindIntersection(ray.GetLoc(), extp, &t);
|
---|
1695 | maxt *= t;
|
---|
1696 |
|
---|
1697 | } else // reached leaf => intersection with view cell
|
---|
1698 | {
|
---|
1699 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
1700 |
|
---|
1701 | if (!leaf->GetViewCell()->Mailed())
|
---|
1702 | {
|
---|
1703 | //ray.bspIntersections.push_back(Ray::VspBspIntersection(maxt, leaf));
|
---|
1704 | leaf->GetViewCell()->Mail();
|
---|
1705 | ++ hits;
|
---|
1706 | }
|
---|
1707 |
|
---|
1708 | //-- fetch the next far child from the stack
|
---|
1709 | if (tQueue.empty())
|
---|
1710 | break;
|
---|
1711 |
|
---|
1712 | entp = extp;
|
---|
1713 | mint = maxt; // NOTE: need this?
|
---|
1714 |
|
---|
1715 | if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
|
---|
1716 | break;
|
---|
1717 |
|
---|
1718 | BspRayTraversalData &s = tQueue.top();
|
---|
1719 |
|
---|
1720 | node = s.mNode;
|
---|
1721 | extp = s.mExitPoint;
|
---|
1722 | maxt = s.mMaxT;
|
---|
1723 |
|
---|
1724 | tQueue.pop();
|
---|
1725 | }
|
---|
1726 | }
|
---|
1727 |
|
---|
1728 | return hits;
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 |
|
---|
1732 | void VspBspTree::CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const
|
---|
1733 | {
|
---|
1734 | ViewCell::NewMail();
|
---|
1735 |
|
---|
1736 | CollectViewCells(mRoot, onlyValid, viewCells, true);
|
---|
1737 | }
|
---|
1738 |
|
---|
1739 |
|
---|
1740 | void VspBspTree::CollapseViewCells()
|
---|
1741 | {
|
---|
1742 | // TODO
|
---|
1743 | #if VC_HISTORY
|
---|
1744 | stack<BspNode *> nodeStack;
|
---|
1745 |
|
---|
1746 | if (!mRoot)
|
---|
1747 | return;
|
---|
1748 |
|
---|
1749 | nodeStack.push(mRoot);
|
---|
1750 |
|
---|
1751 | while (!nodeStack.empty())
|
---|
1752 | {
|
---|
1753 | BspNode *node = nodeStack.top();
|
---|
1754 | nodeStack.pop();
|
---|
1755 |
|
---|
1756 | if (node->IsLeaf())
|
---|
1757 | {
|
---|
1758 | BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
1759 |
|
---|
1760 | if (!viewCell->GetValid())
|
---|
1761 | {
|
---|
1762 | BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
1763 |
|
---|
1764 | ViewCellContainer leaves;
|
---|
1765 | mViewCellsTree->CollectLeaves(viewCell, leaves);
|
---|
1766 |
|
---|
1767 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
1768 |
|
---|
1769 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
1770 | {
|
---|
1771 | BspLeaf *l = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
1772 | l->SetViewCell(GetOrCreateOutOfBoundsCell());
|
---|
1773 | ++ mBspStats.invalidLeaves;
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | // add to unbounded view cell
|
---|
1777 | GetOrCreateOutOfBoundsCell()->GetPvs().AddPvs(viewCell->GetPvs());
|
---|
1778 | DEL_PTR(viewCell);
|
---|
1779 | }
|
---|
1780 | }
|
---|
1781 | else
|
---|
1782 | {
|
---|
1783 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1784 |
|
---|
1785 | nodeStack.push(interior->GetFront());
|
---|
1786 | nodeStack.push(interior->GetBack());
|
---|
1787 | }
|
---|
1788 | }
|
---|
1789 |
|
---|
1790 | Debug << "invalid leaves: " << mBspStats.invalidLeaves << endl;
|
---|
1791 | #endif
|
---|
1792 | }
|
---|
1793 |
|
---|
1794 |
|
---|
1795 | void VspBspTree::ValidateTree()
|
---|
1796 | {
|
---|
1797 | stack<BspNode *> nodeStack;
|
---|
1798 |
|
---|
1799 | if (!mRoot)
|
---|
1800 | return;
|
---|
1801 |
|
---|
1802 | nodeStack.push(mRoot);
|
---|
1803 |
|
---|
1804 | mBspStats.invalidLeaves = 0;
|
---|
1805 | while (!nodeStack.empty())
|
---|
1806 | {
|
---|
1807 | BspNode *node = nodeStack.top();
|
---|
1808 | nodeStack.pop();
|
---|
1809 |
|
---|
1810 | if (node->IsLeaf())
|
---|
1811 | {
|
---|
1812 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
1813 |
|
---|
1814 | if (!leaf->GetViewCell()->GetValid())
|
---|
1815 | ++ mBspStats.invalidLeaves;
|
---|
1816 |
|
---|
1817 | // validity flags don't match => repair
|
---|
1818 | if (leaf->GetViewCell()->GetValid() != leaf->TreeValid())
|
---|
1819 | {
|
---|
1820 | leaf->SetTreeValid(leaf->GetViewCell()->GetValid());
|
---|
1821 | PropagateUpValidity(leaf);
|
---|
1822 | }
|
---|
1823 | }
|
---|
1824 | else
|
---|
1825 | {
|
---|
1826 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1827 |
|
---|
1828 | nodeStack.push(interior->GetFront());
|
---|
1829 | nodeStack.push(interior->GetBack());
|
---|
1830 | }
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | Debug << "invalid leaves: " << mBspStats.invalidLeaves << endl;
|
---|
1834 | }
|
---|
1835 |
|
---|
1836 |
|
---|
1837 |
|
---|
1838 | void VspBspTree::CollectViewCells(BspNode *root,
|
---|
1839 | bool onlyValid,
|
---|
1840 | ViewCellContainer &viewCells,
|
---|
1841 | bool onlyUnmailed) const
|
---|
1842 | {
|
---|
1843 | stack<BspNode *> nodeStack;
|
---|
1844 |
|
---|
1845 | if (!root)
|
---|
1846 | return;
|
---|
1847 |
|
---|
1848 | nodeStack.push(root);
|
---|
1849 |
|
---|
1850 | while (!nodeStack.empty())
|
---|
1851 | {
|
---|
1852 | BspNode *node = nodeStack.top();
|
---|
1853 | nodeStack.pop();
|
---|
1854 |
|
---|
1855 | if (node->IsLeaf())
|
---|
1856 | {
|
---|
1857 | if (!onlyValid || node->TreeValid())
|
---|
1858 | {
|
---|
1859 | ViewCell *viewCell =
|
---|
1860 | mViewCellsTree->GetActiveViewCell(dynamic_cast<BspLeaf *>(node)->GetViewCell());
|
---|
1861 |
|
---|
1862 | if (!onlyUnmailed || !viewCell->Mailed())
|
---|
1863 | {
|
---|
1864 | viewCell->Mail();
|
---|
1865 | viewCells.push_back(viewCell);
|
---|
1866 | }
|
---|
1867 | }
|
---|
1868 | }
|
---|
1869 | else
|
---|
1870 | {
|
---|
1871 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1872 |
|
---|
1873 | nodeStack.push(interior->GetFront());
|
---|
1874 | nodeStack.push(interior->GetBack());
|
---|
1875 | }
|
---|
1876 | }
|
---|
1877 |
|
---|
1878 | }
|
---|
1879 |
|
---|
1880 |
|
---|
1881 | float VspBspTree::AccumulatedRayLength(const RayInfoContainer &rays) const
|
---|
1882 | {
|
---|
1883 | float len = 0;
|
---|
1884 |
|
---|
1885 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
1886 |
|
---|
1887 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
1888 | len += (*it).SegmentLength();
|
---|
1889 |
|
---|
1890 | return len;
|
---|
1891 | }
|
---|
1892 |
|
---|
1893 |
|
---|
1894 | int VspBspTree::SplitRays(const Plane3 &plane,
|
---|
1895 | RayInfoContainer &rays,
|
---|
1896 | RayInfoContainer &frontRays,
|
---|
1897 | RayInfoContainer &backRays)
|
---|
1898 | {
|
---|
1899 | int splits = 0;
|
---|
1900 |
|
---|
1901 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
1902 |
|
---|
1903 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
1904 | {
|
---|
1905 | RayInfo bRay = *it;
|
---|
1906 |
|
---|
1907 | VssRay *ray = bRay.mRay;
|
---|
1908 | float t;
|
---|
1909 |
|
---|
1910 | // get classification and receive new t
|
---|
1911 | const int cf = bRay.ComputeRayIntersection(plane, t);
|
---|
1912 |
|
---|
1913 | switch (cf)
|
---|
1914 | {
|
---|
1915 | case -1:
|
---|
1916 | backRays.push_back(bRay);
|
---|
1917 | break;
|
---|
1918 | case 1:
|
---|
1919 | frontRays.push_back(bRay);
|
---|
1920 | break;
|
---|
1921 | case 0:
|
---|
1922 | {
|
---|
1923 | //-- split ray
|
---|
1924 | //-- test if start point behind or in front of plane
|
---|
1925 | const int side = plane.Side(bRay.ExtrapOrigin());
|
---|
1926 |
|
---|
1927 | if (side <= 0)
|
---|
1928 | {
|
---|
1929 | backRays.push_back(RayInfo(ray, bRay.GetMinT(), t));
|
---|
1930 | frontRays.push_back(RayInfo(ray, t, bRay.GetMaxT()));
|
---|
1931 | }
|
---|
1932 | else
|
---|
1933 | {
|
---|
1934 | frontRays.push_back(RayInfo(ray, bRay.GetMinT(), t));
|
---|
1935 | backRays.push_back(RayInfo(ray, t, bRay.GetMaxT()));
|
---|
1936 | }
|
---|
1937 | }
|
---|
1938 | break;
|
---|
1939 | default:
|
---|
1940 | Debug << "Should not come here" << endl;
|
---|
1941 | break;
|
---|
1942 | }
|
---|
1943 | }
|
---|
1944 |
|
---|
1945 | return splits;
|
---|
1946 | }
|
---|
1947 |
|
---|
1948 |
|
---|
1949 | void VspBspTree::ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const
|
---|
1950 | {
|
---|
1951 | BspNode *lastNode;
|
---|
1952 |
|
---|
1953 | do
|
---|
1954 | {
|
---|
1955 | lastNode = n;
|
---|
1956 |
|
---|
1957 | // want to get planes defining geometry of this node => don't take
|
---|
1958 | // split plane of node itself
|
---|
1959 | n = n->GetParent();
|
---|
1960 |
|
---|
1961 | if (n)
|
---|
1962 | {
|
---|
1963 | BspInterior *interior = dynamic_cast<BspInterior *>(n);
|
---|
1964 | Plane3 halfSpace = dynamic_cast<BspInterior *>(interior)->GetPlane();
|
---|
1965 |
|
---|
1966 | if (interior->GetFront() != lastNode)
|
---|
1967 | halfSpace.ReverseOrientation();
|
---|
1968 |
|
---|
1969 | halfSpaces.push_back(halfSpace);
|
---|
1970 | }
|
---|
1971 | }
|
---|
1972 | while (n);
|
---|
1973 | }
|
---|
1974 |
|
---|
1975 |
|
---|
1976 | void VspBspTree::ConstructGeometry(BspNode *n,
|
---|
1977 | BspNodeGeometry &geom) const
|
---|
1978 | {
|
---|
1979 | vector<Plane3> halfSpaces;
|
---|
1980 | ExtractHalfSpaces(n, halfSpaces);
|
---|
1981 |
|
---|
1982 | PolygonContainer candidatePolys;
|
---|
1983 |
|
---|
1984 | // bounded planes are added to the polygons (reverse polygons
|
---|
1985 | // as they have to be outfacing
|
---|
1986 | for (int i = 0; i < (int)halfSpaces.size(); ++ i)
|
---|
1987 | {
|
---|
1988 | Polygon3 *p = GetBoundingBox().CrossSection(halfSpaces[i]);
|
---|
1989 |
|
---|
1990 | if (p->Valid(mEpsilon))
|
---|
1991 | {
|
---|
1992 | candidatePolys.push_back(p->CreateReversePolygon());
|
---|
1993 | DEL_PTR(p);
|
---|
1994 | }
|
---|
1995 | }
|
---|
1996 |
|
---|
1997 | // add faces of bounding box (also could be faces of the cell)
|
---|
1998 | for (int i = 0; i < 6; ++ i)
|
---|
1999 | {
|
---|
2000 | VertexContainer vertices;
|
---|
2001 |
|
---|
2002 | for (int j = 0; j < 4; ++ j)
|
---|
2003 | vertices.push_back(mBox.GetFace(i).mVertices[j]);
|
---|
2004 |
|
---|
2005 | candidatePolys.push_back(new Polygon3(vertices));
|
---|
2006 | }
|
---|
2007 |
|
---|
2008 | for (int i = 0; i < (int)candidatePolys.size(); ++ i)
|
---|
2009 | {
|
---|
2010 | // polygon is split by all other planes
|
---|
2011 | for (int j = 0; (j < (int)halfSpaces.size()) && candidatePolys[i]; ++ j)
|
---|
2012 | {
|
---|
2013 | if (i == j) // polygon and plane are coincident
|
---|
2014 | continue;
|
---|
2015 |
|
---|
2016 | VertexContainer splitPts;
|
---|
2017 | Polygon3 *frontPoly, *backPoly;
|
---|
2018 |
|
---|
2019 | const int cf =
|
---|
2020 | candidatePolys[i]->ClassifyPlane(halfSpaces[j],
|
---|
2021 | mEpsilon);
|
---|
2022 |
|
---|
2023 | switch (cf)
|
---|
2024 | {
|
---|
2025 | case Polygon3::SPLIT:
|
---|
2026 | frontPoly = new Polygon3();
|
---|
2027 | backPoly = new Polygon3();
|
---|
2028 |
|
---|
2029 | candidatePolys[i]->Split(halfSpaces[j],
|
---|
2030 | *frontPoly,
|
---|
2031 | *backPoly,
|
---|
2032 | mEpsilon);
|
---|
2033 |
|
---|
2034 | DEL_PTR(candidatePolys[i]);
|
---|
2035 |
|
---|
2036 | if (frontPoly->Valid(mEpsilon))
|
---|
2037 | candidatePolys[i] = frontPoly;
|
---|
2038 | else
|
---|
2039 | DEL_PTR(frontPoly);
|
---|
2040 |
|
---|
2041 | DEL_PTR(backPoly);
|
---|
2042 | break;
|
---|
2043 | case Polygon3::BACK_SIDE:
|
---|
2044 | DEL_PTR(candidatePolys[i]);
|
---|
2045 | break;
|
---|
2046 | // just take polygon as it is
|
---|
2047 | case Polygon3::FRONT_SIDE:
|
---|
2048 | case Polygon3::COINCIDENT:
|
---|
2049 | default:
|
---|
2050 | break;
|
---|
2051 | }
|
---|
2052 | }
|
---|
2053 |
|
---|
2054 | if (candidatePolys[i])
|
---|
2055 | geom.mPolys.push_back(candidatePolys[i]);
|
---|
2056 | }
|
---|
2057 | }
|
---|
2058 |
|
---|
2059 |
|
---|
2060 | void VspBspTree::ConstructGeometry(ViewCell *vc,
|
---|
2061 | BspNodeGeometry &vcGeom) const
|
---|
2062 | {
|
---|
2063 | ViewCellContainer leaves;
|
---|
2064 |
|
---|
2065 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
2066 |
|
---|
2067 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
2068 |
|
---|
2069 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
2070 | {
|
---|
2071 | BspLeaf *l = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
2072 |
|
---|
2073 | ConstructGeometry(l, vcGeom);
|
---|
2074 | }
|
---|
2075 | }
|
---|
2076 |
|
---|
2077 |
|
---|
2078 | typedef pair<BspNode *, BspNodeGeometry *> bspNodePair;
|
---|
2079 |
|
---|
2080 |
|
---|
2081 | int VspBspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,
|
---|
2082 | const bool onlyUnmailed) const
|
---|
2083 | {
|
---|
2084 | stack<bspNodePair> nodeStack;
|
---|
2085 |
|
---|
2086 | BspNodeGeometry nodeGeom;
|
---|
2087 | ConstructGeometry(n, nodeGeom);
|
---|
2088 |
|
---|
2089 | // split planes from the root to this node
|
---|
2090 | // needed to verify that we found neighbor leaf
|
---|
2091 | // TODO: really needed?
|
---|
2092 | vector<Plane3> halfSpaces;
|
---|
2093 | ExtractHalfSpaces(n, halfSpaces);
|
---|
2094 |
|
---|
2095 |
|
---|
2096 | BspNodeGeometry *rgeom = new BspNodeGeometry();
|
---|
2097 | ConstructGeometry(mRoot, *rgeom);
|
---|
2098 |
|
---|
2099 | nodeStack.push(bspNodePair(mRoot, rgeom));
|
---|
2100 |
|
---|
2101 | while (!nodeStack.empty())
|
---|
2102 | {
|
---|
2103 | BspNode *node = nodeStack.top().first;
|
---|
2104 | BspNodeGeometry *geom = nodeStack.top().second;
|
---|
2105 |
|
---|
2106 | nodeStack.pop();
|
---|
2107 |
|
---|
2108 | if (node->IsLeaf())
|
---|
2109 | {
|
---|
2110 | // test if this leaf is in valid view space
|
---|
2111 | if (node->TreeValid() &&
|
---|
2112 | (node != n) &&
|
---|
2113 | (!onlyUnmailed || !node->Mailed()))
|
---|
2114 | {
|
---|
2115 | bool isAdjacent = true;
|
---|
2116 |
|
---|
2117 | if (1)
|
---|
2118 | {
|
---|
2119 | // test all planes of current node if still adjacent
|
---|
2120 | for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i)
|
---|
2121 | {
|
---|
2122 | const int cf =
|
---|
2123 | Polygon3::ClassifyPlane(geom->mPolys,
|
---|
2124 | halfSpaces[i],
|
---|
2125 | mEpsilon);
|
---|
2126 |
|
---|
2127 | if (cf == Polygon3::BACK_SIDE)
|
---|
2128 | {
|
---|
2129 | isAdjacent = false;
|
---|
2130 | }
|
---|
2131 | }
|
---|
2132 | }
|
---|
2133 | else
|
---|
2134 | {
|
---|
2135 | // TODO: why is this wrong??
|
---|
2136 | // test all planes of current node if still adjacent
|
---|
2137 | for (int i = 0; (i < (int)nodeGeom.mPolys.size()) && isAdjacent; ++ i)
|
---|
2138 | {
|
---|
2139 | Polygon3 *poly = nodeGeom.mPolys[i];
|
---|
2140 |
|
---|
2141 | const int cf =
|
---|
2142 | Polygon3::ClassifyPlane(geom->mPolys,
|
---|
2143 | poly->GetSupportingPlane(),
|
---|
2144 | mEpsilon);
|
---|
2145 |
|
---|
2146 | if (cf == Polygon3::BACK_SIDE)
|
---|
2147 | {
|
---|
2148 | isAdjacent = false;
|
---|
2149 | }
|
---|
2150 | }
|
---|
2151 | }
|
---|
2152 | // neighbor was found
|
---|
2153 | if (isAdjacent)
|
---|
2154 | {
|
---|
2155 | neighbors.push_back(dynamic_cast<BspLeaf *>(node));
|
---|
2156 | }
|
---|
2157 | }
|
---|
2158 | }
|
---|
2159 | else
|
---|
2160 | {
|
---|
2161 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2162 |
|
---|
2163 | const int cf = Polygon3::ClassifyPlane(nodeGeom.mPolys,
|
---|
2164 | interior->GetPlane(),
|
---|
2165 | mEpsilon);
|
---|
2166 |
|
---|
2167 | BspNode *front = interior->GetFront();
|
---|
2168 | BspNode *back = interior->GetBack();
|
---|
2169 |
|
---|
2170 | BspNodeGeometry *fGeom = new BspNodeGeometry();
|
---|
2171 | BspNodeGeometry *bGeom = new BspNodeGeometry();
|
---|
2172 |
|
---|
2173 | geom->SplitGeometry(*fGeom,
|
---|
2174 | *bGeom,
|
---|
2175 | interior->GetPlane(),
|
---|
2176 | mBox,
|
---|
2177 | mEpsilon);
|
---|
2178 |
|
---|
2179 | if (cf == Polygon3::FRONT_SIDE)
|
---|
2180 | {
|
---|
2181 | nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
|
---|
2182 | DEL_PTR(bGeom);
|
---|
2183 | }
|
---|
2184 | else
|
---|
2185 | {
|
---|
2186 | if (cf == Polygon3::BACK_SIDE)
|
---|
2187 | {
|
---|
2188 | nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
|
---|
2189 | DEL_PTR(fGeom);
|
---|
2190 | }
|
---|
2191 | else
|
---|
2192 | { // random decision
|
---|
2193 | nodeStack.push(bspNodePair(front, fGeom));
|
---|
2194 | nodeStack.push(bspNodePair(back, bGeom));
|
---|
2195 | }
|
---|
2196 | }
|
---|
2197 | }
|
---|
2198 |
|
---|
2199 | DEL_PTR(geom);
|
---|
2200 | }
|
---|
2201 |
|
---|
2202 | return (int)neighbors.size();
|
---|
2203 | }
|
---|
2204 |
|
---|
2205 |
|
---|
2206 |
|
---|
2207 | int VspBspTree::FindApproximateNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,
|
---|
2208 | const bool onlyUnmailed) const
|
---|
2209 | {
|
---|
2210 | stack<bspNodePair> nodeStack;
|
---|
2211 |
|
---|
2212 | BspNodeGeometry nodeGeom;
|
---|
2213 | ConstructGeometry(n, nodeGeom);
|
---|
2214 |
|
---|
2215 | // split planes from the root to this node
|
---|
2216 | // needed to verify that we found neighbor leaf
|
---|
2217 | // TODO: really needed?
|
---|
2218 | vector<Plane3> halfSpaces;
|
---|
2219 | ExtractHalfSpaces(n, halfSpaces);
|
---|
2220 |
|
---|
2221 |
|
---|
2222 | BspNodeGeometry *rgeom = new BspNodeGeometry();
|
---|
2223 | ConstructGeometry(mRoot, *rgeom);
|
---|
2224 |
|
---|
2225 | nodeStack.push(bspNodePair(mRoot, rgeom));
|
---|
2226 |
|
---|
2227 | while (!nodeStack.empty())
|
---|
2228 | {
|
---|
2229 | BspNode *node = nodeStack.top().first;
|
---|
2230 | BspNodeGeometry *geom = nodeStack.top().second;
|
---|
2231 |
|
---|
2232 | nodeStack.pop();
|
---|
2233 |
|
---|
2234 | if (node->IsLeaf())
|
---|
2235 | {
|
---|
2236 | // test if this leaf is in valid view space
|
---|
2237 | if (node->TreeValid() &&
|
---|
2238 | (node != n) &&
|
---|
2239 | (!onlyUnmailed || !node->Mailed()))
|
---|
2240 | {
|
---|
2241 | bool isAdjacent = true;
|
---|
2242 |
|
---|
2243 | // neighbor was found
|
---|
2244 | if (isAdjacent)
|
---|
2245 | {
|
---|
2246 | neighbors.push_back(dynamic_cast<BspLeaf *>(node));
|
---|
2247 | }
|
---|
2248 | }
|
---|
2249 | }
|
---|
2250 | else
|
---|
2251 | {
|
---|
2252 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2253 |
|
---|
2254 | const int cf = Polygon3::ClassifyPlane(nodeGeom.mPolys,
|
---|
2255 | interior->GetPlane(),
|
---|
2256 | mEpsilon);
|
---|
2257 |
|
---|
2258 | BspNode *front = interior->GetFront();
|
---|
2259 | BspNode *back = interior->GetBack();
|
---|
2260 |
|
---|
2261 | BspNodeGeometry *fGeom = new BspNodeGeometry();
|
---|
2262 | BspNodeGeometry *bGeom = new BspNodeGeometry();
|
---|
2263 |
|
---|
2264 | geom->SplitGeometry(*fGeom,
|
---|
2265 | *bGeom,
|
---|
2266 | interior->GetPlane(),
|
---|
2267 | mBox,
|
---|
2268 | mEpsilon);
|
---|
2269 |
|
---|
2270 | if (cf == Polygon3::FRONT_SIDE)
|
---|
2271 | {
|
---|
2272 | nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
|
---|
2273 | DEL_PTR(bGeom);
|
---|
2274 | }
|
---|
2275 | else
|
---|
2276 | {
|
---|
2277 | if (cf == Polygon3::BACK_SIDE)
|
---|
2278 | {
|
---|
2279 | nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
|
---|
2280 | DEL_PTR(fGeom);
|
---|
2281 | }
|
---|
2282 | else
|
---|
2283 | { // random decision
|
---|
2284 | nodeStack.push(bspNodePair(front, fGeom));
|
---|
2285 | nodeStack.push(bspNodePair(back, bGeom));
|
---|
2286 | }
|
---|
2287 | }
|
---|
2288 | }
|
---|
2289 |
|
---|
2290 | DEL_PTR(geom);
|
---|
2291 | }
|
---|
2292 |
|
---|
2293 | return (int)neighbors.size();
|
---|
2294 | }
|
---|
2295 |
|
---|
2296 |
|
---|
2297 |
|
---|
2298 | BspLeaf *VspBspTree::GetRandomLeaf(const Plane3 &halfspace)
|
---|
2299 | {
|
---|
2300 | stack<BspNode *> nodeStack;
|
---|
2301 | nodeStack.push(mRoot);
|
---|
2302 |
|
---|
2303 | int mask = rand();
|
---|
2304 |
|
---|
2305 | while (!nodeStack.empty())
|
---|
2306 | {
|
---|
2307 | BspNode *node = nodeStack.top();
|
---|
2308 | nodeStack.pop();
|
---|
2309 |
|
---|
2310 | if (node->IsLeaf())
|
---|
2311 | {
|
---|
2312 | return dynamic_cast<BspLeaf *>(node);
|
---|
2313 | }
|
---|
2314 | else
|
---|
2315 | {
|
---|
2316 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2317 | BspNode *next;
|
---|
2318 | BspNodeGeometry geom;
|
---|
2319 |
|
---|
2320 | // todo: not very efficient: constructs full cell everytime
|
---|
2321 | ConstructGeometry(interior, geom);
|
---|
2322 |
|
---|
2323 | const int cf =
|
---|
2324 | Polygon3::ClassifyPlane(geom.mPolys, halfspace, mEpsilon);
|
---|
2325 |
|
---|
2326 | if (cf == Polygon3::BACK_SIDE)
|
---|
2327 | next = interior->GetFront();
|
---|
2328 | else
|
---|
2329 | if (cf == Polygon3::FRONT_SIDE)
|
---|
2330 | next = interior->GetFront();
|
---|
2331 | else
|
---|
2332 | {
|
---|
2333 | // random decision
|
---|
2334 | if (mask & 1)
|
---|
2335 | next = interior->GetBack();
|
---|
2336 | else
|
---|
2337 | next = interior->GetFront();
|
---|
2338 | mask = mask >> 1;
|
---|
2339 | }
|
---|
2340 |
|
---|
2341 | nodeStack.push(next);
|
---|
2342 | }
|
---|
2343 | }
|
---|
2344 |
|
---|
2345 | return NULL;
|
---|
2346 | }
|
---|
2347 |
|
---|
2348 | BspLeaf *VspBspTree::GetRandomLeaf(const bool onlyUnmailed)
|
---|
2349 | {
|
---|
2350 | stack<BspNode *> nodeStack;
|
---|
2351 |
|
---|
2352 | nodeStack.push(mRoot);
|
---|
2353 |
|
---|
2354 | int mask = rand();
|
---|
2355 |
|
---|
2356 | while (!nodeStack.empty())
|
---|
2357 | {
|
---|
2358 | BspNode *node = nodeStack.top();
|
---|
2359 | nodeStack.pop();
|
---|
2360 |
|
---|
2361 | if (node->IsLeaf())
|
---|
2362 | {
|
---|
2363 | if ( (!onlyUnmailed || !node->Mailed()) )
|
---|
2364 | return dynamic_cast<BspLeaf *>(node);
|
---|
2365 | }
|
---|
2366 | else
|
---|
2367 | {
|
---|
2368 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2369 |
|
---|
2370 | // random decision
|
---|
2371 | if (mask & 1)
|
---|
2372 | nodeStack.push(interior->GetBack());
|
---|
2373 | else
|
---|
2374 | nodeStack.push(interior->GetFront());
|
---|
2375 |
|
---|
2376 | mask = mask >> 1;
|
---|
2377 | }
|
---|
2378 | }
|
---|
2379 |
|
---|
2380 | return NULL;
|
---|
2381 | }
|
---|
2382 |
|
---|
2383 | int VspBspTree::ComputePvsSize(const RayInfoContainer &rays) const
|
---|
2384 | {
|
---|
2385 | int pvsSize = 0;
|
---|
2386 |
|
---|
2387 | RayInfoContainer::const_iterator rit, rit_end = rays.end();
|
---|
2388 |
|
---|
2389 | Intersectable::NewMail();
|
---|
2390 |
|
---|
2391 | for (rit = rays.begin(); rit != rays.end(); ++ rit)
|
---|
2392 | {
|
---|
2393 | VssRay *ray = (*rit).mRay;
|
---|
2394 |
|
---|
2395 | if (ray->mOriginObject)
|
---|
2396 | {
|
---|
2397 | if (!ray->mOriginObject->Mailed())
|
---|
2398 | {
|
---|
2399 | ray->mOriginObject->Mail();
|
---|
2400 | ++ pvsSize;
|
---|
2401 | }
|
---|
2402 | }
|
---|
2403 | if (ray->mTerminationObject)
|
---|
2404 | {
|
---|
2405 | if (!ray->mTerminationObject->Mailed())
|
---|
2406 | {
|
---|
2407 | ray->mTerminationObject->Mail();
|
---|
2408 | ++ pvsSize;
|
---|
2409 | }
|
---|
2410 | }
|
---|
2411 | }
|
---|
2412 |
|
---|
2413 | return pvsSize;
|
---|
2414 | }
|
---|
2415 |
|
---|
2416 | float VspBspTree::GetEpsilon() const
|
---|
2417 | {
|
---|
2418 | return mEpsilon;
|
---|
2419 | }
|
---|
2420 |
|
---|
2421 |
|
---|
2422 | int VspBspTree::SplitPolygons(const Plane3 &plane,
|
---|
2423 | PolygonContainer &polys,
|
---|
2424 | PolygonContainer &frontPolys,
|
---|
2425 | PolygonContainer &backPolys,
|
---|
2426 | PolygonContainer &coincident) const
|
---|
2427 | {
|
---|
2428 | int splits = 0;
|
---|
2429 |
|
---|
2430 | PolygonContainer::const_iterator it, it_end = polys.end();
|
---|
2431 |
|
---|
2432 | for (it = polys.begin(); it != polys.end(); ++ it)
|
---|
2433 | {
|
---|
2434 | Polygon3 *poly = *it;
|
---|
2435 |
|
---|
2436 | // classify polygon
|
---|
2437 | const int cf = poly->ClassifyPlane(plane, mEpsilon);
|
---|
2438 |
|
---|
2439 | switch (cf)
|
---|
2440 | {
|
---|
2441 | case Polygon3::COINCIDENT:
|
---|
2442 | coincident.push_back(poly);
|
---|
2443 | break;
|
---|
2444 | case Polygon3::FRONT_SIDE:
|
---|
2445 | frontPolys.push_back(poly);
|
---|
2446 | break;
|
---|
2447 | case Polygon3::BACK_SIDE:
|
---|
2448 | backPolys.push_back(poly);
|
---|
2449 | break;
|
---|
2450 | case Polygon3::SPLIT:
|
---|
2451 | backPolys.push_back(poly);
|
---|
2452 | frontPolys.push_back(poly);
|
---|
2453 | ++ splits;
|
---|
2454 | break;
|
---|
2455 | default:
|
---|
2456 | Debug << "SHOULD NEVER COME HERE\n";
|
---|
2457 | break;
|
---|
2458 | }
|
---|
2459 | }
|
---|
2460 |
|
---|
2461 | return splits;
|
---|
2462 | }
|
---|
2463 |
|
---|
2464 |
|
---|
2465 | int VspBspTree::CastLineSegment(const Vector3 &origin,
|
---|
2466 | const Vector3 &termination,
|
---|
2467 | vector<ViewCell *> &viewcells)
|
---|
2468 | {
|
---|
2469 | int hits = 0;
|
---|
2470 | stack<BspRayTraversalData> tQueue;
|
---|
2471 |
|
---|
2472 | float mint = 0.0f, maxt = 1.0f;
|
---|
2473 |
|
---|
2474 | Intersectable::NewMail();
|
---|
2475 | ViewCell::NewMail();
|
---|
2476 |
|
---|
2477 | Vector3 entp = origin;
|
---|
2478 | Vector3 extp = termination;
|
---|
2479 |
|
---|
2480 | BspNode *node = mRoot;
|
---|
2481 | BspNode *farChild = NULL;
|
---|
2482 |
|
---|
2483 | float t;
|
---|
2484 | while (1)
|
---|
2485 | {
|
---|
2486 | if (!node->IsLeaf())
|
---|
2487 | {
|
---|
2488 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
2489 |
|
---|
2490 | Plane3 splitPlane = in->GetPlane();
|
---|
2491 |
|
---|
2492 | const int entSide = splitPlane.Side(entp);
|
---|
2493 | const int extSide = splitPlane.Side(extp);
|
---|
2494 |
|
---|
2495 | if (entSide < 0)
|
---|
2496 | {
|
---|
2497 | node = in->GetBack();
|
---|
2498 | // plane does not split ray => no far child
|
---|
2499 | if (extSide <= 0)
|
---|
2500 | continue;
|
---|
2501 |
|
---|
2502 | farChild = in->GetFront(); // plane splits ray
|
---|
2503 | }
|
---|
2504 | else if (entSide > 0)
|
---|
2505 | {
|
---|
2506 | node = in->GetFront();
|
---|
2507 |
|
---|
2508 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
2509 | continue;
|
---|
2510 |
|
---|
2511 | farChild = in->GetBack(); // plane splits ray
|
---|
2512 | }
|
---|
2513 | else // ray end point on plane
|
---|
2514 | { // NOTE: what to do if ray is coincident with plane?
|
---|
2515 | if (extSide < 0)
|
---|
2516 | node = in->GetBack();
|
---|
2517 | else
|
---|
2518 | node = in->GetFront();
|
---|
2519 |
|
---|
2520 | continue; // no far child
|
---|
2521 | }
|
---|
2522 |
|
---|
2523 | // push data for far child
|
---|
2524 | tQueue.push(BspRayTraversalData(farChild, extp));
|
---|
2525 |
|
---|
2526 | // find intersection of ray segment with plane
|
---|
2527 | extp = splitPlane.FindIntersection(origin, extp, &t);
|
---|
2528 | }
|
---|
2529 | else
|
---|
2530 | {
|
---|
2531 | // reached leaf => intersection with view cell
|
---|
2532 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
2533 |
|
---|
2534 | ViewCell *viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell());
|
---|
2535 | if (!viewCell->Mailed())
|
---|
2536 | {
|
---|
2537 | viewcells.push_back(viewCell);
|
---|
2538 | viewCell->Mail();
|
---|
2539 | ++ hits;
|
---|
2540 | }
|
---|
2541 |
|
---|
2542 | //-- fetch the next far child from the stack
|
---|
2543 | if (tQueue.empty())
|
---|
2544 | break;
|
---|
2545 |
|
---|
2546 | entp = extp;
|
---|
2547 |
|
---|
2548 | BspRayTraversalData &s = tQueue.top();
|
---|
2549 |
|
---|
2550 | node = s.mNode;
|
---|
2551 | extp = s.mExitPoint;
|
---|
2552 |
|
---|
2553 | tQueue.pop();
|
---|
2554 | }
|
---|
2555 | }
|
---|
2556 |
|
---|
2557 | return hits;
|
---|
2558 | }
|
---|
2559 |
|
---|
2560 |
|
---|
2561 |
|
---|
2562 |
|
---|
2563 | int VspBspTree::TreeDistance(BspNode *n1, BspNode *n2) const
|
---|
2564 | {
|
---|
2565 | std::deque<BspNode *> path1;
|
---|
2566 | BspNode *p1 = n1;
|
---|
2567 |
|
---|
2568 | // create path from node 1 to root
|
---|
2569 | while (p1)
|
---|
2570 | {
|
---|
2571 | if (p1 == n2) // second node on path
|
---|
2572 | return (int)path1.size();
|
---|
2573 |
|
---|
2574 | path1.push_front(p1);
|
---|
2575 | p1 = p1->GetParent();
|
---|
2576 | }
|
---|
2577 |
|
---|
2578 | int depth = n2->GetDepth();
|
---|
2579 | int d = depth;
|
---|
2580 |
|
---|
2581 | BspNode *p2 = n2;
|
---|
2582 |
|
---|
2583 | // compare with same depth
|
---|
2584 | while (1)
|
---|
2585 | {
|
---|
2586 | if ((d < (int)path1.size()) && (p2 == path1[d]))
|
---|
2587 | return (depth - d) + ((int)path1.size() - 1 - d);
|
---|
2588 |
|
---|
2589 | -- d;
|
---|
2590 | p2 = p2->GetParent();
|
---|
2591 | }
|
---|
2592 |
|
---|
2593 | return 0; // never come here
|
---|
2594 | }
|
---|
2595 |
|
---|
2596 |
|
---|
2597 | BspNode *VspBspTree::CollapseTree(BspNode *node, int &collapsed)
|
---|
2598 | {
|
---|
2599 | // TODO
|
---|
2600 | #if VC_HISTORY
|
---|
2601 | if (node->IsLeaf())
|
---|
2602 | return node;
|
---|
2603 |
|
---|
2604 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2605 |
|
---|
2606 | BspNode *front = CollapseTree(interior->GetFront(), collapsed);
|
---|
2607 | BspNode *back = CollapseTree(interior->GetBack(), collapsed);
|
---|
2608 |
|
---|
2609 | if (front->IsLeaf() && back->IsLeaf())
|
---|
2610 | {
|
---|
2611 | BspLeaf *frontLeaf = dynamic_cast<BspLeaf *>(front);
|
---|
2612 | BspLeaf *backLeaf = dynamic_cast<BspLeaf *>(back);
|
---|
2613 |
|
---|
2614 | //-- collapse tree
|
---|
2615 | if (frontLeaf->GetViewCell() == backLeaf->GetViewCell())
|
---|
2616 | {
|
---|
2617 | BspViewCell *vc = frontLeaf->GetViewCell();
|
---|
2618 |
|
---|
2619 | BspLeaf *leaf = new BspLeaf(interior->GetParent(), vc);
|
---|
2620 | leaf->SetTreeValid(frontLeaf->TreeValid());
|
---|
2621 |
|
---|
2622 | // replace a link from node's parent
|
---|
2623 | if (leaf->GetParent())
|
---|
2624 | leaf->GetParent()->ReplaceChildLink(node, leaf);
|
---|
2625 | else
|
---|
2626 | mRoot = leaf;
|
---|
2627 |
|
---|
2628 | ++ collapsed;
|
---|
2629 | delete interior;
|
---|
2630 |
|
---|
2631 | return leaf;
|
---|
2632 | }
|
---|
2633 | }
|
---|
2634 | #endif
|
---|
2635 | return node;
|
---|
2636 | }
|
---|
2637 |
|
---|
2638 |
|
---|
2639 | int VspBspTree::CollapseTree()
|
---|
2640 | {
|
---|
2641 | int collapsed = 0;
|
---|
2642 | //TODO
|
---|
2643 | #if VC_HISTORY
|
---|
2644 | (void)CollapseTree(mRoot, collapsed);
|
---|
2645 |
|
---|
2646 | // revalidate leaves
|
---|
2647 | RepairViewCellsLeafLists();
|
---|
2648 | #endif
|
---|
2649 | return collapsed;
|
---|
2650 | }
|
---|
2651 |
|
---|
2652 |
|
---|
2653 | void VspBspTree::RepairViewCellsLeafLists()
|
---|
2654 | {
|
---|
2655 | // TODO
|
---|
2656 | #if VC_HISTORY
|
---|
2657 | // list not valid anymore => clear
|
---|
2658 | stack<BspNode *> nodeStack;
|
---|
2659 | nodeStack.push(mRoot);
|
---|
2660 |
|
---|
2661 | ViewCell::NewMail();
|
---|
2662 |
|
---|
2663 | while (!nodeStack.empty())
|
---|
2664 | {
|
---|
2665 | BspNode *node = nodeStack.top();
|
---|
2666 | nodeStack.pop();
|
---|
2667 |
|
---|
2668 | if (node->IsLeaf())
|
---|
2669 | {
|
---|
2670 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
2671 |
|
---|
2672 | BspViewCell *viewCell = leaf->GetViewCell();
|
---|
2673 |
|
---|
2674 | if (!viewCell->Mailed())
|
---|
2675 | {
|
---|
2676 | viewCell->mLeaves.clear();
|
---|
2677 | viewCell->Mail();
|
---|
2678 | }
|
---|
2679 |
|
---|
2680 | viewCell->mLeaves.push_back(leaf);
|
---|
2681 |
|
---|
2682 | }
|
---|
2683 | else
|
---|
2684 | {
|
---|
2685 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2686 |
|
---|
2687 | nodeStack.push(interior->GetFront());
|
---|
2688 | nodeStack.push(interior->GetBack());
|
---|
2689 | }
|
---|
2690 | }
|
---|
2691 | // TODO
|
---|
2692 | #endif
|
---|
2693 | }
|
---|
2694 |
|
---|
2695 |
|
---|
2696 | typedef pair<BspNode *, BspNodeGeometry *> bspNodePair;
|
---|
2697 |
|
---|
2698 |
|
---|
2699 | int VspBspTree::CastBeam(Beam &beam)
|
---|
2700 | {
|
---|
2701 | stack<bspNodePair> nodeStack;
|
---|
2702 | BspNodeGeometry *rgeom = new BspNodeGeometry();
|
---|
2703 | ConstructGeometry(mRoot, *rgeom);
|
---|
2704 |
|
---|
2705 | nodeStack.push(bspNodePair(mRoot, rgeom));
|
---|
2706 |
|
---|
2707 | ViewCell::NewMail();
|
---|
2708 |
|
---|
2709 | while (!nodeStack.empty())
|
---|
2710 | {
|
---|
2711 | BspNode *node = nodeStack.top().first;
|
---|
2712 | BspNodeGeometry *geom = nodeStack.top().second;
|
---|
2713 | nodeStack.pop();
|
---|
2714 |
|
---|
2715 | AxisAlignedBox3 box;
|
---|
2716 | box.Initialize();
|
---|
2717 | geom->IncludeInBox(box);
|
---|
2718 |
|
---|
2719 | const int side = beam.ComputeIntersection(box);
|
---|
2720 |
|
---|
2721 | switch (side)
|
---|
2722 | {
|
---|
2723 | case -1:
|
---|
2724 | CollectViewCells(node, true, beam.mViewCells, true);
|
---|
2725 | break;
|
---|
2726 | case 0:
|
---|
2727 |
|
---|
2728 | if (node->IsLeaf())
|
---|
2729 | {
|
---|
2730 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
2731 |
|
---|
2732 | if (!leaf->GetViewCell()->Mailed() && leaf->TreeValid())
|
---|
2733 | {
|
---|
2734 | leaf->GetViewCell()->Mail();
|
---|
2735 | beam.mViewCells.push_back(leaf->GetViewCell());
|
---|
2736 | }
|
---|
2737 | }
|
---|
2738 | else
|
---|
2739 | {
|
---|
2740 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2741 |
|
---|
2742 | BspNode *first = interior->GetFront();
|
---|
2743 | BspNode *second = interior->GetBack();
|
---|
2744 |
|
---|
2745 | BspNodeGeometry *firstGeom = new BspNodeGeometry();
|
---|
2746 | BspNodeGeometry *secondGeom = new BspNodeGeometry();
|
---|
2747 |
|
---|
2748 | geom->SplitGeometry(*firstGeom,
|
---|
2749 | *secondGeom,
|
---|
2750 | interior->GetPlane(),
|
---|
2751 | mBox,
|
---|
2752 | mEpsilon);
|
---|
2753 |
|
---|
2754 | // decide on the order of the nodes
|
---|
2755 | if (DotProd(beam.mPlanes[0].mNormal,
|
---|
2756 | interior->GetPlane().mNormal) > 0)
|
---|
2757 | {
|
---|
2758 | swap(first, second);
|
---|
2759 | swap(firstGeom, secondGeom);
|
---|
2760 | }
|
---|
2761 |
|
---|
2762 | nodeStack.push(bspNodePair(first, firstGeom));
|
---|
2763 | nodeStack.push(bspNodePair(second, secondGeom));
|
---|
2764 | }
|
---|
2765 |
|
---|
2766 | break;
|
---|
2767 | default:
|
---|
2768 | // default: cull
|
---|
2769 | break;
|
---|
2770 | }
|
---|
2771 |
|
---|
2772 | DEL_PTR(geom);
|
---|
2773 |
|
---|
2774 | }
|
---|
2775 |
|
---|
2776 | return (int)beam.mViewCells.size();
|
---|
2777 | }
|
---|
2778 |
|
---|
2779 |
|
---|
2780 | void VspBspTree::SetViewCellsManager(ViewCellsManager *vcm)
|
---|
2781 | {
|
---|
2782 | mViewCellsManager = vcm;
|
---|
2783 | }
|
---|
2784 |
|
---|
2785 |
|
---|
2786 | int VspBspTree::CollectMergeCandidates(const vector<BspLeaf *> leaves,
|
---|
2787 | vector<MergeCandidate> &candidates)
|
---|
2788 | {
|
---|
2789 | BspLeaf::NewMail();
|
---|
2790 |
|
---|
2791 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
2792 |
|
---|
2793 | int numCandidates = 0;
|
---|
2794 |
|
---|
2795 | // find merge candidates and push them into queue
|
---|
2796 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
2797 | {
|
---|
2798 | BspLeaf *leaf = *it;
|
---|
2799 |
|
---|
2800 | // the same leaves must not be part of two merge candidates
|
---|
2801 | leaf->Mail();
|
---|
2802 | vector<BspLeaf *> neighbors;
|
---|
2803 | FindNeighbors(leaf, neighbors, true);
|
---|
2804 | //FindApproximateNeighbors(leaf, neighbors, true);
|
---|
2805 | vector<BspLeaf *>::const_iterator nit, nit_end = neighbors.end();
|
---|
2806 |
|
---|
2807 | // TODO: test if at least one ray goes from one leaf to the other
|
---|
2808 | for (nit = neighbors.begin(); nit != nit_end; ++ nit)
|
---|
2809 | {
|
---|
2810 | if ((*nit)->GetViewCell() != leaf->GetViewCell())
|
---|
2811 | {
|
---|
2812 | MergeCandidate mc(leaf->GetViewCell(), (*nit)->GetViewCell());
|
---|
2813 | candidates.push_back(mc);
|
---|
2814 |
|
---|
2815 | ++ numCandidates;
|
---|
2816 | if ((numCandidates % 1000) == 0)
|
---|
2817 | {
|
---|
2818 | cout << "collected " << numCandidates << " merge candidates" << endl;
|
---|
2819 | }
|
---|
2820 | }
|
---|
2821 | }
|
---|
2822 | }
|
---|
2823 |
|
---|
2824 | Debug << "merge queue: " << (int)candidates.size() << endl;
|
---|
2825 | Debug << "leaves in queue: " << numCandidates << endl;
|
---|
2826 |
|
---|
2827 |
|
---|
2828 | return (int)leaves.size();
|
---|
2829 | }
|
---|
2830 |
|
---|
2831 |
|
---|
2832 | int VspBspTree::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
2833 | vector<MergeCandidate> &candidates)
|
---|
2834 | {
|
---|
2835 | ViewCell::NewMail();
|
---|
2836 | long startTime = GetTime();
|
---|
2837 |
|
---|
2838 | map<BspLeaf *, vector<BspLeaf*> > neighborMap;
|
---|
2839 | ViewCellContainer::const_iterator iit;
|
---|
2840 |
|
---|
2841 | int numLeaves = 0;
|
---|
2842 |
|
---|
2843 | BspLeaf::NewMail();
|
---|
2844 |
|
---|
2845 | for (int i = 0; i < (int)rays.size(); ++ i)
|
---|
2846 | {
|
---|
2847 | VssRay *ray = rays[i];
|
---|
2848 |
|
---|
2849 | // traverse leaves stored in the rays and compare and
|
---|
2850 | // merge consecutive leaves (i.e., the neighbors in the tree)
|
---|
2851 | if (ray->mViewCells.size() < 2)
|
---|
2852 | continue;
|
---|
2853 | //TODO viewcellhierarchy
|
---|
2854 | iit = ray->mViewCells.begin();
|
---|
2855 | BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*(iit ++));
|
---|
2856 | BspLeaf *leaf = bspVc->mLeaf;
|
---|
2857 |
|
---|
2858 | // traverse intersections
|
---|
2859 | // consecutive leaves are neighbors => add them to queue
|
---|
2860 | for (; iit != ray->mViewCells.end(); ++ iit)
|
---|
2861 | {
|
---|
2862 | // next pair
|
---|
2863 | BspLeaf *prevLeaf = leaf;
|
---|
2864 | bspVc = dynamic_cast<BspViewCell *>(*iit);
|
---|
2865 | leaf = bspVc->mLeaf;
|
---|
2866 |
|
---|
2867 | // view space not valid or same view cell
|
---|
2868 | if (!leaf->TreeValid() || !prevLeaf->TreeValid() ||
|
---|
2869 | (leaf->GetViewCell() == prevLeaf->GetViewCell()))
|
---|
2870 | continue;
|
---|
2871 |
|
---|
2872 | vector<BspLeaf *> &neighbors = neighborMap[leaf];
|
---|
2873 |
|
---|
2874 | bool found = false;
|
---|
2875 |
|
---|
2876 | // both leaves inserted in queue already =>
|
---|
2877 | // look if double pair already exists
|
---|
2878 | if (leaf->Mailed() && prevLeaf->Mailed())
|
---|
2879 | {
|
---|
2880 | vector<BspLeaf *>::const_iterator it, it_end = neighbors.end();
|
---|
2881 |
|
---|
2882 | for (it = neighbors.begin(); !found && (it != it_end); ++ it)
|
---|
2883 | if (*it == prevLeaf)
|
---|
2884 | found = true; // already in queue
|
---|
2885 | }
|
---|
2886 |
|
---|
2887 | if (!found)
|
---|
2888 | {
|
---|
2889 | // this pair is not in map yet
|
---|
2890 | // => insert into the neighbor map and the queue
|
---|
2891 | neighbors.push_back(prevLeaf);
|
---|
2892 | neighborMap[prevLeaf].push_back(leaf);
|
---|
2893 |
|
---|
2894 | leaf->Mail();
|
---|
2895 | prevLeaf->Mail();
|
---|
2896 |
|
---|
2897 | MergeCandidate mc(leaf->GetViewCell(), prevLeaf->GetViewCell());
|
---|
2898 |
|
---|
2899 | candidates.push_back(mc);
|
---|
2900 |
|
---|
2901 | if (((int)candidates.size() % 1000) == 0)
|
---|
2902 | {
|
---|
2903 | cout << "collected " << (int)candidates.size() << " merge candidates" << endl;
|
---|
2904 | }
|
---|
2905 | }
|
---|
2906 | }
|
---|
2907 | }
|
---|
2908 |
|
---|
2909 | Debug << "neighbormap size: " << (int)neighborMap.size() << endl;
|
---|
2910 | Debug << "merge queue: " << (int)candidates.size() << endl;
|
---|
2911 | Debug << "leaves in queue: " << numLeaves << endl;
|
---|
2912 |
|
---|
2913 |
|
---|
2914 | //-- collect the leaves which haven't been found by ray casting
|
---|
2915 | if (0)
|
---|
2916 | {
|
---|
2917 | cout << "finding additional merge candidates using geometry" << endl;
|
---|
2918 | vector<BspLeaf *> leaves;
|
---|
2919 | CollectLeaves(leaves, true);
|
---|
2920 | Debug << "found " << (int)leaves.size() << " new leaves" << endl << endl;
|
---|
2921 | CollectMergeCandidates(leaves, candidates);
|
---|
2922 | }
|
---|
2923 |
|
---|
2924 | return numLeaves;
|
---|
2925 | }
|
---|
2926 |
|
---|
2927 |
|
---|
2928 |
|
---|
2929 |
|
---|
2930 | ViewCell *VspBspTree::GetViewCell(const Vector3 &point)
|
---|
2931 | {
|
---|
2932 | if (mRoot == NULL)
|
---|
2933 | return NULL;
|
---|
2934 |
|
---|
2935 | stack<BspNode *> nodeStack;
|
---|
2936 | nodeStack.push(mRoot);
|
---|
2937 |
|
---|
2938 | ViewCell *viewcell = NULL;
|
---|
2939 |
|
---|
2940 | while (!nodeStack.empty()) {
|
---|
2941 | BspNode *node = nodeStack.top();
|
---|
2942 | nodeStack.pop();
|
---|
2943 |
|
---|
2944 | if (node->IsLeaf()) {
|
---|
2945 | viewcell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
2946 | break;
|
---|
2947 | } else {
|
---|
2948 |
|
---|
2949 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2950 |
|
---|
2951 | // random decision
|
---|
2952 | if (interior->GetPlane().Side(point) < 0)
|
---|
2953 | nodeStack.push(interior->GetBack());
|
---|
2954 | else
|
---|
2955 | nodeStack.push(interior->GetFront());
|
---|
2956 | }
|
---|
2957 | }
|
---|
2958 |
|
---|
2959 | return viewcell;
|
---|
2960 | }
|
---|
2961 |
|
---|
2962 |
|
---|
2963 | bool VspBspTree::ViewPointValid(const Vector3 &viewPoint) const
|
---|
2964 | {
|
---|
2965 | BspNode *node = mRoot;
|
---|
2966 |
|
---|
2967 | while (1)
|
---|
2968 | {
|
---|
2969 | // early exit
|
---|
2970 | if (node->TreeValid())
|
---|
2971 | return true;
|
---|
2972 |
|
---|
2973 | if (node->IsLeaf())
|
---|
2974 | return false;
|
---|
2975 |
|
---|
2976 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
2977 |
|
---|
2978 | if (in->GetPlane().Side(viewPoint) <= 0)
|
---|
2979 | {
|
---|
2980 | node = in->GetBack();
|
---|
2981 | }
|
---|
2982 | else
|
---|
2983 | {
|
---|
2984 | node = in->GetFront();
|
---|
2985 | }
|
---|
2986 | }
|
---|
2987 |
|
---|
2988 | // should never come here
|
---|
2989 | return false;
|
---|
2990 | }
|
---|
2991 |
|
---|
2992 |
|
---|
2993 | void VspBspTree::PropagateUpValidity(BspNode *node)
|
---|
2994 | {
|
---|
2995 | const bool isValid = node->TreeValid();
|
---|
2996 |
|
---|
2997 | // propagative up invalid flag until only invalid nodes exist over this node
|
---|
2998 | if (!isValid)
|
---|
2999 | {
|
---|
3000 | while (!node->IsRoot() && node->GetParent()->TreeValid())
|
---|
3001 | {
|
---|
3002 | node = node->GetParent();
|
---|
3003 | node->SetTreeValid(false);
|
---|
3004 | }
|
---|
3005 | }
|
---|
3006 | else
|
---|
3007 | {
|
---|
3008 | // propagative up valid flag until one of the subtrees is invalid
|
---|
3009 | while (!node->IsRoot() && !node->TreeValid())
|
---|
3010 | {
|
---|
3011 | node = node->GetParent();
|
---|
3012 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3013 |
|
---|
3014 | // the parent is valid iff both leaves are valid
|
---|
3015 | node->SetTreeValid(interior->GetBack()->TreeValid() &&
|
---|
3016 | interior->GetFront()->TreeValid());
|
---|
3017 | }
|
---|
3018 | }
|
---|
3019 | }
|
---|
3020 |
|
---|
3021 |
|
---|
3022 | bool VspBspTree::Export(ofstream &stream)
|
---|
3023 | {
|
---|
3024 | ExportNode(mRoot, stream);
|
---|
3025 |
|
---|
3026 | return true;
|
---|
3027 | }
|
---|
3028 |
|
---|
3029 |
|
---|
3030 | void VspBspTree::ExportNode(BspNode *node, ofstream &stream)
|
---|
3031 | {
|
---|
3032 | if (node->IsLeaf())
|
---|
3033 | {
|
---|
3034 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
3035 | ViewCell *viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell());
|
---|
3036 |
|
---|
3037 | int id = -1;
|
---|
3038 | if (viewCell != mOutOfBoundsCell)
|
---|
3039 | id = viewCell->GetId();
|
---|
3040 |
|
---|
3041 | stream << "<Leaf viewCellId=\"" << id << "\" />" << endl;
|
---|
3042 | }
|
---|
3043 | else
|
---|
3044 | {
|
---|
3045 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3046 |
|
---|
3047 | Plane3 plane = interior->GetPlane();
|
---|
3048 | stream << "<Interior plane=\"" << plane.mNormal.x << " "
|
---|
3049 | << plane.mNormal.y << " " << plane.mNormal.z << " "
|
---|
3050 | << plane.mD << "\">" << endl;
|
---|
3051 |
|
---|
3052 | ExportNode(interior->GetBack(), stream);
|
---|
3053 | ExportNode(interior->GetFront(), stream);
|
---|
3054 |
|
---|
3055 | stream << "</Interior>" << endl;
|
---|
3056 | }
|
---|
3057 | }
|
---|