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

Revision 1625, 13.6 KB checked in by mattausch, 18 years ago (diff)

worked on gradient method

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        /** Returns true if the global termination criteria were met.
242        */
243        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
244
245        /** Prepare construction of the hierarchies, set parameters, compute
246                first split candidates.
247        */
248        SubdivisionCandidate *PrepareObjectSpaceSubdivision(const VssRayContainer &sampleRays,
249                                                                                                                const ObjectContainer &objects);
250
251
252        //////////////////////////////
253        // the main loop
254        //////////////////////
255
256        /** This is for interleaved construction / sequential construction.
257        */
258        void RunConstruction(const bool repairQueue,
259                                                 const VssRayContainer &sampleRays,
260                                                 const ObjectContainer &objects,
261                                                 AxisAlignedBox3 *forcedViewSpace);
262       
263        /** This is for interleaved construction using some objects
264                and some view space splits.
265        */
266        int RunConstruction(SplitQueue &splitQueue,
267                                                SubdivisionCandidateContainer &chosenCandidates,
268                                                const float minRenderCostDecr,
269                                                const int maxSteps);
270
271        /** Default subdivision method.
272        */
273        void RunConstruction(const bool repairQueue);
274               
275        ////////////////////////////////////////////////
276
277        /** Evaluates the subdivision candidate and executes the split.
278        */
279        bool ApplySubdivisionCandidate(SubdivisionCandidate *sc,
280                                                                   SplitQueue &splitQueue,
281                                                                   const bool repairQueue);
282
283        /** Tests if hierarchy construction is finished.
284        */
285        bool FinishedConstruction() const;
286
287        /** Returns next subdivision candidate from the split queue.
288        */
289        SubdivisionCandidate *NextSubdivisionCandidate(SplitQueue &splitQueue);
290
291        /** Repairs the dirty entries of the subdivision candidate queue. The
292                list of entries is given in the dirty list.
293        */
294        void RepairQueue(const vector<SubdivisionCandidate *> &dirtyList, SplitQueue &splitQueue);
295
296        /** Collect subdivision candidates which were affected by the splits from the
297                chosenCandidates list.
298        */
299        void CollectDirtyCandidates(const SubdivisionCandidateContainer &chosenCandidates,
300                                                                SubdivisionCandidateContainer &dirtyList);
301
302        /** Collect the list of dirty candidates after the current
303                subdivision candidate split.
304        */
305        void CollectDirtyCandidates(SubdivisionCandidate *sc,
306                                                                vector<SubdivisionCandidate *> &dirtyList);
307
308        /** Evaluate subdivision stats for log.
309        */
310        void EvalSubdivisionStats();
311
312        void AddSubdivisionStats(const int splits,
313                                                         const float renderCostDecr,
314                                                         const float totalRenderCost,
315                                                         const int totalPvsEntries,
316                                                         const int memory,
317                                                         const float renderCostPerStorage);
318
319        bool AddSampleToPvs(Intersectable *obj,
320                                                const float pdf,
321                                                float &contribution) const;
322
323        /** Collect affected view space candidates.
324        */
325        void CollectViewSpaceDirtyList(SubdivisionCandidate *sc,
326                                                                   SubdivisionCandidateContainer &dirtyList);
327
328        /** Collect affected object space candidates.
329        */
330        void CollectObjectSpaceDirtyList(SubdivisionCandidate *sc,
331                                                                         SubdivisionCandidateContainer &dirtyList);
332               
333        /** Export object space partition tree.
334        */
335        void ExportOspTree(Exporter *exporter,
336                                           const ObjectContainer &objects) const;
337
338        /** Parse the environment variables.
339        */
340        void ParseEnvironment();
341
342        bool StartObjectSpaceSubdivision() const;
343        bool StartViewSpaceSubdivision() const;
344
345        ////////////////////////////
346        // Helper function for preparation of subdivision
347        ///////
348
349        /** Prepare bv hierarchy for subdivision
350        */
351        SubdivisionCandidate *PrepareBvHierarchy(const VssRayContainer &sampleRays,
352                                                                           const ObjectContainer &objects);
353
354        /** Prepare object space kd tree for subdivision.
355        */
356        SubdivisionCandidate *PrepareOspTree(const VssRayContainer &sampleRays,
357                                                                   const ObjectContainer &objects);
358
359        /** Prepare view space subdivision and add candidate to queue.
360        */
361        SubdivisionCandidate *PrepareViewSpaceSubdivision(const VssRayContainer &sampleRays,
362                                                                                                          const ObjectContainer &objects);
363
364        /** Was object space subdivision already constructed?
365        */
366        bool ObjectSpaceSubdivisionConstructed() const;
367       
368        /** Was view space subdivision already constructed?
369        */
370        bool ViewSpaceSubdivisionConstructed() const;
371
372        /** Reset the split queue, i.e., reevaluate the split candidates.
373        */
374    void ResetQueue();
375
376        /** After the suddivision has ended, do some final tasks.
377        */
378        void FinishObjectSpaceSubdivision(const ObjectContainer &objects) const;
379
380        /** Returns depth of object space subdivision.
381        */
382        int GetObjectSpaceSubdivisionDepth() const;
383
384        /** Construct object space partition interleaved with view space partition.
385                Each time the best object or view space candidate is selected
386                for the next split.
387        */
388        void ConstructInterleaved(
389                const VssRayContainer &sampleRays,
390                const ObjectContainer &objects,
391                AxisAlignedBox3 *forcedViewSpace);
392
393        /** Construct object space partition interleaved with view space partition.
394                The method chooses a number candidates of each type for subdivision.
395                The number is determined by the "gradient", i.e., the render cost decrease.
396                Once this render cost decrease is lower than the render cost decrease
397                for the splits of previous type, the method will stop current subdivision and
398                evaluate if view space or object space would be the beneficial for the
399                next number of split.
400        */
401        void ConstructInterleaved2(
402                const VssRayContainer &sampleRays,
403                const ObjectContainer &objects,
404                AxisAlignedBox3 *forcedViewSpace);
405
406        /** Use iteration to construct the object space hierarchy.
407        */
408        void ConstructMultiLevel(
409                const VssRayContainer &sampleRays,
410                const ObjectContainer &objects,
411                AxisAlignedBox3 *forcedViewSpace);
412
413        /** Reset the object space subdivision.
414                E.g., deletes hierarchy and resets stats.
415                so construction can be restarted.
416        */
417        SubdivisionCandidate *ResetObjectSpaceSubdivision(const VssRayContainer &rays,
418                                                                                                          const ObjectContainer &objects);
419
420        SubdivisionCandidate *ResetViewSpaceSubdivision(const VssRayContainer &rays,
421                                                                                                        const ObjectContainer &objects);
422
423
424protected:
425
426        enum {SEQUENTIAL, INTERLEAVED};
427        /// type of hierarchy construction
428        int mConstructionType;
429
430        /// Type of object space partition
431        int mObjectSpaceSubdivisionType;
432        /// Type of view space partition
433    int mViewSpaceSubdivisionType;
434
435        /// the traversal queue
436        SplitQueue mTQueue;
437       
438        ////////////
439        //-- helper variables
440       
441        // the original osp type
442        int mSavedObjectSpaceSubdivisionType;
443        // the original vsp type
444        int mSavedViewSpaceSubdivisionType;
445        /// the current subdivision candidate
446        //SubdivisionCandidate *mCurrentCandidate;
447
448
449        ///////////////////
450        // Hierarchies
451
452        /// view space hierarchy
453        VspTree *mVspTree;
454        /// object space partition kd tree
455        OspTree *mOspTree;
456
457        public:
458        /// bounding volume hierarchy
459        BvHierarchy *mBvHierarchy;
460       
461protected:
462
463
464        //////////
465        //-- global termination criteria
466
467        /// the mininal acceptable cost ratio for a split
468        float mTermMinGlobalCostRatio;
469        /// the threshold for global cost miss tolerance
470        int mTermGlobalCostMissTolerance;
471        /// maximum number of leaves
472        int mTermMaxLeaves;
473
474        ////////////////////
475
476        /// statistics about the hierarchy
477        HierarchyStatistics mHierarchyStats;
478
479        int mMinDepthForObjectSpaceSubdivion;
480        int mMinDepthForViewSpaceSubdivion;
481       
482        //int mMinRenderCostDecrease;
483
484        ofstream mSubdivisionStats;
485
486        /// if the queue should be repaired after a subdivision steps
487        bool mRepairQueue;
488
489        bool mStartWithObjectSpace;
490        /** if multi level construction method should be used
491                where we iterate over both hierarchies until we
492                converge to the optimum.
493        */
494        bool mUseMultiLevelConstruction;
495        /// number of iteration steps for multilevel approach   
496        int mNumMultiLevels;
497};
498
499}
500
501#endif
Note: See TracBrowser for help on using the repository browser.