1 | #include <stack>
|
---|
2 | #include <time.h>
|
---|
3 | #include <iomanip>
|
---|
4 |
|
---|
5 | #include "Plane3.h"
|
---|
6 | #include "VspBspTree.h"
|
---|
7 | #include "Mesh.h"
|
---|
8 | #include "common.h"
|
---|
9 | #include "ViewCell.h"
|
---|
10 | #include "Environment.h"
|
---|
11 | #include "Polygon3.h"
|
---|
12 | #include "Ray.h"
|
---|
13 | #include "AxisAlignedBox3.h"
|
---|
14 | #include "Exporter.h"
|
---|
15 | #include "Plane3.h"
|
---|
16 | #include "ViewCellBsp.h"
|
---|
17 | #include "ViewCellsManager.h"
|
---|
18 | #include "Beam.h"
|
---|
19 |
|
---|
20 | namespace GtpVisibilityPreprocessor {
|
---|
21 |
|
---|
22 | #define USE_FIXEDPOINT_T 0
|
---|
23 | #define COUNT_ORIGIN_OBJECTS 1
|
---|
24 |
|
---|
25 |
|
---|
26 | //-- static members
|
---|
27 |
|
---|
28 | int VspBspTree::sFrontId = 0;
|
---|
29 | int VspBspTree::sBackId = 0;
|
---|
30 | int VspBspTree::sFrontAndBackId = 0;
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | typedef pair<BspNode *, BspNodeGeometry *> bspNodePair;
|
---|
35 |
|
---|
36 |
|
---|
37 | // pvs penalty can be different from pvs size
|
---|
38 | inline static float EvalPvsPenalty(const int pvs,
|
---|
39 | const int lower,
|
---|
40 | const int upper)
|
---|
41 | {
|
---|
42 | // clamp to minmax values
|
---|
43 | if (pvs < lower)
|
---|
44 | return (float)lower;
|
---|
45 | if (pvs > upper)
|
---|
46 | return (float)upper;
|
---|
47 |
|
---|
48 | return (float)pvs;
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | /******************************************************************************/
|
---|
55 | /* class VspBspTree implementation */
|
---|
56 | /******************************************************************************/
|
---|
57 |
|
---|
58 |
|
---|
59 | VspBspTree::VspBspTree():
|
---|
60 | mRoot(NULL),
|
---|
61 | mUseAreaForPvs(false),
|
---|
62 | mCostNormalizer(Limits::Small),
|
---|
63 | mViewCellsManager(NULL),
|
---|
64 | mOutOfBoundsCell(NULL),
|
---|
65 | mStoreRays(false),
|
---|
66 | mRenderCostWeight(0.5),
|
---|
67 | mUseRandomAxis(false),
|
---|
68 | mTimeStamp(1)
|
---|
69 | {
|
---|
70 | bool randomize = false;
|
---|
71 | Environment::GetSingleton()->GetBoolValue("VspBspTree.Construction.randomize", randomize);
|
---|
72 | if (randomize) Randomize(); // initialise random generator for heuristics
|
---|
73 |
|
---|
74 | //-- termination criteria for autopartition
|
---|
75 | Environment::GetSingleton()->GetIntValue("VspBspTree.Termination.maxDepth", mTermMaxDepth);
|
---|
76 | Environment::GetSingleton()->GetIntValue("VspBspTree.Termination.minPvs", mTermMinPvs);
|
---|
77 | Environment::GetSingleton()->GetIntValue("VspBspTree.Termination.minRays", mTermMinRays);
|
---|
78 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Termination.minProbability", mTermMinProbability);
|
---|
79 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Termination.maxRayContribution", mTermMaxRayContribution);
|
---|
80 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Termination.minAccRayLenght", mTermMinAccRayLength);
|
---|
81 |
|
---|
82 | Environment::GetSingleton()->GetIntValue("VspBspTree.Termination.missTolerance", mTermMissTolerance);
|
---|
83 | Environment::GetSingleton()->GetIntValue("VspBspTree.Termination.maxViewCells", mMaxViewCells);
|
---|
84 |
|
---|
85 | //-- max cost ratio for early tree termination
|
---|
86 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Termination.maxCostRatio", mTermMaxCostRatio);
|
---|
87 |
|
---|
88 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Termination.minGlobalCostRatio", mTermMinGlobalCostRatio);
|
---|
89 | Environment::GetSingleton()->GetIntValue("VspBspTree.Termination.globalCostMissTolerance", mTermGlobalCostMissTolerance);
|
---|
90 |
|
---|
91 | //-- factors for bsp tree split plane heuristics
|
---|
92 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Factor.pvs", mPvsFactor);
|
---|
93 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Termination.ct_div_ci", mCtDivCi);
|
---|
94 |
|
---|
95 |
|
---|
96 | //-- partition criteria
|
---|
97 | Environment::GetSingleton()->GetIntValue("VspBspTree.maxPolyCandidates", mMaxPolyCandidates);
|
---|
98 | Environment::GetSingleton()->GetIntValue("VspBspTree.maxRayCandidates", mMaxRayCandidates);
|
---|
99 | Environment::GetSingleton()->GetIntValue("VspBspTree.splitPlaneStrategy", mSplitPlaneStrategy);
|
---|
100 |
|
---|
101 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Construction.epsilon", mEpsilon);
|
---|
102 | Environment::GetSingleton()->GetIntValue("VspBspTree.maxTests", mMaxTests);
|
---|
103 |
|
---|
104 | // if only the driving axis is used for axis aligned split
|
---|
105 | Environment::GetSingleton()->GetBoolValue("VspBspTree.splitUseOnlyDrivingAxis", mOnlyDrivingAxis);
|
---|
106 |
|
---|
107 | //-- termination criteria for axis aligned split
|
---|
108 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Termination.AxisAligned.maxRayContribution",
|
---|
109 | mTermMaxRayContriForAxisAligned);
|
---|
110 | Environment::GetSingleton()->GetIntValue("VspBspTree.Termination.AxisAligned.minRays",
|
---|
111 | mTermMinRaysForAxisAligned);
|
---|
112 |
|
---|
113 | //Environment::GetSingleton()->GetFloatValue("VspBspTree.maxTotalMemory", mMaxTotalMemory);
|
---|
114 | Environment::GetSingleton()->GetFloatValue("VspBspTree.maxStaticMemory", mMaxMemory);
|
---|
115 |
|
---|
116 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Construction.renderCostWeight", mRenderCostWeight);
|
---|
117 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Construction.renderCostDecreaseWeight", mRenderCostDecreaseWeight);
|
---|
118 | Environment::GetSingleton()->GetBoolValue("VspBspTree.usePolygonSplitIfAvailable", mUsePolygonSplitIfAvailable);
|
---|
119 |
|
---|
120 | Environment::GetSingleton()->GetBoolValue("VspBspTree.useCostHeuristics", mUseCostHeuristics);
|
---|
121 | Environment::GetSingleton()->GetBoolValue("VspBspTree.useSplitCostQueue", mUseSplitCostQueue);
|
---|
122 | Environment::GetSingleton()->GetBoolValue("VspBspTree.simulateOctree", mCirculatingAxis);
|
---|
123 | Environment::GetSingleton()->GetBoolValue("VspBspTree.useRandomAxis", mUseRandomAxis);
|
---|
124 | Environment::GetSingleton()->GetIntValue("VspBspTree.nodePriorityQueueType", mNodePriorityQueueType);
|
---|
125 |
|
---|
126 |
|
---|
127 | char subdivisionStatsLog[100];
|
---|
128 | Environment::GetSingleton()->GetStringValue("VspBspTree.subdivisionStats", subdivisionStatsLog);
|
---|
129 | mSubdivisionStats.open(subdivisionStatsLog);
|
---|
130 |
|
---|
131 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Construction.minBand", mMinBand);
|
---|
132 | Environment::GetSingleton()->GetFloatValue("VspBspTree.Construction.maxBand", mMaxBand);
|
---|
133 | Environment::GetSingleton()->GetBoolValue("VspBspTree.Construction.useDrivingAxisForMaxCost", mUseDrivingAxisIfMaxCostViolated);
|
---|
134 |
|
---|
135 | //-- debug output
|
---|
136 |
|
---|
137 | Debug << "******* VSP BSP options ******** " << endl;
|
---|
138 | Debug << "max depth: " << mTermMaxDepth << endl;
|
---|
139 | Debug << "min PVS: " << mTermMinPvs << endl;
|
---|
140 | Debug << "min probabiliy: " << mTermMinProbability << endl;
|
---|
141 | Debug << "min rays: " << mTermMinRays << endl;
|
---|
142 | Debug << "max ray contri: " << mTermMaxRayContribution << endl;
|
---|
143 | Debug << "max cost ratio: " << mTermMaxCostRatio << endl;
|
---|
144 | Debug << "miss tolerance: " << mTermMissTolerance << endl;
|
---|
145 | Debug << "max view cells: " << mMaxViewCells << endl;
|
---|
146 | Debug << "max polygon candidates: " << mMaxPolyCandidates << endl;
|
---|
147 | //Debug << "max plane candidates: " << mMaxRayCandidates << endl;
|
---|
148 | Debug << "randomize: " << randomize << endl;
|
---|
149 |
|
---|
150 | Debug << "using area for pvs: " << mUseAreaForPvs << endl;
|
---|
151 | Debug << "render cost weight: " << mRenderCostWeight << endl;
|
---|
152 | Debug << "min global cost ratio: " << mTermMinGlobalCostRatio << endl;
|
---|
153 | Debug << "global cost miss tolerance: " << mTermGlobalCostMissTolerance << endl;
|
---|
154 | Debug << "only driving axis: " << mOnlyDrivingAxis << endl;
|
---|
155 | Debug << "max memory: " << mMaxMemory << endl;
|
---|
156 | Debug << "use poly split if available: " << mUsePolygonSplitIfAvailable << endl;
|
---|
157 | Debug << "use cost heuristics: " << mUseCostHeuristics << endl;
|
---|
158 | Debug << "use split cost queue: " << mUseSplitCostQueue << endl;
|
---|
159 | Debug << "subdivision stats log: " << subdivisionStatsLog << endl;
|
---|
160 | Debug << "use random axis: " << mUseRandomAxis << endl;
|
---|
161 | Debug << "priority queue type: " << mNodePriorityQueueType << endl;
|
---|
162 | Debug << "circulating axis: " << mCirculatingAxis << endl;
|
---|
163 | Debug << "minband: " << mMinBand << endl;
|
---|
164 | Debug << "maxband: " << mMaxBand << endl;
|
---|
165 | Debug << "use driving axis for max cost: " << mUseDrivingAxisIfMaxCostViolated << endl;
|
---|
166 | Debug << "render cost decrease weight: " << mRenderCostDecreaseWeight << endl;
|
---|
167 |
|
---|
168 | Debug << "Split plane strategy: ";
|
---|
169 |
|
---|
170 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
171 | {
|
---|
172 | Debug << "random polygon ";
|
---|
173 | }
|
---|
174 | if (mSplitPlaneStrategy & AXIS_ALIGNED)
|
---|
175 | {
|
---|
176 | Debug << "axis aligned ";
|
---|
177 | }
|
---|
178 | if (mSplitPlaneStrategy & LEAST_RAY_SPLITS)
|
---|
179 | {
|
---|
180 | mCostNormalizer += mLeastRaySplitsFactor;
|
---|
181 | Debug << "least ray splits ";
|
---|
182 | }
|
---|
183 | if (mSplitPlaneStrategy & BALANCED_RAYS)
|
---|
184 | {
|
---|
185 | mCostNormalizer += mBalancedRaysFactor;
|
---|
186 | Debug << "balanced rays ";
|
---|
187 | }
|
---|
188 | if (mSplitPlaneStrategy & PVS)
|
---|
189 | {
|
---|
190 | mCostNormalizer += mPvsFactor;
|
---|
191 | Debug << "pvs";
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | mLocalSplitCandidates = new vector<SortableEntry>;
|
---|
196 |
|
---|
197 | Debug << endl;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | BspViewCell *VspBspTree::GetOutOfBoundsCell()
|
---|
202 | {
|
---|
203 | return mOutOfBoundsCell;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | BspViewCell *VspBspTree::GetOrCreateOutOfBoundsCell()
|
---|
208 | {
|
---|
209 | if (!mOutOfBoundsCell)
|
---|
210 | {
|
---|
211 | mOutOfBoundsCell = new BspViewCell();
|
---|
212 | mOutOfBoundsCell->SetId(-1);
|
---|
213 | mOutOfBoundsCell->SetValid(false);
|
---|
214 | }
|
---|
215 |
|
---|
216 | return mOutOfBoundsCell;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | const BspTreeStatistics &VspBspTree::GetStatistics() const
|
---|
221 | {
|
---|
222 | return mBspStats;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | VspBspTree::~VspBspTree()
|
---|
227 | {
|
---|
228 | DEL_PTR(mRoot);
|
---|
229 | DEL_PTR(mLocalSplitCandidates);
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | int VspBspTree::AddMeshToPolygons(Mesh *mesh,
|
---|
234 | PolygonContainer &polys,
|
---|
235 | MeshInstance *parent)
|
---|
236 | {
|
---|
237 | FaceContainer::const_iterator fi;
|
---|
238 |
|
---|
239 | // copy the face data to polygons
|
---|
240 | for (fi = mesh->mFaces.begin(); fi != mesh->mFaces.end(); ++ fi)
|
---|
241 | {
|
---|
242 | Polygon3 *poly = new Polygon3((*fi), mesh);
|
---|
243 |
|
---|
244 | if (poly->Valid(mEpsilon))
|
---|
245 | {
|
---|
246 | poly->mParent = parent; // set parent intersectable
|
---|
247 | polys.push_back(poly);
|
---|
248 | }
|
---|
249 | else
|
---|
250 | DEL_PTR(poly);
|
---|
251 | }
|
---|
252 | return (int)mesh->mFaces.size();
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | int VspBspTree::AddToPolygonSoup(const ViewCellContainer &viewCells,
|
---|
257 | PolygonContainer &polys,
|
---|
258 | int maxObjects)
|
---|
259 | {
|
---|
260 | int limit = (maxObjects > 0) ?
|
---|
261 | Min((int)viewCells.size(), maxObjects) : (int)viewCells.size();
|
---|
262 |
|
---|
263 | int polysSize = 0;
|
---|
264 |
|
---|
265 | for (int i = 0; i < limit; ++ i)
|
---|
266 | {
|
---|
267 | if (viewCells[i]->GetMesh()) // copy the mesh data to polygons
|
---|
268 | {
|
---|
269 | mBox.Include(viewCells[i]->GetBox()); // add to BSP tree aabb
|
---|
270 | polysSize +=
|
---|
271 | AddMeshToPolygons(viewCells[i]->GetMesh(),
|
---|
272 | polys,
|
---|
273 | viewCells[i]);
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | return polysSize;
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | int VspBspTree::AddToPolygonSoup(const ObjectContainer &objects,
|
---|
282 | PolygonContainer &polys,
|
---|
283 | int maxObjects)
|
---|
284 | {
|
---|
285 | int limit = (maxObjects > 0) ?
|
---|
286 | Min((int)objects.size(), maxObjects) : (int)objects.size();
|
---|
287 |
|
---|
288 | for (int i = 0; i < limit; ++i)
|
---|
289 | {
|
---|
290 | Intersectable *object = objects[i];//*it;
|
---|
291 | Mesh *mesh = NULL;
|
---|
292 |
|
---|
293 | switch (object->Type()) // extract the meshes
|
---|
294 | {
|
---|
295 | case Intersectable::MESH_INSTANCE:
|
---|
296 | mesh = dynamic_cast<MeshInstance *>(object)->GetMesh();
|
---|
297 | break;
|
---|
298 | case Intersectable::VIEW_CELL:
|
---|
299 | mesh = dynamic_cast<ViewCell *>(object)->GetMesh();
|
---|
300 | break;
|
---|
301 | case Intersectable::TRANSFORMED_MESH_INSTANCE:
|
---|
302 | {
|
---|
303 | TransformedMeshInstance *mi = dynamic_cast<TransformedMeshInstance *>(object);
|
---|
304 |
|
---|
305 | if (!mi->GetMesh())
|
---|
306 | break;
|
---|
307 | mesh = new Mesh();
|
---|
308 | mi->GetTransformedMesh(*mesh);
|
---|
309 |
|
---|
310 | break;
|
---|
311 | }
|
---|
312 | default:
|
---|
313 | Debug << "intersectable type not supported" << endl;
|
---|
314 | break;
|
---|
315 | }
|
---|
316 |
|
---|
317 | if (mesh) // copy the mesh data to polygons
|
---|
318 | {
|
---|
319 | mBox.Include(object->GetBox()); // add to BSP tree aabb
|
---|
320 | AddMeshToPolygons(mesh, polys, NULL);
|
---|
321 |
|
---|
322 | // cleanup
|
---|
323 | if (object->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
|
---|
324 | DEL_PTR(mesh);
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | return (int)polys.size();
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | void VspBspTree::ComputeBoundingBox(const VssRayContainer &sampleRays,
|
---|
333 | AxisAlignedBox3 *forcedBoundingBox)
|
---|
334 | {
|
---|
335 | if (forcedBoundingBox)
|
---|
336 | {
|
---|
337 | mBox = *forcedBoundingBox;
|
---|
338 | }
|
---|
339 | else // compute vsp tree bounding box
|
---|
340 | {
|
---|
341 | mBox.Initialize();
|
---|
342 |
|
---|
343 | VssRayContainer::const_iterator rit, rit_end = sampleRays.end();
|
---|
344 |
|
---|
345 | //-- compute bounding box
|
---|
346 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
347 | {
|
---|
348 | VssRay *ray = *rit;
|
---|
349 |
|
---|
350 | // compute bounding box of view space
|
---|
351 | mBox.Include(ray->GetTermination());
|
---|
352 | mBox.Include(ray->GetOrigin());
|
---|
353 | }
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | void VspBspTree::Construct(const VssRayContainer &sampleRays,
|
---|
359 | AxisAlignedBox3 *forcedBoundingBox)
|
---|
360 | {
|
---|
361 | // Compute the bounding box from the rays
|
---|
362 | ComputeBoundingBox(sampleRays, forcedBoundingBox);
|
---|
363 |
|
---|
364 | PolygonContainer polys;
|
---|
365 | RayInfoContainer *rays = new RayInfoContainer();
|
---|
366 |
|
---|
367 | //-- extract polygons from rays if there are polygon candidates
|
---|
368 | if (mMaxPolyCandidates)
|
---|
369 | {
|
---|
370 | int numObj = 0;
|
---|
371 |
|
---|
372 | Intersectable::NewMail();
|
---|
373 |
|
---|
374 | cout << "Extracting polygons from rays ... ";
|
---|
375 |
|
---|
376 | long startTime = GetTime();
|
---|
377 |
|
---|
378 | VssRayContainer::const_iterator rit, rit_end = sampleRays.end();
|
---|
379 |
|
---|
380 | //-- extract polygons intersected by the rays
|
---|
381 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
382 | {
|
---|
383 | VssRay *ray = *rit;
|
---|
384 | Intersectable *obj = ray->mTerminationObject;
|
---|
385 |
|
---|
386 | if ((mBox.IsInside(ray->mTermination) || !forcedBoundingBox) &&
|
---|
387 | obj && !obj->Mailed())
|
---|
388 | {
|
---|
389 | obj->Mail();
|
---|
390 |
|
---|
391 | // transformed mesh instance and mesh instance handle mesh differently
|
---|
392 | if (obj->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE)
|
---|
393 | {
|
---|
394 | Mesh mesh;
|
---|
395 |
|
---|
396 | TransformedMeshInstance *tmobj =
|
---|
397 | dynamic_cast<TransformedMeshInstance *>(obj);
|
---|
398 |
|
---|
399 | tmobj->GetTransformedMesh(mesh);
|
---|
400 | AddMeshToPolygons(&mesh, polys, tmobj);
|
---|
401 | }
|
---|
402 | else // MeshInstance
|
---|
403 | {
|
---|
404 | MeshInstance *mobj = dynamic_cast<MeshInstance *>(obj);
|
---|
405 | AddMeshToPolygons(mobj->GetMesh(), polys, mobj);
|
---|
406 | }
|
---|
407 |
|
---|
408 | ++ numObj;
|
---|
409 |
|
---|
410 | //-- compute bounding box
|
---|
411 | if (!forcedBoundingBox)
|
---|
412 | mBox.Include(ray->mTermination);
|
---|
413 | }
|
---|
414 |
|
---|
415 | if ((mBox.IsInside(ray->mOrigin) || !forcedBoundingBox) &&
|
---|
416 | ray->mOriginObject &&
|
---|
417 | !ray->mOriginObject->Mailed())
|
---|
418 | {
|
---|
419 | ray->mOriginObject->Mail();
|
---|
420 | MeshInstance *obj = dynamic_cast<MeshInstance *>(ray->mOriginObject);
|
---|
421 | AddMeshToPolygons(obj->GetMesh(), polys, obj);
|
---|
422 |
|
---|
423 | ++ numObj;
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | // throw out unnecessary polygons
|
---|
428 | PreprocessPolygons(polys);
|
---|
429 |
|
---|
430 | cout << "finished" << endl;
|
---|
431 |
|
---|
432 | Debug << "\n" << (int)polys.size() << " polys extracted from "
|
---|
433 | << (int)sampleRays.size() << " rays in "
|
---|
434 | << TimeDiff(startTime, GetTime())*1e-3 << " secs" << endl << endl;
|
---|
435 | }
|
---|
436 |
|
---|
437 | Debug << "maximal pvs (i.e., pvs still considered as valid): "
|
---|
438 | << mViewCellsManager->GetMaxPvsSize() << endl;
|
---|
439 |
|
---|
440 | VssRayContainer::const_iterator rit, rit_end = sampleRays.end();
|
---|
441 |
|
---|
442 | //-- store rays
|
---|
443 | for (rit = sampleRays.begin(); rit != rit_end; ++ rit)
|
---|
444 | {
|
---|
445 | VssRay *ray = *rit;
|
---|
446 |
|
---|
447 | float minT, maxT;
|
---|
448 |
|
---|
449 | static Ray hray;
|
---|
450 | hray.Init(*ray);
|
---|
451 |
|
---|
452 | // TODO: not very efficient to implictly cast between rays types
|
---|
453 | if (mBox.GetRaySegment(hray, minT, maxT))
|
---|
454 | {
|
---|
455 | float len = ray->Length();
|
---|
456 |
|
---|
457 | if (!len)
|
---|
458 | len = Limits::Small;
|
---|
459 |
|
---|
460 | rays->push_back(RayInfo(ray, minT / len, maxT / len));
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | // normalize
|
---|
465 | if (mUseAreaForPvs)
|
---|
466 | mTermMinProbability *= mBox.SurfaceArea();
|
---|
467 | else
|
---|
468 | mTermMinProbability *= mBox.GetVolume();
|
---|
469 |
|
---|
470 |
|
---|
471 | mBspStats.nodes = 1;
|
---|
472 | mBspStats.polys = (int)polys.size();
|
---|
473 | mGlobalCostMisses = 0;
|
---|
474 |
|
---|
475 |
|
---|
476 | // use split cost priority queue
|
---|
477 | if (mUseSplitCostQueue)
|
---|
478 | {
|
---|
479 | ConstructWithSplitQueue(polys, rays);
|
---|
480 | }
|
---|
481 | else
|
---|
482 | {
|
---|
483 | Construct(polys, rays);
|
---|
484 | }
|
---|
485 |
|
---|
486 | // clean up polygons
|
---|
487 | CLEAR_CONTAINER(polys);
|
---|
488 | }
|
---|
489 |
|
---|
490 |
|
---|
491 | // TODO: return memory usage in MB
|
---|
492 | float VspBspTree::GetMemUsage() const
|
---|
493 | {
|
---|
494 | return (float)
|
---|
495 | (sizeof(VspBspTree) +
|
---|
496 | mBspStats.Leaves() * sizeof(BspLeaf) +
|
---|
497 | mCreatedViewCells * sizeof(BspViewCell) +
|
---|
498 | mBspStats.pvs * sizeof(ObjectPvsData) +
|
---|
499 | mBspStats.Interior() * sizeof(BspInterior) +
|
---|
500 | mBspStats.accumRays * sizeof(RayInfo)) / (1024.0f * 1024.0f);
|
---|
501 | }
|
---|
502 |
|
---|
503 |
|
---|
504 | void VspBspTree::Construct(const PolygonContainer &polys, RayInfoContainer *rays)
|
---|
505 | {
|
---|
506 | VspBspTraversalQueue tQueue;
|
---|
507 |
|
---|
508 | /// create new vsp tree
|
---|
509 | mRoot = new BspLeaf();
|
---|
510 |
|
---|
511 | // constrruct root node geometry
|
---|
512 | BspNodeGeometry *geom = new BspNodeGeometry();
|
---|
513 | ConstructGeometry(mRoot, *geom);
|
---|
514 |
|
---|
515 | /// we use the overall probability as normalizer
|
---|
516 | /// either the overall area or the volume
|
---|
517 | const float prop = mUseAreaForPvs ? geom->GetArea() : geom->GetVolume();
|
---|
518 |
|
---|
519 | /// first traversal data
|
---|
520 | VspBspTraversalData tData(mRoot,
|
---|
521 | new PolygonContainer(polys),
|
---|
522 | 0,
|
---|
523 | rays,
|
---|
524 | ComputePvsSize(*rays),
|
---|
525 | prop,
|
---|
526 | geom);
|
---|
527 |
|
---|
528 | EvalPriority(tData);
|
---|
529 |
|
---|
530 |
|
---|
531 | // first node is kd node, i.e. an axis aligned box
|
---|
532 | if (1)
|
---|
533 | tData.mIsKdNode = true;
|
---|
534 | else
|
---|
535 | tData.mIsKdNode = false;
|
---|
536 |
|
---|
537 | tQueue.push(tData);
|
---|
538 |
|
---|
539 |
|
---|
540 | mTotalCost = tData.mPvs * tData.mProbability / mBox.GetVolume();
|
---|
541 | mTotalPvsSize = tData.mPvs;
|
---|
542 |
|
---|
543 | // first subdivison statistics
|
---|
544 | AddSubdivisionStats(1, 0, 0, mTotalCost, (float)mTotalPvsSize);
|
---|
545 |
|
---|
546 | mBspStats.Start();
|
---|
547 | cout << "Constructing vsp bsp tree ... \n";
|
---|
548 |
|
---|
549 | const long startTime = GetTime();
|
---|
550 | // used for intermediate time measurements and progress
|
---|
551 | long interTime = GetTime();
|
---|
552 |
|
---|
553 | int nLeaves = 500;
|
---|
554 | int nViewCells = 500;
|
---|
555 |
|
---|
556 | mOutOfMemory = false;
|
---|
557 | mCreatedViewCells = 0;
|
---|
558 |
|
---|
559 | while (!tQueue.empty())
|
---|
560 | {
|
---|
561 | tData = tQueue.top();
|
---|
562 | tQueue.pop();
|
---|
563 |
|
---|
564 | if (0 && !mOutOfMemory)
|
---|
565 | {
|
---|
566 | float mem = GetMemUsage();
|
---|
567 |
|
---|
568 | if (mem > mMaxMemory)
|
---|
569 | {
|
---|
570 | mOutOfMemory = true;
|
---|
571 | Debug << "memory limit reached: " << mem << endl;
|
---|
572 | }
|
---|
573 | }
|
---|
574 |
|
---|
575 | // subdivide leaf node
|
---|
576 | const BspNode *r = Subdivide(tQueue, tData);
|
---|
577 |
|
---|
578 | if (r == mRoot)
|
---|
579 | Debug << "VSP BSP tree construction time spent at root: "
|
---|
580 | << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
581 |
|
---|
582 | if (mBspStats.Leaves() >= nLeaves)
|
---|
583 | {
|
---|
584 | nLeaves += 500;
|
---|
585 |
|
---|
586 | cout << "leaves=" << mBspStats.Leaves() << endl;
|
---|
587 | Debug << "needed "
|
---|
588 | << TimeDiff(interTime, GetTime())*1e-3
|
---|
589 | << " secs to create 500 view cells" << endl;
|
---|
590 | interTime = GetTime();
|
---|
591 | }
|
---|
592 |
|
---|
593 | if (mCreatedViewCells >= nViewCells)
|
---|
594 | {
|
---|
595 | nViewCells += 500;
|
---|
596 |
|
---|
597 | cout << "generated " << mCreatedViewCells << " viewcells" << endl;
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | Debug << "Used Memory: " << GetMemUsage() << " MB" << endl << endl;
|
---|
602 | cout << "finished in " << TimeDiff(startTime, GetTime())*1e-3 << "secs" << endl;
|
---|
603 |
|
---|
604 | mBspStats.Stop();
|
---|
605 | }
|
---|
606 |
|
---|
607 |
|
---|
608 |
|
---|
609 | void VspBspTree::ConstructWithSplitQueue(const PolygonContainer &polys,
|
---|
610 | RayInfoContainer *rays)
|
---|
611 | {
|
---|
612 | VspBspSplitQueue tQueue;
|
---|
613 |
|
---|
614 | mRoot = new BspLeaf();
|
---|
615 |
|
---|
616 | // constrruct root node geometry
|
---|
617 | BspNodeGeometry *geom = new BspNodeGeometry();
|
---|
618 | ConstructGeometry(mRoot, *geom);
|
---|
619 |
|
---|
620 | const float prop = mUseAreaForPvs ? geom->GetArea() : geom->GetVolume();
|
---|
621 |
|
---|
622 | VspBspTraversalData tData(mRoot,
|
---|
623 | new PolygonContainer(polys),
|
---|
624 | 0,
|
---|
625 | rays,
|
---|
626 | ComputePvsSize(*rays),
|
---|
627 | prop,
|
---|
628 | geom);
|
---|
629 |
|
---|
630 |
|
---|
631 | // first node is kd node, i.e. an axis aligned box
|
---|
632 | if (1)
|
---|
633 | tData.mIsKdNode = true;
|
---|
634 | else
|
---|
635 | tData.mIsKdNode = false;
|
---|
636 |
|
---|
637 | // compute first split candidate
|
---|
638 | VspBspSplitCandidate splitCandidate;
|
---|
639 | EvalSplitCandidate(tData, splitCandidate);
|
---|
640 |
|
---|
641 | tQueue.push(splitCandidate);
|
---|
642 |
|
---|
643 | mTotalCost = tData.mPvs * tData.mProbability / mBox.GetVolume();
|
---|
644 | mTotalPvsSize = tData.mPvs;
|
---|
645 |
|
---|
646 | // first subdivison statistics
|
---|
647 | AddSubdivisionStats(1, 0, 0, mTotalCost, (float)mTotalPvsSize);
|
---|
648 |
|
---|
649 | mBspStats.Start();
|
---|
650 | cout << "Constructing vsp bsp tree ... \n";
|
---|
651 |
|
---|
652 | long startTime = GetTime();
|
---|
653 | int nLeaves = 500;
|
---|
654 | int nViewCells = 500;
|
---|
655 |
|
---|
656 | // used for intermediate time measurements and progress
|
---|
657 | long interTime = GetTime();
|
---|
658 |
|
---|
659 | mOutOfMemory = false;
|
---|
660 |
|
---|
661 | mCreatedViewCells = 0;
|
---|
662 |
|
---|
663 | while (!tQueue.empty())
|
---|
664 | {
|
---|
665 | splitCandidate = tQueue.top();
|
---|
666 | tQueue.pop();
|
---|
667 |
|
---|
668 | // cost ratio of cost decrease / totalCost
|
---|
669 | float costRatio = splitCandidate.GetPriority() / mTotalCost;
|
---|
670 |
|
---|
671 | //Debug << "cost ratio: " << costRatio << endl;
|
---|
672 |
|
---|
673 | if (costRatio < mTermMinGlobalCostRatio)
|
---|
674 | ++ mGlobalCostMisses;
|
---|
675 |
|
---|
676 | if (0 && !mOutOfMemory)
|
---|
677 | {
|
---|
678 | float mem = GetMemUsage();
|
---|
679 |
|
---|
680 | if (mem > mMaxMemory)
|
---|
681 | {
|
---|
682 | mOutOfMemory = true;
|
---|
683 | Debug << "memory limit reached: " << mem << endl;
|
---|
684 | }
|
---|
685 | }
|
---|
686 |
|
---|
687 | // subdivide leaf node
|
---|
688 | BspNode *r = Subdivide(tQueue, splitCandidate);
|
---|
689 |
|
---|
690 | if (r == mRoot)
|
---|
691 | Debug << "VSP BSP tree construction time spent at root: "
|
---|
692 | << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl;
|
---|
693 |
|
---|
694 | if (mBspStats.Leaves() >= nLeaves)
|
---|
695 | {
|
---|
696 | nLeaves += 500;
|
---|
697 |
|
---|
698 | cout << "leaves=" << mBspStats.Leaves() << endl;
|
---|
699 | Debug << "needed "
|
---|
700 | << TimeDiff(interTime, GetTime())*1e-3
|
---|
701 | << " secs to create 500 view cells" << endl;
|
---|
702 | interTime = GetTime();
|
---|
703 | }
|
---|
704 |
|
---|
705 | if (mCreatedViewCells == nViewCells)
|
---|
706 | {
|
---|
707 | nViewCells += 500;
|
---|
708 | cout << "generated " << mCreatedViewCells << " viewcells" << endl;
|
---|
709 | }
|
---|
710 | }
|
---|
711 |
|
---|
712 | Debug << "Used Memory: " << GetMemUsage() << " MB" << endl << endl;
|
---|
713 | cout << "finished\n";
|
---|
714 |
|
---|
715 | mBspStats.Stop();
|
---|
716 | }
|
---|
717 |
|
---|
718 |
|
---|
719 | bool VspBspTree::LocalTerminationCriteriaMet(const VspBspTraversalData &data) const
|
---|
720 | {
|
---|
721 | return
|
---|
722 | (((int)data.mRays->size() <= mTermMinRays) ||
|
---|
723 | (data.mPvs <= mTermMinPvs) ||
|
---|
724 | (data.mProbability <= mTermMinProbability) ||
|
---|
725 | (data.GetAvgRayContribution() > mTermMaxRayContribution) ||
|
---|
726 | (data.mDepth >= mTermMaxDepth));
|
---|
727 | }
|
---|
728 |
|
---|
729 |
|
---|
730 | void VspBspTree::AddSubdivisionStats(const int viewCells,
|
---|
731 | const float renderCostDecr,
|
---|
732 | const float splitCandidateCost,
|
---|
733 | const float totalRenderCost,
|
---|
734 | const float avgRenderCost)
|
---|
735 | {
|
---|
736 | mSubdivisionStats
|
---|
737 | << "#ViewCells\n" << viewCells << endl
|
---|
738 | << "#RenderCostDecrease\n" << renderCostDecr << endl
|
---|
739 | << "#SplitCandidateCost\n" << splitCandidateCost << endl
|
---|
740 | << "#TotalRenderCost\n" << totalRenderCost << endl
|
---|
741 | << "#AvgRenderCost\n" << avgRenderCost << endl;
|
---|
742 | }
|
---|
743 |
|
---|
744 |
|
---|
745 | bool VspBspTree::GlobalTerminationCriteriaMet(const VspBspTraversalData &data) const
|
---|
746 | {
|
---|
747 | return
|
---|
748 | (mOutOfMemory
|
---|
749 | || (mBspStats.Leaves() >= mMaxViewCells)
|
---|
750 | || (mGlobalCostMisses >= mTermGlobalCostMissTolerance)
|
---|
751 | );
|
---|
752 | }
|
---|
753 |
|
---|
754 |
|
---|
755 | BspNode *VspBspTree::Subdivide(VspBspTraversalQueue &tQueue,
|
---|
756 | VspBspTraversalData &tData)
|
---|
757 | {
|
---|
758 | BspNode *newNode = tData.mNode;
|
---|
759 |
|
---|
760 | if (!LocalTerminationCriteriaMet(tData) && !GlobalTerminationCriteriaMet(tData))
|
---|
761 | {
|
---|
762 | PolygonContainer coincident;
|
---|
763 |
|
---|
764 | VspBspTraversalData tFrontData;
|
---|
765 | VspBspTraversalData tBackData;
|
---|
766 |
|
---|
767 | // create new interior node and two leaf nodes
|
---|
768 | // or return leaf as it is (if maxCostRatio missed)
|
---|
769 | int splitAxis;
|
---|
770 | bool splitFurther = true;
|
---|
771 | int maxCostMisses = tData.mMaxCostMisses;
|
---|
772 |
|
---|
773 | Plane3 splitPlane;
|
---|
774 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
|
---|
775 |
|
---|
776 | // choose next split plane
|
---|
777 | if (!SelectPlane(splitPlane, leaf, tData, tFrontData, tBackData, splitAxis))
|
---|
778 | {
|
---|
779 | ++ maxCostMisses;
|
---|
780 |
|
---|
781 | if (maxCostMisses > mTermMissTolerance)
|
---|
782 | {
|
---|
783 | // terminate branch because of max cost
|
---|
784 | ++ mBspStats.maxCostNodes;
|
---|
785 | splitFurther = false;
|
---|
786 | }
|
---|
787 | }
|
---|
788 |
|
---|
789 | // if this a valid split => subdivide this node further
|
---|
790 | if (splitFurther) //-- continue subdivision
|
---|
791 | {
|
---|
792 | newNode = SubdivideNode(splitPlane, tData, tFrontData, tBackData, coincident);
|
---|
793 |
|
---|
794 | if (splitAxis < 3)
|
---|
795 | ++ mBspStats.splits[splitAxis];
|
---|
796 | else
|
---|
797 | ++ mBspStats.polySplits;
|
---|
798 |
|
---|
799 | // if it was a kd node (i.e., a box) and the split axis is axis aligned, it is still a kd node
|
---|
800 | tFrontData.mIsKdNode = tBackData.mIsKdNode = (tData.mIsKdNode && (splitAxis < 3));
|
---|
801 |
|
---|
802 | tFrontData.mAxis = tBackData.mAxis = splitAxis;
|
---|
803 |
|
---|
804 | // how often was max cost ratio missed in this branch?
|
---|
805 | tFrontData.mMaxCostMisses = maxCostMisses;
|
---|
806 | tBackData.mMaxCostMisses = maxCostMisses;
|
---|
807 |
|
---|
808 | EvalPriority(tFrontData);
|
---|
809 | EvalPriority(tBackData);
|
---|
810 |
|
---|
811 | // evaluate subdivision stats
|
---|
812 | if (1)
|
---|
813 | {
|
---|
814 | const float cFront = (float)tFrontData.mPvs * tFrontData.mProbability;
|
---|
815 | const float cBack = (float)tBackData.mPvs * tBackData.mProbability;
|
---|
816 | const float cData = (float)tData.mPvs * tData.mProbability;;
|
---|
817 |
|
---|
818 | const float costDecr = (cFront + cBack - cData) / mBox.GetVolume();
|
---|
819 |
|
---|
820 | mTotalCost += costDecr;
|
---|
821 | mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
|
---|
822 |
|
---|
823 | AddSubdivisionStats(mBspStats.Leaves(),
|
---|
824 | -costDecr,
|
---|
825 | 0,
|
---|
826 | mTotalCost,
|
---|
827 | (float)mTotalPvsSize / (float)mBspStats.Leaves());
|
---|
828 | }
|
---|
829 |
|
---|
830 | // push the children on the stack
|
---|
831 | tQueue.push(tFrontData);
|
---|
832 | tQueue.push(tBackData);
|
---|
833 |
|
---|
834 | // delete old leaf node
|
---|
835 | DEL_PTR(tData.mNode);
|
---|
836 | }
|
---|
837 | }
|
---|
838 |
|
---|
839 | //-- terminate traversal and create new view cell
|
---|
840 | if (newNode->IsLeaf())
|
---|
841 | {
|
---|
842 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(newNode);
|
---|
843 |
|
---|
844 | BspViewCell *viewCell = new BspViewCell();
|
---|
845 | leaf->SetViewCell(viewCell);
|
---|
846 |
|
---|
847 | //-- update pvs
|
---|
848 | int conSamp = 0;
|
---|
849 | float sampCon = 0.0f;
|
---|
850 | AddToPvs(leaf, *tData.mRays, sampCon, conSamp);
|
---|
851 |
|
---|
852 | // update scalar pvs size lookup
|
---|
853 | mViewCellsManager->SetScalarPvsSize(viewCell, viewCell->GetPvs().GetSize());
|
---|
854 |
|
---|
855 |
|
---|
856 | mBspStats.contributingSamples += conSamp;
|
---|
857 | mBspStats.sampleContributions += (int)sampCon;
|
---|
858 |
|
---|
859 | //-- store additional info
|
---|
860 | if (mStoreRays)
|
---|
861 | {
|
---|
862 | RayInfoContainer::const_iterator it, it_end = tData.mRays->end();
|
---|
863 | for (it = tData.mRays->begin(); it != it_end; ++ it)
|
---|
864 | {
|
---|
865 | (*it).mRay->Ref();
|
---|
866 | leaf->mVssRays.push_back((*it).mRay);
|
---|
867 | }
|
---|
868 | }
|
---|
869 |
|
---|
870 | // should I check here?
|
---|
871 | if (0 && !mViewCellsManager->CheckValidity(viewCell, 0,
|
---|
872 | mViewCellsManager->GetMaxPvsSize()))
|
---|
873 | {
|
---|
874 | viewCell->SetValid(false);
|
---|
875 | leaf->SetTreeValid(false);
|
---|
876 | PropagateUpValidity(leaf);
|
---|
877 |
|
---|
878 | ++ mBspStats.invalidLeaves;
|
---|
879 | }
|
---|
880 |
|
---|
881 | viewCell->mLeaf = leaf;
|
---|
882 |
|
---|
883 | if (mUseAreaForPvs)
|
---|
884 | viewCell->SetArea(tData.mProbability);
|
---|
885 | else
|
---|
886 | viewCell->SetVolume(tData.mProbability);
|
---|
887 |
|
---|
888 | leaf->mProbability = tData.mProbability;
|
---|
889 |
|
---|
890 | // finally evaluate stats until this leaf
|
---|
891 | if (0)
|
---|
892 | EvaluateLeafStats(tData);
|
---|
893 | }
|
---|
894 |
|
---|
895 | //-- cleanup
|
---|
896 | tData.Clear();
|
---|
897 |
|
---|
898 | return newNode;
|
---|
899 | }
|
---|
900 |
|
---|
901 |
|
---|
902 | // subdivide node using a split plane queue
|
---|
903 | BspNode *VspBspTree::Subdivide(VspBspSplitQueue &tQueue,
|
---|
904 | VspBspSplitCandidate &splitCandidate)
|
---|
905 | {
|
---|
906 | VspBspTraversalData &tData = splitCandidate.mParentData;
|
---|
907 |
|
---|
908 | BspNode *newNode = tData.mNode;
|
---|
909 |
|
---|
910 | if (!LocalTerminationCriteriaMet(tData) && !GlobalTerminationCriteriaMet(tData))
|
---|
911 | {
|
---|
912 | PolygonContainer coincident;
|
---|
913 |
|
---|
914 | VspBspTraversalData tFrontData;
|
---|
915 | VspBspTraversalData tBackData;
|
---|
916 |
|
---|
917 | //-- continue subdivision
|
---|
918 |
|
---|
919 | // create new interior node and two leaf node
|
---|
920 | const Plane3 splitPlane = splitCandidate.mSplitPlane;
|
---|
921 |
|
---|
922 | newNode = SubdivideNode(splitPlane, tData, tFrontData, tBackData, coincident);
|
---|
923 |
|
---|
924 | const int splitAxis = splitCandidate.mSplitAxis;
|
---|
925 | const int maxCostMisses = splitCandidate.mMaxCostMisses;
|
---|
926 |
|
---|
927 | if (splitAxis < 3)
|
---|
928 | ++ mBspStats.splits[splitAxis];
|
---|
929 | else
|
---|
930 | ++ mBspStats.polySplits;
|
---|
931 |
|
---|
932 | tFrontData.mIsKdNode = tBackData.mIsKdNode = (tData.mIsKdNode && (splitAxis < 3));
|
---|
933 | tFrontData.mAxis = tBackData.mAxis = splitAxis;
|
---|
934 |
|
---|
935 | // how often was max cost ratio missed in this branch?
|
---|
936 | tFrontData.mMaxCostMisses = maxCostMisses;
|
---|
937 | tBackData.mMaxCostMisses = maxCostMisses;
|
---|
938 |
|
---|
939 | // statistics
|
---|
940 | if (1)
|
---|
941 | {
|
---|
942 | float cFront = (float)tFrontData.mPvs * tFrontData.mProbability;
|
---|
943 | float cBack = (float)tBackData.mPvs * tBackData.mProbability;
|
---|
944 | float cData = (float)tData.mPvs * tData.mProbability;
|
---|
945 |
|
---|
946 |
|
---|
947 | float costDecr =
|
---|
948 | (cFront + cBack - cData) / mBox.GetVolume();
|
---|
949 |
|
---|
950 | mTotalCost += costDecr;
|
---|
951 | mTotalPvsSize += tFrontData.mPvs + tBackData.mPvs - tData.mPvs;
|
---|
952 |
|
---|
953 | AddSubdivisionStats(mBspStats.Leaves(),
|
---|
954 | -costDecr,
|
---|
955 | splitCandidate.GetPriority(),
|
---|
956 | mTotalCost,
|
---|
957 | (float)mTotalPvsSize / (float)mBspStats.Leaves());
|
---|
958 | }
|
---|
959 |
|
---|
960 |
|
---|
961 | //-- push the new split candidates on the stack
|
---|
962 | VspBspSplitCandidate frontCandidate;
|
---|
963 | VspBspSplitCandidate backCandidate;
|
---|
964 |
|
---|
965 | EvalSplitCandidate(tFrontData, frontCandidate);
|
---|
966 | EvalSplitCandidate(tBackData, backCandidate);
|
---|
967 |
|
---|
968 | tQueue.push(frontCandidate);
|
---|
969 | tQueue.push(backCandidate);
|
---|
970 |
|
---|
971 | // delete old leaf node
|
---|
972 | DEL_PTR(tData.mNode);
|
---|
973 | }
|
---|
974 |
|
---|
975 |
|
---|
976 | //-- terminate traversal and create new view cell
|
---|
977 | if (newNode->IsLeaf())
|
---|
978 | {
|
---|
979 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(newNode);
|
---|
980 |
|
---|
981 | BspViewCell *viewCell = new BspViewCell();
|
---|
982 | leaf->SetViewCell(viewCell);
|
---|
983 |
|
---|
984 | //-- update pvs
|
---|
985 | int conSamp = 0;
|
---|
986 | float sampCon = 0.0f;
|
---|
987 | AddToPvs(leaf, *tData.mRays, sampCon, conSamp);
|
---|
988 |
|
---|
989 | // update scalar pvs size value
|
---|
990 | mViewCellsManager->SetScalarPvsSize(viewCell, viewCell->GetPvs().GetSize());
|
---|
991 |
|
---|
992 | mBspStats.contributingSamples += conSamp;
|
---|
993 | mBspStats.sampleContributions +=(int) sampCon;
|
---|
994 |
|
---|
995 | //-- store additional info
|
---|
996 | if (mStoreRays)
|
---|
997 | {
|
---|
998 | RayInfoContainer::const_iterator it, it_end = tData.mRays->end();
|
---|
999 | for (it = tData.mRays->begin(); it != it_end; ++ it)
|
---|
1000 | {
|
---|
1001 | (*it).mRay->Ref();
|
---|
1002 | leaf->mVssRays.push_back((*it).mRay);
|
---|
1003 | }
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 |
|
---|
1007 | viewCell->mLeaf = leaf;
|
---|
1008 |
|
---|
1009 | if (mUseAreaForPvs)
|
---|
1010 | viewCell->SetArea(tData.mProbability);
|
---|
1011 | else
|
---|
1012 | viewCell->SetVolume(tData.mProbability);
|
---|
1013 |
|
---|
1014 | leaf->mProbability = tData.mProbability;
|
---|
1015 |
|
---|
1016 | // finally evaluate stats until this leaf
|
---|
1017 | if (0)
|
---|
1018 | EvaluateLeafStats(tData);
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | //-- cleanup
|
---|
1022 | tData.Clear();
|
---|
1023 |
|
---|
1024 | return newNode;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 |
|
---|
1028 | void VspBspTree::EvalPriority(VspBspTraversalData &tData) const
|
---|
1029 | {
|
---|
1030 | switch (mNodePriorityQueueType)
|
---|
1031 | {
|
---|
1032 | case BREATH_FIRST:
|
---|
1033 | tData.mPriority = (float)-tData.mDepth;
|
---|
1034 | break;
|
---|
1035 | case DEPTH_FIRST:
|
---|
1036 | tData.mPriority = (float)tData.mDepth;
|
---|
1037 | break;
|
---|
1038 | default:
|
---|
1039 | tData.mPriority = tData.mPvs * tData.mProbability;
|
---|
1040 | //Debug << "priority: " << tData.mPriority << endl;
|
---|
1041 | break;
|
---|
1042 | }
|
---|
1043 | }
|
---|
1044 |
|
---|
1045 |
|
---|
1046 | void VspBspTree::EvalSplitCandidate(VspBspTraversalData &tData,
|
---|
1047 | VspBspSplitCandidate &splitData)
|
---|
1048 | {
|
---|
1049 | VspBspTraversalData frontData;
|
---|
1050 | VspBspTraversalData backData;
|
---|
1051 |
|
---|
1052 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
|
---|
1053 |
|
---|
1054 | // compute locally best split plane
|
---|
1055 | bool success = SelectPlane(splitData.mSplitPlane, leaf, tData,
|
---|
1056 | frontData, backData, splitData.mSplitAxis);
|
---|
1057 |
|
---|
1058 | // TODO: reuse
|
---|
1059 | delete frontData.mGeometry;
|
---|
1060 | delete backData.mGeometry;
|
---|
1061 |
|
---|
1062 | // compute global decrease in render cost
|
---|
1063 | splitData.mPriority = EvalRenderCostDecrease(splitData.mSplitPlane, tData);
|
---|
1064 | splitData.mParentData = tData;
|
---|
1065 | splitData.mMaxCostMisses = success ? tData.mMaxCostMisses : tData.mMaxCostMisses + 1;
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 |
|
---|
1069 | BspInterior *VspBspTree::SubdivideNode(const Plane3 &splitPlane,
|
---|
1070 | VspBspTraversalData &tData,
|
---|
1071 | VspBspTraversalData &frontData,
|
---|
1072 | VspBspTraversalData &backData,
|
---|
1073 | PolygonContainer &coincident)
|
---|
1074 | {
|
---|
1075 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(tData.mNode);
|
---|
1076 |
|
---|
1077 | //-- the front and back traversal data is filled with the new values
|
---|
1078 | frontData.mDepth = tData.mDepth + 1;
|
---|
1079 | frontData.mPolygons = new PolygonContainer();
|
---|
1080 | frontData.mRays = new RayInfoContainer();
|
---|
1081 |
|
---|
1082 | backData.mDepth = tData.mDepth + 1;
|
---|
1083 | backData.mPolygons = new PolygonContainer();
|
---|
1084 | backData.mRays = new RayInfoContainer();
|
---|
1085 |
|
---|
1086 |
|
---|
1087 | //-- subdivide rays
|
---|
1088 | SplitRays(splitPlane,
|
---|
1089 | *tData.mRays,
|
---|
1090 | *frontData.mRays,
|
---|
1091 | *backData.mRays);
|
---|
1092 |
|
---|
1093 |
|
---|
1094 | // compute pvs
|
---|
1095 | frontData.mPvs = ComputePvsSize(*frontData.mRays);
|
---|
1096 | backData.mPvs = ComputePvsSize(*backData.mRays);
|
---|
1097 |
|
---|
1098 | // split front and back node geometry and compute area
|
---|
1099 |
|
---|
1100 | // if geometry was not already computed
|
---|
1101 | if (!frontData.mGeometry && !backData.mGeometry)
|
---|
1102 | {
|
---|
1103 | frontData.mGeometry = new BspNodeGeometry();
|
---|
1104 | backData.mGeometry = new BspNodeGeometry();
|
---|
1105 |
|
---|
1106 | tData.mGeometry->SplitGeometry(*frontData.mGeometry,
|
---|
1107 | *backData.mGeometry,
|
---|
1108 | splitPlane,
|
---|
1109 | mBox,
|
---|
1110 | //0.0f);
|
---|
1111 | mEpsilon);
|
---|
1112 |
|
---|
1113 | if (mUseAreaForPvs)
|
---|
1114 | {
|
---|
1115 | frontData.mProbability = frontData.mGeometry->GetArea();
|
---|
1116 | backData.mProbability = backData.mGeometry->GetArea();
|
---|
1117 | }
|
---|
1118 | else
|
---|
1119 | {
|
---|
1120 | frontData.mProbability = frontData.mGeometry->GetVolume();
|
---|
1121 | backData.mProbability = tData.mProbability - frontData.mProbability;
|
---|
1122 |
|
---|
1123 | // should never come here: wrong volume !!!
|
---|
1124 | if (0)
|
---|
1125 | {
|
---|
1126 | if (frontData.mProbability < -0.00001)
|
---|
1127 | Debug << "fatal error f: " << frontData.mProbability << endl;
|
---|
1128 | if (backData.mProbability < -0.00001)
|
---|
1129 | Debug << "fatal error b: " << backData.mProbability << endl;
|
---|
1130 |
|
---|
1131 | // clamp because of precision issues
|
---|
1132 | if (frontData.mProbability < 0) frontData.mProbability = 0;
|
---|
1133 | if (backData.mProbability < 0) backData.mProbability = 0;
|
---|
1134 | }
|
---|
1135 | }
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 |
|
---|
1139 | // subdivide polygons
|
---|
1140 | SplitPolygons(splitPlane,
|
---|
1141 | *tData.mPolygons,
|
---|
1142 | *frontData.mPolygons,
|
---|
1143 | *backData.mPolygons,
|
---|
1144 | coincident);
|
---|
1145 |
|
---|
1146 |
|
---|
1147 |
|
---|
1148 | ///////////////////////////////////////
|
---|
1149 | // subdivide further
|
---|
1150 |
|
---|
1151 | // store maximal and minimal depth
|
---|
1152 | if (tData.mDepth > mBspStats.maxDepth)
|
---|
1153 | {
|
---|
1154 | Debug << "max depth increases to " << tData.mDepth << " at " << mBspStats.Leaves() << " leaves" << endl;
|
---|
1155 | mBspStats.maxDepth = tData.mDepth;
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | mBspStats.nodes += 2;
|
---|
1159 |
|
---|
1160 |
|
---|
1161 | BspInterior *interior = new BspInterior(splitPlane);
|
---|
1162 |
|
---|
1163 | #ifdef _DEBUG
|
---|
1164 | Debug << interior << endl;
|
---|
1165 | #endif
|
---|
1166 |
|
---|
1167 |
|
---|
1168 | //-- create front and back leaf
|
---|
1169 |
|
---|
1170 | BspInterior *parent = leaf->GetParent();
|
---|
1171 |
|
---|
1172 | // replace a link from node's parent
|
---|
1173 | if (parent)
|
---|
1174 | {
|
---|
1175 | parent->ReplaceChildLink(leaf, interior);
|
---|
1176 | interior->SetParent(parent);
|
---|
1177 | }
|
---|
1178 | else // new root
|
---|
1179 | {
|
---|
1180 | mRoot = interior;
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | // and setup child links
|
---|
1184 | interior->SetupChildLinks(new BspLeaf(interior), new BspLeaf(interior));
|
---|
1185 |
|
---|
1186 | frontData.mNode = interior->GetFront();
|
---|
1187 | backData.mNode = interior->GetBack();
|
---|
1188 |
|
---|
1189 | interior->mTimeStamp = mTimeStamp ++;
|
---|
1190 |
|
---|
1191 |
|
---|
1192 | //DEL_PTR(leaf);
|
---|
1193 | return interior;
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 |
|
---|
1197 | void VspBspTree::AddToPvs(BspLeaf *leaf,
|
---|
1198 | const RayInfoContainer &rays,
|
---|
1199 | float &sampleContributions,
|
---|
1200 | int &contributingSamples)
|
---|
1201 | {
|
---|
1202 | sampleContributions = 0;
|
---|
1203 | contributingSamples = 0;
|
---|
1204 |
|
---|
1205 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
1206 |
|
---|
1207 | ViewCellLeaf *vc = leaf->GetViewCell();
|
---|
1208 |
|
---|
1209 | // add contributions from samples to the PVS
|
---|
1210 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
1211 | {
|
---|
1212 | float sc = 0.0f;
|
---|
1213 | VssRay *ray = (*it).mRay;
|
---|
1214 |
|
---|
1215 | bool madeContrib = false;
|
---|
1216 | float contribution;
|
---|
1217 |
|
---|
1218 | if (ray->mTerminationObject)
|
---|
1219 | {
|
---|
1220 | if (vc->AddPvsSample(ray->mTerminationObject, ray->mPdf, contribution))
|
---|
1221 | madeContrib = true;
|
---|
1222 | sc += contribution;
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | // only count termination objects?
|
---|
1226 | if (COUNT_ORIGIN_OBJECTS && ray->mOriginObject)
|
---|
1227 | {
|
---|
1228 | if (vc->AddPvsSample(ray->mOriginObject, ray->mPdf, contribution))
|
---|
1229 | madeContrib = true;
|
---|
1230 |
|
---|
1231 | sc += contribution;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | sampleContributions += sc;
|
---|
1235 |
|
---|
1236 | if (madeContrib)
|
---|
1237 | ++ contributingSamples;
|
---|
1238 |
|
---|
1239 | // note: vss rays are never deleted
|
---|
1240 | if (0) leaf->mVssRays.push_back(new VssRay(*ray));
|
---|
1241 | }
|
---|
1242 | }
|
---|
1243 |
|
---|
1244 |
|
---|
1245 | void VspBspTree::SortSplitCandidates(const RayInfoContainer &rays,
|
---|
1246 | const int axis,
|
---|
1247 | float minBand,
|
---|
1248 | float maxBand)
|
---|
1249 | {
|
---|
1250 | mLocalSplitCandidates->clear();
|
---|
1251 |
|
---|
1252 | int requestedSize = 2 * (int)(rays.size());
|
---|
1253 | // creates a sorted split candidates array
|
---|
1254 | if (mLocalSplitCandidates->capacity() > 500000 &&
|
---|
1255 | requestedSize < (int)(mLocalSplitCandidates->capacity() / 10) )
|
---|
1256 | {
|
---|
1257 | delete mLocalSplitCandidates;
|
---|
1258 | mLocalSplitCandidates = new vector<SortableEntry>;
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | mLocalSplitCandidates->reserve(requestedSize);
|
---|
1262 |
|
---|
1263 | float pos;
|
---|
1264 |
|
---|
1265 | // float values => don't compare with exact values
|
---|
1266 | if (0)
|
---|
1267 | {
|
---|
1268 | minBand += Limits::Small;
|
---|
1269 | maxBand -= Limits::Small;
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | // insert all queries
|
---|
1273 | for (RayInfoContainer::const_iterator ri = rays.begin(); ri < rays.end(); ++ ri)
|
---|
1274 | {
|
---|
1275 | const bool positive = (*ri).mRay->HasPosDir(axis);
|
---|
1276 |
|
---|
1277 | pos = (*ri).ExtrapOrigin(axis);
|
---|
1278 | // clamp to min / max band
|
---|
1279 | if (0) ClipValue(pos, minBand, maxBand);
|
---|
1280 |
|
---|
1281 | mLocalSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMin : SortableEntry::ERayMax,
|
---|
1282 | pos, (*ri).mRay));
|
---|
1283 |
|
---|
1284 | pos = (*ri).ExtrapTermination(axis);
|
---|
1285 | // clamp to min / max band
|
---|
1286 | if (0) ClipValue(pos, minBand, maxBand);
|
---|
1287 |
|
---|
1288 | mLocalSplitCandidates->push_back(SortableEntry(positive ? SortableEntry::ERayMax : SortableEntry::ERayMin,
|
---|
1289 | pos, (*ri).mRay));
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | stable_sort(mLocalSplitCandidates->begin(), mLocalSplitCandidates->end());
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 |
|
---|
1296 | float VspBspTree::BestCostRatioHeuristics(const RayInfoContainer &rays,
|
---|
1297 | const AxisAlignedBox3 &box,
|
---|
1298 | const int pvsSize,
|
---|
1299 | const int axis,
|
---|
1300 | float &position)
|
---|
1301 | {
|
---|
1302 | const float minBox = box.Min(axis);
|
---|
1303 | const float maxBox = box.Max(axis);
|
---|
1304 |
|
---|
1305 | const float sizeBox = maxBox - minBox;
|
---|
1306 |
|
---|
1307 | const float minBand = minBox + mMinBand * sizeBox;
|
---|
1308 | const float maxBand = minBox + mMaxBand * sizeBox;
|
---|
1309 |
|
---|
1310 | SortSplitCandidates(rays, axis, minBand, maxBand);
|
---|
1311 |
|
---|
1312 | // go through the lists, count the number of objects left and right
|
---|
1313 | // and evaluate the following cost funcion:
|
---|
1314 | // C = ct_div_ci + (ql*rl + qr*rr)/queries
|
---|
1315 |
|
---|
1316 | int pvsl = 0;
|
---|
1317 | int pvsr = pvsSize;
|
---|
1318 |
|
---|
1319 | int pvsBack = pvsl;
|
---|
1320 | int pvsFront = pvsr;
|
---|
1321 |
|
---|
1322 | float sum = (float)pvsSize * sizeBox;
|
---|
1323 | float minSum = 1e20f;
|
---|
1324 |
|
---|
1325 |
|
---|
1326 | // if no border can be found, take mid split
|
---|
1327 | position = minBox + 0.5f * sizeBox;
|
---|
1328 |
|
---|
1329 | // the relative cost ratio
|
---|
1330 | float ratio = 99999999.0f;
|
---|
1331 | bool splitPlaneFound = false;
|
---|
1332 |
|
---|
1333 | Intersectable::NewMail();
|
---|
1334 |
|
---|
1335 | RayInfoContainer::const_iterator ri, ri_end = rays.end();
|
---|
1336 |
|
---|
1337 | // set all object as belonging to the front pvs
|
---|
1338 | for(ri = rays.begin(); ri != ri_end; ++ ri)
|
---|
1339 | {
|
---|
1340 | Intersectable *oObject = (*ri).mRay->mOriginObject;
|
---|
1341 | Intersectable *tObject = (*ri).mRay->mTerminationObject;
|
---|
1342 |
|
---|
1343 | if (COUNT_ORIGIN_OBJECTS && oObject)
|
---|
1344 | {
|
---|
1345 | if (!oObject->Mailed())
|
---|
1346 | {
|
---|
1347 | oObject->Mail();
|
---|
1348 | oObject->mCounter = 1;
|
---|
1349 | }
|
---|
1350 | else
|
---|
1351 | {
|
---|
1352 | ++ oObject->mCounter;
|
---|
1353 | }
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | if (tObject)
|
---|
1357 | {
|
---|
1358 | if (!tObject->Mailed())
|
---|
1359 | {
|
---|
1360 | tObject->Mail();
|
---|
1361 | tObject->mCounter = 1;
|
---|
1362 | }
|
---|
1363 | else
|
---|
1364 | {
|
---|
1365 | ++ tObject->mCounter;
|
---|
1366 | }
|
---|
1367 | }
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | Intersectable::NewMail();
|
---|
1371 |
|
---|
1372 | vector<SortableEntry>::const_iterator ci, ci_end = mLocalSplitCandidates->end();
|
---|
1373 |
|
---|
1374 | for (ci = mLocalSplitCandidates->begin(); ci != ci_end; ++ ci)
|
---|
1375 | {
|
---|
1376 | VssRay *ray;
|
---|
1377 | ray = (*ci).ray;
|
---|
1378 |
|
---|
1379 | Intersectable *oObject = ray->mOriginObject;
|
---|
1380 | Intersectable *tObject = ray->mTerminationObject;
|
---|
1381 |
|
---|
1382 |
|
---|
1383 | switch ((*ci).type)
|
---|
1384 | {
|
---|
1385 | case SortableEntry::ERayMin:
|
---|
1386 | {
|
---|
1387 | if (COUNT_ORIGIN_OBJECTS && oObject && !oObject->Mailed())
|
---|
1388 | {
|
---|
1389 | oObject->Mail();
|
---|
1390 | ++ pvsl;
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | if (tObject && !tObject->Mailed())
|
---|
1394 | {
|
---|
1395 | tObject->Mail();
|
---|
1396 | ++ pvsl;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | break;
|
---|
1400 | }
|
---|
1401 | case SortableEntry::ERayMax:
|
---|
1402 | {
|
---|
1403 | if (COUNT_ORIGIN_OBJECTS && oObject)
|
---|
1404 | {
|
---|
1405 | if (-- oObject->mCounter == 0)
|
---|
1406 | -- pvsr;
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | if (tObject)
|
---|
1410 | {
|
---|
1411 | if (-- tObject->mCounter == 0)
|
---|
1412 | -- pvsr;
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | break;
|
---|
1416 | }
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 |
|
---|
1420 | // Note: we compare size of bounding boxes of front and back side because
|
---|
1421 | // of efficiency reasons (otherwise a new geometry would have to be computed
|
---|
1422 | // in each step and incremential evaluation would be difficult.
|
---|
1423 | // but then errors happen if the geometry is not an axis aligned box
|
---|
1424 | // (i.e., if a geometry aligned split was taken before)
|
---|
1425 | // question: is it sufficient to make this approximation?
|
---|
1426 | if (((*ci).value >= minBand) && ((*ci).value <= maxBand))
|
---|
1427 | {
|
---|
1428 | sum = pvsl * ((*ci).value - minBox) + pvsr * (maxBox - (*ci).value);
|
---|
1429 |
|
---|
1430 | //Debug << "pos=" << (*ci).value << "\t pvs=(" << pvsl << "," << pvsr << ")" << endl;
|
---|
1431 | //Debug << "cost= " << sum << endl;
|
---|
1432 |
|
---|
1433 | if (sum < minSum)
|
---|
1434 | {
|
---|
1435 | splitPlaneFound = true;
|
---|
1436 |
|
---|
1437 | minSum = sum;
|
---|
1438 | position = (*ci).value;
|
---|
1439 |
|
---|
1440 | pvsBack = pvsl;
|
---|
1441 | pvsFront = pvsr;
|
---|
1442 | }
|
---|
1443 | }
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 |
|
---|
1447 | // -- compute cost
|
---|
1448 | const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize();
|
---|
1449 | const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize();
|
---|
1450 |
|
---|
1451 | const float pOverall = sizeBox;
|
---|
1452 |
|
---|
1453 | const float pBack = position - minBox;
|
---|
1454 | const float pFront = maxBox - position;
|
---|
1455 |
|
---|
1456 | const float penaltyOld = EvalPvsPenalty(pvsSize, lowerPvsLimit, upperPvsLimit);
|
---|
1457 | const float penaltyFront = EvalPvsPenalty(pvsFront, lowerPvsLimit, upperPvsLimit);
|
---|
1458 | const float penaltyBack = EvalPvsPenalty(pvsBack, lowerPvsLimit, upperPvsLimit);
|
---|
1459 |
|
---|
1460 | const float oldRenderCost = penaltyOld * pOverall;
|
---|
1461 | const float newRenderCost = penaltyFront * pFront + penaltyBack * pBack;
|
---|
1462 |
|
---|
1463 | if (splitPlaneFound)
|
---|
1464 | {
|
---|
1465 | ratio = mPvsFactor * newRenderCost / (oldRenderCost + Limits::Small);
|
---|
1466 | }
|
---|
1467 | //if (axis != 1)
|
---|
1468 | //Debug << "axis=" << axis << " costRatio=" << ratio << " pos=" << position << " t=" << (position - minBox) / (maxBox - minBox)
|
---|
1469 | // <<"\t pb=(" << pvsBack << ")\t pf=(" << pvsFront << ")" << endl;
|
---|
1470 |
|
---|
1471 | return ratio;
|
---|
1472 | }
|
---|
1473 |
|
---|
1474 |
|
---|
1475 | float VspBspTree::SelectAxisAlignedPlane(Plane3 &plane,
|
---|
1476 | const VspBspTraversalData &tData,
|
---|
1477 | int &axis,
|
---|
1478 | BspNodeGeometry **frontGeom,
|
---|
1479 | BspNodeGeometry **backGeom,
|
---|
1480 | float &pFront,
|
---|
1481 | float &pBack,
|
---|
1482 | const bool isKdNode)
|
---|
1483 | {
|
---|
1484 | float nPosition[3];
|
---|
1485 | float nCostRatio[3];
|
---|
1486 | float nProbFront[3];
|
---|
1487 | float nProbBack[3];
|
---|
1488 |
|
---|
1489 | BspNodeGeometry *nFrontGeom[3];
|
---|
1490 | BspNodeGeometry *nBackGeom[3];
|
---|
1491 |
|
---|
1492 | // set to NULL, so I can find out which gemetry was stored
|
---|
1493 | for (int i = 0; i < 3; ++ i)
|
---|
1494 | {
|
---|
1495 | nFrontGeom[i] = NULL;
|
---|
1496 | nBackGeom[i] = NULL;
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | // create bounding box of node geometry
|
---|
1500 | AxisAlignedBox3 box;
|
---|
1501 |
|
---|
1502 | //TODO: for kd split geometry already is box => only take minmax vertices
|
---|
1503 | if (1)
|
---|
1504 | {
|
---|
1505 | // get bounding box from geometry
|
---|
1506 | tData.mGeometry->GetBoundingBox(box);
|
---|
1507 | }
|
---|
1508 | else
|
---|
1509 | {
|
---|
1510 | box.Initialize();
|
---|
1511 | RayInfoContainer::const_iterator ri, ri_end = tData.mRays->end();
|
---|
1512 |
|
---|
1513 | for(ri = tData.mRays->begin(); ri < ri_end; ++ ri)
|
---|
1514 | box.Include((*ri).ExtrapTermination());
|
---|
1515 | }
|
---|
1516 |
|
---|
1517 | int sAxis = 0;
|
---|
1518 | int bestAxis;
|
---|
1519 |
|
---|
1520 | // if max cost ratio is exceeded, take split along longest axis instead
|
---|
1521 | const float maxCostRatioForArbitraryAxis = 0.9f;
|
---|
1522 |
|
---|
1523 | if (mUseDrivingAxisIfMaxCostViolated)
|
---|
1524 | bestAxis = box.Size().DrivingAxis();
|
---|
1525 | else
|
---|
1526 | bestAxis = -1;
|
---|
1527 |
|
---|
1528 | #if 0
|
---|
1529 | // maximum cost ratio for axis to be valid:
|
---|
1530 | // if exceeded, spatial mid split is used instead
|
---|
1531 | const maxCostRatioForHeur = 0.99f;
|
---|
1532 | #endif
|
---|
1533 |
|
---|
1534 | // if we use some kind of specialised fixed axis
|
---|
1535 | const bool useSpecialAxis =
|
---|
1536 | mOnlyDrivingAxis || mUseRandomAxis || mCirculatingAxis;
|
---|
1537 |
|
---|
1538 | if (mUseRandomAxis)
|
---|
1539 | sAxis = Random(3);
|
---|
1540 | else if (mCirculatingAxis)
|
---|
1541 | sAxis = (tData.mAxis + 1) % 3;
|
---|
1542 | else if (mOnlyDrivingAxis)
|
---|
1543 | sAxis = box.Size().DrivingAxis();
|
---|
1544 |
|
---|
1545 |
|
---|
1546 | //Debug << "use special axis: " << useSpecialAxis << endl;
|
---|
1547 | //Debug << "axis: " << sAxis << " drivingaxis: " << box.Size().DrivingAxis();
|
---|
1548 |
|
---|
1549 | for (axis = 0; axis < 3 ; ++ axis)
|
---|
1550 | {
|
---|
1551 | if (!useSpecialAxis || (axis == sAxis))
|
---|
1552 | {
|
---|
1553 | //-- place split plane using heuristics
|
---|
1554 |
|
---|
1555 | if (mUseCostHeuristics)
|
---|
1556 | {
|
---|
1557 | nCostRatio[axis] =
|
---|
1558 | BestCostRatioHeuristics(*tData.mRays,
|
---|
1559 | box,
|
---|
1560 | tData.mPvs,
|
---|
1561 | axis,
|
---|
1562 | nPosition[axis]);
|
---|
1563 | }
|
---|
1564 | else //-- split plane position is spatial median
|
---|
1565 | {
|
---|
1566 |
|
---|
1567 | nPosition[axis] = (box.Min()[axis] + box.Max()[axis]) * 0.5f;
|
---|
1568 | Vector3 normal(0,0,0); normal[axis] = 1.0f;
|
---|
1569 |
|
---|
1570 | // allows faster split because we have axis aligned kd tree boxes
|
---|
1571 | if (isKdNode)
|
---|
1572 | {
|
---|
1573 | nCostRatio[axis] = EvalAxisAlignedSplitCost(tData,
|
---|
1574 | box,
|
---|
1575 | axis,
|
---|
1576 | nPosition[axis],
|
---|
1577 | nProbFront[axis],
|
---|
1578 | nProbBack[axis]);
|
---|
1579 |
|
---|
1580 | Vector3 pos;
|
---|
1581 |
|
---|
1582 | // create back geometry from box
|
---|
1583 | // NOTE: the geometry is returned from the function so we
|
---|
1584 | // don't have to recompute it when possible
|
---|
1585 | pos = box.Max(); pos[axis] = nPosition[axis];
|
---|
1586 | AxisAlignedBox3 bBox(box.Min(), pos);
|
---|
1587 | PolygonContainer fPolys;
|
---|
1588 | bBox.ExtractPolys(fPolys);
|
---|
1589 |
|
---|
1590 | nBackGeom[axis] = new BspNodeGeometry(fPolys);
|
---|
1591 |
|
---|
1592 | //-- create front geometry from box
|
---|
1593 | pos = box.Min(); pos[axis] = nPosition[axis];
|
---|
1594 | AxisAlignedBox3 fBox(pos, box.Max());
|
---|
1595 |
|
---|
1596 | PolygonContainer bPolys;
|
---|
1597 | fBox.ExtractPolys(bPolys);
|
---|
1598 | nFrontGeom[axis] = new BspNodeGeometry(bPolys);
|
---|
1599 | }
|
---|
1600 | else
|
---|
1601 | {
|
---|
1602 | nFrontGeom[axis] = new BspNodeGeometry();
|
---|
1603 | nBackGeom[axis] = new BspNodeGeometry();
|
---|
1604 |
|
---|
1605 | nCostRatio[axis] =
|
---|
1606 | EvalSplitPlaneCost(Plane3(normal, nPosition[axis]),
|
---|
1607 | tData, *nFrontGeom[axis], *nBackGeom[axis],
|
---|
1608 | nProbFront[axis], nProbBack[axis]);
|
---|
1609 | }
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 |
|
---|
1613 | if (mUseDrivingAxisIfMaxCostViolated)
|
---|
1614 | {
|
---|
1615 | // we take longest axis split if cost ratio exceeds threshold
|
---|
1616 | if (nCostRatio[axis] < min(maxCostRatioForArbitraryAxis, nCostRatio[bestAxis]))
|
---|
1617 | {
|
---|
1618 | bestAxis = axis;
|
---|
1619 | }
|
---|
1620 | /*else if (nCostRatio[axis] < nCostRatio[bestAxis])
|
---|
1621 | {
|
---|
1622 | Debug << "taking split along longest axis (" << bestAxis << ") instead of (" << axis << ")" << endl;
|
---|
1623 | }*/
|
---|
1624 |
|
---|
1625 | }
|
---|
1626 | else
|
---|
1627 | {
|
---|
1628 | if (bestAxis == -1)
|
---|
1629 | {
|
---|
1630 | bestAxis = axis;
|
---|
1631 | }
|
---|
1632 | else if (nCostRatio[axis] < nCostRatio[bestAxis])
|
---|
1633 | {
|
---|
1634 | bestAxis = axis;
|
---|
1635 | }
|
---|
1636 | }
|
---|
1637 | }
|
---|
1638 | }
|
---|
1639 |
|
---|
1640 | //-- assign values
|
---|
1641 |
|
---|
1642 | axis = bestAxis;
|
---|
1643 | pFront = nProbFront[bestAxis];
|
---|
1644 | pBack = nProbBack[bestAxis];
|
---|
1645 |
|
---|
1646 | // assign best split nodes geometry
|
---|
1647 | *frontGeom = nFrontGeom[bestAxis];
|
---|
1648 | *backGeom = nBackGeom[bestAxis];
|
---|
1649 |
|
---|
1650 | // and delete other geometry
|
---|
1651 | DEL_PTR(nFrontGeom[(bestAxis + 1) % 3]);
|
---|
1652 | DEL_PTR(nBackGeom[(bestAxis + 2) % 3]);
|
---|
1653 |
|
---|
1654 | //-- split plane
|
---|
1655 | Vector3 normal(0,0,0); normal[bestAxis] = 1;
|
---|
1656 | plane = Plane3(normal, nPosition[bestAxis]);
|
---|
1657 |
|
---|
1658 | //Debug << "best axis: " << bestAxis << " pos " << nPosition[bestAxis] << endl;
|
---|
1659 |
|
---|
1660 | return nCostRatio[bestAxis];
|
---|
1661 | }
|
---|
1662 |
|
---|
1663 |
|
---|
1664 | bool VspBspTree::SelectPlane(Plane3 &bestPlane,
|
---|
1665 | BspLeaf *leaf,
|
---|
1666 | VspBspTraversalData &data,
|
---|
1667 | VspBspTraversalData &frontData,
|
---|
1668 | VspBspTraversalData &backData,
|
---|
1669 | int &splitAxis)
|
---|
1670 | {
|
---|
1671 | // HACK matt: subdivide regularily to certain depth
|
---|
1672 | if (data.mDepth < 0) // question matt: why depth < 0 ?
|
---|
1673 | {
|
---|
1674 | cout << "depth: " << data.mDepth << endl;
|
---|
1675 |
|
---|
1676 | // return axis aligned split
|
---|
1677 | AxisAlignedBox3 box;
|
---|
1678 | box.Initialize();
|
---|
1679 |
|
---|
1680 | // create bounding box of region
|
---|
1681 | data.mGeometry->GetBoundingBox(box);
|
---|
1682 |
|
---|
1683 | const int axis = box.Size().DrivingAxis();
|
---|
1684 | const Vector3 position = (box.Min()[axis] + box.Max()[axis]) * 0.5f;
|
---|
1685 |
|
---|
1686 | Vector3 norm(0,0,0); norm[axis] = 1.0f;
|
---|
1687 | bestPlane = Plane3(norm, position);
|
---|
1688 | splitAxis = axis;
|
---|
1689 |
|
---|
1690 | return true;
|
---|
1691 | }
|
---|
1692 |
|
---|
1693 | // simplest strategy: just take next polygon
|
---|
1694 | if (mSplitPlaneStrategy & RANDOM_POLYGON)
|
---|
1695 | {
|
---|
1696 | if (!data.mPolygons->empty())
|
---|
1697 | {
|
---|
1698 | const int randIdx =
|
---|
1699 | (int)RandomValue(0, (Real)((int)data.mPolygons->size() - 1));
|
---|
1700 | Polygon3 *nextPoly = (*data.mPolygons)[randIdx];
|
---|
1701 |
|
---|
1702 | bestPlane = nextPoly->GetSupportingPlane();
|
---|
1703 | return true;
|
---|
1704 | }
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | //-- use heuristics to find appropriate plane
|
---|
1708 |
|
---|
1709 | // intermediate plane
|
---|
1710 | Plane3 plane;
|
---|
1711 | float lowestCost = MAX_FLOAT;
|
---|
1712 |
|
---|
1713 | // decides if the first few splits should be only axisAligned
|
---|
1714 | const bool onlyAxisAligned =
|
---|
1715 | (mSplitPlaneStrategy & AXIS_ALIGNED) &&
|
---|
1716 | ((int)data.mRays->size() > mTermMinRaysForAxisAligned) &&
|
---|
1717 | ((int)data.GetAvgRayContribution() < mTermMaxRayContriForAxisAligned);
|
---|
1718 |
|
---|
1719 | const int limit = onlyAxisAligned ? 0 :
|
---|
1720 | Min((int)data.mPolygons->size(), mMaxPolyCandidates);
|
---|
1721 |
|
---|
1722 | float candidateCost;
|
---|
1723 |
|
---|
1724 | int maxIdx = (int)data.mPolygons->size();
|
---|
1725 |
|
---|
1726 | for (int i = 0; i < limit; ++ i)
|
---|
1727 | {
|
---|
1728 | // the already taken candidates are stored behind maxIdx
|
---|
1729 | // => assure that no index is taken twice
|
---|
1730 | const int candidateIdx = (int)RandomValue(0, (Real)(-- maxIdx));
|
---|
1731 | Polygon3 *poly = (*data.mPolygons)[candidateIdx];
|
---|
1732 |
|
---|
1733 | // swap candidate to the end to avoid testing same plane
|
---|
1734 | std::swap((*data.mPolygons)[maxIdx], (*data.mPolygons)[candidateIdx]);
|
---|
1735 | //Polygon3 *poly = (*data.mPolygons)[(int)RandomValue(0, (int)polys.size() - 1)];
|
---|
1736 |
|
---|
1737 | // evaluate current candidate
|
---|
1738 | BspNodeGeometry fGeom, bGeom;
|
---|
1739 | float fArea, bArea;
|
---|
1740 | plane = poly->GetSupportingPlane();
|
---|
1741 | candidateCost = EvalSplitPlaneCost(plane, data, fGeom, bGeom, fArea, bArea);
|
---|
1742 |
|
---|
1743 | if (candidateCost < lowestCost)
|
---|
1744 | {
|
---|
1745 | bestPlane = plane;
|
---|
1746 | lowestCost = candidateCost;
|
---|
1747 | }
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 |
|
---|
1751 | //-- evaluate axis aligned splits
|
---|
1752 |
|
---|
1753 | int axis;
|
---|
1754 | BspNodeGeometry *fGeom, *bGeom;
|
---|
1755 | float pFront, pBack;
|
---|
1756 |
|
---|
1757 | candidateCost = 99999999.0f;
|
---|
1758 |
|
---|
1759 | // as a variant, we take axis aligned split only if there is
|
---|
1760 | // more polygon available to guide the split
|
---|
1761 | if (!mUsePolygonSplitIfAvailable || data.mPolygons->empty())
|
---|
1762 | {
|
---|
1763 | candidateCost = SelectAxisAlignedPlane(plane,
|
---|
1764 | data,
|
---|
1765 | axis,
|
---|
1766 | &fGeom,
|
---|
1767 | &bGeom,
|
---|
1768 | pFront,
|
---|
1769 | pBack,
|
---|
1770 | data.mIsKdNode);
|
---|
1771 | }
|
---|
1772 |
|
---|
1773 | splitAxis = 3;
|
---|
1774 |
|
---|
1775 | if (candidateCost < lowestCost)
|
---|
1776 | {
|
---|
1777 | bestPlane = plane;
|
---|
1778 | lowestCost = candidateCost;
|
---|
1779 | splitAxis = axis;
|
---|
1780 |
|
---|
1781 | // assign already computed values
|
---|
1782 | // we can do this because we always save the
|
---|
1783 | // computed values from the axis aligned splits
|
---|
1784 |
|
---|
1785 | if (fGeom && bGeom)
|
---|
1786 | {
|
---|
1787 | frontData.mGeometry = fGeom;
|
---|
1788 | backData.mGeometry = bGeom;
|
---|
1789 |
|
---|
1790 | frontData.mProbability = pFront;
|
---|
1791 | backData.mProbability = pBack;
|
---|
1792 | }
|
---|
1793 | }
|
---|
1794 | else
|
---|
1795 | {
|
---|
1796 | DEL_PTR(fGeom);
|
---|
1797 | DEL_PTR(bGeom);
|
---|
1798 | }
|
---|
1799 |
|
---|
1800 | #ifdef _DEBUG
|
---|
1801 | Debug << "plane lowest cost: " << lowestCost << endl;
|
---|
1802 | #endif
|
---|
1803 |
|
---|
1804 | // exeeded relative max cost ratio
|
---|
1805 | if (lowestCost > mTermMaxCostRatio)
|
---|
1806 | {
|
---|
1807 | return false;
|
---|
1808 | }
|
---|
1809 |
|
---|
1810 | return true;
|
---|
1811 | }
|
---|
1812 |
|
---|
1813 |
|
---|
1814 | Plane3 VspBspTree::ChooseCandidatePlane(const RayInfoContainer &rays) const
|
---|
1815 | {
|
---|
1816 | const int candidateIdx = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
1817 |
|
---|
1818 | const Vector3 minPt = rays[candidateIdx].ExtrapOrigin();
|
---|
1819 | const Vector3 maxPt = rays[candidateIdx].ExtrapTermination();
|
---|
1820 |
|
---|
1821 | const Vector3 pt = (maxPt + minPt) * 0.5;
|
---|
1822 | const Vector3 normal = Normalize(rays[candidateIdx].mRay->GetDir());
|
---|
1823 |
|
---|
1824 | return Plane3(normal, pt);
|
---|
1825 | }
|
---|
1826 |
|
---|
1827 |
|
---|
1828 | Plane3 VspBspTree::ChooseCandidatePlane2(const RayInfoContainer &rays) const
|
---|
1829 | {
|
---|
1830 | Vector3 pt[3];
|
---|
1831 |
|
---|
1832 | int idx[3];
|
---|
1833 | int cmaxT = 0;
|
---|
1834 | int cminT = 0;
|
---|
1835 | bool chooseMin = false;
|
---|
1836 |
|
---|
1837 | for (int j = 0; j < 3; ++ j)
|
---|
1838 | {
|
---|
1839 | idx[j] = (int)RandomValue(0, (Real)((int)rays.size() * 2 - 1));
|
---|
1840 |
|
---|
1841 | if (idx[j] >= (int)rays.size())
|
---|
1842 | {
|
---|
1843 | idx[j] -= (int)rays.size();
|
---|
1844 |
|
---|
1845 | chooseMin = (cminT < 2);
|
---|
1846 | }
|
---|
1847 | else
|
---|
1848 | chooseMin = (cmaxT < 2);
|
---|
1849 |
|
---|
1850 | RayInfo rayInf = rays[idx[j]];
|
---|
1851 | pt[j] = chooseMin ? rayInf.ExtrapOrigin() : rayInf.ExtrapTermination();
|
---|
1852 | }
|
---|
1853 |
|
---|
1854 | return Plane3(pt[0], pt[1], pt[2]);
|
---|
1855 | }
|
---|
1856 |
|
---|
1857 |
|
---|
1858 | Plane3 VspBspTree::ChooseCandidatePlane3(const RayInfoContainer &rays) const
|
---|
1859 | {
|
---|
1860 | Vector3 pt[3];
|
---|
1861 |
|
---|
1862 | int idx1 = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
1863 | int idx2 = (int)RandomValue(0, (Real)((int)rays.size() - 1));
|
---|
1864 |
|
---|
1865 | // check if rays different
|
---|
1866 | if (idx1 == idx2)
|
---|
1867 | idx2 = (idx2 + 1) % (int)rays.size();
|
---|
1868 |
|
---|
1869 | const RayInfo ray1 = rays[idx1];
|
---|
1870 | const RayInfo ray2 = rays[idx2];
|
---|
1871 |
|
---|
1872 | // normal vector of the plane parallel to both lines
|
---|
1873 | const Vector3 norm = Normalize(CrossProd(ray1.mRay->GetDir(), ray2.mRay->GetDir()));
|
---|
1874 |
|
---|
1875 | // vector from line 1 to line 2
|
---|
1876 | const Vector3 vd = ray2.ExtrapOrigin() - ray1.ExtrapOrigin();
|
---|
1877 |
|
---|
1878 | // project vector on normal to get distance
|
---|
1879 | const float dist = DotProd(vd, norm);
|
---|
1880 |
|
---|
1881 | // point on plane lies halfway between the two planes
|
---|
1882 | const Vector3 planePt = ray1.ExtrapOrigin() + norm * dist * 0.5;
|
---|
1883 |
|
---|
1884 | return Plane3(norm, planePt);
|
---|
1885 | }
|
---|
1886 |
|
---|
1887 |
|
---|
1888 | inline void VspBspTree::GenerateUniqueIdsForPvs()
|
---|
1889 | {
|
---|
1890 | Intersectable::NewMail(); sBackId = Intersectable::sMailId;
|
---|
1891 | Intersectable::NewMail(); sFrontId = Intersectable::sMailId;
|
---|
1892 | Intersectable::NewMail(); sFrontAndBackId = Intersectable::sMailId;
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 |
|
---|
1896 | float VspBspTree::EvalRenderCostDecrease(const Plane3 &candidatePlane,
|
---|
1897 | const VspBspTraversalData &data) const
|
---|
1898 | {
|
---|
1899 | float pvsFront = 0;
|
---|
1900 | float pvsBack = 0;
|
---|
1901 | float totalPvs = 0;
|
---|
1902 |
|
---|
1903 | // probability that view point lies in back / front node
|
---|
1904 | float pOverall = data.mProbability;
|
---|
1905 | float pFront = 0;
|
---|
1906 | float pBack = 0;
|
---|
1907 |
|
---|
1908 |
|
---|
1909 | // create unique ids for pvs heuristics
|
---|
1910 | GenerateUniqueIdsForPvs();
|
---|
1911 |
|
---|
1912 | for (int i = 0; i < data.mRays->size(); ++ i)
|
---|
1913 | {
|
---|
1914 | RayInfo rayInf = (*data.mRays)[i];
|
---|
1915 |
|
---|
1916 | float t;
|
---|
1917 | VssRay *ray = rayInf.mRay;
|
---|
1918 | const int cf = rayInf.ComputeRayIntersection(candidatePlane, t);
|
---|
1919 |
|
---|
1920 | // find front and back pvs for origing and termination object
|
---|
1921 | AddObjToPvs(ray->mTerminationObject, cf, pvsFront, pvsBack, totalPvs);
|
---|
1922 |
|
---|
1923 | if (COUNT_ORIGIN_OBJECTS)
|
---|
1924 | AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs);
|
---|
1925 | }
|
---|
1926 |
|
---|
1927 |
|
---|
1928 | BspNodeGeometry geomFront;
|
---|
1929 | BspNodeGeometry geomBack;
|
---|
1930 |
|
---|
1931 | // construct child geometry with regard to the candidate split plane
|
---|
1932 | data.mGeometry->SplitGeometry(geomFront,
|
---|
1933 | geomBack,
|
---|
1934 | candidatePlane,
|
---|
1935 | mBox,
|
---|
1936 | //0.0f);
|
---|
1937 | mEpsilon);
|
---|
1938 |
|
---|
1939 | if (!mUseAreaForPvs) // use front and back cell areas to approximate volume
|
---|
1940 | {
|
---|
1941 | pFront = geomFront.GetVolume();
|
---|
1942 | pBack = pOverall - pFront;
|
---|
1943 |
|
---|
1944 | // something is wrong with the volume
|
---|
1945 | if (0 && ((pFront < 0.0) || (pBack < 0.0)))
|
---|
1946 | {
|
---|
1947 | Debug << "ERROR in volume:\n"
|
---|
1948 | << "volume f :" << pFront << " b: " << pBack << " p: " << pOverall
|
---|
1949 | << ", real volume f: " << pFront << " b: " << geomBack.GetVolume()
|
---|
1950 | << ", #polygons f: " << geomFront.Size() << " b: " << geomBack.Size() << " p: " << data.mGeometry->Size() << endl;
|
---|
1951 | }
|
---|
1952 | }
|
---|
1953 | else
|
---|
1954 | {
|
---|
1955 | pFront = geomFront.GetArea();
|
---|
1956 | pBack = geomBack.GetArea();
|
---|
1957 | }
|
---|
1958 |
|
---|
1959 |
|
---|
1960 | // -- pvs rendering heuristics
|
---|
1961 |
|
---|
1962 | // upper and lower bounds
|
---|
1963 | const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize();
|
---|
1964 | const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize();
|
---|
1965 |
|
---|
1966 | const float penaltyOld = EvalPvsPenalty((int)totalPvs, lowerPvsLimit, upperPvsLimit);
|
---|
1967 | const float penaltyFront = EvalPvsPenalty((int)pvsFront, lowerPvsLimit, upperPvsLimit);
|
---|
1968 | const float penaltyBack = EvalPvsPenalty((int)pvsBack, lowerPvsLimit, upperPvsLimit);
|
---|
1969 |
|
---|
1970 | const float oldRenderCost = pOverall * penaltyOld;
|
---|
1971 | const float newRenderCost = penaltyFront * pFront + penaltyBack * pBack;
|
---|
1972 |
|
---|
1973 | //Debug << "decrease: " << oldRenderCost - newRenderCost << endl;
|
---|
1974 | const float renderCostDecrease = (oldRenderCost - newRenderCost) / mBox.GetVolume();
|
---|
1975 |
|
---|
1976 | const float weight = mRenderCostDecreaseWeight;
|
---|
1977 | // take render cost of node into account to avoid being stuck in a local minimum
|
---|
1978 | const float normalizedOldRenderCost = oldRenderCost / mBox.GetVolume();
|
---|
1979 |
|
---|
1980 | //Debug << "rendercostdecr: " << weight * renderCostDecrease << " old render cost: " << (1.0f - weight) * normalizedOldRenderCost << endl;
|
---|
1981 | return weight * renderCostDecrease + (1.0f - weight) * normalizedOldRenderCost;
|
---|
1982 | }
|
---|
1983 |
|
---|
1984 |
|
---|
1985 | float VspBspTree::EvalSplitPlaneCost(const Plane3 &candidatePlane,
|
---|
1986 | const VspBspTraversalData &data,
|
---|
1987 | BspNodeGeometry &geomFront,
|
---|
1988 | BspNodeGeometry &geomBack,
|
---|
1989 | float &pFront,
|
---|
1990 | float &pBack) const
|
---|
1991 | {
|
---|
1992 | float totalPvs = 0;
|
---|
1993 | float pvsFront = 0;
|
---|
1994 | float pvsBack = 0;
|
---|
1995 |
|
---|
1996 | // overall probability is used as normalizer
|
---|
1997 | float pOverall = 0;
|
---|
1998 |
|
---|
1999 | // probability that view point lies in back / front node
|
---|
2000 | pFront = 0;
|
---|
2001 | pBack = 0;
|
---|
2002 |
|
---|
2003 | int numTests; // the number of tests
|
---|
2004 |
|
---|
2005 | // if random samples shold be taken instead of testing all the rays
|
---|
2006 | bool useRand;
|
---|
2007 |
|
---|
2008 | if ((int)data.mRays->size() > mMaxTests)
|
---|
2009 | {
|
---|
2010 | useRand = true;
|
---|
2011 | numTests = mMaxTests;
|
---|
2012 | }
|
---|
2013 | else
|
---|
2014 | {
|
---|
2015 | useRand = false;
|
---|
2016 | numTests = (int)data.mRays->size();
|
---|
2017 | }
|
---|
2018 |
|
---|
2019 | // create unique ids for pvs heuristics
|
---|
2020 | GenerateUniqueIdsForPvs();
|
---|
2021 |
|
---|
2022 | for (int i = 0; i < numTests; ++ i)
|
---|
2023 | {
|
---|
2024 | const int testIdx = useRand ?
|
---|
2025 | (int)RandomValue(0, (Real)((int)data.mRays->size() - 1)) : i;
|
---|
2026 | RayInfo rayInf = (*data.mRays)[testIdx];
|
---|
2027 |
|
---|
2028 | float t;
|
---|
2029 | VssRay *ray = rayInf.mRay;
|
---|
2030 | const int cf = rayInf.ComputeRayIntersection(candidatePlane, t);
|
---|
2031 |
|
---|
2032 | // find front and back pvs for origing and termination object
|
---|
2033 | AddObjToPvs(ray->mTerminationObject, cf, pvsFront, pvsBack, totalPvs);
|
---|
2034 |
|
---|
2035 | if (COUNT_ORIGIN_OBJECTS)
|
---|
2036 | AddObjToPvs(ray->mOriginObject, cf, pvsFront, pvsBack, totalPvs);
|
---|
2037 | }
|
---|
2038 |
|
---|
2039 | // construct child geometry with regard to the candidate split plane
|
---|
2040 | bool splitSuccessFull = data.mGeometry->SplitGeometry(geomFront,
|
---|
2041 | geomBack,
|
---|
2042 | candidatePlane,
|
---|
2043 | mBox,
|
---|
2044 | //0.0f);
|
---|
2045 | mEpsilon);
|
---|
2046 |
|
---|
2047 | pOverall = data.mProbability;
|
---|
2048 |
|
---|
2049 | if (!mUseAreaForPvs) // use front and back cell areas to approximate volume
|
---|
2050 | {
|
---|
2051 | pFront = geomFront.GetVolume();
|
---|
2052 | pBack = pOverall - pFront;
|
---|
2053 |
|
---|
2054 | // HACK: precision issues possible for unbalanced split => don't take this split!
|
---|
2055 | if (1 &&
|
---|
2056 | (!splitSuccessFull || (pFront <= 0) || (pBack <= 0) ||
|
---|
2057 | !geomFront.Valid() || !geomBack.Valid()))
|
---|
2058 | {
|
---|
2059 | //Debug << "error f: " << pFront << " b: " << pBack << endl;
|
---|
2060 |
|
---|
2061 | // high penalty for degenerated / wrong split
|
---|
2062 | return 99999.9f;
|
---|
2063 | }
|
---|
2064 | }
|
---|
2065 | else
|
---|
2066 | {
|
---|
2067 | pFront = geomFront.GetArea();
|
---|
2068 | pBack = geomBack.GetArea();
|
---|
2069 | }
|
---|
2070 |
|
---|
2071 |
|
---|
2072 | // -- pvs rendering heuristics
|
---|
2073 | const int lowerPvsLimit = mViewCellsManager->GetMinPvsSize();
|
---|
2074 | const int upperPvsLimit = mViewCellsManager->GetMaxPvsSize();
|
---|
2075 |
|
---|
2076 | // only render cost heuristics or combined with standard deviation
|
---|
2077 | const float penaltyOld = EvalPvsPenalty((int)totalPvs, lowerPvsLimit, upperPvsLimit);
|
---|
2078 | const float penaltyFront = EvalPvsPenalty((int)pvsFront, lowerPvsLimit, upperPvsLimit);
|
---|
2079 | const float penaltyBack = EvalPvsPenalty((int)pvsBack, lowerPvsLimit, upperPvsLimit);
|
---|
2080 |
|
---|
2081 | const float oldRenderCost = pOverall * penaltyOld;
|
---|
2082 | const float newRenderCost = penaltyFront * pFront + penaltyBack * pBack;
|
---|
2083 |
|
---|
2084 | float oldCost, newCost;
|
---|
2085 |
|
---|
2086 | // only render cost
|
---|
2087 | if (1)
|
---|
2088 | {
|
---|
2089 | oldCost = oldRenderCost;
|
---|
2090 | newCost = newRenderCost;
|
---|
2091 | }
|
---|
2092 | else // also considering standard deviation
|
---|
2093 | {
|
---|
2094 | // standard deviation is difference of back and front pvs
|
---|
2095 | const float expectedCost = 0.5f * (penaltyFront + penaltyBack);
|
---|
2096 |
|
---|
2097 | const float newDeviation = 0.5f *
|
---|
2098 | fabs(penaltyFront - expectedCost) + fabs(penaltyBack - expectedCost);
|
---|
2099 |
|
---|
2100 | const float oldDeviation = penaltyOld;
|
---|
2101 |
|
---|
2102 | newCost = mRenderCostWeight * newRenderCost + (1.0f - mRenderCostWeight) * newDeviation;
|
---|
2103 | oldCost = mRenderCostWeight * oldRenderCost + (1.0f - mRenderCostWeight) * oldDeviation;
|
---|
2104 | }
|
---|
2105 |
|
---|
2106 | const float cost = mPvsFactor * newCost / (oldCost + Limits::Small);
|
---|
2107 |
|
---|
2108 |
|
---|
2109 | #ifdef _DEBUG
|
---|
2110 | Debug << "totalpvs: " << data.mPvs << " ptotal: " << pOverall
|
---|
2111 | << " frontpvs: " << pvsFront << " pFront: " << pFront
|
---|
2112 | << " backpvs: " << pvsBack << " pBack: " << pBack << endl << endl;
|
---|
2113 | Debug << "cost: " << cost << endl;
|
---|
2114 | #endif
|
---|
2115 |
|
---|
2116 | return cost;
|
---|
2117 | }
|
---|
2118 |
|
---|
2119 |
|
---|
2120 | int VspBspTree::ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
2121 | ViewCellContainer &viewCells) const
|
---|
2122 | {
|
---|
2123 | stack<bspNodePair> nodeStack;
|
---|
2124 | BspNodeGeometry *rgeom = new BspNodeGeometry();
|
---|
2125 |
|
---|
2126 | ConstructGeometry(mRoot, *rgeom);
|
---|
2127 |
|
---|
2128 | nodeStack.push(bspNodePair(mRoot, rgeom));
|
---|
2129 |
|
---|
2130 | ViewCell::NewMail();
|
---|
2131 |
|
---|
2132 | while (!nodeStack.empty())
|
---|
2133 | {
|
---|
2134 | BspNode *node = nodeStack.top().first;
|
---|
2135 | BspNodeGeometry *geom = nodeStack.top().second;
|
---|
2136 | nodeStack.pop();
|
---|
2137 |
|
---|
2138 | const int side = geom->ComputeIntersection(box);
|
---|
2139 |
|
---|
2140 | switch (side)
|
---|
2141 | {
|
---|
2142 | case -1:
|
---|
2143 | // node geometry is contained in box
|
---|
2144 | CollectViewCells(node, true, viewCells, true);
|
---|
2145 | break;
|
---|
2146 |
|
---|
2147 | case 0:
|
---|
2148 | if (node->IsLeaf())
|
---|
2149 | {
|
---|
2150 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
2151 |
|
---|
2152 | if (!leaf->GetViewCell()->Mailed() && leaf->TreeValid())
|
---|
2153 | {
|
---|
2154 | leaf->GetViewCell()->Mail();
|
---|
2155 | viewCells.push_back(leaf->GetViewCell());
|
---|
2156 | }
|
---|
2157 | }
|
---|
2158 | else
|
---|
2159 | {
|
---|
2160 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2161 |
|
---|
2162 | BspNode *first = interior->GetFront();
|
---|
2163 | BspNode *second = interior->GetBack();
|
---|
2164 |
|
---|
2165 | BspNodeGeometry *firstGeom = new BspNodeGeometry();
|
---|
2166 | BspNodeGeometry *secondGeom = new BspNodeGeometry();
|
---|
2167 |
|
---|
2168 | geom->SplitGeometry(*firstGeom,
|
---|
2169 | *secondGeom,
|
---|
2170 | interior->GetPlane(),
|
---|
2171 | mBox,
|
---|
2172 | //0.0000001f);
|
---|
2173 | mEpsilon);
|
---|
2174 |
|
---|
2175 | nodeStack.push(bspNodePair(first, firstGeom));
|
---|
2176 | nodeStack.push(bspNodePair(second, secondGeom));
|
---|
2177 | }
|
---|
2178 |
|
---|
2179 | break;
|
---|
2180 | default:
|
---|
2181 | // default: cull
|
---|
2182 | break;
|
---|
2183 | }
|
---|
2184 |
|
---|
2185 | DEL_PTR(geom);
|
---|
2186 |
|
---|
2187 | }
|
---|
2188 |
|
---|
2189 | return (int)viewCells.size();
|
---|
2190 | }
|
---|
2191 |
|
---|
2192 |
|
---|
2193 | float VspBspTree::EvalAxisAlignedSplitCost(const VspBspTraversalData &data,
|
---|
2194 | const AxisAlignedBox3 &box,
|
---|
2195 | const int axis,
|
---|
2196 | const float &position,
|
---|
2197 | float &pFront,
|
---|
2198 | float &pBack) const
|
---|
2199 | {
|
---|
2200 | float pvsTotal = 0;
|
---|
2201 | float pvsFront = 0;
|
---|
2202 | float pvsBack = 0;
|
---|
2203 |
|
---|
2204 | // create unique ids for pvs heuristics
|
---|
2205 | GenerateUniqueIdsForPvs();
|
---|
2206 |
|
---|
2207 | const int pvsSize = data.mPvs;
|
---|
2208 |
|
---|
2209 | RayInfoContainer::const_iterator rit, rit_end = data.mRays->end();
|
---|
2210 |
|
---|
2211 | // this is the main ray classification loop!
|
---|
2212 | for(rit = data.mRays->begin(); rit != rit_end; ++ rit)
|
---|
2213 | {
|
---|
2214 | // determine the side of this ray with respect to the plane
|
---|
2215 | float t;
|
---|
2216 | const int side = (*rit).ComputeRayIntersection(axis, position, t);
|
---|
2217 |
|
---|
2218 | AddObjToPvs((*rit).mRay->mTerminationObject, side, pvsFront, pvsBack, pvsTotal);
|
---|
2219 |
|
---|
2220 | if (COUNT_ORIGIN_OBJECTS)
|
---|
2221 | AddObjToPvs((*rit).mRay->mOriginObject, side, pvsFront, pvsBack, pvsTotal);
|
---|
2222 | }
|
---|
2223 |
|
---|
2224 |
|
---|
2225 | //-- pvs heuristics
|
---|
2226 |
|
---|
2227 | float pOverall = data.mProbability;
|
---|
2228 |
|
---|
2229 | // note: we use a simplified computation assuming that we always do a
|
---|
2230 | // spatial mid split
|
---|
2231 |
|
---|
2232 | if (!mUseAreaForPvs)
|
---|
2233 | {
|
---|
2234 | // volume
|
---|
2235 | pBack = pFront = pOverall * 0.5f;
|
---|
2236 | #if 0
|
---|
2237 | // box length substitute for probability
|
---|
2238 | const float minBox = box.Min(axis);
|
---|
2239 | const float maxBox = box.Max(axis);
|
---|
2240 |
|
---|
2241 | pBack = position - minBox;
|
---|
2242 | pFront = maxBox - position;
|
---|
2243 | pOverall = maxBox - minBox;
|
---|
2244 | #endif
|
---|
2245 | }
|
---|
2246 | else //-- area substitute for probability
|
---|
2247 | {
|
---|
2248 | const int axis2 = (axis + 1) % 3;
|
---|
2249 | const int axis3 = (axis + 2) % 3;
|
---|
2250 |
|
---|
2251 | const float faceArea =
|
---|
2252 | (box.Max(axis2) - box.Min(axis2)) *
|
---|
2253 | (box.Max(axis3) - box.Min(axis3));
|
---|
2254 |
|
---|
2255 | pBack = pFront = pOverall * 0.5f + faceArea;
|
---|
2256 | }
|
---|
2257 |
|
---|
2258 | #ifdef _DEBUG
|
---|
2259 | Debug << axis << " " << pvsSize << " " << pvsBack << " " << pvsFront << endl;
|
---|
2260 | Debug << pFront << " " << pBack << " " << pOverall << endl;
|
---|
2261 | #endif
|
---|
2262 |
|
---|
2263 |
|
---|
2264 | const float newCost = pvsBack * pBack + pvsFront * pFront;
|
---|
2265 | const float oldCost = (float)pvsSize * pOverall + Limits::Small;
|
---|
2266 |
|
---|
2267 | return (mCtDivCi + newCost) / oldCost;
|
---|
2268 | }
|
---|
2269 |
|
---|
2270 |
|
---|
2271 | inline void VspBspTree::AddObjToPvs(Intersectable *obj,
|
---|
2272 | const int cf,
|
---|
2273 | float &frontPvs,
|
---|
2274 | float &backPvs,
|
---|
2275 | float &totalPvs) const
|
---|
2276 | {
|
---|
2277 | if (!obj)
|
---|
2278 | return;
|
---|
2279 | #if 0
|
---|
2280 | const float renderCost = mViewCellsManager->EvalRenderCost(obj);
|
---|
2281 | #else
|
---|
2282 | const int renderCost = 1;
|
---|
2283 | #endif
|
---|
2284 | // new object
|
---|
2285 | if ((obj->mMailbox != sFrontId) &&
|
---|
2286 | (obj->mMailbox != sBackId) &&
|
---|
2287 | (obj->mMailbox != sFrontAndBackId))
|
---|
2288 | {
|
---|
2289 | totalPvs += renderCost;
|
---|
2290 | }
|
---|
2291 |
|
---|
2292 | // TODO: does this really belong to no pvs?
|
---|
2293 | //if (cf == Ray::COINCIDENT) return;
|
---|
2294 |
|
---|
2295 | // object belongs to both PVS
|
---|
2296 | if (cf >= 0)
|
---|
2297 | {
|
---|
2298 | if ((obj->mMailbox != sFrontId) &&
|
---|
2299 | (obj->mMailbox != sFrontAndBackId))
|
---|
2300 | {
|
---|
2301 | frontPvs += renderCost;
|
---|
2302 |
|
---|
2303 | if (obj->mMailbox == sBackId)
|
---|
2304 | obj->mMailbox = sFrontAndBackId;
|
---|
2305 | else
|
---|
2306 | obj->mMailbox = sFrontId;
|
---|
2307 | }
|
---|
2308 | }
|
---|
2309 |
|
---|
2310 | if (cf <= 0)
|
---|
2311 | {
|
---|
2312 | if ((obj->mMailbox != sBackId) &&
|
---|
2313 | (obj->mMailbox != sFrontAndBackId))
|
---|
2314 | {
|
---|
2315 | backPvs += renderCost;
|
---|
2316 |
|
---|
2317 | if (obj->mMailbox == sFrontId)
|
---|
2318 | obj->mMailbox = sFrontAndBackId;
|
---|
2319 | else
|
---|
2320 | obj->mMailbox = sBackId;
|
---|
2321 | }
|
---|
2322 | }
|
---|
2323 | }
|
---|
2324 |
|
---|
2325 |
|
---|
2326 | void VspBspTree::CollectLeaves(vector<BspLeaf *> &leaves,
|
---|
2327 | const bool onlyUnmailed,
|
---|
2328 | const int maxPvsSize) const
|
---|
2329 | {
|
---|
2330 | stack<BspNode *> nodeStack;
|
---|
2331 | nodeStack.push(mRoot);
|
---|
2332 |
|
---|
2333 | while (!nodeStack.empty())
|
---|
2334 | {
|
---|
2335 | BspNode *node = nodeStack.top();
|
---|
2336 | nodeStack.pop();
|
---|
2337 |
|
---|
2338 | if (node->IsLeaf())
|
---|
2339 | {
|
---|
2340 | // test if this leaf is in valid view space
|
---|
2341 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
2342 | if (leaf->TreeValid() &&
|
---|
2343 | (!onlyUnmailed || !leaf->Mailed()) &&
|
---|
2344 | ((maxPvsSize < 0) || (leaf->GetViewCell()->GetPvs().GetSize() <= maxPvsSize)))
|
---|
2345 | {
|
---|
2346 | leaves.push_back(leaf);
|
---|
2347 | }
|
---|
2348 | }
|
---|
2349 | else
|
---|
2350 | {
|
---|
2351 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2352 |
|
---|
2353 | nodeStack.push(interior->GetBack());
|
---|
2354 | nodeStack.push(interior->GetFront());
|
---|
2355 | }
|
---|
2356 | }
|
---|
2357 | }
|
---|
2358 |
|
---|
2359 |
|
---|
2360 | AxisAlignedBox3 VspBspTree::GetBoundingBox() const
|
---|
2361 | {
|
---|
2362 | return mBox;
|
---|
2363 | }
|
---|
2364 |
|
---|
2365 |
|
---|
2366 | BspNode *VspBspTree::GetRoot() const
|
---|
2367 | {
|
---|
2368 | return mRoot;
|
---|
2369 | }
|
---|
2370 |
|
---|
2371 |
|
---|
2372 | void VspBspTree::EvaluateLeafStats(const VspBspTraversalData &data)
|
---|
2373 | {
|
---|
2374 | // the node became a leaf -> evaluate stats for leafs
|
---|
2375 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(data.mNode);
|
---|
2376 |
|
---|
2377 |
|
---|
2378 | if (data.mPvs > mBspStats.maxPvs)
|
---|
2379 | {
|
---|
2380 | mBspStats.maxPvs = data.mPvs;
|
---|
2381 | }
|
---|
2382 |
|
---|
2383 | mBspStats.pvs += data.mPvs;
|
---|
2384 |
|
---|
2385 | if (data.mDepth < mBspStats.minDepth)
|
---|
2386 | {
|
---|
2387 | mBspStats.minDepth = data.mDepth;
|
---|
2388 | }
|
---|
2389 |
|
---|
2390 | if (data.mDepth >= mTermMaxDepth)
|
---|
2391 | {
|
---|
2392 | ++ mBspStats.maxDepthNodes;
|
---|
2393 | //Debug << "new max depth: " << mBspStats.maxDepthNodes << endl;
|
---|
2394 | }
|
---|
2395 |
|
---|
2396 | // accumulate rays to compute rays / leaf
|
---|
2397 | mBspStats.accumRays += (int)data.mRays->size();
|
---|
2398 |
|
---|
2399 | if (data.mPvs < mTermMinPvs)
|
---|
2400 | ++ mBspStats.minPvsNodes;
|
---|
2401 |
|
---|
2402 | if ((int)data.mRays->size() < mTermMinRays)
|
---|
2403 | ++ mBspStats.minRaysNodes;
|
---|
2404 |
|
---|
2405 | if (data.GetAvgRayContribution() > mTermMaxRayContribution)
|
---|
2406 | ++ mBspStats.maxRayContribNodes;
|
---|
2407 |
|
---|
2408 | if (data.mProbability <= mTermMinProbability)
|
---|
2409 | ++ mBspStats.minProbabilityNodes;
|
---|
2410 |
|
---|
2411 | // accumulate depth to compute average depth
|
---|
2412 | mBspStats.accumDepth += data.mDepth;
|
---|
2413 |
|
---|
2414 | ++ mCreatedViewCells;
|
---|
2415 |
|
---|
2416 | #ifdef _DEBUG
|
---|
2417 | Debug << "BSP stats: "
|
---|
2418 | << "Depth: " << data.mDepth << " (max: " << mTermMaxDepth << "), "
|
---|
2419 | << "PVS: " << data.mPvs << " (min: " << mTermMinPvs << "), "
|
---|
2420 | << "#rays: " << (int)data.mRays->size() << " (max: " << mTermMinRays << "), "
|
---|
2421 | << "#pvs: " << leaf->GetViewCell()->GetPvs().GetSize() << "), "
|
---|
2422 | << "#avg ray contrib (pvs): " << (float)data.mPvs / (float)data.mRays->size() << endl;
|
---|
2423 | #endif
|
---|
2424 | }
|
---|
2425 |
|
---|
2426 |
|
---|
2427 | int VspBspTree::CastRay(Ray &ray)
|
---|
2428 | {
|
---|
2429 | int hits = 0;
|
---|
2430 |
|
---|
2431 | stack<BspRayTraversalData> tQueue;
|
---|
2432 |
|
---|
2433 | float maxt, mint;
|
---|
2434 |
|
---|
2435 | if (!mBox.GetRaySegment(ray, mint, maxt))
|
---|
2436 | return 0;
|
---|
2437 |
|
---|
2438 | Intersectable::NewMail();
|
---|
2439 | ViewCell::NewMail();
|
---|
2440 |
|
---|
2441 | Vector3 entp = ray.Extrap(mint);
|
---|
2442 | Vector3 extp = ray.Extrap(maxt);
|
---|
2443 |
|
---|
2444 | BspNode *node = mRoot;
|
---|
2445 | BspNode *farChild = NULL;
|
---|
2446 |
|
---|
2447 | while (1)
|
---|
2448 | {
|
---|
2449 | if (!node->IsLeaf())
|
---|
2450 | {
|
---|
2451 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
2452 |
|
---|
2453 | Plane3 splitPlane = in->GetPlane();
|
---|
2454 | const int entSide = splitPlane.Side(entp);
|
---|
2455 | const int extSide = splitPlane.Side(extp);
|
---|
2456 |
|
---|
2457 | if (entSide < 0)
|
---|
2458 | {
|
---|
2459 | node = in->GetBack();
|
---|
2460 |
|
---|
2461 | if(extSide <= 0) // plane does not split ray => no far child
|
---|
2462 | continue;
|
---|
2463 |
|
---|
2464 | farChild = in->GetFront(); // plane splits ray
|
---|
2465 |
|
---|
2466 | } else if (entSide > 0)
|
---|
2467 | {
|
---|
2468 | node = in->GetFront();
|
---|
2469 |
|
---|
2470 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
2471 | continue;
|
---|
2472 |
|
---|
2473 | farChild = in->GetBack(); // plane splits ray
|
---|
2474 | }
|
---|
2475 | else // ray and plane are coincident
|
---|
2476 | {
|
---|
2477 | // matt: WHAT TO DO IN THIS CASE ?
|
---|
2478 | //break;
|
---|
2479 | node = in->GetFront();
|
---|
2480 | continue;
|
---|
2481 | }
|
---|
2482 |
|
---|
2483 | // push data for far child
|
---|
2484 | tQueue.push(BspRayTraversalData(farChild, extp, maxt));
|
---|
2485 |
|
---|
2486 | // find intersection of ray segment with plane
|
---|
2487 | float t;
|
---|
2488 | extp = splitPlane.FindIntersection(ray.GetLoc(), extp, &t);
|
---|
2489 | maxt *= t;
|
---|
2490 | }
|
---|
2491 | else // reached leaf => intersection with view cell
|
---|
2492 | {
|
---|
2493 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
2494 |
|
---|
2495 | if (!leaf->GetViewCell()->Mailed())
|
---|
2496 | {
|
---|
2497 | //ray.bspIntersections.push_back(Ray::VspBspIntersection(maxt, leaf));
|
---|
2498 | leaf->GetViewCell()->Mail();
|
---|
2499 | ++ hits;
|
---|
2500 | }
|
---|
2501 |
|
---|
2502 | //-- fetch the next far child from the stack
|
---|
2503 | if (tQueue.empty())
|
---|
2504 | break;
|
---|
2505 |
|
---|
2506 | entp = extp;
|
---|
2507 | mint = maxt; // NOTE: need this?
|
---|
2508 |
|
---|
2509 | if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f)
|
---|
2510 | break;
|
---|
2511 |
|
---|
2512 | BspRayTraversalData &s = tQueue.top();
|
---|
2513 |
|
---|
2514 | node = s.mNode;
|
---|
2515 | extp = s.mExitPoint;
|
---|
2516 | maxt = s.mMaxT;
|
---|
2517 |
|
---|
2518 | tQueue.pop();
|
---|
2519 | }
|
---|
2520 | }
|
---|
2521 |
|
---|
2522 | return hits;
|
---|
2523 | }
|
---|
2524 |
|
---|
2525 |
|
---|
2526 | void VspBspTree::CollectViewCells(ViewCellContainer &viewCells,
|
---|
2527 | bool onlyValid) const
|
---|
2528 | {
|
---|
2529 | ViewCell::NewMail();
|
---|
2530 |
|
---|
2531 | CollectViewCells(mRoot, onlyValid, viewCells, true);
|
---|
2532 | }
|
---|
2533 |
|
---|
2534 |
|
---|
2535 | void VspBspTree::CollapseViewCells()
|
---|
2536 | {
|
---|
2537 | // TODO
|
---|
2538 | #if HAS_TO_BE_REDONE
|
---|
2539 | stack<BspNode *> nodeStack;
|
---|
2540 |
|
---|
2541 | if (!mRoot)
|
---|
2542 | return;
|
---|
2543 |
|
---|
2544 | nodeStack.push(mRoot);
|
---|
2545 |
|
---|
2546 | while (!nodeStack.empty())
|
---|
2547 | {
|
---|
2548 | BspNode *node = nodeStack.top();
|
---|
2549 | nodeStack.pop();
|
---|
2550 |
|
---|
2551 | if (node->IsLeaf())
|
---|
2552 | {
|
---|
2553 | BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
2554 |
|
---|
2555 | if (!viewCell->GetValid())
|
---|
2556 | {
|
---|
2557 | BspViewCell *viewCell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
2558 |
|
---|
2559 | ViewCellContainer leaves;
|
---|
2560 | mViewCellsTree->CollectLeaves(viewCell, leaves);
|
---|
2561 |
|
---|
2562 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
2563 |
|
---|
2564 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
2565 | {
|
---|
2566 | BspLeaf *l = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
2567 | l->SetViewCell(GetOrCreateOutOfBoundsCell());
|
---|
2568 | ++ mBspStats.invalidLeaves;
|
---|
2569 | }
|
---|
2570 |
|
---|
2571 | // add to unbounded view cell
|
---|
2572 | GetOrCreateOutOfBoundsCell()->GetPvs().AddPvs(viewCell->GetPvs());
|
---|
2573 | DEL_PTR(viewCell);
|
---|
2574 | }
|
---|
2575 | }
|
---|
2576 | else
|
---|
2577 | {
|
---|
2578 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2579 |
|
---|
2580 | nodeStack.push(interior->GetFront());
|
---|
2581 | nodeStack.push(interior->GetBack());
|
---|
2582 | }
|
---|
2583 | }
|
---|
2584 |
|
---|
2585 | Debug << "invalid leaves: " << mBspStats.invalidLeaves << endl;
|
---|
2586 | #endif
|
---|
2587 | }
|
---|
2588 |
|
---|
2589 |
|
---|
2590 | void VspBspTree::CollectRays(VssRayContainer &rays)
|
---|
2591 | {
|
---|
2592 | vector<BspLeaf *> leaves;
|
---|
2593 |
|
---|
2594 | vector<BspLeaf *>::const_iterator lit, lit_end = leaves.end();
|
---|
2595 |
|
---|
2596 | for (lit = leaves.begin(); lit != lit_end; ++ lit)
|
---|
2597 | {
|
---|
2598 | BspLeaf *leaf = *lit;
|
---|
2599 | VssRayContainer::const_iterator rit, rit_end = leaf->mVssRays.end();
|
---|
2600 |
|
---|
2601 | for (rit = leaf->mVssRays.begin(); rit != rit_end; ++ rit)
|
---|
2602 | rays.push_back(*rit);
|
---|
2603 | }
|
---|
2604 | }
|
---|
2605 |
|
---|
2606 |
|
---|
2607 | void VspBspTree::ValidateTree()
|
---|
2608 | {
|
---|
2609 | stack<BspNode *> nodeStack;
|
---|
2610 |
|
---|
2611 | if (!mRoot)
|
---|
2612 | return;
|
---|
2613 |
|
---|
2614 | nodeStack.push(mRoot);
|
---|
2615 |
|
---|
2616 | mBspStats.invalidLeaves = 0;
|
---|
2617 | while (!nodeStack.empty())
|
---|
2618 | {
|
---|
2619 | BspNode *node = nodeStack.top();
|
---|
2620 | nodeStack.pop();
|
---|
2621 |
|
---|
2622 | if (node->IsLeaf())
|
---|
2623 | {
|
---|
2624 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
2625 |
|
---|
2626 | if (!leaf->GetViewCell()->GetValid())
|
---|
2627 | ++ mBspStats.invalidLeaves;
|
---|
2628 |
|
---|
2629 | // validity flags don't match => repair
|
---|
2630 | if (leaf->GetViewCell()->GetValid() != leaf->TreeValid())
|
---|
2631 | {
|
---|
2632 | leaf->SetTreeValid(leaf->GetViewCell()->GetValid());
|
---|
2633 | PropagateUpValidity(leaf);
|
---|
2634 | }
|
---|
2635 | }
|
---|
2636 | else
|
---|
2637 | {
|
---|
2638 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2639 |
|
---|
2640 | nodeStack.push(interior->GetFront());
|
---|
2641 | nodeStack.push(interior->GetBack());
|
---|
2642 | }
|
---|
2643 | }
|
---|
2644 |
|
---|
2645 | Debug << "invalid leaves: " << mBspStats.invalidLeaves << endl;
|
---|
2646 | }
|
---|
2647 |
|
---|
2648 |
|
---|
2649 |
|
---|
2650 | void VspBspTree::CollectViewCells(BspNode *root,
|
---|
2651 | bool onlyValid,
|
---|
2652 | ViewCellContainer &viewCells,
|
---|
2653 | bool onlyUnmailed) const
|
---|
2654 | {
|
---|
2655 | stack<BspNode *> nodeStack;
|
---|
2656 |
|
---|
2657 | if (!root)
|
---|
2658 | return;
|
---|
2659 |
|
---|
2660 | nodeStack.push(root);
|
---|
2661 |
|
---|
2662 | while (!nodeStack.empty())
|
---|
2663 | {
|
---|
2664 | BspNode *node = nodeStack.top();
|
---|
2665 | nodeStack.pop();
|
---|
2666 |
|
---|
2667 | if (node->IsLeaf())
|
---|
2668 | {
|
---|
2669 | if (!onlyValid || node->TreeValid())
|
---|
2670 | {
|
---|
2671 | ViewCellLeaf *leafVc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
2672 |
|
---|
2673 | ViewCell *viewCell = mViewCellsTree->GetActiveViewCell(leafVc);
|
---|
2674 |
|
---|
2675 | if (!onlyUnmailed || !viewCell->Mailed())
|
---|
2676 | {
|
---|
2677 | viewCell->Mail();
|
---|
2678 | viewCells.push_back(viewCell);
|
---|
2679 | }
|
---|
2680 | }
|
---|
2681 | }
|
---|
2682 | else
|
---|
2683 | {
|
---|
2684 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
2685 |
|
---|
2686 | nodeStack.push(interior->GetFront());
|
---|
2687 | nodeStack.push(interior->GetBack());
|
---|
2688 | }
|
---|
2689 | }
|
---|
2690 |
|
---|
2691 | }
|
---|
2692 |
|
---|
2693 |
|
---|
2694 | void VspBspTree::PreprocessPolygons(PolygonContainer &polys)
|
---|
2695 | {
|
---|
2696 | // preprocess: throw out polygons coincident to the view space box (not needed)
|
---|
2697 | PolygonContainer boxPolys;
|
---|
2698 |
|
---|
2699 | mBox.ExtractPolys(boxPolys);
|
---|
2700 | vector<Plane3> boxPlanes;
|
---|
2701 |
|
---|
2702 | PolygonContainer::iterator pit, pit_end = boxPolys.end();
|
---|
2703 |
|
---|
2704 | // extract planes of box
|
---|
2705 | // TODO: can be done more elegantly than first extracting polygons
|
---|
2706 | // and take their planes
|
---|
2707 | for (pit = boxPolys.begin(); pit != pit_end; ++ pit)
|
---|
2708 | {
|
---|
2709 | boxPlanes.push_back((*pit)->GetSupportingPlane());
|
---|
2710 | }
|
---|
2711 |
|
---|
2712 | pit_end = polys.end();
|
---|
2713 |
|
---|
2714 | for (pit = polys.begin(); pit != pit_end; ++ pit)
|
---|
2715 | {
|
---|
2716 | vector<Plane3>::const_iterator bit, bit_end = boxPlanes.end();
|
---|
2717 |
|
---|
2718 | for (bit = boxPlanes.begin(); (bit != bit_end) && (*pit); ++ bit)
|
---|
2719 | {
|
---|
2720 | const int cf = (*pit)->ClassifyPlane(*bit, mEpsilon);
|
---|
2721 |
|
---|
2722 | if (cf == Polygon3::COINCIDENT)
|
---|
2723 | {
|
---|
2724 | DEL_PTR(*pit);
|
---|
2725 | //Debug << "coincident!!" << endl;
|
---|
2726 | }
|
---|
2727 | }
|
---|
2728 | }
|
---|
2729 |
|
---|
2730 | // remove deleted entries after swapping them to end of vector
|
---|
2731 | for (int i = 0; i < (int)polys.size(); ++ i)
|
---|
2732 | {
|
---|
2733 | while (!polys[i] && (i < (int)polys.size()))
|
---|
2734 | {
|
---|
2735 | swap(polys[i], polys.back());
|
---|
2736 | polys.pop_back();
|
---|
2737 | }
|
---|
2738 | }
|
---|
2739 | }
|
---|
2740 |
|
---|
2741 |
|
---|
2742 | float VspBspTree::AccumulatedRayLength(const RayInfoContainer &rays) const
|
---|
2743 | {
|
---|
2744 | float len = 0;
|
---|
2745 |
|
---|
2746 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
2747 |
|
---|
2748 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
2749 | len += (*it).SegmentLength();
|
---|
2750 |
|
---|
2751 | return len;
|
---|
2752 | }
|
---|
2753 |
|
---|
2754 |
|
---|
2755 | int VspBspTree::SplitRays(const Plane3 &plane,
|
---|
2756 | RayInfoContainer &rays,
|
---|
2757 | RayInfoContainer &frontRays,
|
---|
2758 | RayInfoContainer &backRays) const
|
---|
2759 | {
|
---|
2760 | int splits = 0;
|
---|
2761 |
|
---|
2762 | RayInfoContainer::const_iterator it, it_end = rays.end();
|
---|
2763 |
|
---|
2764 | for (it = rays.begin(); it != it_end; ++ it)
|
---|
2765 | {
|
---|
2766 | RayInfo bRay = *it;
|
---|
2767 |
|
---|
2768 | VssRay *ray = bRay.mRay;
|
---|
2769 | float t;
|
---|
2770 |
|
---|
2771 | // get classification and receive new t
|
---|
2772 | const int cf = bRay.ComputeRayIntersection(plane, t);
|
---|
2773 |
|
---|
2774 | switch (cf)
|
---|
2775 | {
|
---|
2776 | case -1:
|
---|
2777 | backRays.push_back(bRay);
|
---|
2778 | break;
|
---|
2779 | case 1:
|
---|
2780 | frontRays.push_back(bRay);
|
---|
2781 | break;
|
---|
2782 | case 0:
|
---|
2783 | {
|
---|
2784 | //-- split ray
|
---|
2785 | // test if start point behind or in front of plane
|
---|
2786 | const int side = plane.Side(bRay.ExtrapOrigin());
|
---|
2787 |
|
---|
2788 | ++ splits;
|
---|
2789 |
|
---|
2790 | if (side <= 0)
|
---|
2791 | {
|
---|
2792 | backRays.push_back(RayInfo(ray, bRay.GetMinT(), t));
|
---|
2793 | frontRays.push_back(RayInfo(ray, t, bRay.GetMaxT()));
|
---|
2794 | }
|
---|
2795 | else
|
---|
2796 | {
|
---|
2797 | frontRays.push_back(RayInfo(ray, bRay.GetMinT(), t));
|
---|
2798 | backRays.push_back(RayInfo(ray, t, bRay.GetMaxT()));
|
---|
2799 | }
|
---|
2800 | }
|
---|
2801 | break;
|
---|
2802 | default:
|
---|
2803 | Debug << "Should not come here" << endl;
|
---|
2804 | break;
|
---|
2805 | }
|
---|
2806 | }
|
---|
2807 |
|
---|
2808 | return splits;
|
---|
2809 | }
|
---|
2810 |
|
---|
2811 |
|
---|
2812 | void VspBspTree::ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const
|
---|
2813 | {
|
---|
2814 | BspNode *lastNode;
|
---|
2815 |
|
---|
2816 | do
|
---|
2817 | {
|
---|
2818 | lastNode = n;
|
---|
2819 |
|
---|
2820 | // want to get planes defining geometry of this node => don't take
|
---|
2821 | // split plane of node itself
|
---|
2822 | n = n->GetParent();
|
---|
2823 |
|
---|
2824 | if (n)
|
---|
2825 | {
|
---|
2826 | BspInterior *interior = dynamic_cast<BspInterior *>(n);
|
---|
2827 | Plane3 halfSpace = dynamic_cast<BspInterior *>(interior)->GetPlane();
|
---|
2828 |
|
---|
2829 | if (interior->GetBack() != lastNode)
|
---|
2830 | halfSpace.ReverseOrientation();
|
---|
2831 |
|
---|
2832 | halfSpaces.push_back(halfSpace);
|
---|
2833 | }
|
---|
2834 | }
|
---|
2835 | while (n);
|
---|
2836 | }
|
---|
2837 |
|
---|
2838 |
|
---|
2839 | void VspBspTree::ConstructGeometry(BspNode *n,
|
---|
2840 | BspNodeGeometry &geom) const
|
---|
2841 | {
|
---|
2842 | vector<Plane3> halfSpaces;
|
---|
2843 | ExtractHalfSpaces(n, halfSpaces);
|
---|
2844 |
|
---|
2845 | PolygonContainer candidatePolys;
|
---|
2846 | vector<Plane3> candidatePlanes;
|
---|
2847 |
|
---|
2848 | vector<Plane3>::const_iterator pit, pit_end = halfSpaces.end();
|
---|
2849 |
|
---|
2850 | // bounded planes are added to the polygons
|
---|
2851 | for (pit = halfSpaces.begin(); pit != pit_end; ++ pit)
|
---|
2852 | {
|
---|
2853 | Polygon3 *p = GetBoundingBox().CrossSection(*pit);
|
---|
2854 |
|
---|
2855 | if (p->Valid(mEpsilon))
|
---|
2856 | {
|
---|
2857 | candidatePolys.push_back(p);
|
---|
2858 | candidatePlanes.push_back(*pit);
|
---|
2859 | }
|
---|
2860 | }
|
---|
2861 |
|
---|
2862 | // add faces of bounding box (also could be faces of the cell)
|
---|
2863 | for (int i = 0; i < 6; ++ i)
|
---|
2864 | {
|
---|
2865 | VertexContainer vertices;
|
---|
2866 |
|
---|
2867 | for (int j = 0; j < 4; ++ j)
|
---|
2868 | vertices.push_back(mBox.GetFace(i).mVertices[j]);
|
---|
2869 |
|
---|
2870 | Polygon3 *poly = new Polygon3(vertices);
|
---|
2871 |
|
---|
2872 | candidatePolys.push_back(poly);
|
---|
2873 | candidatePlanes.push_back(poly->GetSupportingPlane());
|
---|
2874 | }
|
---|
2875 |
|
---|
2876 | for (int i = 0; i < (int)candidatePolys.size(); ++ i)
|
---|
2877 | {
|
---|
2878 | // polygon is split by all other planes
|
---|
2879 | for (int j = 0; (j < (int)halfSpaces.size()) && candidatePolys[i]; ++ j)
|
---|
2880 | {
|
---|
2881 | if (i == j) // polygon and plane are coincident
|
---|
2882 | continue;
|
---|
2883 |
|
---|
2884 | VertexContainer splitPts;
|
---|
2885 | Polygon3 *frontPoly, *backPoly;
|
---|
2886 |
|
---|
2887 | const int cf =
|
---|
2888 | candidatePolys[i]->ClassifyPlane(halfSpaces[j],
|
---|
2889 | mEpsilon);
|
---|
2890 |
|
---|
2891 | switch (cf)
|
---|
2892 | {
|
---|
2893 | case Polygon3::SPLIT:
|
---|
2894 | frontPoly = new Polygon3();
|
---|
2895 | backPoly = new Polygon3();
|
---|
2896 |
|
---|
2897 | candidatePolys[i]->Split(halfSpaces[j],
|
---|
2898 | *frontPoly,
|
---|
2899 | *backPoly,
|
---|
2900 | mEpsilon);
|
---|
2901 |
|
---|
2902 | DEL_PTR(candidatePolys[i]);
|
---|
2903 |
|
---|
2904 | if (backPoly->Valid(mEpsilon))
|
---|
2905 | candidatePolys[i] = backPoly;
|
---|
2906 | else
|
---|
2907 | DEL_PTR(backPoly);
|
---|
2908 |
|
---|
2909 | // outside, don't need this
|
---|
2910 | DEL_PTR(frontPoly);
|
---|
2911 | break;
|
---|
2912 | // polygon outside of halfspace
|
---|
2913 | case Polygon3::FRONT_SIDE:
|
---|
2914 | DEL_PTR(candidatePolys[i]);
|
---|
2915 | break;
|
---|
2916 | // just take polygon as it is
|
---|
2917 | case Polygon3::BACK_SIDE:
|
---|
2918 | case Polygon3::COINCIDENT:
|
---|
2919 | default:
|
---|
2920 | break;
|
---|
2921 | }
|
---|
2922 | }
|
---|
2923 |
|
---|
2924 | if (candidatePolys[i])
|
---|
2925 | {
|
---|
2926 | geom.Add(candidatePolys[i], candidatePlanes[i]);
|
---|
2927 | // geom.mPolys.push_back(candidates[i]);
|
---|
2928 | }
|
---|
2929 | }
|
---|
2930 | }
|
---|
2931 |
|
---|
2932 |
|
---|
2933 | void VspBspTree::ConstructGeometry(ViewCell *vc,
|
---|
2934 | BspNodeGeometry &vcGeom) const
|
---|
2935 | {
|
---|
2936 | ViewCellContainer leaves;
|
---|
2937 |
|
---|
2938 | mViewCellsTree->CollectLeaves(vc, leaves);
|
---|
2939 |
|
---|
2940 | ViewCellContainer::const_iterator it, it_end = leaves.end();
|
---|
2941 |
|
---|
2942 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
2943 | {
|
---|
2944 | BspLeaf *l = dynamic_cast<BspViewCell *>(*it)->mLeaf;
|
---|
2945 |
|
---|
2946 | ConstructGeometry(l, vcGeom);
|
---|
2947 | }
|
---|
2948 | }
|
---|
2949 |
|
---|
2950 |
|
---|
2951 | int VspBspTree::FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors,
|
---|
2952 | const bool onlyUnmailed) const
|
---|
2953 | {
|
---|
2954 | stack<bspNodePair> nodeStack;
|
---|
2955 |
|
---|
2956 | BspNodeGeometry nodeGeom;
|
---|
2957 | ConstructGeometry(n, nodeGeom);
|
---|
2958 | // const float eps = 0.5f;
|
---|
2959 | const float eps = 0.01f;
|
---|
2960 | // split planes from the root to this node
|
---|
2961 | // needed to verify that we found neighbor leaf
|
---|
2962 | // TODO: really needed?
|
---|
2963 | vector<Plane3> halfSpaces;
|
---|
2964 | ExtractHalfSpaces(n, halfSpaces);
|
---|
2965 |
|
---|
2966 |
|
---|
2967 | BspNodeGeometry *rgeom = new BspNodeGeometry();
|
---|
2968 | ConstructGeometry(mRoot, *rgeom);
|
---|
2969 |
|
---|
2970 | nodeStack.push(bspNodePair(mRoot, rgeom));
|
---|
2971 |
|
---|
2972 | while (!nodeStack.empty())
|
---|
2973 | {
|
---|
2974 | BspNode *node = nodeStack.top().first;
|
---|
2975 | BspNodeGeometry *geom = nodeStack.top().second;
|
---|
2976 |
|
---|
2977 | nodeStack.pop();
|
---|
2978 |
|
---|
2979 | if (node->IsLeaf())
|
---|
2980 | {
|
---|
2981 | // test if this leaf is in valid view space
|
---|
2982 | if (node->TreeValid() &&
|
---|
2983 | (node != n) &&
|
---|
2984 | (!onlyUnmailed || !node->Mailed()))
|
---|
2985 | {
|
---|
2986 | bool isAdjacent = true;
|
---|
2987 |
|
---|
2988 | if (1)
|
---|
2989 | {
|
---|
2990 | // test all planes of current node if still adjacent
|
---|
2991 | for (int i = 0; (i < halfSpaces.size()) && isAdjacent; ++ i)
|
---|
2992 | {
|
---|
2993 | const int cf =
|
---|
2994 | Polygon3::ClassifyPlane(geom->GetPolys(),
|
---|
2995 | halfSpaces[i],
|
---|
2996 | eps);
|
---|
2997 |
|
---|
2998 | if (cf == Polygon3::FRONT_SIDE)
|
---|
2999 | {
|
---|
3000 | isAdjacent = false;
|
---|
3001 | }
|
---|
3002 | }
|
---|
3003 | }
|
---|
3004 | else
|
---|
3005 | {
|
---|
3006 | // TODO: why is this wrong??
|
---|
3007 | // test all planes of current node if still adjacent
|
---|
3008 | for (int i = 0; (i < nodeGeom.Size()) && isAdjacent; ++ i)
|
---|
3009 | {
|
---|
3010 | Polygon3 *poly = nodeGeom.GetPolys()[i];
|
---|
3011 |
|
---|
3012 | const int cf =
|
---|
3013 | Polygon3::ClassifyPlane(geom->GetPolys(),
|
---|
3014 | poly->GetSupportingPlane(),
|
---|
3015 | eps);
|
---|
3016 |
|
---|
3017 | if (cf == Polygon3::FRONT_SIDE)
|
---|
3018 | {
|
---|
3019 | isAdjacent = false;
|
---|
3020 | }
|
---|
3021 | }
|
---|
3022 | }
|
---|
3023 | // neighbor was found
|
---|
3024 | if (isAdjacent)
|
---|
3025 | {
|
---|
3026 | neighbors.push_back(dynamic_cast<BspLeaf *>(node));
|
---|
3027 | }
|
---|
3028 | }
|
---|
3029 | }
|
---|
3030 | else
|
---|
3031 | {
|
---|
3032 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3033 |
|
---|
3034 | const int cf = Polygon3::ClassifyPlane(nodeGeom.GetPolys(),
|
---|
3035 | interior->GetPlane(),
|
---|
3036 | eps);
|
---|
3037 |
|
---|
3038 | BspNode *front = interior->GetFront();
|
---|
3039 | BspNode *back = interior->GetBack();
|
---|
3040 |
|
---|
3041 | BspNodeGeometry *fGeom = new BspNodeGeometry();
|
---|
3042 | BspNodeGeometry *bGeom = new BspNodeGeometry();
|
---|
3043 |
|
---|
3044 | geom->SplitGeometry(*fGeom,
|
---|
3045 | *bGeom,
|
---|
3046 | interior->GetPlane(),
|
---|
3047 | mBox,
|
---|
3048 | //0.0000001f);
|
---|
3049 | eps);
|
---|
3050 |
|
---|
3051 | if (cf == Polygon3::BACK_SIDE)
|
---|
3052 | {
|
---|
3053 | nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
|
---|
3054 | DEL_PTR(fGeom);
|
---|
3055 | }
|
---|
3056 | else
|
---|
3057 | {
|
---|
3058 | if (cf == Polygon3::FRONT_SIDE)
|
---|
3059 | {
|
---|
3060 | nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
|
---|
3061 | DEL_PTR(bGeom);
|
---|
3062 | }
|
---|
3063 | else
|
---|
3064 | { // random decision
|
---|
3065 | nodeStack.push(bspNodePair(front, fGeom));
|
---|
3066 | nodeStack.push(bspNodePair(back, bGeom));
|
---|
3067 | }
|
---|
3068 | }
|
---|
3069 | }
|
---|
3070 |
|
---|
3071 | DEL_PTR(geom);
|
---|
3072 | }
|
---|
3073 |
|
---|
3074 | return (int)neighbors.size();
|
---|
3075 | }
|
---|
3076 |
|
---|
3077 |
|
---|
3078 |
|
---|
3079 | int VspBspTree::FindApproximateNeighbors(BspNode *n,
|
---|
3080 | vector<BspLeaf *> &neighbors,
|
---|
3081 | const bool onlyUnmailed) const
|
---|
3082 | {
|
---|
3083 | stack<bspNodePair> nodeStack;
|
---|
3084 |
|
---|
3085 | BspNodeGeometry nodeGeom;
|
---|
3086 | ConstructGeometry(n, nodeGeom);
|
---|
3087 |
|
---|
3088 | float eps = 0.01f;
|
---|
3089 | // split planes from the root to this node
|
---|
3090 | // needed to verify that we found neighbor leaf
|
---|
3091 | // TODO: really needed?
|
---|
3092 | vector<Plane3> halfSpaces;
|
---|
3093 | ExtractHalfSpaces(n, halfSpaces);
|
---|
3094 |
|
---|
3095 |
|
---|
3096 | BspNodeGeometry *rgeom = new BspNodeGeometry();
|
---|
3097 | ConstructGeometry(mRoot, *rgeom);
|
---|
3098 |
|
---|
3099 | nodeStack.push(bspNodePair(mRoot, rgeom));
|
---|
3100 |
|
---|
3101 | while (!nodeStack.empty())
|
---|
3102 | {
|
---|
3103 | BspNode *node = nodeStack.top().first;
|
---|
3104 | BspNodeGeometry *geom = nodeStack.top().second;
|
---|
3105 |
|
---|
3106 | nodeStack.pop();
|
---|
3107 |
|
---|
3108 | if (node->IsLeaf())
|
---|
3109 | {
|
---|
3110 | // test if this leaf is in valid view space
|
---|
3111 | if (node->TreeValid() &&
|
---|
3112 | (node != n) &&
|
---|
3113 | (!onlyUnmailed || !node->Mailed()))
|
---|
3114 | {
|
---|
3115 | bool isAdjacent = true;
|
---|
3116 |
|
---|
3117 | // neighbor was found
|
---|
3118 | if (isAdjacent)
|
---|
3119 | {
|
---|
3120 | neighbors.push_back(dynamic_cast<BspLeaf *>(node));
|
---|
3121 | }
|
---|
3122 | }
|
---|
3123 | }
|
---|
3124 | else
|
---|
3125 | {
|
---|
3126 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3127 |
|
---|
3128 | const int cf = Polygon3::ClassifyPlane(nodeGeom.GetPolys(),
|
---|
3129 | interior->GetPlane(),
|
---|
3130 | eps);
|
---|
3131 |
|
---|
3132 | BspNode *front = interior->GetFront();
|
---|
3133 | BspNode *back = interior->GetBack();
|
---|
3134 |
|
---|
3135 | BspNodeGeometry *fGeom = new BspNodeGeometry();
|
---|
3136 | BspNodeGeometry *bGeom = new BspNodeGeometry();
|
---|
3137 |
|
---|
3138 | geom->SplitGeometry(*fGeom,
|
---|
3139 | *bGeom,
|
---|
3140 | interior->GetPlane(),
|
---|
3141 | mBox,
|
---|
3142 | //0.0000001f);
|
---|
3143 | eps);
|
---|
3144 |
|
---|
3145 | if (cf == Polygon3::BACK_SIDE)
|
---|
3146 | {
|
---|
3147 | nodeStack.push(bspNodePair(interior->GetBack(), bGeom));
|
---|
3148 | DEL_PTR(fGeom);
|
---|
3149 | }
|
---|
3150 | else
|
---|
3151 | {
|
---|
3152 | if (cf == Polygon3::FRONT_SIDE)
|
---|
3153 | {
|
---|
3154 | nodeStack.push(bspNodePair(interior->GetFront(), fGeom));
|
---|
3155 | DEL_PTR(bGeom);
|
---|
3156 | }
|
---|
3157 | else
|
---|
3158 | { // random decision
|
---|
3159 | nodeStack.push(bspNodePair(front, fGeom));
|
---|
3160 | nodeStack.push(bspNodePair(back, bGeom));
|
---|
3161 | }
|
---|
3162 | }
|
---|
3163 | }
|
---|
3164 |
|
---|
3165 | DEL_PTR(geom);
|
---|
3166 | }
|
---|
3167 |
|
---|
3168 | return (int)neighbors.size();
|
---|
3169 | }
|
---|
3170 |
|
---|
3171 |
|
---|
3172 |
|
---|
3173 | BspLeaf *VspBspTree::GetRandomLeaf(const Plane3 &halfspace)
|
---|
3174 | {
|
---|
3175 | stack<BspNode *> nodeStack;
|
---|
3176 | nodeStack.push(mRoot);
|
---|
3177 |
|
---|
3178 | int mask = rand();
|
---|
3179 |
|
---|
3180 | while (!nodeStack.empty())
|
---|
3181 | {
|
---|
3182 | BspNode *node = nodeStack.top();
|
---|
3183 | nodeStack.pop();
|
---|
3184 |
|
---|
3185 | if (node->IsLeaf())
|
---|
3186 | {
|
---|
3187 | return dynamic_cast<BspLeaf *>(node);
|
---|
3188 | }
|
---|
3189 | else
|
---|
3190 | {
|
---|
3191 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3192 | BspNode *next;
|
---|
3193 | BspNodeGeometry geom;
|
---|
3194 |
|
---|
3195 | // todo: not very efficient: constructs full cell everytime
|
---|
3196 | ConstructGeometry(interior, geom);
|
---|
3197 |
|
---|
3198 | const int cf =
|
---|
3199 | Polygon3::ClassifyPlane(geom.GetPolys(), halfspace, mEpsilon);
|
---|
3200 |
|
---|
3201 | if (cf == Polygon3::BACK_SIDE)
|
---|
3202 | next = interior->GetFront();
|
---|
3203 | else
|
---|
3204 | if (cf == Polygon3::FRONT_SIDE)
|
---|
3205 | next = interior->GetFront();
|
---|
3206 | else
|
---|
3207 | {
|
---|
3208 | // random decision
|
---|
3209 | if (mask & 1)
|
---|
3210 | next = interior->GetBack();
|
---|
3211 | else
|
---|
3212 | next = interior->GetFront();
|
---|
3213 | mask = mask >> 1;
|
---|
3214 | }
|
---|
3215 |
|
---|
3216 | nodeStack.push(next);
|
---|
3217 | }
|
---|
3218 | }
|
---|
3219 |
|
---|
3220 | return NULL;
|
---|
3221 | }
|
---|
3222 |
|
---|
3223 |
|
---|
3224 | BspLeaf *VspBspTree::GetRandomLeaf(const bool onlyUnmailed)
|
---|
3225 | {
|
---|
3226 | stack<BspNode *> nodeStack;
|
---|
3227 |
|
---|
3228 | nodeStack.push(mRoot);
|
---|
3229 |
|
---|
3230 | int mask = rand();
|
---|
3231 |
|
---|
3232 | while (!nodeStack.empty())
|
---|
3233 | {
|
---|
3234 | BspNode *node = nodeStack.top();
|
---|
3235 | nodeStack.pop();
|
---|
3236 |
|
---|
3237 | if (node->IsLeaf())
|
---|
3238 | {
|
---|
3239 | if ( (!onlyUnmailed || !node->Mailed()) )
|
---|
3240 | return dynamic_cast<BspLeaf *>(node);
|
---|
3241 | }
|
---|
3242 | else
|
---|
3243 | {
|
---|
3244 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3245 |
|
---|
3246 | // random decision
|
---|
3247 | if (mask & 1)
|
---|
3248 | nodeStack.push(interior->GetBack());
|
---|
3249 | else
|
---|
3250 | nodeStack.push(interior->GetFront());
|
---|
3251 |
|
---|
3252 | mask = mask >> 1;
|
---|
3253 | }
|
---|
3254 | }
|
---|
3255 |
|
---|
3256 | return NULL;
|
---|
3257 | }
|
---|
3258 |
|
---|
3259 |
|
---|
3260 | int VspBspTree::ComputePvsSize(const RayInfoContainer &rays) const
|
---|
3261 | {
|
---|
3262 | int pvsSize = 0;
|
---|
3263 |
|
---|
3264 | RayInfoContainer::const_iterator rit, rit_end = rays.end();
|
---|
3265 |
|
---|
3266 | Intersectable::NewMail();
|
---|
3267 |
|
---|
3268 | for (rit = rays.begin(); rit != rays.end(); ++ rit)
|
---|
3269 | {
|
---|
3270 | VssRay *ray = (*rit).mRay;
|
---|
3271 |
|
---|
3272 | if (COUNT_ORIGIN_OBJECTS && ray->mOriginObject)
|
---|
3273 | {
|
---|
3274 | if (!ray->mOriginObject->Mailed())
|
---|
3275 | {
|
---|
3276 | ray->mOriginObject->Mail();
|
---|
3277 | ++ pvsSize;
|
---|
3278 | }
|
---|
3279 | }
|
---|
3280 |
|
---|
3281 | if (ray->mTerminationObject)
|
---|
3282 | {
|
---|
3283 | if (!ray->mTerminationObject->Mailed())
|
---|
3284 | {
|
---|
3285 | ray->mTerminationObject->Mail();
|
---|
3286 | ++ pvsSize;
|
---|
3287 | }
|
---|
3288 | }
|
---|
3289 | }
|
---|
3290 |
|
---|
3291 | return pvsSize;
|
---|
3292 | }
|
---|
3293 |
|
---|
3294 |
|
---|
3295 | float VspBspTree::GetEpsilon() const
|
---|
3296 | {
|
---|
3297 | return mEpsilon;
|
---|
3298 | }
|
---|
3299 |
|
---|
3300 |
|
---|
3301 | int VspBspTree::SplitPolygons(const Plane3 &plane,
|
---|
3302 | PolygonContainer &polys,
|
---|
3303 | PolygonContainer &frontPolys,
|
---|
3304 | PolygonContainer &backPolys,
|
---|
3305 | PolygonContainer &coincident) const
|
---|
3306 | {
|
---|
3307 | int splits = 0;
|
---|
3308 |
|
---|
3309 | PolygonContainer::const_iterator it, it_end = polys.end();
|
---|
3310 |
|
---|
3311 | for (it = polys.begin(); it != polys.end(); ++ it)
|
---|
3312 | {
|
---|
3313 | Polygon3 *poly = *it;
|
---|
3314 |
|
---|
3315 | // classify polygon
|
---|
3316 | const int cf = poly->ClassifyPlane(plane, mEpsilon);
|
---|
3317 |
|
---|
3318 | switch (cf)
|
---|
3319 | {
|
---|
3320 | case Polygon3::COINCIDENT:
|
---|
3321 | coincident.push_back(poly);
|
---|
3322 | break;
|
---|
3323 | case Polygon3::FRONT_SIDE:
|
---|
3324 | frontPolys.push_back(poly);
|
---|
3325 | break;
|
---|
3326 | case Polygon3::BACK_SIDE:
|
---|
3327 | backPolys.push_back(poly);
|
---|
3328 | break;
|
---|
3329 | case Polygon3::SPLIT:
|
---|
3330 | backPolys.push_back(poly);
|
---|
3331 | frontPolys.push_back(poly);
|
---|
3332 | ++ splits;
|
---|
3333 | break;
|
---|
3334 | default:
|
---|
3335 | Debug << "SHOULD NEVER COME HERE\n";
|
---|
3336 | break;
|
---|
3337 | }
|
---|
3338 | }
|
---|
3339 |
|
---|
3340 | return splits;
|
---|
3341 | }
|
---|
3342 |
|
---|
3343 |
|
---|
3344 | int VspBspTree::CastLineSegment(const Vector3 &origin,
|
---|
3345 | const Vector3 &termination,
|
---|
3346 | ViewCellContainer &viewcells)
|
---|
3347 | {
|
---|
3348 | int hits = 0;
|
---|
3349 | stack<BspRayTraversalData> tStack;
|
---|
3350 |
|
---|
3351 | float mint = 0.0f, maxt = 1.0f;
|
---|
3352 |
|
---|
3353 | Intersectable::NewMail();
|
---|
3354 | ViewCell::NewMail();
|
---|
3355 |
|
---|
3356 | Vector3 entp = origin;
|
---|
3357 | Vector3 extp = termination;
|
---|
3358 |
|
---|
3359 | BspNode *node = mRoot;
|
---|
3360 | BspNode *farChild = NULL;
|
---|
3361 |
|
---|
3362 | float t;
|
---|
3363 | const float thresh = 1e-6f; // matt: change this to adjustable value
|
---|
3364 |
|
---|
3365 | while (1)
|
---|
3366 | {
|
---|
3367 | if (!node->IsLeaf())
|
---|
3368 | {
|
---|
3369 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
3370 |
|
---|
3371 | Plane3 splitPlane = in->GetPlane();
|
---|
3372 |
|
---|
3373 | const int entSide = splitPlane.Side(entp, thresh);
|
---|
3374 | const int extSide = splitPlane.Side(extp, thresh);
|
---|
3375 |
|
---|
3376 | if (entSide < 0)
|
---|
3377 | {
|
---|
3378 | node = in->GetBack();
|
---|
3379 |
|
---|
3380 | // plane does not split ray => no far child
|
---|
3381 | if (extSide <= 0)
|
---|
3382 | continue;
|
---|
3383 |
|
---|
3384 | farChild = in->GetFront(); // plane splits ray
|
---|
3385 | }
|
---|
3386 | else if (entSide > 0)
|
---|
3387 | {
|
---|
3388 | node = in->GetFront();
|
---|
3389 |
|
---|
3390 | if (extSide >= 0) // plane does not split ray => no far child
|
---|
3391 | continue;
|
---|
3392 |
|
---|
3393 | farChild = in->GetBack(); // plane splits ray
|
---|
3394 | }
|
---|
3395 | else // one of the ray end points is on the plane
|
---|
3396 | {
|
---|
3397 | // NOTE: what to do if ray is coincident with plane?
|
---|
3398 | if (extSide < 0)
|
---|
3399 | node = in->GetBack();
|
---|
3400 | else //if (extSide > 0)
|
---|
3401 | node = in->GetFront();
|
---|
3402 | //else break; // coincident => count no intersections
|
---|
3403 |
|
---|
3404 | continue; // no far child
|
---|
3405 | }
|
---|
3406 |
|
---|
3407 | // push data for far child
|
---|
3408 | tStack.push(BspRayTraversalData(farChild, extp));
|
---|
3409 |
|
---|
3410 | // find intersection of ray segment with plane
|
---|
3411 | extp = splitPlane.FindIntersection(origin, extp, &t);
|
---|
3412 | }
|
---|
3413 | else
|
---|
3414 | {
|
---|
3415 | // reached leaf => intersection with view cell
|
---|
3416 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
3417 | ViewCell *viewCell;
|
---|
3418 |
|
---|
3419 | // question: always contribute to leaf or to currently active view cell?
|
---|
3420 | if (0)
|
---|
3421 | viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell());
|
---|
3422 | else
|
---|
3423 | viewCell = leaf->GetViewCell();
|
---|
3424 |
|
---|
3425 | if (!viewCell->Mailed())
|
---|
3426 | {
|
---|
3427 | viewcells.push_back(viewCell);
|
---|
3428 | viewCell->Mail();
|
---|
3429 | ++ hits;
|
---|
3430 | }
|
---|
3431 |
|
---|
3432 | //-- fetch the next far child from the stack
|
---|
3433 | if (tStack.empty())
|
---|
3434 | break;
|
---|
3435 |
|
---|
3436 | entp = extp;
|
---|
3437 |
|
---|
3438 | const BspRayTraversalData &s = tStack.top();
|
---|
3439 |
|
---|
3440 | node = s.mNode;
|
---|
3441 | extp = s.mExitPoint;
|
---|
3442 |
|
---|
3443 | tStack.pop();
|
---|
3444 | }
|
---|
3445 | }
|
---|
3446 |
|
---|
3447 | return hits;
|
---|
3448 | }
|
---|
3449 |
|
---|
3450 |
|
---|
3451 |
|
---|
3452 |
|
---|
3453 | int VspBspTree::TreeDistance(BspNode *n1, BspNode *n2) const
|
---|
3454 | {
|
---|
3455 | std::deque<BspNode *> path1;
|
---|
3456 | BspNode *p1 = n1;
|
---|
3457 |
|
---|
3458 | // create path from node 1 to root
|
---|
3459 | while (p1)
|
---|
3460 | {
|
---|
3461 | if (p1 == n2) // second node on path
|
---|
3462 | return (int)path1.size();
|
---|
3463 |
|
---|
3464 | path1.push_front(p1);
|
---|
3465 | p1 = p1->GetParent();
|
---|
3466 | }
|
---|
3467 |
|
---|
3468 | int depth = n2->GetDepth();
|
---|
3469 | int d = depth;
|
---|
3470 |
|
---|
3471 | BspNode *p2 = n2;
|
---|
3472 |
|
---|
3473 | // compare with same depth
|
---|
3474 | while (1)
|
---|
3475 | {
|
---|
3476 | if ((d < (int)path1.size()) && (p2 == path1[d]))
|
---|
3477 | return (depth - d) + ((int)path1.size() - 1 - d);
|
---|
3478 |
|
---|
3479 | -- d;
|
---|
3480 | p2 = p2->GetParent();
|
---|
3481 | }
|
---|
3482 |
|
---|
3483 | return 0; // never come here
|
---|
3484 | }
|
---|
3485 |
|
---|
3486 |
|
---|
3487 | BspNode *VspBspTree::CollapseTree(BspNode *node, int &collapsed)
|
---|
3488 | {
|
---|
3489 | // TODO
|
---|
3490 | #if HAS_TO_BE_REDONE
|
---|
3491 | if (node->IsLeaf())
|
---|
3492 | return node;
|
---|
3493 |
|
---|
3494 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3495 |
|
---|
3496 | BspNode *front = CollapseTree(interior->GetFront(), collapsed);
|
---|
3497 | BspNode *back = CollapseTree(interior->GetBack(), collapsed);
|
---|
3498 |
|
---|
3499 | if (front->IsLeaf() && back->IsLeaf())
|
---|
3500 | {
|
---|
3501 | BspLeaf *frontLeaf = dynamic_cast<BspLeaf *>(front);
|
---|
3502 | BspLeaf *backLeaf = dynamic_cast<BspLeaf *>(back);
|
---|
3503 |
|
---|
3504 | //-- collapse tree
|
---|
3505 | if (frontLeaf->GetViewCell() == backLeaf->GetViewCell())
|
---|
3506 | {
|
---|
3507 | BspViewCell *vc = frontLeaf->GetViewCell();
|
---|
3508 |
|
---|
3509 | BspLeaf *leaf = new BspLeaf(interior->GetParent(), vc);
|
---|
3510 | leaf->SetTreeValid(frontLeaf->TreeValid());
|
---|
3511 |
|
---|
3512 | // replace a link from node's parent
|
---|
3513 | if (leaf->GetParent())
|
---|
3514 | leaf->GetParent()->ReplaceChildLink(node, leaf);
|
---|
3515 | else
|
---|
3516 | mRoot = leaf;
|
---|
3517 |
|
---|
3518 | ++ collapsed;
|
---|
3519 | delete interior;
|
---|
3520 |
|
---|
3521 | return leaf;
|
---|
3522 | }
|
---|
3523 | }
|
---|
3524 | #endif
|
---|
3525 | return node;
|
---|
3526 | }
|
---|
3527 |
|
---|
3528 |
|
---|
3529 | int VspBspTree::CollapseTree()
|
---|
3530 | {
|
---|
3531 | int collapsed = 0;
|
---|
3532 | //TODO
|
---|
3533 | #if HAS_TO_BE_REDONE
|
---|
3534 | (void)CollapseTree(mRoot, collapsed);
|
---|
3535 |
|
---|
3536 | // revalidate leaves
|
---|
3537 | RepairViewCellsLeafLists();
|
---|
3538 | #endif
|
---|
3539 | return collapsed;
|
---|
3540 | }
|
---|
3541 |
|
---|
3542 |
|
---|
3543 | void VspBspTree::RepairViewCellsLeafLists()
|
---|
3544 | {
|
---|
3545 | // TODO
|
---|
3546 | #if HAS_TO_BE_REDONE
|
---|
3547 | // list not valid anymore => clear
|
---|
3548 | stack<BspNode *> nodeStack;
|
---|
3549 | nodeStack.push(mRoot);
|
---|
3550 |
|
---|
3551 | ViewCell::NewMail();
|
---|
3552 |
|
---|
3553 | while (!nodeStack.empty())
|
---|
3554 | {
|
---|
3555 | BspNode *node = nodeStack.top();
|
---|
3556 | nodeStack.pop();
|
---|
3557 |
|
---|
3558 | if (node->IsLeaf())
|
---|
3559 | {
|
---|
3560 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
3561 |
|
---|
3562 | BspViewCell *viewCell = leaf->GetViewCell();
|
---|
3563 |
|
---|
3564 | if (!viewCell->Mailed())
|
---|
3565 | {
|
---|
3566 | viewCell->mLeaves.clear();
|
---|
3567 | viewCell->Mail();
|
---|
3568 | }
|
---|
3569 |
|
---|
3570 | viewCell->mLeaves.push_back(leaf);
|
---|
3571 |
|
---|
3572 | }
|
---|
3573 | else
|
---|
3574 | {
|
---|
3575 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3576 |
|
---|
3577 | nodeStack.push(interior->GetFront());
|
---|
3578 | nodeStack.push(interior->GetBack());
|
---|
3579 | }
|
---|
3580 | }
|
---|
3581 | // TODO
|
---|
3582 | #endif
|
---|
3583 | }
|
---|
3584 |
|
---|
3585 |
|
---|
3586 | int VspBspTree::CastBeam(Beam &beam)
|
---|
3587 | {
|
---|
3588 | stack<bspNodePair> nodeStack;
|
---|
3589 | BspNodeGeometry *rgeom = new BspNodeGeometry();
|
---|
3590 | ConstructGeometry(mRoot, *rgeom);
|
---|
3591 |
|
---|
3592 | nodeStack.push(bspNodePair(mRoot, rgeom));
|
---|
3593 |
|
---|
3594 | ViewCell::NewMail();
|
---|
3595 |
|
---|
3596 | while (!nodeStack.empty())
|
---|
3597 | {
|
---|
3598 | BspNode *node = nodeStack.top().first;
|
---|
3599 | BspNodeGeometry *geom = nodeStack.top().second;
|
---|
3600 | nodeStack.pop();
|
---|
3601 |
|
---|
3602 | AxisAlignedBox3 box;
|
---|
3603 | geom->GetBoundingBox(box);
|
---|
3604 |
|
---|
3605 | const int side = beam.ComputeIntersection(box);
|
---|
3606 |
|
---|
3607 | switch (side)
|
---|
3608 | {
|
---|
3609 | case -1:
|
---|
3610 | CollectViewCells(node, true, beam.mViewCells, true);
|
---|
3611 | break;
|
---|
3612 | case 0:
|
---|
3613 |
|
---|
3614 | if (node->IsLeaf())
|
---|
3615 | {
|
---|
3616 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
3617 |
|
---|
3618 | if (!leaf->GetViewCell()->Mailed() && leaf->TreeValid())
|
---|
3619 | {
|
---|
3620 | leaf->GetViewCell()->Mail();
|
---|
3621 | beam.mViewCells.push_back(leaf->GetViewCell());
|
---|
3622 | }
|
---|
3623 | }
|
---|
3624 | else
|
---|
3625 | {
|
---|
3626 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3627 |
|
---|
3628 | BspNode *first = interior->GetFront();
|
---|
3629 | BspNode *second = interior->GetBack();
|
---|
3630 |
|
---|
3631 | BspNodeGeometry *firstGeom = new BspNodeGeometry();
|
---|
3632 | BspNodeGeometry *secondGeom = new BspNodeGeometry();
|
---|
3633 |
|
---|
3634 | geom->SplitGeometry(*firstGeom,
|
---|
3635 | *secondGeom,
|
---|
3636 | interior->GetPlane(),
|
---|
3637 | mBox,
|
---|
3638 | //0.0000001f);
|
---|
3639 | mEpsilon);
|
---|
3640 |
|
---|
3641 | // decide on the order of the nodes
|
---|
3642 | if (DotProd(beam.mPlanes[0].mNormal,
|
---|
3643 | interior->GetPlane().mNormal) > 0)
|
---|
3644 | {
|
---|
3645 | swap(first, second);
|
---|
3646 | swap(firstGeom, secondGeom);
|
---|
3647 | }
|
---|
3648 |
|
---|
3649 | nodeStack.push(bspNodePair(first, firstGeom));
|
---|
3650 | nodeStack.push(bspNodePair(second, secondGeom));
|
---|
3651 | }
|
---|
3652 |
|
---|
3653 | break;
|
---|
3654 | default:
|
---|
3655 | // default: cull
|
---|
3656 | break;
|
---|
3657 | }
|
---|
3658 |
|
---|
3659 | DEL_PTR(geom);
|
---|
3660 |
|
---|
3661 | }
|
---|
3662 |
|
---|
3663 | return (int)beam.mViewCells.size();
|
---|
3664 | }
|
---|
3665 |
|
---|
3666 |
|
---|
3667 | void VspBspTree::SetViewCellsManager(ViewCellsManager *vcm)
|
---|
3668 | {
|
---|
3669 | mViewCellsManager = vcm;
|
---|
3670 | }
|
---|
3671 |
|
---|
3672 |
|
---|
3673 | int VspBspTree::CollectMergeCandidates(const vector<BspLeaf *> leaves,
|
---|
3674 | vector<MergeCandidate> &candidates)
|
---|
3675 | {
|
---|
3676 | BspLeaf::NewMail();
|
---|
3677 |
|
---|
3678 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
3679 |
|
---|
3680 | int numCandidates = 0;
|
---|
3681 |
|
---|
3682 | // find merge candidates and push them into queue
|
---|
3683 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
3684 | {
|
---|
3685 | BspLeaf *leaf = *it;
|
---|
3686 |
|
---|
3687 | // the same leaves must not be part of two merge candidates
|
---|
3688 | leaf->Mail();
|
---|
3689 |
|
---|
3690 | vector<BspLeaf *> neighbors;
|
---|
3691 |
|
---|
3692 | // appoximate neighbor search has slightl relaxed constraints
|
---|
3693 | if (1)
|
---|
3694 | FindNeighbors(leaf, neighbors, true);
|
---|
3695 | else
|
---|
3696 | FindApproximateNeighbors(leaf, neighbors, true);
|
---|
3697 |
|
---|
3698 | vector<BspLeaf *>::const_iterator nit, nit_end = neighbors.end();
|
---|
3699 |
|
---|
3700 | // TODO: test if at least one ray goes from one leaf to the other
|
---|
3701 | for (nit = neighbors.begin(); nit != nit_end; ++ nit)
|
---|
3702 | {
|
---|
3703 | if ((*nit)->GetViewCell() != leaf->GetViewCell())
|
---|
3704 | {
|
---|
3705 | MergeCandidate mc(leaf->GetViewCell(), (*nit)->GetViewCell());
|
---|
3706 |
|
---|
3707 | if (!leaf->GetViewCell()->GetPvs().Empty() ||
|
---|
3708 | !(*nit)->GetViewCell()->GetPvs().Empty() ||
|
---|
3709 | leaf->IsSibling(*nit))
|
---|
3710 | {
|
---|
3711 | candidates.push_back(mc);
|
---|
3712 | }
|
---|
3713 |
|
---|
3714 | ++ numCandidates;
|
---|
3715 | if ((numCandidates % 1000) == 0)
|
---|
3716 | {
|
---|
3717 | cout << "collected " << numCandidates << " merge candidates" << endl;
|
---|
3718 | }
|
---|
3719 | }
|
---|
3720 | }
|
---|
3721 | }
|
---|
3722 |
|
---|
3723 | Debug << "merge queue: " << (int)candidates.size() << endl;
|
---|
3724 | Debug << "leaves in queue: " << numCandidates << endl;
|
---|
3725 |
|
---|
3726 |
|
---|
3727 | return (int)leaves.size();
|
---|
3728 | }
|
---|
3729 |
|
---|
3730 |
|
---|
3731 | int VspBspTree::CollectMergeCandidates(const VssRayContainer &rays,
|
---|
3732 | vector<MergeCandidate> &candidates)
|
---|
3733 | {
|
---|
3734 | ViewCell::NewMail();
|
---|
3735 | long startTime = GetTime();
|
---|
3736 |
|
---|
3737 | map<BspLeaf *, vector<BspLeaf*> > neighborMap;
|
---|
3738 | ViewCellContainer::const_iterator iit;
|
---|
3739 |
|
---|
3740 | int numLeaves = 0;
|
---|
3741 |
|
---|
3742 | BspLeaf::NewMail();
|
---|
3743 |
|
---|
3744 | for (int i = 0; i < (int)rays.size(); ++ i)
|
---|
3745 | {
|
---|
3746 | VssRay *ray = rays[i];
|
---|
3747 |
|
---|
3748 | // traverse leaves stored in the rays and compare and
|
---|
3749 | // merge consecutive leaves (i.e., the neighbors in the tree)
|
---|
3750 | if (ray->mViewCells.size() < 2)
|
---|
3751 | continue;
|
---|
3752 | //TODO viewcellhierarchy
|
---|
3753 | iit = ray->mViewCells.begin();
|
---|
3754 | BspViewCell *bspVc = dynamic_cast<BspViewCell *>(*(iit ++));
|
---|
3755 | BspLeaf *leaf = bspVc->mLeaf;
|
---|
3756 |
|
---|
3757 | // traverse intersections
|
---|
3758 | // consecutive leaves are neighbors => add them to queue
|
---|
3759 | for (; iit != ray->mViewCells.end(); ++ iit)
|
---|
3760 | {
|
---|
3761 | // next pair
|
---|
3762 | BspLeaf *prevLeaf = leaf;
|
---|
3763 | bspVc = dynamic_cast<BspViewCell *>(*iit);
|
---|
3764 | leaf = bspVc->mLeaf;
|
---|
3765 |
|
---|
3766 | // view space not valid or same view cell
|
---|
3767 | if (!leaf->TreeValid() || !prevLeaf->TreeValid() ||
|
---|
3768 | (leaf->GetViewCell() == prevLeaf->GetViewCell()))
|
---|
3769 | continue;
|
---|
3770 |
|
---|
3771 | vector<BspLeaf *> &neighbors = neighborMap[leaf];
|
---|
3772 |
|
---|
3773 | bool found = false;
|
---|
3774 |
|
---|
3775 | // both leaves inserted in queue already =>
|
---|
3776 | // look if double pair already exists
|
---|
3777 | if (leaf->Mailed() && prevLeaf->Mailed())
|
---|
3778 | {
|
---|
3779 | vector<BspLeaf *>::const_iterator it, it_end = neighbors.end();
|
---|
3780 |
|
---|
3781 | for (it = neighbors.begin(); !found && (it != it_end); ++ it)
|
---|
3782 | if (*it == prevLeaf)
|
---|
3783 | found = true; // already in queue
|
---|
3784 | }
|
---|
3785 |
|
---|
3786 | if (!found)
|
---|
3787 | {
|
---|
3788 | // this pair is not in map yet
|
---|
3789 | // => insert into the neighbor map and the queue
|
---|
3790 | neighbors.push_back(prevLeaf);
|
---|
3791 | neighborMap[prevLeaf].push_back(leaf);
|
---|
3792 |
|
---|
3793 | leaf->Mail();
|
---|
3794 | prevLeaf->Mail();
|
---|
3795 |
|
---|
3796 | MergeCandidate mc(leaf->GetViewCell(), prevLeaf->GetViewCell());
|
---|
3797 |
|
---|
3798 | candidates.push_back(mc);
|
---|
3799 |
|
---|
3800 | if (((int)candidates.size() % 1000) == 0)
|
---|
3801 | {
|
---|
3802 | cout << "collected " << (int)candidates.size() << " merge candidates" << endl;
|
---|
3803 | }
|
---|
3804 | }
|
---|
3805 | }
|
---|
3806 | }
|
---|
3807 |
|
---|
3808 | Debug << "neighbormap size: " << (int)neighborMap.size() << endl;
|
---|
3809 | Debug << "merge queue: " << (int)candidates.size() << endl;
|
---|
3810 | Debug << "leaves in queue: " << numLeaves << endl;
|
---|
3811 |
|
---|
3812 |
|
---|
3813 | //-- collect the leaves which haven't been found by ray casting
|
---|
3814 | if (0)
|
---|
3815 | {
|
---|
3816 | cout << "finding additional merge candidates using geometry" << endl;
|
---|
3817 | vector<BspLeaf *> leaves;
|
---|
3818 | CollectLeaves(leaves, true);
|
---|
3819 | Debug << "found " << (int)leaves.size() << " new leaves" << endl << endl;
|
---|
3820 | CollectMergeCandidates(leaves, candidates);
|
---|
3821 | }
|
---|
3822 |
|
---|
3823 | return numLeaves;
|
---|
3824 | }
|
---|
3825 |
|
---|
3826 |
|
---|
3827 |
|
---|
3828 |
|
---|
3829 | ViewCell *VspBspTree::GetViewCell(const Vector3 &point, const bool active)
|
---|
3830 | {
|
---|
3831 | if (mRoot == NULL)
|
---|
3832 | return NULL;
|
---|
3833 |
|
---|
3834 | stack<BspNode *> nodeStack;
|
---|
3835 | nodeStack.push(mRoot);
|
---|
3836 |
|
---|
3837 | ViewCellLeaf *viewcell = NULL;
|
---|
3838 |
|
---|
3839 | while (!nodeStack.empty())
|
---|
3840 | {
|
---|
3841 | BspNode *node = nodeStack.top();
|
---|
3842 | nodeStack.pop();
|
---|
3843 |
|
---|
3844 | if (node->IsLeaf())
|
---|
3845 | {
|
---|
3846 | viewcell = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
3847 | break;
|
---|
3848 | }
|
---|
3849 | else
|
---|
3850 | {
|
---|
3851 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3852 |
|
---|
3853 | // random decision
|
---|
3854 | if (interior->GetPlane().Side(point) < 0)
|
---|
3855 | nodeStack.push(interior->GetBack());
|
---|
3856 | else
|
---|
3857 | nodeStack.push(interior->GetFront());
|
---|
3858 | }
|
---|
3859 | }
|
---|
3860 |
|
---|
3861 | if (active)
|
---|
3862 | return mViewCellsTree->GetActiveViewCell(viewcell);
|
---|
3863 | else
|
---|
3864 | return viewcell;
|
---|
3865 | }
|
---|
3866 |
|
---|
3867 |
|
---|
3868 | bool VspBspTree::ViewPointValid(const Vector3 &viewPoint) const
|
---|
3869 | {
|
---|
3870 | BspNode *node = mRoot;
|
---|
3871 |
|
---|
3872 | while (1)
|
---|
3873 | {
|
---|
3874 | // early exit
|
---|
3875 | if (node->TreeValid())
|
---|
3876 | return true;
|
---|
3877 |
|
---|
3878 | if (node->IsLeaf())
|
---|
3879 | return false;
|
---|
3880 |
|
---|
3881 | BspInterior *in = dynamic_cast<BspInterior *>(node);
|
---|
3882 |
|
---|
3883 | if (in->GetPlane().Side(viewPoint) <= 0)
|
---|
3884 | {
|
---|
3885 | node = in->GetBack();
|
---|
3886 | }
|
---|
3887 | else
|
---|
3888 | {
|
---|
3889 | node = in->GetFront();
|
---|
3890 | }
|
---|
3891 | }
|
---|
3892 |
|
---|
3893 | // should never come here
|
---|
3894 | return false;
|
---|
3895 | }
|
---|
3896 |
|
---|
3897 |
|
---|
3898 | void VspBspTree::PropagateUpValidity(BspNode *node)
|
---|
3899 | {
|
---|
3900 | const bool isValid = node->TreeValid();
|
---|
3901 |
|
---|
3902 | // propagative up invalid flag until only invalid nodes exist over this node
|
---|
3903 | if (!isValid)
|
---|
3904 | {
|
---|
3905 | while (!node->IsRoot() && node->GetParent()->TreeValid())
|
---|
3906 | {
|
---|
3907 | node = node->GetParent();
|
---|
3908 | node->SetTreeValid(false);
|
---|
3909 | }
|
---|
3910 | }
|
---|
3911 | else
|
---|
3912 | {
|
---|
3913 | // propagative up valid flag until one of the subtrees is invalid
|
---|
3914 | while (!node->IsRoot() && !node->TreeValid())
|
---|
3915 | {
|
---|
3916 | node = node->GetParent();
|
---|
3917 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3918 |
|
---|
3919 | // the parent is valid iff both leaves are valid
|
---|
3920 | node->SetTreeValid(interior->GetBack()->TreeValid() &&
|
---|
3921 | interior->GetFront()->TreeValid());
|
---|
3922 | }
|
---|
3923 | }
|
---|
3924 | }
|
---|
3925 |
|
---|
3926 | #if ZIPPED_VIEWCELLS
|
---|
3927 | bool VspBspTree::Export(ogzstream &stream)
|
---|
3928 | #else
|
---|
3929 | bool VspBspTree::Export(ofstream &stream)
|
---|
3930 | #endif
|
---|
3931 | {
|
---|
3932 | ExportNode(mRoot, stream);
|
---|
3933 |
|
---|
3934 | return true;
|
---|
3935 | }
|
---|
3936 |
|
---|
3937 | #if ZIPPED_VIEWCELLS
|
---|
3938 | void VspBspTree::ExportNode(BspNode *node, ogzstream &stream)
|
---|
3939 | #else
|
---|
3940 | void VspBspTree::ExportNode(BspNode *node, ofstream &stream)
|
---|
3941 | #endif
|
---|
3942 | {
|
---|
3943 | if (node->IsLeaf())
|
---|
3944 | {
|
---|
3945 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
3946 | ViewCell *viewCell = mViewCellsTree->GetActiveViewCell(leaf->GetViewCell());
|
---|
3947 |
|
---|
3948 | int id = -1;
|
---|
3949 | if (viewCell != mOutOfBoundsCell)
|
---|
3950 | id = viewCell->GetId();
|
---|
3951 |
|
---|
3952 | stream << "<Leaf viewCellId=\"" << id << "\" />" << endl;
|
---|
3953 | }
|
---|
3954 | else
|
---|
3955 | {
|
---|
3956 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
3957 |
|
---|
3958 | Plane3 plane = interior->GetPlane();
|
---|
3959 | stream << "<Interior plane=\"" << plane.mNormal.x << " "
|
---|
3960 | << plane.mNormal.y << " " << plane.mNormal.z << " "
|
---|
3961 | << plane.mD << "\">" << endl;
|
---|
3962 |
|
---|
3963 | ExportNode(interior->GetBack(), stream);
|
---|
3964 | ExportNode(interior->GetFront(), stream);
|
---|
3965 |
|
---|
3966 | stream << "</Interior>" << endl;
|
---|
3967 | }
|
---|
3968 | }
|
---|
3969 |
|
---|
3970 | } |
---|