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->mBspLeaves.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 | const int cf = rayInf.ComputeRayIntersection(candidatePlane, ray->mT); |
---|
865 | |
---|
866 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS) |
---|
867 | { |
---|
868 | sumBalancedRays += cf; |
---|
869 | } |
---|
870 | |
---|
871 | if (mSplitPlaneStrategy & BALANCED_RAYS) |
---|
872 | { |
---|
873 | if (cf == 0) |
---|
874 | ++ sumRaySplits; |
---|
875 | } |
---|
876 | |
---|
877 | if (mSplitPlaneStrategy & PVS) |
---|
878 | { |
---|
879 | // in case the ray intersects an object |
---|
880 | // assure that we only count the object |
---|
881 | // once for the front and once for the back side of the plane |
---|
882 | |
---|
883 | // add the termination object |
---|
884 | AddObjToPvs(ray->mTerminationObject, cf, frontPvs, backPvs); |
---|
885 | |
---|
886 | // add the source object |
---|
887 | AddObjToPvs(ray->mOriginObject, cf, frontPvs, backPvs); |
---|
888 | |
---|
889 | // use number or length of rays to approximate volume |
---|
890 | if (!mPvsUseArea) |
---|
891 | { |
---|
892 | float len = 1; |
---|
893 | |
---|
894 | if (pvsUseLen) // use length of rays |
---|
895 | len = rayInf.SqrSegmentLength(); |
---|
896 | |
---|
897 | pOverall += len; |
---|
898 | |
---|
899 | if (cf == 1) |
---|
900 | pFront += len; |
---|
901 | if (cf == -1) |
---|
902 | pBack += len; |
---|
903 | if (cf == 0) |
---|
904 | { |
---|
905 | // use length of rays to approximate volume |
---|
906 | if (pvsUseLen) |
---|
907 | { |
---|
908 | float newLen = len * |
---|
909 | (rayInf.GetMaxT() - rayInf.mRay->mT) / |
---|
910 | (rayInf.GetMaxT() - rayInf.GetMinT()); |
---|
911 | |
---|
912 | if (candidatePlane.Side(rayInf.ExtrapOrigin()) <= 0) |
---|
913 | { |
---|
914 | pBack += newLen; |
---|
915 | pFront += len - newLen; |
---|
916 | } |
---|
917 | else |
---|
918 | { |
---|
919 | pFront += newLen; |
---|
920 | pBack += len - newLen; |
---|
921 | } |
---|
922 | } |
---|
923 | else |
---|
924 | { |
---|
925 | ++ pFront; |
---|
926 | ++ pBack; |
---|
927 | } |
---|
928 | } |
---|
929 | } |
---|
930 | } |
---|
931 | } |
---|
932 | |
---|
933 | const float raysSize = (float)data.mRays->size() + Limits::Small; |
---|
934 | |
---|
935 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS) |
---|
936 | val += mLeastRaySplitsFactor * sumRaySplits / raysSize; |
---|
937 | |
---|
938 | if (mSplitPlaneStrategy & BALANCED_RAYS) |
---|
939 | val += mBalancedRaysFactor * fabs(sumBalancedRays) / raysSize; |
---|
940 | |
---|
941 | const float denom = pOverall * (float)data.mPvs * 2.0f + Limits::Small; |
---|
942 | |
---|
943 | if (mSplitPlaneStrategy & PVS) |
---|
944 | { |
---|
945 | val += mPvsFactor * (frontPvs * pFront + (backPvs * pBack)) / denom; |
---|
946 | |
---|
947 | // give penalty to unbalanced split |
---|
948 | if (0) |
---|
949 | if (((pFront * 0.2 + Limits::Small) > pBack) || |
---|
950 | (pFront < (pBack * 0.2 + Limits::Small))) |
---|
951 | val += 0.5; |
---|
952 | } |
---|
953 | |
---|
954 | #ifdef _DEBUG |
---|
955 | // Debug << "totalpvs: " << pvs << " ptotal: " << pOverall |
---|
956 | // << " frontpvs: " << frontPvs << " pFront: " << pFront |
---|
957 | // << " backpvs: " << backPvs << " pBack: " << pBack << endl << endl; |
---|
958 | #endif |
---|
959 | return val; |
---|
960 | } |
---|
961 | |
---|
962 | void VspBspTree::AddObjToPvs(Intersectable *obj, |
---|
963 | const int cf, |
---|
964 | int &frontPvs, |
---|
965 | int &backPvs) const |
---|
966 | { |
---|
967 | if (!obj) |
---|
968 | return; |
---|
969 | // TODO: does this really belong to no pvs? |
---|
970 | //if (cf == Ray::COINCIDENT) return; |
---|
971 | |
---|
972 | // object belongs to both PVS |
---|
973 | if (cf >= 0) |
---|
974 | { |
---|
975 | if ((obj->mMailbox != sFrontId) && |
---|
976 | (obj->mMailbox != sFrontAndBackId)) |
---|
977 | { |
---|
978 | ++ frontPvs; |
---|
979 | |
---|
980 | if (obj->mMailbox == sBackId) |
---|
981 | obj->mMailbox = sFrontAndBackId; |
---|
982 | else |
---|
983 | obj->mMailbox = sFrontId; |
---|
984 | } |
---|
985 | } |
---|
986 | |
---|
987 | if (cf <= 0) |
---|
988 | { |
---|
989 | if ((obj->mMailbox != sBackId) && |
---|
990 | (obj->mMailbox != sFrontAndBackId)) |
---|
991 | { |
---|
992 | ++ backPvs; |
---|
993 | |
---|
994 | if (obj->mMailbox == sFrontId) |
---|
995 | obj->mMailbox = sFrontAndBackId; |
---|
996 | else |
---|
997 | obj->mMailbox = sBackId; |
---|
998 | } |
---|
999 | } |
---|
1000 | } |
---|
1001 | |
---|
1002 | void VspBspTree::CollectLeaves(vector<BspLeaf *> &leaves) const |
---|
1003 | { |
---|
1004 | stack<BspNode *> nodeStack; |
---|
1005 | nodeStack.push(mRoot); |
---|
1006 | |
---|
1007 | while (!nodeStack.empty()) |
---|
1008 | { |
---|
1009 | BspNode *node = nodeStack.top(); |
---|
1010 | |
---|
1011 | nodeStack.pop(); |
---|
1012 | |
---|
1013 | if (node->IsLeaf()) |
---|
1014 | { |
---|
1015 | BspLeaf *leaf = (BspLeaf *)node; |
---|
1016 | leaves.push_back(leaf); |
---|
1017 | } |
---|
1018 | else |
---|
1019 | { |
---|
1020 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
1021 | |
---|
1022 | nodeStack.push(interior->GetBack()); |
---|
1023 | nodeStack.push(interior->GetFront()); |
---|
1024 | } |
---|
1025 | } |
---|
1026 | } |
---|
1027 | |
---|
1028 | AxisAlignedBox3 VspBspTree::GetBoundingBox() const |
---|
1029 | { |
---|
1030 | return mBox; |
---|
1031 | } |
---|
1032 | |
---|
1033 | BspNode *VspBspTree::GetRoot() const |
---|
1034 | { |
---|
1035 | return mRoot; |
---|
1036 | } |
---|
1037 | |
---|
1038 | void VspBspTree::EvaluateLeafStats(const VspBspTraversalData &data) |
---|
1039 | { |
---|
1040 | // the node became a leaf -> evaluate stats for leafs |
---|
1041 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode); |
---|
1042 | |
---|
1043 | // store maximal and minimal depth |
---|
1044 | if (data.mDepth > mStat.maxDepth) |
---|
1045 | mStat.maxDepth = data.mDepth; |
---|
1046 | |
---|
1047 | if (data.mDepth < mStat.minDepth) |
---|
1048 | mStat.minDepth = data.mDepth; |
---|
1049 | |
---|
1050 | // accumulate depth to compute average depth |
---|
1051 | mStat.accumDepth += data.mDepth; |
---|
1052 | |
---|
1053 | |
---|
1054 | if (data.mDepth >= mTermMaxDepth) |
---|
1055 | ++ mStat.maxDepthNodes; |
---|
1056 | |
---|
1057 | if (data.mPvs < mTermMinPvs)
|
---|
1058 | ++ mStat.minPvsNodes;
|
---|
1059 |
|
---|
1060 | if ((int)data.mRays->size() < mTermMinRays)
|
---|
1061 | ++ mStat.minRaysNodes;
|
---|
1062 |
|
---|
1063 | if (data.GetAvgRayContribution() > mTermMaxRayContribution)
|
---|
1064 | ++ mStat.maxRayContribNodes;
|
---|
1065 | |
---|
1066 | if (data.mGeometry->GetArea() <= mTermMinArea) |
---|
1067 | ++ mStat.minAreaNodes; |
---|
1068 | |
---|
1069 | #ifdef _DEBUG |
---|
1070 | Debug << "BSP stats: " |
---|
1071 | << "Depth: " << data.mDepth << " (max: " << mTermMaxDepth << "), " |
---|
1072 | << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), " |
---|
1073 | << "Area: " << data.mArea << " (min: " << mTermMinArea << "), " |
---|
1074 | << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), " |
---|
1075 | << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "=, " |
---|
1076 | << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl; |
---|
1077 | #endif |
---|
1078 | } |
---|
1079 | |
---|
1080 | int VspBspTree::CastRay(Ray &ray) |
---|
1081 | { |
---|
1082 | int hits = 0; |
---|
1083 | |
---|
1084 | stack<BspRayTraversalData> tStack; |
---|
1085 | |
---|
1086 | float maxt, mint; |
---|
1087 | |
---|
1088 | if (!mBox.GetRaySegment(ray, mint, maxt)) |
---|
1089 | return 0; |
---|
1090 | |
---|
1091 | Intersectable::NewMail(); |
---|
1092 | |
---|
1093 | Vector3 entp = ray.Extrap(mint); |
---|
1094 | Vector3 extp = ray.Extrap(maxt); |
---|
1095 | |
---|
1096 | BspNode *node = mRoot; |
---|
1097 | BspNode *farChild = NULL; |
---|
1098 | |
---|
1099 | while (1) |
---|
1100 | { |
---|
1101 | if (!node->IsLeaf()) |
---|
1102 | { |
---|
1103 | BspInterior *in = dynamic_cast<BspInterior *>(node); |
---|
1104 | |
---|
1105 | Plane3 splitPlane = in->GetPlane(); |
---|
1106 | const int entSide = splitPlane.Side(entp); |
---|
1107 | const int extSide = splitPlane.Side(extp); |
---|
1108 | |
---|
1109 | if (entSide < 0) |
---|
1110 | { |
---|
1111 | node = in->GetBack(); |
---|
1112 | |
---|
1113 | if(extSide <= 0) // plane does not split ray => no far child |
---|
1114 | continue; |
---|
1115 | |
---|
1116 | farChild = in->GetFront(); // plane splits ray |
---|
1117 | |
---|
1118 | } else if (entSide > 0) |
---|
1119 | { |
---|
1120 | node = in->GetFront(); |
---|
1121 | |
---|
1122 | if (extSide >= 0) // plane does not split ray => no far child |
---|
1123 | continue; |
---|
1124 | |
---|
1125 | farChild = in->GetBack(); // plane splits ray |
---|
1126 | } |
---|
1127 | else // ray and plane are coincident |
---|
1128 | { |
---|
1129 | // WHAT TO DO IN THIS CASE ? |
---|
1130 | //break; |
---|
1131 | node = in->GetFront(); |
---|
1132 | continue; |
---|
1133 | } |
---|
1134 | |
---|
1135 | // push data for far child |
---|
1136 | tStack.push(BspRayTraversalData(farChild, extp, maxt)); |
---|
1137 | |
---|
1138 | // find intersection of ray segment with plane |
---|
1139 | float t; |
---|
1140 | extp = splitPlane.FindIntersection(ray.GetLoc(), extp, &t); |
---|
1141 | maxt *= t; |
---|
1142 | |
---|
1143 | } else // reached leaf => intersection with view cell |
---|
1144 | { |
---|
1145 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node); |
---|
1146 | |
---|
1147 | if (!leaf->GetViewCell()->Mailed()) |
---|
1148 | { |
---|
1149 | //ray.bspIntersections.push_back(Ray::VspBspIntersection(maxt, leaf)); |
---|
1150 | leaf->GetViewCell()->Mail(); |
---|
1151 | ++ hits; |
---|
1152 | } |
---|
1153 | |
---|
1154 | //-- fetch the next far child from the stack |
---|
1155 | if (tStack.empty()) |
---|
1156 | break; |
---|
1157 | |
---|
1158 | entp = extp; |
---|
1159 | mint = maxt; // NOTE: need this? |
---|
1160 | |
---|
1161 | if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f) |
---|
1162 | break; |
---|
1163 | |
---|
1164 | BspRayTraversalData &s = tStack.top(); |
---|
1165 | |
---|
1166 | node = s.mNode; |
---|
1167 | extp = s.mExitPoint; |
---|
1168 | maxt = s.mMaxT; |
---|
1169 | |
---|
1170 | tStack.pop(); |
---|
1171 | } |
---|
1172 | } |
---|
1173 | |
---|
1174 | return hits; |
---|
1175 | } |
---|
1176 | |
---|
1177 | bool VspBspTree::Export(const string filename) |
---|
1178 | { |
---|
1179 | Exporter *exporter = Exporter::GetExporter(filename); |
---|
1180 | |
---|
1181 | if (exporter) |
---|
1182 | { |
---|
1183 | //exporter->ExportVspBspTree(*this); |
---|
1184 | return true; |
---|
1185 | } |
---|
1186 | |
---|
1187 | return false; |
---|
1188 | } |
---|
1189 | |
---|
1190 | void VspBspTree::CollectViewCells(ViewCellContainer &viewCells) const |
---|
1191 | { |
---|
1192 | stack<BspNode *> nodeStack; |
---|
1193 | nodeStack.push(mRoot); |
---|
1194 | |
---|
1195 | ViewCell::NewMail(); |
---|
1196 | |
---|
1197 | while (!nodeStack.empty()) |
---|
1198 | { |
---|
1199 | BspNode *node = nodeStack.top(); |
---|
1200 | nodeStack.pop(); |
---|
1201 | |
---|
1202 | if (node->IsLeaf()) |
---|
1203 | { |
---|
1204 | ViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell(); |
---|
1205 | |
---|
1206 | if (!viewCell->Mailed()) |
---|
1207 | { |
---|
1208 | viewCell->Mail(); |
---|
1209 | viewCells.push_back(viewCell); |
---|
1210 | } |
---|
1211 | } |
---|
1212 | else |
---|
1213 | { |
---|
1214 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
1215 | |
---|
1216 | nodeStack.push(interior->GetFront()); |
---|
1217 | nodeStack.push(interior->GetBack()); |
---|
1218 | } |
---|
1219 | } |
---|
1220 | } |
---|
1221 | |
---|
1222 | void VspBspTree::EvaluateViewCellsStats(ViewCellsStatistics &stat) const |
---|
1223 | { |
---|
1224 | stat.Reset(); |
---|
1225 | |
---|
1226 | stack<BspNode *> nodeStack; |
---|
1227 | nodeStack.push(mRoot); |
---|
1228 | |
---|
1229 | ViewCell::NewMail(); |
---|
1230 | |
---|
1231 | // exclude root cell |
---|
1232 | mRootCell->Mail(); |
---|
1233 | |
---|
1234 | while (!nodeStack.empty()) |
---|
1235 | { |
---|
1236 | BspNode *node = nodeStack.top(); |
---|
1237 | nodeStack.pop(); |
---|
1238 | |
---|
1239 | if (node->IsLeaf()) |
---|
1240 | { |
---|
1241 | ++ stat.leaves; |
---|
1242 | |
---|
1243 | BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell(); |
---|
1244 | |
---|
1245 | if (!viewCell->Mailed()) |
---|
1246 | { |
---|
1247 | viewCell->Mail(); |
---|
1248 | |
---|
1249 | ++ stat.viewCells; |
---|
1250 | const int pvsSize = viewCell->GetPvs().GetSize(); |
---|
1251 | |
---|
1252 | stat.pvs += pvsSize; |
---|
1253 | |
---|
1254 | if (pvsSize < 1) |
---|
1255 | ++ stat.emptyPvs; |
---|
1256 | |
---|
1257 | if (pvsSize > stat.maxPvs) |
---|
1258 | stat.maxPvs = pvsSize; |
---|
1259 | |
---|
1260 | if (pvsSize < stat.minPvs) |
---|
1261 | stat.minPvs = pvsSize; |
---|
1262 | |
---|
1263 | if ((int)viewCell->mBspLeaves.size() > stat.maxLeaves) |
---|
1264 | stat.maxLeaves = (int)viewCell->mBspLeaves.size(); |
---|
1265 | } |
---|
1266 | } |
---|
1267 | else |
---|
1268 | { |
---|
1269 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
1270 | |
---|
1271 | nodeStack.push(interior->GetFront()); |
---|
1272 | nodeStack.push(interior->GetBack()); |
---|
1273 | } |
---|
1274 | } |
---|
1275 | } |
---|
1276 | |
---|
1277 | BspTreeStatistics &VspBspTree::GetStat() |
---|
1278 | { |
---|
1279 | return mStat; |
---|
1280 | } |
---|
1281 | |
---|
1282 | float VspBspTree::AccumulatedRayLength(const RayInfoContainer &rays) const |
---|
1283 | { |
---|
1284 | float len = 0; |
---|
1285 | |
---|
1286 | RayInfoContainer::const_iterator it, it_end = rays.end(); |
---|
1287 | |
---|
1288 | for (it = rays.begin(); it != it_end; ++ it) |
---|
1289 | len += (*it).SegmentLength(); |
---|
1290 | |
---|
1291 | return len; |
---|
1292 | } |
---|
1293 | |
---|
1294 | int VspBspTree::SplitRays(const Plane3 &plane, |
---|
1295 | RayInfoContainer &rays, |
---|
1296 | RayInfoContainer &frontRays, |
---|
1297 | RayInfoContainer &backRays) |
---|
1298 | { |
---|
1299 | int splits = 0; |
---|
1300 | |
---|
1301 | while (!rays.empty()) |
---|
1302 | { |
---|
1303 | RayInfo bRay = rays.back(); |
---|
1304 | VssRay *ray = bRay.mRay; |
---|
1305 | |
---|
1306 | rays.pop_back(); |
---|
1307 | |
---|
1308 | // get classification and receive new t |
---|
1309 | const int cf = bRay.ComputeRayIntersection(plane, ray->mT); |
---|
1310 | |
---|
1311 | switch (cf) |
---|
1312 | { |
---|
1313 | case -1: |
---|
1314 | backRays.push_back(bRay); |
---|
1315 | break; |
---|
1316 | case 1: |
---|
1317 | frontRays.push_back(bRay); |
---|
1318 | break; |
---|
1319 | case 0: |
---|
1320 | //-- split ray |
---|
1321 | |
---|
1322 | // if start point behind plane |
---|
1323 | if (plane.Side(bRay.ExtrapOrigin()) <= 0) |
---|
1324 | { |
---|
1325 | backRays.push_back(RayInfo(ray, bRay.GetMinT(), ray->mT)); |
---|
1326 | frontRays.push_back(RayInfo(ray, ray->mT, bRay.GetMaxT())); |
---|
1327 | } |
---|
1328 | else |
---|
1329 | { |
---|
1330 | frontRays.push_back(RayInfo(ray, bRay.GetMinT(), ray->mT)); |
---|
1331 | backRays.push_back(RayInfo(ray, ray->mT, bRay.GetMaxT())); |
---|
1332 | } |
---|
1333 | break; |
---|
1334 | default: |
---|
1335 | Debug << "Should not come here 4" << endl; |
---|
1336 | break; |
---|
1337 | } |
---|
1338 | } |
---|
1339 | |
---|
1340 | return splits; |
---|
1341 | } |
---|
1342 | |
---|
1343 | void VspBspTree::ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const |
---|
1344 | { |
---|
1345 | BspNode *lastNode; |
---|
1346 | |
---|
1347 | do |
---|
1348 | { |
---|
1349 | lastNode = n; |
---|
1350 | |
---|
1351 | // want to get planes defining geometry of this node => don't take |
---|
1352 | // split plane of node itself |
---|
1353 | n = n->GetParent(); |
---|
1354 | |
---|
1355 | if (n) |
---|
1356 | { |
---|
1357 | BspInterior *interior = dynamic_cast<BspInterior *>(n); |
---|
1358 | Plane3 halfSpace = dynamic_cast<BspInterior *>(interior)->GetPlane(); |
---|
1359 | |
---|
1360 | if (interior->GetFront() != lastNode) |
---|
1361 | halfSpace.ReverseOrientation(); |
---|
1362 | |
---|
1363 | halfSpaces.push_back(halfSpace); |
---|
1364 | } |
---|
1365 | } |
---|
1366 | while (n); |
---|
1367 | } |
---|
1368 | |
---|
1369 | void VspBspTree::ConstructGeometry(BspNode *n, |
---|
1370 | BspNodeGeometry &cell) const |
---|
1371 | { |
---|
1372 | PolygonContainer polys; |
---|
1373 | ConstructGeometry(n, polys); |
---|
1374 | cell.mPolys = polys; |
---|
1375 | } |
---|
1376 | |
---|
1377 | void VspBspTree::ConstructGeometry(BspNode *n,
|
---|
1378 | PolygonContainer &cell) const
|
---|
1379 | {
|
---|
1380 | vector<Plane3> halfSpaces;
|
---|
1381 | ExtractHalfSpaces(n, halfSpaces);
|
---|
1382 |
|
---|
1383 | PolygonContainer candidatePolys;
|
---|
1384 |
|
---|
1385 | // bounded planes are added to the polygons (reverse polygons
|
---|
1386 | // as they have to be outfacing
|
---|
1387 | for (int i = 0; i < (int)halfSpaces.size(); ++ i)
|
---|
1388 | {
|
---|
1389 | Polygon3 *p = GetBoundingBox().CrossSection(halfSpaces[i]);
|
---|
1390 |
|
---|
1391 | if (p->Valid(mEpsilon))
|
---|
1392 | {
|
---|
1393 | candidatePolys.push_back(p->CreateReversePolygon());
|
---|
1394 | DEL_PTR(p);
|
---|
1395 | }
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | // add faces of bounding box (also could be faces of the cell)
|
---|
1399 | for (int i = 0; i < 6; ++ i)
|
---|
1400 | {
|
---|
1401 | VertexContainer vertices;
|
---|
1402 |
|
---|
1403 | for (int j = 0; j < 4; ++ j)
|
---|
1404 | vertices.push_back(mBox.GetFace(i).mVertices[j]);
|
---|
1405 |
|
---|
1406 | candidatePolys.push_back(new Polygon3(vertices));
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | for (int i = 0; i < (int)candidatePolys.size(); ++ i)
|
---|
1410 | {
|
---|
1411 | // polygon is split by all other planes
|
---|
1412 | for (int j = 0; (j < (int)halfSpaces.size()) && candidatePolys[i]; ++ j)
|
---|
1413 | {
|
---|
1414 | if (i == j) // polygon and plane are coincident
|
---|
1415 | continue;
|
---|
1416 |
|
---|
1417 | VertexContainer splitPts;
|
---|
1418 | Polygon3 *frontPoly, *backPoly;
|
---|
1419 |
|
---|
1420 | const int cf =
|
---|
1421 | candidatePolys[i]->ClassifyPlane(halfSpaces[j],
|
---|
1422 | mEpsilon);
|
---|
1423 |
|
---|
1424 | switch (cf)
|
---|
1425 | {
|
---|
1426 | case Polygon3::SPLIT:
|
---|
1427 | frontPoly = new Polygon3();
|
---|
1428 | backPoly = new Polygon3();
|
---|
1429 |
|
---|
1430 | candidatePolys[i]->Split(halfSpaces[j],
|
---|
1431 | *frontPoly,
|
---|
1432 | *backPoly,
|
---|
1433 | mEpsilon);
|
---|
1434 |
|
---|
1435 | DEL_PTR(candidatePolys[i]);
|
---|
1436 |
|
---|
1437 | if (frontPoly->Valid(mEpsilon))
|
---|
1438 | candidatePolys[i] = frontPoly;
|
---|
1439 | else
|
---|
1440 | DEL_PTR(frontPoly);
|
---|
1441 |
|
---|
1442 | DEL_PTR(backPoly);
|
---|
1443 | break;
|
---|
1444 | case Polygon3::BACK_SIDE:
|
---|
1445 | DEL_PTR(candidatePolys[i]);
|
---|
1446 | break;
|
---|
1447 | // just take polygon as it is
|
---|
1448 | case Polygon3::FRONT_SIDE:
|
---|
1449 | case Polygon3::COINCIDENT:
|
---|
1450 | default:
|
---|
1451 | break;
|
---|
1452 | }
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | if (candidatePolys[i])
|
---|
1456 | cell.push_back(candidatePolys[i]);
|
---|
1457 | }
|
---|
1458 | } |
---|
1459 | |
---|
1460 | void VspBspTree::ConstructGeometry(BspViewCell *vc, PolygonContainer &vcGeom) const |
---|
1461 | { |
---|
1462 | vector<BspLeaf *> leaves = vc->mBspLeaves; |
---|
1463 | |
---|
1464 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end(); |
---|
1465 | |
---|
1466 | for (it = leaves.begin(); it != it_end; ++ it) |
---|
1467 | ConstructGeometry(*it, vcGeom); |
---|
1468 | } |
---|
1469 | |
---|
1470 | int VspBspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors, |
---|
1471 | const bool onlyUnmailed) const |
---|
1472 | { |
---|
1473 | PolygonContainer cell; |
---|
1474 | |
---|
1475 | ConstructGeometry(n, cell); |
---|
1476 | |
---|
1477 | stack<BspNode *> nodeStack; |
---|
1478 | nodeStack.push(mRoot); |
---|
1479 | |
---|
1480 | // planes needed to verify that we found neighbor leaf. |
---|
1481 | vector<Plane3> halfSpaces; |
---|
1482 | ExtractHalfSpaces(n, halfSpaces); |
---|
1483 | |
---|
1484 | while (!nodeStack.empty()) |
---|
1485 | { |
---|
1486 | BspNode *node = nodeStack.top(); |
---|
1487 | nodeStack.pop(); |
---|
1488 | |
---|
1489 | if (node->IsLeaf()) |
---|
1490 | { |
---|
1491 | if (node != n && (!onlyUnmailed || !node->Mailed())) |
---|
1492 | { |
---|
1493 | // test all planes of current node if candidate really |
---|
1494 | // is neighbour |
---|
1495 | PolygonContainer neighborCandidate; |
---|
1496 | ConstructGeometry(node, neighborCandidate); |
---|
1497 | |
---|
1498 | bool isAdjacent = true; |
---|
1499 | for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i) |
---|
1500 | { |
---|
1501 | const int cf = |
---|
1502 | Polygon3::ClassifyPlane(neighborCandidate, |
---|
1503 | halfSpaces[i], |
---|
1504 | mEpsilon); |
---|
1505 | |
---|
1506 | if (cf == Polygon3::BACK_SIDE) |
---|
1507 | isAdjacent = false; |
---|
1508 | } |
---|
1509 | |
---|
1510 | if (isAdjacent) |
---|
1511 | neighbors.push_back(dynamic_cast<BspLeaf *>(node)); |
---|
1512 | |
---|
1513 | CLEAR_CONTAINER(neighborCandidate); |
---|
1514 | } |
---|
1515 | } |
---|
1516 | else |
---|
1517 | { |
---|
1518 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
1519 | |
---|
1520 | const int cf = Polygon3::ClassifyPlane(cell, |
---|
1521 | interior->GetPlane(), |
---|
1522 | mEpsilon); |
---|
1523 | |
---|
1524 | if (cf == Polygon3::FRONT_SIDE) |
---|
1525 | nodeStack.push(interior->GetFront()); |
---|
1526 | else |
---|
1527 | if (cf == Polygon3::BACK_SIDE) |
---|
1528 | nodeStack.push(interior->GetBack()); |
---|
1529 | else |
---|
1530 | { |
---|
1531 | // random decision |
---|
1532 | nodeStack.push(interior->GetBack()); |
---|
1533 | nodeStack.push(interior->GetFront()); |
---|
1534 | } |
---|
1535 | } |
---|
1536 | } |
---|
1537 | |
---|
1538 | CLEAR_CONTAINER(cell); |
---|
1539 | return (int)neighbors.size(); |
---|
1540 | } |
---|
1541 | |
---|
1542 | BspLeaf *VspBspTree::GetRandomLeaf(const Plane3 &halfspace) |
---|
1543 | { |
---|
1544 | stack<BspNode *> nodeStack; |
---|
1545 | nodeStack.push(mRoot); |
---|
1546 | |
---|
1547 | int mask = rand(); |
---|
1548 | |
---|
1549 | while (!nodeStack.empty()) |
---|
1550 | { |
---|
1551 | BspNode *node = nodeStack.top(); |
---|
1552 | nodeStack.pop(); |
---|
1553 | |
---|
1554 | if (node->IsLeaf()) |
---|
1555 | { |
---|
1556 | return dynamic_cast<BspLeaf *>(node); |
---|
1557 | } |
---|
1558 | else |
---|
1559 | { |
---|
1560 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
1561 | |
---|
1562 | BspNode *next; |
---|
1563 | |
---|
1564 | PolygonContainer cell; |
---|
1565 | |
---|
1566 | // todo: not very efficient: constructs full cell everytime |
---|
1567 | ConstructGeometry(interior, cell); |
---|
1568 | |
---|
1569 | const int cf = Polygon3::ClassifyPlane(cell, halfspace, mEpsilon); |
---|
1570 | |
---|
1571 | if (cf == Polygon3::BACK_SIDE) |
---|
1572 | next = interior->GetFront(); |
---|
1573 | else |
---|
1574 | if (cf == Polygon3::FRONT_SIDE) |
---|
1575 | next = interior->GetFront(); |
---|
1576 | else |
---|
1577 | { |
---|
1578 | // random decision |
---|
1579 | if (mask & 1) |
---|
1580 | next = interior->GetBack(); |
---|
1581 | else |
---|
1582 | next = interior->GetFront(); |
---|
1583 | mask = mask >> 1; |
---|
1584 | } |
---|
1585 | |
---|
1586 | nodeStack.push(next); |
---|
1587 | } |
---|
1588 | } |
---|
1589 | |
---|
1590 | return NULL; |
---|
1591 | } |
---|
1592 | |
---|
1593 | BspLeaf *VspBspTree::GetRandomLeaf(const bool onlyUnmailed) |
---|
1594 | { |
---|
1595 | stack<BspNode *> nodeStack; |
---|
1596 | |
---|
1597 | nodeStack.push(mRoot); |
---|
1598 | |
---|
1599 | int mask = rand(); |
---|
1600 | |
---|
1601 | while (!nodeStack.empty()) |
---|
1602 | { |
---|
1603 | BspNode *node = nodeStack.top(); |
---|
1604 | nodeStack.pop(); |
---|
1605 | |
---|
1606 | if (node->IsLeaf()) |
---|
1607 | { |
---|
1608 | if ( (!onlyUnmailed || !node->Mailed()) ) |
---|
1609 | return dynamic_cast<BspLeaf *>(node); |
---|
1610 | } |
---|
1611 | else |
---|
1612 | { |
---|
1613 | BspInterior *interior = dynamic_cast<BspInterior *>(node); |
---|
1614 | |
---|
1615 | // random decision |
---|
1616 | if (mask & 1) |
---|
1617 | nodeStack.push(interior->GetBack()); |
---|
1618 | else |
---|
1619 | nodeStack.push(interior->GetFront()); |
---|
1620 | |
---|
1621 | mask = mask >> 1; |
---|
1622 | } |
---|
1623 | } |
---|
1624 | |
---|
1625 | return NULL; |
---|
1626 | } |
---|
1627 | |
---|
1628 | int VspBspTree::ComputePvsSize(const RayInfoContainer &rays) const |
---|
1629 | { |
---|
1630 | int pvsSize = 0; |
---|
1631 | |
---|
1632 | RayInfoContainer::const_iterator rit, rit_end = rays.end(); |
---|
1633 | |
---|
1634 | Intersectable::NewMail(); |
---|
1635 | |
---|
1636 | for (rit = rays.begin(); rit != rays.end(); ++ rit) |
---|
1637 | { |
---|
1638 | VssRay *ray = (*rit).mRay; |
---|
1639 | |
---|
1640 | if (ray->mOriginObject) |
---|
1641 | { |
---|
1642 | if (!ray->mOriginObject->Mailed()) |
---|
1643 | { |
---|
1644 | ray->mOriginObject->Mail(); |
---|
1645 | ++ pvsSize; |
---|
1646 | } |
---|
1647 | } |
---|
1648 | if (ray->mTerminationObject) |
---|
1649 | { |
---|
1650 | if (!ray->mTerminationObject->Mailed()) |
---|
1651 | { |
---|
1652 | ray->mTerminationObject->Mail(); |
---|
1653 | ++ pvsSize; |
---|
1654 | } |
---|
1655 | } |
---|
1656 | } |
---|
1657 | |
---|
1658 | return pvsSize; |
---|
1659 | } |
---|
1660 | |
---|
1661 | float VspBspTree::GetEpsilon() const |
---|
1662 | { |
---|
1663 | return mEpsilon; |
---|
1664 | } |
---|
1665 | |
---|
1666 | BspViewCell *VspBspTree::GetRootCell() const |
---|
1667 | { |
---|
1668 | return mRootCell; |
---|
1669 | } |
---|
1670 | |
---|
1671 | int VspBspTree::SplitPolygons(const Plane3 &plane, |
---|
1672 | PolygonContainer &polys, |
---|
1673 | PolygonContainer &frontPolys, |
---|
1674 | PolygonContainer &backPolys, |
---|
1675 | PolygonContainer &coincident) const |
---|
1676 | { |
---|
1677 | int splits = 0; |
---|
1678 | |
---|
1679 | while (!polys.empty()) |
---|
1680 | { |
---|
1681 | Polygon3 *poly = polys.back(); |
---|
1682 | polys.pop_back(); |
---|
1683 | |
---|
1684 | // classify polygon |
---|
1685 | const int cf = poly->ClassifyPlane(plane, mEpsilon); |
---|
1686 | |
---|
1687 | switch (cf) |
---|
1688 | { |
---|
1689 | case Polygon3::COINCIDENT: |
---|
1690 | coincident.push_back(poly); |
---|
1691 | break; |
---|
1692 | case Polygon3::FRONT_SIDE: |
---|
1693 | frontPolys.push_back(poly); |
---|
1694 | break; |
---|
1695 | case Polygon3::BACK_SIDE: |
---|
1696 | backPolys.push_back(poly); |
---|
1697 | break; |
---|
1698 | case Polygon3::SPLIT: |
---|
1699 | backPolys.push_back(poly); |
---|
1700 | frontPolys.push_back(poly); |
---|
1701 | ++ splits; |
---|
1702 | break; |
---|
1703 | default: |
---|
1704 | Debug << "SHOULD NEVER COME HERE\n"; |
---|
1705 | break; |
---|
1706 | } |
---|
1707 | } |
---|
1708 | |
---|
1709 | return splits; |
---|
1710 | } |
---|