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

Revision 1293, 7.3 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               
71        /// maximal reached depth
72        int maxDepth;
73       
74        /// accumulated depth
75        int accumDepth;
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
91        void Reset()
92        {
93                nodes = 0;
94                maxDepth = 0;
95                accumDepth = 0;
96        }
97
98
99        void Print(ostream &app) const;
100
101        friend ostream &operator<<(ostream &s, const HierarchyStatistics &stat)
102        {
103                stat.Print(s);
104                return s;
105        }
106};
107
108
109typedef FlexibleHeap<SubdivisionCandidate *> SplitQueue;
110
111/** This class implements a structure holding two different hierarchies,
112        one for object space partitioning and one for view space partitioning.
113
114        The object space and the view space are subdivided using a cost heuristics.
115        If an object space split or a view space split is chosen is also evaluated
116        based on the heuristics.
117       
118        The view space heuristics is evaluated by weighting and adding the pvss of the back and
119        front node of each specific split. unlike for the standalone method vspbsp tree,
120        the pvs of an object would not be the pvs of single object but that of all objects
121        which are contained in the same leaf of the object subdivision. This could be done
122        by storing the pointer to the object space partition parent, which would allow access to all children.
123        Another possibility is to include traced kd-cells in the ray casing process.
124
125        Accordingly, the object space heuristics is evaluated by storing a pvs of view cells with each object.
126        the contribution to an object to the pvs is the number of view cells it can be seen from.
127
128        @note
129        There is a potential efficiency problem involved in a sense that once a certain type
130        of split is chosen for view space / object space, the candidates for the next split of
131        object space / view space must be reevaluated.
132       
133*/
134class HierarchyManager
135{
136        friend VspTree;
137        friend OspTree;
138        friend BvHierarchy;
139        friend ViewCellsParseHandlers;
140
141public:
142        /** Constructor with the object space hierarchy type as argument.
143        */
144        HierarchyManager(VspTree *vspTree, const int objectSpaceHierarchyType);
145        /** Hack: OspTree will copy the content from this kd tree.
146                Only view space hierarchy will be constructed.
147        */
148        HierarchyManager(VspTree *vspTree, KdTree *kdTree);
149
150        ~HierarchyManager();
151
152        void Construct(
153                const VssRayContainer &sampleRays,
154                const ObjectContainer &objects,
155                AxisAlignedBox3 *forcedViewSpace);
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 ConstructInterleaved(
163                const VssRayContainer &sampleRays,
164                const ObjectContainer &objects,
165                AxisAlignedBox3 *forcedViewSpace);
166
167        /** Constructs first view space hierarchy, then object space hierarchy.
168        */
169        void ConstructSequential(
170                const VssRayContainer &sampleRays,
171                const ObjectContainer &objects,
172                AxisAlignedBox3 *forcedViewSpace);
173
174        enum
175        {
176                NO_OBJ_SUBDIV,
177                KD_BASED_OBJ_SUBDIV,
178                BV_BASED_OBJ_SUBDIV
179        };
180
181        /** The type of object space subdivison
182        */
183        inline int GetObjectSpaceSubdivisonType() const
184        {
185                return mObjectSpaceSubdivisonType;
186        }
187       
188        void SetViewCellsManager(ViewCellsManager *vcm);
189
190        void SetViewCellsTree(ViewCellsTree *vcTree);
191
192        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
193
194        bool AddSampleToPvs(
195                Intersectable *obj,
196                const Vector3 &hitPoint,
197                ViewCell *vc,
198                const float pdf,
199                float &contribution) const;
200
201        void PrintObjectSpaceHierarchyStatistics(ofstream &stream) const;
202
203        VspTree *GetVspTree() { return mVspTree; }
204
205        void ExportObjectSpaceHierarchy(
206                Exporter *exporter,
207                const ObjectContainer &objects) const;
208
209
210protected:
211
212        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
213
214        /** Prepare construction of the hierarchies, set parameters, compute
215                first split candidates.
216        */
217        void PrepareConstruction(
218                const VssRayContainer &sampleRays,
219                const ObjectContainer &objects,
220                AxisAlignedBox3 *forcedViewSpace,
221                RayInfoContainer &viewSpaceRays,
222                RayInfoContainer &objectSpaceRays);
223
224        void RunConstruction(const bool repair);
225        bool ApplySubdivisionCandidate(SubdivisionCandidate *sc);
226
227        bool FinishedConstruction() const;
228
229        SubdivisionCandidate *NextSubdivisionCandidate();
230
231        void RepairQueue();
232
233        void CollectDirtyCandidates(vector<SubdivisionCandidate *> &dirtyList);
234
235        void EvalSubdivisionStats(const SubdivisionCandidate &tData);
236
237        void AddSubdivisionStats(
238                const int splits,
239                const float renderCostDecr,
240                const float totalRenderCost);
241
242        void CollectObjectSpaceDirtyList();
243        void CollectViewSpaceDirtyList();
244
245        bool AddSampleToPvs(Intersectable *obj,
246                                                const float pdf,
247                                                float &contribution) const;
248
249        void CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
250        void CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
251               
252        void ExportOspTree(Exporter *exporter, const ObjectContainer &objects) const;
253        void ExportBvHierarchy(Exporter *exporter, const ObjectContainer &objects) const;
254
255
256        void ConstructBvHierarchy(
257                const VssRayContainer &sampleRays,
258                const ObjectContainer &objects);
259
260        void ConstructOspTree(
261                const VssRayContainer &sampleRays,
262                const ObjectContainer &objects);
263
264        void ParseEnvironment();
265
266
267protected:
268
269        int mConstructionType;
270        int mObjectSpaceSubdivisonType;
271
272        VspTree *mVspTree;
273        OspTree *mOspTree;
274        BvHierarchy *mBvHierarchy;
275
276        AxisAlignedBox3 mBoundingBox;
277
278        SplitQueue mTQueue;
279
280        SubdivisionCandidate *mCurrentCandidate;
281
282        //-- global criteria
283        float mTermMinGlobalCostRatio;
284        int mTermGlobalCostMissTolerance;
285        int mGlobalCostMisses;
286
287        /// keeps track of cost during subdivision
288        float mTotalCost;
289
290        HierarchyStatistics mHierarchyStats;
291
292        int mMaxLeaves;
293        ofstream mSubdivisionStats;
294};
295
296}
297
298#endif
Note: See TracBrowser for help on using the repository browser.