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

Revision 1279, 6.0 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        /** Constructs the view space and object space subdivision from a given set of rays
103                and a set of objects.
104                @param sampleRays the set of sample rays the construction is based on
105                @param objects the set of objects
106        */
107        void Construct(const VssRayContainer &sampleRays,
108                                   const ObjectContainer &objects,
109                                   AxisAlignedBox3 *forcedViewSpace);
110
111        /** Constructs first view cells, then object space partition.
112        */
113        void Construct2(const VssRayContainer &sampleRays,
114                                        const ObjectContainer &objects,
115                                        AxisAlignedBox3 *forcedViewSpace);
116
117        /** Constructs only vsp tree.
118        */
119        void Construct3(const VssRayContainer &sampleRays,
120                                        const ObjectContainer &objects,
121                                        AxisAlignedBox3 *forcedViewSpace);
122
123        enum
124        {
125                NO_OBJ_SUBDIV,
126                KD_BASED_OBJ_SUBDIV,
127                BV_BASED_OBJ_SUBDIV
128        };
129
130        /** The type of object space subdivison
131        */
132        inline int GetObjectSpaceSubdivisonType() const
133        {
134                return mObjectSpaceSubdivisonType;
135        }
136       
137        void SetViewCellsManager(ViewCellsManager *vcm);
138
139        void SetViewCellsTree(ViewCellsTree *vcTree);
140
141        void ExportObjectSpaceHierarchy(OUT_STREAM &stream);
142
143        bool AddSampleToPvs(
144                Intersectable *obj,
145                const Vector3 &hitPoint,
146                ViewCell *vc,
147                const float pdf,
148                float &contribution) const;
149
150        void PrintObjectSpaceHierarchyStatistics(ofstream &stream) const;
151
152        VspTree *GetVspTree() { return mVspTree; }
153
154        void ExportObjectSpaceHierarchyForViz(const ObjectContainer &objects) const;
155
156protected:
157
158        bool GlobalTerminationCriteriaMet(SubdivisionCandidate *candidate) const;
159
160        /** Prepare construction of the hierarchies, set parameters, compute
161                first split candidates.
162        */
163        void PrepareConstruction(
164                const VssRayContainer &sampleRays,
165                const ObjectContainer &objects,
166                AxisAlignedBox3 *forcedViewSpace,
167                RayInfoContainer &viewSpaceRays,
168                RayInfoContainer &objectSpaceRays);
169
170        void RunConstruction(const bool repair);
171        bool SubdivideSubdivisionCandidate(SubdivisionCandidate *sc);
172
173        bool FinishedConstruction() const;
174
175        SubdivisionCandidate *NextSubdivisionCandidate();
176
177        void RepairQueue();
178
179        void CollectDirtyCandidates(vector<SubdivisionCandidate *> &dirtyList);
180
181        void EvalSubdivisionStats(const SubdivisionCandidate &tData);
182
183        void AddSubdivisionStats(
184                const int splits,
185                const float renderCostDecr,
186                const float totalRenderCost);
187
188        void CollectObjectSpaceDirtyList();
189        void CollectViewSpaceDirtyList();
190
191        bool AddSampleToPvs(Intersectable *obj,
192                                                const float pdf,
193                                                float &contribution) const;
194
195        void CollectViewSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
196        void CollectObjectSpaceDirtyList(SubdivisionCandidateContainer &dirtyList);
197               
198
199protected:
200
201        int mObjectSpaceSubdivisonType;
202
203        VspTree *mVspTree;
204        OspTree *mOspTree;
205        BvHierarchy *mBvHierarchy;
206
207        AxisAlignedBox3 mBoundingBox;
208
209        SplitQueue mTQueue;
210
211        SubdivisionCandidate *mCurrentCandidate;
212
213        //-- global criteria
214        float mTermMinGlobalCostRatio;
215        int mTermGlobalCostMissTolerance;
216        int mGlobalCostMisses;
217
218        /// keeps track of cost during subdivision
219        float mTotalCost;
220
221        ofstream mSubdivisionStats;
222};
223
224}
225
226#endif
Note: See TracBrowser for help on using the repository browser.