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