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