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

Revision 1418, 8.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
75        float repairTime;
76
77        // Constructor
78        HierarchyStatistics()
79        {
80                Reset();
81        }
82
83        int Nodes() const {return nodes;}
84        int Interior() const { return nodes / 2; }
85        int Leaves() const { return (nodes / 2) + 1; }
86       
87        // TODO: computation wrong
88        double AvgDepth() const { return accumDepth / (double)Leaves();}
89
90        void Reset()
91        {
92                nodes = 0;
93                maxDepth = 0;
94                accumDepth = 0;
95                repairTime = 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       
132*/
133class HierarchyManager
134{
135        friend VspTree;
136        friend OspTree;
137        friend BvHierarchy;
138        friend ViewCellsParseHandlers;
139
140public:
141        /** Constructor with the object space hierarchy type as argument.
142        */
143        HierarchyManager(VspTree *vspTree, 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(VspTree *vspTree, KdTree *kdTree);
148
149        ~HierarchyManager();
150
151        /** Constructs the view space and object space subdivision from a given set of rays
152                and a set of objects.
153                @param sampleRays the set of sample rays the construction is based on
154                @param objects the set of objects
155        */
156        void Construct(
157                const VssRayContainer &sampleRays,
158                const ObjectContainer &objects,
159                AxisAlignedBox3 *forcedViewSpace);
160
161        enum
162        {
163                NO_OBJ_SUBDIV,
164                KD_BASED_OBJ_SUBDIV,
165                BV_BASED_OBJ_SUBDIV
166        };
167
168        enum
169        {
170                NO_VIEWSPACE_SUBDIV,
171                KD_BASED_VIEWSPACE_SUBDIV
172        };
173
174        /** The type of object space subdivison
175        */
176        int GetObjectSpaceSubdivisionType() const;     
177        /** The type of view space space subdivison
178        */
179        int GetViewSpaceSubdivisionType() const;
180        /** Sets a pointer to the view cells manager.
181        */             
182        void SetViewCellsManager(ViewCellsManager *vcm);
183        /** Sets a pointer to the view cells tree.
184        */
185        void SetViewCellsTree(ViewCellsTree *vcTree);
186        /** Exports the object hierarchy to disc.
187        */
188        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
189        /** Adds a sample to the pvs of the specified view cell.
190        */
191        bool AddSampleToPvs(
192                Intersectable *obj,
193                const Vector3 &hitPoint,
194                ViewCell *vc,
195                const float pdf,
196                float &contribution) const;
197
198        void PrintHierarchyStatistics(ofstream &stream) const;
199
200        VspTree *GetVspTree();
201
202        AxisAlignedBox3 GetViewSpaceBox() const;
203        AxisAlignedBox3 GetObjectSpaceBox() const;
204
205        void ExportObjectSpaceHierarchy(
206                Exporter *exporter,
207                const ObjectContainer &objects,
208                const AxisAlignedBox3 *bbox,
209                const bool exportBounds = true) const;
210
211
212        Intersectable *GetIntersectable(
213                const VssRay &ray,
214                const bool isTermination) const;
215
216protected:
217
218        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
219
220        /** Prepare construction of the hierarchies, set parameters, compute
221                first split candidates.
222        */
223        void PrepareObjectSpaceSubdivision(
224                const VssRayContainer &sampleRays,
225                const ObjectContainer &objects);
226
227        void RunConstruction(
228                const VssRayContainer &sampleRays,
229                const ObjectContainer &objects,
230                AxisAlignedBox3 *forcedViewSpace);
231
232        bool ApplySubdivisionCandidate(SubdivisionCandidate *sc);
233
234        bool FinishedConstruction() const;
235
236        SubdivisionCandidate *NextSubdivisionCandidate();
237
238        void RepairQueue();
239
240        void CollectDirtyCandidates(vector<SubdivisionCandidate *> &dirtyList);
241
242        void EvalSubdivisionStats(const SubdivisionCandidate &tData);
243
244        void AddSubdivisionStats(
245                const int splits,
246                const float renderCostDecr,
247                const float totalRenderCost);
248
249        void CollectObjectSpaceDirtyList();
250        void CollectViewSpaceDirtyList();
251
252        bool AddSampleToPvs(Intersectable *obj,
253                                                const float pdf,
254                                                float &contribution) const;
255
256        void CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
257        void CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
258               
259        void ExportOspTree(Exporter *exporter, const ObjectContainer &objects) const;
260
261        void ParseEnvironment();
262
263        bool StartObjectSpaceSubdivision() const;
264        bool StartViewSpaceSubdivision() const;
265
266        void PrepareBvHierarchy(
267                const VssRayContainer &sampleRays,
268                const ObjectContainer &objects);
269
270        void PrepareOspTree(
271                const VssRayContainer &sampleRays,
272                const ObjectContainer &objects);
273
274        void PrepareViewSpaceSubdivision(
275                const VssRayContainer &sampleRays,
276                const ObjectContainer &objects);
277
278        bool ObjectSpaceSubdivisionConstructed() const;
279        bool ViewSpaceSubdivisionConstructed() const;
280
281    void ResetQueue();
282
283        void FinishObjectSpaceSubdivision(const ObjectContainer &objects) const;
284
285        int GetObjectSpaceSubdivisionDepth() const;
286
287protected:
288
289        enum {SEQUENTIAL, INTERLEAVED};
290       
291        int mObjectSpaceSubdivisionType;
292    int mViewSpaceSubdivisionType;
293
294        /// the original osp type
295        int mSavedObjectSpaceSubdivisionType;
296        int mSavedViewSpaceSubdivisionType;
297
298        int mConstructionType;
299
300        VspTree *mVspTree;
301        OspTree *mOspTree;
302        BvHierarchy *mBvHierarchy;
303
304        AxisAlignedBox3 mBoundingBox;
305
306        SplitQueue mTQueue;
307
308        SubdivisionCandidate *mCurrentCandidate;
309
310        //-- global criteria
311        float mTermMinGlobalCostRatio;
312        int mTermGlobalCostMissTolerance;
313        int mGlobalCostMisses;
314
315        /// keeps track of cost during subdivision
316        float mTotalCost;
317
318        HierarchyStatistics mHierarchyStats;
319
320        int mMinDepthForObjectSpaceSubdivion;
321        int mMinDepthForViewSpaceSubdivion;
322
323        int mTermMaxLeaves;
324        ofstream mSubdivisionStats;
325
326        bool mRepairQueue;
327
328        bool mStartWithObjectSpace;
329};
330
331}
332
333#endif
Note: See TracBrowser for help on using the repository browser.