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

Revision 1614, 10.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;
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 pvsEntries;
53        /// storage cost
54        int memory;
55        /// total number of nodes
56        int nodes;
57        /// maximal reached depth
58        int maxDepth;
59        /// accumulated depth
60        int accumDepth;
61        /// time spent for queue repair
62        float repairTime;
63        // global cost ratio violations
64        int mGlobalCostMisses;
65
66        // Constructor
67        HierarchyStatistics()
68        {
69                Reset();
70        }
71
72        int Nodes() const {return nodes;}
73        int Interior() const { return nodes / 2; }
74        int Leaves() const { return (nodes / 2) + 1; }
75       
76        // TODO: computation wrong
77        double AvgDepth() const { return accumDepth / (double)Leaves();}
78
79        void Reset()
80        {
81                mGlobalCostMisses = 0;
82                nodes = 0;
83                maxDepth = 0;
84                accumDepth = 0;
85                repairTime = 0;
86                memory = 0;
87                pvsEntries = 0;
88        }
89
90        void Print(ostream &app) const;
91
92        friend ostream &operator<<(ostream &s, const HierarchyStatistics &stat)
93        {
94                stat.Print(s);
95                return s;
96        }
97};
98
99
100typedef FlexibleHeap<SubdivisionCandidate *> SplitQueue;
101
102/** This class implements a structure holding two different hierarchies,
103        one for object space partitioning and one for view space partitioning.
104
105        The object space and the view space are subdivided using a cost heuristics.
106        If an object space split or a view space split is chosen is also evaluated
107        based on the heuristics.
108       
109        The view space heuristics is evaluated by weighting and adding the pvss of the back and
110        front node of each specific split. unlike for the standalone method vspbsp tree,
111        the pvs of an object would not be the pvs of single object but that of all objects
112        which are contained in the same leaf of the object subdivision. This could be done
113        by storing the pointer to the object space partition parent, which would allow access to all children.
114        Another possibility is to include traced kd-cells in the ray casing process.
115
116        Accordingly, the object space heuristics is evaluated by storing a pvs of view cells with each object.
117        the contribution to an object to the pvs is the number of view cells it can be seen from.
118
119        @note
120        There is a potential efficiency problem involved in a sense that once a certain type
121        of split is chosen for view space / object space, the candidates for the next split of
122        object space / view space must be reevaluated.
123*/
124class HierarchyManager
125{
126        friend VspTree;
127        friend OspTree;
128        friend BvHierarchy;
129        friend ViewCellsParseHandlers;
130
131public:
132        /** Constructor with the view space partition tree and
133                the object space hierarchy type as argument.
134        */
135        HierarchyManager(const int objectSpaceHierarchyType);
136        /** Hack: OspTree will copy the content from this kd tree.
137                Only view space hierarchy will be constructed.
138        */
139        HierarchyManager(KdTree *kdTree);
140
141        /** Deletes space partition and view space partition.
142        */
143        ~HierarchyManager();
144
145        /** Constructs the view space and object space subdivision from a given set of rays
146                and a set of objects.
147                @param sampleRays the set of sample rays the construction is based on
148                @param objects the set of objects
149        */
150        void Construct(
151                const VssRayContainer &sampleRays,
152                const ObjectContainer &objects,
153                AxisAlignedBox3 *forcedViewSpace);
154
155        enum
156        {
157                NO_OBJ_SUBDIV,
158                KD_BASED_OBJ_SUBDIV,
159                BV_BASED_OBJ_SUBDIV
160        };
161
162        enum
163        {
164                NO_VIEWSPACE_SUBDIV,
165                KD_BASED_VIEWSPACE_SUBDIV
166        };
167
168        /** The type of object space subdivison
169        */
170        int GetObjectSpaceSubdivisionType() const;     
171        /** The type of view space space subdivison
172        */
173        int GetViewSpaceSubdivisionType() const;
174        /** Sets a pointer to the view cells manager.
175        */             
176        void SetViewCellsManager(ViewCellsManager *vcm);
177        /** Sets a pointer to the view cells tree.
178        */
179        void SetViewCellsTree(ViewCellsTree *vcTree);
180        /** Exports the object hierarchy to disc.
181        */
182        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
183        /** Adds a sample to the pvs of the specified view cell.
184        */
185        bool AddSampleToPvs(
186                Intersectable *obj,
187                const Vector3 &hitPoint,
188                ViewCell *vc,
189                const float pdf,
190                float &contribution) const;
191
192        /** Print out statistics.
193        */
194        void PrintHierarchyStatistics(ostream &stream) const;
195
196        /** Returns the view space partition tree.
197        */
198        VspTree *GetVspTree();
199
200        /** Returns view space bounding box.
201        */
202        //AxisAlignedBox3 GetViewSpaceBox() const;
203        /** Returns object space bounding box.
204        */
205        AxisAlignedBox3 GetObjectSpaceBox() const;
206
207        /** Exports object space hierarchy for visualization.
208        */
209        void ExportObjectSpaceHierarchy(
210                Exporter *exporter,
211                const ObjectContainer &objects,
212                const AxisAlignedBox3 *bbox,
213                const bool exportBounds = true) const;
214
215        /** Returns intersectable pierced by this ray.
216        */
217        Intersectable *GetIntersectable(
218                const VssRay &ray,
219                const bool isTermination) const;
220
221        friend ostream &operator<<(ostream &s, const HierarchyManager &hm)
222        {
223                hm.PrintHierarchyStatistics(s);
224                return s;
225        }
226
227        void ExportBoundingBoxes(OUT_STREAM &stream, const ObjectContainer &objects);
228
229
230protected:
231
232        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
233
234        /** Prepare construction of the hierarchies, set parameters, compute
235                first split candidates.
236        */
237        void PrepareObjectSpaceSubdivision(
238                const VssRayContainer &sampleRays,
239                const ObjectContainer &objects);
240
241        void RunConstruction(
242                const bool repairQueue,
243                const VssRayContainer &sampleRays,
244                const ObjectContainer &objects,
245                AxisAlignedBox3 *forcedViewSpace);
246       
247        void RunConstruction(const bool repairQueue);
248               
249        /** Evaluates the subdivision candidate and executes the split.
250        */
251        bool ApplySubdivisionCandidate(SubdivisionCandidate *sc, const bool repairQueue);
252
253        bool FinishedConstruction() const;
254
255        SubdivisionCandidate *NextSubdivisionCandidate();
256
257        /** Repairs the dirty entries of the candidate queue.
258        */
259        void RepairQueue();
260
261        /** Collect the list of dirty candidates after the current subdivision candidate
262                split.
263        */
264        void CollectDirtyCandidates(vector<SubdivisionCandidate *> &dirtyList);
265
266        /** Evaluate subdivision stats for log.
267        */
268        void EvalSubdivisionStats(const float renderCostDecr);
269
270        void AddSubdivisionStats(
271                const int splits,
272                const float renderCostDecr,
273                const float totalRenderCost,
274                const int totalPvsEntries);
275
276        void CollectObjectSpaceDirtyList();
277        void CollectViewSpaceDirtyList();
278
279        bool AddSampleToPvs(Intersectable *obj,
280                                                const float pdf,
281                                                float &contribution) const;
282
283        void CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
284        void CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
285               
286        void ExportOspTree(Exporter *exporter, const ObjectContainer &objects) const;
287
288        void ParseEnvironment();
289
290        bool StartObjectSpaceSubdivision() const;
291        bool StartViewSpaceSubdivision() const;
292
293        void PrepareBvHierarchy(
294                const VssRayContainer &sampleRays,
295                const ObjectContainer &objects);
296
297        void PrepareOspTree(
298                const VssRayContainer &sampleRays,
299                const ObjectContainer &objects);
300
301        void PrepareViewSpaceSubdivision(
302                const VssRayContainer &sampleRays,
303                const ObjectContainer &objects);
304
305        bool ObjectSpaceSubdivisionConstructed() const;
306        bool ViewSpaceSubdivisionConstructed() const;
307
308    void ResetQueue();
309
310        void FinishObjectSpaceSubdivision(const ObjectContainer &objects) const;
311
312        int GetObjectSpaceSubdivisionDepth() const;
313
314        void ConstructInterleaved(
315                const VssRayContainer &sampleRays,
316                const ObjectContainer &objects,
317                AxisAlignedBox3 *forcedViewSpace);
318
319        /** Use iteration to construct the object space hierarchy.
320        */
321        void ConstructMultiLevel(
322                const VssRayContainer &sampleRays,
323                const ObjectContainer &objects,
324                AxisAlignedBox3 *forcedViewSpace);
325
326        /** Reset the object space subdivision.
327                E.g., deletes hierarchy and resets stats.
328                so construction can be restarted.
329        */
330        void ResetObjectSpaceSubdivision(
331                const VssRayContainer &rays,
332                const ObjectContainer &objects);
333
334        void HierarchyManager::ResetViewSpaceSubdivision(
335                const VssRayContainer &rays,
336                const ObjectContainer &objects);
337
338
339protected:
340
341        enum {SEQUENTIAL, INTERLEAVED};
342        /// type of hierarchy construction
343        int mConstructionType;
344
345        /// Type of object space partition
346        int mObjectSpaceSubdivisionType;
347        /// Type of view space partition
348    int mViewSpaceSubdivisionType;
349
350        /// the traversal queue
351        SplitQueue mTQueue;
352       
353        ////////////
354        //-- helper variables
355       
356        // the original osp type
357        int mSavedObjectSpaceSubdivisionType;
358        // the original vsp type
359        int mSavedViewSpaceSubdivisionType;
360        /// the current subdivision candidate
361        SubdivisionCandidate *mCurrentCandidate;
362
363
364        ///////////////////
365        // Hierarchies
366
367        /// view space hierarchy
368        VspTree *mVspTree;
369        /// object space partition kd tree
370        OspTree *mOspTree;
371        public:
372        /// bounding volume hierarchy
373        BvHierarchy *mBvHierarchy;
374       
375protected:
376
377
378        //////////
379        //-- global termination criteria
380
381        /// the mininal acceptable cost ratio for a split
382        float mTermMinGlobalCostRatio;
383        /// the threshold for global cost miss tolerance
384        int mTermGlobalCostMissTolerance;
385        /// maximum number of leaves
386        int mTermMaxLeaves;
387
388        ////////////////////
389
390        /// keeps track of cost during subdivision
391        float mTotalCost;
392        /// statistics about the hierarchy
393        HierarchyStatistics mHierarchyStats;
394
395        int mMinDepthForObjectSpaceSubdivion;
396        int mMinDepthForViewSpaceSubdivion;
397       
398        ofstream mSubdivisionStats;
399
400        /// if the queue should be repaired after a subdivision steps
401        bool mRepairQueue;
402
403        bool mStartWithObjectSpace;
404        /** if multi level construction method should be used
405                where we iterate over both hierarchies until we
406                converge to the optimum.
407        */
408        bool mUseMultiLevelConstruction;
409        /// number of iteration steps for multilevel approach   
410        int mNumMultiLevels;
411};
412
413}
414
415#endif
Note: See TracBrowser for help on using the repository browser.