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

Revision 1473, 9.1 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#if 0
45template <typename T> class GtPriority
46{
47public:
48        bool operator() (const T c1, const T c2) const
49        {
50                //return false;
51                return c1->GetPriority() < c2->GetPriority();
52        }
53};
54
55typedef std::priority_queue<SubdivisionCandidate *,
56                                                        std::vector<SubdivisionCandidate *>,
57                                                        GtPriority<std::vector<SubdivisionCandidate *>::value_type> > SplitQueue;
58#endif
59
60
61
62/** View space partition statistics.
63*/
64class HierarchyStatistics: public StatisticsBase
65{
66public:
67
68        /// total number of nodes
69        int nodes;
70        /// maximal reached depth
71        int maxDepth;
72        /// accumulated depth
73        int accumDepth;
74        /// time spent for queue repair
75        float repairTime;
76        // global cost ratio violations
77        int mGlobalCostMisses;
78
79        // Constructor
80        HierarchyStatistics()
81        {
82                Reset();
83        }
84
85        int Nodes() const {return nodes;}
86        int Interior() const { return nodes / 2; }
87        int Leaves() const { return (nodes / 2) + 1; }
88       
89        // TODO: computation wrong
90        double AvgDepth() const { return accumDepth / (double)Leaves();}
91
92        void Reset()
93        {
94                mGlobalCostMisses = 0;
95                nodes = 0;
96                maxDepth = 0;
97                accumDepth = 0;
98                repairTime = 0;
99        }
100
101        void Print(ostream &app) const;
102
103        friend ostream &operator<<(ostream &s, const HierarchyStatistics &stat)
104        {
105                stat.Print(s);
106                return s;
107        }
108};
109
110
111typedef FlexibleHeap<SubdivisionCandidate *> SplitQueue;
112
113/** This class implements a structure holding two different hierarchies,
114        one for object space partitioning and one for view space partitioning.
115
116        The object space and the view space are subdivided using a cost heuristics.
117        If an object space split or a view space split is chosen is also evaluated
118        based on the heuristics.
119       
120        The view space heuristics is evaluated by weighting and adding the pvss of the back and
121        front node of each specific split. unlike for the standalone method vspbsp tree,
122        the pvs of an object would not be the pvs of single object but that of all objects
123        which are contained in the same leaf of the object subdivision. This could be done
124        by storing the pointer to the object space partition parent, which would allow access to all children.
125        Another possibility is to include traced kd-cells in the ray casing process.
126
127        Accordingly, the object space heuristics is evaluated by storing a pvs of view cells with each object.
128        the contribution to an object to the pvs is the number of view cells it can be seen from.
129
130        @note
131        There is a potential efficiency problem involved in a sense that once a certain type
132        of split is chosen for view space / object space, the candidates for the next split of
133        object space / view space must be reevaluated.
134*/
135class HierarchyManager
136{
137        friend VspTree;
138        friend OspTree;
139        friend BvHierarchy;
140        friend ViewCellsParseHandlers;
141
142public:
143        /** Constructor with the view space partition tree and
144                the object space hierarchy type as argument.
145        */
146        HierarchyManager(const int objectSpaceHierarchyType);
147        /** Hack: OspTree will copy the content from this kd tree.
148                Only view space hierarchy will be constructed.
149        */
150        HierarchyManager(KdTree *kdTree);
151
152        /** Deletes space partition and view space partition.
153        */
154        ~HierarchyManager();
155
156        /** Constructs the view space and object space subdivision from a given set of rays
157                and a set of objects.
158                @param sampleRays the set of sample rays the construction is based on
159                @param objects the set of objects
160        */
161        void Construct(
162                const VssRayContainer &sampleRays,
163                const ObjectContainer &objects,
164                AxisAlignedBox3 *forcedViewSpace);
165
166        enum
167        {
168                NO_OBJ_SUBDIV,
169                KD_BASED_OBJ_SUBDIV,
170                BV_BASED_OBJ_SUBDIV
171        };
172
173        enum
174        {
175                NO_VIEWSPACE_SUBDIV,
176                KD_BASED_VIEWSPACE_SUBDIV
177        };
178
179        /** The type of object space subdivison
180        */
181        int GetObjectSpaceSubdivisionType() const;     
182        /** The type of view space space subdivison
183        */
184        int GetViewSpaceSubdivisionType() const;
185        /** Sets a pointer to the view cells manager.
186        */             
187        void SetViewCellsManager(ViewCellsManager *vcm);
188        /** Sets a pointer to the view cells tree.
189        */
190        void SetViewCellsTree(ViewCellsTree *vcTree);
191        /** Exports the object hierarchy to disc.
192        */
193        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
194        /** Adds a sample to the pvs of the specified view cell.
195        */
196        bool AddSampleToPvs(
197                Intersectable *obj,
198                const Vector3 &hitPoint,
199                ViewCell *vc,
200                const float pdf,
201                float &contribution) const;
202
203        /** Print out statistics.
204        */
205        void PrintHierarchyStatistics(ostream &stream) const;
206
207        /** Returns the view space partition tree.
208        */
209        VspTree *GetVspTree();
210
211        /** Returns view space bounding box.
212        */
213        AxisAlignedBox3 GetViewSpaceBox() const;
214        /** Returns object space bounding box.
215        */
216        AxisAlignedBox3 GetObjectSpaceBox() const;
217
218        /** Exports object space hierarchy for visualization.
219        */
220        void ExportObjectSpaceHierarchy(
221                Exporter *exporter,
222                const ObjectContainer &objects,
223                const AxisAlignedBox3 *bbox,
224                const bool exportBounds = true) const;
225
226        /** Returns intersectable pierced by this ray.
227        */
228        Intersectable *GetIntersectable(
229                const VssRay &ray,
230                const bool isTermination) const;
231
232        friend ostream &operator<<(ostream &s, const HierarchyManager &hm)
233        {
234                hm.PrintHierarchyStatistics(s);
235                return s;
236        }
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        bool ApplySubdivisionCandidate(SubdivisionCandidate *sc);
259
260        bool FinishedConstruction() const;
261
262        SubdivisionCandidate *NextSubdivisionCandidate();
263
264        void RepairQueue();
265
266        void CollectDirtyCandidates(vector<SubdivisionCandidate *> &dirtyList);
267
268        void EvalSubdivisionStats(const SubdivisionCandidate &tData);
269
270        void AddSubdivisionStats(
271                const int splits,
272                const float renderCostDecr,
273                const float totalRenderCost);
274
275        void CollectObjectSpaceDirtyList();
276        void CollectViewSpaceDirtyList();
277
278        bool AddSampleToPvs(Intersectable *obj,
279                                                const float pdf,
280                                                float &contribution) const;
281
282        void CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
283        void CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
284               
285        void ExportOspTree(Exporter *exporter, const ObjectContainer &objects) const;
286
287        void ParseEnvironment();
288
289        bool StartObjectSpaceSubdivision() const;
290        bool StartViewSpaceSubdivision() const;
291
292        void PrepareBvHierarchy(
293                const VssRayContainer &sampleRays,
294                const ObjectContainer &objects);
295
296        void PrepareOspTree(
297                const VssRayContainer &sampleRays,
298                const ObjectContainer &objects);
299
300        void PrepareViewSpaceSubdivision(
301                const VssRayContainer &sampleRays,
302                const ObjectContainer &objects);
303
304        bool ObjectSpaceSubdivisionConstructed() const;
305        bool ViewSpaceSubdivisionConstructed() const;
306
307    void ResetQueue();
308
309        void FinishObjectSpaceSubdivision(const ObjectContainer &objects) const;
310
311        int GetObjectSpaceSubdivisionDepth() const;
312
313        void ConstructInterleaved(
314                const VssRayContainer &sampleRays,
315                const ObjectContainer &objects,
316                AxisAlignedBox3 *forcedViewSpace);
317
318        void ConstructMultiLevel(
319                const VssRayContainer &sampleRays,
320                const ObjectContainer &objects,
321                AxisAlignedBox3 *forcedViewSpace);
322
323        void ResetObjectSpaceSubdivision(const ObjectContainer &objects);
324
325protected:
326
327        enum {SEQUENTIAL, INTERLEAVED};
328       
329        int mObjectSpaceSubdivisionType;
330    int mViewSpaceSubdivisionType;
331
332        /// the original osp type
333        int mSavedObjectSpaceSubdivisionType;
334        int mSavedViewSpaceSubdivisionType;
335
336        int mConstructionType;
337
338        VspTree *mVspTree;
339        OspTree *mOspTree;
340        BvHierarchy *mBvHierarchy;
341
342        AxisAlignedBox3 mBoundingBox;
343
344        SplitQueue mTQueue;
345
346        SubdivisionCandidate *mCurrentCandidate;
347
348        ////////
349        //-- global criteria
350        float mTermMinGlobalCostRatio;
351        int mTermGlobalCostMissTolerance;
352       
353        /// keeps track of cost during subdivision
354        float mTotalCost;
355
356        HierarchyStatistics mHierarchyStats;
357
358        int mMinDepthForObjectSpaceSubdivion;
359        int mMinDepthForViewSpaceSubdivion;
360
361        int mTermMaxLeaves;
362        ofstream mSubdivisionStats;
363
364        bool mRepairQueue;
365
366        bool mStartWithObjectSpace;
367
368        bool mUseMultiLevelConstruction;
369};
370
371}
372
373#endif
Note: See TracBrowser for help on using the repository browser.