1 | #ifndef _HierarchyManager_H__
|
---|
2 | #define _HierarchyManager_H__
|
---|
3 |
|
---|
4 | #include <stack>
|
---|
5 |
|
---|
6 | #include "Mesh.h"
|
---|
7 | #include "Containers.h"
|
---|
8 | #include "Statistics.h"
|
---|
9 | #include "VssRay.h"
|
---|
10 | #include "RayInfo.h"
|
---|
11 | #include "gzstream.h"
|
---|
12 | #include "SubdivisionCandidate.h"
|
---|
13 |
|
---|
14 |
|
---|
15 |
|
---|
16 | namespace GtpVisibilityPreprocessor {
|
---|
17 |
|
---|
18 | class ViewCellLeaf;
|
---|
19 | class OspTree;
|
---|
20 | class VspTree;
|
---|
21 | class Plane3;
|
---|
22 | class AxisAlignedBox3;
|
---|
23 | class Ray;
|
---|
24 | class ViewCellsStatistics;
|
---|
25 | class ViewCellsManager;
|
---|
26 | class MergeCandidate;
|
---|
27 | class Beam;
|
---|
28 | class ViewCellsTree;
|
---|
29 | class Environment;
|
---|
30 | class VspInterior;
|
---|
31 | class VspLeaf;
|
---|
32 | class VspNode;
|
---|
33 | class KdNode;
|
---|
34 | class KdInterior;
|
---|
35 | class KdLeaf;
|
---|
36 | class OspTree;
|
---|
37 | class KdIntersectable;
|
---|
38 | class KdTree;
|
---|
39 | class VspTree;
|
---|
40 | class KdTreeStatistics;
|
---|
41 | class BvHierarchy;
|
---|
42 | class Exporter;
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | /** View space / object space hierarchy statistics.
|
---|
47 | */
|
---|
48 | class HierarchyStatistics: public StatisticsBase
|
---|
49 | {
|
---|
50 | public:
|
---|
51 | /// total number of entries in the pvs
|
---|
52 | int mPvsEntries;
|
---|
53 | /// storage cost in MB
|
---|
54 | float mMemory;
|
---|
55 | /// total number of nodes
|
---|
56 | int mNodes;
|
---|
57 | /// maximal reached depth
|
---|
58 | int mMaxDepth;
|
---|
59 | /// accumulated depth
|
---|
60 | int mAccumDepth;
|
---|
61 | /// time spent for queue repair
|
---|
62 | float mRepairTime;
|
---|
63 |
|
---|
64 | // global cost ratio violations
|
---|
65 | int mGlobalCostMisses;
|
---|
66 | /// total cost of subdivision
|
---|
67 | float mTotalCost;
|
---|
68 | /// render cost decrease of subdivision
|
---|
69 | float mRenderCostDecrease;
|
---|
70 |
|
---|
71 | // Constructor
|
---|
72 | HierarchyStatistics()
|
---|
73 | {
|
---|
74 | Reset();
|
---|
75 | }
|
---|
76 |
|
---|
77 | int Nodes() const {return mNodes;}
|
---|
78 | int Interior() const { return mNodes / 2 - 1; }
|
---|
79 | int Leaves() const { return (mNodes / 2) + 1; }
|
---|
80 |
|
---|
81 | // TODO: computation wrong
|
---|
82 | double AvgDepth() const { return mAccumDepth / (double)Leaves();}
|
---|
83 |
|
---|
84 | void Reset()
|
---|
85 | {
|
---|
86 | mGlobalCostMisses = 0;
|
---|
87 | mTotalCost = 0;
|
---|
88 | mRenderCostDecrease = 0;
|
---|
89 |
|
---|
90 | mNodes = 0;
|
---|
91 | mMaxDepth = 0;
|
---|
92 | mAccumDepth = 0;
|
---|
93 | mRepairTime = 0;
|
---|
94 | mMemory = 0;
|
---|
95 | mPvsEntries = 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | void Print(ostream &app) const;
|
---|
99 |
|
---|
100 | friend ostream &operator<<(ostream &s, const HierarchyStatistics &stat)
|
---|
101 | {
|
---|
102 | stat.Print(s);
|
---|
103 | return s;
|
---|
104 | }
|
---|
105 | };
|
---|
106 |
|
---|
107 |
|
---|
108 | /** This class implements a structure holding two different hierarchies,
|
---|
109 | one for object space partitioning and one for view space partitioning.
|
---|
110 |
|
---|
111 | The object space and the view space are subdivided using a cost heuristics.
|
---|
112 | If an object space split or a view space split is chosen is also evaluated
|
---|
113 | based on the heuristics.
|
---|
114 |
|
---|
115 | The view space heuristics is evaluated by weighting and adding the pvss of the back and
|
---|
116 | front node of each specific split. unlike for the standalone method vspbsp tree,
|
---|
117 | the pvs of an object would not be the pvs of single object but that of all objects
|
---|
118 | which are contained in the same leaf of the object subdivision. This could be done
|
---|
119 | by storing the pointer to the object space partition parent, which would allow access to all children.
|
---|
120 | Another possibility is to include traced kd-cells in the ray casing process.
|
---|
121 |
|
---|
122 | Accordingly, the object space heuristics is evaluated by storing a pvs of view cells with each object.
|
---|
123 | the contribution to an object to the pvs is the number of view cells it can be seen from.
|
---|
124 |
|
---|
125 | @note
|
---|
126 | There is a potential efficiency problem involved in a sense that once a certain type
|
---|
127 | of split is chosen for view space / object space, the candidates for the next split of
|
---|
128 | object space / view space must be reevaluated.
|
---|
129 | */
|
---|
130 | class HierarchyManager
|
---|
131 | {
|
---|
132 | friend VspTree;
|
---|
133 | friend OspTree;
|
---|
134 | friend BvHierarchy;
|
---|
135 | friend ViewCellsParseHandlers;
|
---|
136 |
|
---|
137 | public:
|
---|
138 | /** Constructor with the view space partition tree and
|
---|
139 | the object space hierarchy type as argument.
|
---|
140 | */
|
---|
141 | HierarchyManager(const int objectSpaceHierarchyType);
|
---|
142 | /** Hack: OspTree will copy the content from this kd tree.
|
---|
143 | Only view space hierarchy will be constructed.
|
---|
144 | */
|
---|
145 | HierarchyManager(KdTree *kdTree);
|
---|
146 |
|
---|
147 | /** Deletes space partition and view space partition.
|
---|
148 | */
|
---|
149 | ~HierarchyManager();
|
---|
150 |
|
---|
151 | /** Constructs the view space and object space subdivision from a given set of rays
|
---|
152 | and a set of objects.
|
---|
153 | @param sampleRays the set of sample rays the construction is based on
|
---|
154 | @param objects the set of objects
|
---|
155 | */
|
---|
156 | void Construct(
|
---|
157 | const VssRayContainer &sampleRays,
|
---|
158 | const ObjectContainer &objects,
|
---|
159 | AxisAlignedBox3 *forcedViewSpace);
|
---|
160 |
|
---|
161 | enum
|
---|
162 | {
|
---|
163 | NO_OBJ_SUBDIV,
|
---|
164 | KD_BASED_OBJ_SUBDIV,
|
---|
165 | BV_BASED_OBJ_SUBDIV
|
---|
166 | };
|
---|
167 |
|
---|
168 | enum
|
---|
169 | {
|
---|
170 | NO_VIEWSPACE_SUBDIV,
|
---|
171 | KD_BASED_VIEWSPACE_SUBDIV
|
---|
172 | };
|
---|
173 |
|
---|
174 | /** The type of object space subdivison
|
---|
175 | */
|
---|
176 | int GetObjectSpaceSubdivisionType() const;
|
---|
177 | /** The type of view space space subdivison
|
---|
178 | */
|
---|
179 | int GetViewSpaceSubdivisionType() const;
|
---|
180 | /** Sets a pointer to the view cells manager.
|
---|
181 | */
|
---|
182 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
183 | /** Sets a pointer to the view cells tree.
|
---|
184 | */
|
---|
185 | void SetViewCellsTree(ViewCellsTree *vcTree);
|
---|
186 | /** Exports the object hierarchy to disc.
|
---|
187 | */
|
---|
188 | void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
|
---|
189 | /** Adds a sample to the pvs of the specified view cell.
|
---|
190 | */
|
---|
191 | bool AddSampleToPvs(
|
---|
192 | Intersectable *obj,
|
---|
193 | const Vector3 &hitPoint,
|
---|
194 | ViewCell *vc,
|
---|
195 | const float pdf,
|
---|
196 | float &contribution) const;
|
---|
197 |
|
---|
198 | /** Print out statistics.
|
---|
199 | */
|
---|
200 | void PrintHierarchyStatistics(ostream &stream) const;
|
---|
201 |
|
---|
202 | /** Returns the view space partition tree.
|
---|
203 | */
|
---|
204 | VspTree *GetVspTree();
|
---|
205 |
|
---|
206 | /** Returns view space bounding box.
|
---|
207 | */
|
---|
208 | //AxisAlignedBox3 GetViewSpaceBox() const;
|
---|
209 |
|
---|
210 | /** Returns object space bounding box.
|
---|
211 | */
|
---|
212 | AxisAlignedBox3 GetObjectSpaceBox() const;
|
---|
213 |
|
---|
214 | /** Exports object space hierarchy for visualization.
|
---|
215 | */
|
---|
216 | void ExportObjectSpaceHierarchy(Exporter *exporter,
|
---|
217 | const ObjectContainer &objects,
|
---|
218 | const AxisAlignedBox3 *bbox,
|
---|
219 | const bool exportBounds = true) const;
|
---|
220 |
|
---|
221 | /** Returns intersectable pierced by this ray.
|
---|
222 | */
|
---|
223 | Intersectable *GetIntersectable(const VssRay &ray, const bool isTermination) const;
|
---|
224 |
|
---|
225 | /** Export object space partition bounding boxes.
|
---|
226 | */
|
---|
227 | void ExportBoundingBoxes(OUT_STREAM &stream, const ObjectContainer &objects);
|
---|
228 |
|
---|
229 | friend ostream &operator<<(ostream &s, const HierarchyManager &hm)
|
---|
230 | {
|
---|
231 | hm.PrintHierarchyStatistics(s);
|
---|
232 | return s;
|
---|
233 | }
|
---|
234 |
|
---|
235 | protected:
|
---|
236 |
|
---|
237 | /** Returns true if the global termination criteria were met.
|
---|
238 | */
|
---|
239 | bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
|
---|
240 |
|
---|
241 | /** Prepare construction of the hierarchies, set parameters, compute
|
---|
242 | first split candidates.
|
---|
243 | */
|
---|
244 | SubdivisionCandidate *PrepareObjectSpaceSubdivision(const VssRayContainer &sampleRays,
|
---|
245 | const ObjectContainer &objects);
|
---|
246 |
|
---|
247 |
|
---|
248 | /** Create bounding box and root.
|
---|
249 | */
|
---|
250 | void InitialiseObjectSpaceSubdivision(const ObjectContainer &objects);
|
---|
251 |
|
---|
252 | /** Returns memory usage of object space hierarchy.
|
---|
253 | */
|
---|
254 | float GetObjectSpaceMemUsage() const;
|
---|
255 |
|
---|
256 | //////////////////////////////
|
---|
257 | // the main loop
|
---|
258 | //////////////////////
|
---|
259 |
|
---|
260 | /** This is for interleaved construction / sequential construction.
|
---|
261 | */
|
---|
262 | void RunConstruction(const bool repairQueue,
|
---|
263 | const VssRayContainer &sampleRays,
|
---|
264 | const ObjectContainer &objects,
|
---|
265 | AxisAlignedBox3 *forcedViewSpace);
|
---|
266 |
|
---|
267 | /** This is for interleaved construction using some objects
|
---|
268 | and some view space splits.
|
---|
269 | */
|
---|
270 | int RunConstruction(SplitQueue &splitQueue,
|
---|
271 | SubdivisionCandidateContainer &chosenCandidates,
|
---|
272 | const float minRenderCostDecr,
|
---|
273 | const int minSteps);
|
---|
274 |
|
---|
275 | /** Default subdivision method.
|
---|
276 | */
|
---|
277 | void RunConstruction(const bool repairQueue);
|
---|
278 |
|
---|
279 | ////////////////////////////////////////////////
|
---|
280 |
|
---|
281 | /** Evaluates the subdivision candidate and executes the split.
|
---|
282 | */
|
---|
283 | bool ApplySubdivisionCandidate(SubdivisionCandidate *sc,
|
---|
284 | SplitQueue &splitQueue,
|
---|
285 | const bool repairQueue);
|
---|
286 |
|
---|
287 | /** Tests if hierarchy construction is finished.
|
---|
288 | */
|
---|
289 | bool FinishedConstruction() const;
|
---|
290 |
|
---|
291 | /** Returns next subdivision candidate from the split queue.
|
---|
292 | */
|
---|
293 | SubdivisionCandidate *NextSubdivisionCandidate(SplitQueue &splitQueue);
|
---|
294 |
|
---|
295 | /** Repairs the dirty entries of the subdivision candidate queue. The
|
---|
296 | list of entries is given in the dirty list.
|
---|
297 | */
|
---|
298 | void RepairQueue(const SubdivisionCandidateContainer &dirtyList,
|
---|
299 | SplitQueue &splitQueue,
|
---|
300 | const bool recomputeSplitPlaneOnRepair);
|
---|
301 |
|
---|
302 | /** Collect subdivision candidates which were affected by the splits from the
|
---|
303 | chosenCandidates list.
|
---|
304 | */
|
---|
305 | void CollectDirtyCandidates(const SubdivisionCandidateContainer &chosenCandidates,
|
---|
306 | SubdivisionCandidateContainer &dirtyList);
|
---|
307 |
|
---|
308 | /** Evaluate subdivision stats for log.
|
---|
309 | */
|
---|
310 | void EvalSubdivisionStats();
|
---|
311 |
|
---|
312 | void AddSubdivisionStats(const int splits,
|
---|
313 | const float renderCostDecr,
|
---|
314 | const float totalRenderCost,
|
---|
315 | const int totalPvsEntries,
|
---|
316 | const float memory,
|
---|
317 | const float renderCostPerStorage);
|
---|
318 |
|
---|
319 | bool AddSampleToPvs(Intersectable *obj,
|
---|
320 | const float pdf,
|
---|
321 | float &contribution) const;
|
---|
322 |
|
---|
323 | /** Collect affected view space candidates.
|
---|
324 | */
|
---|
325 | void CollectViewSpaceDirtyList(SubdivisionCandidate *sc,
|
---|
326 | SubdivisionCandidateContainer &dirtyList);
|
---|
327 |
|
---|
328 | /** Collect affected object space candidates.
|
---|
329 | */
|
---|
330 | void CollectObjectSpaceDirtyList(SubdivisionCandidate *sc,
|
---|
331 | SubdivisionCandidateContainer &dirtyList);
|
---|
332 |
|
---|
333 | /** Export object space partition tree.
|
---|
334 | */
|
---|
335 | void ExportOspTree(Exporter *exporter,
|
---|
336 | const ObjectContainer &objects) const;
|
---|
337 |
|
---|
338 | /** Parse the environment variables.
|
---|
339 | */
|
---|
340 | void ParseEnvironment();
|
---|
341 |
|
---|
342 | bool StartObjectSpaceSubdivision() const;
|
---|
343 | bool StartViewSpaceSubdivision() const;
|
---|
344 |
|
---|
345 | ////////////////////////////
|
---|
346 | // Helper function for preparation of subdivision
|
---|
347 | ///////
|
---|
348 |
|
---|
349 | /** Prepare bv hierarchy for subdivision
|
---|
350 | */
|
---|
351 | SubdivisionCandidate *PrepareBvHierarchy(const VssRayContainer &sampleRays,
|
---|
352 | const ObjectContainer &objects);
|
---|
353 |
|
---|
354 | /** Prepare object space kd tree for subdivision.
|
---|
355 | */
|
---|
356 | SubdivisionCandidate *PrepareOspTree(const VssRayContainer &sampleRays,
|
---|
357 | const ObjectContainer &objects);
|
---|
358 |
|
---|
359 | /** Prepare view space subdivision and add candidate to queue.
|
---|
360 | */
|
---|
361 | SubdivisionCandidate *PrepareViewSpaceSubdivision(const VssRayContainer &sampleRays,
|
---|
362 | const ObjectContainer &objects);
|
---|
363 |
|
---|
364 | /** Was object space subdivision already constructed?
|
---|
365 | */
|
---|
366 | bool ObjectSpaceSubdivisionConstructed() const;
|
---|
367 |
|
---|
368 | /** Was view space subdivision already constructed?
|
---|
369 | */
|
---|
370 | bool ViewSpaceSubdivisionConstructed() const;
|
---|
371 |
|
---|
372 | /** Reset the split queue, i.e., reevaluate the split candidates.
|
---|
373 | */
|
---|
374 | void ResetQueue(SplitQueue &splitQueue, const bool recomputeSplitPlane);
|
---|
375 |
|
---|
376 | /** After the suddivision has ended, do some final tasks.
|
---|
377 | */
|
---|
378 | void FinishObjectSpaceSubdivision(const ObjectContainer &objects) const;
|
---|
379 |
|
---|
380 | /** Returns depth of object space subdivision.
|
---|
381 | */
|
---|
382 | int GetObjectSpaceSubdivisionDepth() const;
|
---|
383 |
|
---|
384 | /** Returns number of leaves in object space subdivision.
|
---|
385 | */
|
---|
386 | int GetObjectSpaceSubdivisionLeaves() const;
|
---|
387 |
|
---|
388 | /** Construct object space partition interleaved with view space partition.
|
---|
389 | Each time the best object or view space candidate is selected
|
---|
390 | for the next split.
|
---|
391 | */
|
---|
392 | void ConstructInterleaved(const VssRayContainer &sampleRays,
|
---|
393 | const ObjectContainer &objects,
|
---|
394 | AxisAlignedBox3 *forcedViewSpace);
|
---|
395 |
|
---|
396 | /** Construct object space partition interleaved with view space partition.
|
---|
397 | The method chooses a number candidates of each type for subdivision.
|
---|
398 | The number is determined by the "gradient", i.e., the render cost decrease.
|
---|
399 | Once this render cost decrease is lower than the render cost decrease
|
---|
400 | for the splits of previous type, the method will stop current subdivision and
|
---|
401 | evaluate if view space or object space would be the beneficial for the
|
---|
402 | next number of split.
|
---|
403 | */
|
---|
404 | void ConstructInterleavedWithGradient(const VssRayContainer &sampleRays,
|
---|
405 | const ObjectContainer &objects,
|
---|
406 | AxisAlignedBox3 *forcedViewSpace);
|
---|
407 |
|
---|
408 | /** Use iteration to construct the object space hierarchy.
|
---|
409 | */
|
---|
410 | void ConstructMultiLevel(const VssRayContainer &sampleRays,
|
---|
411 | const ObjectContainer &objects,
|
---|
412 | AxisAlignedBox3 *forcedViewSpace);
|
---|
413 |
|
---|
414 | /** Based on a given subdivision, we try to optimize using an
|
---|
415 | multiple iteration over view and object space.
|
---|
416 | */
|
---|
417 | void OptimizeMultiLevel(const VssRayContainer &sampleRays,
|
---|
418 | const ObjectContainer &objects,
|
---|
419 | AxisAlignedBox3 *forcedViewSpace);
|
---|
420 |
|
---|
421 | /** Reset the object space subdivision.
|
---|
422 | E.g., deletes hierarchy and resets stats.
|
---|
423 | so construction can be restarted.
|
---|
424 | */
|
---|
425 | SubdivisionCandidate *ResetObjectSpaceSubdivision(const VssRayContainer &rays,
|
---|
426 | const ObjectContainer &objects);
|
---|
427 |
|
---|
428 | SubdivisionCandidate *ResetViewSpaceSubdivision(const VssRayContainer &rays,
|
---|
429 | const ObjectContainer &objects);
|
---|
430 |
|
---|
431 |
|
---|
432 | protected:
|
---|
433 |
|
---|
434 | /** construction types
|
---|
435 | sequential: construct first view space, then object space
|
---|
436 | interleaved: construct view space and object space fully interleaved
|
---|
437 | gradient: construct view space / object space until a threshold is reached
|
---|
438 | multilevel: iterate until subdivisions converge to the optimum.
|
---|
439 | */
|
---|
440 | enum {SEQUENTIAL, INTERLEAVED, GRADIENT, MULTILEVEL};
|
---|
441 |
|
---|
442 | /// type of hierarchy construction
|
---|
443 | int mConstructionType;
|
---|
444 |
|
---|
445 | /// Type of object space partition
|
---|
446 | int mObjectSpaceSubdivisionType;
|
---|
447 | /// Type of view space partition
|
---|
448 | int mViewSpaceSubdivisionType;
|
---|
449 |
|
---|
450 | /// the traversal queue
|
---|
451 | SplitQueue mTQueue;
|
---|
452 |
|
---|
453 | ////////////
|
---|
454 | //-- helper variables
|
---|
455 |
|
---|
456 | // the original osp type
|
---|
457 | int mSavedObjectSpaceSubdivisionType;
|
---|
458 | // the original vsp type
|
---|
459 | int mSavedViewSpaceSubdivisionType;
|
---|
460 | /// the current subdivision candidate
|
---|
461 | //SubdivisionCandidate *mCurrentCandidate;
|
---|
462 |
|
---|
463 |
|
---|
464 | ///////////////////
|
---|
465 | // Hierarchies
|
---|
466 |
|
---|
467 | /// view space hierarchy
|
---|
468 | VspTree *mVspTree;
|
---|
469 | /// object space partition kd tree
|
---|
470 | OspTree *mOspTree;
|
---|
471 |
|
---|
472 | public:
|
---|
473 | /// bounding volume hierarchy
|
---|
474 | BvHierarchy *mBvHierarchy;
|
---|
475 |
|
---|
476 | protected:
|
---|
477 |
|
---|
478 |
|
---|
479 | //////////
|
---|
480 | //-- global termination criteria
|
---|
481 |
|
---|
482 | /// the mininal acceptable cost ratio for a split
|
---|
483 | float mTermMinGlobalCostRatio;
|
---|
484 | /// the threshold for global cost miss tolerance
|
---|
485 | int mTermGlobalCostMissTolerance;
|
---|
486 | /// maximum number of leaves
|
---|
487 | int mTermMaxLeaves;
|
---|
488 |
|
---|
489 | ////////////////////
|
---|
490 |
|
---|
491 | int mMinStepsOfSameType;
|
---|
492 |
|
---|
493 | /// statistics about the hierarchy
|
---|
494 | HierarchyStatistics mHierarchyStats;
|
---|
495 |
|
---|
496 | int mMinDepthForObjectSpaceSubdivion;
|
---|
497 | int mMinDepthForViewSpaceSubdivion;
|
---|
498 |
|
---|
499 | //int mMinRenderCostDecrease;
|
---|
500 |
|
---|
501 | ofstream mSubdivisionStats;
|
---|
502 |
|
---|
503 | /// if the queue should be repaired after a subdivision steps
|
---|
504 | bool mRepairQueue;
|
---|
505 |
|
---|
506 | bool mStartWithObjectSpace;
|
---|
507 | /** if multi level construction method should be used
|
---|
508 | where we iterate over both hierarchies until we
|
---|
509 | converge to the optimum.
|
---|
510 | */
|
---|
511 | bool mUseMultiLevelConstruction;
|
---|
512 |
|
---|
513 | /// number of iteration steps for multilevel approach
|
---|
514 | int mNumMultiLevels;
|
---|
515 |
|
---|
516 | /** if split plane should be recomputed for the repair.
|
---|
517 | Otherwise only the priority is recomputed, the
|
---|
518 | split plane itself stays the same
|
---|
519 | */
|
---|
520 | bool mRecomputeSplitPlaneOnRepair;
|
---|
521 | };
|
---|
522 |
|
---|
523 | }
|
---|
524 |
|
---|
525 | #endif
|
---|