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