1 | #include "Plane3.h"
|
---|
2 | #include "ViewCellBsp.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 "Triangle3.h"
|
---|
11 | #include "Tetrahedron3.h"
|
---|
12 | #include "ViewCellsManager.h"
|
---|
13 | #include "Exporter.h"
|
---|
14 | #include "Plane3.h"
|
---|
15 | #include <stack>
|
---|
16 |
|
---|
17 | namespace GtpVisibilityPreprocessor {
|
---|
18 |
|
---|
19 | //////////////
|
---|
20 | //-- static members
|
---|
21 |
|
---|
22 | int BspNode::sMailId = 1;
|
---|
23 |
|
---|
24 | /** Evaluates split plane classification with respect to the plane's
|
---|
25 | contribution for a minimum number splits in the tree.
|
---|
26 | */
|
---|
27 | const float BspTree::sLeastPolySplitsTable[] = {0, 0, 1, 0};
|
---|
28 | /** Evaluates split plane classification with respect to the plane's
|
---|
29 | contribution for a balanced tree.
|
---|
30 | */
|
---|
31 | const float BspTree::sBalancedPolysTable[] = {1, -1, 0, 0};
|
---|
32 |
|
---|
33 | /** Evaluates split plane classification with respect to the plane's
|
---|
34 | contribution for a minimum number of ray splits.
|
---|
35 | */
|
---|
36 | const float BspTree::sLeastRaySplitsTable[] = {0, 0, 1, 1, 0};
|
---|
37 | /** Evaluates split plane classification with respect to the plane's
|
---|
38 | contribution for balanced rays.
|
---|
39 | */
|
---|
40 | const float BspTree::sBalancedRaysTable[] = {1, -1, 0, 0, 0};
|
---|
41 |
|
---|
42 | int BspTree::sFrontId = 0;
|
---|
43 | int BspTree::sBackId = 0;
|
---|
44 | int BspTree::sFrontAndBackId = 0;
|
---|
45 |
|
---|
46 |
|
---|
47 | /******************************************************************/
|
---|
48 | /* class BspNode implementation */
|
---|
49 | /******************************************************************/
|
---|
50 |
|
---|
51 |
|
---|
52 | BspNode::BspNode():
|
---|
53 | mParent(NULL), mTreeValid(true), mTimeStamp(0)
|
---|
54 | {}
|
---|
55 |
|
---|
56 |
|
---|
57 | BspNode::BspNode(BspInterior *parent):
|
---|
58 | mParent(parent), mTreeValid(true)
|
---|
59 | {}
|
---|
60 |
|
---|
61 |
|
---|
62 | bool BspNode::IsSibling(BspNode *n) const
|
---|
63 | {
|
---|
64 | return ((this != n) && mParent &&
|
---|
65 | (mParent->GetFront() == n) || (mParent->GetBack() == n));
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | int BspNode::GetDepth() const
|
---|
70 | {
|
---|
71 | int depth = 0;
|
---|
72 | BspNode *p = mParent;
|
---|
73 |
|
---|
74 | while (p)
|
---|
75 | {
|
---|
76 | p = p->mParent;
|
---|
77 | ++ depth;
|
---|
78 | }
|
---|
79 |
|
---|
80 | return depth;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | bool BspNode::TreeValid() const
|
---|
85 | {
|
---|
86 | return mTreeValid;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | void BspNode::SetTreeValid(const bool v)
|
---|
91 | {
|
---|
92 | mTreeValid = v;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /****************************************************************/
|
---|
97 | /* class BspInterior implementation */
|
---|
98 | /****************************************************************/
|
---|
99 |
|
---|
100 |
|
---|
101 | BspInterior::BspInterior(const Plane3 &plane):
|
---|
102 | mPlane(plane), mFront(NULL), mBack(NULL)
|
---|
103 | {}
|
---|
104 |
|
---|
105 |
|
---|
106 | BspInterior::~BspInterior()
|
---|
107 | {
|
---|
108 | DEL_PTR(mFront);
|
---|
109 | DEL_PTR(mBack);
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | void BspInterior::SetupChildLinks(BspNode *b, BspNode *f)
|
---|
114 | {
|
---|
115 | mBack = b;
|
---|
116 | mFront = f;
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | /****************************************************************/
|
---|
121 | /* class BspLeaf implementation */
|
---|
122 | /****************************************************************/
|
---|
123 |
|
---|
124 |
|
---|
125 | BspLeaf::BspLeaf(): mViewCell(NULL), mPvs(NULL)
|
---|
126 | {
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | BspLeaf::~BspLeaf()
|
---|
131 | {
|
---|
132 | DEL_PTR(mPvs);
|
---|
133 |
|
---|
134 | VssRayContainer::const_iterator vit, vit_end = mVssRays.end();
|
---|
135 | for (vit = mVssRays.begin(); vit != vit_end; ++ vit)
|
---|
136 | {
|
---|
137 | VssRay *ray = *vit;
|
---|
138 | ray->Unref();
|
---|
139 |
|
---|
140 | if (!ray->IsActive())
|
---|
141 | delete ray;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | BspLeaf::BspLeaf(ViewCellLeaf *viewCell):
|
---|
147 | mViewCell(viewCell)
|
---|
148 | {
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | BspLeaf::BspLeaf(BspInterior *parent):
|
---|
153 | BspNode(parent), mViewCell(NULL), mPvs(NULL)
|
---|
154 | {}
|
---|
155 |
|
---|
156 |
|
---|
157 |
|
---|
158 | BspLeaf::BspLeaf(BspInterior *parent, ViewCellLeaf *viewCell):
|
---|
159 | BspNode(parent), mViewCell(viewCell), mPvs(NULL)
|
---|
160 | {
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | /*********************************************************************/
|
---|
165 | /* class BspTree implementation */
|
---|
166 | /*********************************************************************/
|
---|
167 |
|
---|
168 | BspTree::BspTree():
|
---|
169 | mRoot(NULL),
|
---|
170 | mUseAreaForPvs(false),
|
---|
171 | mUsePredefinedViewCells(false),
|
---|
172 | mTimeStamp(1),
|
---|
173 | mViewCellsTree(NULL),
|
---|
174 | mOutOfBoundsCellPartOfTree(false),
|
---|
175 | mOutOfBoundsCell(NULL)
|
---|
176 | {
|
---|
177 | Randomize(); // initialise random generator for heuristics
|
---|
178 | mOutOfBoundsCell = GetOrCreateOutOfBoundsCell();
|
---|
179 |
|
---|
180 | /////////
|
---|
181 | //-- termination criteria for autopartition
|
---|
182 |
|
---|
183 | Environment::GetSingleton()->GetIntValue("BspTree.Termination.maxDepth", mTermMaxDepth);
|
---|
184 | Environment::GetSingleton()->GetIntValue("BspTree.Termination.minPvs", mTermMinPvs);
|
---|
185 | Environment::GetSingleton()->GetIntValue("BspTree.Termination.minPolygons", mTermMinPolys);
|
---|
186 | Environment::GetSingleton()->GetIntValue("BspTree.Termination.minRays", mTermMinRays);
|
---|
187 | Environment::GetSingleton()->GetFloatValue("BspTree.Termination.minProbability", mTermMinProbability);
|
---|
188 | Environment::GetSingleton()->GetFloatValue("BspTree.Termination.maxRayContribution", mTermMaxRayContribution);
|
---|
189 | Environment::GetSingleton()->GetFloatValue("BspTree.Termination.minAccRayLenght", mTermMinAccRayLength);
|
---|
190 |
|
---|
191 |
|
---|
192 | /////////
|
---|
193 | //-- factors for bsp tree split plane heuristics
|
---|
194 |
|
---|
195 | Environment::GetSingleton()->GetFloatValue("BspTree.Factor.verticalSplits", mVerticalSplitsFactor);
|
---|
196 | Environment::GetSingleton()->GetFloatValue("BspTree.Factor.largestPolyArea", mLargestPolyAreaFactor);
|
---|
197 | Environment::GetSingleton()->GetFloatValue("BspTree.Factor.blockedRays", mBlockedRaysFactor);
|
---|
198 | Environment::GetSingleton()->GetFloatValue("BspTree.Factor.leastRaySplits", mLeastRaySplitsFactor);
|
---|
199 | Environment::GetSingleton()->GetFloatValue("BspTree.Factor.balancedRays", mBalancedRaysFactor);
|
---|
200 | Environment::GetSingleton()->GetFloatValue("BspTree.Factor.pvs", mPvsFactor);
|
---|
201 | Environment::GetSingleton()->GetFloatValue("BspTree.Factor.leastSplits" , mLeastSplitsFactor);
|
---|
202 | Environment::GetSingleton()->GetFloatValue("BspTree.Factor.balancedPolys", mBalancedPolysFactor);
|
---|
203 | Environment::GetSingleton()->GetFloatValue("BspTree.Factor.balancedViewCells", mBalancedViewCellsFactor);
|
---|
204 | Environment::GetSingleton()->GetFloatValue("BspTree.Termination.ct_div_ci", mCtDivCi);
|
---|
205 |
|
---|
206 |
|
---|
207 | ///////////
|
---|
208 | //-- termination criteria for axis aligned split
|
---|
209 |
|
---|
210 | Environment::GetSingleton()->GetFloatValue("BspTree.Termination.AxisAligned.ct_div_ci", mAxisAlignedCtDivCi);
|
---|
211 | Environment::GetSingleton()->GetFloatValue("BspTree.Termination.maxCostRatio", mMaxCostRatio);
|
---|
212 | Environment::GetSingleton()->GetIntValue("BspTree.Termination.AxisAligned.minPolys",
|
---|
213 | mTermMinPolysForAxisAligned);
|
---|
214 | Environment::GetSingleton()->GetIntValue("BspTree.Termination.AxisAligned.minRays",
|
---|
215 | mTermMinRaysForAxisAligned);
|
---|
216 | Environment::GetSingleton()->GetIntValue("BspTree.Termination.AxisAligned.minObjects",
|
---|
217 | mTermMinObjectsForAxisAligned);
|
---|
218 |
|
---|
219 |
|
---|
220 | //////////////
|
---|
221 | //-- partition criteria
|
---|
222 |
|
---|
223 | Environment::GetSingleton()->GetIntValue("BspTree.maxPolyCandidates", mMaxPolyCandidates);
|
---|
224 | Environment::GetSingleton()->GetIntValue("BspTree.maxRayCandidates", mMaxRayCandidates);
|
---|
225 | Environment::GetSingleton()->GetIntValue("BspTree.splitPlaneStrategy", mSplitPlaneStrategy);
|
---|
226 | Environment::GetSingleton()->GetFloatValue("BspTree.AxisAligned.splitBorder", mSplitBorder);
|
---|
227 | Environment::GetSingleton()->GetIntValue("BspTree.maxTests", mMaxTests);
|
---|
228 | Environment::GetSingleton()->GetIntValue("BspTree.Termination.maxViewCells", mMaxViewCells);
|
---|
229 |
|
---|
230 | Environment::GetSingleton()->GetFloatValue("BspTree.Construction.epsilon", mEpsilon);
|
---|
231 |
|
---|
232 | char subdivisionStatsLog[100];
|
---|
233 | Environment::GetSingleton()->GetStringValue("BspTree.subdivisionStats", subdivisionStatsLog);
|
---|
234 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
235 |
|
---|
236 | /////////////////////7
|
---|
237 |
|
---|
238 | Debug << "BSP options: " << endl;
|
---|
239 | Debug << "BSP max depth: " << mTermMaxDepth << endl;
|
---|
240 | Debug << "BSP min PVS: " << mTermMinPvs << endl;
|
---|
241 | Debug << "BSP min probability: " << mTermMinProbability << endl;
|
---|
242 | Debug << "BSP max polys: " << mTermMinPolys << endl;
|
---|
243 | Debug << "BSP max rays: " << mTermMinRays << endl;
|
---|
244 | Debug << "BSP max polygon candidates: " << mMaxPolyCandidates << endl;
|
---|
245 | Debug << "BSP max plane candidates: " << mMaxRayCandidates << endl;
|
---|
246 |
|
---|
247 | Debug << "Split plane strategy: ";
|
---|
248 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
249 | Debug << "random polygon ";
|
---|
250 | if (mSplitPlaneStrategy & AXIS_ALIGNED)
|
---|
251 | Debug << "axis aligned ";
|
---|
252 | if (mSplitPlaneStrategy & LEAST_SPLITS)
|
---|
253 | Debug << "least splits ";
|
---|
254 | if (mSplitPlaneStrategy & BALANCED_POLYS)
|
---|
255 | Debug << "balanced polygons ";
|
---|
256 | if (mSplitPlaneStrategy & BALANCED_VIEW_CELLS)
|
---|
257 | Debug << "balanced view cells ";
|
---|
258 | if (mSplitPlaneStrategy & LARGEST_POLY_AREA)
|
---|
259 | Debug << "largest polygon area ";
|
---|
260 | if (mSplitPlaneStrategy & VERTICAL_AXIS)
|
---|
261 | Debug << "vertical axis ";
|
---|
262 | if (mSplitPlaneStrategy & BLOCKED_RAYS)
|
---|
263 | Debug << "blocked rays ";
|
---|
264 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
265 | Debug << "least ray splits ";
|
---|
266 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
267 | Debug << "balanced rays ";
|
---|
268 | if (mSplitPlaneStrategy & PVS)
|
---|
269 | Debug << "pvs";
|
---|
270 |
|
---|
271 | Debug << endl;
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | bool BspTree::IsOutOfBounds(ViewCell *vc) const
|
---|
276 | {
|
---|
277 | return vc->GetId() == OUT_OF_BOUNDS_ID;
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | const BspTreeStatistics &BspTree::GetStatistics() const
|
---|
282 | {
|
---|
283 | return mStat;
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | BspViewCell *BspTree::GetOrCreateOutOfBoundsCell()
|
---|
288 | {
|
---|
289 | if (!mOutOfBoundsCell)
|
---|
290 | {
|
---|
291 | mOutOfBoundsCell = new BspViewCell();
|
---|
292 | mOutOfBoundsCell->SetId(OUT_OF_BOUNDS_ID);
|
---|
293 | mOutOfBoundsCell->SetValid(false);
|
---|
294 | }
|
---|
295 |
|
---|
296 | return mOutOfBoundsCell;
|
---|
297 | }
|
---|
298 |
|
---|
299 | int BspTree::SplitPolygons(const Plane3 &plane,
|
---|
300 | PolygonContainer &polys,
|
---|
301 | PolygonContainer &frontPolys,
|
---|
302 | PolygonContainer &backPolys,
|
---|
303 | PolygonContainer &coincident) const
|
---|
304 | {
|
---|
305 | int splits = 0;
|
---|
306 |
|
---|
307 | #ifdef _Debug
|
---|
308 | Debug << "splitting polygons of node " << this << " with plane " << mPlane << endl;
|
---|
309 | #endif
|
---|
310 |
|
---|
311 | PolygonContainer::const_iterator it, it_end = polys.end();
|
---|
312 |
|
---|
313 | for (it = polys.begin(); it != polys.end(); ++ it)
|
---|
314 | {
|
---|
315 | Polygon3 *poly = *it;
|
---|
316 |
|
---|
317 | //-- classify polygon
|
---|
318 | const int cf = poly->ClassifyPlane(plane, mEpsilon);
|
---|
319 |
|
---|
320 | switch (cf)
|
---|
321 | {
|
---|
322 | case Polygon3::COINCIDENT:
|
---|
323 | coincident.push_back(poly);
|
---|
324 | break;
|
---|
325 | case Polygon3::FRONT_SIDE:
|
---|
326 | frontPolys.push_back(poly);
|
---|
327 | break;
|
---|
328 | case Polygon3::BACK_SIDE:
|
---|
329 | backPolys.push_back(poly);
|
---|
330 | break;
|
---|
331 | case Polygon3::SPLIT:
|
---|
332 | {
|
---|
333 | Polygon3 *front_piece = new Polygon3(poly->mParent);
|
---|
334 | Polygon3 *back_piece = new Polygon3(poly->mParent);
|
---|
335 |
|
---|
336 | //-- split polygon into front and back part
|
---|
337 | poly->Split(plane,
|
---|
338 | *front_piece,
|
---|
339 | *back_piece,
|
---|
340 | mEpsilon);
|
---|
341 |
|
---|
342 | ++ splits; // increase number of splits
|
---|
343 |
|
---|
344 | //-- inherit rays from parent polygon for blocked ray criterium
|
---|
345 | poly->InheritRays(*front_piece, *back_piece);
|
---|
346 |
|
---|
347 | // check if polygons still valid
|
---|
348 | if (front_piece->Valid(mEpsilon))
|
---|
349 | frontPolys.push_back(front_piece);
|
---|
350 | else
|
---|
351 | DEL_PTR(front_piece);
|
---|
352 |
|
---|
353 | if (back_piece->Valid(mEpsilon))
|
---|
354 | backPolys.push_back(back_piece);
|
---|
355 | else
|
---|
356 | DEL_PTR(back_piece);
|
---|
357 |
|
---|
358 | #ifdef GTP_DEBUG
|
---|
359 | Debug << "split " << *poly << endl << *front_piece << endl << *back_piece << endl;
|
---|
360 | #endif
|
---|
361 | DEL_PTR(poly);
|
---|
362 | }
|
---|
363 | break;
|
---|
364 | default:
|
---|
365 | Debug << "SHOULD NEVER COME HERE\n";
|
---|
366 | break;
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | return splits;
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 | void BspTreeStatistics::Print(ostream &app) const
|
---|
375 | {
|
---|
376 | app << "===== BspTree statistics ===============\n";
|
---|
377 |
|
---|
378 | app << setprecision(4);
|
---|
379 |
|
---|
380 | app << "#N_CTIME ( Construction time [s] )\n" << Time() << " \n";
|
---|
381 |
|
---|
382 | app << "#N_NODES ( Number of nodes )\n" << nodes << "\n";
|
---|
383 |
|
---|
384 | app << "#N_INTERIORS ( Number of interior nodes )\n" << Interior() << "\n";
|
---|
385 |
|
---|
386 | app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n";
|
---|
387 |
|
---|
388 | app << "#N_POLYSPLITS ( Number of polygon splits )\n" << polySplits << "\n";
|
---|
389 |
|
---|
390 | app << "#AXIS_ALIGNED_SPLITS (number of axis aligned splits)\n" << splits[0] + splits[1] + splits[2] << endl;
|
---|
391 |
|
---|
392 | app << "#N_SPLITS ( Number of splits in axes x y z)\n";
|
---|
393 |
|
---|
394 | for (int i = 0; i < 3; ++ i)
|
---|
395 | app << splits[i] << " ";
|
---|
396 | app << endl;
|
---|
397 |
|
---|
398 | app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maximum depth )\n"
|
---|
399 | << maxDepthNodes * 100 / (double)Leaves() << endl;
|
---|
400 |
|
---|
401 | app << "#N_PMINPVSLEAVES ( Percentage of leaves with mininimal PVS )\n"
|
---|
402 | << minPvsNodes * 100 / (double)Leaves() << endl;
|
---|
403 |
|
---|
404 | app << "#N_PMINRAYSLEAVES ( Percentage of leaves with minimal number of rays)\n"
|
---|
405 | << minRaysNodes * 100 / (double)Leaves() << endl;
|
---|
406 |
|
---|
407 | app << "#N_MAXCOSTNODES ( Percentage of leaves with terminated because of max cost ratio )\n"
|
---|
408 | << maxCostNodes * 100 / (double)Leaves() << endl;
|
---|
409 |
|
---|
410 | app << "#N_PMINPROBABILITYLEAVES ( Percentage of leaves with mininum probability )\n"
|
---|
411 | << minProbabilityNodes * 100 / (double)Leaves() << endl;
|
---|
412 |
|
---|
413 | app << "#N_PMAXRAYCONTRIBLEAVES ( Percentage of leaves with maximal ray contribution )\n"
|
---|
414 | << maxRayContribNodes * 100 / (double)Leaves() << endl;
|
---|
415 |
|
---|
416 | app << "#N_PMAXDEPTH ( Maximal reached depth )\n" << maxDepth << endl;
|
---|
417 |
|
---|
418 | app << "#N_PMINDEPTH ( Minimal reached depth )\n" << minDepth << endl;
|
---|
419 |
|
---|
420 | app << "#AVGDEPTH ( average depth )\n" << AvgDepth() << endl;
|
---|
421 |
|
---|
422 | app << "#N_INPUTPOLYGONS (number of input polygons )\n" << polys << endl;
|
---|
423 |
|
---|
424 | app << "#N_INVALIDLEAVES (number of invalid leaves )\n" << invalidLeaves << endl;
|
---|
425 |
|
---|
426 | app << "#N_RAYS (number of rays / leaf)\n" << AvgRays() << endl;
|
---|
427 | //app << "#N_PVS: " << pvs << endl;
|
---|
428 |
|
---|
429 | app << "#N_ROUTPUT_INPUT_POLYGONS ( ratio polygons after subdivision / input polygons )\n" <<
|
---|
430 | (polys + polySplits) / (double)polys << endl;
|
---|
431 |
|
---|
432 | app << "===== END OF BspTree statistics ==========\n";
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 | BspTree::~BspTree()
|
---|
437 | {
|
---|
438 | DEL_PTR(mRoot);
|
---|
439 |
|
---|
440 | if (!mOutOfBoundsCellPartOfTree)
|
---|
441 | {
|
---|
442 | // out of bounds cell not part of tree =>
|
---|
443 | // delete manually
|
---|
444 | DEL_PTR(mOutOfBoundsCell);
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 |
|
---|
449 | BspNode *BspTree::GetRoot() const
|
---|
450 | {
|
---|
451 | return mRoot;
|
---|
452 | }
|
---|
453 |
|
---|
454 |
|
---|
455 | void BspTree::SetViewCellsTree(ViewCellsTree *viewCellsTree)
|
---|
456 | {
|
---|
457 | mViewCellsTree = viewCellsTree;
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | void BspTree::InsertViewCell(ViewCellLeaf *viewCell)
|
---|
462 | {
|
---|
463 | // don't generate new view cell, insert this view cell
|
---|
464 | mUsePredefinedViewCells = true;
|
---|
465 | PolygonContainer *polys = new PolygonContainer();
|
---|
466 |
|
---|
467 | // extract polygons that guide the split process
|
---|
468 | mStat.polys += AddMeshToPolygons(viewCell->GetMesh(), *polys, viewCell);
|
---|
469 | mBbox.Include(viewCell->GetBox()); // add to BSP aabb
|
---|
470 |
|
---|
471 | InsertPolygons(polys);
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | void BspTree::InsertPolygons(PolygonContainer *polys)
|
---|
476 | {
|
---|
477 | BspTraversalStack tStack;
|
---|
478 |
|
---|
479 | // traverse existing tree or create new tree
|
---|
480 | if (!mRoot)
|
---|
481 | mRoot = new BspLeaf();
|
---|
482 |
|
---|
483 | tStack.push(BspTraversalData(mRoot,
|
---|
484 | polys,
|
---|
485 | 0,
|
---|
486 | mOutOfBoundsCell,
|
---|
487 | new BoundedRayContainer(),
|
---|
488 | 0,
|
---|
489 | mUseAreaForPvs ? mBbox.SurfaceArea() : mBbox.GetVolume(),
|
---|
490 | new BspNodeGeometry()));
|
---|
491 |
|
---|
492 | while (!tStack.empty())
|
---|
493 | {
|
---|
494 | // filter polygons donw the tree
|
---|
495 | BspTraversalData tData = tStack.top();
|
---|
496 | tStack.pop();
|
---|
497 |
|
---|
498 | if (!tData.mNode->IsLeaf())
|
---|
499 | {
|
---|
500 | BspInterior *interior = static_cast<BspInterior *>(tData.mNode);
|
---|
501 |
|
---|
502 | ///////////////////
|
---|
503 | //-- filter view cell polygons down the tree until a leaf is reached
|
---|
504 | if (!tData.mPolygons->empty())
|
---|
505 | {
|
---|
506 | PolygonContainer *frontPolys = new PolygonContainer();
|
---|
507 | PolygonContainer *backPolys = new PolygonContainer();
|
---|
508 | PolygonContainer coincident;
|
---|
509 |
|
---|
510 | int splits = 0;
|
---|
511 |
|
---|
512 | // split viewcell polygons with respect to split plane
|
---|
513 | splits += SplitPolygons(interior->GetPlane(),
|
---|
514 | *tData.mPolygons,
|
---|
515 | *frontPolys,
|
---|
516 | *backPolys,
|
---|
517 | coincident);
|
---|
518 |
|
---|
519 | // extract view cells associated with the split polygons
|
---|
520 | ViewCellLeaf *frontViewCell = mOutOfBoundsCell;
|
---|
521 | ViewCellLeaf *backViewCell = mOutOfBoundsCell;
|
---|
522 |
|
---|
523 | BspTraversalData frontData(interior->GetFront(),
|
---|
524 | frontPolys,
|
---|
525 | tData.mDepth + 1,
|
---|
526 | mOutOfBoundsCell,
|
---|
527 | tData.mRays,
|
---|
528 | tData.mPvs,
|
---|
529 | mUseAreaForPvs ? mBbox.SurfaceArea() : mBbox.GetVolume(),
|
---|
530 | new BspNodeGeometry());
|
---|
531 |
|
---|
532 | BspTraversalData backData(interior->GetBack(),
|
---|
533 | backPolys,
|
---|
534 | tData.mDepth + 1,
|
---|
535 | mOutOfBoundsCell,
|
---|
536 | tData.mRays,
|
---|
537 | tData.mPvs,
|
---|
538 | mUseAreaForPvs ? mBbox.SurfaceArea() : mBbox.GetVolume(),
|
---|
539 | new BspNodeGeometry());
|
---|
540 |
|
---|
541 | if (mUsePredefinedViewCells)
|
---|
542 | {
|
---|
543 | ExtractViewCells(frontData,
|
---|
544 | backData,
|
---|
545 | coincident,
|
---|
546 | interior->mPlane);
|
---|
547 | }
|
---|
548 |
|
---|
549 | // don't need coincident polygons anymore
|
---|
550 | CLEAR_CONTAINER(coincident);
|
---|
551 |
|
---|
552 | mStat.polySplits += splits;
|
---|
553 |
|
---|
554 | // push the children on the stack
|
---|
555 | tStack.push(frontData);
|
---|
556 | tStack.push(backData);
|
---|
557 | }
|
---|
558 |
|
---|
559 | // cleanup
|
---|
560 | DEL_PTR(tData.mPolygons);
|
---|
561 | DEL_PTR(tData.mRays);
|
---|
562 | }
|
---|
563 | else
|
---|
564 | {
|
---|
565 | // reached leaf => subdivide current viewcell
|
---|
566 | BspNode *subRoot = Subdivide(tStack, tData);
|
---|
567 | }
|
---|
568 | }
|
---|
569 | }
|
---|
570 |
|
---|
571 |
|
---|
572 | int BspTree::AddMeshToPolygons(Mesh *mesh,
|
---|
573 | PolygonContainer &polys,
|
---|
574 | MeshInstance *parent)
|
---|
575 | {
|
---|
576 | FaceContainer::const_iterator fi, fi_end = mesh->mFaces.end();
|
---|
577 |
|
---|
578 | // copy the face data to polygons
|
---|
579 | for (fi = mesh->mFaces.begin(); fi != fi_end; ++ fi)
|
---|
580 | {
|
---|
581 | Polygon3 *poly = new Polygon3((*fi), mesh);
|
---|
582 |
|
---|
583 | if (poly->Valid(mEpsilon))
|
---|
584 | {
|
---|
585 | poly->mParent = parent; // set parent intersectable
|
---|
586 | polys.push_back(poly);
|
---|
587 | }
|
---|
588 | else
|
---|
589 | {
|
---|
590 | DEL_PTR(poly);
|
---|
591 | }
|
---|
592 | }
|
---|
593 | return (int)mesh->mFaces.size();
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 | int BspTree::AddToPolygonSoup(const ViewCellContainer &viewCells,
|
---|
598 | PolygonContainer &polys,
|
---|
599 | int maxObjects)
|
---|
600 | {
|
---|
601 | int limit = (maxObjects > 0) ?
|
---|
602 | Min((int)viewCells.size(), maxObjects) : (int)viewCells.size();
|
---|
603 |
|
---|
604 | int polysSize = 0;
|
---|
605 |
|
---|
606 | for (int i = 0; i < limit; ++ i)
|
---|
607 | {
|
---|
608 | Mesh *mesh = viewCells[i]->GetMesh();
|
---|
609 |
|
---|
610 | if (mesh)
|
---|
611 | {
|
---|
612 | // copy the mesh into polygons and add to BSP tree aabb
|
---|
613 | mBbox.Include(viewCells[i]->GetBox());
|
---|
614 | polysSize += AddMeshToPolygons(mesh, polys, viewCells[i]);
|
---|
615 | }
|
---|
616 | }
|
---|
617 |
|
---|
618 | return polysSize;
|
---|
619 | }
|
---|
620 |
|
---|
621 |
|
---|
622 | int BspTree::AddToPolygonSoup(const ObjectContainer &objects,
|
---|
623 | PolygonContainer &polys,
|
---|
624 | int maxObjects,
|
---|
625 | bool addToBbox)
|
---|
626 | {
|
---|
627 | int limit = (maxObjects > 0) ?
|
---|
628 | Min((int)objects.size(), maxObjects) : (int)objects.size();
|
---|
629 |
|
---|
630 | for (int i = 0; i < limit; ++i)
|
---|
631 | {
|
---|
632 | Intersectable *object = objects[i];
|
---|
633 | Mesh *mesh = NULL;
|
---|
634 |
|
---|
635 | switch (object->Type()) // extract the meshes
|
---|
636 | {
|
---|
637 | case Intersectable::MESH_INSTANCE:
|
---|
638 | mesh = static_cast<MeshInstance *>(object)->GetMesh();
|
---|
639 | break;
|
---|
640 | case Intersectable::VIEW_CELL:
|
---|
641 | mesh = static_cast<ViewCell *>(object)->GetMesh();
|
---|
642 | break;
|
---|
643 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
644 | {
|
---|
645 | TransformedMeshInstance *mi = static_cast<TransformedMeshInstance *>(object);
|
---|
646 |
|
---|
647 | if (!mi->GetMesh())
|
---|
648 | break;
|
---|
649 | mesh = new Mesh();
|
---|
650 | mi->GetTransformedMesh(*mesh);
|
---|
651 | break;
|
---|
652 | }
|
---|
653 | default:
|
---|
654 | Debug << "intersectable type not supported" << endl;
|
---|
655 | break;
|
---|
656 | }
|
---|
657 |
|
---|
658 | if (!mesh) continue;
|
---|
659 |
|
---|
660 | // copy the mesh data to polygons
|
---|
661 | if (addToBbox)
|
---|
662 | {
|
---|
663 | mBbox.Include(object->GetBox()); // add to BSP tree aabb
|
---|
664 | }
|
---|
665 |
|
---|
666 | AddMeshToPolygons(mesh, polys, mOutOfBoundsCell);
|
---|
667 |
|
---|
668 | // cleanup
|
---|
669 | if (object->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
|
---|
670 | {
|
---|
671 | DEL_PTR(mesh);
|
---|
672 | }
|
---|
673 | }
|
---|
674 |
|
---|
675 | return (int)polys.size();
|
---|
676 | }
|
---|
677 |
|
---|
678 |
|
---|
679 | void BspTree::Construct(const ViewCellContainer &viewCells)
|
---|
680 | {
|
---|
681 | // construct hierarchy over the given view cells
|
---|
682 | mUsePredefinedViewCells = true;
|
---|
683 |
|
---|
684 | mStat.nodes = 1;
|
---|
685 | mBbox.Initialize(); // initialise bsp tree bounding box
|
---|
686 |
|
---|
687 | // copy view cell meshes into one big polygon soup
|
---|
688 | PolygonContainer *polys = new PolygonContainer();
|
---|
689 | mStat.polys = AddToPolygonSoup(viewCells, *polys);
|
---|
690 |
|
---|
691 | Exporter *expo = Exporter::GetExporter("dummy2.wrl");
|
---|
692 | expo->ExportPolygons(*polys);
|
---|
693 | delete expo;
|
---|
694 | // construct tree from the view cell polygons
|
---|
695 | Construct(polys, new BoundedRayContainer());
|
---|
696 | }
|
---|
697 |
|
---|
698 |
|
---|
699 | void BspTree::Construct(const ObjectContainer &objects)
|
---|
700 | {
|
---|
701 | // generate new view cells for this type
|
---|
702 | mUsePredefinedViewCells = false;
|
---|
703 |
|
---|
704 | mStat.nodes = 1;
|
---|
705 | mBbox.Initialize(); // initialise bsp tree bounding box
|
---|
706 |
|
---|
707 | PolygonContainer *polys = new PolygonContainer();
|
---|
708 |
|
---|
709 | // copy mesh instance polygons into one big polygon soup
|
---|
710 | mStat.polys = AddToPolygonSoup(objects, *polys);
|
---|
711 |
|
---|
712 | // construct tree from polygon soup
|
---|
713 | Construct(polys, new BoundedRayContainer());
|
---|
714 | }
|
---|
715 |
|
---|
716 |
|
---|
717 | void BspTree::PreprocessPolygons(PolygonContainer &polys)
|
---|
718 | {
|
---|
719 | // preprocess: throw out polygons coincident to the view space box (not needed)
|
---|
720 | PolygonContainer boxPolys;
|
---|
721 | mBbox.ExtractPolys(boxPolys);
|
---|
722 | vector<Plane3> boxPlanes;
|
---|
723 |
|
---|
724 | PolygonContainer::iterator pit, pit_end = boxPolys.end();
|
---|
725 |
|
---|
726 | // extract planes of box
|
---|
727 | // TODO: can be done more elegantly than here
|
---|
728 | // where we first extract polygons, then compute their planes
|
---|
729 | for (pit = boxPolys.begin(); pit != pit_end; ++ pit)
|
---|
730 | {
|
---|
731 | boxPlanes.push_back((*pit)->GetSupportingPlane());
|
---|
732 | }
|
---|
733 |
|
---|
734 | pit_end = polys.end();
|
---|
735 |
|
---|
736 | for (pit = polys.begin(); pit != pit_end; ++ pit)
|
---|
737 | {
|
---|
738 | vector<Plane3>::const_iterator bit, bit_end = boxPlanes.end();
|
---|
739 |
|
---|
740 | for (bit = boxPlanes.begin(); (bit != bit_end) && (*pit); ++ bit)
|
---|
741 | {
|
---|
742 | const int cf = (*pit)->ClassifyPlane(*bit, mEpsilon);
|
---|
743 |
|
---|
744 | if (cf == Polygon3::COINCIDENT)
|
---|
745 | {
|
---|
746 | DEL_PTR(*pit);
|
---|
747 | //Debug << "coincident!!" << endl;
|
---|
748 | }
|
---|
749 | }
|
---|
750 | }
|
---|
751 |
|
---|
752 | // remove deleted entries
|
---|
753 | for (int i = 0; i < (int)polys.size(); ++ i)
|
---|
754 | {
|
---|
755 | while (!polys[i] && (i < (int)polys.size()))
|
---|
756 | {
|
---|
757 | swap(polys[i], polys.back());
|
---|
758 | polys.pop_back();
|
---|
759 | }
|
---|
760 | }
|
---|
761 |
|
---|
762 | CLEAR_CONTAINER(boxPolys);
|
---|
763 | }
|
---|
764 |
|
---|
765 |
|
---|
766 | void BspTree::Construct(const RayContainer &sampleRays,
|
---|
767 | AxisAlignedBox3 *forcedBoundingBox)
|
---|
768 | {
|
---|
769 | // generate new view cells for this contruction type
|
---|
770 | mUsePredefinedViewCells = false;
|
---|
771 |
|
---|
772 | mStat.nodes = 1;
|
---|
773 |
|
---|
774 | if (forcedBoundingBox)
|
---|
775 | {
|
---|
776 | mBbox = *forcedBoundingBox;
|
---|
777 | }
|
---|
778 | else
|
---|
779 | {
|
---|
780 | mBbox.Initialize(); // initialise BSP tree bounding box
|
---|
781 | }
|
---|
782 |
|
---|
783 | PolygonContainer *polys = new PolygonContainer();
|
---|
784 | BoundedRayContainer *rays = new BoundedRayContainer();
|
---|
785 |
|
---|
786 | RayContainer::const_iterator rit, rit_end = sampleRays.end();
|
---|
787 |
|
---|
788 | long startTime = GetTime();
|
---|
789 | Debug << "**** Extracting polygons from rays ****\n";
|
---|
790 |
|
---|
791 | std::map<Face *, Polygon3 *> facePolyMap;
|
---|
792 |
|
---|
793 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
794 | {
|
---|
795 | //////////////
|
---|
796 | //-- extract polygons intersected by the rays
|
---|
797 | Ray *ray = *rit;
|
---|
798 |
|
---|
799 | // get ray-face intersection. Store polygon representing the rays together
|
---|
800 | // with rays intersecting the face.
|
---|
801 | if (!ray->intersections.empty())
|
---|
802 | {
|
---|
803 | MeshInstance *obj = static_cast<MeshInstance *>(ray->intersections[0].mObject);
|
---|
804 |
|
---|
805 | Mesh *mesh;
|
---|
806 | if (obj->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
|
---|
807 | {
|
---|
808 | TransformedMeshInstance *tmobj =
|
---|
809 | static_cast<TransformedMeshInstance *>(obj);
|
---|
810 |
|
---|
811 | mesh = new Mesh();
|
---|
812 | tmobj->GetTransformedMesh(*mesh);
|
---|
813 | }
|
---|
814 | else // MeshInstance
|
---|
815 | {
|
---|
816 | mesh = obj->GetMesh();
|
---|
817 | }
|
---|
818 |
|
---|
819 | Face *face = mesh->mFaces[ray->intersections[0].mFace];
|
---|
820 | std::map<Face *, Polygon3 *>::iterator it = facePolyMap.find(face);
|
---|
821 |
|
---|
822 | if (it != facePolyMap.end())
|
---|
823 | {
|
---|
824 | //store rays if needed for heuristics
|
---|
825 | if (mSplitPlaneStrategy & BLOCKED_RAYS)
|
---|
826 | (*it).second->mPiercingRays.push_back(ray);
|
---|
827 | }
|
---|
828 | else
|
---|
829 | {
|
---|
830 | //store rays if needed for heuristics
|
---|
831 | Polygon3 *poly = new Polygon3(face, mesh);
|
---|
832 | poly->mParent = obj;
|
---|
833 | polys->push_back(poly);
|
---|
834 |
|
---|
835 | if (mSplitPlaneStrategy & BLOCKED_RAYS)
|
---|
836 | poly->mPiercingRays.push_back(ray);
|
---|
837 |
|
---|
838 | facePolyMap[face] = poly;
|
---|
839 | }
|
---|
840 |
|
---|
841 | // cleanup
|
---|
842 | if (obj->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
|
---|
843 | DEL_PTR(mesh);
|
---|
844 | }
|
---|
845 | }
|
---|
846 | // clear helper structure
|
---|
847 | // note: memory will not be released using clear!
|
---|
848 | facePolyMap.clear();
|
---|
849 |
|
---|
850 | // compute bounding box
|
---|
851 | if (!forcedBoundingBox)
|
---|
852 | {
|
---|
853 | mBbox.Include(*polys);
|
---|
854 | }
|
---|
855 |
|
---|
856 | ////////////
|
---|
857 | //-- store rays
|
---|
858 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
859 | {
|
---|
860 | Ray *ray = *rit;
|
---|
861 | ray->SetId(-1); // reset id
|
---|
862 |
|
---|
863 | float minT, maxT;
|
---|
864 | if (mBbox.GetRaySegment(*ray, minT, maxT))
|
---|
865 | rays->push_back(new BoundedRay(ray, minT, maxT));
|
---|
866 | }
|
---|
867 |
|
---|
868 | // throw out bad polygons
|
---|
869 | PreprocessPolygons(*polys);
|
---|
870 |
|
---|
871 | mStat.polys = (int)polys->size();
|
---|
872 |
|
---|
873 | Debug << "**** Finished polygon extraction ****" << endl;
|
---|
874 | Debug << (int)polys->size() << " polys extracted from " << (int)sampleRays.size() << " rays" << endl;
|
---|
875 | Debug << "extraction time: " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
876 |
|
---|
877 | Construct(polys, rays);
|
---|
878 | }
|
---|
879 |
|
---|
880 |
|
---|
881 | void BspTree::Construct(const ObjectContainer &objects,
|
---|
882 | const RayContainer &sampleRays,
|
---|
883 | AxisAlignedBox3 *forcedBoundingBox)
|
---|
884 | {
|
---|
885 | // generate new view cells for this construction type
|
---|
886 | mUsePredefinedViewCells = false;
|
---|
887 |
|
---|
888 | mStat.nodes = 1;
|
---|
889 | mBbox.Initialize(); // initialise BSP tree bounding box
|
---|
890 |
|
---|
891 | if (forcedBoundingBox)
|
---|
892 | {
|
---|
893 | mBbox = *forcedBoundingBox;
|
---|
894 | }
|
---|
895 |
|
---|
896 | BoundedRayContainer *rays = new BoundedRayContainer();
|
---|
897 | PolygonContainer *polys = new PolygonContainer();
|
---|
898 |
|
---|
899 | // copy mesh instance polygons into one big polygon soup
|
---|
900 | mStat.polys = AddToPolygonSoup(objects, *polys, 0, !forcedBoundingBox);
|
---|
901 |
|
---|
902 | ///////
|
---|
903 | //-- store rays
|
---|
904 | RayContainer::const_iterator rit, rit_end = sampleRays.end();
|
---|
905 |
|
---|
906 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
907 | {
|
---|
908 | Ray *ray = *rit;
|
---|
909 | ray->SetId(-1); // reset id
|
---|
910 |
|
---|
911 | float minT, maxT;
|
---|
912 | if (mBbox.GetRaySegment(*ray, minT, maxT))
|
---|
913 | rays->push_back(new BoundedRay(ray, minT, maxT));
|
---|
914 | }
|
---|
915 |
|
---|
916 | PreprocessPolygons(*polys);
|
---|
917 | Debug << "tree has " << (int)polys->size() << " polys, " << (int)sampleRays.size() << " rays" << endl;
|
---|
918 | Construct(polys, rays);
|
---|
919 | }
|
---|
920 |
|
---|
921 |
|
---|
922 | void BspTree::Construct(PolygonContainer *polys, BoundedRayContainer *rays)
|
---|
923 | {
|
---|
924 | BspTraversalStack tStack;
|
---|
925 | mRoot = new BspLeaf();
|
---|
926 |
|
---|
927 | // constrruct root node geometry
|
---|
928 | BspNodeGeometry *geom = new BspNodeGeometry();
|
---|
929 | ConstructGeometry(mRoot, *geom);
|
---|
930 |
|
---|
931 | BspTraversalData tData(mRoot,
|
---|
932 | polys,
|
---|
933 | 0,
|
---|
934 | mOutOfBoundsCell,
|
---|
935 | rays,
|
---|
936 | ComputePvsSize(*rays),
|
---|
937 | mUseAreaForPvs ? geom->GetArea() : geom->GetVolume(),
|
---|
938 | geom);
|
---|
939 |
|
---|
940 | mTotalCost = tData.mPvs * tData.mProbability / mBbox.GetVolume();
|
---|
941 | mTotalPvsSize = tData.mPvs;
|
---|
942 |
|
---|
943 | mSubdivisionStats
|
---|
944 | << "#ViewCells\n1\n" << endl
|
---|
945 | << "#RenderCostDecrease\n0\n" << endl
|
---|
946 | << "#TotalRenderCost\n" << mTotalCost << endl
|
---|
947 | << "#AvgRenderCost\n" << mTotalPvsSize << endl;
|
---|
948 |
|
---|
949 | tStack.push(tData);
|
---|
950 |
|
---|
951 | // used for intermediate time measurements and progress
|
---|
952 | long interTime = GetTime();
|
---|
953 | int nleaves = 500;
|
---|
954 |
|
---|
955 | mStat.Start();
|
---|
956 | cout << "Constructing bsp tree ...\n";
|
---|
957 | long startTime = GetTime();
|
---|
958 | while (!tStack.empty())
|
---|
959 | {
|
---|
960 | tData = tStack.top();
|
---|
961 | tStack.pop();
|
---|
962 |
|
---|
963 | // subdivide leaf node
|
---|
964 | BspNode *r = Subdivide(tStack, tData);
|
---|
965 |
|
---|
966 | if (r == mRoot)
|
---|
967 | Debug << "BSP tree construction time spent at root: "
|
---|
968 | << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl;
|
---|
969 |
|
---|
970 | if (mStat.Leaves() >= nleaves)
|
---|
971 | {
|
---|
972 | nleaves += 500;
|
---|
973 |
|
---|
974 | cout << "leaves=" << mStat.Leaves() << endl;
|
---|
975 | Debug << "needed "
|
---|
976 | << TimeDiff(interTime, GetTime())*1e-3
|
---|
977 | << " secs to create 500 leaves" << endl;
|
---|
978 | interTime = GetTime();
|
---|
979 | }
|
---|
980 | }
|
---|
981 |
|
---|
982 | cout << "finished\n";
|
---|
983 |
|
---|
984 | mStat.Stop();
|
---|
985 | }
|
---|
986 |
|
---|
987 |
|
---|
988 | bool BspTree::TerminationCriteriaMet(const BspTraversalData &data) const
|
---|
989 | {
|
---|
990 | return
|
---|
991 | (((int)data.mPolygons->size() <= mTermMinPolys) ||
|
---|
992 | ((int)data.mRays->size() <= mTermMinRays) ||
|
---|
993 | (data.mPvs <= mTermMinPvs) ||
|
---|
994 | (data.mProbability <= mTermMinProbability) ||
|
---|
995 | (data.mDepth >= mTermMaxDepth) ||
|
---|
996 | (mStat.Leaves() >= mMaxViewCells) ||
|
---|
997 | (data.GetAvgRayContribution() > mTermMaxRayContribution));
|
---|
998 | }
|
---|
999 |
|
---|
1000 |
|
---|
1001 | BspNode *BspTree::Subdivide(BspTraversalStack &tStack, BspTraversalData &tData)
|
---|
1002 | {
|
---|
1003 | ////////
|
---|
1004 | //-- terminate traversal
|
---|
1005 |
|
---|
1006 | if (TerminationCriteriaMet(tData))
|
---|
1007 | {
|
---|
1008 | BspLeaf *leaf = static_cast<BspLeaf *>(tData.mNode);
|
---|
1009 |
|
---|
1010 | BspViewCell *viewCell;
|
---|
1011 |
|
---|
1012 | if (!mUsePredefinedViewCells)
|
---|
1013 | { // generate new view cell for each leaf
|
---|
1014 | viewCell = new BspViewCell();cout << "g";
|
---|
1015 | }
|
---|
1016 | else
|
---|
1017 | {
|
---|
1018 | // add predefined view cell to leaf
|
---|
1019 | viewCell = static_cast<BspViewCell *>(tData.mViewCell);
|
---|
1020 |
|
---|
1021 | // out of bounds cell can be handled as any other view cell,
|
---|
1022 | // responsibility for deleting it has the view cells manager.
|
---|
1023 | if (IsOutOfBounds(viewCell))
|
---|
1024 | {
|
---|
1025 | mOutOfBoundsCellPartOfTree = true;
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | leaf->SetViewCell(viewCell);
|
---|
1031 | viewCell->mLeaves.push_back(leaf);
|
---|
1032 |
|
---|
1033 | //float probability = max(0.0f, tData.mProbability);
|
---|
1034 | float probability = tData.mProbability;
|
---|
1035 |
|
---|
1036 | if (mUseAreaForPvs)
|
---|
1037 | viewCell->SetArea(probability);
|
---|
1038 | else
|
---|
1039 | viewCell->SetVolume(probability);
|
---|
1040 |
|
---|
1041 | ///////////
|
---|
1042 | //-- add pvs contribution of rays
|
---|
1043 |
|
---|
1044 | if (viewCell != mOutOfBoundsCell)
|
---|
1045 | {
|
---|
1046 | int conSamp = 0, sampCon = 0;
|
---|
1047 | AddToPvs(leaf, *tData.mRays, conSamp, sampCon);
|
---|
1048 |
|
---|
1049 | mStat.contributingSamples += conSamp;
|
---|
1050 | mStat.sampleContributions += sampCon;
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | if (1) EvaluateLeafStats(tData);
|
---|
1054 |
|
---|
1055 | ////////
|
---|
1056 | //-- clean up
|
---|
1057 |
|
---|
1058 | // discard polygons
|
---|
1059 | CLEAR_CONTAINER(*tData.mPolygons);
|
---|
1060 | // discard rays
|
---|
1061 | CLEAR_CONTAINER(*tData.mRays);
|
---|
1062 |
|
---|
1063 | delete tData.mPolygons;
|
---|
1064 | delete tData.mRays;
|
---|
1065 | delete tData.mGeometry;
|
---|
1066 |
|
---|
1067 | return leaf;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | ///////////
|
---|
1071 | //-- continue subdivision
|
---|
1072 |
|
---|
1073 | PolygonContainer coincident;
|
---|
1074 |
|
---|
1075 | BspTraversalData tFrontData(NULL, new PolygonContainer(), tData.mDepth + 1, mOutOfBoundsCell,
|
---|
1076 | new BoundedRayContainer(), 0, 0, new BspNodeGeometry());
|
---|
1077 | BspTraversalData tBackData(NULL, new PolygonContainer(), tData.mDepth + 1, mOutOfBoundsCell,
|
---|
1078 | new BoundedRayContainer(), 0, 0, new BspNodeGeometry());
|
---|
1079 |
|
---|
1080 |
|
---|
1081 | // create new interior node and two leaf nodes
|
---|
1082 | BspInterior *interior =
|
---|
1083 | SubdivideNode(tData, tFrontData, tBackData, coincident);
|
---|
1084 |
|
---|
1085 | if (1)
|
---|
1086 | {
|
---|
1087 | int pvsData = tData.mPvs;
|
---|
1088 |
|
---|
1089 | float cData = (float)pvsData * tData.mProbability;
|
---|
1090 | float cFront = (float)tFrontData.mPvs * tFrontData.mProbability;
|
---|
1091 | float cBack = (float)tBackData.mPvs * tBackData.mProbability;
|
---|
1092 |
|
---|
1093 | float costDecr = (cFront + cBack - cData) / mBbox.GetVolume();
|
---|
1094 |
|
---|
1095 | mTotalCost += costDecr;
|
---|
1096 | mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - pvsData;
|
---|
1097 |
|
---|
1098 | mSubdivisionStats
|
---|
1099 | << "#ViewCells\n" << mStat.Leaves() << endl
|
---|
1100 | << "#RenderCostDecrease\n" << -costDecr << endl
|
---|
1101 | << "#TotalRenderCost\n" << mTotalCost << endl
|
---|
1102 | << "#AvgRenderCost\n" << mTotalPvsSize / mStat.Leaves() << endl;
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | // extract view cells from coincident polygons
|
---|
1106 | // with respect to the orientation of their normal
|
---|
1107 | // note: if front or back polygons are empty,
|
---|
1108 | // we get the valid in - out classification for the view cell
|
---|
1109 |
|
---|
1110 | if (mUsePredefinedViewCells)
|
---|
1111 | {
|
---|
1112 | ExtractViewCells(tFrontData,
|
---|
1113 | tBackData,
|
---|
1114 | coincident,
|
---|
1115 | interior->mPlane);
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | // don't need coincident polygons anymory
|
---|
1119 | CLEAR_CONTAINER(coincident);
|
---|
1120 |
|
---|
1121 | // push the children on the stack
|
---|
1122 | tStack.push(tFrontData);
|
---|
1123 | tStack.push(tBackData);
|
---|
1124 |
|
---|
1125 | ////////
|
---|
1126 | //-- cleanup
|
---|
1127 |
|
---|
1128 | DEL_PTR(tData.mNode);
|
---|
1129 | DEL_PTR(tData.mPolygons);
|
---|
1130 | DEL_PTR(tData.mRays);
|
---|
1131 | DEL_PTR(tData.mGeometry);
|
---|
1132 |
|
---|
1133 | return interior;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 |
|
---|
1137 | void BspTree::ExtractViewCells(BspTraversalData &frontData,
|
---|
1138 | BspTraversalData &backData,
|
---|
1139 | const PolygonContainer &coincident,
|
---|
1140 | const Plane3 &splitPlane) const
|
---|
1141 | {
|
---|
1142 | // if not empty, tree is further subdivided => don't have to find view cell
|
---|
1143 | bool foundFront = !frontData.mPolygons->empty();
|
---|
1144 | bool foundBack = !frontData.mPolygons->empty();
|
---|
1145 |
|
---|
1146 | PolygonContainer::const_iterator it =
|
---|
1147 | coincident.begin(), it_end = coincident.end();
|
---|
1148 |
|
---|
1149 | //////////
|
---|
1150 | //-- find first view cells in front and back leafs
|
---|
1151 |
|
---|
1152 | for (; !(foundFront && foundBack) && (it != it_end); ++ it)
|
---|
1153 | {
|
---|
1154 | if (DotProd((*it)->GetNormal(), splitPlane.mNormal) > 0)
|
---|
1155 | {
|
---|
1156 | backData.mViewCell = static_cast<ViewCellLeaf *>((*it)->mParent);
|
---|
1157 | foundBack = true;
|
---|
1158 | }
|
---|
1159 | else
|
---|
1160 | {
|
---|
1161 | frontData.mViewCell = static_cast<ViewCellLeaf *>((*it)->mParent);
|
---|
1162 | foundFront = true;
|
---|
1163 | }
|
---|
1164 | }
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 |
|
---|
1168 | BspInterior *BspTree::SubdivideNode(BspTraversalData &tData,
|
---|
1169 | BspTraversalData &frontData,
|
---|
1170 | BspTraversalData &backData,
|
---|
1171 | PolygonContainer &coincident)
|
---|
1172 | {
|
---|
1173 | mStat.nodes += 2;
|
---|
1174 |
|
---|
1175 | BspLeaf *leaf = static_cast<BspLeaf *>(tData.mNode);
|
---|
1176 |
|
---|
1177 | // select subdivision plane
|
---|
1178 | BspInterior *interior = new BspInterior(SelectPlane(leaf, tData));
|
---|
1179 |
|
---|
1180 | #ifdef GTP_DEBUG
|
---|
1181 | Debug << interior << endl;
|
---|
1182 | #endif
|
---|
1183 |
|
---|
1184 | // subdivide rays into front and back rays
|
---|
1185 | SplitRays(interior->mPlane, *tData.mRays, *frontData.mRays, *backData.mRays);
|
---|
1186 |
|
---|
1187 | // subdivide polygons with plane
|
---|
1188 | mStat.polySplits += SplitPolygons(interior->GetPlane(),
|
---|
1189 | *tData.mPolygons,
|
---|
1190 | *frontData.mPolygons,
|
---|
1191 | *backData.mPolygons,
|
---|
1192 | coincident);
|
---|
1193 |
|
---|
1194 |
|
---|
1195 | // compute pvs
|
---|
1196 | frontData.mPvs = ComputePvsSize(*frontData.mRays);
|
---|
1197 | backData.mPvs = ComputePvsSize(*backData.mRays);
|
---|
1198 |
|
---|
1199 | // split geometry and compute area
|
---|
1200 | if (1)
|
---|
1201 | {
|
---|
1202 | tData.mGeometry->SplitGeometry(*frontData.mGeometry,
|
---|
1203 | *backData.mGeometry,
|
---|
1204 | interior->mPlane,
|
---|
1205 | mBbox,
|
---|
1206 | //0.000000000001);
|
---|
1207 | mEpsilon);
|
---|
1208 |
|
---|
1209 |
|
---|
1210 | if (mUseAreaForPvs)
|
---|
1211 | {
|
---|
1212 | frontData.mProbability = frontData.mGeometry->GetArea();
|
---|
1213 | backData.mProbability = backData.mGeometry->GetArea();
|
---|
1214 | }
|
---|
1215 | else
|
---|
1216 | {
|
---|
1217 | frontData.mProbability = frontData.mGeometry->GetVolume();
|
---|
1218 | backData.mProbability = tData.mProbability - frontData.mProbability;
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | //-- create front and back leaf
|
---|
1223 |
|
---|
1224 | BspInterior *parent = leaf->GetParent();
|
---|
1225 |
|
---|
1226 | // replace a link from node's parent
|
---|
1227 | if (!leaf->IsRoot())
|
---|
1228 | {
|
---|
1229 | parent->ReplaceChildLink(leaf, interior);
|
---|
1230 | interior->SetParent(parent);
|
---|
1231 | }
|
---|
1232 | else // new root
|
---|
1233 | {
|
---|
1234 | mRoot = interior;
|
---|
1235 | }
|
---|
1236 |
|
---|
1237 | // and setup child links
|
---|
1238 | interior->SetupChildLinks(new BspLeaf(interior), new BspLeaf(interior));
|
---|
1239 |
|
---|
1240 | frontData.mNode = interior->GetFront();
|
---|
1241 | backData.mNode = interior->GetBack();
|
---|
1242 |
|
---|
1243 | interior->mTimeStamp = mTimeStamp ++;
|
---|
1244 |
|
---|
1245 | //DEL_PTR(leaf);
|
---|
1246 | return interior;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 |
|
---|
1250 | void BspTree::SortSubdivisionCandidates(const PolygonContainer &polys,
|
---|
1251 | const int axis,
|
---|
1252 | vector<SortableEntry> &splitCandidates) const
|
---|
1253 | {
|
---|
1254 | splitCandidates.clear();
|
---|
1255 |
|
---|
1256 | int requestedSize = 2 * (int)polys.size();
|
---|
1257 | // creates a sorted split candidates array
|
---|
1258 | splitCandidates.reserve(requestedSize);
|
---|
1259 |
|
---|
1260 | PolygonContainer::const_iterator it, it_end = polys.end();
|
---|
1261 |
|
---|
1262 | AxisAlignedBox3 box;
|
---|
1263 |
|
---|
1264 | // insert all queries
|
---|
1265 | for(it = polys.begin(); it != it_end; ++ it)
|
---|
1266 | {
|
---|
1267 | box.Initialize();
|
---|
1268 | box.Include(*(*it));
|
---|
1269 |
|
---|
1270 | splitCandidates.push_back(SortableEntry(SortableEntry::POLY_MIN, box.Min(axis), *it));
|
---|
1271 | splitCandidates.push_back(SortableEntry(SortableEntry::POLY_MAX, box.Max(axis), *it));
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | stable_sort(splitCandidates.begin(), splitCandidates.end());
|
---|
1275 | }
|
---|
1276 |
|
---|
1277 |
|
---|
1278 | float BspTree::BestCostRatio(const PolygonContainer &polys,
|
---|
1279 | const AxisAlignedBox3 &box,
|
---|
1280 | const int axis,
|
---|
1281 | float &position,
|
---|
1282 | int &objectsBack,
|
---|
1283 | int &objectsFront) const
|
---|
1284 | {
|
---|
1285 | vector<SortableEntry> splitCandidates;
|
---|
1286 |
|
---|
1287 | SortSubdivisionCandidates(polys, axis, splitCandidates);
|
---|
1288 |
|
---|
1289 | // go through the lists, count the number of objects left and right
|
---|
1290 | // and evaluate the following cost funcion:
|
---|
1291 | // C = ct_div_ci + (ol + or)/queries
|
---|
1292 |
|
---|
1293 | int objectsLeft = 0, objectsRight = (int)polys.size();
|
---|
1294 |
|
---|
1295 | float minBox = box.Min(axis);
|
---|
1296 | float maxBox = box.Max(axis);
|
---|
1297 | float boxArea = box.SurfaceArea();
|
---|
1298 |
|
---|
1299 | float minBand = minBox + mSplitBorder * (maxBox - minBox);
|
---|
1300 | float maxBand = minBox + (1.0f - mSplitBorder) * (maxBox - minBox);
|
---|
1301 |
|
---|
1302 | float minSum = 1e20f;
|
---|
1303 | vector<SortableEntry>::const_iterator ci, ci_end = splitCandidates.end();
|
---|
1304 |
|
---|
1305 | for(ci = splitCandidates.begin(); ci != ci_end; ++ ci)
|
---|
1306 | {
|
---|
1307 | switch ((*ci).type)
|
---|
1308 | {
|
---|
1309 | case SortableEntry::POLY_MIN:
|
---|
1310 | ++ objectsLeft;
|
---|
1311 | break;
|
---|
1312 | case SortableEntry::POLY_MAX:
|
---|
1313 | -- objectsRight;
|
---|
1314 | break;
|
---|
1315 | default:
|
---|
1316 | break;
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | if ((*ci).value > minBand && (*ci).value < maxBand)
|
---|
1320 | {
|
---|
1321 | AxisAlignedBox3 lbox = box;
|
---|
1322 | AxisAlignedBox3 rbox = box;
|
---|
1323 | lbox.SetMax(axis, (*ci).value);
|
---|
1324 | rbox.SetMin(axis, (*ci).value);
|
---|
1325 |
|
---|
1326 | const float sum = objectsLeft * lbox.SurfaceArea() +
|
---|
1327 | objectsRight * rbox.SurfaceArea();
|
---|
1328 |
|
---|
1329 | if (sum < minSum)
|
---|
1330 | {
|
---|
1331 | minSum = sum;
|
---|
1332 | position = (*ci).value;
|
---|
1333 |
|
---|
1334 | objectsBack = objectsLeft;
|
---|
1335 | objectsFront = objectsRight;
|
---|
1336 | }
|
---|
1337 | }
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | const float oldCost = (float)polys.size();
|
---|
1341 | const float newCost = mAxisAlignedCtDivCi + minSum / boxArea;
|
---|
1342 | const float ratio = newCost / oldCost;
|
---|
1343 |
|
---|
1344 | #ifdef GTP_DEBUG
|
---|
1345 | Debug << "====================" << endl;
|
---|
1346 | Debug << "costRatio=" << ratio << " pos=" << position<<" t=" << (position - minBox)/(maxBox - minBox)
|
---|
1347 | << "\t o=(" << objectsBack << "," << objectsFront << ")" << endl;
|
---|
1348 | #endif
|
---|
1349 | return ratio;
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 | bool BspTree::SelectAxisAlignedPlane(Plane3 &plane,
|
---|
1353 | const PolygonContainer &polys) const
|
---|
1354 | {
|
---|
1355 | AxisAlignedBox3 box;
|
---|
1356 | box.Initialize();
|
---|
1357 |
|
---|
1358 | // create bounding box of region
|
---|
1359 | box.Include(polys);
|
---|
1360 |
|
---|
1361 | int objectsBack = 0, objectsFront = 0;
|
---|
1362 | int axis = 0;
|
---|
1363 | float costRatio = MAX_FLOAT;
|
---|
1364 | Vector3 position;
|
---|
1365 |
|
---|
1366 | //-- area subdivision
|
---|
1367 | for (int i = 0; i < 3; ++ i)
|
---|
1368 | {
|
---|
1369 | float p = 0;
|
---|
1370 | float r = BestCostRatio(polys, box, i, p, objectsBack, objectsFront);
|
---|
1371 |
|
---|
1372 | if (r < costRatio)
|
---|
1373 | {
|
---|
1374 | costRatio = r;
|
---|
1375 | axis = i;
|
---|
1376 | position = p;
|
---|
1377 | }
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | if (costRatio >= mMaxCostRatio)
|
---|
1381 | return false;
|
---|
1382 |
|
---|
1383 | Vector3 norm(0,0,0); norm[axis] = 1.0f;
|
---|
1384 | plane = Plane3(norm, position);
|
---|
1385 |
|
---|
1386 | return true;
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 |
|
---|
1390 | Plane3 BspTree::SelectPlane(BspLeaf *leaf, BspTraversalData &data)
|
---|
1391 | {
|
---|
1392 | if ((!mMaxPolyCandidates || data.mPolygons->empty()) &&
|
---|
1393 | (!mMaxRayCandidates || data.mRays->empty()))
|
---|
1394 | {
|
---|
1395 | Debug << "Warning: No autopartition polygon candidate available\n";
|
---|
1396 |
|
---|
1397 | // return axis aligned split
|
---|
1398 | AxisAlignedBox3 box;
|
---|
1399 | box.Initialize();
|
---|
1400 |
|
---|
1401 | // create bounding box of region
|
---|
1402 | box.Include(*data.mPolygons);
|
---|
1403 |
|
---|
1404 | const int axis = box.Size().DrivingAxis();
|
---|
1405 | const Vector3 position = (box.Min()[axis] + box.Max()[axis]) * 0.5f;
|
---|
1406 |
|
---|
1407 | Vector3 norm(0,0,0); norm[axis] = 1.0f;
|
---|
1408 | return Plane3(norm, position);
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | if ((mSplitPlaneStrategy & AXIS_ALIGNED) &&
|
---|
1412 | ((int)data.mPolygons->size() > mTermMinPolysForAxisAligned) &&
|
---|
1413 | ((int)data.mRays->size() > mTermMinRaysForAxisAligned) &&
|
---|
1414 | ((mTermMinObjectsForAxisAligned < 0) ||
|
---|
1415 | (Polygon3::ParentObjectsSize(*data.mPolygons) > mTermMinObjectsForAxisAligned)))
|
---|
1416 | {
|
---|
1417 | Plane3 plane;
|
---|
1418 | if (SelectAxisAlignedPlane(plane, *data.mPolygons))
|
---|
1419 | return plane;
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | // simplest strategy: just take next polygon
|
---|
1423 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
1424 | {
|
---|
1425 | if (!data.mPolygons->empty())
|
---|
1426 | {
|
---|
1427 | Polygon3 *nextPoly =
|
---|
1428 | (*data.mPolygons)[(int)RandomValue(0, (Real)((int)data.mPolygons->size() - 1))];
|
---|
1429 | return nextPoly->GetSupportingPlane();
|
---|
1430 | }
|
---|
1431 | else
|
---|
1432 | {
|
---|
1433 | const int candidateIdx = (int)RandomValue(0, (Real)((int)data.mRays->size() - 1));
|
---|
1434 | BoundedRay *bRay = (*data.mRays)[candidateIdx];
|
---|
1435 |
|
---|
1436 | Ray *ray = bRay->mRay;
|
---|
1437 |
|
---|
1438 | const Vector3 minPt = ray->Extrap(bRay->mMinT);
|
---|
1439 | const Vector3 maxPt = ray->Extrap(bRay->mMaxT);
|
---|
1440 |
|
---|
1441 | const Vector3 pt = (maxPt + minPt) * 0.5;
|
---|
1442 |
|
---|
1443 | const Vector3 normal = ray->GetDir();
|
---|
1444 |
|
---|
1445 | return Plane3(normal, pt);
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | return Plane3();
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | // use heuristics to find appropriate plane
|
---|
1452 | return SelectPlaneHeuristics(leaf, data);
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 |
|
---|
1456 | Plane3 BspTree::ChooseCandidatePlane(const BoundedRayContainer &rays) const
|
---|
1457 | {
|
---|
1458 | const int candidateIdx = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
1459 | BoundedRay *bRay = rays[candidateIdx];
|
---|
1460 | Ray *ray = bRay->mRay;
|
---|
1461 |
|
---|
1462 | const Vector3 minPt = ray->Extrap(bRay->mMinT);
|
---|
1463 | const Vector3 maxPt = ray->Extrap(bRay->mMaxT);
|
---|
1464 |
|
---|
1465 | const Vector3 pt = (maxPt + minPt) * 0.5;
|
---|
1466 |
|
---|
1467 | const Vector3 normal = ray->GetDir();
|
---|
1468 |
|
---|
1469 | return Plane3(normal, pt);
|
---|
1470 | }
|
---|
1471 |
|
---|
1472 | Plane3 BspTree::ChooseCandidatePlane2(const BoundedRayContainer &rays) const
|
---|
1473 | {
|
---|
1474 | Vector3 pt[3];
|
---|
1475 | int idx[3];
|
---|
1476 | int cmaxT = 0;
|
---|
1477 | int cminT = 0;
|
---|
1478 | bool chooseMin = false;
|
---|
1479 |
|
---|
1480 | for (int j = 0; j < 3; j ++)
|
---|
1481 | {
|
---|
1482 | idx[j] = (int)RandomValue(0, Real((int)rays.size() * 2 - 1));
|
---|
1483 |
|
---|
1484 | if (idx[j] >= (int)rays.size())
|
---|
1485 | {
|
---|
1486 | idx[j] -= (int)rays.size();
|
---|
1487 | chooseMin = (cminT < 2);
|
---|
1488 | }
|
---|
1489 | else
|
---|
1490 | chooseMin = (cmaxT < 2);
|
---|
1491 |
|
---|
1492 | BoundedRay *bRay = rays[idx[j]];
|
---|
1493 | pt[j] = chooseMin ? bRay->mRay->Extrap(bRay->mMinT) :
|
---|
1494 | bRay->mRay->Extrap(bRay->mMaxT);
|
---|
1495 | }
|
---|
1496 |
|
---|
1497 | return Plane3(pt[0], pt[1], pt[2]);
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 |
|
---|
1501 | Plane3 BspTree::ChooseCandidatePlane3(const BoundedRayContainer &rays) const
|
---|
1502 | {
|
---|
1503 | Vector3 pt[3];
|
---|
1504 |
|
---|
1505 | int idx1 = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
1506 | int idx2 = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
1507 |
|
---|
1508 | // check if rays different
|
---|
1509 | if (idx1 == idx2)
|
---|
1510 | idx2 = (idx2 + 1) % (int)rays.size();
|
---|
1511 |
|
---|
1512 | const BoundedRay *ray1 = rays[idx1];
|
---|
1513 | const BoundedRay *ray2 = rays[idx2];
|
---|
1514 |
|
---|
1515 | // normal vector of the plane parallel to both lines
|
---|
1516 | const Vector3 norm =
|
---|
1517 | Normalize(CrossProd(ray1->mRay->GetDir(), ray2->mRay->GetDir()));
|
---|
1518 |
|
---|
1519 | const Vector3 orig1 = ray1->mRay->Extrap(ray1->mMinT);
|
---|
1520 | const Vector3 orig2 = ray2->mRay->Extrap(ray2->mMinT);
|
---|
1521 |
|
---|
1522 | // vector from line 1 to line 2
|
---|
1523 | const Vector3 vd = orig1 - orig2;
|
---|
1524 |
|
---|
1525 | // project vector on normal to get distance
|
---|
1526 | const float dist = DotProd(vd, norm);
|
---|
1527 |
|
---|
1528 | // point on plane lies halfway between the two planes
|
---|
1529 | const Vector3 planePt = orig1 + norm * dist * 0.5;
|
---|
1530 |
|
---|
1531 | return Plane3(norm, planePt);
|
---|
1532 | }
|
---|
1533 |
|
---|
1534 |
|
---|
1535 | Plane3 BspTree::SelectPlaneHeuristics(BspLeaf *leaf, BspTraversalData &data)
|
---|
1536 | {
|
---|
1537 | float lowestCost = MAX_FLOAT;
|
---|
1538 | Plane3 bestPlane;
|
---|
1539 | // intermediate plane
|
---|
1540 | Plane3 plane;
|
---|
1541 |
|
---|
1542 | const int limit = Min((int)data.mPolygons->size(), mMaxPolyCandidates);
|
---|
1543 | int maxIdx = (int)data.mPolygons->size();
|
---|
1544 |
|
---|
1545 | for (int i = 0; i < limit; ++ i)
|
---|
1546 | {
|
---|
1547 | // assure that no index is taken twice
|
---|
1548 | const int candidateIdx = (int)RandomValue(0, (Real)(-- maxIdx));
|
---|
1549 |
|
---|
1550 | Polygon3 *poly = (*data.mPolygons)[candidateIdx];
|
---|
1551 |
|
---|
1552 | // swap candidate to the end to avoid testing same plane
|
---|
1553 | std::swap((*data.mPolygons)[maxIdx], (*data.mPolygons)[candidateIdx]);
|
---|
1554 | //Polygon3 *poly = (*data.mPolygons)[(int)RandomValue(0, (int)polys.size() - 1)];
|
---|
1555 |
|
---|
1556 | // evaluate current candidate
|
---|
1557 | const float candidateCost =
|
---|
1558 | SplitPlaneCost(poly->GetSupportingPlane(), data);
|
---|
1559 |
|
---|
1560 | if (candidateCost < lowestCost)
|
---|
1561 | {
|
---|
1562 | bestPlane = poly->GetSupportingPlane();
|
---|
1563 | lowestCost = candidateCost;
|
---|
1564 | }
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | //-- choose candidate planes extracted from rays
|
---|
1568 | for (int i = 0; i < mMaxRayCandidates; ++ i)
|
---|
1569 | {
|
---|
1570 | plane = ChooseCandidatePlane3(*data.mRays);
|
---|
1571 | const float candidateCost = SplitPlaneCost(plane, data);
|
---|
1572 |
|
---|
1573 | if (candidateCost < lowestCost)
|
---|
1574 | {
|
---|
1575 | bestPlane = plane;
|
---|
1576 | lowestCost = candidateCost;
|
---|
1577 | }
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | #ifdef GTP_DEBUG
|
---|
1581 | Debug << "plane lowest cost: " << lowestCost << endl;
|
---|
1582 | #endif
|
---|
1583 |
|
---|
1584 | return bestPlane;
|
---|
1585 | }
|
---|
1586 |
|
---|
1587 |
|
---|
1588 | float BspTree::SplitPlaneCost(const Plane3 &candidatePlane,
|
---|
1589 | const PolygonContainer &polys) const
|
---|
1590 | {
|
---|
1591 | float val = 0;
|
---|
1592 |
|
---|
1593 | float sumBalancedPolys = 0;
|
---|
1594 | float sumSplits = 0;
|
---|
1595 | float sumPolyArea = 0;
|
---|
1596 | float sumBalancedViewCells = 0;
|
---|
1597 | float sumBlockedRays = 0;
|
---|
1598 | float totalBlockedRays = 0;
|
---|
1599 | //float totalArea = 0;
|
---|
1600 | int totalViewCells = 0;
|
---|
1601 |
|
---|
1602 | // need three unique ids for each type of view cell
|
---|
1603 | // for balanced view cells criterium
|
---|
1604 | ViewCell::NewMail();
|
---|
1605 | const int backId = ViewCell::sMailId;
|
---|
1606 | ViewCell::NewMail();
|
---|
1607 | const int frontId = ViewCell::sMailId;
|
---|
1608 | ViewCell::NewMail();
|
---|
1609 | const int frontAndBackId = ViewCell::sMailId;
|
---|
1610 |
|
---|
1611 | bool useRand;
|
---|
1612 | int limit;
|
---|
1613 |
|
---|
1614 | // choose test polyongs randomly if over threshold
|
---|
1615 | if ((int)polys.size() > mMaxTests)
|
---|
1616 | {
|
---|
1617 | useRand = true;
|
---|
1618 | limit = mMaxTests;
|
---|
1619 | }
|
---|
1620 | else
|
---|
1621 | {
|
---|
1622 | useRand = false;
|
---|
1623 | limit = (int)polys.size();
|
---|
1624 | }
|
---|
1625 |
|
---|
1626 | for (int i = 0; i < limit; ++ i)
|
---|
1627 | {
|
---|
1628 | const int testIdx = useRand ? (int)RandomValue(0, (Real)(limit - 1)) : i;
|
---|
1629 |
|
---|
1630 | Polygon3 *poly = polys[testIdx];
|
---|
1631 |
|
---|
1632 | const int classification =
|
---|
1633 | poly->ClassifyPlane(candidatePlane, mEpsilon);
|
---|
1634 |
|
---|
1635 | if (mSplitPlaneStrategy & BALANCED_POLYS)
|
---|
1636 | sumBalancedPolys += sBalancedPolysTable[classification];
|
---|
1637 |
|
---|
1638 | if (mSplitPlaneStrategy & LEAST_SPLITS)
|
---|
1639 | sumSplits += sLeastPolySplitsTable[classification];
|
---|
1640 |
|
---|
1641 | if (mSplitPlaneStrategy & LARGEST_POLY_AREA)
|
---|
1642 | {
|
---|
1643 | if (classification == Polygon3::COINCIDENT)
|
---|
1644 | sumPolyArea += poly->GetArea();
|
---|
1645 | //totalArea += area;
|
---|
1646 | }
|
---|
1647 |
|
---|
1648 | if (mSplitPlaneStrategy & BLOCKED_RAYS)
|
---|
1649 | {
|
---|
1650 | const float blockedRays = (float)poly->mPiercingRays.size();
|
---|
1651 |
|
---|
1652 | if (classification == Polygon3::COINCIDENT)
|
---|
1653 | sumBlockedRays += blockedRays;
|
---|
1654 |
|
---|
1655 | totalBlockedRays += blockedRays;
|
---|
1656 | }
|
---|
1657 |
|
---|
1658 | // assign view cells to back or front according to classificaion
|
---|
1659 | if (mSplitPlaneStrategy & BALANCED_VIEW_CELLS)
|
---|
1660 | {
|
---|
1661 | MeshInstance *viewCell = poly->mParent;
|
---|
1662 |
|
---|
1663 | // assure that we only count a view cell
|
---|
1664 | // once for the front and once for the back side of the plane
|
---|
1665 | if (classification == Polygon3::FRONT_SIDE)
|
---|
1666 | {
|
---|
1667 | if ((viewCell->mMailbox != frontId) &&
|
---|
1668 | (viewCell->mMailbox != frontAndBackId))
|
---|
1669 | {
|
---|
1670 | sumBalancedViewCells += 1.0;
|
---|
1671 |
|
---|
1672 | if (viewCell->mMailbox != backId)
|
---|
1673 | viewCell->mMailbox = frontId;
|
---|
1674 | else
|
---|
1675 | viewCell->mMailbox = frontAndBackId;
|
---|
1676 |
|
---|
1677 | ++ totalViewCells;
|
---|
1678 | }
|
---|
1679 | }
|
---|
1680 | else if (classification == Polygon3::BACK_SIDE)
|
---|
1681 | {
|
---|
1682 | if ((viewCell->mMailbox != backId) &&
|
---|
1683 | (viewCell->mMailbox != frontAndBackId))
|
---|
1684 | {
|
---|
1685 | sumBalancedViewCells -= 1.0;
|
---|
1686 |
|
---|
1687 | if (viewCell->mMailbox != frontId)
|
---|
1688 | viewCell->mMailbox = backId;
|
---|
1689 | else
|
---|
1690 | viewCell->mMailbox = frontAndBackId;
|
---|
1691 |
|
---|
1692 | ++ totalViewCells;
|
---|
1693 | }
|
---|
1694 | }
|
---|
1695 | }
|
---|
1696 | }
|
---|
1697 |
|
---|
1698 | const float polysSize = (float)polys.size() + Limits::Small;
|
---|
1699 |
|
---|
1700 | // all values should be approx. between 0 and 1 so they can be combined
|
---|
1701 | // and scaled with the factors according to their importance
|
---|
1702 | if (mSplitPlaneStrategy & BALANCED_POLYS)
|
---|
1703 | val += mBalancedPolysFactor * fabs(sumBalancedPolys) / polysSize;
|
---|
1704 |
|
---|
1705 | if (mSplitPlaneStrategy & LEAST_SPLITS)
|
---|
1706 | val += mLeastSplitsFactor * sumSplits / polysSize;
|
---|
1707 |
|
---|
1708 | if (mSplitPlaneStrategy & LARGEST_POLY_AREA)
|
---|
1709 | // HACK: polys.size should be total area so scaling is between 0 and 1
|
---|
1710 | val += mLargestPolyAreaFactor * (float)polys.size() / sumPolyArea;
|
---|
1711 |
|
---|
1712 | if (mSplitPlaneStrategy & BLOCKED_RAYS)
|
---|
1713 | if (totalBlockedRays != 0)
|
---|
1714 | val += mBlockedRaysFactor * (totalBlockedRays - sumBlockedRays) / totalBlockedRays;
|
---|
1715 |
|
---|
1716 | if (mSplitPlaneStrategy & BALANCED_VIEW_CELLS)
|
---|
1717 | val += mBalancedViewCellsFactor * fabs(sumBalancedViewCells) /
|
---|
1718 | ((float)totalViewCells + Limits::Small);
|
---|
1719 |
|
---|
1720 | return val;
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 |
|
---|
1724 | inline void BspTree::GenerateUniqueIdsForPvs()
|
---|
1725 | {
|
---|
1726 | Intersectable::NewMail(); sBackId = Intersectable::sMailId;
|
---|
1727 | Intersectable::NewMail(); sFrontId = Intersectable::sMailId;
|
---|
1728 | Intersectable::NewMail(); sFrontAndBackId = Intersectable::sMailId;
|
---|
1729 | }
|
---|
1730 |
|
---|
1731 |
|
---|
1732 | float BspTree::SplitPlaneCost(const Plane3 &candidatePlane,
|
---|
1733 | const BoundedRayContainer &rays,
|
---|
1734 | const int pvs,
|
---|
1735 | const float probability,
|
---|
1736 | const BspNodeGeometry &cell) const
|
---|
1737 | {
|
---|
1738 | float val = 0;
|
---|
1739 |
|
---|
1740 | float sumBalancedRays = 0;
|
---|
1741 | float sumRaySplits = 0;
|
---|
1742 |
|
---|
1743 | int frontPvs = 0;
|
---|
1744 | int backPvs = 0;
|
---|
1745 |
|
---|
1746 | // probability that view point lies in child
|
---|
1747 | float pOverall = 0;
|
---|
1748 | float pFront = 0;
|
---|
1749 | float pBack = 0;
|
---|
1750 |
|
---|
1751 | const bool pvsUseLen = false;
|
---|
1752 |
|
---|
1753 |
|
---|
1754 | if (mSplitPlaneStrategy & PVS)
|
---|
1755 | {
|
---|
1756 | // create unique ids for pvs heuristics
|
---|
1757 | GenerateUniqueIdsForPvs();
|
---|
1758 |
|
---|
1759 | // construct child geometry with regard to the candidate split plane
|
---|
1760 | BspNodeGeometry geomFront;
|
---|
1761 | BspNodeGeometry geomBack;
|
---|
1762 |
|
---|
1763 | const bool splitSuccessFull =
|
---|
1764 | cell.SplitGeometry(geomFront,
|
---|
1765 | geomBack,
|
---|
1766 | candidatePlane,
|
---|
1767 | mBbox,
|
---|
1768 | mEpsilon);
|
---|
1769 |
|
---|
1770 | if (mUseAreaForPvs)
|
---|
1771 | {
|
---|
1772 | pFront = geomFront.GetArea();
|
---|
1773 | pBack = geomBack.GetArea();
|
---|
1774 | }
|
---|
1775 | else
|
---|
1776 | {
|
---|
1777 | pFront = geomFront.GetVolume();
|
---|
1778 | pBack = pOverall - pFront;
|
---|
1779 | }
|
---|
1780 |
|
---|
1781 |
|
---|
1782 | // give penalty to unbalanced split
|
---|
1783 | if (1 &&
|
---|
1784 | (!splitSuccessFull || (pFront <= 0) || (pBack <= 0) ||
|
---|
1785 | !geomFront.Valid() || !geomBack.Valid()))
|
---|
1786 | {
|
---|
1787 | //Debug << "error f: " << pFront << " b: " << pBack << endl;
|
---|
1788 | return 99999.9f;
|
---|
1789 | }
|
---|
1790 |
|
---|
1791 | pOverall = probability;
|
---|
1792 | }
|
---|
1793 |
|
---|
1794 | bool useRand;
|
---|
1795 | int limit;
|
---|
1796 |
|
---|
1797 | // choose test polyongs randomly if over threshold
|
---|
1798 | if ((int)rays.size() > mMaxTests)
|
---|
1799 | {
|
---|
1800 | useRand = true;
|
---|
1801 | limit = mMaxTests;
|
---|
1802 | }
|
---|
1803 | else
|
---|
1804 | {
|
---|
1805 | useRand = false;
|
---|
1806 | limit = (int)rays.size();
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 | for (int i = 0; i < limit; ++ i)
|
---|
1810 | {
|
---|
1811 | const int testIdx = useRand ? (int)RandomValue(0, (Real)(limit - 1)) : i;
|
---|
1812 |
|
---|
1813 | BoundedRay *bRay = rays[testIdx];
|
---|
1814 |
|
---|
1815 | Ray *ray = bRay->mRay;
|
---|
1816 | const float minT = bRay->mMinT;
|
---|
1817 | const float maxT = bRay->mMaxT;
|
---|
1818 |
|
---|
1819 | Vector3 entP, extP;
|
---|
1820 |
|
---|
1821 | const int cf =
|
---|
1822 | ray->ClassifyPlane(candidatePlane, minT, maxT, entP, extP);
|
---|
1823 |
|
---|
1824 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
1825 | {
|
---|
1826 | sumBalancedRays += sBalancedRaysTable[cf];
|
---|
1827 | }
|
---|
1828 |
|
---|
1829 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
1830 | {
|
---|
1831 | sumRaySplits += sLeastRaySplitsTable[cf];
|
---|
1832 | }
|
---|
1833 |
|
---|
1834 | if (mSplitPlaneStrategy & PVS)
|
---|
1835 | {
|
---|
1836 | // in case the ray intersects an object
|
---|
1837 | // assure that we only count the object
|
---|
1838 | // once for the front and once for the back side of the plane
|
---|
1839 |
|
---|
1840 | // add the termination object
|
---|
1841 | if (!ray->intersections.empty())
|
---|
1842 | AddObjToPvs(ray->intersections[0].mObject, cf, frontPvs, backPvs);
|
---|
1843 |
|
---|
1844 | // add the source object
|
---|
1845 | AddObjToPvs(ray->sourceObject.mObject, cf, frontPvs, backPvs);
|
---|
1846 | }
|
---|
1847 | }
|
---|
1848 |
|
---|
1849 | const float raysSize = (float)rays.size() + Limits::Small;
|
---|
1850 |
|
---|
1851 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
1852 | val += mLeastRaySplitsFactor * sumRaySplits / raysSize;
|
---|
1853 |
|
---|
1854 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
1855 | val += mBalancedRaysFactor * fabs(sumBalancedRays) / raysSize;
|
---|
1856 |
|
---|
1857 | const float denom = pOverall * (float)pvs + Limits::Small;
|
---|
1858 |
|
---|
1859 | if (mSplitPlaneStrategy & PVS)
|
---|
1860 | {
|
---|
1861 | val += mPvsFactor * (frontPvs * pFront + (backPvs * pBack)) / denom;
|
---|
1862 | }
|
---|
1863 |
|
---|
1864 |
|
---|
1865 | #ifdef GTP_DEBUG
|
---|
1866 | Debug << "totalpvs: " << pvs << " ptotal: " << pOverall
|
---|
1867 | << " frontpvs: " << frontPvs << " pFront: " << pFront
|
---|
1868 | << " backpvs: " << backPvs << " pBack: " << pBack << endl << endl;
|
---|
1869 | #endif
|
---|
1870 |
|
---|
1871 | return val;
|
---|
1872 | }
|
---|
1873 |
|
---|
1874 | void BspTree::AddObjToPvs(Intersectable *obj,
|
---|
1875 | const int cf,
|
---|
1876 | int &frontPvs,
|
---|
1877 | int &backPvs) const
|
---|
1878 | {
|
---|
1879 | if (!obj)
|
---|
1880 | return;
|
---|
1881 | // TODO: does this really belong to no pvs?
|
---|
1882 | //if (cf == Ray::COINCIDENT) return;
|
---|
1883 |
|
---|
1884 | // object belongs to both PVS
|
---|
1885 | const bool bothSides = (cf == Ray::FRONT_BACK) ||
|
---|
1886 | (cf == Ray::BACK_FRONT) ||
|
---|
1887 | (cf == Ray::COINCIDENT);
|
---|
1888 |
|
---|
1889 | if ((cf == Ray::FRONT) || bothSides)
|
---|
1890 | {
|
---|
1891 | if ((obj->mMailbox != sFrontId) &&
|
---|
1892 | (obj->mMailbox != sFrontAndBackId))
|
---|
1893 | {
|
---|
1894 | ++ frontPvs;
|
---|
1895 |
|
---|
1896 | if (obj->mMailbox == sBackId)
|
---|
1897 | obj->mMailbox = sFrontAndBackId;
|
---|
1898 | else
|
---|
1899 | obj->mMailbox = sFrontId;
|
---|
1900 | }
|
---|
1901 | }
|
---|
1902 |
|
---|
1903 | if ((cf == Ray::BACK) || bothSides)
|
---|
1904 | {
|
---|
1905 | if ((obj->mMailbox != sBackId) &&
|
---|
1906 | (obj->mMailbox != sFrontAndBackId))
|
---|
1907 | {
|
---|
1908 | ++ backPvs;
|
---|
1909 |
|
---|
1910 | if (obj->mMailbox == sFrontId)
|
---|
1911 | obj->mMailbox = sFrontAndBackId;
|
---|
1912 | else
|
---|
1913 | obj->mMailbox = sBackId;
|
---|
1914 | }
|
---|
1915 | }
|
---|
1916 | }
|
---|
1917 |
|
---|
1918 |
|
---|
1919 | float BspTree::SplitPlaneCost(const Plane3 &candidatePlane,
|
---|
1920 | BspTraversalData &data) const
|
---|
1921 | {
|
---|
1922 | float val = 0;
|
---|
1923 |
|
---|
1924 | if (mSplitPlaneStrategy & VERTICAL_AXIS)
|
---|
1925 | {
|
---|
1926 | Vector3 tinyAxis(0,0,0); tinyAxis[mBbox.Size().TinyAxis()] = 1.0f;
|
---|
1927 | // we put a penalty on the dot product between the "tiny" vertical axis
|
---|
1928 | // and the split plane axis
|
---|
1929 | val += mVerticalSplitsFactor *
|
---|
1930 | fabs(DotProd(candidatePlane.mNormal, tinyAxis));
|
---|
1931 | }
|
---|
1932 |
|
---|
1933 | // the following criteria loop over all polygons to find the cost value
|
---|
1934 | if ((mSplitPlaneStrategy & BALANCED_POLYS) ||
|
---|
1935 | (mSplitPlaneStrategy & LEAST_SPLITS) ||
|
---|
1936 | (mSplitPlaneStrategy & LARGEST_POLY_AREA) ||
|
---|
1937 | (mSplitPlaneStrategy & BALANCED_VIEW_CELLS) ||
|
---|
1938 | (mSplitPlaneStrategy & BLOCKED_RAYS))
|
---|
1939 | {
|
---|
1940 | val += SplitPlaneCost(candidatePlane, *data.mPolygons);
|
---|
1941 | }
|
---|
1942 |
|
---|
1943 | // the following criteria loop over all rays to find the cost value
|
---|
1944 | if ((mSplitPlaneStrategy & BALANCED_RAYS) ||
|
---|
1945 | (mSplitPlaneStrategy & LEAST_RAY_SPLITS) ||
|
---|
1946 | (mSplitPlaneStrategy & PVS))
|
---|
1947 | {
|
---|
1948 | val += SplitPlaneCost(candidatePlane, *data.mRays, data.mPvs,
|
---|
1949 | data.mProbability, *data.mGeometry);
|
---|
1950 | }
|
---|
1951 |
|
---|
1952 | // return linear combination of the sums
|
---|
1953 | return val;
|
---|
1954 | }
|
---|
1955 |
|
---|
1956 | void BspTree::CollectLeaves(vector<BspLeaf *> &leaves) const
|
---|
1957 | {
|
---|
1958 | stack<BspNode *> nodeStack;
|
---|
1959 | nodeStack.push(mRoot);
|
---|
1960 |
|
---|
1961 | while (!nodeStack.empty())
|
---|
1962 | {
|
---|
1963 | BspNode *node = nodeStack.top();
|
---|
1964 |
|
---|
1965 | nodeStack.pop();
|
---|
1966 |
|
---|
1967 | if (node->IsLeaf())
|
---|
1968 | {
|
---|
1969 | BspLeaf *leaf = (BspLeaf *)node;
|
---|
1970 | leaves.push_back(leaf);
|
---|
1971 | }
|
---|
1972 | else
|
---|
1973 | {
|
---|
1974 | BspInterior *interior = static_cast<BspInterior *>(node);
|
---|
1975 |
|
---|
1976 | nodeStack.push(interior->GetBack());
|
---|
1977 | nodeStack.push(interior->GetFront());
|
---|
1978 | }
|
---|
1979 | }
|
---|
1980 | }
|
---|
1981 |
|
---|
1982 |
|
---|
1983 | AxisAlignedBox3 BspTree::GetBoundingBox() const
|
---|
1984 | {
|
---|
1985 | return mBbox;
|
---|
1986 | }
|
---|
1987 |
|
---|
1988 |
|
---|
1989 | void BspTree::EvaluateLeafStats(const BspTraversalData &data)
|
---|
1990 | {
|
---|
1991 | // the node became a leaf -> evaluate stats for leafs
|
---|
1992 | BspLeaf *leaf = static_cast<BspLeaf *>(data.mNode);
|
---|
1993 |
|
---|
1994 | // store maximal and minimal depth
|
---|
1995 | if (data.mDepth > mStat.maxDepth)
|
---|
1996 | mStat.maxDepth = data.mDepth;
|
---|
1997 |
|
---|
1998 | if (data.mDepth < mStat.minDepth)
|
---|
1999 | mStat.minDepth = data.mDepth;
|
---|
2000 |
|
---|
2001 | // accumulate depth to compute average depth
|
---|
2002 | mStat.accumDepth += data.mDepth;
|
---|
2003 | // accumulate rays to compute rays / leaf
|
---|
2004 | mStat.accumRays += (int)data.mRays->size();
|
---|
2005 |
|
---|
2006 | if (data.mDepth >= mTermMaxDepth)
|
---|
2007 | ++ mStat.maxDepthNodes;
|
---|
2008 |
|
---|
2009 | if (data.mPvs < mTermMinPvs)
|
---|
2010 | ++ mStat.minPvsNodes;
|
---|
2011 |
|
---|
2012 | if ((int)data.mRays->size() < mTermMinRays)
|
---|
2013 | ++ mStat.minRaysNodes;
|
---|
2014 |
|
---|
2015 | if (data.GetAvgRayContribution() > mTermMaxRayContribution)
|
---|
2016 | ++ mStat.maxRayContribNodes;
|
---|
2017 |
|
---|
2018 | if (data.mProbability <= mTermMinProbability)
|
---|
2019 | ++ mStat.minProbabilityNodes;
|
---|
2020 |
|
---|
2021 | #ifdef GTP_DEBUG
|
---|
2022 | Debug << "BSP stats: "
|
---|
2023 | << "Depth: " << data.mDepth << " (max: " << mTermMaxDepth << "), "
|
---|
2024 | << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), "
|
---|
2025 | << "Probability: " << data.mProbability << " (min: " << mTermMinProbability << "), "
|
---|
2026 | << "#polygons: " << (int)data.mPolygons->size() << " (max: " << mTermMinPolys << "), "
|
---|
2027 | << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "
|
---|
2028 | << "#pvs: " << leaf->GetViewCell()->GetPvs().EvalPvsCost() << "=, "
|
---|
2029 | << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl;
|
---|
2030 | #endif
|
---|
2031 | }
|
---|
2032 |
|
---|
2033 |
|
---|
2034 | int BspTree::_CastRay(Ray &ray)
|
---|
2035 | {
|
---|
2036 | int hits = 0;
|
---|
2037 |
|
---|
2038 | stack<BspRayTraversalData> tStack;
|
---|
2039 |
|
---|
2040 | float maxt, mint;
|
---|
2041 |
|
---|
2042 | if (!mBbox.GetRaySegment(ray, mint, maxt))
|
---|
2043 | return 0;
|
---|
2044 |
|
---|
2045 | ViewCell::NewMail();
|
---|
2046 |
|
---|
2047 | Vector3 entp = ray.Extrap(mint);
|
---|
2048 | Vector3 extp = ray.Extrap(maxt);
|
---|
2049 |
|
---|
2050 | BspNode *node = mRoot;
|
---|
2051 | BspNode *farChild = NULL;
|
---|
2052 |
|
---|
2053 | while (1)
|
---|
2054 | {
|
---|
2055 | if (!node->IsLeaf())
|
---|
2056 | {
|
---|
2057 | BspInterior *in = static_cast<BspInterior *>(node);
|
---|
2058 |
|
---|
2059 | Plane3 splitPlane = in->GetPlane();
|
---|
2060 | const int entSide = splitPlane.Side(entp);
|
---|
2061 | const int extSide = splitPlane.Side(extp);
|
---|
2062 |
|
---|
2063 | if (entSide < 0)
|
---|
2064 | {
|
---|
2065 | node = in->GetBack();
|
---|
2066 |
|
---|
2067 | if(extSide <= 0) // plane does not split ray => no far child
|
---|
2068 | continue;
|
---|
2069 |
|
---|
2070 | farChild = in->GetFront(); // plane splits ray
|
---|
2071 |
|
---|
2072 | } else if (entSide > 0)
|
---|
2073 | {
|
---|
2074 | node = in->GetFront();
|
---|
2075 |
|
---|
2076 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
2077 | continue;
|
---|
2078 |
|
---|
2079 | farChild = in->GetBack(); // plane splits ray
|
---|
2080 | }
|
---|
2081 | else // ray and plane are coincident
|
---|
2082 | {
|
---|
2083 | // WHAT TO DO IN THIS CASE ?
|
---|
2084 | //break;
|
---|
2085 | node = in->GetFront();
|
---|
2086 | continue;
|
---|
2087 | }
|
---|
2088 |
|
---|
2089 | // push data for far child
|
---|
2090 | tStack.push(BspRayTraversalData(farChild, extp, maxt));
|
---|
2091 |
|
---|
2092 | // find intersection of ray segment with plane
|
---|
2093 | float t;
|
---|
2094 | extp = splitPlane.FindIntersection(ray.GetLoc(), extp, &t);
|
---|
2095 | maxt *= t;
|
---|
2096 |
|
---|
2097 | } else // reached leaf => intersection with view cell
|
---|
2098 | {
|
---|
2099 | BspLeaf *leaf = static_cast<BspLeaf *>(node);
|
---|
2100 |
|
---|
2101 | if (!leaf->mViewCell->Mailed())
|
---|
2102 | {
|
---|
2103 | //ray.bspIntersections.push_back(Ray::BspIntersection(maxt, leaf));
|
---|
2104 | leaf->mViewCell->Mail();
|
---|
2105 | ++ hits;
|
---|
2106 | }
|
---|
2107 |
|
---|
2108 | //-- fetch the next far child from the stack
|
---|
2109 | if (tStack.empty())
|
---|
2110 | break;
|
---|
2111 |
|
---|
2112 | entp = extp;
|
---|
2113 | mint = maxt; // NOTE: need this?
|
---|
2114 |
|
---|
2115 | if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
|
---|
2116 | break;
|
---|
2117 |
|
---|
2118 | BspRayTraversalData &s = tStack.top();
|
---|
2119 |
|
---|
2120 | node = s.mNode;
|
---|
2121 | extp = s.mExitPoint;
|
---|
2122 | maxt = s.mMaxT;
|
---|
2123 |
|
---|
2124 | tStack.pop();
|
---|
2125 | }
|
---|
2126 | }
|
---|
2127 |
|
---|
2128 | return hits;
|
---|
2129 | }
|
---|
2130 |
|
---|
2131 |
|
---|
2132 | int BspTree::CastLineSegment(const Vector3 &origin,
|
---|
2133 | const Vector3 &termination,
|
---|
2134 | ViewCellContainer &viewcells)
|
---|
2135 | {
|
---|
2136 | int hits = 0;
|
---|
2137 | stack<BspRayTraversalData> tStack;
|
---|
2138 |
|
---|
2139 | float mint = 0.0f, maxt = 1.0f;
|
---|
2140 |
|
---|
2141 | //ViewCell::NewMail();
|
---|
2142 |
|
---|
2143 | Vector3 entp = origin;
|
---|
2144 | Vector3 extp = termination;
|
---|
2145 |
|
---|
2146 | BspNode *node = mRoot;
|
---|
2147 | BspNode *farChild = NULL;
|
---|
2148 |
|
---|
2149 |
|
---|
2150 | const float thresh = 1 ? 1e-6f : 0.0f;
|
---|
2151 |
|
---|
2152 | while (1)
|
---|
2153 | {
|
---|
2154 | if (!node->IsLeaf())
|
---|
2155 | {
|
---|
2156 | BspInterior *in = static_cast<BspInterior *>(node);
|
---|
2157 |
|
---|
2158 | Plane3 splitPlane = in->GetPlane();
|
---|
2159 |
|
---|
2160 | const int entSide = splitPlane.Side(entp, thresh);
|
---|
2161 | const int extSide = splitPlane.Side(extp, thresh);
|
---|
2162 |
|
---|
2163 | if (entSide < 0)
|
---|
2164 | {
|
---|
2165 | node = in->GetBack();
|
---|
2166 |
|
---|
2167 | // plane does not split ray => no far child
|
---|
2168 | if (extSide <= 0)
|
---|
2169 | continue;
|
---|
2170 |
|
---|
2171 | farChild = in->GetFront(); // plane splits ray
|
---|
2172 | }
|
---|
2173 | else if (entSide > 0)
|
---|
2174 | {
|
---|
2175 | node = in->GetFront();
|
---|
2176 |
|
---|
2177 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
2178 | continue;
|
---|
2179 |
|
---|
2180 | farChild = in->GetBack(); // plane splits ray
|
---|
2181 | }
|
---|
2182 | else // one of the ray end points is on the plane
|
---|
2183 | { // NOTE: what to do if ray is coincident with plane?
|
---|
2184 | if (extSide < 0)
|
---|
2185 | node = in->GetBack();
|
---|
2186 | else //if (extSide > 0)
|
---|
2187 | node = in->GetFront();
|
---|
2188 | //else break; // coincident => count no intersections
|
---|
2189 |
|
---|
2190 | continue; // no far child
|
---|
2191 | }
|
---|
2192 |
|
---|
2193 | // push data for far child
|
---|
2194 | tStack.push(BspRayTraversalData(farChild, extp, maxt));
|
---|
2195 |
|
---|
2196 | // find intersection of ray segment with plane
|
---|
2197 | float t;
|
---|
2198 | extp = splitPlane.FindIntersection(origin, extp, &t);
|
---|
2199 | maxt *= t;
|
---|
2200 | }
|
---|
2201 | else
|
---|
2202 | {
|
---|
2203 | // reached leaf => intersection with view cell
|
---|
2204 | BspLeaf *leaf = static_cast<BspLeaf *>(node);
|
---|
2205 |
|
---|
2206 | if (!leaf->mViewCell->Mailed())
|
---|
2207 | {
|
---|
2208 | viewcells.push_back(leaf->mViewCell);
|
---|
2209 | leaf->mViewCell->Mail();
|
---|
2210 | ++ hits;
|
---|
2211 | }
|
---|
2212 |
|
---|
2213 | //-- fetch the next far child from the stack
|
---|
2214 | if (tStack.empty())
|
---|
2215 | break;
|
---|
2216 |
|
---|
2217 | entp = extp;
|
---|
2218 | mint = maxt; // NOTE: need this?
|
---|
2219 |
|
---|
2220 | BspRayTraversalData &s = tStack.top();
|
---|
2221 |
|
---|
2222 | node = s.mNode;
|
---|
2223 | extp = s.mExitPoint;
|
---|
2224 | maxt = s.mMaxT;
|
---|
2225 |
|
---|
2226 | tStack.pop();
|
---|
2227 | }
|
---|
2228 | }
|
---|
2229 |
|
---|
2230 | return hits;
|
---|
2231 | }
|
---|
2232 |
|
---|
2233 |
|
---|
2234 | void BspTree::CollectViewCells(ViewCellContainer &viewCells) const
|
---|
2235 | {
|
---|
2236 | stack<BspNode *> nodeStack;
|
---|
2237 | nodeStack.push(mRoot);
|
---|
2238 |
|
---|
2239 | ViewCell::NewMail();
|
---|
2240 |
|
---|
2241 | while (!nodeStack.empty())
|
---|
2242 | {
|
---|
2243 | BspNode *node = nodeStack.top();
|
---|
2244 | nodeStack.pop();
|
---|
2245 |
|
---|
2246 | if (node->IsLeaf())
|
---|
2247 | {
|
---|
2248 | ViewCellLeaf *viewCell = static_cast<BspLeaf *>(node)->mViewCell;
|
---|
2249 |
|
---|
2250 | if (!viewCell->Mailed())
|
---|
2251 | {
|
---|
2252 | viewCell->Mail();
|
---|
2253 | viewCells.push_back(viewCell);
|
---|
2254 | }
|
---|
2255 | }
|
---|
2256 | else
|
---|
2257 | {
|
---|
2258 | BspInterior *interior = static_cast<BspInterior *>(node);
|
---|
2259 |
|
---|
2260 | nodeStack.push(interior->GetFront());
|
---|
2261 | nodeStack.push(interior->GetBack());
|
---|
2262 | }
|
---|
2263 | }
|
---|
2264 | }
|
---|
2265 |
|
---|
2266 |
|
---|
2267 | BspTreeStatistics &BspTree::GetStat()
|
---|
2268 | {
|
---|
2269 | return mStat;
|
---|
2270 | }
|
---|
2271 |
|
---|
2272 |
|
---|
2273 | float BspTree::AccumulatedRayLength(BoundedRayContainer &rays) const
|
---|
2274 | {
|
---|
2275 | float len = 0;
|
---|
2276 |
|
---|
2277 | BoundedRayContainer::const_iterator it, it_end = rays.end();
|
---|
2278 |
|
---|
2279 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
2280 | {
|
---|
2281 | len += SqrDistance((*it)->mRay->Extrap((*it)->mMinT),
|
---|
2282 | (*it)->mRay->Extrap((*it)->mMaxT));
|
---|
2283 | }
|
---|
2284 |
|
---|
2285 | return len;
|
---|
2286 | }
|
---|
2287 |
|
---|
2288 |
|
---|
2289 | int BspTree::SplitRays(const Plane3 &plane,
|
---|
2290 | BoundedRayContainer &rays,
|
---|
2291 | BoundedRayContainer &frontRays,
|
---|
2292 | BoundedRayContainer &backRays)
|
---|
2293 | {
|
---|
2294 | int splits = 0;
|
---|
2295 |
|
---|
2296 | while (!rays.empty())
|
---|
2297 | {
|
---|
2298 | BoundedRay *bRay = rays.back();
|
---|
2299 | Ray *ray = bRay->mRay;
|
---|
2300 | float minT = bRay->mMinT;
|
---|
2301 | float maxT = bRay->mMaxT;
|
---|
2302 |
|
---|
2303 | rays.pop_back();
|
---|
2304 |
|
---|
2305 | Vector3 entP, extP;
|
---|
2306 |
|
---|
2307 | const int cf =
|
---|
2308 | ray->ClassifyPlane(plane, minT, maxT, entP, extP);
|
---|
2309 |
|
---|
2310 | // set id to ray classification
|
---|
2311 | ray->SetId(cf);
|
---|
2312 |
|
---|
2313 | switch (cf)
|
---|
2314 | {
|
---|
2315 | case Ray::COINCIDENT: // TODO: should really discard ray?
|
---|
2316 | frontRays.push_back(bRay);
|
---|
2317 | break;
|
---|
2318 | case Ray::BACK:
|
---|
2319 | backRays.push_back(bRay);
|
---|
2320 | break;
|
---|
2321 | case Ray::FRONT:
|
---|
2322 | frontRays.push_back(bRay);
|
---|
2323 | break;
|
---|
2324 | case Ray::FRONT_BACK:
|
---|
2325 | {
|
---|
2326 | // find intersection of ray segment with plane
|
---|
2327 | const float t = plane.FindT(ray->GetLoc(), extP);
|
---|
2328 |
|
---|
2329 | const float newT = t * maxT;
|
---|
2330 |
|
---|
2331 | frontRays.push_back(new BoundedRay(ray, minT, newT));
|
---|
2332 | backRays.push_back(new BoundedRay(ray, newT, maxT));
|
---|
2333 |
|
---|
2334 | DEL_PTR(bRay);
|
---|
2335 | }
|
---|
2336 | break;
|
---|
2337 | case Ray::BACK_FRONT:
|
---|
2338 | {
|
---|
2339 | // find intersection of ray segment with plane
|
---|
2340 | const float t = plane.FindT(ray->GetLoc(), extP);
|
---|
2341 | const float newT = t * bRay->mMaxT;
|
---|
2342 |
|
---|
2343 | backRays.push_back(new BoundedRay(ray, minT, newT));
|
---|
2344 | frontRays.push_back(new BoundedRay(ray, newT, maxT));
|
---|
2345 |
|
---|
2346 | DEL_PTR(bRay);
|
---|
2347 |
|
---|
2348 | ++ splits;
|
---|
2349 | }
|
---|
2350 | break;
|
---|
2351 | default:
|
---|
2352 | Debug << "Should not come here 4" << endl;
|
---|
2353 | break;
|
---|
2354 | }
|
---|
2355 | }
|
---|
2356 |
|
---|
2357 | return splits;
|
---|
2358 | }
|
---|
2359 |
|
---|
2360 | void BspTree::ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const
|
---|
2361 | {
|
---|
2362 | BspNode *lastNode;
|
---|
2363 |
|
---|
2364 | do
|
---|
2365 | {
|
---|
2366 | lastNode = n;
|
---|
2367 |
|
---|
2368 | // want to get planes defining geometry of this node => don't take
|
---|
2369 | // split plane of node itself
|
---|
2370 | n = n->GetParent();
|
---|
2371 |
|
---|
2372 | if (n)
|
---|
2373 | {
|
---|
2374 | BspInterior *interior = static_cast<BspInterior *>(n);
|
---|
2375 | Plane3 halfSpace = static_cast<BspInterior *>(interior)->GetPlane();
|
---|
2376 |
|
---|
2377 | if (interior->GetBack() != lastNode)
|
---|
2378 | halfSpace.ReverseOrientation();
|
---|
2379 |
|
---|
2380 | halfSpaces.push_back(halfSpace);
|
---|
2381 | }
|
---|
2382 | }
|
---|
2383 | while (n);
|
---|
2384 | }
|
---|
2385 |
|
---|
2386 |
|
---|
2387 | void BspTree::ConstructGeometry(ViewCell *vc,
|
---|
2388 | BspNodeGeometry &vcGeom) const
|
---|
2389 | {
|
---|
2390 | // if false, cannot construct geometry for interior leaf
|
---|
2391 | if (!mViewCellsTree)
|
---|
2392 | return;
|
---|
2393 |
|
---|
2394 | ViewCellContainer leaves;
|
---|
2395 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
2396 |
|
---|
2397 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
2398 |
|
---|
2399 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
2400 | {
|
---|
2401 | // per definition out of bounds cell has zero volume
|
---|
2402 | if (IsOutOfBounds(*it))
|
---|
2403 | continue;
|
---|
2404 |
|
---|
2405 | BspViewCell *bspVc = static_cast<BspViewCell *>(*it);
|
---|
2406 | vector<BspLeaf *>::const_iterator bit, bit_end = bspVc->mLeaves.end();
|
---|
2407 |
|
---|
2408 | for (bit = bspVc->mLeaves.begin(); bit != bit_end; ++ bit)
|
---|
2409 | {
|
---|
2410 | BspLeaf *l = *bit;
|
---|
2411 | ConstructGeometry(l, vcGeom);
|
---|
2412 | }
|
---|
2413 | }
|
---|
2414 | }
|
---|
2415 |
|
---|
2416 |
|
---|
2417 | void BspTree::ConstructGeometry(BspNode *n,
|
---|
2418 | BspNodeGeometry &geom) const
|
---|
2419 | {
|
---|
2420 | vector<Plane3> halfSpaces;
|
---|
2421 | ExtractHalfSpaces(n, halfSpaces);
|
---|
2422 |
|
---|
2423 | PolygonContainer candidatePolys;
|
---|
2424 | vector<Plane3> candidatePlanes;
|
---|
2425 |
|
---|
2426 | vector<Plane3>::const_iterator pit, pit_end = halfSpaces.end();
|
---|
2427 |
|
---|
2428 | // bounded planes are added to the polygons
|
---|
2429 | for (pit = halfSpaces.begin(); pit != pit_end; ++ pit)
|
---|
2430 | {
|
---|
2431 | Polygon3 *p = GetBoundingBox().CrossSection(*pit);
|
---|
2432 |
|
---|
2433 | if (p->Valid(mEpsilon))
|
---|
2434 | {
|
---|
2435 | candidatePolys.push_back(p);
|
---|
2436 | candidatePlanes.push_back(*pit);
|
---|
2437 | }
|
---|
2438 | }
|
---|
2439 |
|
---|
2440 | // add faces of bounding box (also could be faces of the cell)
|
---|
2441 | for (int i = 0; i < 6; ++ i)
|
---|
2442 | {
|
---|
2443 | VertexContainer vertices;
|
---|
2444 |
|
---|
2445 | for (int j = 0; j < 4; ++ j)
|
---|
2446 | vertices.push_back(mBbox.GetFace(i).mVertices[j]);
|
---|
2447 |
|
---|
2448 | Polygon3 *poly = new Polygon3(vertices);
|
---|
2449 |
|
---|
2450 | candidatePolys.push_back(poly);
|
---|
2451 | candidatePlanes.push_back(poly->GetSupportingPlane());
|
---|
2452 | }
|
---|
2453 |
|
---|
2454 | for (int i = 0; i < (int)candidatePolys.size(); ++ i)
|
---|
2455 | {
|
---|
2456 | // polygon is split by all other planes
|
---|
2457 | for (int j = 0; (j < (int)halfSpaces.size()) && candidatePolys[i]; ++ j)
|
---|
2458 | {
|
---|
2459 | if (i == j) // polygon and plane are coincident
|
---|
2460 | continue;
|
---|
2461 |
|
---|
2462 | VertexContainer splitPts;
|
---|
2463 | Polygon3 *frontPoly, *backPoly;
|
---|
2464 |
|
---|
2465 | const int cf =
|
---|
2466 | candidatePolys[i]->ClassifyPlane(halfSpaces[j],
|
---|
2467 | mEpsilon);
|
---|
2468 |
|
---|
2469 | switch (cf)
|
---|
2470 | {
|
---|
2471 | case Polygon3::SPLIT:
|
---|
2472 | frontPoly = new Polygon3();
|
---|
2473 | backPoly = new Polygon3();
|
---|
2474 |
|
---|
2475 | candidatePolys[i]->Split(halfSpaces[j],
|
---|
2476 | *frontPoly,
|
---|
2477 | *backPoly,
|
---|
2478 | mEpsilon);
|
---|
2479 |
|
---|
2480 | DEL_PTR(candidatePolys[i]);
|
---|
2481 |
|
---|
2482 | if (backPoly->Valid(mEpsilon))
|
---|
2483 | candidatePolys[i] = backPoly;
|
---|
2484 | else
|
---|
2485 | DEL_PTR(backPoly);
|
---|
2486 |
|
---|
2487 | // outside, don't need this
|
---|
2488 | DEL_PTR(frontPoly);
|
---|
2489 | break;
|
---|
2490 | // polygon outside of halfspace
|
---|
2491 | case Polygon3::FRONT_SIDE:
|
---|
2492 | DEL_PTR(candidatePolys[i]);
|
---|
2493 | break;
|
---|
2494 | // just take polygon as it is
|
---|
2495 | case Polygon3::BACK_SIDE:
|
---|
2496 | case Polygon3::COINCIDENT:
|
---|
2497 | default:
|
---|
2498 | break;
|
---|
2499 | }
|
---|
2500 | }
|
---|
2501 |
|
---|
2502 | if (candidatePolys[i])
|
---|
2503 | {
|
---|
2504 | geom.Add(candidatePolys[i], candidatePlanes[i]);
|
---|
2505 | }
|
---|
2506 | }
|
---|
2507 | }
|
---|
2508 |
|
---|
2509 |
|
---|
2510 | void BspTree::SetViewCellsManager(ViewCellsManager *vcm)
|
---|
2511 | {
|
---|
2512 | mViewCellsManager = vcm;
|
---|
2513 | }
|
---|
2514 |
|
---|
2515 |
|
---|
2516 | typedef pair<BspNode *, BspNodeGeometry *> bspNodePair;
|
---|
2517 |
|
---|
2518 |
|
---|
2519 | int BspTree::FindNeighbors(BspNode *n,
|
---|
2520 | vector<BspLeaf *> &neighbors,
|
---|
2521 | const bool onlyUnmailed) const
|
---|
2522 | {
|
---|
2523 | stack<bspNodePair> nodeStack;
|
---|
2524 |
|
---|
2525 | BspNodeGeometry nodeGeom;
|
---|
2526 | ConstructGeometry(n, nodeGeom);
|
---|
2527 |
|
---|
2528 | // split planes from the root to this node
|
---|
2529 | // needed to verify that we found neighbor leaf
|
---|
2530 | // TODO: really needed?
|
---|
2531 | vector<Plane3> halfSpaces;
|
---|
2532 | ExtractHalfSpaces(n, halfSpaces);
|
---|
2533 |
|
---|
2534 |
|
---|
2535 | BspNodeGeometry *rgeom = new BspNodeGeometry();
|
---|
2536 | ConstructGeometry(mRoot, *rgeom);
|
---|
2537 |
|
---|
2538 | nodeStack.push(bspNodePair(mRoot, rgeom));
|
---|
2539 |
|
---|
2540 | while (!nodeStack.empty())
|
---|
2541 | {
|
---|
2542 | BspNode *node = nodeStack.top().first;
|
---|
2543 | BspNodeGeometry *geom = nodeStack.top().second;
|
---|
2544 |
|
---|
2545 | nodeStack.pop();
|
---|
2546 |
|
---|
2547 | if (node->IsLeaf())
|
---|
2548 | {
|
---|
2549 | // test if this leaf is in valid view space
|
---|
2550 | if (node->TreeValid() &&
|
---|
2551 | (node != n) &&
|
---|
2552 | (!onlyUnmailed || !node->Mailed()))
|
---|
2553 | {
|
---|
2554 | bool isAdjacent = true;
|
---|
2555 |
|
---|
2556 | if (1)
|
---|
2557 | {
|
---|
2558 | // test all planes of current node if still adjacent
|
---|
2559 | for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i)
|
---|
2560 | {
|
---|
2561 | const int cf =
|
---|
2562 | Polygon3::ClassifyPlane(geom->GetPolys(),
|
---|
2563 | halfSpaces[i],
|
---|
2564 | mEpsilon);
|
---|
2565 |
|
---|
2566 | if (cf == Polygon3::FRONT_SIDE)
|
---|
2567 | {
|
---|
2568 | isAdjacent = false;
|
---|
2569 | }
|
---|
2570 | }
|
---|
2571 | }
|
---|
2572 | else
|
---|
2573 | {
|
---|
2574 | // TODO: why is this wrong??
|
---|
2575 | // test all planes of current node if still adjacent
|
---|
2576 | for (int i = 0; (i < nodeGeom.Size()) && isAdjacent; ++ i)
|
---|
2577 | {
|
---|
2578 | Polygon3 *poly = nodeGeom.GetPolys()[i];
|
---|
2579 |
|
---|
2580 | const int cf =
|
---|
2581 | Polygon3::ClassifyPlane(geom->GetPolys(),
|
---|
2582 | poly->GetSupportingPlane(),
|
---|
2583 | mEpsilon);
|
---|
2584 |
|
---|
2585 | if (cf == Polygon3::FRONT_SIDE)
|
---|
2586 | {
|
---|
2587 | isAdjacent = false;
|
---|
2588 | }
|
---|
2589 | }
|
---|
2590 | }
|
---|
2591 | // neighbor was found
|
---|
2592 | if (isAdjacent)
|
---|
2593 | {
|
---|
2594 | neighbors.push_back(static_cast<BspLeaf *>(node));
|
---|
2595 | }
|
---|
2596 | }
|
---|
2597 | }
|
---|
2598 | else
|
---|
2599 | {
|
---|
2600 | BspInterior *interior = static_cast<BspInterior *>(node);
|
---|
2601 |
|
---|
2602 | const int cf = Polygon3::ClassifyPlane(nodeGeom.GetPolys(),
|
---|
2603 | interior->GetPlane(),
|
---|
2604 | mEpsilon);
|
---|
2605 |
|
---|
2606 | BspNode *front = interior->GetFront();
|
---|
2607 | BspNode *back = interior->GetBack();
|
---|
2608 |
|
---|
2609 | BspNodeGeometry *fGeom = new BspNodeGeometry();
|
---|
2610 | BspNodeGeometry *bGeom = new BspNodeGeometry();
|
---|
2611 |
|
---|
2612 | geom->SplitGeometry(*fGeom,
|
---|
2613 | *bGeom,
|
---|
2614 | interior->GetPlane(),
|
---|
2615 | mBbox,
|
---|
2616 | //0.0000001f);
|
---|
2617 | mEpsilon);
|
---|
2618 |
|
---|
2619 | if (cf == Polygon3::BACK_SIDE)
|
---|
2620 | {
|
---|
2621 | nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
|
---|
2622 | DEL_PTR(fGeom);
|
---|
2623 | }
|
---|
2624 | else
|
---|
2625 | {
|
---|
2626 | if (cf == Polygon3::FRONT_SIDE)
|
---|
2627 | {
|
---|
2628 | nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
|
---|
2629 | DEL_PTR(bGeom);
|
---|
2630 | }
|
---|
2631 | else
|
---|
2632 | { // random decision
|
---|
2633 | nodeStack.push(bspNodePair(front, fGeom));
|
---|
2634 | nodeStack.push(bspNodePair(back, bGeom));
|
---|
2635 | }
|
---|
2636 | }
|
---|
2637 | }
|
---|
2638 |
|
---|
2639 | DEL_PTR(geom);
|
---|
2640 | }
|
---|
2641 |
|
---|
2642 | return (int)neighbors.size();
|
---|
2643 | }
|
---|
2644 |
|
---|
2645 |
|
---|
2646 | BspLeaf *BspTree::GetRandomLeaf(const bool onlyUnmailed)
|
---|
2647 | {
|
---|
2648 | stack<BspNode *> nodeStack;
|
---|
2649 |
|
---|
2650 | nodeStack.push(mRoot);
|
---|
2651 |
|
---|
2652 | int mask = rand();
|
---|
2653 |
|
---|
2654 | while (!nodeStack.empty())
|
---|
2655 | {
|
---|
2656 | BspNode *node = nodeStack.top();
|
---|
2657 | nodeStack.pop();
|
---|
2658 |
|
---|
2659 | if (node->IsLeaf())
|
---|
2660 | {
|
---|
2661 | if ( (!onlyUnmailed || !node->Mailed()) )
|
---|
2662 | return static_cast<BspLeaf *>(node);
|
---|
2663 | }
|
---|
2664 | else
|
---|
2665 | {
|
---|
2666 | BspInterior *interior = static_cast<BspInterior *>(node);
|
---|
2667 |
|
---|
2668 | // random decision
|
---|
2669 | if (mask & 1)
|
---|
2670 | nodeStack.push(interior->GetBack());
|
---|
2671 | else
|
---|
2672 | nodeStack.push(interior->GetFront());
|
---|
2673 |
|
---|
2674 | mask = mask >> 1;
|
---|
2675 | }
|
---|
2676 | }
|
---|
2677 |
|
---|
2678 | return NULL;
|
---|
2679 | }
|
---|
2680 |
|
---|
2681 |
|
---|
2682 | void BspTree::AddToPvs(BspLeaf *leaf,
|
---|
2683 | const BoundedRayContainer &rays,
|
---|
2684 | int &sampleContributions,
|
---|
2685 | int &contributingSamples)
|
---|
2686 | {
|
---|
2687 | sampleContributions = 0;
|
---|
2688 | contributingSamples = 0;
|
---|
2689 |
|
---|
2690 | BoundedRayContainer::const_iterator it, it_end = rays.end();
|
---|
2691 |
|
---|
2692 | ViewCellLeaf *vc = leaf->GetViewCell();
|
---|
2693 |
|
---|
2694 | // add contributions from samples to the PVS
|
---|
2695 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
2696 | {
|
---|
2697 | float contribution = 0;
|
---|
2698 | Ray *ray = (*it)->mRay;
|
---|
2699 | //float relContribution;
|
---|
2700 | if (!ray->intersections.empty())
|
---|
2701 | contribution += vc->GetPvs().AddSample(ray->intersections[0].mObject,
|
---|
2702 | 1.0f);
|
---|
2703 | //relContribution);
|
---|
2704 |
|
---|
2705 | if (ray->sourceObject.mObject)
|
---|
2706 | contribution += vc->GetPvs().AddSample(ray->sourceObject.mObject,
|
---|
2707 | 1.0f);
|
---|
2708 | //relContribution);
|
---|
2709 |
|
---|
2710 | if (contribution > 0)
|
---|
2711 | {
|
---|
2712 | sampleContributions += (int)contribution;
|
---|
2713 | ++ contributingSamples;
|
---|
2714 | }
|
---|
2715 |
|
---|
2716 | //if (ray->mFlags & Ray::STORE_BSP_INTERSECTIONS)
|
---|
2717 | // ray->bspIntersections.push_back(Ray::BspIntersection((*it)->mMinT, this));
|
---|
2718 | }
|
---|
2719 | }
|
---|
2720 |
|
---|
2721 |
|
---|
2722 | int BspTree::ComputePvsSize(const BoundedRayContainer &rays) const
|
---|
2723 | {
|
---|
2724 | int pvsSize = 0;
|
---|
2725 |
|
---|
2726 | BoundedRayContainer::const_iterator rit, rit_end = rays.end();
|
---|
2727 |
|
---|
2728 | Intersectable::NewMail();
|
---|
2729 |
|
---|
2730 | for (rit = rays.begin(); rit != rays.end(); ++ rit)
|
---|
2731 | {
|
---|
2732 | Ray *ray = (*rit)->mRay;
|
---|
2733 |
|
---|
2734 | if (!ray->intersections.empty())
|
---|
2735 | {
|
---|
2736 | if (!ray->intersections[0].mObject->Mailed())
|
---|
2737 | {
|
---|
2738 | ray->intersections[0].mObject->Mail();
|
---|
2739 | ++ pvsSize;
|
---|
2740 | }
|
---|
2741 | }
|
---|
2742 | if (ray->sourceObject.mObject)
|
---|
2743 | {
|
---|
2744 | if (!ray->sourceObject.mObject->Mailed())
|
---|
2745 | {
|
---|
2746 | ray->sourceObject.mObject->Mail();
|
---|
2747 | ++ pvsSize;
|
---|
2748 | }
|
---|
2749 | }
|
---|
2750 | }
|
---|
2751 |
|
---|
2752 | return pvsSize;
|
---|
2753 | }
|
---|
2754 |
|
---|
2755 |
|
---|
2756 | float BspTree::GetEpsilon() const
|
---|
2757 | {
|
---|
2758 | return mEpsilon;
|
---|
2759 | }
|
---|
2760 |
|
---|
2761 |
|
---|
2762 | int BspTree::CollectMergeCandidates(const vector<BspLeaf *> leaves,
|
---|
2763 | vector<MergeCandidate> &candidates)
|
---|
2764 | {
|
---|
2765 | BspLeaf::NewMail();
|
---|
2766 |
|
---|
2767 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
2768 |
|
---|
2769 | int numCandidates = 0;
|
---|
2770 |
|
---|
2771 | // find merge candidates and push them into queue
|
---|
2772 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
2773 | {
|
---|
2774 | BspLeaf *leaf = *it;
|
---|
2775 |
|
---|
2776 | // the same leaves must not be part of two merge candidates
|
---|
2777 | leaf->Mail();
|
---|
2778 | vector<BspLeaf *> neighbors;
|
---|
2779 | FindNeighbors(leaf, neighbors, true);
|
---|
2780 |
|
---|
2781 | vector<BspLeaf *>::const_iterator nit, nit_end = neighbors.end();
|
---|
2782 |
|
---|
2783 | // TODO: test if at least one ray goes from one leaf to the other
|
---|
2784 | for (nit = neighbors.begin(); nit != nit_end; ++ nit)
|
---|
2785 | {
|
---|
2786 | if ((*nit)->GetViewCell() != leaf->GetViewCell())
|
---|
2787 | {
|
---|
2788 | MergeCandidate mc(leaf->GetViewCell(), (*nit)->GetViewCell());
|
---|
2789 | candidates.push_back(mc);
|
---|
2790 |
|
---|
2791 | ++ numCandidates;
|
---|
2792 | if ((numCandidates % 1000) == 0)
|
---|
2793 | {
|
---|
2794 | cout << "collected " << numCandidates << " merge candidates" << endl;
|
---|
2795 | }
|
---|
2796 | }
|
---|
2797 | }
|
---|
2798 | }
|
---|
2799 |
|
---|
2800 | Debug << "merge queue: " << (int)candidates.size() << endl;
|
---|
2801 | Debug << "leaves in queue: " << numCandidates << endl;
|
---|
2802 |
|
---|
2803 |
|
---|
2804 | return (int)leaves.size();
|
---|
2805 | }
|
---|
2806 |
|
---|
2807 |
|
---|
2808 | int BspTree::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
2809 | vector<MergeCandidate> &candidates)
|
---|
2810 | {
|
---|
2811 | ViewCell::NewMail();
|
---|
2812 | long startTime = GetTime();
|
---|
2813 |
|
---|
2814 | map<BspLeaf *, vector<BspLeaf*> > neighborMap;
|
---|
2815 | ViewCellContainer::const_iterator iit;
|
---|
2816 |
|
---|
2817 | int numLeaves = 0;
|
---|
2818 |
|
---|
2819 | BspLeaf::NewMail();
|
---|
2820 |
|
---|
2821 | for (int i = 0; i < (int)rays.size(); ++ i)
|
---|
2822 | {
|
---|
2823 | VssRay *ray = rays[i];
|
---|
2824 | #if VSS_STORE_VIEWCELLS
|
---|
2825 |
|
---|
2826 | // traverse leaves stored in the rays and compare and
|
---|
2827 | // merge consecutive leaves (i.e., the neighbors in the tree)
|
---|
2828 | if (ray->mViewCells.size() < 2)
|
---|
2829 | continue;
|
---|
2830 |
|
---|
2831 | iit = ray->mViewCells.begin();
|
---|
2832 | BspViewCell *bspVc = static_cast<BspViewCell *>(*(iit ++));
|
---|
2833 | BspLeaf *leaf = bspVc->mLeaves[0];
|
---|
2834 |
|
---|
2835 | // traverse intersections
|
---|
2836 | // consecutive leaves are neighbors => add them to queue
|
---|
2837 | for (; iit != ray->mViewCells.end(); ++ iit)
|
---|
2838 | {
|
---|
2839 | // next pair
|
---|
2840 | BspLeaf *prevLeaf = leaf;
|
---|
2841 | bspVc = static_cast<BspViewCell *>(*iit);
|
---|
2842 | leaf = bspVc->mLeaves[0];
|
---|
2843 |
|
---|
2844 | // view space not valid or same view cell
|
---|
2845 | if (!leaf->TreeValid() || !prevLeaf->TreeValid() ||
|
---|
2846 | (leaf->GetViewCell() == prevLeaf->GetViewCell()))
|
---|
2847 | continue;
|
---|
2848 |
|
---|
2849 | vector<BspLeaf *> &neighbors = neighborMap[leaf];
|
---|
2850 |
|
---|
2851 | bool found = false;
|
---|
2852 |
|
---|
2853 | // both leaves inserted in queue already =>
|
---|
2854 | // look if double pair already exists
|
---|
2855 | if (leaf->Mailed() && prevLeaf->Mailed())
|
---|
2856 | {
|
---|
2857 | vector<BspLeaf *>::const_iterator it, it_end = neighbors.end();
|
---|
2858 |
|
---|
2859 | for (it = neighbors.begin(); !found && (it != it_end); ++ it)
|
---|
2860 | if (*it == prevLeaf)
|
---|
2861 | found = true; // already in queue
|
---|
2862 | }
|
---|
2863 |
|
---|
2864 | if (!found)
|
---|
2865 | {
|
---|
2866 | // this pair is not in map yet
|
---|
2867 | // => insert into the neighbor map and the queue
|
---|
2868 | neighbors.push_back(prevLeaf);
|
---|
2869 | neighborMap[prevLeaf].push_back(leaf);
|
---|
2870 |
|
---|
2871 | leaf->Mail();
|
---|
2872 | prevLeaf->Mail();
|
---|
2873 |
|
---|
2874 | MergeCandidate mc(leaf->GetViewCell(), prevLeaf->GetViewCell());
|
---|
2875 |
|
---|
2876 | candidates.push_back(mc);
|
---|
2877 |
|
---|
2878 | if ((candidates.size() % 1000) == 0)
|
---|
2879 | {
|
---|
2880 | cout << "collected " << (int)candidates.size() << " merge candidates" << endl;
|
---|
2881 | }
|
---|
2882 | }
|
---|
2883 | }
|
---|
2884 | #endif
|
---|
2885 | }
|
---|
2886 |
|
---|
2887 | Debug << "neighbormap size: " << (int)neighborMap.size() << endl;
|
---|
2888 | Debug << "merge queue: " << (int)candidates.size() << endl;
|
---|
2889 | Debug << "leaves in queue: " << numLeaves << endl;
|
---|
2890 |
|
---|
2891 | #if 0
|
---|
2892 | //////////
|
---|
2893 | //-- collect the leaves which haven't been found by ray casting
|
---|
2894 | cout << "finding additional merge candidates using geometry" << endl;
|
---|
2895 | vector<BspLeaf *> leaves;
|
---|
2896 | CollectLeaves(leaves, true);
|
---|
2897 | Debug << "found " << (int)leaves.size() << " new leaves" << endl << endl;
|
---|
2898 | CollectMergeCandidates(leaves, candidates);
|
---|
2899 | #endif
|
---|
2900 |
|
---|
2901 | return numLeaves;
|
---|
2902 | }
|
---|
2903 |
|
---|
2904 |
|
---|
2905 |
|
---|
2906 | /***************************************************************/
|
---|
2907 | /* BspNodeGeometry Implementation */
|
---|
2908 | /***************************************************************/
|
---|
2909 |
|
---|
2910 |
|
---|
2911 | BspNodeGeometry::BspNodeGeometry(const BspNodeGeometry &rhs)
|
---|
2912 | {
|
---|
2913 | mPolys.reserve(rhs.mPolys.size());
|
---|
2914 | mPlanes.reserve(rhs.mPolys.size());
|
---|
2915 |
|
---|
2916 | PolygonContainer::const_iterator it, it_end = rhs.mPolys.end();
|
---|
2917 |
|
---|
2918 | int i = 0;
|
---|
2919 |
|
---|
2920 | for (it = rhs.mPolys.begin(); it != it_end; ++ it, ++i)
|
---|
2921 | {
|
---|
2922 | Polygon3 *poly = *it;
|
---|
2923 | Add(new Polygon3(*poly), rhs.mPlanes[i]);
|
---|
2924 | }
|
---|
2925 | }
|
---|
2926 |
|
---|
2927 |
|
---|
2928 | BspNodeGeometry& BspNodeGeometry::operator=(const BspNodeGeometry& g)
|
---|
2929 | {
|
---|
2930 | if (this == &g)
|
---|
2931 | return *this;
|
---|
2932 |
|
---|
2933 | CLEAR_CONTAINER(mPolys);
|
---|
2934 |
|
---|
2935 | mPolys.reserve(g.mPolys.size());
|
---|
2936 | mPlanes.reserve(g.mPolys.size());
|
---|
2937 |
|
---|
2938 | PolygonContainer::const_iterator it, it_end = g.mPolys.end();
|
---|
2939 |
|
---|
2940 | int i = 0;
|
---|
2941 |
|
---|
2942 | for (it = g.mPolys.begin(); it != it_end; ++ it, ++ i)
|
---|
2943 | {
|
---|
2944 | Polygon3 *poly = *it;
|
---|
2945 | Add(new Polygon3(*poly), g.mPlanes[i]);
|
---|
2946 | }
|
---|
2947 |
|
---|
2948 | return *this;
|
---|
2949 | }
|
---|
2950 |
|
---|
2951 |
|
---|
2952 | BspNodeGeometry::BspNodeGeometry(const PolygonContainer &polys)
|
---|
2953 | {
|
---|
2954 | mPolys = polys;
|
---|
2955 | mPlanes.reserve(polys.size());
|
---|
2956 |
|
---|
2957 | PolygonContainer::const_iterator it, it_end = polys.end();
|
---|
2958 |
|
---|
2959 | for (it = polys.begin(); it != it_end; ++ it)
|
---|
2960 | {
|
---|
2961 | Polygon3 *poly = *it;
|
---|
2962 | mPlanes.push_back(poly->GetSupportingPlane());
|
---|
2963 | }
|
---|
2964 | }
|
---|
2965 |
|
---|
2966 |
|
---|
2967 | BspNodeGeometry::~BspNodeGeometry()
|
---|
2968 | {
|
---|
2969 | CLEAR_CONTAINER(mPolys);
|
---|
2970 | }
|
---|
2971 |
|
---|
2972 |
|
---|
2973 | int BspNodeGeometry::Size() const
|
---|
2974 | {
|
---|
2975 | return (int)mPolys.size();
|
---|
2976 | }
|
---|
2977 |
|
---|
2978 |
|
---|
2979 | const PolygonContainer &BspNodeGeometry::GetPolys()
|
---|
2980 | {
|
---|
2981 | return mPolys;
|
---|
2982 | }
|
---|
2983 |
|
---|
2984 |
|
---|
2985 | void BspNodeGeometry::Add(Polygon3 *p, const Plane3 &plane)
|
---|
2986 | {
|
---|
2987 | mPolys.push_back(p);
|
---|
2988 | mPlanes.push_back(plane);
|
---|
2989 | }
|
---|
2990 |
|
---|
2991 |
|
---|
2992 | float BspNodeGeometry::GetArea() const
|
---|
2993 | {
|
---|
2994 | return Polygon3::GetArea(mPolys);
|
---|
2995 | }
|
---|
2996 |
|
---|
2997 |
|
---|
2998 | float BspNodeGeometry::GetVolume() const
|
---|
2999 | {
|
---|
3000 | //-- compute volume using tetrahedralization of the geometry
|
---|
3001 | // and adding the volume of the single tetrahedrons
|
---|
3002 | float volume = 0;
|
---|
3003 | const float f = 1.0f / 6.0f;
|
---|
3004 |
|
---|
3005 | PolygonContainer::const_iterator pit, pit_end = mPolys.end();
|
---|
3006 |
|
---|
3007 | // note: can take arbitrary point, e.g., the origin. However,
|
---|
3008 | // we rather take the center of mass to prevents precision errors
|
---|
3009 | const Vector3 center = CenterOfMass();
|
---|
3010 |
|
---|
3011 | for (pit = mPolys.begin(); pit != pit_end; ++ pit)
|
---|
3012 | {
|
---|
3013 | Polygon3 *poly = *pit;
|
---|
3014 | const Vector3 v0 = poly->mVertices[0] - center;
|
---|
3015 |
|
---|
3016 | for (int i = 1; i < (int)poly->mVertices.size() - 1; ++ i)
|
---|
3017 | {
|
---|
3018 | const Vector3 v1 = poly->mVertices[i] - center;
|
---|
3019 | const Vector3 v2 = poly->mVertices[i + 1] - center;
|
---|
3020 |
|
---|
3021 | // more robust version using abs and the center of mass
|
---|
3022 | volume += fabs (f * (DotProd(v0, CrossProd(v1, v2))));
|
---|
3023 | }
|
---|
3024 | }
|
---|
3025 |
|
---|
3026 | return volume;
|
---|
3027 | }
|
---|
3028 |
|
---|
3029 |
|
---|
3030 | void BspNodeGeometry::GetBoundingBox(AxisAlignedBox3 &box) const
|
---|
3031 | {
|
---|
3032 | box.Initialize();
|
---|
3033 | box.Include(mPolys);
|
---|
3034 | }
|
---|
3035 |
|
---|
3036 |
|
---|
3037 | int BspNodeGeometry::Side(const Plane3 &plane, const float eps) const
|
---|
3038 | {
|
---|
3039 | PolygonContainer::const_iterator it, it_end = mPolys.end();
|
---|
3040 |
|
---|
3041 | bool onFrontSide = false;
|
---|
3042 | bool onBackSide = false;
|
---|
3043 |
|
---|
3044 | for (it = mPolys.begin(); it != it_end; ++ it)
|
---|
3045 | {
|
---|
3046 | const int side = (*it)->Side(plane, eps);
|
---|
3047 |
|
---|
3048 | if (side == -1)
|
---|
3049 | onBackSide = true;
|
---|
3050 | else if (side == 1)
|
---|
3051 | onFrontSide = true;
|
---|
3052 |
|
---|
3053 | if ((side == 0) || (onBackSide && onFrontSide))
|
---|
3054 | return 0;
|
---|
3055 | }
|
---|
3056 |
|
---|
3057 | if (onFrontSide)
|
---|
3058 | return 1;
|
---|
3059 |
|
---|
3060 | return -1;
|
---|
3061 | }
|
---|
3062 |
|
---|
3063 |
|
---|
3064 | int BspNodeGeometry::ComputeIntersection(const AxisAlignedBox3 &box) const
|
---|
3065 | {
|
---|
3066 | // 1: box does not intersect geometry
|
---|
3067 | // 0: box intersects geometry
|
---|
3068 | // -1: box contains geometry
|
---|
3069 |
|
---|
3070 | AxisAlignedBox3 boundingBox;
|
---|
3071 | GetBoundingBox(boundingBox);
|
---|
3072 |
|
---|
3073 | // no intersections with bounding box
|
---|
3074 | if (!Overlap(box, boundingBox))
|
---|
3075 | {
|
---|
3076 | return 1;
|
---|
3077 | }
|
---|
3078 |
|
---|
3079 | // box cotains bounding box of geometry >=> contains geometry
|
---|
3080 | if (box.Includes(boundingBox))
|
---|
3081 | return -1;
|
---|
3082 |
|
---|
3083 | int result = 0;
|
---|
3084 |
|
---|
3085 | // test geometry planes for intersections
|
---|
3086 | vector<Plane3>::const_iterator it, it_end = mPlanes.end();
|
---|
3087 |
|
---|
3088 | for (it = mPlanes.begin(); it != it_end; ++ it)
|
---|
3089 | {
|
---|
3090 | const int side = box.Side(*it);
|
---|
3091 |
|
---|
3092 | // box does not intersects geometry
|
---|
3093 | if (side == 1)
|
---|
3094 | {
|
---|
3095 | return 1;
|
---|
3096 | }
|
---|
3097 | //if (side == 0) result = 0;
|
---|
3098 | }
|
---|
3099 |
|
---|
3100 | return result;
|
---|
3101 | }
|
---|
3102 |
|
---|
3103 |
|
---|
3104 | Vector3 BspNodeGeometry::CenterOfMass() const
|
---|
3105 | {
|
---|
3106 | int n = 0;
|
---|
3107 |
|
---|
3108 | Vector3 center(0,0,0);
|
---|
3109 |
|
---|
3110 | PolygonContainer::const_iterator pit, pit_end = mPolys.end();
|
---|
3111 |
|
---|
3112 | for (pit = mPolys.begin(); pit != pit_end; ++ pit)
|
---|
3113 | {
|
---|
3114 | Polygon3 *poly = *pit;
|
---|
3115 |
|
---|
3116 | VertexContainer::const_iterator vit, vit_end = poly->mVertices.end();
|
---|
3117 |
|
---|
3118 | for(vit = poly->mVertices.begin(); vit != vit_end; ++ vit)
|
---|
3119 | {
|
---|
3120 | center += *vit;
|
---|
3121 | ++ n;
|
---|
3122 | }
|
---|
3123 | }
|
---|
3124 |
|
---|
3125 | //Debug << "center: " << center << " new " << center / (float)n << endl;
|
---|
3126 |
|
---|
3127 | return center / (float)n;
|
---|
3128 | }
|
---|
3129 |
|
---|
3130 |
|
---|
3131 | bool BspNodeGeometry::Valid() const
|
---|
3132 | {
|
---|
3133 | // geometry is degenerated
|
---|
3134 | if (mPolys.size() < 4)
|
---|
3135 | return false;
|
---|
3136 |
|
---|
3137 | return true;
|
---|
3138 | }
|
---|
3139 |
|
---|
3140 | void IncludeNodeGeomInMesh(const BspNodeGeometry &geom, Mesh &mesh)
|
---|
3141 | {
|
---|
3142 | // add single polygons to mesh
|
---|
3143 | PolygonContainer::const_iterator it, it_end = geom.mPolys.end();
|
---|
3144 |
|
---|
3145 | for (it = geom.mPolys.begin(); it != geom.mPolys.end(); ++ it)
|
---|
3146 | {
|
---|
3147 | Polygon3 *poly = (*it);
|
---|
3148 | IncludePolyInMesh(*poly, mesh);
|
---|
3149 | }
|
---|
3150 | }
|
---|
3151 |
|
---|
3152 |
|
---|
3153 | bool BspNodeGeometry::SplitGeometry(BspNodeGeometry &front,
|
---|
3154 | BspNodeGeometry &back,
|
---|
3155 | const Plane3 &splitPlane,
|
---|
3156 | const AxisAlignedBox3 &box,
|
---|
3157 | const float epsilon) const
|
---|
3158 | {
|
---|
3159 | //-- trivial cases
|
---|
3160 | if (0)
|
---|
3161 | {
|
---|
3162 | const int cf = Side(splitPlane, epsilon);
|
---|
3163 |
|
---|
3164 | if (cf == -1)
|
---|
3165 | {
|
---|
3166 | back = *this;
|
---|
3167 | return false;
|
---|
3168 | }
|
---|
3169 | else if (cf == 1)
|
---|
3170 | {
|
---|
3171 | front = *this;
|
---|
3172 | return false;
|
---|
3173 | }
|
---|
3174 | }
|
---|
3175 |
|
---|
3176 | // get cross section of new polygon
|
---|
3177 | Polygon3 *planePoly = box.CrossSection(splitPlane);
|
---|
3178 |
|
---|
3179 | // split polygon with all other polygons
|
---|
3180 | planePoly = SplitPolygon(planePoly, epsilon);
|
---|
3181 |
|
---|
3182 | bool splitsGeom = (planePoly != NULL);
|
---|
3183 |
|
---|
3184 | //-- new polygon splits all other polygons
|
---|
3185 | for (int i = 0; i < (int)mPolys.size()/* && planePoly*/; ++ i)
|
---|
3186 | {
|
---|
3187 | /// don't use epsilon here to get exact split planes
|
---|
3188 | const int cf =
|
---|
3189 | mPolys[i]->ClassifyPlane(splitPlane, epsilon);
|
---|
3190 |
|
---|
3191 | switch (cf)
|
---|
3192 | {
|
---|
3193 | case Polygon3::SPLIT:
|
---|
3194 | {
|
---|
3195 | Polygon3 *poly = new Polygon3(mPolys[i]->mVertices);
|
---|
3196 |
|
---|
3197 | Polygon3 *frontPoly = new Polygon3();
|
---|
3198 | Polygon3 *backPoly = new Polygon3();
|
---|
3199 |
|
---|
3200 | poly->Split(splitPlane,
|
---|
3201 | *frontPoly,
|
---|
3202 | *backPoly,
|
---|
3203 | epsilon);
|
---|
3204 |
|
---|
3205 | DEL_PTR(poly);
|
---|
3206 |
|
---|
3207 | if (frontPoly->Valid(epsilon))
|
---|
3208 | {
|
---|
3209 | front.Add(frontPoly, mPlanes[i]);
|
---|
3210 | }
|
---|
3211 | else
|
---|
3212 | {
|
---|
3213 | //Debug << "no f! " << endl;
|
---|
3214 | DEL_PTR(frontPoly);
|
---|
3215 | }
|
---|
3216 |
|
---|
3217 | if (backPoly->Valid(epsilon))
|
---|
3218 | {
|
---|
3219 | back.Add(backPoly, mPlanes[i]);
|
---|
3220 | }
|
---|
3221 | else
|
---|
3222 | {
|
---|
3223 | //Debug << "no b! " << endl;
|
---|
3224 | DEL_PTR(backPoly);
|
---|
3225 | }
|
---|
3226 | }
|
---|
3227 |
|
---|
3228 | break;
|
---|
3229 | case Polygon3::BACK_SIDE:
|
---|
3230 | back.Add(new Polygon3(mPolys[i]->mVertices), mPlanes[i]);
|
---|
3231 | break;
|
---|
3232 | case Polygon3::FRONT_SIDE:
|
---|
3233 | front.Add(new Polygon3(mPolys[i]->mVertices), mPlanes[i]);
|
---|
3234 | break;
|
---|
3235 | // only put into back container (split should have no effect ...)
|
---|
3236 | case Polygon3::COINCIDENT:
|
---|
3237 | //Debug << "error should not come here" << endl;
|
---|
3238 | splitsGeom = false;
|
---|
3239 |
|
---|
3240 | if (DotProd(mPlanes[i].mNormal, splitPlane.mNormal > 0))
|
---|
3241 | back.Add(new Polygon3(mPolys[i]->mVertices), mPlanes[i]);
|
---|
3242 | else
|
---|
3243 | front.Add(new Polygon3(mPolys[i]->mVertices), mPlanes[i]);
|
---|
3244 | break;
|
---|
3245 | default:
|
---|
3246 | break;
|
---|
3247 | }
|
---|
3248 | }
|
---|
3249 |
|
---|
3250 | //-- finally add the new polygon to the child node geometries
|
---|
3251 | if (planePoly)
|
---|
3252 | {
|
---|
3253 | // add polygon with reverse orientation to front cell
|
---|
3254 | Plane3 reversePlane(splitPlane);
|
---|
3255 | reversePlane.ReverseOrientation();
|
---|
3256 |
|
---|
3257 | // add polygon with normal pointing into positive half space to back cell
|
---|
3258 | back.Add(planePoly, splitPlane);
|
---|
3259 | //back.mPolys.push_back(planePoly);
|
---|
3260 |
|
---|
3261 | Polygon3 *reversePoly = planePoly->CreateReversePolygon();
|
---|
3262 | front.Add(reversePoly, reversePlane);
|
---|
3263 | //Debug << "poly normal : " << reversePoly->GetSupportingPlane().mNormal << " split plane normal " << reversePlane.mNormal << endl;
|
---|
3264 | }
|
---|
3265 |
|
---|
3266 |
|
---|
3267 | return splitsGeom;
|
---|
3268 | }
|
---|
3269 |
|
---|
3270 |
|
---|
3271 | Polygon3 *BspNodeGeometry::SplitPolygon(Polygon3 *planePoly,
|
---|
3272 | const float epsilon) const
|
---|
3273 | {
|
---|
3274 | if (!planePoly->Valid(epsilon))
|
---|
3275 | {
|
---|
3276 | //Debug << "not valid!!" << endl;
|
---|
3277 | DEL_PTR(planePoly);
|
---|
3278 | }
|
---|
3279 | // polygon is split by all other planes
|
---|
3280 | for (int i = 0; (i < (int)mPolys.size()) && planePoly; ++ i)
|
---|
3281 | {
|
---|
3282 | //Plane3 plane = mPolys[i]->GetSupportingPlane();
|
---|
3283 | Plane3 plane = mPlanes[i];
|
---|
3284 |
|
---|
3285 | /// don't use epsilon here to get exact split planes
|
---|
3286 | const int cf =
|
---|
3287 | planePoly->ClassifyPlane(plane, epsilon);
|
---|
3288 |
|
---|
3289 | // split new polygon with all previous planes
|
---|
3290 | switch (cf)
|
---|
3291 | {
|
---|
3292 | case Polygon3::SPLIT:
|
---|
3293 | {
|
---|
3294 | Polygon3 *frontPoly = new Polygon3();
|
---|
3295 | Polygon3 *backPoly = new Polygon3();
|
---|
3296 |
|
---|
3297 | planePoly->Split(plane,
|
---|
3298 | *frontPoly,
|
---|
3299 | *backPoly,
|
---|
3300 | epsilon);
|
---|
3301 |
|
---|
3302 | // don't need anymore
|
---|
3303 | DEL_PTR(planePoly);
|
---|
3304 | DEL_PTR(frontPoly);
|
---|
3305 |
|
---|
3306 | // back polygon is belonging to geometry
|
---|
3307 | if (backPoly->Valid(epsilon))
|
---|
3308 | planePoly = backPoly;
|
---|
3309 | else
|
---|
3310 | DEL_PTR(backPoly);
|
---|
3311 | }
|
---|
3312 | break;
|
---|
3313 | case Polygon3::FRONT_SIDE:
|
---|
3314 | DEL_PTR(planePoly);
|
---|
3315 | break;
|
---|
3316 | // polygon is taken as it is
|
---|
3317 | case Polygon3::BACK_SIDE:
|
---|
3318 | case Polygon3::COINCIDENT:
|
---|
3319 | default:
|
---|
3320 | break;
|
---|
3321 | }
|
---|
3322 | }
|
---|
3323 |
|
---|
3324 | return planePoly;
|
---|
3325 | }
|
---|
3326 |
|
---|
3327 |
|
---|
3328 | ViewCell *BspTree::GetViewCell(const Vector3 &point)
|
---|
3329 | {
|
---|
3330 | if (mRoot == NULL)
|
---|
3331 | return NULL;
|
---|
3332 |
|
---|
3333 | stack<BspNode *> nodeStack;
|
---|
3334 | nodeStack.push(mRoot);
|
---|
3335 |
|
---|
3336 | ViewCellLeaf *viewcell = NULL;
|
---|
3337 |
|
---|
3338 | while (!nodeStack.empty()) {
|
---|
3339 | BspNode *node = nodeStack.top();
|
---|
3340 | nodeStack.pop();
|
---|
3341 |
|
---|
3342 | if (node->IsLeaf())
|
---|
3343 | {
|
---|
3344 | viewcell = static_cast<BspLeaf *>(node)->mViewCell;
|
---|
3345 | break;
|
---|
3346 | }
|
---|
3347 | else
|
---|
3348 | {
|
---|
3349 |
|
---|
3350 | BspInterior *interior = static_cast<BspInterior *>(node);
|
---|
3351 |
|
---|
3352 | // random decision
|
---|
3353 | if (interior->GetPlane().Side(point) < 0)
|
---|
3354 | {
|
---|
3355 | nodeStack.push(interior->GetBack());
|
---|
3356 | }
|
---|
3357 | else
|
---|
3358 | {
|
---|
3359 | nodeStack.push(interior->GetFront());
|
---|
3360 | }
|
---|
3361 | }
|
---|
3362 | }
|
---|
3363 |
|
---|
3364 | return viewcell;
|
---|
3365 | }
|
---|
3366 |
|
---|
3367 |
|
---|
3368 | bool BspTree::Export(OUT_STREAM &stream)
|
---|
3369 | {
|
---|
3370 | ExportNode(mRoot, stream);
|
---|
3371 | return true;
|
---|
3372 | }
|
---|
3373 |
|
---|
3374 |
|
---|
3375 | void BspTree::ExportNode(BspNode *node, OUT_STREAM &stream)
|
---|
3376 | {
|
---|
3377 | if (node->IsLeaf())
|
---|
3378 | {
|
---|
3379 | BspLeaf *leaf = static_cast<BspLeaf *>(node);
|
---|
3380 | ViewCell *viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell());
|
---|
3381 |
|
---|
3382 | const int id = viewCell->GetId();
|
---|
3383 |
|
---|
3384 | stream << "<Leaf viewCellId=\"" << id << "\" />" << endl;
|
---|
3385 | }
|
---|
3386 | else
|
---|
3387 | {
|
---|
3388 | BspInterior *interior = static_cast<BspInterior *>(node);
|
---|
3389 |
|
---|
3390 | Plane3 plane = interior->GetPlane();
|
---|
3391 | stream << "<Interior plane=\"" << plane.mNormal.x << " "
|
---|
3392 | << plane.mNormal.y << " " << plane.mNormal.z << " "
|
---|
3393 | << plane.mD << "\">" << endl;
|
---|
3394 |
|
---|
3395 | ExportNode(interior->GetBack(), stream);
|
---|
3396 | ExportNode(interior->GetFront(), stream);
|
---|
3397 |
|
---|
3398 | stream << "</Interior>" << endl;
|
---|
3399 | }
|
---|
3400 | }
|
---|
3401 |
|
---|
3402 |
|
---|
3403 | }
|
---|