1 | #ifndef _ViewCellBsp_H__ |
---|
2 | #define _ViewCellBsp_H__ |
---|
3 | |
---|
4 | #include "Mesh.h" |
---|
5 | #include "Containers.h" |
---|
6 | #include "Polygon3.h" |
---|
7 | #include <stack> |
---|
8 | |
---|
9 | class ViewCell; |
---|
10 | class BspViewCell; |
---|
11 | class Plane3; |
---|
12 | class BspTree; |
---|
13 | class BspInterior; |
---|
14 | //class Polygon3; |
---|
15 | class AxisAlignedBox3; |
---|
16 | class Ray; |
---|
17 | |
---|
18 | class BspNodeGeometry |
---|
19 | { |
---|
20 | public: |
---|
21 | BspNodeGeometry() |
---|
22 | {}; |
---|
23 | |
---|
24 | BspNodeGeometry(const PolygonContainer &polys, |
---|
25 | const vector<Plane3> &planes, |
---|
26 | const vector<bool> &sides); |
---|
27 | BspNodeGeometry(const vector<Plane3> &planes, |
---|
28 | const vector<bool> &sides); |
---|
29 | |
---|
30 | ~BspNodeGeometry(); |
---|
31 | |
---|
32 | float GetArea() const; |
---|
33 | |
---|
34 | /** Computes new cell based on the old cell definition and a new split plane |
---|
35 | @param side indicates which side of the halfspace |
---|
36 | @returns true if plane contributes to the cell. |
---|
37 | */ |
---|
38 | BspNodeGeometry *ConstructChild(const BspTree &tree, |
---|
39 | const Plane3 &splitPlane, |
---|
40 | const bool side) const; |
---|
41 | |
---|
42 | vector<Plane3> mPlanes; |
---|
43 | vector<bool> mSides; |
---|
44 | PolygonContainer mPolys; |
---|
45 | }; |
---|
46 | |
---|
47 | /** Data structure used for optimized ray casting. |
---|
48 | */ |
---|
49 | struct BspRayTraversalData |
---|
50 | { |
---|
51 | BspNode *mNode; |
---|
52 | Vector3 mExitPoint; |
---|
53 | float mMaxT; |
---|
54 | |
---|
55 | BspRayTraversalData() {} |
---|
56 | |
---|
57 | BspRayTraversalData(BspNode *n, const Vector3 &extp, const float maxt): |
---|
58 | mNode(n), mExitPoint(extp), mMaxT(maxt) |
---|
59 | {} |
---|
60 | }; |
---|
61 | |
---|
62 | /** Data used for passing ray data down the tree. |
---|
63 | */ |
---|
64 | struct BoundedRay |
---|
65 | { |
---|
66 | Ray *mRay; |
---|
67 | float mMinT; |
---|
68 | float mMaxT; |
---|
69 | |
---|
70 | BoundedRay(): mMinT(0), mMaxT(1e6), mRay(NULL) |
---|
71 | {} |
---|
72 | BoundedRay(Ray *r, float minT, float maxT): |
---|
73 | mRay(r), mMinT(minT), mMaxT(maxT) |
---|
74 | {} |
---|
75 | }; |
---|
76 | |
---|
77 | typedef vector<BoundedRay *> BoundedRayContainer; |
---|
78 | |
---|
79 | class BspTreeStatistics |
---|
80 | { |
---|
81 | public: |
---|
82 | // total number of nodes |
---|
83 | int nodes; |
---|
84 | // number of splits |
---|
85 | int splits; |
---|
86 | // totals number of rays |
---|
87 | int rays; |
---|
88 | // maximal reached depth |
---|
89 | int maxDepth; |
---|
90 | // minimal depth |
---|
91 | int minDepth; |
---|
92 | // max depth nodes |
---|
93 | int maxDepthNodes; |
---|
94 | // max number of rays per node |
---|
95 | int maxObjectRefs; |
---|
96 | // accumulated depth (used to compute average) |
---|
97 | int accumDepth; |
---|
98 | // number of initial polygons |
---|
99 | int polys; |
---|
100 | /// samples contributing to pvs |
---|
101 | int contributingSamples; |
---|
102 | /// sample contributions to pvs |
---|
103 | int sampleContributions; |
---|
104 | /// largest pvs |
---|
105 | int largestPvs; |
---|
106 | |
---|
107 | // Constructor |
---|
108 | BspTreeStatistics() |
---|
109 | { |
---|
110 | Reset(); |
---|
111 | } |
---|
112 | |
---|
113 | int Nodes() const {return nodes;} |
---|
114 | int Interior() const { return nodes / 2; } |
---|
115 | int Leaves() const { return (nodes / 2) + 1; } |
---|
116 | double AvgDepth() const { return accumDepth / (double)Leaves();}; // TODO: computation wrong |
---|
117 | |
---|
118 | void Reset() |
---|
119 | { |
---|
120 | nodes = 0; |
---|
121 | splits = 0; |
---|
122 | maxDepthNodes = 0; |
---|
123 | maxDepth = 0; |
---|
124 | minDepth = 99999; |
---|
125 | polys = 0; |
---|
126 | accumDepth = 0; |
---|
127 | |
---|
128 | contributingSamples = 0; |
---|
129 | sampleContributions = 0; |
---|
130 | } |
---|
131 | |
---|
132 | void |
---|
133 | Print(ostream &app) const; |
---|
134 | |
---|
135 | friend ostream &operator<<(ostream &s, const BspTreeStatistics &stat) { |
---|
136 | stat.Print(s); |
---|
137 | return s; |
---|
138 | } |
---|
139 | |
---|
140 | }; |
---|
141 | |
---|
142 | class BspViewCellsStatistics |
---|
143 | { |
---|
144 | public: |
---|
145 | |
---|
146 | /// number of view cells |
---|
147 | int viewCells; |
---|
148 | |
---|
149 | /// size of the PVS |
---|
150 | int pvs; |
---|
151 | |
---|
152 | /// largest PVS of all view cells |
---|
153 | int maxPvs; |
---|
154 | |
---|
155 | /// smallest PVS of all view cells |
---|
156 | int minPvs; |
---|
157 | |
---|
158 | /// view cells with empty PVS |
---|
159 | int emptyPvs; |
---|
160 | |
---|
161 | /// number of bsp leaves covering the view space |
---|
162 | int bspLeaves; |
---|
163 | |
---|
164 | /// largest number of leaves covered by one view cell |
---|
165 | int maxBspLeaves; |
---|
166 | |
---|
167 | |
---|
168 | // Constructor |
---|
169 | BspViewCellsStatistics() |
---|
170 | { |
---|
171 | Reset(); |
---|
172 | } |
---|
173 | |
---|
174 | double AvgBspLeaves() const {return (double)bspLeaves / (double)viewCells;}; |
---|
175 | double AvgPvs() const {return (double)pvs / (double)viewCells;}; |
---|
176 | |
---|
177 | |
---|
178 | void Reset() |
---|
179 | { |
---|
180 | viewCells = 0; |
---|
181 | pvs = 0; |
---|
182 | maxPvs = 0; |
---|
183 | minPvs = 999999; |
---|
184 | emptyPvs = 0; |
---|
185 | bspLeaves = 0; |
---|
186 | maxBspLeaves = 0; |
---|
187 | } |
---|
188 | |
---|
189 | void |
---|
190 | Print(ostream &app) const; |
---|
191 | |
---|
192 | friend ostream &operator<<(ostream &s, const BspViewCellsStatistics &stat) { |
---|
193 | stat.Print(s); |
---|
194 | return s; |
---|
195 | } |
---|
196 | |
---|
197 | }; |
---|
198 | |
---|
199 | /** |
---|
200 | BspNode abstract class serving for interior and leaf node implementation |
---|
201 | */ |
---|
202 | class BspNode |
---|
203 | { |
---|
204 | friend class BspTree; |
---|
205 | |
---|
206 | public: |
---|
207 | BspNode(); |
---|
208 | virtual ~BspNode(){}; |
---|
209 | BspNode(BspInterior *parent); |
---|
210 | |
---|
211 | /** Determines whether this node is a leaf or not |
---|
212 | @return true if leaf |
---|
213 | */ |
---|
214 | virtual bool IsLeaf() const = 0; |
---|
215 | |
---|
216 | /** Determines whether this node is a root |
---|
217 | @return true if root |
---|
218 | */ |
---|
219 | virtual bool IsRoot() const; |
---|
220 | |
---|
221 | /** Returns parent node. |
---|
222 | */ |
---|
223 | BspInterior *GetParent(); |
---|
224 | |
---|
225 | /** Sets parent node. |
---|
226 | */ |
---|
227 | void SetParent(BspInterior *parent); |
---|
228 | |
---|
229 | |
---|
230 | static int mailID; |
---|
231 | int mailbox; |
---|
232 | |
---|
233 | void Mail() { mailbox = mailID; } |
---|
234 | static void NewMail() { mailID++; } |
---|
235 | bool Mailed() const { return mailbox == mailID; } |
---|
236 | |
---|
237 | protected: |
---|
238 | |
---|
239 | /// parent of this node |
---|
240 | BspInterior *mParent; |
---|
241 | }; |
---|
242 | |
---|
243 | /** BSP interior node implementation |
---|
244 | */ |
---|
245 | class BspInterior : public BspNode |
---|
246 | { |
---|
247 | friend class BspTree; |
---|
248 | public: |
---|
249 | /** Standard contructor taking split plane as argument. |
---|
250 | */ |
---|
251 | BspInterior(const Plane3 &plane); |
---|
252 | /** @return false since it is an interior node |
---|
253 | */ |
---|
254 | bool IsLeaf() const; |
---|
255 | |
---|
256 | BspNode *GetBack(); |
---|
257 | BspNode *GetFront(); |
---|
258 | |
---|
259 | Plane3 *GetPlane(); |
---|
260 | |
---|
261 | void ReplaceChildLink(BspNode *oldChild, BspNode *newChild); |
---|
262 | void SetupChildLinks(BspNode *b, BspNode *f); |
---|
263 | |
---|
264 | /** Splits polygons with respect to the split plane. |
---|
265 | @param polys the polygons to be split. the polygons are consumed and |
---|
266 | distributed to the containers frontPolys, backPolys, coincident. |
---|
267 | @param frontPolys returns the polygons in the front of the split plane |
---|
268 | @param backPolys returns the polygons in the back of the split plane |
---|
269 | @param coincident returns the polygons coincident to the split plane |
---|
270 | |
---|
271 | @returns the number of splits |
---|
272 | */ |
---|
273 | int SplitPolygons(PolygonContainer &polys, |
---|
274 | PolygonContainer &frontPolys, |
---|
275 | PolygonContainer &backPolys, |
---|
276 | PolygonContainer &coincident); |
---|
277 | |
---|
278 | friend ostream &operator<<(ostream &s, const BspInterior &A) |
---|
279 | { |
---|
280 | return s << A.mPlane; |
---|
281 | } |
---|
282 | |
---|
283 | protected: |
---|
284 | |
---|
285 | /// Splitting plane corresponding to this node |
---|
286 | Plane3 mPlane; |
---|
287 | /// back node |
---|
288 | BspNode *mBack; |
---|
289 | /// front node |
---|
290 | BspNode *mFront; |
---|
291 | }; |
---|
292 | |
---|
293 | /** BSP leaf node implementation. |
---|
294 | */ |
---|
295 | class BspLeaf : public BspNode |
---|
296 | { |
---|
297 | friend class BspTree; |
---|
298 | |
---|
299 | public: |
---|
300 | BspLeaf(); |
---|
301 | BspLeaf(BspViewCell *viewCell); |
---|
302 | BspLeaf(BspInterior *parent); |
---|
303 | BspLeaf(BspInterior *parent, BspViewCell *viewCell); |
---|
304 | |
---|
305 | /** @return true since it is an interior node |
---|
306 | */ |
---|
307 | bool IsLeaf() const; |
---|
308 | |
---|
309 | /** Returns pointer of view cell. |
---|
310 | */ |
---|
311 | BspViewCell *GetViewCell() const; |
---|
312 | |
---|
313 | /** Sets pointer to view cell. |
---|
314 | */ |
---|
315 | void SetViewCell(BspViewCell *viewCell); |
---|
316 | |
---|
317 | /** Adds rays to the PVS. |
---|
318 | @param sampleContributions the number contributions of the sampels |
---|
319 | @param contributingSampels the number of contributing rays |
---|
320 | |
---|
321 | */ |
---|
322 | void AddToPvs(const BoundedRayContainer &rays, int &sampleContributions, |
---|
323 | int &contributingSamples); |
---|
324 | |
---|
325 | protected: |
---|
326 | |
---|
327 | /// if NULL this does not correspond to feasible viewcell |
---|
328 | BspViewCell *mViewCell; |
---|
329 | }; |
---|
330 | |
---|
331 | /** Implementation of the view cell BSP tree. |
---|
332 | */ |
---|
333 | class BspTree |
---|
334 | { |
---|
335 | public: |
---|
336 | |
---|
337 | /** Additional data which is passed down the BSP tree during traversal. |
---|
338 | */ |
---|
339 | struct BspTraversalData |
---|
340 | { |
---|
341 | /// the current node |
---|
342 | BspNode *mNode; |
---|
343 | /// polygonal data for splitting |
---|
344 | PolygonContainer *mPolygons; |
---|
345 | /// current depth |
---|
346 | int mDepth; |
---|
347 | /// the view cell associated with this subdivsion |
---|
348 | ViewCell *mViewCell; |
---|
349 | /// rays piercing this node |
---|
350 | BoundedRayContainer *mRays; |
---|
351 | /// area of current node |
---|
352 | float mArea; |
---|
353 | BspNodeGeometry *mCell; |
---|
354 | |
---|
355 | /// pvs size |
---|
356 | int mPvs; |
---|
357 | |
---|
358 | BspTraversalData(): |
---|
359 | mNode(NULL), |
---|
360 | mPolygons(NULL), |
---|
361 | mDepth(0), |
---|
362 | mViewCell(NULL), |
---|
363 | mRays(NULL), |
---|
364 | mPvs(0), |
---|
365 | mArea(0.0), |
---|
366 | mCell(NULL) |
---|
367 | {} |
---|
368 | |
---|
369 | BspTraversalData(BspNode *node, |
---|
370 | PolygonContainer *polys, |
---|
371 | const int depth, |
---|
372 | ViewCell *viewCell, |
---|
373 | BoundedRayContainer *rays, |
---|
374 | int pvs, |
---|
375 | float area, |
---|
376 | BspNodeGeometry *cell): |
---|
377 | mNode(node), |
---|
378 | mPolygons(polys), |
---|
379 | mDepth(depth), |
---|
380 | mViewCell(viewCell), |
---|
381 | mRays(rays), |
---|
382 | mPvs(pvs), |
---|
383 | mArea(area), |
---|
384 | mCell(cell) |
---|
385 | {} |
---|
386 | }; |
---|
387 | |
---|
388 | typedef std::stack<BspTraversalData> BspTraversalStack; |
---|
389 | |
---|
390 | /** Default constructor creating an empty tree. |
---|
391 | @param viewCell view cell corresponding to unbounded space |
---|
392 | */ |
---|
393 | BspTree(BspViewCell *viewCell); |
---|
394 | |
---|
395 | ~BspTree(); |
---|
396 | |
---|
397 | const BspTreeStatistics &GetStatistics() const; |
---|
398 | |
---|
399 | /** Constructs tree using the given list of view cells. |
---|
400 | For this type of construction we filter all view cells down the |
---|
401 | tree. If there is no polygon left, the last split plane |
---|
402 | decides inside or outside of the viewcell. A pointer to the |
---|
403 | appropriate view cell is stored within each leaf. |
---|
404 | Many leafs can point to the same viewcell. |
---|
405 | */ |
---|
406 | void Construct(const ViewCellContainer &viewCells); |
---|
407 | |
---|
408 | /** Constructs tree using the given list of objects. |
---|
409 | @note the objects are not taken as view cells, but the view cells are |
---|
410 | constructed from the subdivision: Each leaf is taken as one viewcell. |
---|
411 | @param objects list of objects |
---|
412 | */ |
---|
413 | void Construct(const ObjectContainer &objects); |
---|
414 | |
---|
415 | /** Constructs the tree from a given set of rays. |
---|
416 | @param sampleRays the set of sample rays the construction is based on |
---|
417 | @param viewCells if not NULL, new view cells are |
---|
418 | created in the leafs and stored in the conatainer |
---|
419 | */ |
---|
420 | void Construct(const RayContainer &sampleRays); |
---|
421 | |
---|
422 | /** Returns list of BSP leaves. |
---|
423 | */ |
---|
424 | void CollectLeaves(vector<BspLeaf *> &leaves) const; |
---|
425 | |
---|
426 | /** Returns box which bounds the whole tree. |
---|
427 | */ |
---|
428 | AxisAlignedBox3 GetBoundingBox()const; |
---|
429 | |
---|
430 | /** Returns root of BSP tree. |
---|
431 | */ |
---|
432 | BspNode *GetRoot() const; |
---|
433 | |
---|
434 | /** Exports Bsp tree to file. |
---|
435 | */ |
---|
436 | bool Export(const string filename); |
---|
437 | |
---|
438 | /** Collects the leaf view cells of the tree |
---|
439 | @param viewCells returns the view cells |
---|
440 | */ |
---|
441 | void CollectViewCells(ViewCellContainer &viewCells) const; |
---|
442 | |
---|
443 | /** A ray is cast possible intersecting the tree. |
---|
444 | @param the ray that is cast. |
---|
445 | @returns the number of intersections with objects stored in the tree. |
---|
446 | */ |
---|
447 | int CastRay(Ray &ray); |
---|
448 | |
---|
449 | /** Set to true if new view cells shall be generated in each leaf. |
---|
450 | */ |
---|
451 | void SetGenerateViewCells(int generateViewCells); |
---|
452 | |
---|
453 | /// bsp tree construction types |
---|
454 | enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_RAYS}; |
---|
455 | |
---|
456 | /** Returns statistics. |
---|
457 | */ |
---|
458 | BspTreeStatistics &GetStat(); |
---|
459 | |
---|
460 | /** finds neighbouring leaves of this tree node. |
---|
461 | */ |
---|
462 | int FindNeighbors(BspNode *n, vector<BspLeaf *> &neighbors, |
---|
463 | const bool onlyUnmailed) const; |
---|
464 | |
---|
465 | /** Constructs geometry associated with the half space intersections |
---|
466 | leading to this node. |
---|
467 | */ |
---|
468 | void ConstructGeometry(BspNode *n, PolygonContainer &cell) const; |
---|
469 | |
---|
470 | /** Construct geometry of view cell. |
---|
471 | */ |
---|
472 | void ConstructGeometry(BspViewCell *vc, PolygonContainer &cell) const; |
---|
473 | |
---|
474 | void ConstructGeometry(BspNode *n, BspNodeGeometry &cell) const; |
---|
475 | |
---|
476 | /** Returns random leaf of BSP tree. |
---|
477 | @param halfspace defines the halfspace from which the leaf is taken. |
---|
478 | */ |
---|
479 | BspLeaf *GetRandomLeaf(const Plane3 &halfspace); |
---|
480 | |
---|
481 | /** Returns random leaf of BSP tree. |
---|
482 | @param onlyUnmailed if only unmailed leaves should be returned. |
---|
483 | */ |
---|
484 | BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false); |
---|
485 | |
---|
486 | /** Returns true if merge criteria are reached. |
---|
487 | */ |
---|
488 | bool ShouldMerge(BspLeaf *front, BspLeaf *back) const; |
---|
489 | |
---|
490 | /** Merges view cells based on some criteria |
---|
491 | E.g., empty view cells can pe purged, view cells which have |
---|
492 | a very similar PVS can be merged to one larger view cell. |
---|
493 | |
---|
494 | @returns true if merge was successful. |
---|
495 | */ |
---|
496 | bool MergeViewCells(BspLeaf *front, BspLeaf *back) const; |
---|
497 | |
---|
498 | /** Traverses tree and counts all view cells as well as their PVS size. |
---|
499 | */ |
---|
500 | void EvaluateViewCellsStats(BspViewCellsStatistics &stat) const; |
---|
501 | |
---|
502 | protected: |
---|
503 | |
---|
504 | // -------------------------------------------------------------- |
---|
505 | // For sorting objects |
---|
506 | // -------------------------------------------------------------- |
---|
507 | struct SortableEntry |
---|
508 | { |
---|
509 | enum {POLY_MIN, POLY_MAX}; |
---|
510 | |
---|
511 | int type; |
---|
512 | float value; |
---|
513 | Polygon3 *poly; |
---|
514 | SortableEntry() {} |
---|
515 | SortableEntry(const int t, const float v, Polygon3 *poly): |
---|
516 | type(t), value(v), poly(poly) {} |
---|
517 | |
---|
518 | bool operator<(const SortableEntry &b) const |
---|
519 | { |
---|
520 | return value < b.value; |
---|
521 | } |
---|
522 | }; |
---|
523 | |
---|
524 | /** Evaluates tree stats in the BSP tree leafs. |
---|
525 | */ |
---|
526 | void EvaluateLeafStats(const BspTraversalData &data); |
---|
527 | |
---|
528 | /** Subdivides node with respect to the traversal data. |
---|
529 | @param tStack current traversal stack |
---|
530 | @param tData traversal data also holding node to be subdivided |
---|
531 | @returns new root of the subtree |
---|
532 | */ |
---|
533 | BspNode *Subdivide(BspTraversalStack &tStack, BspTraversalData &tData); |
---|
534 | |
---|
535 | /** Constructs the tree from the given list of polygons and rays. |
---|
536 | @param polys stores set of polygons on which subdivision may be based |
---|
537 | @param rays storesset of rays on which subdivision may be based |
---|
538 | */ |
---|
539 | void Construct(PolygonContainer *polys, BoundedRayContainer *rays); |
---|
540 | |
---|
541 | /** Selects the best possible splitting plane. |
---|
542 | @param leaf the leaf to be split |
---|
543 | @param polys the polygon list on which the split decition is based |
---|
544 | @param rays ray container on which selection may be based |
---|
545 | @note the polygons can be reordered in the process |
---|
546 | @returns the split plane |
---|
547 | */ |
---|
548 | Plane3 SelectPlane(BspLeaf *leaf, |
---|
549 | BspTraversalData &data, |
---|
550 | BspTraversalData &frontData, |
---|
551 | BspTraversalData &backData); |
---|
552 | |
---|
553 | /** Evaluates the contribution of the candidate split plane. |
---|
554 | |
---|
555 | @param candidatePlane the candidate split plane |
---|
556 | @param polys the polygons the split can be based on |
---|
557 | @param rays the rays the split can be based on |
---|
558 | |
---|
559 | @returns the cost of the candidate split plane |
---|
560 | */ |
---|
561 | float SplitPlaneCost(const Plane3 &candidatePlane, |
---|
562 | BspTraversalData &data, |
---|
563 | BspTraversalData &frontData, |
---|
564 | BspTraversalData &backData) const; |
---|
565 | |
---|
566 | /** Strategies where the effect of the split plane is tested |
---|
567 | on all input rays. |
---|
568 | @returns the cost of the candidate split plane |
---|
569 | */ |
---|
570 | float SplitPlaneCost(const Plane3 &candidatePlane, |
---|
571 | const PolygonContainer &polys) const; |
---|
572 | |
---|
573 | /** Strategies where the effect of the split plane is tested |
---|
574 | on all input rays. |
---|
575 | |
---|
576 | @returns the cost of the candidate split plane |
---|
577 | */ |
---|
578 | float SplitPlaneCost(const Plane3 &candidatePlane, |
---|
579 | const BoundedRayContainer &rays, |
---|
580 | const int pvs, |
---|
581 | const float area, |
---|
582 | const BspNodeGeometry &cell, |
---|
583 | BspTraversalData &frontData, |
---|
584 | BspTraversalData &backData) const; |
---|
585 | |
---|
586 | /** Filters next view cell down the tree and inserts it into the appropriate leaves |
---|
587 | (i.e., possibly more than one leaf). |
---|
588 | */ |
---|
589 | void InsertViewCell(ViewCell *viewCell); |
---|
590 | /** Inserts polygons down the tree. The polygons are filtered until a leaf is reached, |
---|
591 | then further subdivided. |
---|
592 | */ |
---|
593 | void InsertPolygons(PolygonContainer *polys); |
---|
594 | |
---|
595 | /** Subdivide leaf. |
---|
596 | @param leaf the leaf to be subdivided |
---|
597 | |
---|
598 | @param polys the polygons to be split |
---|
599 | @param frontPolys returns the polygons in front of the split plane |
---|
600 | @param backPolys returns the polygons in the back of the split plane |
---|
601 | |
---|
602 | @param rays the polygons to be filtered |
---|
603 | @param frontRays returns the polygons in front of the split plane |
---|
604 | @param backRays returns the polygons in the back of the split plane |
---|
605 | |
---|
606 | @returns the root of the subdivision |
---|
607 | */ |
---|
608 | |
---|
609 | BspInterior *SubdivideNode(BspTraversalData &tData, |
---|
610 | BspTraversalData &frontData, |
---|
611 | BspTraversalData &backData, |
---|
612 | PolygonContainer &coincident); |
---|
613 | |
---|
614 | /** Filters polygons down the tree. |
---|
615 | @param node the current BSP node |
---|
616 | @param polys the polygons to be filtered |
---|
617 | @param frontPolys returns the polygons in front of the split plane |
---|
618 | @param backPolys returns the polygons in the back of the split plane |
---|
619 | */ |
---|
620 | void FilterPolygons(BspInterior *node, |
---|
621 | PolygonContainer *polys, |
---|
622 | PolygonContainer *frontPolys, |
---|
623 | PolygonContainer *backPolys); |
---|
624 | |
---|
625 | /** Selects the split plane in order to construct a tree with |
---|
626 | certain characteristics (e.g., balanced tree, least splits, |
---|
627 | 2.5d aligned) |
---|
628 | @param polygons container of polygons |
---|
629 | @param rays bundle of rays on which the split can be based |
---|
630 | */ |
---|
631 | Plane3 SelectPlaneHeuristics(BspLeaf *leaf, |
---|
632 | BspTraversalData &data, |
---|
633 | BspTraversalData &frontData, |
---|
634 | BspTraversalData &backData); |
---|
635 | |
---|
636 | /** Extracts the meshes of the objects and adds them to polygons. |
---|
637 | Adds object aabb to the aabb of the tree. |
---|
638 | @param maxPolys the maximal number of objects to be stored as polygons |
---|
639 | @returns the number of polygons |
---|
640 | */ |
---|
641 | int AddToPolygonSoup(const ObjectContainer &objects, |
---|
642 | PolygonContainer &polys, |
---|
643 | int maxObjects = 0); |
---|
644 | |
---|
645 | /** Extracts the meshes of the view cells and and adds them to polygons. |
---|
646 | Adds view cell aabb to the aabb of the tree. |
---|
647 | @param maxPolys the maximal number of objects to be stored as polygons |
---|
648 | @returns the number of polygons |
---|
649 | */ |
---|
650 | int AddToPolygonSoup(const ViewCellContainer &viewCells, |
---|
651 | PolygonContainer &polys, |
---|
652 | int maxObjects = 0); |
---|
653 | |
---|
654 | /** Extract polygons of this mesh and add to polygon container. |
---|
655 | @param mesh the mesh that drives the polygon construction |
---|
656 | @param parent the parent intersectable this polygon is constructed from |
---|
657 | @returns number of polygons |
---|
658 | */ |
---|
659 | int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent); |
---|
660 | |
---|
661 | /** returns next candidate index and reorders polygons so no candidate is chosen two times |
---|
662 | @param the current candidate index |
---|
663 | @param max the range of candidates |
---|
664 | */ |
---|
665 | int GetNextCandidateIdx(int currentIdx, PolygonContainer &polys); |
---|
666 | |
---|
667 | /** Helper function which extracts a view cell on the front and the back |
---|
668 | of the split plane. |
---|
669 | @param backViewCell returns view cell on the back of the split plane |
---|
670 | @param frontViewCell returns a view cell on the front of the split plane |
---|
671 | @param coincident container of polygons coincident to the split plane |
---|
672 | @param splitPlane the split plane which decides about back and front |
---|
673 | @param extractBack if a back view cell is extracted |
---|
674 | @param extractFront if a front view cell is extracted |
---|
675 | */ |
---|
676 | void ExtractViewCells(BspTraversalData &frontData, |
---|
677 | BspTraversalData &backData, |
---|
678 | const PolygonContainer &coincident, |
---|
679 | const Plane3 splitPlane) const; |
---|
680 | |
---|
681 | /** Computes best cost ratio for the suface area heuristics for axis aligned |
---|
682 | splits. This heuristics minimizes the cost for ray traversal. |
---|
683 | @param polys the polygons guiding the ratio computation |
---|
684 | @param box the bounding box of the leaf |
---|
685 | @param axis the current split axis |
---|
686 | @param position returns the split position |
---|
687 | @param objectsBack the number of objects in the back of the split plane |
---|
688 | @param objectsFront the number of objects in the front of the split plane |
---|
689 | */ |
---|
690 | float BestCostRatio(const PolygonContainer &polys, |
---|
691 | const AxisAlignedBox3 &box, |
---|
692 | const int axis, |
---|
693 | float &position, |
---|
694 | int &objectsBack, |
---|
695 | int &objectsFront) const; |
---|
696 | |
---|
697 | /** Sorts split candidates for surface area heuristics for axis aligned splits. |
---|
698 | @param polys the input for choosing split candidates |
---|
699 | @param axis the current split axis |
---|
700 | @param splitCandidates returns sorted list of split candidates |
---|
701 | */ |
---|
702 | void SortSplitCandidates(const PolygonContainer &polys, |
---|
703 | const int axis, |
---|
704 | vector<SortableEntry> &splitCandidates) const; |
---|
705 | |
---|
706 | /** Selects an axis aligned split plane. |
---|
707 | Returns true if split is valied |
---|
708 | */ |
---|
709 | bool SelectAxisAlignedPlane(Plane3 &plane, const PolygonContainer &polys) const; |
---|
710 | |
---|
711 | /** Bounds ray and returns minT and maxT. |
---|
712 | @returns true if ray hits BSP tree bounding box |
---|
713 | */ |
---|
714 | bool BoundRay(const Ray &ray, float &minT, float &maxT) const; |
---|
715 | |
---|
716 | /** Subdivides the rays into front and back rays according to the split plane. |
---|
717 | |
---|
718 | @param plane the split plane |
---|
719 | @param rays contains the rays to be split. The rays are |
---|
720 | distributed into front and back rays. |
---|
721 | @param frontRays returns rays on the front side of the plane |
---|
722 | @param backRays returns rays on the back side of the plane |
---|
723 | |
---|
724 | @returns the number of splits |
---|
725 | */ |
---|
726 | int SplitRays(const Plane3 &plane, |
---|
727 | BoundedRayContainer &rays, |
---|
728 | BoundedRayContainer &frontRays, |
---|
729 | BoundedRayContainer &backRays); |
---|
730 | |
---|
731 | |
---|
732 | /** Extracts the split planes representing the space bounded by node n. |
---|
733 | */ |
---|
734 | void ExtractSplitPlanes(BspNode *n, vector<Plane3> &planes, vector<bool> &sides) const; |
---|
735 | |
---|
736 | /** Computes the pvs of the front and back leaf with a given classification. |
---|
737 | */ |
---|
738 | void AddToPvs(Intersectable &obj, |
---|
739 | int &frontPvs, |
---|
740 | int &backPvs, |
---|
741 | const int cf, |
---|
742 | const int frontId, |
---|
743 | const int backId, |
---|
744 | const int frontAndBackId) const; |
---|
745 | |
---|
746 | int ComputePvsSize(const BoundedRayContainer &rays) const; |
---|
747 | |
---|
748 | /// Pointer to the root of the tree |
---|
749 | BspNode *mRoot; |
---|
750 | |
---|
751 | BspTreeStatistics mStat; |
---|
752 | |
---|
753 | /// Strategies for choosing next split plane. |
---|
754 | enum {NO_STRATEGY = 0, |
---|
755 | RANDOM_POLYGON = 1, |
---|
756 | AXIS_ALIGNED = 2, |
---|
757 | LEAST_SPLITS = 4, |
---|
758 | BALANCED_POLYS = 8, |
---|
759 | BALANCED_VIEW_CELLS = 16, |
---|
760 | LARGEST_POLY_AREA = 32, |
---|
761 | VERTICAL_AXIS = 64, |
---|
762 | BLOCKED_RAYS = 128, |
---|
763 | LEAST_RAY_SPLITS = 256, |
---|
764 | BALANCED_RAYS = 512, |
---|
765 | PVS = 1024 |
---|
766 | }; |
---|
767 | |
---|
768 | /// box around the whole view domain |
---|
769 | AxisAlignedBox3 mBox; |
---|
770 | |
---|
771 | /// view cell corresponding to unbounded space |
---|
772 | BspViewCell *mRootCell; |
---|
773 | |
---|
774 | /// should view cells be stored or generated in the leaves? |
---|
775 | bool mGenerateViewCells; |
---|
776 | |
---|
777 | /// if rays should be stored that are piercing this view cell |
---|
778 | bool mStorePiercingRays; |
---|
779 | |
---|
780 | public: |
---|
781 | /// Parses the environment and stores the global BSP tree parameters |
---|
782 | static void ParseEnvironment(); |
---|
783 | |
---|
784 | /// maximal number of polygons before subdivision termination |
---|
785 | static int sTermMaxPolygons; |
---|
786 | /// maximal number of rays before subdivision termination |
---|
787 | static int sTermMaxRays; |
---|
788 | /// maximal possible depth |
---|
789 | static int sTermMaxDepth; |
---|
790 | /// mininam pvs |
---|
791 | static int sTermMinPvs; |
---|
792 | /// strategy to get the best split plane |
---|
793 | static int sSplitPlaneStrategy; |
---|
794 | /// number of candidates evaluated for the next split plane |
---|
795 | static int sMaxPolyCandidates; |
---|
796 | static int sMaxRayCandidates; |
---|
797 | /// BSP tree construction method |
---|
798 | static int sConstructionMethod; |
---|
799 | /// maximal number of polygons for axis aligned split |
---|
800 | static int sTermMaxPolysForAxisAligned; |
---|
801 | /// maximal number of rays for axis aligned split |
---|
802 | static int sTermMaxRaysForAxisAligned; |
---|
803 | /// maximal number of objects for axis aligned split |
---|
804 | static int sTermMaxObjectsForAxisAligned; |
---|
805 | |
---|
806 | /// axis aligned split criteria |
---|
807 | static float sCt_div_ci; |
---|
808 | static float sSplitBorder; |
---|
809 | static float sMaxCostRatio; |
---|
810 | |
---|
811 | // factors guiding the split plane heuristics |
---|
812 | static float sLeastSplitsFactor; |
---|
813 | static float sBalancedPolysFactor; |
---|
814 | static float sBalancedViewCellsFactor; |
---|
815 | static float sVerticalSplitsFactor; |
---|
816 | static float sLargestPolyAreaFactor; |
---|
817 | static float sBlockedRaysFactor; |
---|
818 | static float sLeastRaySplitsFactor; |
---|
819 | static float sBalancedRaysFactor; |
---|
820 | static float sPvsFactor; |
---|
821 | |
---|
822 | //-- thresholds used for view cells are merging |
---|
823 | static int sMinPvsDif; |
---|
824 | static int sMinPvs; |
---|
825 | static int sMaxPvs; |
---|
826 | |
---|
827 | private: |
---|
828 | /** Evaluates split plane classification with respect to the plane's |
---|
829 | contribution for a balanced tree. |
---|
830 | */ |
---|
831 | static float sLeastPolySplitsTable[4]; |
---|
832 | /** Evaluates split plane classification with respect to the plane's |
---|
833 | contribution for a minimum number splits in the tree. |
---|
834 | */ |
---|
835 | static float sBalancedPolysTable[4]; |
---|
836 | /** Evaluates split plane classification with respect to the plane's |
---|
837 | contribution for a minimum number of ray splits. |
---|
838 | */ |
---|
839 | static float sLeastRaySplitsTable[5]; |
---|
840 | /** Evaluates split plane classification with respect to the plane's |
---|
841 | contribution for balanced rays. |
---|
842 | */ |
---|
843 | static float sBalancedRaysTable[5]; |
---|
844 | |
---|
845 | }; |
---|
846 | |
---|
847 | #endif |
---|