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

Revision 1732, 17.2 KB checked in by mattausch, 18 years ago (diff)

resolved coflicts
improved memory constant

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;
43class ViewCellsParseHandlers;
44
45
46#define COUNT_ORIGIN_OBJECTS 1
47
48/** View space / object space hierarchy statistics.
49*/
50class HierarchyStatistics: public StatisticsBase
51{
52public:
53        /// total number of entries in the pvs
54        int mPvsEntries;
55        /// storage cost in MB
56        float mMemory;
57        /// total number of nodes
58        int mNodes;
59        /// maximal reached depth
60        int mMaxDepth;
61        /// accumulated depth
62        int mAccumDepth;
63        /// time spent for queue repair
64        float mRepairTime;
65
66        // global cost ratio violations
67        int mGlobalCostMisses;
68        /// total cost of subdivision
69        float mTotalCost;
70        /// render cost decrease of subdivision
71        float mRenderCostDecrease;
72
73        // Constructor
74        HierarchyStatistics()
75        {
76                Reset();
77        }
78
79        int Nodes() const {return mNodes;}
80        int Interior() const { return mNodes / 2 - 1; }
81        int Leaves() const { return (mNodes / 2) + 1; }
82       
83        // TODO: computation wrong
84        double AvgDepth() const { return mAccumDepth / (double)Leaves();}
85
86        void Reset()
87        {
88                mGlobalCostMisses = 0;
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;
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
110class HierarchySubdivisionStats
111{
112public:
113           
114        int mNumSplits;
115               
116        float mRenderCostDecrease;
117
118    float mTotalRenderCost;
119   
120        int mEntriesInPvs;
121   
122        float mMemoryCost;
123       
124        float mFullMemory;
125
126        int mViewSpaceSplits;
127
128        int mObjectSpaceSplits;
129
130
131        float VspOspRatio() const { return (float)mViewSpaceSplits / (float)mObjectSpaceSplits; }
132
133        float FpsPerMb() const { return 1.0f / (mTotalRenderCost * mMemoryCost); }
134
135        HierarchySubdivisionStats() { Reset(); }
136
137        void Reset()
138        {
139                mNumSplits = 0;
140                mRenderCostDecrease = 0;
141                mTotalRenderCost = 0;
142                mEntriesInPvs = 0;
143                mMemoryCost = 0;
144                mFullMemory = 0;
145                mViewSpaceSplits = 0;
146                mObjectSpaceSplits = 0;
147        }
148
149
150        void Print(ostream &app) const;
151
152        friend ostream &operator<<(ostream &s, const HierarchySubdivisionStats &stat)
153        {
154                stat.Print(s);
155                return s;
156        }
157};
158
159
160/** This class implements a structure holding two different hierarchies,
161        one for object space partitioning and one for view space partitioning.
162
163        The object space and the view space are subdivided using a cost heuristics.
164        If an object space split or a view space split is chosen is also evaluated
165        based on the heuristics.
166       
167        The view space heuristics is evaluated by weighting and adding the pvss of the back and
168        front node of each specific split. unlike for the standalone method vspbsp tree,
169        the pvs of an object would not be the pvs of single object but that of all objects
170        which are contained in the same leaf of the object subdivision. This could be done
171        by storing the pointer to the object space partition parent, which would allow access to all children.
172        Another possibility is to include traced kd-cells in the ray casing process.
173
174        Accordingly, the object space heuristics is evaluated by storing a pvs of view cells with each object.
175        the contribution to an object to the pvs is the number of view cells it can be seen from.
176
177        @note
178        There is a potential efficiency problem involved in a sense that once a certain type
179        of split is chosen for view space / object space, the candidates for the next split of
180        object space / view space must be reevaluated.
181*/
182class HierarchyManager
183{
184public:
185        /** Constructor with the view space partition tree and
186                the object space hierarchy type as argument.
187        */
188        HierarchyManager(const int objectSpaceHierarchyType);
189        /** Hack: OspTree will copy the content from this kd tree.
190                Only view space hierarchy will be constructed.
191        */
192        HierarchyManager(KdTree *kdTree);
193
194        /** Deletes space partition and view space partition.
195        */
196        ~HierarchyManager();
197
198        /** Constructs the view space and object space subdivision from a given set of rays
199                and a set of objects.
200                @param sampleRays the set of sample rays the construction is based on
201                @param objects the set of objects
202        */
203        void Construct(
204                const VssRayContainer &sampleRays,
205                const ObjectContainer &objects,
206                AxisAlignedBox3 *forcedViewSpace);
207
208        enum
209        {
210                NO_OBJ_SUBDIV,
211                KD_BASED_OBJ_SUBDIV,
212                BV_BASED_OBJ_SUBDIV
213        };
214
215        enum
216        {
217                NO_VIEWSPACE_SUBDIV,
218                KD_BASED_VIEWSPACE_SUBDIV
219        };
220
221        /** The type of object space subdivison
222        */
223        int GetObjectSpaceSubdivisionType() const;     
224        /** The type of view space space subdivison
225        */
226        int GetViewSpaceSubdivisionType() const;
227        /** Sets a pointer to the view cells manager.
228        */             
229        void SetViewCellsManager(ViewCellsManager *vcm);
230        /** Sets a pointer to the view cells tree.
231        */
232        void SetViewCellsTree(ViewCellsTree *vcTree);
233        /** Exports the object hierarchy to disc.
234        */
235        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
236        /** Adds a sample to the pvs of the specified view cell.
237        */
238        bool AddSampleToPvs(
239                Intersectable *obj,
240                const Vector3 &hitPoint,
241                ViewCell *vc,
242                const float pdf,
243                float &contribution) const;
244
245        /** Print out statistics.
246        */
247        void PrintHierarchyStatistics(ostream &stream) const;
248
249        /** Returns the view space partition tree.
250        */
251        VspTree *GetVspTree();
252
253        /** Returns view space bounding box.
254        */
255        //AxisAlignedBox3 GetViewSpaceBox() const;
256
257        /** Returns object space bounding box.
258        */
259        AxisAlignedBox3 GetObjectSpaceBox() const;
260
261        /** Exports object space hierarchy for visualization.
262        */
263        void ExportObjectSpaceHierarchy(Exporter *exporter,
264                                                                        const ObjectContainer &objects,
265                                                                        const AxisAlignedBox3 *bbox,
266                                                                        const bool exportBounds = true) const;
267
268        /** Returns intersectable pierced by this ray.
269        */
270        Intersectable *GetIntersectable(const VssRay &ray, const bool isTermination) const;
271
272        /** Export object space partition bounding boxes.
273        */
274        void ExportBoundingBoxes(OUT_STREAM &stream, const ObjectContainer &objects);
275
276        friend ostream &operator<<(ostream &s, const HierarchyManager &hm)
277        {
278                hm.PrintHierarchyStatistics(s);
279                return s;
280        }
281
282        HierarchyStatistics &GetHierarchyStats() { return mHierarchyStats; };
283
284        inline bool ConsiderMemory() const { return mConsiderMemory; }
285        //inline float GetMemoryConst() const { return mMemoryConst; }
286
287       
288        void EvaluateSubdivision(const VssRayContainer &sampleRays,                                                                                     
289                                                         const ObjectContainer &objects,
290                                                         const string &filename);
291
292        void EvaluateSubdivision2(ofstream &splitsStats,
293                                                          const int splitsStepSize);
294
295
296        void CollectObjects(const AxisAlignedBox3 &box, ObjectContainer &objects);
297
298        float mInitialRenderCost;
299
300
301protected:
302
303        /** Returns true if the global termination criteria were met.
304        */
305        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
306
307        /** Prepare construction of the hierarchies, set parameters, compute
308                first split candidates.
309        */
310        SubdivisionCandidate *PrepareObjectSpaceSubdivision(const VssRayContainer &sampleRays,
311                                                                                                                const ObjectContainer &objects);
312
313
314        /** Create bounding box and root.
315        */
316        void InitialiseObjectSpaceSubdivision(const ObjectContainer &objects);
317
318        /** Returns memory usage of object space hierarchy.
319        */
320        float GetObjectSpaceMemUsage() const;
321
322
323        //////////////////////////////
324        // the main loop
325
326        /** This is for interleaved construction / sequential construction.
327        */
328        void RunConstruction(const bool repairQueue,
329                                                 const VssRayContainer &sampleRays,
330                                                 const ObjectContainer &objects,
331                                                 AxisAlignedBox3 *forcedViewSpace);
332       
333        /** This is for interleaved construction using some objects
334                and some view space splits.
335        */
336        int RunConstruction(SplitQueue &splitQueue,
337                                                SubdivisionCandidateContainer &chosenCandidates,
338                                                //const float minRenderCostDecr,
339                                                SubdivisionCandidate *oldCandidate,
340                                                const int minSteps,
341                                                const int maxSteps);
342
343        /** Default subdivision method.
344        */
345        void RunConstruction(const bool repairQueue);
346               
347        ////////////////////////////////////////////////
348
349        /** Evaluates the subdivision candidate and executes the split.
350        */
351        bool ApplySubdivisionCandidate(SubdivisionCandidate *sc,
352                                                                   SplitQueue &splitQueue,
353                                                                   const bool repairQueue);
354
355        /** Tests if hierarchy construction is finished.
356        */
357        bool FinishedConstruction() const;
358
359        /** Returns next subdivision candidate from the split queue.
360        */
361        SubdivisionCandidate *NextSubdivisionCandidate(SplitQueue &splitQueue);
362
363        /** Repairs the dirty entries of the subdivision candidate queue. The
364                list of entries is given in the dirty list.
365        */
366        void RepairQueue(const SubdivisionCandidateContainer &dirtyList,
367                                         SplitQueue &splitQueue,
368                                         const bool recomputeSplitPlaneOnRepair);
369
370        /** Collect subdivision candidates which were affected by the splits from the
371                chosenCandidates list.
372        */
373        void CollectDirtyCandidates(const SubdivisionCandidateContainer &chosenCandidates,
374                                                                SubdivisionCandidateContainer &dirtyList);
375
376        /** Evaluate subdivision stats for log.
377        */
378        void EvalSubdivisionStats();
379
380        void AddSubdivisionStats(const int splits,
381                                                         const float renderCostDecr,
382                                                         const float totalRenderCost,
383                                                         const int totalPvsEntries,
384                                                         const float memory,
385                                                         const float renderCostPerStorage,
386                                                         const float vspOspRatio);
387
388        bool AddSampleToPvs(Intersectable *obj,
389                                                const float pdf,
390                                                float &contribution) const;
391
392        /** Collect affected view space candidates.
393        */
394        void CollectViewSpaceDirtyList(SubdivisionCandidate *sc,
395                                                                   SubdivisionCandidateContainer &dirtyList);
396
397        /** Collect affected object space candidates.
398        */
399        void CollectObjectSpaceDirtyList(SubdivisionCandidate *sc,
400                                                                         SubdivisionCandidateContainer &dirtyList);
401               
402        /** Export object space partition tree.
403        */
404        void ExportOspTree(Exporter *exporter,
405                                           const ObjectContainer &objects) const;
406
407        /** Parse the environment variables.
408        */
409        void ParseEnvironment();
410
411        bool StartObjectSpaceSubdivision() const;
412        bool StartViewSpaceSubdivision() const;
413
414
415        ////////////////////////////
416        // Helper function for preparation of subdivision
417
418        /** Prepare bv hierarchy for subdivision
419        */
420        SubdivisionCandidate *PrepareBvHierarchy(const VssRayContainer &sampleRays,
421                                                                           const ObjectContainer &objects);
422
423        /** Prepare object space kd tree for subdivision.
424        */
425        SubdivisionCandidate *PrepareOspTree(const VssRayContainer &sampleRays,
426                                                                   const ObjectContainer &objects);
427
428        /** Prepare view space subdivision and add candidate to queue.
429        */
430        SubdivisionCandidate *PrepareViewSpaceSubdivision(const VssRayContainer &sampleRays,
431                                                                                                          const ObjectContainer &objects);
432
433        /** Was object space subdivision already constructed?
434        */
435        bool ObjectSpaceSubdivisionConstructed() const;
436       
437        /** Was view space subdivision already constructed?
438        */
439        bool ViewSpaceSubdivisionConstructed() const;
440
441        /** Reset the split queue, i.e., reevaluate the split candidates.
442        */
443    void ResetQueue(SplitQueue &splitQueue, const bool recomputeSplitPlane);
444
445        /** After the suddivision has ended, do some final tasks.
446        */
447        void FinishObjectSpaceSubdivision(const ObjectContainer &objects,
448                                                                          const bool removeRayRefs = true) const;
449
450        /** Returns depth of object space subdivision.
451        */
452        int GetObjectSpaceSubdivisionDepth() const;
453
454        /** Returns number of leaves in object space subdivision.
455        */
456        int GetObjectSpaceSubdivisionLeaves() const;
457        int GetObjectSpaceSubdivisionNodes() const;
458
459        /** Construct object space partition interleaved with view space partition.
460                Each time the best object or view space candidate is selected
461                for the next split.
462        */
463        void ConstructInterleaved(const VssRayContainer &sampleRays,
464                                                          const ObjectContainer &objects,
465                                                          AxisAlignedBox3 *forcedViewSpace);
466
467        /** Construct object space partition interleaved with view space partition.
468                The method chooses a number candidates of each type for subdivision.
469                The number is determined by the "gradient", i.e., the render cost decrease.
470                Once this render cost decrease is lower than the render cost decrease
471                for the splits of previous type, the method will stop current subdivision and
472                evaluate if view space or object space would be the beneficial for the
473                next number of split.
474        */
475        void ConstructInterleavedWithGradient(const VssRayContainer &sampleRays,
476                                                                                  const ObjectContainer &objects,
477                                                                                  AxisAlignedBox3 *forcedViewSpace);
478
479        /** Use iteration to construct the object space hierarchy.
480        */
481        void ConstructMultiLevel(const VssRayContainer &sampleRays,
482                                                         const ObjectContainer &objects,
483                                                         AxisAlignedBox3 *forcedViewSpace);
484
485        /** Based on a given subdivision, we try to optimize using an
486                multiple iteration over view and object space.
487        */
488        void OptimizeMultiLevel(const VssRayContainer &sampleRays,                                                                                       
489                                                        const ObjectContainer &objects,
490                                                        AxisAlignedBox3 *forcedViewSpace);
491
492        /** Reset the object space subdivision.
493                E.g., deletes hierarchy and resets stats.
494                so construction can be restarted.
495        */
496        SubdivisionCandidate *ResetObjectSpaceSubdivision(const VssRayContainer &rays,
497                                                                                                          const ObjectContainer &objects);
498
499        SubdivisionCandidate *ResetViewSpaceSubdivision(const VssRayContainer &rays,
500                                                                                                        const ObjectContainer &objects,
501                                                                                                        AxisAlignedBox3 *forcedViewSpace);
502
503
504        ///////////////////////////
505
506        void ExportStats(ofstream &stats, SplitQueue &tQueue, const ObjectContainer &objects);
507
508        void CollectBestSet(const int maxSplits,
509                                                const float maxMemoryCost,
510                                                ViewCellContainer &viewCells,
511                                                vector<BvhNode *> &bvhNodes);
512
513        int ExtractStatistics(const int maxSplits,
514                                                  const float maxMemoryCost,
515                                                  float &renderCost,
516                                                  float &memory,
517                                                  int &pvsEntries,
518                                                  int &viewSpaceSplits,
519                                                  int &objectSpaceSplits);
520
521
522protected:
523
524        /** construction types
525                sequential: construct first view space, then object space
526                interleaved: construct view space and object space fully interleaved
527                gradient: construct view space / object space until a threshold is reached
528                multilevel: iterate until subdivisions converge to the optimum.
529        */
530        enum {SEQUENTIAL, INTERLEAVED, GRADIENT, MULTILEVEL};
531
532        /// type of hierarchy construction
533        int mConstructionType;
534
535        /// Type of object space partition
536        int mObjectSpaceSubdivisionType;
537        /// Type of view space partition
538    int mViewSpaceSubdivisionType;
539
540        /// the traversal queue
541        SplitQueue mTQueue;
542       
543        ////////////
544        //-- helper variables
545       
546        // the original osp type
547        int mSavedObjectSpaceSubdivisionType;
548        // the original vsp type
549        int mSavedViewSpaceSubdivisionType;
550        /// the current subdivision candidate
551        //SubdivisionCandidate *mCurrentCandidate;
552
553
554        ///////////////////
555        // Hierarchies
556
557        /// view space hierarchy
558        VspTree *mVspTree;
559        /// object space partition kd tree
560        OspTree *mOspTree;
561
562        public:
563        /// bounding volume hierarchy
564        BvHierarchy *mBvHierarchy;
565       
566protected:
567
568
569        //////////
570        //-- global termination criteria
571
572        /// the mininal acceptable cost ratio for a split
573        float mTermMinGlobalCostRatio;
574        /// the threshold for global cost miss tolerance
575        int mTermGlobalCostMissTolerance;
576        /// maximum number of leaves
577        int mTermMaxLeaves;
578        /// Maximal allowed memory consumption.
579        float mTermMaxMemory;
580
581
582        ////////////////////
583
584        /// number of minimal steps of the same type
585        int mMinStepsOfSameType;
586        int mMaxStepsOfSameType;
587
588        /// statistics about the hierarchy
589        HierarchyStatistics mHierarchyStats;
590
591        int mMinDepthForObjectSpaceSubdivion;
592        int mMinDepthForViewSpaceSubdivion;
593       
594        //int mMinRenderCostDecrease;
595
596        ofstream mSubdivisionStats;
597
598        /// if the queue should be repaired after a subdivision steps
599        bool mRepairQueue;
600
601        bool mStartWithObjectSpace;
602        /** if multi level construction method should be used
603                where we iterate over both hierarchies until we
604                converge to the optimum.
605        */
606        bool mUseMultiLevelConstruction;
607
608        /// number of iteration steps for multilevel approach   
609        int mNumMultiLevels;
610
611        /** if split plane should be recomputed for the repair.
612                Otherwise only the priority is recomputed, the
613                split plane itself stays the same
614        */
615        bool mRecomputeSplitPlaneOnRepair;
616
617        /** If memory should be considered during choosing
618                of the next split type during gradient method.
619        */
620        bool mConsiderMemory;
621
622        bool mConsiderMemory2;
623
624        int mMaxRepairs;
625
626        int mTimeStamp;
627        friend VspTree;
628        friend OspTree;
629        friend BvHierarchy;
630        friend ViewCellsParseHandlers;
631
632};
633
634}
635
636#endif
Note: See TracBrowser for help on using the repository browser.