1 | #ifndef _ViewCellBsp_H__ |
---|
2 | #define _ViewCellBsp_H__ |
---|
3 | |
---|
4 | #include "Mesh.h" |
---|
5 | #include "Containers.h" |
---|
6 | #include <queue> |
---|
7 | #include <stack> |
---|
8 | |
---|
9 | class ViewCell; |
---|
10 | class Plane3; |
---|
11 | //class Mesh; |
---|
12 | |
---|
13 | //namespace GtpVisibilityPreprocessor { |
---|
14 | class BspTree; |
---|
15 | class BspInterior; |
---|
16 | class Polygon3; |
---|
17 | |
---|
18 | /** Container storing a soup of polygons used during BSP tree construction |
---|
19 | */ |
---|
20 | typedef std::vector<Polygon3 *> PolygonContainer; |
---|
21 | |
---|
22 | class BspTreeStatistics // TODO: should have common superclass with KdTreeStatistics |
---|
23 | { |
---|
24 | public: |
---|
25 | // total number of nodes |
---|
26 | int nodes; |
---|
27 | // number of splits |
---|
28 | int splits; |
---|
29 | // totals number of rays |
---|
30 | int rays; |
---|
31 | // maximal reached depth |
---|
32 | int maxDepth; |
---|
33 | // total number of query domains |
---|
34 | int queryDomains; |
---|
35 | // total number of ray references |
---|
36 | int rayRefs; |
---|
37 | // refs in non empty leafs |
---|
38 | int rayRefsNonZeroQuery; |
---|
39 | // total number of query references |
---|
40 | int objectRefs; |
---|
41 | // nodes with zero queries |
---|
42 | int zeroQueryNodes; |
---|
43 | // max depth nodes |
---|
44 | int maxDepthNodes; |
---|
45 | // max number of rays per node |
---|
46 | int maxObjectRefs; |
---|
47 | // number of dynamically added ray refs |
---|
48 | int addedRayRefs; |
---|
49 | // number of dynamically removed ray refs |
---|
50 | int removedRayRefs; |
---|
51 | |
---|
52 | // Constructor |
---|
53 | BspTreeStatistics() |
---|
54 | { |
---|
55 | Reset(); |
---|
56 | } |
---|
57 | |
---|
58 | int Nodes() const {return nodes;} |
---|
59 | int Interior() const { return nodes/2; } |
---|
60 | int Leaves() const { return (nodes/2) + 1; } |
---|
61 | |
---|
62 | void Reset() |
---|
63 | { |
---|
64 | nodes = 0; |
---|
65 | splits = 0; |
---|
66 | rays = queryDomains = 0; |
---|
67 | rayRefs = rayRefsNonZeroQuery = objectRefs = 0; |
---|
68 | zeroQueryNodes = 0; |
---|
69 | maxDepthNodes = 0; |
---|
70 | //minCostNodes = 0; |
---|
71 | maxObjectRefs = 0; |
---|
72 | addedRayRefs = removedRayRefs = 0; |
---|
73 | } |
---|
74 | |
---|
75 | void |
---|
76 | Print(ostream &app) const; |
---|
77 | |
---|
78 | friend ostream &operator<<(ostream &s, const BspTreeStatistics &stat) { |
---|
79 | stat.Print(s); |
---|
80 | return s; |
---|
81 | } |
---|
82 | |
---|
83 | }; |
---|
84 | |
---|
85 | /** |
---|
86 | BspNode abstract class serving for interior and leaf node implementation |
---|
87 | */ |
---|
88 | class BspNode |
---|
89 | { |
---|
90 | friend BspTree; |
---|
91 | |
---|
92 | public: |
---|
93 | BspNode(); |
---|
94 | BspNode(BspInterior *parent); |
---|
95 | |
---|
96 | /** Determines whether this node is a leaf or not |
---|
97 | @return true if leaf |
---|
98 | */ |
---|
99 | virtual bool IsLeaf() const = 0; |
---|
100 | |
---|
101 | /** Determines whether this node is a root |
---|
102 | @return true if root |
---|
103 | */ |
---|
104 | virtual bool IsRoot() const; |
---|
105 | |
---|
106 | /** Returns parent node. |
---|
107 | */ |
---|
108 | BspInterior *GetParent(); |
---|
109 | /** Sets parent node. |
---|
110 | */ |
---|
111 | void SetParent(BspInterior *parent); |
---|
112 | |
---|
113 | protected: |
---|
114 | |
---|
115 | /// parent of this node |
---|
116 | BspInterior *mParent; |
---|
117 | }; |
---|
118 | |
---|
119 | /** BSP interior node implementation |
---|
120 | */ |
---|
121 | class BspInterior : public BspNode |
---|
122 | { |
---|
123 | public: |
---|
124 | /** Standard contructor taking split plane as argument. |
---|
125 | */ |
---|
126 | BspInterior(Plane3 plane); |
---|
127 | /** @return false since it is an interior node |
---|
128 | */ |
---|
129 | bool IsLeaf() const; |
---|
130 | |
---|
131 | BspNode *GetBack(); |
---|
132 | BspNode *GetFront(); |
---|
133 | |
---|
134 | Plane3 *GetPlane(); |
---|
135 | |
---|
136 | void ReplaceChildLink(BspNode *oldChild, BspNode *newChild); |
---|
137 | void SetupChildLinks(BspNode *b, BspNode *f); |
---|
138 | |
---|
139 | /** Splits polygons. |
---|
140 | @param polys the polygons to be split |
---|
141 | @param frontPolys returns the polygons in the front of the split plane |
---|
142 | @param backPolys returns the polygons in the back of the split plane |
---|
143 | @param splits number of splits |
---|
144 | */ |
---|
145 | void SplitPolygons(PolygonContainer *polys, PolygonContainer *frontPolys, |
---|
146 | PolygonContainer *backPolys, int &splits); |
---|
147 | |
---|
148 | friend ostream &operator<<(ostream &s, const BspInterior &A) |
---|
149 | { |
---|
150 | return s << A.mPlane; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | protected: |
---|
155 | |
---|
156 | /// Splitting plane corresponding to this node |
---|
157 | Plane3 mPlane; |
---|
158 | /// back node |
---|
159 | BspNode *mBack; |
---|
160 | /// front node |
---|
161 | BspNode *mFront; |
---|
162 | }; |
---|
163 | |
---|
164 | /** BSP leaf node implementation. |
---|
165 | */ |
---|
166 | class BspLeaf : public BspNode |
---|
167 | { |
---|
168 | public: |
---|
169 | BspLeaf(ViewCell *viewCell = NULL); |
---|
170 | |
---|
171 | /** @return true since it is an interior node */ |
---|
172 | bool IsLeaf() const; |
---|
173 | ViewCell *GetViewCell(); |
---|
174 | |
---|
175 | protected: |
---|
176 | |
---|
177 | /// polygonal representation of this viewcell |
---|
178 | /// if NULL this note does not correspond to feasible viewcell |
---|
179 | ViewCell *mViewCell; |
---|
180 | }; |
---|
181 | |
---|
182 | |
---|
183 | /** Implementation of the ViewCell BSP tree. */ |
---|
184 | class BspTree |
---|
185 | { |
---|
186 | public: |
---|
187 | |
---|
188 | /** Additional data which is passed down the BSP tree during traversal. |
---|
189 | */ |
---|
190 | struct BspTraversalData |
---|
191 | { |
---|
192 | /// the current node |
---|
193 | BspNode *mNode; |
---|
194 | /// parent of current node |
---|
195 | BspInterior *mParent; |
---|
196 | /// polygonal data for splitting |
---|
197 | PolygonContainer *mPolygons; |
---|
198 | /// current depth |
---|
199 | int mDepth; |
---|
200 | |
---|
201 | BspTraversalData() {} |
---|
202 | |
---|
203 | BspTraversalData(BspNode *node, BspInterior *parent, PolygonContainer *polys, const int depth): |
---|
204 | mNode(node), mParent(parent), mPolygons(polys), mDepth(depth) {} |
---|
205 | }; |
---|
206 | |
---|
207 | typedef std::stack<BspTraversalData> BspTraversalStack; |
---|
208 | |
---|
209 | /// BSP tree construction type |
---|
210 | enum {VIEWCELLS, SCENE_GEOMETRY}; |
---|
211 | |
---|
212 | /** Default constructor creating an empty tree. |
---|
213 | */ |
---|
214 | BspTree(); |
---|
215 | |
---|
216 | ~BspTree(); |
---|
217 | |
---|
218 | const BspTreeStatistics &GetStatistics() const; |
---|
219 | |
---|
220 | /** Constructs tree using the given list of viewcells.
|
---|
221 | A pointer to the appropriate viewcell is stored within each leaf.
|
---|
222 | Many leafs can point to the same viewcell.
|
---|
223 | */
|
---|
224 | void Construct(const ViewCellContainer &viewCells);
|
---|
225 | /** Constructs tree using the given list of objects. Each leaf is taken as viewcell.
|
---|
226 | No objects are treated as viewcells explicitly.
|
---|
227 |
|
---|
228 | @param objects list of objects
|
---|
229 | */
|
---|
230 | void Construct(const ObjectContainer &objects); |
---|
231 | |
---|
232 | |
---|
233 | protected: |
---|
234 | |
---|
235 | /** Evaluates plane classification with respect to the plane's |
---|
236 | contribution for a balanced tree. |
---|
237 | */ |
---|
238 | static inline int EvalForBalancedTree(const int classficiation); |
---|
239 | /** Evaluates plane classification with respect to the plane's |
---|
240 | contribution for a minimum number splits in the tree. |
---|
241 | */ |
---|
242 | static inline int EvalForLeastSplits(const int classification); |
---|
243 | |
---|
244 | /** Evaluates the contribution of the candidate split plane. |
---|
245 | */ |
---|
246 | int EvalSplitPlane(PolygonContainer *polygons, const Plane3 &candidatePlane) const; |
---|
247 | |
---|
248 | /** Evaluates tree stats in the BSP tree leafs. |
---|
249 | */ |
---|
250 | void EvaluateLeafStats(const BspTraversalData &data); |
---|
251 | |
---|
252 | /** Subdivides node with respect to the traversal data. |
---|
253 | @param tStack current traversal stack |
---|
254 | @param tData traversal data also holding node to be subdivided |
---|
255 | @param viewCell the view cell that will be represented with this part of the Bsp tree. |
---|
256 | */ |
---|
257 | void Subdivide(BspTraversalStack &tStack, BspTraversalData &tData, ViewCell *viewCell = NULL); |
---|
258 | |
---|
259 | /** Selects a splitting plane. |
---|
260 | @param polygons the polygon data on which the split decition is based |
---|
261 | */ |
---|
262 | Plane3 SelectPlane(PolygonContainer *polygons) const; |
---|
263 |
|
---|
264 | /** Filters next viewcell down the tree and inserts it into the appropriate leaves
|
---|
265 | (i.e., possibly more than one leaf).
|
---|
266 | */
|
---|
267 | void InsertViewCell(ViewCell *viewCell); |
---|
268 |
|
---|
269 | /** Subdivide leaf.
|
---|
270 | @param leaf the leaf to be subdivided
|
---|
271 | @param parent the parent node of this leaf
|
---|
272 | @param polys the input polygons
|
---|
273 | @param depth the current tree depth
|
---|
274 | @param frontPolys the polygons of the front child node as a result from splitting
|
---|
275 | @param backPolys the polygons of the back child node as a result from splitting
|
---|
276 | */
|
---|
277 | BspNode *SubdivideNode(BspLeaf *leaf, BspInterior *parent,
|
---|
278 | PolygonContainer *polys, const int depth,
|
---|
279 | ViewCell *viewCell,
|
---|
280 | PolygonContainer *frontPolys, PolygonContainer *backPolys); |
---|
281 | |
---|
282 | /** Filters polygons down the tree. |
---|
283 | @param node the current BSP node |
---|
284 | @param polys the polygons to be filtered |
---|
285 | @param frontPolys returns the polygons in front of the split plane |
---|
286 | @param backPolys returns the polygons in the back of the split plane |
---|
287 | */ |
---|
288 | void FilterPolygons(BspInterior *node, PolygonContainer *polys, |
---|
289 | PolygonContainer *frontPolys, PolygonContainer *backPolys); |
---|
290 | |
---|
291 | /** Selects the split plane in order to get a balanced tree. |
---|
292 | @param polygons container of polygons |
---|
293 | @param maxTests the maximal number of candidate tests |
---|
294 | */ |
---|
295 | Plane3 SelectPlaneHeuristics(PolygonContainer *polygons, int maxTests) const; |
---|
296 | |
---|
297 | /** Extracts the meshes of the objects and copies them into the mesh. |
---|
298 | */ |
---|
299 | static void Copy2PolygonSoup(const ObjectContainer &objects, PolygonContainer &polys, int maxPolys); |
---|
300 | |
---|
301 | /** copy this mesh into polygons. |
---|
302 | */ |
---|
303 | static void CopyMesh2Polygons(Mesh *mesh, PolygonContainer &polys); |
---|
304 | |
---|
305 | /// Pointer to the root of the tree |
---|
306 | BspNode *mRoot; |
---|
307 | |
---|
308 | /// Pointer to the root cell of the viewspace |
---|
309 | // ViewCell *mRootCell; |
---|
310 | |
---|
311 | BspTreeStatistics mStat; |
---|
312 | |
---|
313 | /// local maximal polygons (changes depending on method) |
---|
314 | int mTermMaxPolygons; |
---|
315 | int mTermMaxDepth; |
---|
316 | |
---|
317 | /// Strategies for choosing next split plane. |
---|
318 | enum {NEXT_POLYGON, LEAST_SPLITS, BALANCED_TREE, COMBINED}; |
---|
319 | |
---|
320 | public: |
---|
321 | /// Parses the environment and stores the global BSP tree parameters |
---|
322 | static void ParseEnvironment(); |
---|
323 | |
---|
324 | /// maximal number of polygons where tree construction is terminated |
---|
325 | static int sTermMaxPolygons; |
---|
326 | /// maximal possible depth |
---|
327 | static int sTermMaxDepth; |
---|
328 | /// strategy to get the best split plane |
---|
329 | static int sSplitPlaneStrategy; |
---|
330 | /// number of candidates evaluated for the next split plane |
---|
331 | static int sMaxCandidates; |
---|
332 | }; |
---|
333 | |
---|
334 | //}; // GtpVisibilityPreprocessor |
---|
335 | |
---|
336 | #endif |
---|