1 | #include "Plane3.h"
|
---|
2 | #include "VspBspTree.h"
|
---|
3 | #include "Mesh.h"
|
---|
4 | #include "common.h"
|
---|
5 | #include "ViewCell.h"
|
---|
6 | #include "Environment.h"
|
---|
7 | #include "Polygon3.h"
|
---|
8 | #include "Ray.h"
|
---|
9 | #include "AxisAlignedBox3.h"
|
---|
10 | #include <stack>
|
---|
11 | #include <time.h>
|
---|
12 | #include <iomanip>
|
---|
13 | #include "Exporter.h"
|
---|
14 | #include "Plane3.h"
|
---|
15 | #include "ViewCellBsp.h"
|
---|
16 |
|
---|
17 | //-- static members
|
---|
18 | /** Evaluates split plane classification with respect to the plane's
|
---|
19 | contribution for a minimum number of ray splits.
|
---|
20 | */
|
---|
21 | const float VspBspTree::sLeastRaySplitsTable[] = {0, 0, 1, 1, 0};
|
---|
22 | /** Evaluates split plane classification with respect to the plane's
|
---|
23 | contribution for balanced rays.
|
---|
24 | */
|
---|
25 | const float VspBspTree::sBalancedRaysTable[] = {1, -1, 0, 0, 0};
|
---|
26 |
|
---|
27 |
|
---|
28 | int VspBspTree::sFrontId = 0;
|
---|
29 | int VspBspTree::sBackId = 0;
|
---|
30 | int VspBspTree::sFrontAndBackId = 0;
|
---|
31 |
|
---|
32 |
|
---|
33 | /****************************************************************/
|
---|
34 | /* class VspBspTree implementation */
|
---|
35 | /****************************************************************/
|
---|
36 |
|
---|
37 | VspBspTree::VspBspTree():
|
---|
38 | mRoot(NULL),
|
---|
39 | mPvsUseArea(true)
|
---|
40 | {
|
---|
41 | mRootCell = new BspViewCell();
|
---|
42 |
|
---|
43 | Randomize(); // initialise random generator for heuristics
|
---|
44 |
|
---|
45 | //-- termination criteria for autopartition
|
---|
46 | environment->GetIntValue("VspBspTree.Termination.maxDepth", mTermMaxDepth);
|
---|
47 | environment->GetIntValue("VspBspTree.Termination.minPvs", mTermMinPvs);
|
---|
48 | environment->GetIntValue("VspBspTree.Termination.minRays", mTermMinRays);
|
---|
49 | environment->GetFloatValue("VspBspTree.Termination.minArea", mTermMinArea);
|
---|
50 | environment->GetFloatValue("VspBspTree.Termination.maxRayContribution", mTermMaxRayContribution);
|
---|
51 | environment->GetFloatValue("VspBspTree.Termination.minAccRayLenght", mTermMinAccRayLength);
|
---|
52 |
|
---|
53 | //-- factors for bsp tree split plane heuristics
|
---|
54 | environment->GetFloatValue("VspBspTree.Factor.balancedRays", mBalancedRaysFactor);
|
---|
55 | environment->GetFloatValue("VspBspTree.Factor.pvs", mPvsFactor);
|
---|
56 | environment->GetFloatValue("VspBspTree.Termination.ct_div_ci", mCtDivCi);
|
---|
57 |
|
---|
58 | //-- termination criteria for axis aligned split
|
---|
59 | environment->GetFloatValue("VspBspTree.Termination.AxisAligned.ct_div_ci", mAaCtDivCi);
|
---|
60 | environment->GetFloatValue("VspBspTree.Termination.AxisAligned.maxCostRatio", mMaxCostRatio);
|
---|
61 |
|
---|
62 | environment->GetIntValue("VspBspTree.Termination.AxisAligned.minRays",
|
---|
63 | mTermMinRaysForAxisAligned);
|
---|
64 |
|
---|
65 | //-- partition criteria
|
---|
66 | environment->GetIntValue("VspBspTree.maxPolyCandidates", mMaxPolyCandidates);
|
---|
67 | environment->GetIntValue("VspBspTree.maxRayCandidates", mMaxRayCandidates);
|
---|
68 | environment->GetIntValue("VspBspTree.splitPlaneStrategy", mSplitPlaneStrategy);
|
---|
69 | environment->GetFloatValue("VspBspTree.AxisAligned.splitBorder", mSplitBorder);
|
---|
70 |
|
---|
71 | environment->GetFloatValue("VspBspTree.Construction.epsilon", mEpsilon);
|
---|
72 | environment->GetIntValue("VspBspTree.maxTests", mMaxTests);
|
---|
73 |
|
---|
74 | Debug << "BSP max depth: " << mTermMaxDepth << endl;
|
---|
75 | Debug << "BSP min PVS: " << mTermMinPvs << endl;
|
---|
76 | Debug << "BSP min area: " << mTermMinArea << endl;
|
---|
77 | Debug << "BSP min rays: " << mTermMinRays << endl;
|
---|
78 | Debug << "BSP max polygon candidates: " << mMaxPolyCandidates << endl;
|
---|
79 | Debug << "BSP max plane candidates: " << mMaxRayCandidates << endl;
|
---|
80 |
|
---|
81 | Debug << "Split plane strategy: ";
|
---|
82 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
83 | Debug << "random polygon ";
|
---|
84 | if (mSplitPlaneStrategy & AXIS_ALIGNED)
|
---|
85 | Debug << "axis aligned ";
|
---|
86 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
87 | Debug << "least ray splits ";
|
---|
88 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
89 | Debug << "balanced rays ";
|
---|
90 | if (mSplitPlaneStrategy & PVS)
|
---|
91 | Debug << "pvs";
|
---|
92 |
|
---|
93 | Debug << endl;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | const BspTreeStatistics &VspBspTree::GetStatistics() const
|
---|
98 | {
|
---|
99 | return mStat;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | VspBspTree::~VspBspTree()
|
---|
104 | {
|
---|
105 | DEL_PTR(mRoot);
|
---|
106 | DEL_PTR(mRootCell);
|
---|
107 | }
|
---|
108 |
|
---|
109 | int VspBspTree::AddMeshToPolygons(Mesh *mesh,
|
---|
110 | PolygonContainer &polys,
|
---|
111 | MeshInstance *parent)
|
---|
112 | {
|
---|
113 | FaceContainer::const_iterator fi;
|
---|
114 |
|
---|
115 | // copy the face data to polygons
|
---|
116 | for (fi = mesh->mFaces.begin(); fi != mesh->mFaces.end(); ++ fi)
|
---|
117 | {
|
---|
118 | Polygon3 *poly = new Polygon3((*fi), mesh);
|
---|
119 |
|
---|
120 | if (poly->Valid(mEpsilon))
|
---|
121 | {
|
---|
122 | poly->mParent = parent; // set parent intersectable
|
---|
123 | polys.push_back(poly);
|
---|
124 | }
|
---|
125 | else
|
---|
126 | DEL_PTR(poly);
|
---|
127 | }
|
---|
128 | return (int)mesh->mFaces.size();
|
---|
129 | }
|
---|
130 |
|
---|
131 | int VspBspTree::AddToPolygonSoup(const ViewCellContainer &viewCells,
|
---|
132 | PolygonContainer &polys,
|
---|
133 | int maxObjects)
|
---|
134 | {
|
---|
135 | int limit = (maxObjects > 0) ?
|
---|
136 | Min((int)viewCells.size(), maxObjects) : (int)viewCells.size();
|
---|
137 |
|
---|
138 | int polysSize = 0;
|
---|
139 |
|
---|
140 | for (int i = 0; i < limit; ++ i)
|
---|
141 | {
|
---|
142 | if (viewCells[i]->GetMesh()) // copy the mesh data to polygons
|
---|
143 | {
|
---|
144 | mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb
|
---|
145 | polysSize +=
|
---|
146 | AddMeshToPolygons(viewCells[i]->GetMesh(),
|
---|
147 | polys,
|
---|
148 | viewCells[i]);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | return polysSize;
|
---|
153 | }
|
---|
154 |
|
---|
155 | int VspBspTree::AddToPolygonSoup(const ObjectContainer &objects,
|
---|
156 | PolygonContainer &polys,
|
---|
157 | int maxObjects)
|
---|
158 | {
|
---|
159 | int limit = (maxObjects > 0) ?
|
---|
160 | Min((int)objects.size(), maxObjects) : (int)objects.size();
|
---|
161 |
|
---|
162 | for (int i = 0; i < limit; ++i)
|
---|
163 | {
|
---|
164 | Intersectable *object = objects[i];//*it;
|
---|
165 | Mesh *mesh = NULL;
|
---|
166 |
|
---|
167 | switch (object->Type()) // extract the meshes
|
---|
168 | {
|
---|
169 | case Intersectable::MESH_INSTANCE:
|
---|
170 | mesh = dynamic_cast<MeshInstance *>(object)->GetMesh();
|
---|
171 | break;
|
---|
172 | case Intersectable::VIEW_CELL:
|
---|
173 | mesh = dynamic_cast<ViewCell *>(object)->GetMesh();
|
---|
174 | break;
|
---|
175 | // TODO: handle transformed mesh instances
|
---|
176 | default:
|
---|
177 | Debug << "intersectable type not supported" << endl;
|
---|
178 | break;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (mesh) // copy the mesh data to polygons
|
---|
182 | {
|
---|
183 | mBox.Include(object->GetBox()); // add to BSP tree aabb
|
---|
184 | AddMeshToPolygons(mesh, polys, mRootCell);
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | return (int)polys.size();
|
---|
189 | }
|
---|
190 |
|
---|
191 | void VspBspTree::Construct(const VssRayContainer &sampleRays)
|
---|
192 | {
|
---|
193 | mStat.nodes = 1;
|
---|
194 | mBox.Initialize(); // initialise BSP tree bounding box
|
---|
195 |
|
---|
196 | PolygonContainer polys;
|
---|
197 | RayInfoContainer *rays = new RayInfoContainer();
|
---|
198 |
|
---|
199 | VssRayContainer::const_iterator rit, rit_end = sampleRays.end();
|
---|
200 |
|
---|
201 | long startTime = GetTime();
|
---|
202 |
|
---|
203 | Debug << "**** Extracting polygons from rays ****\n";
|
---|
204 |
|
---|
205 | Intersectable::NewMail();
|
---|
206 |
|
---|
207 | //-- extract polygons intersected by the rays
|
---|
208 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
209 | {
|
---|
210 | VssRay *ray = *rit;
|
---|
211 |
|
---|
212 | if (ray->mTerminationObject && !ray->mTerminationObject->Mailed())
|
---|
213 | {
|
---|
214 | ray->mTerminationObject->Mail();
|
---|
215 | MeshInstance *obj = dynamic_cast<MeshInstance *>(ray->mTerminationObject);
|
---|
216 | AddMeshToPolygons(obj->GetMesh(), polys, obj);
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (ray->mOriginObject && !ray->mOriginObject->Mailed())
|
---|
220 | {
|
---|
221 | ray->mOriginObject->Mail();
|
---|
222 | MeshInstance *obj = dynamic_cast<MeshInstance *>(ray->mOriginObject);
|
---|
223 | AddMeshToPolygons(obj->GetMesh(), polys, obj);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | // compute bounding box
|
---|
228 | Polygon3::IncludeInBox(polys, mBox);
|
---|
229 |
|
---|
230 | //-- store rays
|
---|
231 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
232 | {
|
---|
233 | VssRay *ray = *rit;
|
---|
234 |
|
---|
235 | float minT, maxT;
|
---|
236 |
|
---|
237 | // TODO: not very efficient to implictly cast between rays types ...
|
---|
238 | if (mBox.GetRaySegment(*ray, minT, maxT))
|
---|
239 | {
|
---|
240 | float len = ray->Length();
|
---|
241 |
|
---|
242 | if (!len)
|
---|
243 | len = Limits::Small;
|
---|
244 |
|
---|
245 | rays->push_back(RayInfo(ray, minT / len, maxT / len));
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | mStat.polys = (int)polys.size();
|
---|
250 |
|
---|
251 | Debug << "**** Finished polygon extraction ****" << endl;
|
---|
252 | Debug << (int)polys.size() << " polys extracted from " << (int)sampleRays.size() << " rays" << endl;
|
---|
253 | Debug << "extraction time: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
254 |
|
---|
255 | Construct(polys, rays);
|
---|
256 |
|
---|
257 | // clean up polygons
|
---|
258 | CLEAR_CONTAINER(polys);
|
---|
259 | }
|
---|
260 |
|
---|
261 | void VspBspTree::Construct(const PolygonContainer &polys, RayInfoContainer *rays)
|
---|
262 | {
|
---|
263 | std::stack<VspBspTraversalData> tStack;
|
---|
264 |
|
---|
265 | mRoot = new BspLeaf();
|
---|
266 |
|
---|
267 | // constrruct root node geometry
|
---|
268 | BspNodeGeometry *geom = new BspNodeGeometry();
|
---|
269 | ConstructGeometry(mRoot, *geom);
|
---|
270 |
|
---|
271 | VspBspTraversalData tData(mRoot,
|
---|
272 | new PolygonContainer(polys),
|
---|
273 | 0,
|
---|
274 | rays,
|
---|
275 | ComputePvsSize(*rays),
|
---|
276 | geom->GetArea(),
|
---|
277 | geom);
|
---|
278 |
|
---|
279 | tStack.push(tData);
|
---|
280 |
|
---|
281 | mStat.Start();
|
---|
282 | cout << "Contructing vsp bsp tree ... ";
|
---|
283 |
|
---|
284 | long startTime = GetTime();
|
---|
285 |
|
---|
286 | while (!tStack.empty())
|
---|
287 | {
|
---|
288 | tData = tStack.top();
|
---|
289 |
|
---|
290 | tStack.pop();
|
---|
291 |
|
---|
292 | // subdivide leaf node
|
---|
293 | BspNode *r = Subdivide(tStack, tData);
|
---|
294 |
|
---|
295 | if (r == mRoot)
|
---|
296 | Debug << "BSP tree construction time spent at root: "
|
---|
297 | << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
298 | }
|
---|
299 |
|
---|
300 | cout << "finished\n";
|
---|
301 |
|
---|
302 | mStat.Stop();
|
---|
303 | }
|
---|
304 |
|
---|
305 | bool VspBspTree::TerminationCriteriaMet(const VspBspTraversalData &data) const
|
---|
306 | {
|
---|
307 | return
|
---|
308 | (((int)data.mRays->size() <= mTermMinRays) ||
|
---|
309 | (data.mPvs <= mTermMinPvs) ||
|
---|
310 | (data.mArea <= mTermMinArea) ||
|
---|
311 | // (data.GetAvgRayContribution() >= mTermMaxRayContribution) ||
|
---|
312 | (data.mDepth >= mTermMaxDepth));
|
---|
313 | }
|
---|
314 |
|
---|
315 | BspNode *VspBspTree::Subdivide(VspBspTraversalStack &tStack,
|
---|
316 | VspBspTraversalData &tData)
|
---|
317 | {
|
---|
318 | //-- terminate traversal
|
---|
319 | if (TerminationCriteriaMet(tData))
|
---|
320 | {
|
---|
321 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
|
---|
322 |
|
---|
323 | BspViewCell *viewCell = new BspViewCell();
|
---|
324 |
|
---|
325 | leaf->SetViewCell(viewCell);
|
---|
326 | viewCell->mLeaves.push_back(leaf);
|
---|
327 |
|
---|
328 | //-- add pvs
|
---|
329 | if (viewCell != mRootCell)
|
---|
330 | {
|
---|
331 | int conSamp = 0, sampCon = 0;
|
---|
332 | AddToPvs(leaf, *tData.mRays, conSamp, sampCon);
|
---|
333 |
|
---|
334 | mStat.contributingSamples += conSamp;
|
---|
335 | mStat.sampleContributions += sampCon;
|
---|
336 | }
|
---|
337 |
|
---|
338 | EvaluateLeafStats(tData);
|
---|
339 |
|
---|
340 | //-- clean up
|
---|
341 |
|
---|
342 | DEL_PTR(tData.mPolygons);
|
---|
343 | DEL_PTR(tData.mRays);
|
---|
344 | DEL_PTR(tData.mGeometry);
|
---|
345 |
|
---|
346 | return leaf;
|
---|
347 | }
|
---|
348 |
|
---|
349 | //-- continue subdivision
|
---|
350 | PolygonContainer coincident;
|
---|
351 |
|
---|
352 | VspBspTraversalData tFrontData(new PolygonContainer(),
|
---|
353 | tData.mDepth + 1,
|
---|
354 | new RayInfoContainer(),
|
---|
355 | new BspNodeGeometry());
|
---|
356 |
|
---|
357 | VspBspTraversalData tBackData(new PolygonContainer(),
|
---|
358 | tData.mDepth + 1,
|
---|
359 | new RayInfoContainer(),
|
---|
360 | new BspNodeGeometry());
|
---|
361 |
|
---|
362 | // create new interior node and two leaf nodes
|
---|
363 | BspInterior *interior =
|
---|
364 | SubdivideNode(tData, tFrontData, tBackData, coincident);
|
---|
365 |
|
---|
366 | #ifdef _DEBUG
|
---|
367 | // if (frontPolys->empty() && backPolys->empty() && (coincident.size() > 2))
|
---|
368 | // { for (PolygonContainer::iterator it = coincident.begin(); it != coincident.end(); ++it)
|
---|
369 | // Debug << (*it) << " " << (*it)->GetArea() << " " << (*it)->mParent << endl ;
|
---|
370 | // Debug << endl;}
|
---|
371 | #endif
|
---|
372 |
|
---|
373 | // push the children on the stack
|
---|
374 | tStack.push(tFrontData);
|
---|
375 | tStack.push(tBackData);
|
---|
376 |
|
---|
377 | // cleanup
|
---|
378 | DEL_PTR(tData.mNode);
|
---|
379 |
|
---|
380 | DEL_PTR(tData.mPolygons);
|
---|
381 | DEL_PTR(tData.mRays);
|
---|
382 | DEL_PTR(tData.mGeometry);
|
---|
383 |
|
---|
384 | return interior;
|
---|
385 | }
|
---|
386 |
|
---|
387 | BspInterior *VspBspTree::SubdivideNode(VspBspTraversalData &tData,
|
---|
388 | VspBspTraversalData &frontData,
|
---|
389 | VspBspTraversalData &backData,
|
---|
390 | PolygonContainer &coincident)
|
---|
391 | {
|
---|
392 | mStat.nodes += 2;
|
---|
393 |
|
---|
394 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
|
---|
395 |
|
---|
396 | // select subdivision plane
|
---|
397 | BspInterior *interior = new BspInterior(SelectPlane(leaf, tData));
|
---|
398 |
|
---|
399 | #ifdef _DEBUG
|
---|
400 | Debug << interior << endl;
|
---|
401 | #endif
|
---|
402 |
|
---|
403 | // subdivide rays into front and back rays
|
---|
404 | SplitRays(interior->GetPlane(),
|
---|
405 | *tData.mRays,
|
---|
406 | *frontData.mRays,
|
---|
407 | *backData.mRays);
|
---|
408 |
|
---|
409 | // subdivide polygons with plane
|
---|
410 | mStat.splits += SplitPolygons(interior->GetPlane(),
|
---|
411 | *tData.mPolygons,
|
---|
412 | *frontData.mPolygons,
|
---|
413 | *backData.mPolygons,
|
---|
414 | coincident);
|
---|
415 |
|
---|
416 | // compute pvs
|
---|
417 | frontData.mPvs = ComputePvsSize(*frontData.mRays);
|
---|
418 | backData.mPvs = ComputePvsSize(*backData.mRays);
|
---|
419 |
|
---|
420 | // split geometry and compute area
|
---|
421 | if (1)
|
---|
422 | {
|
---|
423 | tData.mGeometry->SplitGeometry(*frontData.mGeometry,
|
---|
424 | *backData.mGeometry,
|
---|
425 | interior->GetPlane(),
|
---|
426 | mBox,
|
---|
427 | mEpsilon);
|
---|
428 |
|
---|
429 |
|
---|
430 | frontData.mArea = frontData.mGeometry->GetArea();
|
---|
431 | backData.mArea = backData.mGeometry->GetArea();
|
---|
432 | }
|
---|
433 |
|
---|
434 | // compute accumulated ray length
|
---|
435 | //frontData.mAccRayLength = AccumulatedRayLength(*frontData.mRays);
|
---|
436 | //backData.mAccRayLength = AccumulatedRayLength(*backData.mRays);
|
---|
437 |
|
---|
438 | //-- create front and back leaf
|
---|
439 |
|
---|
440 | BspInterior *parent = leaf->GetParent();
|
---|
441 |
|
---|
442 | // replace a link from node's parent
|
---|
443 | if (!leaf->IsRoot())
|
---|
444 | {
|
---|
445 | parent->ReplaceChildLink(leaf, interior);
|
---|
446 | interior->SetParent(parent);
|
---|
447 | }
|
---|
448 | else // new root
|
---|
449 | {
|
---|
450 | mRoot = interior;
|
---|
451 | }
|
---|
452 |
|
---|
453 | // and setup child links
|
---|
454 | interior->SetupChildLinks(new BspLeaf(interior), new BspLeaf(interior));
|
---|
455 |
|
---|
456 | frontData.mNode = interior->GetFront();
|
---|
457 | backData.mNode = interior->GetBack();
|
---|
458 |
|
---|
459 | //DEL_PTR(leaf);
|
---|
460 | return interior;
|
---|
461 | }
|
---|
462 |
|
---|
463 | void VspBspTree::AddToPvs(BspLeaf *leaf,
|
---|
464 | const RayInfoContainer &rays,
|
---|
465 | int &sampleContributions,
|
---|
466 | int &contributingSamples)
|
---|
467 | {
|
---|
468 | sampleContributions = 0;
|
---|
469 | contributingSamples = 0;
|
---|
470 |
|
---|
471 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
472 |
|
---|
473 | ViewCell *vc = leaf->GetViewCell();
|
---|
474 |
|
---|
475 | // add contributions from samples to the PVS
|
---|
476 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
477 | {
|
---|
478 | int contribution = 0;
|
---|
479 | VssRay *ray = (*it).mRay;
|
---|
480 |
|
---|
481 | if (ray->mTerminationObject)
|
---|
482 | contribution += vc->GetPvs().AddSample(ray->mTerminationObject);
|
---|
483 |
|
---|
484 | if (ray->mOriginObject)
|
---|
485 | contribution += vc->GetPvs().AddSample(ray->mOriginObject);
|
---|
486 |
|
---|
487 | if (contribution)
|
---|
488 | {
|
---|
489 | sampleContributions += contribution;
|
---|
490 | ++ contributingSamples;
|
---|
491 | }
|
---|
492 | //leaf->mVssRays.push_back(ray);
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | void VspBspTree::SortSplitCandidates(const PolygonContainer &polys,
|
---|
497 | const int axis,
|
---|
498 | vector<SortableEntry> &splitCandidates) const
|
---|
499 | {
|
---|
500 | splitCandidates.clear();
|
---|
501 |
|
---|
502 | int requestedSize = 2 * (int)polys.size();
|
---|
503 | // creates a sorted split candidates array
|
---|
504 | splitCandidates.reserve(requestedSize);
|
---|
505 |
|
---|
506 | PolygonContainer::const_iterator it, it_end = polys.end();
|
---|
507 |
|
---|
508 | AxisAlignedBox3 box;
|
---|
509 |
|
---|
510 | // insert all queries
|
---|
511 | for(it = polys.begin(); it != it_end; ++ it)
|
---|
512 | {
|
---|
513 | box.Initialize();
|
---|
514 | box.Include(*(*it));
|
---|
515 |
|
---|
516 | splitCandidates.push_back(SortableEntry(SortableEntry::POLY_MIN, box.Min(axis), *it));
|
---|
517 | splitCandidates.push_back(SortableEntry(SortableEntry::POLY_MAX, box.Max(axis), *it));
|
---|
518 | }
|
---|
519 |
|
---|
520 | stable_sort(splitCandidates.begin(), splitCandidates.end());
|
---|
521 | }
|
---|
522 |
|
---|
523 |
|
---|
524 | float VspBspTree::BestCostRatio(const PolygonContainer &polys,
|
---|
525 | const AxisAlignedBox3 &box,
|
---|
526 | const int axis,
|
---|
527 | float &position,
|
---|
528 | int &objectsBack,
|
---|
529 | int &objectsFront) const
|
---|
530 | {
|
---|
531 | vector<SortableEntry> splitCandidates;
|
---|
532 |
|
---|
533 | SortSplitCandidates(polys, axis, splitCandidates);
|
---|
534 |
|
---|
535 | // go through the lists, count the number of objects left and right
|
---|
536 | // and evaluate the following cost funcion:
|
---|
537 | // C = ct_div_ci + (ol + or)/queries
|
---|
538 |
|
---|
539 | int objectsLeft = 0, objectsRight = (int)polys.size();
|
---|
540 |
|
---|
541 | float minBox = box.Min(axis);
|
---|
542 | float maxBox = box.Max(axis);
|
---|
543 | float boxArea = box.SurfaceArea();
|
---|
544 |
|
---|
545 | float minBand = minBox + mSplitBorder * (maxBox - minBox);
|
---|
546 | float maxBand = minBox + (1.0f - mSplitBorder) * (maxBox - minBox);
|
---|
547 |
|
---|
548 | float minSum = 1e20f;
|
---|
549 | vector<SortableEntry>::const_iterator ci, ci_end = splitCandidates.end();
|
---|
550 |
|
---|
551 | for(ci = splitCandidates.begin(); ci != ci_end; ++ ci)
|
---|
552 | {
|
---|
553 | switch ((*ci).type)
|
---|
554 | {
|
---|
555 | case SortableEntry::POLY_MIN:
|
---|
556 | ++ objectsLeft;
|
---|
557 | break;
|
---|
558 | case SortableEntry::POLY_MAX:
|
---|
559 | -- objectsRight;
|
---|
560 | break;
|
---|
561 | default:
|
---|
562 | break;
|
---|
563 | }
|
---|
564 |
|
---|
565 | if ((*ci).value > minBand && (*ci).value < maxBand)
|
---|
566 | {
|
---|
567 | AxisAlignedBox3 lbox = box;
|
---|
568 | AxisAlignedBox3 rbox = box;
|
---|
569 | lbox.SetMax(axis, (*ci).value);
|
---|
570 | rbox.SetMin(axis, (*ci).value);
|
---|
571 |
|
---|
572 | float sum = objectsLeft * lbox.SurfaceArea() +
|
---|
573 | objectsRight * rbox.SurfaceArea();
|
---|
574 |
|
---|
575 | if (sum < minSum)
|
---|
576 | {
|
---|
577 | minSum = sum;
|
---|
578 | position = (*ci).value;
|
---|
579 |
|
---|
580 | objectsBack = objectsLeft;
|
---|
581 | objectsFront = objectsRight;
|
---|
582 | }
|
---|
583 | }
|
---|
584 | }
|
---|
585 |
|
---|
586 | float oldCost = (float)polys.size();
|
---|
587 | float newCost = mAaCtDivCi + minSum / boxArea;
|
---|
588 | float ratio = newCost / oldCost;
|
---|
589 |
|
---|
590 |
|
---|
591 | #if 0
|
---|
592 | Debug << "====================" << endl;
|
---|
593 | Debug << "costRatio=" << ratio << " pos=" << position<<" t=" << (position - minBox)/(maxBox - minBox)
|
---|
594 | << "\t o=(" << objectsBack << "," << objectsFront << ")" << endl;
|
---|
595 | #endif
|
---|
596 | return ratio;
|
---|
597 | }
|
---|
598 |
|
---|
599 | bool VspBspTree::SelectAxisAlignedPlane(Plane3 &plane,
|
---|
600 | const PolygonContainer &polys) const
|
---|
601 | {
|
---|
602 | AxisAlignedBox3 box;
|
---|
603 | box.Initialize();
|
---|
604 |
|
---|
605 | // create bounding box of region
|
---|
606 | Polygon3::IncludeInBox(polys, box);
|
---|
607 |
|
---|
608 | int objectsBack = 0, objectsFront = 0;
|
---|
609 | int axis = 0;
|
---|
610 | float costRatio = MAX_FLOAT;
|
---|
611 | Vector3 position;
|
---|
612 |
|
---|
613 | //-- area subdivision
|
---|
614 | for (int i = 0; i < 3; ++ i)
|
---|
615 | {
|
---|
616 | float p = 0;
|
---|
617 | float r = BestCostRatio(polys, box, i, p, objectsBack, objectsFront);
|
---|
618 |
|
---|
619 | if (r < costRatio)
|
---|
620 | {
|
---|
621 | costRatio = r;
|
---|
622 | axis = i;
|
---|
623 | position = p;
|
---|
624 | }
|
---|
625 | }
|
---|
626 |
|
---|
627 | if (costRatio >= mMaxCostRatio)
|
---|
628 | return false;
|
---|
629 |
|
---|
630 | Vector3 norm(0,0,0); norm[axis] = 1.0f;
|
---|
631 | plane = Plane3(norm, position);
|
---|
632 |
|
---|
633 | return true;
|
---|
634 | }
|
---|
635 |
|
---|
636 | Plane3 VspBspTree::SelectPlane(BspLeaf *leaf, VspBspTraversalData &data)
|
---|
637 | {
|
---|
638 | if ((mSplitPlaneStrategy & AXIS_ALIGNED) &&
|
---|
639 | ((int)data.mRays->size() > mTermMinRaysForAxisAligned))
|
---|
640 | {
|
---|
641 | Plane3 plane;
|
---|
642 | if (SelectAxisAlignedPlane(plane, *data.mPolygons)) // TODO: should be rays
|
---|
643 | return plane;
|
---|
644 | }
|
---|
645 |
|
---|
646 | // simplest strategy: just take next polygon
|
---|
647 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
648 | {
|
---|
649 | if (!data.mPolygons->empty())
|
---|
650 | {
|
---|
651 | const int randIdx = Random((int)data.mPolygons->size());
|
---|
652 | Polygon3 *nextPoly = (*data.mPolygons)[randIdx];
|
---|
653 | return nextPoly->GetSupportingPlane();
|
---|
654 | }
|
---|
655 | else
|
---|
656 | {
|
---|
657 | //-- choose plane on midpoint of a ray
|
---|
658 | const int candidateIdx = Random((int)data.mRays->size());
|
---|
659 |
|
---|
660 | const Vector3 minPt = (*data.mRays)[candidateIdx].ExtrapOrigin();
|
---|
661 | const Vector3 maxPt = (*data.mRays)[candidateIdx].ExtrapTermination();
|
---|
662 |
|
---|
663 | const Vector3 pt = (maxPt + minPt) * 0.5;
|
---|
664 |
|
---|
665 | const Vector3 normal = (*data.mRays)[candidateIdx].mRay->GetDir();
|
---|
666 |
|
---|
667 | return Plane3(normal, pt);
|
---|
668 | }
|
---|
669 |
|
---|
670 | return Plane3();
|
---|
671 | }
|
---|
672 |
|
---|
673 | // use heuristics to find appropriate plane
|
---|
674 | return SelectPlaneHeuristics(leaf, data);
|
---|
675 | }
|
---|
676 |
|
---|
677 | Plane3 VspBspTree::SelectPlaneHeuristics(BspLeaf *leaf, VspBspTraversalData &data)
|
---|
678 | {
|
---|
679 | float lowestCost = MAX_FLOAT;
|
---|
680 | Plane3 bestPlane;
|
---|
681 | Plane3 plane;
|
---|
682 |
|
---|
683 | int limit = Min((int)data.mPolygons->size(), mMaxPolyCandidates);
|
---|
684 |
|
---|
685 | int candidateIdx = limit;
|
---|
686 |
|
---|
687 | for (int i = 0; i < limit; ++ i)
|
---|
688 | {
|
---|
689 | candidateIdx = GetNextCandidateIdx(candidateIdx, *data.mPolygons);
|
---|
690 |
|
---|
691 | Polygon3 *poly = (*data.mPolygons)[candidateIdx];
|
---|
692 |
|
---|
693 | // evaluate current candidate
|
---|
694 | const float candidateCost =
|
---|
695 | SplitPlaneCost(poly->GetSupportingPlane(), data);
|
---|
696 |
|
---|
697 | if (candidateCost < lowestCost)
|
---|
698 | {
|
---|
699 | bestPlane = poly->GetSupportingPlane();
|
---|
700 | lowestCost = candidateCost;
|
---|
701 | }
|
---|
702 | }
|
---|
703 |
|
---|
704 | //Debug << "lowest: " << lowestCost << endl;
|
---|
705 |
|
---|
706 | //-- choose candidate planes extracted from rays
|
---|
707 | // we currently use different two methods chosen with
|
---|
708 | // equal probability
|
---|
709 |
|
---|
710 | // take 3 ray endpoints, where two are minimum and one a maximum
|
---|
711 | // point or the other way round
|
---|
712 | for (int i = 0; i < mMaxRayCandidates / 2; ++ i)
|
---|
713 | {
|
---|
714 | candidateIdx = Random((int)data.mRays->size());
|
---|
715 |
|
---|
716 | RayInfo rayInf = (*data.mRays)[candidateIdx];
|
---|
717 |
|
---|
718 | const Vector3 minPt = rayInf.ExtrapOrigin();
|
---|
719 | const Vector3 maxPt = rayInf.ExtrapTermination();
|
---|
720 |
|
---|
721 | const Vector3 pt = (maxPt + minPt) * 0.5;
|
---|
722 | const Vector3 normal = Normalize(rayInf.mRay->GetDir());
|
---|
723 |
|
---|
724 | plane = Plane3(normal, pt);
|
---|
725 |
|
---|
726 | const float candidateCost = SplitPlaneCost(plane, data);
|
---|
727 |
|
---|
728 | if (candidateCost < lowestCost)
|
---|
729 | {
|
---|
730 | bestPlane = plane;
|
---|
731 | lowestCost = candidateCost;
|
---|
732 | }
|
---|
733 | }
|
---|
734 |
|
---|
735 | // take plane normal as plane normal and the midpoint of the ray.
|
---|
736 | // PROBLEM: does not resemble any point where visibility is likely to change
|
---|
737 | //Debug << "lowest2: " << lowestCost << endl;
|
---|
738 | for (int i = 0; i < mMaxRayCandidates / 2; ++ i)
|
---|
739 | {
|
---|
740 | Vector3 pt[3];
|
---|
741 | int idx[3];
|
---|
742 | int cmaxT = 0;
|
---|
743 | int cminT = 0;
|
---|
744 | bool chooseMin = false;
|
---|
745 |
|
---|
746 | for (int j = 0; j < 3; j ++)
|
---|
747 | {
|
---|
748 | idx[j] = Random((int)data.mRays->size() * 2);
|
---|
749 |
|
---|
750 | if (idx[j] >= (int)data.mRays->size())
|
---|
751 | {
|
---|
752 | idx[j] -= (int)data.mRays->size();
|
---|
753 |
|
---|
754 | chooseMin = (cminT < 2);
|
---|
755 | }
|
---|
756 | else
|
---|
757 | chooseMin = (cmaxT < 2);
|
---|
758 |
|
---|
759 | RayInfo rayInf = (*data.mRays)[idx[j]];
|
---|
760 | pt[j] = chooseMin ? rayInf.ExtrapOrigin() : rayInf.ExtrapTermination();
|
---|
761 | }
|
---|
762 |
|
---|
763 | plane = Plane3(pt[0], pt[1], pt[2]);
|
---|
764 |
|
---|
765 | const float candidateCost = SplitPlaneCost(plane, data);
|
---|
766 |
|
---|
767 | if (candidateCost < lowestCost)
|
---|
768 | {
|
---|
769 | //Debug << "choose ray plane 2: " << candidateCost << endl;
|
---|
770 | bestPlane = plane;
|
---|
771 |
|
---|
772 | lowestCost = candidateCost;
|
---|
773 | }
|
---|
774 | }
|
---|
775 |
|
---|
776 | #ifdef _DEBUG
|
---|
777 | Debug << "plane lowest cost: " << lowestCost << endl;
|
---|
778 | #endif
|
---|
779 | return bestPlane;
|
---|
780 | }
|
---|
781 |
|
---|
782 | int VspBspTree::GetNextCandidateIdx(int currentIdx, PolygonContainer &polys)
|
---|
783 | {
|
---|
784 | const int candidateIdx = Random(currentIdx --);
|
---|
785 |
|
---|
786 | // swap candidates to avoid testing same plane 2 times
|
---|
787 | std::swap(polys[currentIdx], polys[candidateIdx]);
|
---|
788 |
|
---|
789 | return currentIdx;
|
---|
790 | //return Random((int)polys.size());
|
---|
791 | }
|
---|
792 |
|
---|
793 |
|
---|
794 | inline void VspBspTree::GenerateUniqueIdsForPvs()
|
---|
795 | {
|
---|
796 | Intersectable::NewMail(); sBackId = ViewCell::sMailId;
|
---|
797 | Intersectable::NewMail(); sFrontId = ViewCell::sMailId;
|
---|
798 | Intersectable::NewMail(); sFrontAndBackId = ViewCell::sMailId;
|
---|
799 | }
|
---|
800 |
|
---|
801 | float VspBspTree::SplitPlaneCost(const Plane3 &candidatePlane,
|
---|
802 | const VspBspTraversalData &data)
|
---|
803 | {
|
---|
804 | float val = 0;
|
---|
805 |
|
---|
806 | float sumBalancedRays = 0;
|
---|
807 | float sumRaySplits = 0;
|
---|
808 |
|
---|
809 | int frontPvs = 0;
|
---|
810 | int backPvs = 0;
|
---|
811 |
|
---|
812 | // probability that view point lies in child
|
---|
813 | float pOverall = 0;
|
---|
814 | float pFront = 0;
|
---|
815 | float pBack = 0;
|
---|
816 |
|
---|
817 | const bool pvsUseLen = false;
|
---|
818 |
|
---|
819 | if (mSplitPlaneStrategy & PVS)
|
---|
820 | {
|
---|
821 | // create unique ids for pvs heuristics
|
---|
822 | GenerateUniqueIdsForPvs();
|
---|
823 |
|
---|
824 | if (mPvsUseArea) // use front and back cell areas to approximate volume
|
---|
825 | {
|
---|
826 | // construct child geometry with regard to the candidate split plane
|
---|
827 | BspNodeGeometry frontCell;
|
---|
828 | BspNodeGeometry backCell;
|
---|
829 |
|
---|
830 | data.mGeometry->SplitGeometry(frontCell,
|
---|
831 | backCell,
|
---|
832 | candidatePlane,
|
---|
833 | mBox,
|
---|
834 | mEpsilon);
|
---|
835 |
|
---|
836 | pFront = frontCell.GetArea();
|
---|
837 | pBack = backCell.GetArea();
|
---|
838 |
|
---|
839 | pOverall = data.mArea;
|
---|
840 | }
|
---|
841 | }
|
---|
842 |
|
---|
843 | int limit;
|
---|
844 | bool useRand;
|
---|
845 |
|
---|
846 | // choose test polyongs randomly if over threshold
|
---|
847 | if ((int)data.mRays->size() > mMaxTests)
|
---|
848 | {
|
---|
849 | useRand = true;
|
---|
850 | limit = mMaxTests;
|
---|
851 | }
|
---|
852 | else
|
---|
853 | {
|
---|
854 | useRand = false;
|
---|
855 | limit = (int)data.mRays->size();
|
---|
856 | }
|
---|
857 |
|
---|
858 | for (int i = 0; i < limit; ++ i)
|
---|
859 | {
|
---|
860 | const int testIdx = useRand ? Random(limit) : i;
|
---|
861 | RayInfo rayInf = (*data.mRays)[testIdx];
|
---|
862 |
|
---|
863 | VssRay *ray = rayInf.mRay;
|
---|
864 | float t;
|
---|
865 | const int cf = rayInf.ComputeRayIntersection(candidatePlane, t);
|
---|
866 |
|
---|
867 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
868 | {
|
---|
869 | sumBalancedRays += cf;
|
---|
870 | }
|
---|
871 |
|
---|
872 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
873 | {
|
---|
874 | if (cf == 0)
|
---|
875 | ++ sumRaySplits;
|
---|
876 | }
|
---|
877 |
|
---|
878 | if (mSplitPlaneStrategy & PVS)
|
---|
879 | {
|
---|
880 | // in case the ray intersects an object
|
---|
881 | // assure that we only count the object
|
---|
882 | // once for the front and once for the back side of the plane
|
---|
883 |
|
---|
884 | // add the termination object
|
---|
885 | AddObjToPvs(ray->mTerminationObject, cf, frontPvs, backPvs);
|
---|
886 |
|
---|
887 | // add the source object
|
---|
888 | AddObjToPvs(ray->mOriginObject, cf, frontPvs, backPvs);
|
---|
889 |
|
---|
890 | // use number or length of rays to approximate volume
|
---|
891 | if (!mPvsUseArea)
|
---|
892 | {
|
---|
893 | float len = 1;
|
---|
894 |
|
---|
895 | if (pvsUseLen) // use length of rays
|
---|
896 | len = rayInf.SqrSegmentLength();
|
---|
897 |
|
---|
898 | pOverall += len;
|
---|
899 |
|
---|
900 | if (cf == 1)
|
---|
901 | pFront += len;
|
---|
902 | if (cf == -1)
|
---|
903 | pBack += len;
|
---|
904 | if (cf == 0)
|
---|
905 | {
|
---|
906 | // use length of rays to approximate volume
|
---|
907 | if (pvsUseLen)
|
---|
908 | {
|
---|
909 | float newLen = len *
|
---|
910 | (rayInf.GetMaxT() - t) /
|
---|
911 | (rayInf.GetMaxT() - rayInf.GetMinT());
|
---|
912 |
|
---|
913 | if (candidatePlane.Side(rayInf.ExtrapOrigin()) <= 0)
|
---|
914 | {
|
---|
915 | pBack += newLen;
|
---|
916 | pFront += len - newLen;
|
---|
917 | }
|
---|
918 | else
|
---|
919 | {
|
---|
920 | pFront += newLen;
|
---|
921 | pBack += len - newLen;
|
---|
922 | }
|
---|
923 | }
|
---|
924 | else
|
---|
925 | {
|
---|
926 | ++ pFront;
|
---|
927 | ++ pBack;
|
---|
928 | }
|
---|
929 | }
|
---|
930 | }
|
---|
931 | }
|
---|
932 | }
|
---|
933 |
|
---|
934 | const float raysSize = (float)data.mRays->size() + Limits::Small;
|
---|
935 |
|
---|
936 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
937 | val += mLeastRaySplitsFactor * sumRaySplits / raysSize;
|
---|
938 |
|
---|
939 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
940 | val += mBalancedRaysFactor * fabs(sumBalancedRays) / raysSize;
|
---|
941 |
|
---|
942 | const float denom = pOverall * (float)data.mPvs * 2.0f + Limits::Small;
|
---|
943 |
|
---|
944 | if (mSplitPlaneStrategy & PVS)
|
---|
945 | {
|
---|
946 | val += mPvsFactor * (frontPvs * pFront + (backPvs * pBack)) / denom;
|
---|
947 |
|
---|
948 | // give penalty to unbalanced split
|
---|
949 | if (0)
|
---|
950 | if (((pFront * 0.2 + Limits::Small) > pBack) ||
|
---|
951 | (pFront < (pBack * 0.2 + Limits::Small)))
|
---|
952 | val += 0.5;
|
---|
953 | }
|
---|
954 |
|
---|
955 | #ifdef _DEBUG
|
---|
956 | // Debug << "totalpvs: " << pvs << " ptotal: " << pOverall
|
---|
957 | // << " frontpvs: " << frontPvs << " pFront: " << pFront
|
---|
958 | // << " backpvs: " << backPvs << " pBack: " << pBack << endl << endl;
|
---|
959 | #endif
|
---|
960 | return val;
|
---|
961 | }
|
---|
962 |
|
---|
963 | void VspBspTree::AddObjToPvs(Intersectable *obj,
|
---|
964 | const int cf,
|
---|
965 | int &frontPvs,
|
---|
966 | int &backPvs) const
|
---|
967 | {
|
---|
968 | if (!obj)
|
---|
969 | return;
|
---|
970 | // TODO: does this really belong to no pvs?
|
---|
971 | //if (cf == Ray::COINCIDENT) return;
|
---|
972 |
|
---|
973 | // object belongs to both PVS
|
---|
974 | if (cf >= 0)
|
---|
975 | {
|
---|
976 | if ((obj->mMailbox != sFrontId) &&
|
---|
977 | (obj->mMailbox != sFrontAndBackId))
|
---|
978 | {
|
---|
979 | ++ frontPvs;
|
---|
980 |
|
---|
981 | if (obj->mMailbox == sBackId)
|
---|
982 | obj->mMailbox = sFrontAndBackId;
|
---|
983 | else
|
---|
984 | obj->mMailbox = sFrontId;
|
---|
985 | }
|
---|
986 | }
|
---|
987 |
|
---|
988 | if (cf <= 0)
|
---|
989 | {
|
---|
990 | if ((obj->mMailbox != sBackId) &&
|
---|
991 | (obj->mMailbox != sFrontAndBackId))
|
---|
992 | {
|
---|
993 | ++ backPvs;
|
---|
994 |
|
---|
995 | if (obj->mMailbox == sFrontId)
|
---|
996 | obj->mMailbox = sFrontAndBackId;
|
---|
997 | else
|
---|
998 | obj->mMailbox = sBackId;
|
---|
999 | }
|
---|
1000 | }
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 | void VspBspTree::CollectLeaves(vector<BspLeaf *> &leaves) const
|
---|
1004 | {
|
---|
1005 | stack<BspNode *> nodeStack;
|
---|
1006 | nodeStack.push(mRoot);
|
---|
1007 |
|
---|
1008 | while (!nodeStack.empty())
|
---|
1009 | {
|
---|
1010 | BspNode *node = nodeStack.top();
|
---|
1011 |
|
---|
1012 | nodeStack.pop();
|
---|
1013 |
|
---|
1014 | if (node->IsLeaf())
|
---|
1015 | {
|
---|
1016 | BspLeaf *leaf = (BspLeaf *)node;
|
---|
1017 | leaves.push_back(leaf);
|
---|
1018 | }
|
---|
1019 | else
|
---|
1020 | {
|
---|
1021 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1022 |
|
---|
1023 | nodeStack.push(interior->GetBack());
|
---|
1024 | nodeStack.push(interior->GetFront());
|
---|
1025 | }
|
---|
1026 | }
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | AxisAlignedBox3 VspBspTree::GetBoundingBox() const
|
---|
1030 | {
|
---|
1031 | return mBox;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | BspNode *VspBspTree::GetRoot() const
|
---|
1035 | {
|
---|
1036 | return mRoot;
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | void VspBspTree::EvaluateLeafStats(const VspBspTraversalData &data)
|
---|
1040 | {
|
---|
1041 | // the node became a leaf -> evaluate stats for leafs
|
---|
1042 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode);
|
---|
1043 |
|
---|
1044 | // store maximal and minimal depth
|
---|
1045 | if (data.mDepth > mStat.maxDepth)
|
---|
1046 | mStat.maxDepth = data.mDepth;
|
---|
1047 |
|
---|
1048 | if (data.mDepth < mStat.minDepth)
|
---|
1049 | mStat.minDepth = data.mDepth;
|
---|
1050 |
|
---|
1051 | // accumulate depth to compute average depth
|
---|
1052 | mStat.accumDepth += data.mDepth;
|
---|
1053 |
|
---|
1054 |
|
---|
1055 | if (data.mDepth >= mTermMaxDepth)
|
---|
1056 | ++ mStat.maxDepthNodes;
|
---|
1057 |
|
---|
1058 | if (data.mPvs < mTermMinPvs)
|
---|
1059 | ++ mStat.minPvsNodes;
|
---|
1060 |
|
---|
1061 | if ((int)data.mRays->size() < mTermMinRays)
|
---|
1062 | ++ mStat.minRaysNodes;
|
---|
1063 |
|
---|
1064 | if (data.GetAvgRayContribution() > mTermMaxRayContribution)
|
---|
1065 | ++ mStat.maxRayContribNodes;
|
---|
1066 |
|
---|
1067 | if (data.mGeometry->GetArea() <= mTermMinArea)
|
---|
1068 | ++ mStat.minAreaNodes;
|
---|
1069 |
|
---|
1070 | #ifdef _DEBUG
|
---|
1071 | Debug << "BSP stats: "
|
---|
1072 | << "Depth: " << data.mDepth << " (max: " << mTermMaxDepth << "), "
|
---|
1073 | << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), "
|
---|
1074 | << "Area: " << data.mArea << " (min: " << mTermMinArea << "), "
|
---|
1075 | << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "
|
---|
1076 | << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "=, "
|
---|
1077 | << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl;
|
---|
1078 | #endif
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | int VspBspTree::CastRay(Ray &ray)
|
---|
1082 | {
|
---|
1083 | int hits = 0;
|
---|
1084 |
|
---|
1085 | stack<BspRayTraversalData> tStack;
|
---|
1086 |
|
---|
1087 | float maxt, mint;
|
---|
1088 |
|
---|
1089 | if (!mBox.GetRaySegment(ray, mint, maxt))
|
---|
1090 | return 0;
|
---|
1091 |
|
---|
1092 | Intersectable::NewMail();
|
---|
1093 |
|
---|
1094 | Vector3 entp = ray.Extrap(mint);
|
---|
1095 | Vector3 extp = ray.Extrap(maxt);
|
---|
1096 |
|
---|
1097 | BspNode *node = mRoot;
|
---|
1098 | BspNode *farChild = NULL;
|
---|
1099 |
|
---|
1100 | while (1)
|
---|
1101 | {
|
---|
1102 | if (!node->IsLeaf())
|
---|
1103 | {
|
---|
1104 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
1105 |
|
---|
1106 | Plane3 splitPlane = in->GetPlane();
|
---|
1107 | const int entSide = splitPlane.Side(entp);
|
---|
1108 | const int extSide = splitPlane.Side(extp);
|
---|
1109 |
|
---|
1110 | if (entSide < 0)
|
---|
1111 | {
|
---|
1112 | node = in->GetBack();
|
---|
1113 |
|
---|
1114 | if(extSide <= 0) // plane does not split ray => no far child
|
---|
1115 | continue;
|
---|
1116 |
|
---|
1117 | farChild = in->GetFront(); // plane splits ray
|
---|
1118 |
|
---|
1119 | } else if (entSide > 0)
|
---|
1120 | {
|
---|
1121 | node = in->GetFront();
|
---|
1122 |
|
---|
1123 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
1124 | continue;
|
---|
1125 |
|
---|
1126 | farChild = in->GetBack(); // plane splits ray
|
---|
1127 | }
|
---|
1128 | else // ray and plane are coincident
|
---|
1129 | {
|
---|
1130 | // WHAT TO DO IN THIS CASE ?
|
---|
1131 | //break;
|
---|
1132 | node = in->GetFront();
|
---|
1133 | continue;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | // push data for far child
|
---|
1137 | tStack.push(BspRayTraversalData(farChild, extp, maxt));
|
---|
1138 |
|
---|
1139 | // find intersection of ray segment with plane
|
---|
1140 | float t;
|
---|
1141 | extp = splitPlane.FindIntersection(ray.GetLoc(), extp, &t);
|
---|
1142 | maxt *= t;
|
---|
1143 |
|
---|
1144 | } else // reached leaf => intersection with view cell
|
---|
1145 | {
|
---|
1146 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
1147 |
|
---|
1148 | if (!leaf->GetViewCell()->Mailed())
|
---|
1149 | {
|
---|
1150 | //ray.bspIntersections.push_back(Ray::VspBspIntersection(maxt, leaf));
|
---|
1151 | leaf->GetViewCell()->Mail();
|
---|
1152 | ++ hits;
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | //-- fetch the next far child from the stack
|
---|
1156 | if (tStack.empty())
|
---|
1157 | break;
|
---|
1158 |
|
---|
1159 | entp = extp;
|
---|
1160 | mint = maxt; // NOTE: need this?
|
---|
1161 |
|
---|
1162 | if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
|
---|
1163 | break;
|
---|
1164 |
|
---|
1165 | BspRayTraversalData &s = tStack.top();
|
---|
1166 |
|
---|
1167 | node = s.mNode;
|
---|
1168 | extp = s.mExitPoint;
|
---|
1169 | maxt = s.mMaxT;
|
---|
1170 |
|
---|
1171 | tStack.pop();
|
---|
1172 | }
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | return hits;
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | bool VspBspTree::Export(const string filename)
|
---|
1179 | {
|
---|
1180 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
1181 |
|
---|
1182 | if (exporter)
|
---|
1183 | {
|
---|
1184 | //exporter->ExportVspBspTree(*this);
|
---|
1185 | return true;
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | return false;
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | void VspBspTree::CollectViewCells(ViewCellContainer &viewCells) const
|
---|
1192 | {
|
---|
1193 | stack<BspNode *> nodeStack;
|
---|
1194 | nodeStack.push(mRoot);
|
---|
1195 |
|
---|
1196 | ViewCell::NewMail();
|
---|
1197 |
|
---|
1198 | while (!nodeStack.empty())
|
---|
1199 | {
|
---|
1200 | BspNode *node = nodeStack.top();
|
---|
1201 | nodeStack.pop();
|
---|
1202 |
|
---|
1203 | if (node->IsLeaf())
|
---|
1204 | {
|
---|
1205 | ViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
1206 |
|
---|
1207 | if (!viewCell->Mailed())
|
---|
1208 | {
|
---|
1209 | viewCell->Mail();
|
---|
1210 | viewCells.push_back(viewCell);
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 | else
|
---|
1214 | {
|
---|
1215 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1216 |
|
---|
1217 | nodeStack.push(interior->GetFront());
|
---|
1218 | nodeStack.push(interior->GetBack());
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 | void VspBspTree::EvaluateViewCellsStats(ViewCellsStatistics &stat) const
|
---|
1224 | {
|
---|
1225 | stat.Reset();
|
---|
1226 |
|
---|
1227 | stack<BspNode *> nodeStack;
|
---|
1228 | nodeStack.push(mRoot);
|
---|
1229 |
|
---|
1230 | ViewCell::NewMail();
|
---|
1231 |
|
---|
1232 | // exclude root cell
|
---|
1233 | mRootCell->Mail();
|
---|
1234 |
|
---|
1235 | while (!nodeStack.empty())
|
---|
1236 | {
|
---|
1237 | BspNode *node = nodeStack.top();
|
---|
1238 | nodeStack.pop();
|
---|
1239 |
|
---|
1240 | if (node->IsLeaf())
|
---|
1241 | {
|
---|
1242 | ++ stat.leaves;
|
---|
1243 |
|
---|
1244 | BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
1245 |
|
---|
1246 | if (!viewCell->Mailed())
|
---|
1247 | {
|
---|
1248 | viewCell->Mail();
|
---|
1249 |
|
---|
1250 | ++ stat.viewCells;
|
---|
1251 | const int pvsSize = viewCell->GetPvs().GetSize();
|
---|
1252 |
|
---|
1253 | stat.pvs += pvsSize;
|
---|
1254 |
|
---|
1255 | if (pvsSize < 1)
|
---|
1256 | ++ stat.emptyPvs;
|
---|
1257 |
|
---|
1258 | if (pvsSize > stat.maxPvs)
|
---|
1259 | stat.maxPvs = pvsSize;
|
---|
1260 |
|
---|
1261 | if (pvsSize < stat.minPvs)
|
---|
1262 | stat.minPvs = pvsSize;
|
---|
1263 |
|
---|
1264 | if ((int)viewCell->mLeaves.size() > stat.maxLeaves)
|
---|
1265 | stat.maxLeaves = (int)viewCell->mLeaves.size();
|
---|
1266 | }
|
---|
1267 | }
|
---|
1268 | else
|
---|
1269 | {
|
---|
1270 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1271 |
|
---|
1272 | nodeStack.push(interior->GetFront());
|
---|
1273 | nodeStack.push(interior->GetBack());
|
---|
1274 | }
|
---|
1275 | }
|
---|
1276 | }
|
---|
1277 |
|
---|
1278 | BspTreeStatistics &VspBspTree::GetStat()
|
---|
1279 | {
|
---|
1280 | return mStat;
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | float VspBspTree::AccumulatedRayLength(const RayInfoContainer &rays) const
|
---|
1284 | {
|
---|
1285 | float len = 0;
|
---|
1286 |
|
---|
1287 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
1288 |
|
---|
1289 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
1290 | len += (*it).SegmentLength();
|
---|
1291 |
|
---|
1292 | return len;
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | int VspBspTree::SplitRays(const Plane3 &plane,
|
---|
1296 | RayInfoContainer &rays,
|
---|
1297 | RayInfoContainer &frontRays,
|
---|
1298 | RayInfoContainer &backRays)
|
---|
1299 | {
|
---|
1300 | int splits = 0;
|
---|
1301 |
|
---|
1302 | while (!rays.empty())
|
---|
1303 | {
|
---|
1304 | RayInfo bRay = rays.back();
|
---|
1305 | VssRay *ray = bRay.mRay;
|
---|
1306 |
|
---|
1307 | rays.pop_back();
|
---|
1308 | float t;
|
---|
1309 | // get classification and receive new t
|
---|
1310 | const int cf = bRay.ComputeRayIntersection(plane, t);
|
---|
1311 |
|
---|
1312 | switch (cf)
|
---|
1313 | {
|
---|
1314 | case -1:
|
---|
1315 | backRays.push_back(bRay);
|
---|
1316 | break;
|
---|
1317 | case 1:
|
---|
1318 | frontRays.push_back(bRay);
|
---|
1319 | break;
|
---|
1320 | case 0:
|
---|
1321 | //-- split ray
|
---|
1322 |
|
---|
1323 | // if start point behind plane
|
---|
1324 | if (plane.Side(bRay.ExtrapOrigin()) <= 0)
|
---|
1325 | {
|
---|
1326 | backRays.push_back(RayInfo(ray, bRay.GetMinT(), t));
|
---|
1327 | frontRays.push_back(RayInfo(ray, t, bRay.GetMaxT()));
|
---|
1328 | }
|
---|
1329 | else
|
---|
1330 | {
|
---|
1331 | frontRays.push_back(RayInfo(ray, bRay.GetMinT(), t));
|
---|
1332 | backRays.push_back(RayInfo(ray, t, bRay.GetMaxT()));
|
---|
1333 | }
|
---|
1334 | break;
|
---|
1335 | default:
|
---|
1336 | Debug << "Should not come here 4" << endl;
|
---|
1337 | break;
|
---|
1338 | }
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | return splits;
|
---|
1342 | }
|
---|
1343 |
|
---|
1344 | void VspBspTree::ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const
|
---|
1345 | {
|
---|
1346 | BspNode *lastNode;
|
---|
1347 |
|
---|
1348 | do
|
---|
1349 | {
|
---|
1350 | lastNode = n;
|
---|
1351 |
|
---|
1352 | // want to get planes defining geometry of this node => don't take
|
---|
1353 | // split plane of node itself
|
---|
1354 | n = n->GetParent();
|
---|
1355 |
|
---|
1356 | if (n)
|
---|
1357 | {
|
---|
1358 | BspInterior *interior = dynamic_cast<BspInterior *>(n);
|
---|
1359 | Plane3 halfSpace = dynamic_cast<BspInterior *>(interior)->GetPlane();
|
---|
1360 |
|
---|
1361 | if (interior->GetFront() != lastNode)
|
---|
1362 | halfSpace.ReverseOrientation();
|
---|
1363 |
|
---|
1364 | halfSpaces.push_back(halfSpace);
|
---|
1365 | }
|
---|
1366 | }
|
---|
1367 | while (n);
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | void VspBspTree::ConstructGeometry(BspNode *n,
|
---|
1371 | BspNodeGeometry &cell) const
|
---|
1372 | {
|
---|
1373 | PolygonContainer polys;
|
---|
1374 | ConstructGeometry(n, polys);
|
---|
1375 | cell.mPolys = polys;
|
---|
1376 | }
|
---|
1377 |
|
---|
1378 | void VspBspTree::ConstructGeometry(BspNode *n,
|
---|
1379 | PolygonContainer &cell) const
|
---|
1380 | {
|
---|
1381 | vector<Plane3> halfSpaces;
|
---|
1382 | ExtractHalfSpaces(n, halfSpaces);
|
---|
1383 |
|
---|
1384 | PolygonContainer candidatePolys;
|
---|
1385 |
|
---|
1386 | // bounded planes are added to the polygons (reverse polygons
|
---|
1387 | // as they have to be outfacing
|
---|
1388 | for (int i = 0; i < (int)halfSpaces.size(); ++ i)
|
---|
1389 | {
|
---|
1390 | Polygon3 *p = GetBoundingBox().CrossSection(halfSpaces[i]);
|
---|
1391 |
|
---|
1392 | if (p->Valid(mEpsilon))
|
---|
1393 | {
|
---|
1394 | candidatePolys.push_back(p->CreateReversePolygon());
|
---|
1395 | DEL_PTR(p);
|
---|
1396 | }
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | // add faces of bounding box (also could be faces of the cell)
|
---|
1400 | for (int i = 0; i < 6; ++ i)
|
---|
1401 | {
|
---|
1402 | VertexContainer vertices;
|
---|
1403 |
|
---|
1404 | for (int j = 0; j < 4; ++ j)
|
---|
1405 | vertices.push_back(mBox.GetFace(i).mVertices[j]);
|
---|
1406 |
|
---|
1407 | candidatePolys.push_back(new Polygon3(vertices));
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | for (int i = 0; i < (int)candidatePolys.size(); ++ i)
|
---|
1411 | {
|
---|
1412 | // polygon is split by all other planes
|
---|
1413 | for (int j = 0; (j < (int)halfSpaces.size()) && candidatePolys[i]; ++ j)
|
---|
1414 | {
|
---|
1415 | if (i == j) // polygon and plane are coincident
|
---|
1416 | continue;
|
---|
1417 |
|
---|
1418 | VertexContainer splitPts;
|
---|
1419 | Polygon3 *frontPoly, *backPoly;
|
---|
1420 |
|
---|
1421 | const int cf =
|
---|
1422 | candidatePolys[i]->ClassifyPlane(halfSpaces[j],
|
---|
1423 | mEpsilon);
|
---|
1424 |
|
---|
1425 | switch (cf)
|
---|
1426 | {
|
---|
1427 | case Polygon3::SPLIT:
|
---|
1428 | frontPoly = new Polygon3();
|
---|
1429 | backPoly = new Polygon3();
|
---|
1430 |
|
---|
1431 | candidatePolys[i]->Split(halfSpaces[j],
|
---|
1432 | *frontPoly,
|
---|
1433 | *backPoly,
|
---|
1434 | mEpsilon);
|
---|
1435 |
|
---|
1436 | DEL_PTR(candidatePolys[i]);
|
---|
1437 |
|
---|
1438 | if (frontPoly->Valid(mEpsilon))
|
---|
1439 | candidatePolys[i] = frontPoly;
|
---|
1440 | else
|
---|
1441 | DEL_PTR(frontPoly);
|
---|
1442 |
|
---|
1443 | DEL_PTR(backPoly);
|
---|
1444 | break;
|
---|
1445 | case Polygon3::BACK_SIDE:
|
---|
1446 | DEL_PTR(candidatePolys[i]);
|
---|
1447 | break;
|
---|
1448 | // just take polygon as it is
|
---|
1449 | case Polygon3::FRONT_SIDE:
|
---|
1450 | case Polygon3::COINCIDENT:
|
---|
1451 | default:
|
---|
1452 | break;
|
---|
1453 | }
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | if (candidatePolys[i])
|
---|
1457 | cell.push_back(candidatePolys[i]);
|
---|
1458 | }
|
---|
1459 | }
|
---|
1460 |
|
---|
1461 | void VspBspTree::ConstructGeometry(BspViewCell *vc, PolygonContainer &vcGeom) const
|
---|
1462 | {
|
---|
1463 | vector<BspLeaf *> leaves = vc->mLeaves;
|
---|
1464 |
|
---|
1465 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
1466 |
|
---|
1467 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
1468 | ConstructGeometry(*it, vcGeom);
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | int VspBspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,
|
---|
1472 | const bool onlyUnmailed) const
|
---|
1473 | {
|
---|
1474 | PolygonContainer cell;
|
---|
1475 |
|
---|
1476 | ConstructGeometry(n, cell);
|
---|
1477 |
|
---|
1478 | stack<BspNode *> nodeStack;
|
---|
1479 | nodeStack.push(mRoot);
|
---|
1480 |
|
---|
1481 | // planes needed to verify that we found neighbor leaf.
|
---|
1482 | vector<Plane3> halfSpaces;
|
---|
1483 | ExtractHalfSpaces(n, halfSpaces);
|
---|
1484 |
|
---|
1485 | while (!nodeStack.empty())
|
---|
1486 | {
|
---|
1487 | BspNode *node = nodeStack.top();
|
---|
1488 | nodeStack.pop();
|
---|
1489 |
|
---|
1490 | if (node->IsLeaf())
|
---|
1491 | {
|
---|
1492 | if (node != n && (!onlyUnmailed || !node->Mailed()))
|
---|
1493 | {
|
---|
1494 | // test all planes of current node if candidate really
|
---|
1495 | // is neighbour
|
---|
1496 | PolygonContainer neighborCandidate;
|
---|
1497 | ConstructGeometry(node, neighborCandidate);
|
---|
1498 |
|
---|
1499 | bool isAdjacent = true;
|
---|
1500 | for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i)
|
---|
1501 | {
|
---|
1502 | const int cf =
|
---|
1503 | Polygon3::ClassifyPlane(neighborCandidate,
|
---|
1504 | halfSpaces[i],
|
---|
1505 | mEpsilon);
|
---|
1506 |
|
---|
1507 | if (cf == Polygon3::BACK_SIDE)
|
---|
1508 | isAdjacent = false;
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | if (isAdjacent)
|
---|
1512 | neighbors.push_back(dynamic_cast<BspLeaf *>(node));
|
---|
1513 |
|
---|
1514 | CLEAR_CONTAINER(neighborCandidate);
|
---|
1515 | }
|
---|
1516 | }
|
---|
1517 | else
|
---|
1518 | {
|
---|
1519 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1520 |
|
---|
1521 | const int cf = Polygon3::ClassifyPlane(cell,
|
---|
1522 | interior->GetPlane(),
|
---|
1523 | mEpsilon);
|
---|
1524 |
|
---|
1525 | if (cf == Polygon3::FRONT_SIDE)
|
---|
1526 | nodeStack.push(interior->GetFront());
|
---|
1527 | else
|
---|
1528 | if (cf == Polygon3::BACK_SIDE)
|
---|
1529 | nodeStack.push(interior->GetBack());
|
---|
1530 | else
|
---|
1531 | {
|
---|
1532 | // random decision
|
---|
1533 | nodeStack.push(interior->GetBack());
|
---|
1534 | nodeStack.push(interior->GetFront());
|
---|
1535 | }
|
---|
1536 | }
|
---|
1537 | }
|
---|
1538 |
|
---|
1539 | CLEAR_CONTAINER(cell);
|
---|
1540 | return (int)neighbors.size();
|
---|
1541 | }
|
---|
1542 |
|
---|
1543 | BspLeaf *VspBspTree::GetRandomLeaf(const Plane3 &halfspace)
|
---|
1544 | {
|
---|
1545 | stack<BspNode *> nodeStack;
|
---|
1546 | nodeStack.push(mRoot);
|
---|
1547 |
|
---|
1548 | int mask = rand();
|
---|
1549 |
|
---|
1550 | while (!nodeStack.empty())
|
---|
1551 | {
|
---|
1552 | BspNode *node = nodeStack.top();
|
---|
1553 | nodeStack.pop();
|
---|
1554 |
|
---|
1555 | if (node->IsLeaf())
|
---|
1556 | {
|
---|
1557 | return dynamic_cast<BspLeaf *>(node);
|
---|
1558 | }
|
---|
1559 | else
|
---|
1560 | {
|
---|
1561 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1562 |
|
---|
1563 | BspNode *next;
|
---|
1564 |
|
---|
1565 | PolygonContainer cell;
|
---|
1566 |
|
---|
1567 | // todo: not very efficient: constructs full cell everytime
|
---|
1568 | ConstructGeometry(interior, cell);
|
---|
1569 |
|
---|
1570 | const int cf = Polygon3::ClassifyPlane(cell, halfspace, mEpsilon);
|
---|
1571 |
|
---|
1572 | if (cf == Polygon3::BACK_SIDE)
|
---|
1573 | next = interior->GetFront();
|
---|
1574 | else
|
---|
1575 | if (cf == Polygon3::FRONT_SIDE)
|
---|
1576 | next = interior->GetFront();
|
---|
1577 | else
|
---|
1578 | {
|
---|
1579 | // random decision
|
---|
1580 | if (mask & 1)
|
---|
1581 | next = interior->GetBack();
|
---|
1582 | else
|
---|
1583 | next = interior->GetFront();
|
---|
1584 | mask = mask >> 1;
|
---|
1585 | }
|
---|
1586 |
|
---|
1587 | nodeStack.push(next);
|
---|
1588 | }
|
---|
1589 | }
|
---|
1590 |
|
---|
1591 | return NULL;
|
---|
1592 | }
|
---|
1593 |
|
---|
1594 | BspLeaf *VspBspTree::GetRandomLeaf(const bool onlyUnmailed)
|
---|
1595 | {
|
---|
1596 | stack<BspNode *> nodeStack;
|
---|
1597 |
|
---|
1598 | nodeStack.push(mRoot);
|
---|
1599 |
|
---|
1600 | int mask = rand();
|
---|
1601 |
|
---|
1602 | while (!nodeStack.empty())
|
---|
1603 | {
|
---|
1604 | BspNode *node = nodeStack.top();
|
---|
1605 | nodeStack.pop();
|
---|
1606 |
|
---|
1607 | if (node->IsLeaf())
|
---|
1608 | {
|
---|
1609 | if ( (!onlyUnmailed || !node->Mailed()) )
|
---|
1610 | return dynamic_cast<BspLeaf *>(node);
|
---|
1611 | }
|
---|
1612 | else
|
---|
1613 | {
|
---|
1614 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
1615 |
|
---|
1616 | // random decision
|
---|
1617 | if (mask & 1)
|
---|
1618 | nodeStack.push(interior->GetBack());
|
---|
1619 | else
|
---|
1620 | nodeStack.push(interior->GetFront());
|
---|
1621 |
|
---|
1622 | mask = mask >> 1;
|
---|
1623 | }
|
---|
1624 | }
|
---|
1625 |
|
---|
1626 | return NULL;
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 | int VspBspTree::ComputePvsSize(const RayInfoContainer &rays) const
|
---|
1630 | {
|
---|
1631 | int pvsSize = 0;
|
---|
1632 |
|
---|
1633 | RayInfoContainer::const_iterator rit, rit_end = rays.end();
|
---|
1634 |
|
---|
1635 | Intersectable::NewMail();
|
---|
1636 |
|
---|
1637 | for (rit = rays.begin(); rit != rays.end(); ++ rit)
|
---|
1638 | {
|
---|
1639 | VssRay *ray = (*rit).mRay;
|
---|
1640 |
|
---|
1641 | if (ray->mOriginObject)
|
---|
1642 | {
|
---|
1643 | if (!ray->mOriginObject->Mailed())
|
---|
1644 | {
|
---|
1645 | ray->mOriginObject->Mail();
|
---|
1646 | ++ pvsSize;
|
---|
1647 | }
|
---|
1648 | }
|
---|
1649 | if (ray->mTerminationObject)
|
---|
1650 | {
|
---|
1651 | if (!ray->mTerminationObject->Mailed())
|
---|
1652 | {
|
---|
1653 | ray->mTerminationObject->Mail();
|
---|
1654 | ++ pvsSize;
|
---|
1655 | }
|
---|
1656 | }
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | return pvsSize;
|
---|
1660 | }
|
---|
1661 |
|
---|
1662 | float VspBspTree::GetEpsilon() const
|
---|
1663 | {
|
---|
1664 | return mEpsilon;
|
---|
1665 | }
|
---|
1666 |
|
---|
1667 | BspViewCell *VspBspTree::GetRootCell() const
|
---|
1668 | {
|
---|
1669 | return mRootCell;
|
---|
1670 | }
|
---|
1671 |
|
---|
1672 | int VspBspTree::SplitPolygons(const Plane3 &plane,
|
---|
1673 | PolygonContainer &polys,
|
---|
1674 | PolygonContainer &frontPolys,
|
---|
1675 | PolygonContainer &backPolys,
|
---|
1676 | PolygonContainer &coincident) const
|
---|
1677 | {
|
---|
1678 | int splits = 0;
|
---|
1679 |
|
---|
1680 | while (!polys.empty())
|
---|
1681 | {
|
---|
1682 | Polygon3 *poly = polys.back();
|
---|
1683 | polys.pop_back();
|
---|
1684 |
|
---|
1685 | // classify polygon
|
---|
1686 | const int cf = poly->ClassifyPlane(plane, mEpsilon);
|
---|
1687 |
|
---|
1688 | switch (cf)
|
---|
1689 | {
|
---|
1690 | case Polygon3::COINCIDENT:
|
---|
1691 | coincident.push_back(poly);
|
---|
1692 | break;
|
---|
1693 | case Polygon3::FRONT_SIDE:
|
---|
1694 | frontPolys.push_back(poly);
|
---|
1695 | break;
|
---|
1696 | case Polygon3::BACK_SIDE:
|
---|
1697 | backPolys.push_back(poly);
|
---|
1698 | break;
|
---|
1699 | case Polygon3::SPLIT:
|
---|
1700 | backPolys.push_back(poly);
|
---|
1701 | frontPolys.push_back(poly);
|
---|
1702 | ++ splits;
|
---|
1703 | break;
|
---|
1704 | default:
|
---|
1705 | Debug << "SHOULD NEVER COME HERE\n";
|
---|
1706 | break;
|
---|
1707 | }
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | return splits;
|
---|
1711 | }
|
---|
1712 |
|
---|
1713 |
|
---|
1714 | int VspBspTree::CastLineSegment(const Vector3 &origin,
|
---|
1715 | const Vector3 &termination,
|
---|
1716 | vector<ViewCell *> &viewcells)
|
---|
1717 | {
|
---|
1718 | int hits = 0;
|
---|
1719 | stack<BspRayTraversalData> tStack;
|
---|
1720 |
|
---|
1721 | float mint = 0.0f, maxt = 1.0f;
|
---|
1722 |
|
---|
1723 | Intersectable::NewMail();
|
---|
1724 |
|
---|
1725 | Vector3 entp = origin;
|
---|
1726 | Vector3 extp = termination;
|
---|
1727 |
|
---|
1728 | BspNode *node = mRoot;
|
---|
1729 | BspNode *farChild = NULL;
|
---|
1730 |
|
---|
1731 | while (1)
|
---|
1732 | {
|
---|
1733 | if (!node->IsLeaf())
|
---|
1734 | {
|
---|
1735 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
1736 |
|
---|
1737 | Plane3 splitPlane = in->GetPlane();
|
---|
1738 | const int entSide = splitPlane.Side(entp);
|
---|
1739 | const int extSide = splitPlane.Side(extp);
|
---|
1740 |
|
---|
1741 | if (entSide < 0)
|
---|
1742 | {
|
---|
1743 | node = in->GetBack();
|
---|
1744 |
|
---|
1745 | if(extSide <= 0) // plane does not split ray => no far child
|
---|
1746 | continue;
|
---|
1747 |
|
---|
1748 | farChild = in->GetFront(); // plane splits ray
|
---|
1749 | } else if (entSide > 0)
|
---|
1750 | {
|
---|
1751 | node = in->GetFront();
|
---|
1752 |
|
---|
1753 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
1754 | continue;
|
---|
1755 |
|
---|
1756 | farChild = in->GetBack(); // plane splits ray
|
---|
1757 | }
|
---|
1758 | else // ray and plane are coincident
|
---|
1759 | {
|
---|
1760 | // WHAT TO DO IN THIS CASE ?
|
---|
1761 | //break;
|
---|
1762 | node = in->GetFront();
|
---|
1763 | continue;
|
---|
1764 | }
|
---|
1765 |
|
---|
1766 | // push data for far child
|
---|
1767 | tStack.push(BspRayTraversalData(farChild, extp, maxt));
|
---|
1768 |
|
---|
1769 | // find intersection of ray segment with plane
|
---|
1770 | float t;
|
---|
1771 | extp = splitPlane.FindIntersection(origin, extp, &t);
|
---|
1772 | maxt *= t;
|
---|
1773 |
|
---|
1774 | } else
|
---|
1775 | {
|
---|
1776 | // reached leaf => intersection with view cell
|
---|
1777 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
1778 |
|
---|
1779 | if (!leaf->GetViewCell()->Mailed())
|
---|
1780 | {
|
---|
1781 | viewcells.push_back(leaf->GetViewCell());
|
---|
1782 | leaf->GetViewCell()->Mail();
|
---|
1783 | ++ hits;
|
---|
1784 | }
|
---|
1785 |
|
---|
1786 | //-- fetch the next far child from the stack
|
---|
1787 | if (tStack.empty())
|
---|
1788 | break;
|
---|
1789 |
|
---|
1790 | entp = extp;
|
---|
1791 | mint = maxt; // NOTE: need this?
|
---|
1792 |
|
---|
1793 |
|
---|
1794 | BspRayTraversalData &s = tStack.top();
|
---|
1795 |
|
---|
1796 | node = s.mNode;
|
---|
1797 | extp = s.mExitPoint;
|
---|
1798 | maxt = s.mMaxT;
|
---|
1799 |
|
---|
1800 | tStack.pop();
|
---|
1801 | }
|
---|
1802 | }
|
---|
1803 | return hits;
|
---|
1804 | }
|
---|