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

Revision 1563, 9.1 KB checked in by mattausch, 18 years ago (diff)

fixed bug with view space box

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