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

Revision 1673, 15.4 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
[1237]47
[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
[1237]110/** This class implements a structure holding two different hierarchies,
111        one for object space partitioning and one for view space partitioning.
112
113        The object space and the view space are subdivided using a cost heuristics.
114        If an object space split or a view space split is chosen is also evaluated
115        based on the heuristics.
116       
117        The view space heuristics is evaluated by weighting and adding the pvss of the back and
118        front node of each specific split. unlike for the standalone method vspbsp tree,
119        the pvs of an object would not be the pvs of single object but that of all objects
120        which are contained in the same leaf of the object subdivision. This could be done
121        by storing the pointer to the object space partition parent, which would allow access to all children.
122        Another possibility is to include traced kd-cells in the ray casing process.
123
124        Accordingly, the object space heuristics is evaluated by storing a pvs of view cells with each object.
125        the contribution to an object to the pvs is the number of view cells it can be seen from.
126
127        @note
128        There is a potential efficiency problem involved in a sense that once a certain type
129        of split is chosen for view space / object space, the candidates for the next split of
130        object space / view space must be reevaluated.
131*/
132class HierarchyManager
133{
134public:
[1421]135        /** Constructor with the view space partition tree and
136                the object space hierarchy type as argument.
[1237]137        */
[1421]138        HierarchyManager(const int objectSpaceHierarchyType);
[1279]139        /** Hack: OspTree will copy the content from this kd tree.
140                Only view space hierarchy will be constructed.
141        */
[1421]142        HierarchyManager(KdTree *kdTree);
[1237]143
[1421]144        /** Deletes space partition and view space partition.
145        */
[1286]146        ~HierarchyManager();
147
[1237]148        /** Constructs the view space and object space subdivision from a given set of rays
149                and a set of objects.
150                @param sampleRays the set of sample rays the construction is based on
151                @param objects the set of objects
152        */
[1308]153        void Construct(
[1293]154                const VssRayContainer &sampleRays,
155                const ObjectContainer &objects,
156                AxisAlignedBox3 *forcedViewSpace);
[1237]157
[1259]158        enum
159        {
160                NO_OBJ_SUBDIV,
161                KD_BASED_OBJ_SUBDIV,
162                BV_BASED_OBJ_SUBDIV
163        };
[1237]164
[1370]165        enum
166        {
167                NO_VIEWSPACE_SUBDIV,
168                KD_BASED_VIEWSPACE_SUBDIV
169        };
170
[1259]171        /** The type of object space subdivison
172        */
[1370]173        int GetObjectSpaceSubdivisionType() const;     
174        /** The type of view space space subdivison
175        */
176        int GetViewSpaceSubdivisionType() const;
177        /** Sets a pointer to the view cells manager.
178        */             
[1279]179        void SetViewCellsManager(ViewCellsManager *vcm);
[1370]180        /** Sets a pointer to the view cells tree.
181        */
[1279]182        void SetViewCellsTree(ViewCellsTree *vcTree);
[1370]183        /** Exports the object hierarchy to disc.
184        */
[1279]185        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
[1370]186        /** Adds a sample to the pvs of the specified view cell.
187        */
[1279]188        bool AddSampleToPvs(
189                Intersectable *obj,
190                const Vector3 &hitPoint,
191                ViewCell *vc,
192                const float pdf,
193                float &contribution) const;
194
[1421]195        /** Print out statistics.
196        */
197        void PrintHierarchyStatistics(ostream &stream) const;
[1279]198
[1421]199        /** Returns the view space partition tree.
200        */
[1379]201        VspTree *GetVspTree();
[1279]202
[1421]203        /** Returns view space bounding box.
204        */
[1563]205        //AxisAlignedBox3 GetViewSpaceBox() const;
[1624]206
[1421]207        /** Returns object space bounding box.
208        */
[1416]209        AxisAlignedBox3 GetObjectSpaceBox() const;
[1379]210
[1421]211        /** Exports object space hierarchy for visualization.
212        */
[1626]213        void ExportObjectSpaceHierarchy(Exporter *exporter,
214                                                                        const ObjectContainer &objects,
215                                                                        const AxisAlignedBox3 *bbox,
216                                                                        const bool exportBounds = true) const;
[1279]217
[1421]218        /** Returns intersectable pierced by this ray.
219        */
[1626]220        Intersectable *GetIntersectable(const VssRay &ray, const bool isTermination) const;
[1418]221
[1626]222        /** Export object space partition bounding boxes.
223        */
224        void ExportBoundingBoxes(OUT_STREAM &stream, const ObjectContainer &objects);
225
[1419]226        friend ostream &operator<<(ostream &s, const HierarchyManager &hm)
227        {
[1421]228                hm.PrintHierarchyStatistics(s);
[1419]229                return s;
230        }
231
[1667]232        HierarchyStatistics &GetHierarchyStats() { return mHierarchyStats; };
233
234        inline bool ConsiderMemory() const { return mConsiderMemory; }
235
[1237]236protected:
237
[1625]238        /** Returns true if the global termination criteria were met.
239        */
[1237]240        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
241
242        /** Prepare construction of the hierarchies, set parameters, compute
243                first split candidates.
244        */
[1625]245        SubdivisionCandidate *PrepareObjectSpaceSubdivision(const VssRayContainer &sampleRays,
246                                                                                                                const ObjectContainer &objects);
[1237]247
[1625]248
[1640]249        /** Create bounding box and root.
250        */
251        void InitialiseObjectSpaceSubdivision(const ObjectContainer &objects);
252
253        /** Returns memory usage of object space hierarchy.
254        */
255        float GetObjectSpaceMemUsage() const;
256
[1667]257
[1625]258        //////////////////////////////
259        // the main loop
260
261        /** This is for interleaved construction / sequential construction.
262        */
263        void RunConstruction(const bool repairQueue,
264                                                 const VssRayContainer &sampleRays,
265                                                 const ObjectContainer &objects,
[1640]266                                                 AxisAlignedBox3 *forcedViewSpace);
[1449]267       
[1625]268        /** This is for interleaved construction using some objects
269                and some view space splits.
270        */
271        int RunConstruction(SplitQueue &splitQueue,
272                                                SubdivisionCandidateContainer &chosenCandidates,
[1667]273                                                //const float minRenderCostDecr,
274                                                SubdivisionCandidate *oldCandidate,
[1634]275                                                const int minSteps);
[1625]276
277        /** Default subdivision method.
278        */
[1449]279        void RunConstruction(const bool repairQueue);
280               
[1625]281        ////////////////////////////////////////////////
282
[1580]283        /** Evaluates the subdivision candidate and executes the split.
284        */
[1625]285        bool ApplySubdivisionCandidate(SubdivisionCandidate *sc,
286                                                                   SplitQueue &splitQueue,
287                                                                   const bool repairQueue);
[1237]288
[1625]289        /** Tests if hierarchy construction is finished.
290        */
[1237]291        bool FinishedConstruction() const;
292
[1625]293        /** Returns next subdivision candidate from the split queue.
294        */
295        SubdivisionCandidate *NextSubdivisionCandidate(SplitQueue &splitQueue);
[1237]296
[1625]297        /** Repairs the dirty entries of the subdivision candidate queue. The
298                list of entries is given in the dirty list.
[1580]299        */
[1633]300        void RepairQueue(const SubdivisionCandidateContainer &dirtyList,
301                                         SplitQueue &splitQueue,
302                                         const bool recomputeSplitPlaneOnRepair);
[1237]303
[1625]304        /** Collect subdivision candidates which were affected by the splits from the
305                chosenCandidates list.
306        */
307        void CollectDirtyCandidates(const SubdivisionCandidateContainer &chosenCandidates,
308                                                                SubdivisionCandidateContainer &dirtyList);
309
[1580]310        /** Evaluate subdivision stats for log.
311        */
[1624]312        void EvalSubdivisionStats();
[1237]313
[1625]314        void AddSubdivisionStats(const int splits,
315                                                         const float renderCostDecr,
316                                                         const float totalRenderCost,
317                                                         const int totalPvsEntries,
[1640]318                                                         const float memory,
[1654]319                                                         const float renderCostPerStorage,
320                                                         const float vspOspRatio);
[1237]321
[1625]322        bool AddSampleToPvs(Intersectable *obj,
323                                                const float pdf,
324                                                float &contribution) const;
[1237]325
[1625]326        /** Collect affected view space candidates.
327        */
328        void CollectViewSpaceDirtyList(SubdivisionCandidate *sc,
329                                                                   SubdivisionCandidateContainer &dirtyList);
[1259]330
[1625]331        /** Collect affected object space candidates.
332        */
333        void CollectObjectSpaceDirtyList(SubdivisionCandidate *sc,
334                                                                         SubdivisionCandidateContainer &dirtyList);
[1259]335               
[1625]336        /** Export object space partition tree.
337        */
338        void ExportOspTree(Exporter *exporter,
339                                           const ObjectContainer &objects) const;
[1259]340
[1625]341        /** Parse the environment variables.
342        */
[1418]343        void ParseEnvironment();
[1415]344
[1418]345        bool StartObjectSpaceSubdivision() const;
346        bool StartViewSpaceSubdivision() const;
347
[1667]348
[1625]349        ////////////////////////////
350        // Helper function for preparation of subdivision
[1286]351
[1625]352        /** Prepare bv hierarchy for subdivision
353        */
354        SubdivisionCandidate *PrepareBvHierarchy(const VssRayContainer &sampleRays,
355                                                                           const ObjectContainer &objects);
[1286]356
[1625]357        /** Prepare object space kd tree for subdivision.
358        */
359        SubdivisionCandidate *PrepareOspTree(const VssRayContainer &sampleRays,
360                                                                   const ObjectContainer &objects);
[1308]361
[1625]362        /** Prepare view space subdivision and add candidate to queue.
363        */
364        SubdivisionCandidate *PrepareViewSpaceSubdivision(const VssRayContainer &sampleRays,
365                                                                                                          const ObjectContainer &objects);
366
367        /** Was object space subdivision already constructed?
368        */
[1313]369        bool ObjectSpaceSubdivisionConstructed() const;
[1625]370       
371        /** Was view space subdivision already constructed?
372        */
[1329]373        bool ViewSpaceSubdivisionConstructed() const;
[1311]374
[1625]375        /** Reset the split queue, i.e., reevaluate the split candidates.
376        */
[1640]377    void ResetQueue(SplitQueue &splitQueue, const bool recomputeSplitPlane);
[1313]378
[1625]379        /** After the suddivision has ended, do some final tasks.
380        */
[1654]381        void FinishObjectSpaceSubdivision(const ObjectContainer &objects,
382                                                                          const bool removeRayRefs = true) const;
[1313]383
[1625]384        /** Returns depth of object space subdivision.
385        */
[1370]386        int GetObjectSpaceSubdivisionDepth() const;
387
[1640]388        /** Returns number of leaves in object space subdivision.
389        */
390        int GetObjectSpaceSubdivisionLeaves() const;
[1663]391        int GetObjectSpaceSubdivisionNodes() const;
[1640]392
[1625]393        /** Construct object space partition interleaved with view space partition.
394                Each time the best object or view space candidate is selected
395                for the next split.
396        */
[1626]397        void ConstructInterleaved(const VssRayContainer &sampleRays,
398                                                          const ObjectContainer &objects,
399                                                          AxisAlignedBox3 *forcedViewSpace);
[1449]400
[1625]401        /** Construct object space partition interleaved with view space partition.
402                The method chooses a number candidates of each type for subdivision.
403                The number is determined by the "gradient", i.e., the render cost decrease.
404                Once this render cost decrease is lower than the render cost decrease
405                for the splits of previous type, the method will stop current subdivision and
406                evaluate if view space or object space would be the beneficial for the
407                next number of split.
408        */
[1626]409        void ConstructInterleavedWithGradient(const VssRayContainer &sampleRays,
410                                                                                  const ObjectContainer &objects,
411                                                                                  AxisAlignedBox3 *forcedViewSpace);
[1624]412
[1548]413        /** Use iteration to construct the object space hierarchy.
414        */
[1626]415        void ConstructMultiLevel(const VssRayContainer &sampleRays,
416                                                         const ObjectContainer &objects,
417                                                         AxisAlignedBox3 *forcedViewSpace);
[1449]418
[1640]419        /** Based on a given subdivision, we try to optimize using an
420                multiple iteration over view and object space.
421        */
422        void OptimizeMultiLevel(const VssRayContainer &sampleRays,                                                                                       
423                                                        const ObjectContainer &objects,
424                                                        AxisAlignedBox3 *forcedViewSpace);
425
[1548]426        /** Reset the object space subdivision.
427                E.g., deletes hierarchy and resets stats.
428                so construction can be restarted.
429        */
[1625]430        SubdivisionCandidate *ResetObjectSpaceSubdivision(const VssRayContainer &rays,
431                                                                                                          const ObjectContainer &objects);
[1449]432
[1625]433        SubdivisionCandidate *ResetViewSpaceSubdivision(const VssRayContainer &rays,
[1642]434                                                                                                        const ObjectContainer &objects,
435                                                                                                        AxisAlignedBox3 *forcedViewSpace);
[1557]436
[1614]437
[1237]438protected:
439
[1627]440        /** construction types
441                sequential: construct first view space, then object space
442                interleaved: construct view space and object space fully interleaved
443                gradient: construct view space / object space until a threshold is reached
444                multilevel: iterate until subdivisions converge to the optimum.
445        */
446        enum {SEQUENTIAL, INTERLEAVED, GRADIENT, MULTILEVEL};
447
[1580]448        /// type of hierarchy construction
449        int mConstructionType;
450
451        /// Type of object space partition
[1308]452        int mObjectSpaceSubdivisionType;
[1580]453        /// Type of view space partition
[1329]454    int mViewSpaceSubdivisionType;
455
[1589]456        /// the traversal queue
457        SplitQueue mTQueue;
458       
[1580]459        ////////////
460        //-- helper variables
461       
462        // the original osp type
[1323]463        int mSavedObjectSpaceSubdivisionType;
[1580]464        // the original vsp type
[1329]465        int mSavedViewSpaceSubdivisionType;
[1580]466        /// the current subdivision candidate
[1624]467        //SubdivisionCandidate *mCurrentCandidate;
[1323]468
[1259]469
[1580]470        ///////////////////
471        // Hierarchies
472
473        /// view space hierarchy
[1259]474        VspTree *mVspTree;
[1580]475        /// object space partition kd tree
[1259]476        OspTree *mOspTree;
[1625]477
[1589]478        public:
[1580]479        /// bounding volume hierarchy
[1259]480        BvHierarchy *mBvHierarchy;
[1580]481       
[1589]482protected:
[1237]483
484
[1580]485        //////////
[1576]486        //-- global termination criteria
487
[1580]488        /// the mininal acceptable cost ratio for a split
[1237]489        float mTermMinGlobalCostRatio;
[1580]490        /// the threshold for global cost miss tolerance
[1237]491        int mTermGlobalCostMissTolerance;
[1580]492        /// maximum number of leaves
493        int mTermMaxLeaves;
[1649]494        /// Maximal allowed memory consumption.
495        float mTermMaxMemory;
[1580]496
[1667]497
[1576]498        ////////////////////
499
[1667]500        /// number of minimal steps of the same type
[1640]501        int mMinStepsOfSameType;
502
[1580]503        /// statistics about the hierarchy
[1288]504        HierarchyStatistics mHierarchyStats;
505
[1308]506        int mMinDepthForObjectSpaceSubdivion;
[1370]507        int mMinDepthForViewSpaceSubdivion;
[1580]508       
[1625]509        //int mMinRenderCostDecrease;
[1624]510
[1237]511        ofstream mSubdivisionStats;
[1314]512
[1580]513        /// if the queue should be repaired after a subdivision steps
[1314]514        bool mRepairQueue;
[1370]515
516        bool mStartWithObjectSpace;
[1580]517        /** if multi level construction method should be used
518                where we iterate over both hierarchies until we
519                converge to the optimum.
520        */
[1449]521        bool mUseMultiLevelConstruction;
[1640]522
[1580]523        /// number of iteration steps for multilevel approach   
524        int mNumMultiLevels;
[1640]525
[1633]526        /** if split plane should be recomputed for the repair.
527                Otherwise only the priority is recomputed, the
528                split plane itself stays the same
529        */
530        bool mRecomputeSplitPlaneOnRepair;
[1662]531
532        /** If memory should be considered during choosing
533                of the next split type during gradient method.
534        */
535        bool mConsiderMemory;
[1666]536
[1673]537        /// constant value for driving the heuristics
[1666]538        float mMemoryConst;
[1673]539       
540        bool mConsiderMemory2;
[1667]541
542        friend VspTree;
543        friend OspTree;
544        friend BvHierarchy;
545        friend ViewCellsParseHandlers;
546
[1237]547};
548
549}
550
551#endif
Note: See TracBrowser for help on using the repository browser.