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

Revision 1286, 6.4 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;
42
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
61typedef FlexibleHeap<SubdivisionCandidate *> SplitQueue;
62
63/** This class implements a structure holding two different hierarchies,
64        one for object space partitioning and one for view space partitioning.
65
66        The object space and the view space are subdivided using a cost heuristics.
67        If an object space split or a view space split is chosen is also evaluated
68        based on the heuristics.
69       
70        The view space heuristics is evaluated by weighting and adding the pvss of the back and
71        front node of each specific split. unlike for the standalone method vspbsp tree,
72        the pvs of an object would not be the pvs of single object but that of all objects
73        which are contained in the same leaf of the object subdivision. This could be done
74        by storing the pointer to the object space partition parent, which would allow access to all children.
75        Another possibility is to include traced kd-cells in the ray casing process.
76
77        Accordingly, the object space heuristics is evaluated by storing a pvs of view cells with each object.
78        the contribution to an object to the pvs is the number of view cells it can be seen from.
79
80        @note
81        There is a potential efficiency problem involved in a sense that once a certain type
82        of split is chosen for view space / object space, the candidates for the next split of
83        object space / view space must be reevaluated.
84       
85*/
86class HierarchyManager
87{
88        friend VspTree;
89        friend OspTree;
90        friend BvHierarchy;
91        friend ViewCellsParseHandlers;
92
93public:
94        /** Constructor with the object space hierarchy type as argument.
95        */
96        HierarchyManager(VspTree *vspTree, const int objectSpaceHierarchyType);
97        /** Hack: OspTree will copy the content from this kd tree.
98                Only view space hierarchy will be constructed.
99        */
100        HierarchyManager(VspTree *vspTree, KdTree *kdTree);
101
102        ~HierarchyManager();
103
104        /** Constructs the view space and object space subdivision from a given set of rays
105                and a set of objects.
106                @param sampleRays the set of sample rays the construction is based on
107                @param objects the set of objects
108        */
109        void Construct(const VssRayContainer &sampleRays,
110                                   const ObjectContainer &objects,
111                                   AxisAlignedBox3 *forcedViewSpace);
112
113        /** Constructs first view cells, then object space partition.
114        */
115        void Construct2(const VssRayContainer &sampleRays,
116                                        const ObjectContainer &objects,
117                                        AxisAlignedBox3 *forcedViewSpace);
118
119        /** Constructs only vsp tree.
120        */
121        void Construct3(const VssRayContainer &sampleRays,
122                                        const ObjectContainer &objects,
123                                        AxisAlignedBox3 *forcedViewSpace);
124
125        enum
126        {
127                NO_OBJ_SUBDIV,
128                KD_BASED_OBJ_SUBDIV,
129                BV_BASED_OBJ_SUBDIV
130        };
131
132        /** The type of object space subdivison
133        */
134        inline int GetObjectSpaceSubdivisonType() const
135        {
136                return mObjectSpaceSubdivisonType;
137        }
138       
139        void SetViewCellsManager(ViewCellsManager *vcm);
140
141        void SetViewCellsTree(ViewCellsTree *vcTree);
142
143        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
144
145        bool AddSampleToPvs(
146                Intersectable *obj,
147                const Vector3 &hitPoint,
148                ViewCell *vc,
149                const float pdf,
150                float &contribution) const;
151
152        void PrintObjectSpaceHierarchyStatistics(ofstream &stream) const;
153
154        VspTree *GetVspTree() { return mVspTree; }
155
156        void ExportObjectSpaceHierarchyForViz(const ObjectContainer &objects) const;
157
158
159protected:
160
161        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
162
163        /** Prepare construction of the hierarchies, set parameters, compute
164                first split candidates.
165        */
166        void PrepareConstruction(
167                const VssRayContainer &sampleRays,
168                const ObjectContainer &objects,
169                AxisAlignedBox3 *forcedViewSpace,
170                RayInfoContainer &viewSpaceRays,
171                RayInfoContainer &objectSpaceRays);
172
173        void RunConstruction(const bool repair);
174        bool SubdivideSubdivisionCandidate(SubdivisionCandidate *sc);
175
176        bool FinishedConstruction() const;
177
178        SubdivisionCandidate *NextSubdivisionCandidate();
179
180        void RepairQueue();
181
182        void CollectDirtyCandidates(vector<SubdivisionCandidate *> &dirtyList);
183
184        void EvalSubdivisionStats(const SubdivisionCandidate &tData);
185
186        void AddSubdivisionStats(
187                const int splits,
188                const float renderCostDecr,
189                const float totalRenderCost);
190
191        void CollectObjectSpaceDirtyList();
192        void CollectViewSpaceDirtyList();
193
194        bool AddSampleToPvs(Intersectable *obj,
195                                                const float pdf,
196                                                float &contribution) const;
197
198        void CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
199        void CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
200               
201        void ExportOspTreeForViz(const ObjectContainer &objects) const;
202        void ExportBvhForViz(const ObjectContainer &objects) const;
203
204        void ConstructBvHierarchy(
205                const VssRayContainer &sampleRays,
206                const ObjectContainer &objects,
207                AxisAlignedBox3 *forcedViewSpace);
208
209        void ConstructOspTree(
210                const VssRayContainer &sampleRays,
211                const ObjectContainer &objects,
212                AxisAlignedBox3 *forcedViewSpace);
213
214protected:
215
216        int mObjectSpaceSubdivisonType;
217
218        VspTree *mVspTree;
219        OspTree *mOspTree;
220        BvHierarchy *mBvHierarchy;
221
222        AxisAlignedBox3 mBoundingBox;
223
224        SplitQueue mTQueue;
225
226        SubdivisionCandidate *mCurrentCandidate;
227
228        //-- global criteria
229        float mTermMinGlobalCostRatio;
230        int mTermGlobalCostMissTolerance;
231        int mGlobalCostMisses;
232
233        /// keeps track of cost during subdivision
234        float mTotalCost;
235
236        ofstream mSubdivisionStats;
237};
238
239}
240
241#endif
Note: See TracBrowser for help on using the repository browser.