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