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

Revision 1899, 17.6 KB checked in by mattausch, 18 years ago (diff)
RevLine 
[1237]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"
[1239]12#include "SubdivisionCandidate.h"
[1237]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;
[1259]41class BvHierarchy;
[1287]42class Exporter;
[1667]43class ViewCellsParseHandlers;
[1237]44
[1667]45
[1649]46#define COUNT_ORIGIN_OBJECTS 1
[1899]47#define USE_AVGRAYCONTRI 1
[1551]48/** View space / object space hierarchy statistics.
[1288]49*/
50class HierarchyStatistics: public StatisticsBase
51{
52public:
[1576]53        /// total number of entries in the pvs
[1624]54        int mPvsEntries;
[1640]55        /// storage cost in MB
56        float mMemory;
[1288]57        /// total number of nodes
[1624]58        int mNodes;
[1288]59        /// maximal reached depth
[1624]60        int mMaxDepth;
[1288]61        /// accumulated depth
[1624]62        int mAccumDepth;
[1449]63        /// time spent for queue repair
[1624]64        float mRepairTime;
65
[1449]66        // global cost ratio violations
67        int mGlobalCostMisses;
[1624]68        /// total cost of subdivision
69        float mTotalCost;
70        /// render cost decrease of subdivision
71        float mRenderCostDecrease;
[1313]72
[1288]73        // Constructor
74        HierarchyStatistics()
75        {
76                Reset();
77        }
78
[1624]79        int Nodes() const {return mNodes;}
[1640]80        int Interior() const { return mNodes / 2 - 1; }
[1624]81        int Leaves() const { return (mNodes / 2) + 1; }
[1288]82       
83        // TODO: computation wrong
[1624]84        double AvgDepth() const { return mAccumDepth / (double)Leaves();}
[1288]85
[1449]86        void Reset()
[1288]87        {
[1449]88                mGlobalCostMisses = 0;
[1624]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;
[1288]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
[1723]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
[1744]130        float mPriority;
[1723]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;
[1744]148                mPriority = 0;
[1723]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
[1237]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:
[1421]187        /** Constructor with the view space partition tree and
188                the object space hierarchy type as argument.
[1237]189        */
[1421]190        HierarchyManager(const int objectSpaceHierarchyType);
[1279]191        /** Hack: OspTree will copy the content from this kd tree.
192                Only view space hierarchy will be constructed.
193        */
[1421]194        HierarchyManager(KdTree *kdTree);
[1237]195
[1421]196        /** Deletes space partition and view space partition.
197        */
[1286]198        ~HierarchyManager();
199
[1237]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        */
[1308]205        void Construct(
[1293]206                const VssRayContainer &sampleRays,
207                const ObjectContainer &objects,
208                AxisAlignedBox3 *forcedViewSpace);
[1237]209
[1259]210        enum
211        {
212                NO_OBJ_SUBDIV,
213                KD_BASED_OBJ_SUBDIV,
214                BV_BASED_OBJ_SUBDIV
215        };
[1237]216
[1370]217        enum
218        {
219                NO_VIEWSPACE_SUBDIV,
220                KD_BASED_VIEWSPACE_SUBDIV
221        };
222
[1259]223        /** The type of object space subdivison
224        */
[1370]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        */             
[1279]231        void SetViewCellsManager(ViewCellsManager *vcm);
[1370]232        /** Sets a pointer to the view cells tree.
233        */
[1279]234        void SetViewCellsTree(ViewCellsTree *vcTree);
[1370]235        /** Exports the object hierarchy to disc.
236        */
[1279]237        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
[1764]238       
[1421]239        /** Print out statistics.
240        */
241        void PrintHierarchyStatistics(ostream &stream) const;
[1279]242
[1421]243        /** Returns the view space partition tree.
244        */
[1379]245        VspTree *GetVspTree();
[1279]246
[1421]247        /** Returns view space bounding box.
248        */
[1563]249        //AxisAlignedBox3 GetViewSpaceBox() const;
[1624]250
[1421]251        /** Returns object space bounding box.
252        */
[1416]253        AxisAlignedBox3 GetObjectSpaceBox() const;
[1379]254
[1421]255        /** Exports object space hierarchy for visualization.
256        */
[1626]257        void ExportObjectSpaceHierarchy(Exporter *exporter,
258                                                                        const ObjectContainer &objects,
[1764]259                                                                        AxisAlignedBox3 *bbox,
[1626]260                                                                        const bool exportBounds = true) const;
[1279]261
[1421]262        /** Returns intersectable pierced by this ray.
263        */
[1626]264        Intersectable *GetIntersectable(const VssRay &ray, const bool isTermination) const;
[1743]265 
[1889]266        Intersectable *GetIntersectable(Intersectable *obj, const Vector3 &point) const;
267
[1626]268        /** Export object space partition bounding boxes.
269        */
270        void ExportBoundingBoxes(OUT_STREAM &stream, const ObjectContainer &objects);
271
[1419]272        friend ostream &operator<<(ostream &s, const HierarchyManager &hm)
273        {
[1421]274                hm.PrintHierarchyStatistics(s);
[1419]275                return s;
276        }
277
[1667]278        HierarchyStatistics &GetHierarchyStats() { return mHierarchyStats; };
279
280        inline bool ConsiderMemory() const { return mConsiderMemory; }
[1686]281       
282        void EvaluateSubdivision(const VssRayContainer &sampleRays,                                                                                     
283                                                         const ObjectContainer &objects,
284                                                         const string &filename);
[1676]285
[1710]286        void EvaluateSubdivision2(ofstream &splitsStats,
[1745]287                                                          const int splitsStepSize,
288                                                          const bool useFilter);
[1709]289
290
[1718]291        void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
292
[1845]293        int CompressObjectSpace();
[1843]294        void CreateUniqueObjectIds();
295
296
[1686]297        float mInitialRenderCost;
298
299
[1237]300protected:
301
[1625]302        /** Returns true if the global termination criteria were met.
303        */
[1237]304        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
305
306        /** Prepare construction of the hierarchies, set parameters, compute
307                first split candidates.
308        */
[1779]309        void PrepareObjectSpaceSubdivision(SplitQueue &tQueue,
310                                                                           const VssRayContainer &sampleRays,
311                                                                           const ObjectContainer &objects);
[1237]312
[1625]313
[1640]314        /** Create bounding box and root.
315        */
316        void InitialiseObjectSpaceSubdivision(const ObjectContainer &objects);
317
318        /** Returns memory usage of object space hierarchy.
319        */
320        float GetObjectSpaceMemUsage() const;
321
[1667]322
[1625]323        //////////////////////////////
324        // the main loop
325
326        /** This is for interleaved construction / sequential construction.
327        */
328        void RunConstruction(const bool repairQueue,
329                                                 const VssRayContainer &sampleRays,
330                                                 const ObjectContainer &objects,
[1640]331                                                 AxisAlignedBox3 *forcedViewSpace);
[1449]332       
[1625]333        /** This is for interleaved construction using some objects
334                and some view space splits.
335        */
336        int RunConstruction(SplitQueue &splitQueue,
337                                                SubdivisionCandidateContainer &chosenCandidates,
[1667]338                                                //const float minRenderCostDecr,
339                                                SubdivisionCandidate *oldCandidate,
[1676]340                                                const int minSteps,
341                                                const int maxSteps);
[1625]342
343        /** Default subdivision method.
344        */
[1449]345        void RunConstruction(const bool repairQueue);
346               
[1625]347        ////////////////////////////////////////////////
348
[1580]349        /** Evaluates the subdivision candidate and executes the split.
350        */
[1625]351        bool ApplySubdivisionCandidate(SubdivisionCandidate *sc,
352                                                                   SplitQueue &splitQueue,
353                                                                   const bool repairQueue);
[1237]354
[1625]355        /** Tests if hierarchy construction is finished.
356        */
[1237]357        bool FinishedConstruction() const;
358
[1625]359        /** Returns next subdivision candidate from the split queue.
360        */
361        SubdivisionCandidate *NextSubdivisionCandidate(SplitQueue &splitQueue);
[1237]362
[1625]363        /** Repairs the dirty entries of the subdivision candidate queue. The
364                list of entries is given in the dirty list.
[1736]365
366                @returns number of repaired candidates
[1580]367        */
[1736]368        int RepairQueue(const SubdivisionCandidateContainer &dirtyList,
369                                        SplitQueue &splitQueue,
370                                        const bool recomputeSplitPlaneOnRepair);
[1237]371
[1625]372        /** Collect subdivision candidates which were affected by the splits from the
373                chosenCandidates list.
374        */
375        void CollectDirtyCandidates(const SubdivisionCandidateContainer &chosenCandidates,
376                                                                SubdivisionCandidateContainer &dirtyList);
377
[1580]378        /** Evaluate subdivision stats for log.
379        */
[1624]380        void EvalSubdivisionStats();
[1237]381
[1625]382        void AddSubdivisionStats(const int splits,
383                                                         const float renderCostDecr,
384                                                         const float totalRenderCost,
385                                                         const int totalPvsEntries,
[1640]386                                                         const float memory,
[1654]387                                                         const float renderCostPerStorage,
388                                                         const float vspOspRatio);
[1237]389
[1625]390        /** Collect affected view space candidates.
391        */
392        void CollectViewSpaceDirtyList(SubdivisionCandidate *sc,
393                                                                   SubdivisionCandidateContainer &dirtyList);
[1259]394
[1625]395        /** Collect affected object space candidates.
396        */
397        void CollectObjectSpaceDirtyList(SubdivisionCandidate *sc,
398                                                                         SubdivisionCandidateContainer &dirtyList);
[1259]399               
[1625]400        /** Export object space partition tree.
401        */
402        void ExportOspTree(Exporter *exporter,
403                                           const ObjectContainer &objects) const;
[1259]404
[1625]405        /** Parse the environment variables.
406        */
[1418]407        void ParseEnvironment();
[1415]408
[1418]409        bool StartObjectSpaceSubdivision() const;
410        bool StartViewSpaceSubdivision() const;
411
[1667]412
[1625]413        ////////////////////////////
414        // Helper function for preparation of subdivision
[1286]415
[1625]416        /** Prepare bv hierarchy for subdivision
417        */
[1779]418        void PrepareBvHierarchy(SplitQueue &tQueue,
419                                                        const VssRayContainer &sampleRays,
420                                                        const ObjectContainer &objects);
[1286]421
[1625]422        /** Prepare object space kd tree for subdivision.
423        */
[1779]424        void PrepareOspTree(SplitQueue &tQueue,
425                                                const VssRayContainer &sampleRays,
426                                                const ObjectContainer &objects);
[1308]427
[1625]428        /** Prepare view space subdivision and add candidate to queue.
429        */
[1779]430        void PrepareViewSpaceSubdivision(SplitQueue &tQueue,
431                                                                         const VssRayContainer &sampleRays,
432                                                                         const ObjectContainer &objects);
[1625]433
434        /** Was object space subdivision already constructed?
435        */
[1313]436        bool ObjectSpaceSubdivisionConstructed() const;
[1625]437       
438        /** Was view space subdivision already constructed?
439        */
[1329]440        bool ViewSpaceSubdivisionConstructed() const;
[1311]441
[1625]442        /** Reset the split queue, i.e., reevaluate the split candidates.
443        */
[1640]444    void ResetQueue(SplitQueue &splitQueue, const bool recomputeSplitPlane);
[1313]445
[1625]446        /** After the suddivision has ended, do some final tasks.
447        */
[1654]448        void FinishObjectSpaceSubdivision(const ObjectContainer &objects,
449                                                                          const bool removeRayRefs = true) const;
[1313]450
[1625]451        /** Returns depth of object space subdivision.
452        */
[1370]453        int GetObjectSpaceSubdivisionDepth() const;
454
[1640]455        /** Returns number of leaves in object space subdivision.
456        */
457        int GetObjectSpaceSubdivisionLeaves() const;
[1663]458        int GetObjectSpaceSubdivisionNodes() const;
[1640]459
[1625]460        /** Construct object space partition interleaved with view space partition.
461                Each time the best object or view space candidate is selected
462                for the next split.
463        */
[1626]464        void ConstructInterleaved(const VssRayContainer &sampleRays,
465                                                          const ObjectContainer &objects,
466                                                          AxisAlignedBox3 *forcedViewSpace);
[1449]467
[1625]468        /** Construct object space partition interleaved with view space partition.
469                The method chooses a number candidates of each type for subdivision.
470                The number is determined by the "gradient", i.e., the render cost decrease.
471                Once this render cost decrease is lower than the render cost decrease
472                for the splits of previous type, the method will stop current subdivision and
473                evaluate if view space or object space would be the beneficial for the
474                next number of split.
475        */
[1626]476        void ConstructInterleavedWithGradient(const VssRayContainer &sampleRays,
477                                                                                  const ObjectContainer &objects,
478                                                                                  AxisAlignedBox3 *forcedViewSpace);
[1624]479
[1548]480        /** Use iteration to construct the object space hierarchy.
481        */
[1626]482        void ConstructMultiLevel(const VssRayContainer &sampleRays,
483                                                         const ObjectContainer &objects,
484                                                         AxisAlignedBox3 *forcedViewSpace);
[1449]485
[1640]486        /** Based on a given subdivision, we try to optimize using an
487                multiple iteration over view and object space.
488        */
489        void OptimizeMultiLevel(const VssRayContainer &sampleRays,                                                                                       
490                                                        const ObjectContainer &objects,
491                                                        AxisAlignedBox3 *forcedViewSpace);
492
[1548]493        /** Reset the object space subdivision.
494                E.g., deletes hierarchy and resets stats.
495                so construction can be restarted.
496        */
[1779]497        void ResetObjectSpaceSubdivision(SplitQueue &tQueue,
498                                                                         const VssRayContainer &rays,
499                                                                         const ObjectContainer &objects);
[1449]500
[1779]501        void ResetViewSpaceSubdivision(SplitQueue &tQueue,
502                                                                   const VssRayContainer &rays,
503                                                                   const ObjectContainer &objects,
504                                                                   AxisAlignedBox3 *forcedViewSpace);
[1557]505
[1614]506
[1686]507        ///////////////////////////
[1680]508
[1779]509        void ExportStats(ofstream &stats,
510                                         SplitQueue &tQueue,
511                                         const ObjectContainer &objects);
[1686]512
[1706]513        void CollectBestSet(const int maxSplits,
514                                                const float maxMemoryCost,
[1707]515                                                ViewCellContainer &viewCells,
[1706]516                                                vector<BvhNode *> &bvhNodes);
517
[1713]518        int ExtractStatistics(const int maxSplits,
519                                                  const float maxMemoryCost,
520                                                  float &renderCost,
521                                                  float &memory,
[1718]522                                                  int &pvsEntries,
523                                                  int &viewSpaceSplits,
[1745]524                                                  int &objectSpaceSplits,
525                                                  const bool useFilter);
[1709]526
[1745]527        void ComputePvs(const ObjectPvs &pvs, float &rc, int &pvsEntries);
[1787]528        void GetPvsIncrementially(ViewCell *vc, ObjectPvs &pvs) const;
[1786]529        void PullUpPvsIncrementally(ViewCell *viewCell) const;
530        void GetPvsRecursive(ViewCell *vc, ObjectPvs &pvs) const;
[1787]531        void GetPvsEfficiently(ViewCell *vc, ObjectPvs &pvs) const;
532
[1830]533        float EvalFullMem() const;
534
[1843]535
536
[1237]537protected:
538
[1627]539        /** construction types
540                sequential: construct first view space, then object space
541                interleaved: construct view space and object space fully interleaved
542                gradient: construct view space / object space until a threshold is reached
543                multilevel: iterate until subdivisions converge to the optimum.
544        */
545        enum {SEQUENTIAL, INTERLEAVED, GRADIENT, MULTILEVEL};
546
[1580]547        /// type of hierarchy construction
548        int mConstructionType;
549
550        /// Type of object space partition
[1308]551        int mObjectSpaceSubdivisionType;
[1580]552        /// Type of view space partition
[1329]553    int mViewSpaceSubdivisionType;
554
[1589]555        /// the traversal queue
556        SplitQueue mTQueue;
557       
[1580]558        ////////////
559        //-- helper variables
560       
561        // the original osp type
[1323]562        int mSavedObjectSpaceSubdivisionType;
[1580]563        // the original vsp type
[1329]564        int mSavedViewSpaceSubdivisionType;
[1580]565        /// the current subdivision candidate
[1624]566        //SubdivisionCandidate *mCurrentCandidate;
[1323]567
[1259]568
[1580]569        ///////////////////
570        // Hierarchies
571
572        /// view space hierarchy
[1259]573        VspTree *mVspTree;
[1580]574        /// object space partition kd tree
[1259]575        OspTree *mOspTree;
[1625]576
[1895]577        // quick hack:
578public:
[1580]579        /// bounding volume hierarchy
[1259]580        BvHierarchy *mBvHierarchy;
[1580]581       
[1895]582        float mMaxAvgRayContri;
583
[1589]584protected:
[1237]585
586
[1580]587        //////////
[1576]588        //-- global termination criteria
589
[1580]590        /// the mininal acceptable cost ratio for a split
[1237]591        float mTermMinGlobalCostRatio;
[1580]592        /// the threshold for global cost miss tolerance
[1237]593        int mTermGlobalCostMissTolerance;
[1580]594        /// maximum number of leaves
595        int mTermMaxLeaves;
[1649]596        /// Maximal allowed memory consumption.
597        float mTermMaxMemory;
[1580]598
[1667]599
[1576]600        ////////////////////
601
[1667]602        /// number of minimal steps of the same type
[1640]603        int mMinStepsOfSameType;
[1684]604        int mMaxStepsOfSameType;
[1640]605
[1580]606        /// statistics about the hierarchy
[1288]607        HierarchyStatistics mHierarchyStats;
608
[1308]609        int mMinDepthForObjectSpaceSubdivion;
[1370]610        int mMinDepthForViewSpaceSubdivion;
[1580]611       
[1625]612        //int mMinRenderCostDecrease;
[1624]613
[1237]614        ofstream mSubdivisionStats;
[1314]615
[1580]616        /// if the queue should be repaired after a subdivision steps
[1314]617        bool mRepairQueue;
[1370]618
619        bool mStartWithObjectSpace;
[1580]620        /** if multi level construction method should be used
621                where we iterate over both hierarchies until we
622                converge to the optimum.
623        */
[1449]624        bool mUseMultiLevelConstruction;
[1640]625
[1580]626        /// number of iteration steps for multilevel approach   
627        int mNumMultiLevels;
[1640]628
[1633]629        /** if split plane should be recomputed for the repair.
630                Otherwise only the priority is recomputed, the
631                split plane itself stays the same
632        */
633        bool mRecomputeSplitPlaneOnRepair;
[1662]634
635        /** If memory should be considered during choosing
636                of the next split type during gradient method.
637        */
638        bool mConsiderMemory;
[1666]639
[1727]640        int mMaxRepairs;
641
[1679]642        int mTimeStamp;
[1667]643        friend VspTree;
644        friend OspTree;
645        friend BvHierarchy;
[1744]646
647        float mPriority;
648
[1667]649        friend ViewCellsParseHandlers;
650
[1750]651        ViewCellContainer mOldViewCells;
[1786]652
653        ObjectPvs mOldPvs;
[1237]654};
655
656}
657
658#endif
Note: See TracBrowser for help on using the repository browser.