source: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.h @ 1787

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