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

Revision 1449, 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       
135*/
136class HierarchyManager
137{
138        friend VspTree;
139        friend OspTree;
140        friend BvHierarchy;
141        friend ViewCellsParseHandlers;
142
143public:
144        /** Constructor with the view space partition tree and
145                the object space hierarchy type as argument.
146        */
147        HierarchyManager(const int objectSpaceHierarchyType);
148        /** Hack: OspTree will copy the content from this kd tree.
149                Only view space hierarchy will be constructed.
150        */
151        HierarchyManager(KdTree *kdTree);
152
153        /** Deletes space partition and view space partition.
154        */
155        ~HierarchyManager();
156
157        /** Constructs the view space and object space subdivision from a given set of rays
158                and a set of objects.
159                @param sampleRays the set of sample rays the construction is based on
160                @param objects the set of objects
161        */
162        void Construct(
163                const VssRayContainer &sampleRays,
164                const ObjectContainer &objects,
165                AxisAlignedBox3 *forcedViewSpace);
166
167        enum
168        {
169                NO_OBJ_SUBDIV,
170                KD_BASED_OBJ_SUBDIV,
171                BV_BASED_OBJ_SUBDIV
172        };
173
174        enum
175        {
176                NO_VIEWSPACE_SUBDIV,
177                KD_BASED_VIEWSPACE_SUBDIV
178        };
179
180        /** The type of object space subdivison
181        */
182        int GetObjectSpaceSubdivisionType() const;     
183        /** The type of view space space subdivison
184        */
185        int GetViewSpaceSubdivisionType() const;
186        /** Sets a pointer to the view cells manager.
187        */             
188        void SetViewCellsManager(ViewCellsManager *vcm);
189        /** Sets a pointer to the view cells tree.
190        */
191        void SetViewCellsTree(ViewCellsTree *vcTree);
192        /** Exports the object hierarchy to disc.
193        */
194        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
195        /** Adds a sample to the pvs of the specified view cell.
196        */
197        bool AddSampleToPvs(
198                Intersectable *obj,
199                const Vector3 &hitPoint,
200                ViewCell *vc,
201                const float pdf,
202                float &contribution) const;
203
204        /** Print out statistics.
205        */
206        void PrintHierarchyStatistics(ostream &stream) const;
207
208        /** Returns the view space partition tree.
209        */
210        VspTree *GetVspTree();
211
212        /** Returns view space bounding box.
213        */
214        AxisAlignedBox3 GetViewSpaceBox() const;
215        /** Returns object space bounding box.
216        */
217        AxisAlignedBox3 GetObjectSpaceBox() const;
218
219        /** Exports object space hierarchy for visualization.
220        */
221        void ExportObjectSpaceHierarchy(
222                Exporter *exporter,
223                const ObjectContainer &objects,
224                const AxisAlignedBox3 *bbox,
225                const bool exportBounds = true) const;
226
227        /** Returns intersectable pierced by this ray.
228        */
229        Intersectable *GetIntersectable(
230                const VssRay &ray,
231                const bool isTermination) const;
232
233        friend ostream &operator<<(ostream &s, const HierarchyManager &hm)
234        {
235                hm.PrintHierarchyStatistics(s);
236                return s;
237        }
238
239
240protected:
241
242        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
243
244        /** Prepare construction of the hierarchies, set parameters, compute
245                first split candidates.
246        */
247        void PrepareObjectSpaceSubdivision(
248                const VssRayContainer &sampleRays,
249                const ObjectContainer &objects);
250
251        void RunConstruction(
252                const bool repairQueue,
253                const VssRayContainer &sampleRays,
254                const ObjectContainer &objects,
255                AxisAlignedBox3 *forcedViewSpace);
256       
257        void RunConstruction(const bool repairQueue);
258               
259        bool ApplySubdivisionCandidate(SubdivisionCandidate *sc);
260
261        bool FinishedConstruction() const;
262
263        SubdivisionCandidate *NextSubdivisionCandidate();
264
265        void RepairQueue();
266
267        void CollectDirtyCandidates(vector<SubdivisionCandidate *> &dirtyList);
268
269        void EvalSubdivisionStats(const SubdivisionCandidate &tData);
270
271        void AddSubdivisionStats(
272                const int splits,
273                const float renderCostDecr,
274                const float totalRenderCost);
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        void ConstructMultiLevel(
320                const VssRayContainer &sampleRays,
321                const ObjectContainer &objects,
322                AxisAlignedBox3 *forcedViewSpace);
323
324        void ResetObjectSpaceSubdivision(const ObjectContainer &objects);
325
326protected:
327
328        enum {SEQUENTIAL, INTERLEAVED};
329       
330        int mObjectSpaceSubdivisionType;
331    int mViewSpaceSubdivisionType;
332
333        /// the original osp type
334        int mSavedObjectSpaceSubdivisionType;
335        int mSavedViewSpaceSubdivisionType;
336
337        int mConstructionType;
338
339        VspTree *mVspTree;
340        OspTree *mOspTree;
341        BvHierarchy *mBvHierarchy;
342
343        AxisAlignedBox3 mBoundingBox;
344
345        SplitQueue mTQueue;
346
347        SubdivisionCandidate *mCurrentCandidate;
348
349        ////////
350        //-- global criteria
351        float mTermMinGlobalCostRatio;
352        int mTermGlobalCostMissTolerance;
353       
354        /// keeps track of cost during subdivision
355        float mTotalCost;
356
357        HierarchyStatistics mHierarchyStats;
358
359        int mMinDepthForObjectSpaceSubdivion;
360        int mMinDepthForViewSpaceSubdivion;
361
362        int mTermMaxLeaves;
363        ofstream mSubdivisionStats;
364
365        bool mRepairQueue;
366
367        bool mStartWithObjectSpace;
368
369        bool mUseMultiLevelConstruction;
370};
371
372}
373
374#endif
Note: See TracBrowser for help on using the repository browser.