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

Revision 1379, 8.0 KB checked in by mattausch, 18 years ago (diff)

fixed sah for objeect partition
loader for single triangles also for x3d

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
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 PrepareObjectSpaceSubdivision(
217                const VssRayContainer &sampleRays,
218                const ObjectContainer &objects);
219
220        void RunConstruction(
221                const VssRayContainer &sampleRays,
222                const ObjectContainer &objects,
223                AxisAlignedBox3 *forcedViewSpace);
224
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        void PrepareBvHierarchy(
256                const VssRayContainer &sampleRays,
257                const ObjectContainer &objects);
258
259        void PrepareOspTree(
260                const VssRayContainer &sampleRays,
261                const ObjectContainer &objects);
262
263        void ParseEnvironment();
264
265        bool StartObjectSpaceSubdivision() const;
266        bool StartViewSpaceSubdivision() const;
267
268        void PrepareViewSpaceSubdivision(
269                const VssRayContainer &sampleRays,
270                const ObjectContainer &objects);
271
272        bool ObjectSpaceSubdivisionConstructed() const;
273        bool ViewSpaceSubdivisionConstructed() const;
274
275    void ResetQueue();
276
277        void FinishObjectSpaceSubdivision() const;
278
279        int GetObjectSpaceSubdivisionDepth() const;
280
281protected:
282
283        enum {SEQUENTIAL, INTERLEAVED};
284       
285        int mObjectSpaceSubdivisionType;
286    int mViewSpaceSubdivisionType;
287
288        /// the original osp type
289        int mSavedObjectSpaceSubdivisionType;
290        int mSavedViewSpaceSubdivisionType;
291
292        int mConstructionType;
293
294        VspTree *mVspTree;
295        OspTree *mOspTree;
296        BvHierarchy *mBvHierarchy;
297
298        AxisAlignedBox3 mBoundingBox;
299
300        SplitQueue mTQueue;
301
302        SubdivisionCandidate *mCurrentCandidate;
303
304        //-- global criteria
305        float mTermMinGlobalCostRatio;
306        int mTermGlobalCostMissTolerance;
307        int mGlobalCostMisses;
308
309        /// keeps track of cost during subdivision
310        float mTotalCost;
311
312        HierarchyStatistics mHierarchyStats;
313
314        int mMinDepthForObjectSpaceSubdivion;
315        int mMinDepthForViewSpaceSubdivion;
316
317        int mTermMaxLeaves;
318        ofstream mSubdivisionStats;
319
320        bool mRepairQueue;
321
322        bool mStartWithObjectSpace;
323};
324
325}
326
327#endif
Note: See TracBrowser for help on using the repository browser.