1 | #ifndef _FromPointVisibilityTree_H__
|
---|
2 | #define _FromPointVisibilityTree_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 "RayInfo.h"
|
---|
11 | #include "ViewCellBsp.h"
|
---|
12 |
|
---|
13 | namespace GtpVisibilityPreprocessor {
|
---|
14 |
|
---|
15 | class ViewCell;
|
---|
16 | class Plane3;
|
---|
17 | class FromPointVisibilityTree;
|
---|
18 | class BspInterior;
|
---|
19 | class BspNode;
|
---|
20 | class AxisAlignedBox3;
|
---|
21 | class Ray;
|
---|
22 | class ViewCellsStatistics;
|
---|
23 | class ViewCellsManager;
|
---|
24 | class MergeCandidate;
|
---|
25 | class Beam;
|
---|
26 | class ViewCellsTree;
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 | /** A location in space representing from point-visibility.
|
---|
31 | */
|
---|
32 | struct VisibilityPoint
|
---|
33 | {
|
---|
34 | Vector3 mPosition;
|
---|
35 | /// from point visibility => visible objects, not potentially
|
---|
36 | ObjectPvs mVisibleObjects;
|
---|
37 | };
|
---|
38 |
|
---|
39 |
|
---|
40 | /**
|
---|
41 | This is a view space partitioning specialised BSPtree.
|
---|
42 | There are no polygon splits, but we split the sample rays.
|
---|
43 | The candidates for the next split plane are evaluated only
|
---|
44 | by checking the sampled visibility information.
|
---|
45 | The polygons are employed merely as candidates for the next split planes.
|
---|
46 | */
|
---|
47 | class FromPointVisibilityTree
|
---|
48 | {
|
---|
49 | friend class ViewCellsParseHandlers;
|
---|
50 | friend class VspBspViewCellsManager;
|
---|
51 | public:
|
---|
52 |
|
---|
53 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
54 | */
|
---|
55 | class VspBspTraversalData
|
---|
56 | {
|
---|
57 | public:
|
---|
58 | /// the current node
|
---|
59 | BspNode *mNode;
|
---|
60 | /// polygonal data for splitting
|
---|
61 | PolygonContainer *mPolygons;
|
---|
62 | /// current depth
|
---|
63 | int mDepth;
|
---|
64 | /// rays piercing this node
|
---|
65 | RayInfoContainer *mRays;
|
---|
66 | /// the probability that this node contains view point
|
---|
67 | float mProbability;
|
---|
68 | /// geometry of node as induced by planes
|
---|
69 | BspNodeGeometry *mGeometry;
|
---|
70 | /// pvs size
|
---|
71 | int mPvs;
|
---|
72 | /// how often this branch has missed the max-cost ratio
|
---|
73 | int mMaxCostMisses;
|
---|
74 | /// if this node is a kd-node (i.e., boundaries are axis aligned
|
---|
75 | bool mIsKdNode;
|
---|
76 | // current axis
|
---|
77 | int mAxis;
|
---|
78 | // current priority
|
---|
79 | float mPriority;
|
---|
80 |
|
---|
81 |
|
---|
82 | /** Returns average ray contribution.
|
---|
83 | */
|
---|
84 | float GetAvgRayContribution() const
|
---|
85 | {
|
---|
86 | return (float)mPvs / ((float)mRays->size() + Limits::Small);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | VspBspTraversalData():
|
---|
91 | mNode(NULL),
|
---|
92 | mPolygons(NULL),
|
---|
93 | mDepth(0),
|
---|
94 | mRays(NULL),
|
---|
95 | mPvs(0),
|
---|
96 | mProbability(0.0),
|
---|
97 | mGeometry(NULL),
|
---|
98 | mMaxCostMisses(0),
|
---|
99 | mIsKdNode(false),
|
---|
100 | mPriority(0),
|
---|
101 | mAxis(0)
|
---|
102 | {}
|
---|
103 |
|
---|
104 | VspBspTraversalData(BspNode *node,
|
---|
105 | PolygonContainer *polys,
|
---|
106 | const int depth,
|
---|
107 | RayInfoContainer *rays,
|
---|
108 | const int pvs,
|
---|
109 | const float p,
|
---|
110 | BspNodeGeometry *geom):
|
---|
111 | mNode(node),
|
---|
112 | mPolygons(polys),
|
---|
113 | mDepth(depth),
|
---|
114 | mRays(rays),
|
---|
115 | mPvs(pvs),
|
---|
116 | mProbability(p),
|
---|
117 | mGeometry(geom),
|
---|
118 | mMaxCostMisses(0),
|
---|
119 | mIsKdNode(false),
|
---|
120 | mPriority(0),
|
---|
121 | mAxis(0)
|
---|
122 | {}
|
---|
123 |
|
---|
124 | VspBspTraversalData(PolygonContainer *polys,
|
---|
125 | const int depth,
|
---|
126 | RayInfoContainer *rays,
|
---|
127 | BspNodeGeometry *geom):
|
---|
128 | mNode(NULL),
|
---|
129 | mPolygons(polys),
|
---|
130 | mDepth(depth),
|
---|
131 | mRays(rays),
|
---|
132 | mPvs(0),
|
---|
133 | mProbability(0),
|
---|
134 | mGeometry(geom),
|
---|
135 | mMaxCostMisses(0),
|
---|
136 | mIsKdNode(false),
|
---|
137 | mAxis(0)
|
---|
138 | {}
|
---|
139 |
|
---|
140 | /** Returns cost of the traversal data.
|
---|
141 | */
|
---|
142 | float GetCost() const
|
---|
143 | {
|
---|
144 | //cout << mPriority << endl;
|
---|
145 | return mPriority;
|
---|
146 | }
|
---|
147 |
|
---|
148 | // deletes contents and sets them to NULL
|
---|
149 | void Clear()
|
---|
150 | {
|
---|
151 | DEL_PTR(mPolygons);
|
---|
152 | DEL_PTR(mRays);
|
---|
153 | DEL_PTR(mGeometry);
|
---|
154 | }
|
---|
155 |
|
---|
156 | friend bool operator<(const VspBspTraversalData &a, const VspBspTraversalData &b)
|
---|
157 | {
|
---|
158 | return a.GetCost() < b.GetCost();
|
---|
159 | }
|
---|
160 | };
|
---|
161 |
|
---|
162 |
|
---|
163 | typedef std::priority_queue<VspBspTraversalData> VspBspTraversalQueue;
|
---|
164 |
|
---|
165 |
|
---|
166 | struct VspBspSplitCandidate
|
---|
167 | {
|
---|
168 | /// the current node
|
---|
169 | Plane3 mSplitPlane;
|
---|
170 | /// split axis of this plane (0, 1, 2, or 3 if non-axis-aligned)
|
---|
171 | int mSplitAxis;
|
---|
172 | /// the number of misses of max cost ratio until this split
|
---|
173 | int mMaxCostMisses;
|
---|
174 |
|
---|
175 | // parent data
|
---|
176 | VspBspTraversalData mParentData;
|
---|
177 | // cost of applying this split
|
---|
178 | float mRenderCost;
|
---|
179 |
|
---|
180 | VspBspSplitCandidate(): mRenderCost(0)
|
---|
181 | {};
|
---|
182 |
|
---|
183 | VspBspSplitCandidate(const Plane3 &plane, const VspBspTraversalData &tData):
|
---|
184 | mSplitPlane(plane), mParentData(tData), mRenderCost(0)
|
---|
185 | {}
|
---|
186 |
|
---|
187 | /** Returns cost of the traversal data.
|
---|
188 | */
|
---|
189 | float GetCost() const
|
---|
190 | {
|
---|
191 | #if 1
|
---|
192 | return mRenderCost;
|
---|
193 | #endif
|
---|
194 | #if 0
|
---|
195 | return (float) (-mDepth); // for kd tree
|
---|
196 | #endif
|
---|
197 | }
|
---|
198 |
|
---|
199 | friend bool operator<(const VspBspSplitCandidate &a, const VspBspSplitCandidate &b)
|
---|
200 | {
|
---|
201 | return a.GetCost() < b.GetCost();
|
---|
202 | }
|
---|
203 | };
|
---|
204 |
|
---|
205 | typedef std::priority_queue<VspBspSplitCandidate> VspBspSplitQueue;
|
---|
206 |
|
---|
207 | /** Default constructor creating an empty tree.
|
---|
208 | */
|
---|
209 | FromPointVisibilityTree();
|
---|
210 |
|
---|
211 | /** Default destructor.
|
---|
212 | */
|
---|
213 | ~FromPointVisibilityTree();
|
---|
214 |
|
---|
215 | /** Returns BSP Tree statistics.
|
---|
216 | */
|
---|
217 | const BspTreeStatistics &GetStatistics() const;
|
---|
218 |
|
---|
219 |
|
---|
220 | /** Constructs the tree from a given set of rays.
|
---|
221 | @param sampleRays the set of sample rays the construction is based on
|
---|
222 | */
|
---|
223 | void Construct(const VssRayContainer &sampleRays,
|
---|
224 | AxisAlignedBox3 *forcedBoundingBox);
|
---|
225 |
|
---|
226 |
|
---|
227 | /** Constructs the tree from a given set of visibility points
|
---|
228 | @param visibiltiyPointe visibility points the construction is based on
|
---|
229 | */
|
---|
230 | void Construct(const VisibilityPointsContainer &visibilityPoints,
|
---|
231 | AxisAlignedBox3 *forcedBoundingBox);
|
---|
232 |
|
---|
233 | /** Returns list of BSP leaves with pvs smaller than
|
---|
234 | a certain threshold.
|
---|
235 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
236 | @param maxPvs the maximal pvs (-1 means unlimited)
|
---|
237 | */
|
---|
238 | void CollectLeaves(vector<BspLeaf *> &leaves,
|
---|
239 | const bool onlyUnmailed = false,
|
---|
240 | const int maxPvs = -1) const;
|
---|
241 |
|
---|
242 | /** Returns box which bounds the whole tree.
|
---|
243 | */
|
---|
244 | AxisAlignedBox3 GetBoundingBox()const;
|
---|
245 |
|
---|
246 | /** Returns root of BSP tree.
|
---|
247 | */
|
---|
248 | BspNode *GetRoot() const;
|
---|
249 |
|
---|
250 | /** Collects the leaf view cells of the tree
|
---|
251 | @param viewCells returns the view cells
|
---|
252 | */
|
---|
253 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
254 |
|
---|
255 | /** A ray is cast possible intersecting the tree.
|
---|
256 | @param the ray that is cast.
|
---|
257 | @returns the number of intersections with objects stored in the tree.
|
---|
258 | */
|
---|
259 | int CastRay(Ray &ray);
|
---|
260 |
|
---|
261 | /// bsp tree construction types
|
---|
262 | enum {FROM_INPUT_VIEW_CELLS, FROM_SCENE_GEOMETRY, FROM_SAMPLES};
|
---|
263 |
|
---|
264 | /** finds neighbouring leaves of this tree node.
|
---|
265 | */
|
---|
266 | int FindNeighbors(BspNode *n,
|
---|
267 | vector<BspLeaf *> &neighbors,
|
---|
268 | const bool onlyUnmailed) const;
|
---|
269 |
|
---|
270 | /** Constructs geometry associated with the half space intersections
|
---|
271 | leading to this node.
|
---|
272 | */
|
---|
273 | void ConstructGeometry(BspNode *n, BspNodeGeometry &geom) const;
|
---|
274 |
|
---|
275 | /** Construct geometry of view cell.
|
---|
276 | */
|
---|
277 | void ConstructGeometry(ViewCell *vc, BspNodeGeometry &geom) const;
|
---|
278 |
|
---|
279 | /** Returns random leaf of BSP tree.
|
---|
280 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
281 | */
|
---|
282 | BspLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
283 |
|
---|
284 | /** Returns random leaf of BSP tree.
|
---|
285 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
286 | */
|
---|
287 | BspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
288 |
|
---|
289 | /** Returns epsilon of this tree.
|
---|
290 | */
|
---|
291 | float GetEpsilon() const;
|
---|
292 |
|
---|
293 | /** Casts line segment into the tree.
|
---|
294 | @param origin the origin of the line segment
|
---|
295 | @param termination the end point of the line segment
|
---|
296 | @returns view cells intersecting the line segment.
|
---|
297 | */
|
---|
298 | int CastLineSegment(const Vector3 &origin,
|
---|
299 | const Vector3 &termination,
|
---|
300 | ViewCellContainer &viewcells);
|
---|
301 |
|
---|
302 |
|
---|
303 | /** Sets pointer to view cells manager.
|
---|
304 | */
|
---|
305 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
306 |
|
---|
307 | /** Returns distance from node 1 to node 2.
|
---|
308 | */
|
---|
309 | int TreeDistance(BspNode *n1, BspNode *n2) const;
|
---|
310 |
|
---|
311 | /** Collapses the tree with respect to the view cell partition.
|
---|
312 | @returns number of collapsed nodes
|
---|
313 | */
|
---|
314 | int CollapseTree();
|
---|
315 |
|
---|
316 | /** Returns view cell the current point is located in.
|
---|
317 | */
|
---|
318 | ViewCell *GetViewCell(const Vector3 &point);
|
---|
319 |
|
---|
320 |
|
---|
321 | /** Returns true if this view point is in a valid view space,
|
---|
322 | false otherwise.
|
---|
323 | */
|
---|
324 | bool ViewPointValid(const Vector3 &viewPoint) const;
|
---|
325 |
|
---|
326 | /** Returns view cell corresponding to
|
---|
327 | the invalid view space.
|
---|
328 | */
|
---|
329 | BspViewCell *GetOutOfBoundsCell();
|
---|
330 |
|
---|
331 | /** Writes tree to output stream
|
---|
332 | */
|
---|
333 | bool Export(ofstream &stream);
|
---|
334 |
|
---|
335 | /** Casts beam, i.e. a 5D frustum of rays, into tree.
|
---|
336 | Tests conservative using the bounding box of the nodes.
|
---|
337 | @returns number of view cells it intersected
|
---|
338 | */
|
---|
339 | int CastBeam(Beam &beam);
|
---|
340 |
|
---|
341 | void CollectViewCells(BspNode *root,
|
---|
342 | bool onlyValid,
|
---|
343 | ViewCellContainer &viewCells,
|
---|
344 | bool onlyUnmailed = false) const;
|
---|
345 |
|
---|
346 |
|
---|
347 | int FindApproximateNeighbors(BspNode *n,
|
---|
348 | vector<BspLeaf *> &neighbors,
|
---|
349 | const bool onlyUnmailed) const;
|
---|
350 |
|
---|
351 | /** Checks if tree validity-flags are right
|
---|
352 | with respect to view cell valitiy.
|
---|
353 | If not, marks subtree as invalid.
|
---|
354 | */
|
---|
355 | void ValidateTree();
|
---|
356 |
|
---|
357 | /** Invalid view cells are added to the unbounded space
|
---|
358 | */
|
---|
359 | void CollapseViewCells();
|
---|
360 |
|
---|
361 | /** Collects rays stored in the leaves.
|
---|
362 | */
|
---|
363 | void CollectRays(VssRayContainer &rays);
|
---|
364 |
|
---|
365 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
366 | @returns number of view cells found
|
---|
367 | */
|
---|
368 | int ComputeBoxIntersections(const AxisAlignedBox3 &box, ViewCellContainer &viewCells) const;
|
---|
369 |
|
---|
370 | // pointer to the hierarchy of view cells
|
---|
371 | ViewCellsTree *mViewCellsTree;
|
---|
372 |
|
---|
373 |
|
---|
374 | protected:
|
---|
375 |
|
---|
376 | // --------------------------------------------------------------
|
---|
377 | // For sorting objects
|
---|
378 | // --------------------------------------------------------------
|
---|
379 | struct SortableEntry
|
---|
380 | {
|
---|
381 | enum EType
|
---|
382 | {
|
---|
383 | ERayMin,
|
---|
384 | ERayMax
|
---|
385 | };
|
---|
386 |
|
---|
387 | int type;
|
---|
388 | float value;
|
---|
389 | VssRay *ray;
|
---|
390 |
|
---|
391 | SortableEntry() {}
|
---|
392 | SortableEntry(const int t, const float v, VssRay *r):type(t),
|
---|
393 | value(v), ray(r)
|
---|
394 | {
|
---|
395 | }
|
---|
396 |
|
---|
397 | friend bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
398 | {
|
---|
399 | return a.value < b.value;
|
---|
400 | }
|
---|
401 | };
|
---|
402 |
|
---|
403 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
404 | */
|
---|
405 | float EvalAxisAlignedSplitCost(const VspBspTraversalData &data,
|
---|
406 | const AxisAlignedBox3 &box,
|
---|
407 | const int axis,
|
---|
408 | const float &position,
|
---|
409 | float &pFront,
|
---|
410 | float &pBack) const;
|
---|
411 |
|
---|
412 | /** Evaluates candidate for splitting.
|
---|
413 | */
|
---|
414 | void EvalSplitCandidate(VspBspTraversalData &tData, VspBspSplitCandidate &splitData);
|
---|
415 |
|
---|
416 | /** Computes priority of the traversal data and stores it in tData.
|
---|
417 | */
|
---|
418 | void EvalPriority(VspBspTraversalData &tData) const;
|
---|
419 |
|
---|
420 | float EvalRenderCostDecrease(const Plane3 &candidatePlane,
|
---|
421 | const VspBspTraversalData &data) const;
|
---|
422 |
|
---|
423 | void ConstructWithSplitQueue(const PolygonContainer &polys, RayInfoContainer *rays);
|
---|
424 |
|
---|
425 |
|
---|
426 | /** Returns view cell corresponding to
|
---|
427 | the invalid view space. If it does not exist, it is created.
|
---|
428 | */
|
---|
429 | BspViewCell *GetOrCreateOutOfBoundsCell();
|
---|
430 |
|
---|
431 | /** Collapses the tree with respect to the view cell partition,
|
---|
432 | i.e. leaves having the same view cell are collapsed.
|
---|
433 | @param node the root of the subtree to be collapsed
|
---|
434 | @param collapsed returns the number of collapsed nodes
|
---|
435 | @returns node of type leaf if the node could be collapsed,
|
---|
436 | this node otherwise
|
---|
437 | */
|
---|
438 | BspNode *CollapseTree(BspNode *node, int &collapsed);
|
---|
439 |
|
---|
440 | /** Helper function revalidating the view cell leaf list after merge.
|
---|
441 | */
|
---|
442 | void RepairViewCellsLeafLists();
|
---|
443 |
|
---|
444 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
445 | */
|
---|
446 | void EvaluateLeafStats(const VspBspTraversalData &data);
|
---|
447 |
|
---|
448 | /** Subdivides node with respect to the traversal data.
|
---|
449 | @param tStack current traversal stack
|
---|
450 | @param tData traversal data also holding node to be subdivided
|
---|
451 | @returns new root of the subtree
|
---|
452 | */
|
---|
453 | BspNode *Subdivide(VspBspTraversalQueue &tStack,
|
---|
454 | VspBspTraversalData &tData);
|
---|
455 |
|
---|
456 | BspNode *Subdivide(VspBspSplitQueue &tQueue,
|
---|
457 | VspBspSplitCandidate &splitCandidate);
|
---|
458 |
|
---|
459 | /** Constructs the tree from the given traversal data.
|
---|
460 | @param polys stores set of polygons on which subdivision may be based
|
---|
461 | @param rays storesset of rays on which subdivision may be based
|
---|
462 | */
|
---|
463 | void Construct(const PolygonContainer &polys, RayInfoContainer *rays);
|
---|
464 |
|
---|
465 | /** Selects the best possible splitting plane.
|
---|
466 | @param plane returns the split plane
|
---|
467 | @param leaf the leaf to be split
|
---|
468 | @param polys the polygon list on which the split decition is based
|
---|
469 | @param rays ray container on which selection may be based
|
---|
470 | @note the polygons can be reordered in the process
|
---|
471 | @returns true if the cost of the split is under maxCostRatio
|
---|
472 |
|
---|
473 | */
|
---|
474 | bool SelectPlane(Plane3 &plane,
|
---|
475 | BspLeaf *leaf,
|
---|
476 | VspBspTraversalData &data,
|
---|
477 | VspBspTraversalData &frontData,
|
---|
478 | VspBspTraversalData &backData,
|
---|
479 | int &splitAxis);
|
---|
480 |
|
---|
481 | /** Strategies where the effect of the split plane is tested
|
---|
482 | on all input rays.
|
---|
483 |
|
---|
484 | @returns the cost of the candidate split plane
|
---|
485 | */
|
---|
486 | float EvalSplitPlaneCost(const Plane3 &candidatePlane,
|
---|
487 | const VspBspTraversalData &data,
|
---|
488 | BspNodeGeometry &geomFront,
|
---|
489 | BspNodeGeometry &geomBack,
|
---|
490 | float &pFront,
|
---|
491 | float &pBack) const;
|
---|
492 |
|
---|
493 | /** Subdivides leaf.
|
---|
494 | @param leaf the leaf to be subdivided
|
---|
495 |
|
---|
496 | @param polys the polygons to be split
|
---|
497 | @param frontPolys returns the polygons in front of the split plane
|
---|
498 | @param backPolys returns the polygons in the back of the split plane
|
---|
499 |
|
---|
500 | @param rays the polygons to be filtered
|
---|
501 | @param frontRays returns the polygons in front of the split plane
|
---|
502 | @param backRays returns the polygons in the back of the split plane
|
---|
503 |
|
---|
504 | @returns the root of the subdivision
|
---|
505 | */
|
---|
506 |
|
---|
507 | BspInterior *SubdivideNode(const Plane3 &splitPlane,
|
---|
508 | VspBspTraversalData &tData,
|
---|
509 | VspBspTraversalData &frontData,
|
---|
510 | VspBspTraversalData &backData,
|
---|
511 | PolygonContainer &coincident);
|
---|
512 |
|
---|
513 | /** Extracts the meshes of the objects and adds them to polygons.
|
---|
514 | Adds object aabb to the aabb of the tree.
|
---|
515 | @param maxPolys the maximal number of objects to be stored as polygons
|
---|
516 | @returns the number of polygons
|
---|
517 | */
|
---|
518 | int AddToPolygonSoup(const ObjectContainer &objects,
|
---|
519 | PolygonContainer &polys,
|
---|
520 | int maxObjects = 0);
|
---|
521 |
|
---|
522 | /** Extracts the meshes of the view cells and and adds them to polygons.
|
---|
523 | Adds view cell aabb to the aabb of the tree.
|
---|
524 | @param maxPolys the maximal number of objects to be stored as polygons
|
---|
525 | @returns the number of polygons
|
---|
526 | */
|
---|
527 | int AddToPolygonSoup(const ViewCellContainer &viewCells,
|
---|
528 | PolygonContainer &polys,
|
---|
529 | int maxObjects = 0);
|
---|
530 |
|
---|
531 | /** Extract polygons of this mesh and add to polygon container.
|
---|
532 | @param mesh the mesh that drives the polygon construction
|
---|
533 | @param parent the parent intersectable this polygon is constructed from
|
---|
534 | @returns number of polygons
|
---|
535 | */
|
---|
536 | int AddMeshToPolygons(Mesh *mesh, PolygonContainer &polys, MeshInstance *parent);
|
---|
537 |
|
---|
538 | /** Selects an axis aligned for the next split.
|
---|
539 | @returns cost for this split
|
---|
540 | */
|
---|
541 | float SelectAxisAlignedPlane(Plane3 &plane,
|
---|
542 | const VspBspTraversalData &tData,
|
---|
543 | int &axis,
|
---|
544 | BspNodeGeometry **frontGeom,
|
---|
545 | BspNodeGeometry **backGeom,
|
---|
546 | float &pFront,
|
---|
547 | float &pBack,
|
---|
548 | const bool useKdSplit);
|
---|
549 |
|
---|
550 | /** Sorts split candidates for surface area heuristics for axis aligned splits.
|
---|
551 | @param polys the input for choosing split candidates
|
---|
552 | @param axis the current split axis
|
---|
553 | @param splitCandidates returns sorted list of split candidates
|
---|
554 | */
|
---|
555 | void SortSplitCandidates(const RayInfoContainer &rays,
|
---|
556 | const int axis,
|
---|
557 | float minBand,
|
---|
558 | float maxBand);
|
---|
559 |
|
---|
560 | /** Computes best cost for axis aligned planes.
|
---|
561 | */
|
---|
562 | float BestCostRatioHeuristics(const RayInfoContainer &rays,
|
---|
563 | const AxisAlignedBox3 &box,
|
---|
564 | const int pvsSize,
|
---|
565 | const int axis,
|
---|
566 | float &position);
|
---|
567 |
|
---|
568 | /** Selects an axis aligned split plane.
|
---|
569 | @Returns true if split is valied
|
---|
570 | */
|
---|
571 | bool SelectAxisAlignedPlane(Plane3 &plane, const PolygonContainer &polys) const;
|
---|
572 |
|
---|
573 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
574 |
|
---|
575 | @param plane the split plane
|
---|
576 | @param rays contains the rays to be split. The rays are
|
---|
577 | distributed into front and back rays.
|
---|
578 | @param frontRays returns rays on the front side of the plane
|
---|
579 | @param backRays returns rays on the back side of the plane
|
---|
580 |
|
---|
581 | @returns the number of splits
|
---|
582 | */
|
---|
583 | int SplitRays(const Plane3 &plane,
|
---|
584 | RayInfoContainer &rays,
|
---|
585 | RayInfoContainer &frontRays,
|
---|
586 | RayInfoContainer &backRays) const;
|
---|
587 |
|
---|
588 |
|
---|
589 | /** Extracts the split planes representing the space bounded by node n.
|
---|
590 | */
|
---|
591 | void ExtractHalfSpaces(BspNode *n, vector<Plane3> &halfSpaces) const;
|
---|
592 |
|
---|
593 | /** Adds the object to the pvs of the front and back leaf with a given classification.
|
---|
594 |
|
---|
595 | @param obj the object to be added
|
---|
596 | @param cf the ray classification regarding the split plane
|
---|
597 | @param frontPvs returns the PVS of the front partition
|
---|
598 | @param backPvs returns the PVS of the back partition
|
---|
599 |
|
---|
600 | */
|
---|
601 | void AddObjToPvs(Intersectable *obj,
|
---|
602 | const int cf,
|
---|
603 | float &frontPvs,
|
---|
604 | float &backPvs,
|
---|
605 | float &totalPvs) const;
|
---|
606 |
|
---|
607 | /** Computes PVS size induced by the rays.
|
---|
608 | */
|
---|
609 | int ComputePvsSize(const RayInfoContainer &rays) const;
|
---|
610 |
|
---|
611 | /** Returns true if tree can be terminated.
|
---|
612 | */
|
---|
613 | inline bool LocalTerminationCriteriaMet(const VspBspTraversalData &data) const;
|
---|
614 |
|
---|
615 | /** Returns true if global tree can be terminated.
|
---|
616 | */
|
---|
617 | inline bool GlobalTerminationCriteriaMet(const VspBspTraversalData &data) const;
|
---|
618 |
|
---|
619 | /** Computes accumulated ray lenght of this rays.
|
---|
620 | */
|
---|
621 | float AccumulatedRayLength(const RayInfoContainer &rays) const;
|
---|
622 |
|
---|
623 | /** Splits polygons with respect to the split plane.
|
---|
624 |
|
---|
625 | @param plane the split plane
|
---|
626 | @param polys the polygons to be split. the polygons are consumed and
|
---|
627 | distributed to the containers frontPolys, backPolys, coincident.
|
---|
628 | @param frontPolys returns the polygons in the front of the split plane
|
---|
629 | @param backPolys returns the polygons in the back of the split plane
|
---|
630 | @param coincident returns the polygons coincident to the split plane
|
---|
631 |
|
---|
632 | @returns the number of splits
|
---|
633 | */
|
---|
634 | int SplitPolygons(const Plane3 &plane,
|
---|
635 | PolygonContainer &polys,
|
---|
636 | PolygonContainer &frontPolys,
|
---|
637 | PolygonContainer &backPolys,
|
---|
638 | PolygonContainer &coincident) const;
|
---|
639 |
|
---|
640 | /** Adds ray sample contributions to the PVS.
|
---|
641 | @param sampleContributions the number contributions of the samples
|
---|
642 | @param contributingSampels the number of contributing rays
|
---|
643 |
|
---|
644 | */
|
---|
645 | void AddToPvs(BspLeaf *leaf,
|
---|
646 | const RayInfoContainer &rays,
|
---|
647 | float &sampleContributions,
|
---|
648 | int &contributingSamples);
|
---|
649 |
|
---|
650 |
|
---|
651 |
|
---|
652 |
|
---|
653 |
|
---|
654 |
|
---|
655 | /** Take 3 ray endpoints, where two are minimum and one a maximum
|
---|
656 | point or the other way round.
|
---|
657 | */
|
---|
658 | Plane3 ChooseCandidatePlane(const RayInfoContainer &rays) const;
|
---|
659 |
|
---|
660 | /** Take plane normal as plane normal and the midpoint of the ray.
|
---|
661 | PROBLEM: does not resemble any point where visibility is
|
---|
662 | likely to change
|
---|
663 | */
|
---|
664 | Plane3 ChooseCandidatePlane2(const RayInfoContainer &rays) const;
|
---|
665 |
|
---|
666 | /** Fit the plane between the two lines so that the plane
|
---|
667 | has equal shortest distance to both lines.
|
---|
668 | */
|
---|
669 | Plane3 ChooseCandidatePlane3(const RayInfoContainer &rays) const;
|
---|
670 |
|
---|
671 | /** Collects candidates for merging.
|
---|
672 | @param leaves the leaves to be merged
|
---|
673 | @returns number of leaves in queue
|
---|
674 | */
|
---|
675 | int CollectMergeCandidates(const vector<BspLeaf *> leaves, vector<MergeCandidate> &candidates);
|
---|
676 |
|
---|
677 | /** Collects candidates for the merge in the merge queue.
|
---|
678 | @returns number of leaves in queue
|
---|
679 | */
|
---|
680 | int CollectMergeCandidates(const VssRayContainer &rays, vector<MergeCandidate> &candidates);
|
---|
681 |
|
---|
682 | /** Preprocesses polygons and throws out all polygons which are coincident to
|
---|
683 | the view space box faces (they can be problematic).
|
---|
684 | */
|
---|
685 | void PreprocessPolygons(PolygonContainer &polys);
|
---|
686 |
|
---|
687 | /** Propagates valid flag up the tree.
|
---|
688 | */
|
---|
689 | void PropagateUpValidity(BspNode *node);
|
---|
690 |
|
---|
691 | /** Writes the node to disk
|
---|
692 | @note: should be implemented as visitor
|
---|
693 | */
|
---|
694 | void ExportNode(BspNode *node, ofstream &stream);
|
---|
695 |
|
---|
696 | /** Returns estimated memory usage of tree.
|
---|
697 | */
|
---|
698 | //float GetMemUsage(const VspBspTraversalQueue &tstack) const;
|
---|
699 | float GetMemUsage() const;
|
---|
700 |
|
---|
701 |
|
---|
702 |
|
---|
703 | /// Pointer to the root of the tree
|
---|
704 | BspNode *mRoot;
|
---|
705 |
|
---|
706 | BspTreeStatistics mBspStats;
|
---|
707 |
|
---|
708 | /// Strategies for choosing next split plane.
|
---|
709 | enum {NO_STRATEGY = 0,
|
---|
710 | RANDOM_POLYGON = 1,
|
---|
711 | AXIS_ALIGNED = 2,
|
---|
712 | LEAST_RAY_SPLITS = 256,
|
---|
713 | BALANCED_RAYS = 512,
|
---|
714 | PVS = 1024
|
---|
715 | };
|
---|
716 |
|
---|
717 | /// box around the whole view domain
|
---|
718 | AxisAlignedBox3 mBox;
|
---|
719 |
|
---|
720 | bool mUseCostHeuristics;
|
---|
721 |
|
---|
722 | /// minimal number of rays before subdivision termination
|
---|
723 | int mTermMinRays;
|
---|
724 | /// maximal possible depth
|
---|
725 | int mTermMaxDepth;
|
---|
726 | /// mininum probability
|
---|
727 | float mTermMinProbability;
|
---|
728 | /// mininum PVS
|
---|
729 | int mTermMinPvs;
|
---|
730 | /// maximal contribution per ray
|
---|
731 | float mTermMaxRayContribution;
|
---|
732 | /// minimal accumulated ray length
|
---|
733 | float mTermMinAccRayLength;
|
---|
734 |
|
---|
735 | //HACK
|
---|
736 | int mTermMinPolygons;
|
---|
737 |
|
---|
738 | float mTermMinGlobalCostRatio;
|
---|
739 | int mTermGlobalCostMissTolerance;
|
---|
740 |
|
---|
741 | int mGlobalCostMisses;
|
---|
742 |
|
---|
743 | bool mUseSplitCostQueue;
|
---|
744 | //-- termination criteria for axis aligned split
|
---|
745 |
|
---|
746 | /// minimal number of rays for axis aligned split
|
---|
747 | int mTermMinRaysForAxisAligned;
|
---|
748 | // max ray contribution
|
---|
749 | float mTermMaxRayContriForAxisAligned;
|
---|
750 |
|
---|
751 | /// strategy to get the best split plane
|
---|
752 | int mSplitPlaneStrategy;
|
---|
753 | /// number of candidates evaluated for the next split plane
|
---|
754 | int mMaxPolyCandidates;
|
---|
755 | /// number of candidates for split planes evaluated using the rays
|
---|
756 | int mMaxRayCandidates;
|
---|
757 | /// balancing factor for PVS criterium
|
---|
758 | float mCtDivCi;
|
---|
759 |
|
---|
760 | //-- axis aligned split criteria
|
---|
761 | float mAxisAlignedCtDivCi;
|
---|
762 | /// spezifies the split border of the axis aligned split
|
---|
763 | float mAxisAlignedSplitBorder;
|
---|
764 |
|
---|
765 | /// maximal acceptable cost ratio
|
---|
766 | float mTermMaxCostRatio;
|
---|
767 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
768 | int mTermMissTolerance;
|
---|
769 |
|
---|
770 | //-- factors guiding the split plane heuristics
|
---|
771 | float mLeastRaySplitsFactor;
|
---|
772 | float mBalancedRaysFactor;
|
---|
773 | float mPvsFactor;
|
---|
774 |
|
---|
775 | /// if area or volume should be used for PVS heuristics
|
---|
776 | bool mUseAreaForPvs;
|
---|
777 | /// tolerance for polygon split
|
---|
778 | float mEpsilon;
|
---|
779 | /// maximal number of test rays used to evaluate candidate split plane
|
---|
780 | int mMaxTests;
|
---|
781 | /// normalizes different bsp split plane criteria
|
---|
782 | float mCostNormalizer;
|
---|
783 | /// maximal number of view cells
|
---|
784 | int mMaxViewCells;
|
---|
785 |
|
---|
786 | ofstream mSubdivisionStats;
|
---|
787 |
|
---|
788 | // if rays should be stored in leaves
|
---|
789 | bool mStoreRays;
|
---|
790 |
|
---|
791 | /// if only driving axis should be used for split
|
---|
792 | bool mOnlyDrivingAxis;
|
---|
793 |
|
---|
794 | ViewCellsManager *mViewCellsManager;
|
---|
795 |
|
---|
796 | vector<SortableEntry> *mSplitCandidates;
|
---|
797 |
|
---|
798 |
|
---|
799 | float mRenderCostWeight;
|
---|
800 | /// View cell corresponding to the space outside the valid view space
|
---|
801 | BspViewCell *mOutOfBoundsCell;
|
---|
802 |
|
---|
803 | /// maximal tree memory
|
---|
804 | float mMaxMemory;
|
---|
805 | /// the tree is out of memory
|
---|
806 | bool mOutOfMemory;
|
---|
807 |
|
---|
808 | float mTotalCost;
|
---|
809 | int mTotalPvsSize;
|
---|
810 |
|
---|
811 | float mMinBand;
|
---|
812 | float mMaxBand;
|
---|
813 |
|
---|
814 | //int mSplits;
|
---|
815 | /// subdivision stats output file
|
---|
816 | ofstream mSubdivsionStats;
|
---|
817 | /// if random split axis should be used
|
---|
818 | bool mUseRandomAxis;
|
---|
819 | /// use polygon split whenever there are polys left
|
---|
820 | bool mUsePolygonSplitIfAvailable;
|
---|
821 | /// current time stamp (used for keeping split history)
|
---|
822 | int mTimeStamp;
|
---|
823 | /// number of currenly generated view cells
|
---|
824 | int mCreatedViewCells;
|
---|
825 | /// if vsp bsp tree should simulate octree
|
---|
826 | bool mCirculatingAxis;
|
---|
827 |
|
---|
828 | /// if we should use breath first priority for the splits
|
---|
829 | int mNodePriorityQueueType;
|
---|
830 |
|
---|
831 | bool mEmptyViewCellsMergeAllowed;
|
---|
832 |
|
---|
833 | // priority queue strategy
|
---|
834 | enum {BREATH_FIRST, DEPTH_FIRST, COST_BASED};
|
---|
835 | private:
|
---|
836 |
|
---|
837 | /// Generates unique ids for PVS criterium
|
---|
838 | static void GenerateUniqueIdsForPvs();
|
---|
839 |
|
---|
840 | //-- unique ids for PVS criterium
|
---|
841 | static int sFrontId;
|
---|
842 | static int sBackId;
|
---|
843 | static int sFrontAndBackId;
|
---|
844 | };
|
---|
845 |
|
---|
846 | }
|
---|
847 |
|
---|
848 |
|
---|
849 | #endif
|
---|