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

Revision 1624, 10.9 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;
[1237]43
44
45
[1551]46/** View space / object space hierarchy statistics.
[1288]47*/
48class HierarchyStatistics: public StatisticsBase
49{
50public:
[1576]51        /// total number of entries in the pvs
[1624]52        int mPvsEntries;
[1576]53        /// storage cost
[1624]54        int mMemory;
[1288]55        /// total number of nodes
[1624]56        int mNodes;
[1288]57        /// maximal reached depth
[1624]58        int mMaxDepth;
[1288]59        /// accumulated depth
[1624]60        int mAccumDepth;
[1449]61        /// time spent for queue repair
[1624]62        float mRepairTime;
63
[1449]64        // global cost ratio violations
65        int mGlobalCostMisses;
[1624]66        /// total cost of subdivision
67        float mTotalCost;
68        /// render cost decrease of subdivision
69        float mRenderCostDecrease;
[1313]70
[1288]71        // Constructor
72        HierarchyStatistics()
73        {
74                Reset();
75        }
76
[1624]77        int Nodes() const {return mNodes;}
78        int Interior() const { return mNodes / 2; }
79        int Leaves() const { return (mNodes / 2) + 1; }
[1288]80       
81        // TODO: computation wrong
[1624]82        double AvgDepth() const { return mAccumDepth / (double)Leaves();}
[1288]83
[1449]84        void Reset()
[1288]85        {
[1449]86                mGlobalCostMisses = 0;
[1624]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;
[1288]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
[1237]108typedef FlexibleHeap<SubdivisionCandidate *> SplitQueue;
109
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{
[1259]134        friend VspTree;
135        friend OspTree;
136        friend BvHierarchy;
[1279]137        friend ViewCellsParseHandlers;
[1259]138
[1237]139public:
[1421]140        /** Constructor with the view space partition tree and
141                the object space hierarchy type as argument.
[1237]142        */
[1421]143        HierarchyManager(const int objectSpaceHierarchyType);
[1279]144        /** Hack: OspTree will copy the content from this kd tree.
145                Only view space hierarchy will be constructed.
146        */
[1421]147        HierarchyManager(KdTree *kdTree);
[1237]148
[1421]149        /** Deletes space partition and view space partition.
150        */
[1286]151        ~HierarchyManager();
152
[1237]153        /** Constructs the view space and object space subdivision from a given set of rays
154                and a set of objects.
155                @param sampleRays the set of sample rays the construction is based on
156                @param objects the set of objects
157        */
[1308]158        void Construct(
[1293]159                const VssRayContainer &sampleRays,
160                const ObjectContainer &objects,
161                AxisAlignedBox3 *forcedViewSpace);
[1237]162
[1259]163        enum
164        {
165                NO_OBJ_SUBDIV,
166                KD_BASED_OBJ_SUBDIV,
167                BV_BASED_OBJ_SUBDIV
168        };
[1237]169
[1370]170        enum
171        {
172                NO_VIEWSPACE_SUBDIV,
173                KD_BASED_VIEWSPACE_SUBDIV
174        };
175
[1259]176        /** The type of object space subdivison
177        */
[1370]178        int GetObjectSpaceSubdivisionType() const;     
179        /** The type of view space space subdivison
180        */
181        int GetViewSpaceSubdivisionType() const;
182        /** Sets a pointer to the view cells manager.
183        */             
[1279]184        void SetViewCellsManager(ViewCellsManager *vcm);
[1370]185        /** Sets a pointer to the view cells tree.
186        */
[1279]187        void SetViewCellsTree(ViewCellsTree *vcTree);
[1370]188        /** Exports the object hierarchy to disc.
189        */
[1279]190        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
[1370]191        /** Adds a sample to the pvs of the specified view cell.
192        */
[1279]193        bool AddSampleToPvs(
194                Intersectable *obj,
195                const Vector3 &hitPoint,
196                ViewCell *vc,
197                const float pdf,
198                float &contribution) const;
199
[1421]200        /** Print out statistics.
201        */
202        void PrintHierarchyStatistics(ostream &stream) const;
[1279]203
[1421]204        /** Returns the view space partition tree.
205        */
[1379]206        VspTree *GetVspTree();
[1279]207
[1421]208        /** Returns view space bounding box.
209        */
[1563]210        //AxisAlignedBox3 GetViewSpaceBox() const;
[1624]211
[1421]212        /** Returns object space bounding box.
213        */
[1416]214        AxisAlignedBox3 GetObjectSpaceBox() const;
[1379]215
[1421]216        /** Exports object space hierarchy for visualization.
217        */
[1287]218        void ExportObjectSpaceHierarchy(
219                Exporter *exporter,
[1416]220                const ObjectContainer &objects,
[1418]221                const AxisAlignedBox3 *bbox,
222                const bool exportBounds = true) const;
[1279]223
[1421]224        /** Returns intersectable pierced by this ray.
225        */
[1418]226        Intersectable *GetIntersectable(
227                const VssRay &ray,
228                const bool isTermination) const;
229
[1419]230        friend ostream &operator<<(ostream &s, const HierarchyManager &hm)
231        {
[1421]232                hm.PrintHierarchyStatistics(s);
[1419]233                return s;
234        }
235
[1614]236        void ExportBoundingBoxes(OUT_STREAM &stream, const ObjectContainer &objects);
[1421]237
[1614]238
[1237]239protected:
240
241        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
242
243        /** Prepare construction of the hierarchies, set parameters, compute
244                first split candidates.
245        */
[1308]246        void PrepareObjectSpaceSubdivision(
[1237]247                const VssRayContainer &sampleRays,
[1308]248                const ObjectContainer &objects);
[1237]249
[1311]250        void RunConstruction(
[1449]251                const bool repairQueue,
[1311]252                const VssRayContainer &sampleRays,
253                const ObjectContainer &objects,
254                AxisAlignedBox3 *forcedViewSpace);
[1449]255       
256        void RunConstruction(const bool repairQueue);
257               
[1580]258        /** Evaluates the subdivision candidate and executes the split.
259        */
260        bool ApplySubdivisionCandidate(SubdivisionCandidate *sc, const bool repairQueue);
[1237]261
262        bool FinishedConstruction() const;
263
264        SubdivisionCandidate *NextSubdivisionCandidate();
265
[1580]266        /** Repairs the dirty entries of the candidate queue.
267        */
[1624]268        void RepairQueue(SubdivisionCandidate *sc);
[1237]269
[1624]270        /** Collect the list of dirty candidates after the current
271                subdivision candidate split.
[1580]272        */
[1624]273        void CollectDirtyCandidates(
274                SubdivisionCandidate *sc,
275                vector<SubdivisionCandidate *> &dirtyList);
[1237]276
[1580]277        /** Evaluate subdivision stats for log.
278        */
[1624]279        void EvalSubdivisionStats();
[1237]280
[1259]281        void AddSubdivisionStats(
282                const int splits,
[1237]283                const float renderCostDecr,
[1576]284                const float totalRenderCost,
285                const int totalPvsEntries);
[1237]286
[1624]287        bool AddSampleToPvs(
288                Intersectable *obj,
289                const float pdf,
290                float &contribution) const;
[1237]291
[1624]292        void CollectViewSpaceDirtyList(
293                SubdivisionCandidate *sc,
294                SubdivisionCandidateContainer &dirtyList);
[1259]295
[1624]296        void CollectObjectSpaceDirtyList(
297                SubdivisionCandidate *sc,
298                SubdivisionCandidateContainer &dirtyList);
[1259]299               
[1624]300        void ExportOspTree(
301                Exporter *exporter,
302                const ObjectContainer &objects) const;
[1259]303
[1418]304        void ParseEnvironment();
[1415]305
[1418]306        bool StartObjectSpaceSubdivision() const;
307        bool StartViewSpaceSubdivision() const;
308
[1308]309        void PrepareBvHierarchy(
[1286]310                const VssRayContainer &sampleRays,
[1287]311                const ObjectContainer &objects);
[1286]312
[1308]313        void PrepareOspTree(
[1286]314                const VssRayContainer &sampleRays,
[1287]315                const ObjectContainer &objects);
[1286]316
[1311]317        void PrepareViewSpaceSubdivision(
318                const VssRayContainer &sampleRays,
[1379]319                const ObjectContainer &objects);
[1308]320
[1313]321        bool ObjectSpaceSubdivisionConstructed() const;
[1329]322        bool ViewSpaceSubdivisionConstructed() const;
[1311]323
[1313]324    void ResetQueue();
325
[1418]326        void FinishObjectSpaceSubdivision(const ObjectContainer &objects) const;
[1313]327
[1370]328        int GetObjectSpaceSubdivisionDepth() const;
329
[1449]330        void ConstructInterleaved(
331                const VssRayContainer &sampleRays,
332                const ObjectContainer &objects,
333                AxisAlignedBox3 *forcedViewSpace);
334
[1624]335        void ConstructInterleaved2(
336                const VssRayContainer &sampleRays,
337                const ObjectContainer &objects,
338                AxisAlignedBox3 *forcedViewSpace);
339
[1548]340        /** Use iteration to construct the object space hierarchy.
341        */
[1449]342        void ConstructMultiLevel(
343                const VssRayContainer &sampleRays,
344                const ObjectContainer &objects,
345                AxisAlignedBox3 *forcedViewSpace);
346
[1548]347        /** Reset the object space subdivision.
348                E.g., deletes hierarchy and resets stats.
349                so construction can be restarted.
350        */
351        void ResetObjectSpaceSubdivision(
352                const VssRayContainer &rays,
353                const ObjectContainer &objects);
[1449]354
[1557]355        void HierarchyManager::ResetViewSpaceSubdivision(
356                const VssRayContainer &rays,
357                const ObjectContainer &objects);
358
[1614]359
[1237]360protected:
361
[1323]362        enum {SEQUENTIAL, INTERLEAVED};
[1580]363        /// type of hierarchy construction
364        int mConstructionType;
365
366        /// Type of object space partition
[1308]367        int mObjectSpaceSubdivisionType;
[1580]368        /// Type of view space partition
[1329]369    int mViewSpaceSubdivisionType;
370
[1589]371        /// the traversal queue
372        SplitQueue mTQueue;
373       
[1580]374        ////////////
375        //-- helper variables
376       
377        // the original osp type
[1323]378        int mSavedObjectSpaceSubdivisionType;
[1580]379        // the original vsp type
[1329]380        int mSavedViewSpaceSubdivisionType;
[1580]381        /// the current subdivision candidate
[1624]382        //SubdivisionCandidate *mCurrentCandidate;
[1323]383
[1259]384
[1580]385        ///////////////////
386        // Hierarchies
387
388        /// view space hierarchy
[1259]389        VspTree *mVspTree;
[1580]390        /// object space partition kd tree
[1259]391        OspTree *mOspTree;
[1589]392        public:
[1580]393        /// bounding volume hierarchy
[1259]394        BvHierarchy *mBvHierarchy;
[1580]395       
[1589]396protected:
[1237]397
398
[1580]399        //////////
[1576]400        //-- global termination criteria
401
[1580]402        /// the mininal acceptable cost ratio for a split
[1237]403        float mTermMinGlobalCostRatio;
[1580]404        /// the threshold for global cost miss tolerance
[1237]405        int mTermGlobalCostMissTolerance;
[1580]406        /// maximum number of leaves
407        int mTermMaxLeaves;
408
[1576]409        ////////////////////
410
[1237]411        /// keeps track of cost during subdivision
[1624]412        //float mTotalCost;
[1580]413        /// statistics about the hierarchy
[1288]414        HierarchyStatistics mHierarchyStats;
415
[1308]416        int mMinDepthForObjectSpaceSubdivion;
[1370]417        int mMinDepthForViewSpaceSubdivion;
[1580]418       
[1624]419        int mMinRenderCostDecrease;
420
[1237]421        ofstream mSubdivisionStats;
[1314]422
[1580]423        /// if the queue should be repaired after a subdivision steps
[1314]424        bool mRepairQueue;
[1370]425
426        bool mStartWithObjectSpace;
[1580]427        /** if multi level construction method should be used
428                where we iterate over both hierarchies until we
429                converge to the optimum.
430        */
[1449]431        bool mUseMultiLevelConstruction;
[1580]432        /// number of iteration steps for multilevel approach   
433        int mNumMultiLevels;
[1237]434};
435
436}
437
438#endif
Note: See TracBrowser for help on using the repository browser.