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

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