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