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

Revision 1288, 7.2 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        /** Constructs the view space and object space subdivision from a given set of rays
153                and a set of objects.
154                @param sampleRays the set of sample rays the construction is based on
155                @param objects the set of objects
156        */
157        void Construct(const VssRayContainer &sampleRays,
158                                   const ObjectContainer &objects,
159                                   AxisAlignedBox3 *forcedViewSpace);
160
161        /** Constructs first view cells, then object space partition.
162        */
163        void Construct2(const VssRayContainer &sampleRays,
164                                        const ObjectContainer &objects,
165                                        AxisAlignedBox3 *forcedViewSpace);
166
167        /** Constructs only vsp tree.
168        */
169        void Construct3(const VssRayContainer &sampleRays,
170                                        const ObjectContainer &objects,
171                                        AxisAlignedBox3 *forcedViewSpace);
172
173        enum
174        {
175                NO_OBJ_SUBDIV,
176                KD_BASED_OBJ_SUBDIV,
177                BV_BASED_OBJ_SUBDIV
178        };
179
180        /** The type of object space subdivison
181        */
182        inline int GetObjectSpaceSubdivisonType() const
183        {
184                return mObjectSpaceSubdivisonType;
185        }
186       
187        void SetViewCellsManager(ViewCellsManager *vcm);
188
189        void SetViewCellsTree(ViewCellsTree *vcTree);
190
191        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
192
193        bool AddSampleToPvs(
194                Intersectable *obj,
195                const Vector3 &hitPoint,
196                ViewCell *vc,
197                const float pdf,
198                float &contribution) const;
199
200        void PrintObjectSpaceHierarchyStatistics(ofstream &stream) const;
201
202        VspTree *GetVspTree() { return mVspTree; }
203
204        void ExportObjectSpaceHierarchy(
205                Exporter *exporter,
206                const ObjectContainer &objects) const;
207
208
209protected:
210
211        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
212
213        /** Prepare construction of the hierarchies, set parameters, compute
214                first split candidates.
215        */
216        void PrepareConstruction(
217                const VssRayContainer &sampleRays,
218                const ObjectContainer &objects,
219                AxisAlignedBox3 *forcedViewSpace,
220                RayInfoContainer &viewSpaceRays,
221                RayInfoContainer &objectSpaceRays);
222
223        void RunConstruction(const bool repair);
224        bool ApplySubdivisionCandidate(SubdivisionCandidate *sc);
225
226        bool FinishedConstruction() const;
227
228        SubdivisionCandidate *NextSubdivisionCandidate();
229
230        void RepairQueue();
231
232        void CollectDirtyCandidates(vector<SubdivisionCandidate *> &dirtyList);
233
234        void EvalSubdivisionStats(const SubdivisionCandidate &tData);
235
236        void AddSubdivisionStats(
237                const int splits,
238                const float renderCostDecr,
239                const float totalRenderCost);
240
241        void CollectObjectSpaceDirtyList();
242        void CollectViewSpaceDirtyList();
243
244        bool AddSampleToPvs(Intersectable *obj,
245                                                const float pdf,
246                                                float &contribution) const;
247
248        void CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
249        void CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
250               
251        void ExportOspTree(Exporter *exporter, const ObjectContainer &objects) const;
252        void ExportBvHierarchy(Exporter *exporter, const ObjectContainer &objects) const;
253
254
255        void ConstructBvHierarchy(
256                const VssRayContainer &sampleRays,
257                const ObjectContainer &objects);
258
259        void ConstructOspTree(
260                const VssRayContainer &sampleRays,
261                const ObjectContainer &objects);
262
263        void ParseEnvironment();
264
265
266protected:
267
268        int mObjectSpaceSubdivisonType;
269
270        VspTree *mVspTree;
271        OspTree *mOspTree;
272        BvHierarchy *mBvHierarchy;
273
274        AxisAlignedBox3 mBoundingBox;
275
276        SplitQueue mTQueue;
277
278        SubdivisionCandidate *mCurrentCandidate;
279
280        //-- global criteria
281        float mTermMinGlobalCostRatio;
282        int mTermGlobalCostMissTolerance;
283        int mGlobalCostMisses;
284
285        /// keeps track of cost during subdivision
286        float mTotalCost;
287
288        HierarchyStatistics mHierarchyStats;
289
290        int mMaxLeaves;
291        ofstream mSubdivisionStats;
292};
293
294}
295
296#endif
Note: See TracBrowser for help on using the repository browser.