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 Polygon; |
---|
17 | |
---|
18 | typedef std::queue<Polygon *> PolygonQueue; |
---|
19 | |
---|
20 | class BspTreeStatistics // TODO: should have common superclass with KdTreeStatistics |
---|
21 | { |
---|
22 | public: |
---|
23 | // total number of nodes |
---|
24 | int nodes; |
---|
25 | // totals number of rays |
---|
26 | int rays; |
---|
27 | // total number of query domains |
---|
28 | int queryDomains; |
---|
29 | // total number of ray references |
---|
30 | int rayRefs; |
---|
31 | // refs in non empty leafs |
---|
32 | int rayRefsNonZeroQuery; |
---|
33 | // total number of query references |
---|
34 | int objectRefs; |
---|
35 | // nodes with zero queries |
---|
36 | int zeroQueryNodes; |
---|
37 | // max depth nodes |
---|
38 | int maxDepthNodes; |
---|
39 | // max number of rays per node |
---|
40 | int maxObjectRefs; |
---|
41 | // number of dynamically added ray refs |
---|
42 | int addedRayRefs; |
---|
43 | // number of dynamically removed ray refs |
---|
44 | int removedRayRefs; |
---|
45 | |
---|
46 | // Constructor |
---|
47 | BspTreeStatistics() |
---|
48 | { |
---|
49 | Reset(); |
---|
50 | } |
---|
51 | |
---|
52 | int Nodes() const {return nodes;} |
---|
53 | int Interior() const { return nodes/2; } |
---|
54 | int Leaves() const { return (nodes/2) + 1; } |
---|
55 | |
---|
56 | void Reset() |
---|
57 | { |
---|
58 | nodes = 0; |
---|
59 | |
---|
60 | rays = queryDomains = 0; |
---|
61 | rayRefs = rayRefsNonZeroQuery = objectRefs = 0; |
---|
62 | zeroQueryNodes = 0; |
---|
63 | maxDepthNodes = 0; |
---|
64 | //minCostNodes = 0; |
---|
65 | maxObjectRefs = 0; |
---|
66 | addedRayRefs = removedRayRefs = 0; |
---|
67 | } |
---|
68 | |
---|
69 | void |
---|
70 | Print(ostream &app) const; |
---|
71 | |
---|
72 | friend ostream &operator<<(ostream &s, const BspTreeStatistics &stat) { |
---|
73 | stat.Print(s); |
---|
74 | return s; |
---|
75 | } |
---|
76 | |
---|
77 | }; |
---|
78 | |
---|
79 | /** |
---|
80 | BspNode abstract class serving for interior and leaf node implementation |
---|
81 | */ |
---|
82 | class BspNode |
---|
83 | { |
---|
84 | friend BspTree; |
---|
85 | |
---|
86 | public: |
---|
87 | /** Determines whether this node is a leaf or not |
---|
88 | @return true if leaf |
---|
89 | */ |
---|
90 | virtual bool IsLeaf() const = 0; |
---|
91 | |
---|
92 | /** Determines whether this node is a root |
---|
93 | @return true if root |
---|
94 | */ |
---|
95 | virtual bool IsRoot() const; |
---|
96 | |
---|
97 | /** Returns parent node. |
---|
98 | */ |
---|
99 | BspInterior *GetParent(); |
---|
100 | |
---|
101 | protected: |
---|
102 | |
---|
103 | /// parent of this node |
---|
104 | BspInterior *mParent; |
---|
105 | }; |
---|
106 | |
---|
107 | /** BSP interior node implementation |
---|
108 | */ |
---|
109 | class BspInterior : public BspNode |
---|
110 | { |
---|
111 | public: |
---|
112 | /** Standard contructor taking split plane as argument. |
---|
113 | */ |
---|
114 | BspInterior(Plane3 plane); |
---|
115 | /** @return false since it is an interior node |
---|
116 | */ |
---|
117 | bool IsLeaf() const; |
---|
118 | |
---|
119 | BspNode *GetBack(); |
---|
120 | BspNode *GetFront(); |
---|
121 | |
---|
122 | Plane3 *GetPlane(); |
---|
123 | |
---|
124 | void ReplaceChildLink(BspNode *oldChild, BspNode *newChild); |
---|
125 | void SetupChildLinks(BspNode *b, BspNode *f); |
---|
126 | |
---|
127 | /** Splits polygons. |
---|
128 | @param polys the polygons to be split |
---|
129 | @param frontPolys returns the polygons in the front of the split plane |
---|
130 | @param backPolys returns the polygons in the back of the split plane |
---|
131 | */ |
---|
132 | void SplitPolygons(PolygonQueue *polys, PolygonQueue *frontPolys, PolygonQueue *backPolys); |
---|
133 | |
---|
134 | protected: |
---|
135 | |
---|
136 | /// Splitting plane corresponding to this node |
---|
137 | Plane3 mPlane; |
---|
138 | /// back node |
---|
139 | BspNode *mBack; |
---|
140 | /// front node |
---|
141 | BspNode *mFront; |
---|
142 | }; |
---|
143 | |
---|
144 | |
---|
145 | /** BSP leaf node implementation */ |
---|
146 | class BspLeaf : public BspNode |
---|
147 | { |
---|
148 | public: |
---|
149 | BspLeaf(ViewCell *viewCell = NULL); |
---|
150 | |
---|
151 | /** @return true since it is an interior node */ |
---|
152 | bool IsLeaf() const; |
---|
153 | ViewCell *GetViewCell(); |
---|
154 | |
---|
155 | protected: |
---|
156 | |
---|
157 | /// polygonal representation of this viewcell |
---|
158 | /// if NULL this note does not correspond to feasible viewcell |
---|
159 | ViewCell *mViewCell; |
---|
160 | }; |
---|
161 | |
---|
162 | |
---|
163 | /** Class representing a polygon. |
---|
164 | */ |
---|
165 | class Polygon |
---|
166 | { |
---|
167 | public: |
---|
168 | Polygon(); |
---|
169 | Polygon(const VertexContainer &vertices); |
---|
170 | |
---|
171 | /** Copies all the vertices of the face. |
---|
172 | */ |
---|
173 | Polygon(Face *face, Mesh *parent); |
---|
174 | |
---|
175 | /** Returns supporting plane of this polygon. |
---|
176 | */ |
---|
177 | Plane3 GetSupportingPlane(); |
---|
178 | |
---|
179 | /** Splits polygon. |
---|
180 | @param partition the split plane |
---|
181 | @param front the front polygon |
---|
182 | @param back the back polygon |
---|
183 | */ |
---|
184 | void Split(Plane3 *partition, Polygon *front, Polygon *back); |
---|
185 | |
---|
186 | enum {BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT}; |
---|
187 | |
---|
188 | /** Side of the plane where the polygon is located. |
---|
189 | @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT |
---|
190 | */ |
---|
191 | int Side(Plane3 *plane); |
---|
192 | |
---|
193 | static void DeletePolygons(PolygonQueue *polys); |
---|
194 | |
---|
195 | /// vertices are connected in counterclockwise order. |
---|
196 | VertexContainer mVertices; |
---|
197 | }; |
---|
198 | |
---|
199 | /** Implementation of the ViewCell BSP tree. */ |
---|
200 | class BspTree |
---|
201 | { |
---|
202 | public: |
---|
203 | |
---|
204 | /** Additional data which is passed down the BSP tree during traversal. |
---|
205 | */ |
---|
206 | struct BspTraversalData |
---|
207 | { |
---|
208 | /// the current node |
---|
209 | BspNode *mNode; |
---|
210 | /// parent of current node |
---|
211 | BspInterior *mParent; |
---|
212 | /// polygonal data for splitting |
---|
213 | PolygonQueue *mPolys; |
---|
214 | /// current depth |
---|
215 | int mDepth; |
---|
216 | |
---|
217 | BspTraversalData() {} |
---|
218 | |
---|
219 | BspTraversalData(BspNode *node, BspInterior *parent, PolygonQueue *polys, const int depth): |
---|
220 | mNode(node), mParent(parent), mPolys(polys), mDepth(depth) {} |
---|
221 | }; |
---|
222 | |
---|
223 | typedef std::stack<BspTraversalData> BspTraversalStack; |
---|
224 | |
---|
225 | /** Constructs BSP tree from list of view cells. |
---|
226 | */ |
---|
227 | BspTree(const ViewCellContainer &viewCells); |
---|
228 | /** Constructs BSP tree from list of objects. |
---|
229 | @param object list of intersectables |
---|
230 | @param maxPolys maximal allowed number of polygons |
---|
231 | @param maxDepth maximal allowed BSP tree depth |
---|
232 | */ |
---|
233 | BspTree(const ObjectContainer &objects, int maxPolys, int maxDepth); |
---|
234 | |
---|
235 | ~BspTree(); |
---|
236 | |
---|
237 | protected: |
---|
238 | /** Builds a new extension of the tree. |
---|
239 | */ |
---|
240 | void BuildTree(BspTraversalStack &tStack, BspTraversalData ¤tData, ViewCell *viewCell = NULL); |
---|
241 | |
---|
242 | /** Selects a splitting plane from the given polygons. |
---|
243 | */ |
---|
244 | Plane3 SelectPlane(PolygonQueue *polyQueue); |
---|
245 |
|
---|
246 | /** Filters next viewcell down the tree and inserts it into the appropriate leaves
|
---|
247 | (i.e., possibly more than one leaf).
|
---|
248 | */
|
---|
249 | void InsertViewCell(ViewCell *viewCell);
|
---|
250 |
|
---|
251 | /** Subdivides tree according to the given list of viewcells.
|
---|
252 | */
|
---|
253 | void Subdivide(const ViewCellContainer &viewCells);
|
---|
254 | /** Subdivides tree according to the given list of objects.
|
---|
255 | */
|
---|
256 | void Subdivide(const ObjectContainer &objects); |
---|
257 |
|
---|
258 | /** Subdivides leaf.
|
---|
259 | @param leaf the leaf to be subdivided
|
---|
260 | @param parent the parent node of this leaf
|
---|
261 | @param polys the input polygons
|
---|
262 | @param depth the current tree depth
|
---|
263 | @param frontPolys the polygons of the front child node as a result from splitting
|
---|
264 | @param backPolys the polygons of the back child node as a result from splitting
|
---|
265 | */
|
---|
266 | BspNode *SubdivideNode(BspLeaf *leaf, BspInterior *parent,
|
---|
267 | PolygonQueue *polys, const int depth,
|
---|
268 | ViewCell *viewCell,
|
---|
269 | PolygonQueue *frontPolys, PolygonQueue *backPolys); |
---|
270 | |
---|
271 | /** Filters polygons down the tree. |
---|
272 | @param node the current BSP node |
---|
273 | @param polys the polygons to be filtered |
---|
274 | @param frontPolys returns the polygons in front of the split plane |
---|
275 | @param backPolys returns the polygons in the back of the split plane |
---|
276 | */ |
---|
277 | void FilterPolygons(BspInterior *node, PolygonQueue *polys, PolygonQueue *frontPolys, PolygonQueue *backPolys); |
---|
278 | |
---|
279 | /** Extracts the mesh instances of the objects and copies them into the mesh. |
---|
280 | */ |
---|
281 | static void Copy2PolygonSoup(const ObjectContainer &objects, PolygonQueue &polys); |
---|
282 | |
---|
283 | static void CopyMesh2Polygon(Mesh *mesh, PolygonQueue &polys); |
---|
284 | |
---|
285 | /// Pointer to the root of the tree |
---|
286 | BspNode *mRoot; |
---|
287 | /// Pointer to the root cell of the viewspace |
---|
288 | // ViewCell *mRootCell; |
---|
289 | /// maximal number of polygons allowed in leaf |
---|
290 | int mMaxPolys; |
---|
291 | /// maximal depth |
---|
292 | int mMaxDepth; |
---|
293 | |
---|
294 | BspTreeStatistics mStat; |
---|
295 | }; |
---|
296 | |
---|
297 | //}; // GtpVisibilityPreprocessor |
---|
298 | |
---|
299 | #endif |
---|