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