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