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

Revision 2695, 18.4 KB checked in by mattausch, 16 years ago (diff)

debug version: problematic view points for vienna detected

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