[1237] | 1 | #ifndef _OspTree_H__
|
---|
| 2 | #define _OspTree_H__
|
---|
| 3 |
|
---|
| 4 | #include <stack>
|
---|
| 5 |
|
---|
| 6 | #include "Mesh.h"
|
---|
| 7 | #include "Containers.h"
|
---|
| 8 | #include "Statistics.h"
|
---|
| 9 | #include "VssRay.h"
|
---|
| 10 | #include "RayInfo.h"
|
---|
| 11 | #include "gzstream.h"
|
---|
[1239] | 12 | #include "SubdivisionCandidate.h"
|
---|
[1594] | 13 | #include "IntersectableWrapper.h"
|
---|
[1633] | 14 | #include "KdTree.h"
|
---|
[1237] | 15 |
|
---|
| 16 |
|
---|
| 17 | namespace GtpVisibilityPreprocessor {
|
---|
| 18 |
|
---|
| 19 | class ViewCellLeaf;
|
---|
| 20 | class Plane3;
|
---|
| 21 | class AxisAlignedBox3;
|
---|
| 22 | class Ray;
|
---|
| 23 | class ViewCellsStatistics;
|
---|
| 24 | class ViewCellsManager;
|
---|
| 25 | class MergeCandidate;
|
---|
| 26 | class Beam;
|
---|
| 27 | class ViewCellsTree;
|
---|
| 28 | class Environment;
|
---|
| 29 | class VspInterior;
|
---|
| 30 | class VspLeaf;
|
---|
| 31 | class VspNode;
|
---|
| 32 | class KdNode;
|
---|
| 33 | class KdInterior;
|
---|
| 34 | class KdLeaf;
|
---|
| 35 | class OspTree;
|
---|
| 36 | class KdIntersectable;
|
---|
| 37 | class KdTree;
|
---|
| 38 | class VspTree;
|
---|
| 39 | class KdTreeStatistics;
|
---|
| 40 | class SubdivisionCandidate;
|
---|
[1379] | 41 | class HierarchyManager;
|
---|
[1633] | 42 | class KdNode;
|
---|
[1237] | 43 |
|
---|
| 44 |
|
---|
| 45 | /** View space partition statistics.
|
---|
| 46 | */
|
---|
| 47 | class OspTreeStatistics: public StatisticsBase
|
---|
| 48 | {
|
---|
| 49 | public:
|
---|
| 50 | // total number of nodes
|
---|
| 51 | int nodes;
|
---|
| 52 | // number of splits
|
---|
| 53 | int splits[3];
|
---|
| 54 |
|
---|
| 55 | // maximal reached depth
|
---|
| 56 | int maxDepth;
|
---|
| 57 | // minimal depth
|
---|
| 58 | int minDepth;
|
---|
| 59 |
|
---|
| 60 | // max depth nodes
|
---|
| 61 | int maxDepthNodes;
|
---|
| 62 | // minimum depth nodes
|
---|
| 63 | int minDepthNodes;
|
---|
| 64 | // max depth nodes
|
---|
| 65 | int minPvsNodes;
|
---|
| 66 | // minimum area nodes
|
---|
| 67 | int minProbabilityNodes;
|
---|
| 68 | /// nodes termination because of max cost ratio;
|
---|
| 69 | int maxCostNodes;
|
---|
| 70 | // max number of objects per node
|
---|
| 71 | int maxObjectRefs;
|
---|
| 72 | int objectRefs;
|
---|
| 73 | /// samples contributing to pvs
|
---|
| 74 | int contributingSamples;
|
---|
| 75 | /// sample contributions to pvs
|
---|
| 76 | int sampleContributions;
|
---|
| 77 | /// largest pvs
|
---|
| 78 | int maxPvs;
|
---|
| 79 | /// number of invalid leaves
|
---|
| 80 | int invalidLeaves;
|
---|
| 81 | /// accumulated number of rays refs
|
---|
| 82 | int accumRays;
|
---|
[1449] | 83 | /// overall potentially visible objects
|
---|
[1237] | 84 | int pvs;
|
---|
| 85 | // accumulated depth (used to compute average)
|
---|
| 86 | int accumDepth;
|
---|
[1449] | 87 | // global cost ratio violations
|
---|
| 88 | int mGlobalCostMisses;
|
---|
[1237] | 89 |
|
---|
[1449] | 90 |
|
---|
[1237] | 91 | // Constructor
|
---|
| 92 | OspTreeStatistics()
|
---|
| 93 | {
|
---|
| 94 | Reset();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | int Nodes() const {return nodes;}
|
---|
| 98 | int Interior() const { return nodes / 2; }
|
---|
| 99 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
| 100 |
|
---|
| 101 | // TODO: computation wrong
|
---|
| 102 | double AvgDepth() const { return accumDepth / (double)Leaves();};
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 | void Reset()
|
---|
| 106 | {
|
---|
| 107 | nodes = 0;
|
---|
| 108 | for (int i = 0; i < 3; ++ i)
|
---|
| 109 | splits[i] = 0;
|
---|
| 110 |
|
---|
| 111 | maxDepth = 0;
|
---|
| 112 | minDepth = 99999;
|
---|
| 113 | accumDepth = 0;
|
---|
| 114 | pvs = 0;
|
---|
| 115 | maxDepthNodes = 0;
|
---|
| 116 | minPvsNodes = 0;
|
---|
| 117 | minProbabilityNodes = 0;
|
---|
| 118 | maxCostNodes = 0;
|
---|
| 119 | contributingSamples = 0;
|
---|
| 120 | sampleContributions = 0;
|
---|
| 121 | maxPvs = 0;
|
---|
| 122 | invalidLeaves = 0;
|
---|
| 123 | objectRefs = 0;
|
---|
| 124 | maxObjectRefs = 0;
|
---|
[1449] | 125 | mGlobalCostMisses = 0;
|
---|
[1237] | 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | void Print(ostream &app) const;
|
---|
| 130 |
|
---|
| 131 | friend ostream &operator<<(ostream &s, const OspTreeStatistics &stat)
|
---|
| 132 | {
|
---|
| 133 | stat.Print(s);
|
---|
| 134 | return s;
|
---|
| 135 | }
|
---|
| 136 | };
|
---|
| 137 |
|
---|
| 138 |
|
---|
[1633] | 139 | /** Object Space Partitioning Kd tree.
|
---|
[1237] | 140 | */
|
---|
| 141 | class OspTree
|
---|
| 142 | {
|
---|
| 143 | friend class ViewCellsParseHandlers;
|
---|
| 144 | friend class HierarchyManager;
|
---|
| 145 |
|
---|
| 146 | public:
|
---|
| 147 |
|
---|
| 148 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
| 149 | */
|
---|
| 150 | class OspTraversalData
|
---|
| 151 | {
|
---|
| 152 | public:
|
---|
| 153 | /// the current node
|
---|
| 154 | KdLeaf *mNode;
|
---|
| 155 | /// current depth
|
---|
| 156 | int mDepth;
|
---|
| 157 | /// rays piercing this node
|
---|
| 158 | RayInfoContainer *mRays;
|
---|
| 159 | /// the probability that this node contains view point
|
---|
| 160 | float mProbability;
|
---|
| 161 | /// the bounding box of the node
|
---|
| 162 | AxisAlignedBox3 mBoundingBox;
|
---|
| 163 | /// pvs size
|
---|
| 164 | float mRenderCost;
|
---|
| 165 | /// how often this branch has missed the max-cost ratio
|
---|
| 166 | int mMaxCostMisses;
|
---|
| 167 | // current axis
|
---|
| 168 | int mAxis;
|
---|
| 169 | // current priority
|
---|
| 170 | float mPriority;
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | OspTraversalData():
|
---|
| 174 | mNode(NULL),
|
---|
| 175 | mRays(NULL),
|
---|
| 176 | mDepth(0),
|
---|
| 177 | mRenderCost(0),
|
---|
| 178 | mProbability(0.0),
|
---|
| 179 | mMaxCostMisses(0),
|
---|
| 180 | mPriority(0),
|
---|
| 181 | mAxis(0)
|
---|
| 182 | {}
|
---|
| 183 |
|
---|
| 184 | OspTraversalData(KdLeaf *node,
|
---|
| 185 | const int depth,
|
---|
| 186 | RayInfoContainer *rays,
|
---|
| 187 | const float rc,
|
---|
| 188 | const float p,
|
---|
| 189 | const AxisAlignedBox3 &box):
|
---|
| 190 | mNode(node),
|
---|
| 191 | mDepth(depth),
|
---|
| 192 | mRays(rays),
|
---|
| 193 | mRenderCost(rc),
|
---|
| 194 | mProbability(p),
|
---|
| 195 | mBoundingBox(box),
|
---|
| 196 | mMaxCostMisses(0),
|
---|
| 197 | mPriority(0),
|
---|
| 198 | mAxis(0)
|
---|
| 199 | {}
|
---|
| 200 |
|
---|
| 201 | OspTraversalData(const int depth,
|
---|
| 202 | RayInfoContainer *rays,
|
---|
| 203 | const AxisAlignedBox3 &box):
|
---|
| 204 | mNode(NULL),
|
---|
| 205 | mDepth(depth),
|
---|
| 206 | mRays(rays),
|
---|
| 207 | mRenderCost(0),
|
---|
| 208 | mProbability(0),
|
---|
| 209 | mMaxCostMisses(0),
|
---|
| 210 | mAxis(0),
|
---|
| 211 | mBoundingBox(box)
|
---|
| 212 | {}
|
---|
| 213 |
|
---|
| 214 | /** Returns cost of the traversal data.
|
---|
| 215 | */
|
---|
| 216 | float GetCost() const
|
---|
| 217 | {
|
---|
| 218 | //cout << mPriority << endl;
|
---|
| 219 | return mPriority;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | /// deletes contents and sets them to NULL
|
---|
| 223 | void Clear()
|
---|
| 224 | {
|
---|
| 225 | DEL_PTR(mRays);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | friend bool operator<(const OspTraversalData &a, const OspTraversalData &b)
|
---|
| 230 | {
|
---|
| 231 | return a.GetCost() < b.GetCost();
|
---|
| 232 | }
|
---|
| 233 | };
|
---|
| 234 |
|
---|
| 235 | /** Candidate for a view space split.
|
---|
| 236 | */
|
---|
| 237 | class OspSubdivisionCandidate: public SubdivisionCandidate
|
---|
| 238 | {
|
---|
| 239 | public:
|
---|
| 240 | static OspTree* sOspTree;
|
---|
| 241 |
|
---|
| 242 | /// the current split plane
|
---|
| 243 | AxisAlignedPlane mSplitPlane;
|
---|
| 244 | /// parent data
|
---|
| 245 | OspTraversalData mParentData;
|
---|
| 246 |
|
---|
| 247 | OspSubdivisionCandidate(const OspTraversalData &tData): mParentData(tData)
|
---|
| 248 | {};
|
---|
| 249 |
|
---|
| 250 | int Type() const { return OBJECT_SPACE; }
|
---|
| 251 |
|
---|
[1633] | 252 | void EvalPriority(bool computeSplitplane = true)
|
---|
[1237] | 253 | {
|
---|
[1633] | 254 | if (computeSplitplane)
|
---|
| 255 | sOspTree->EvalSubdivisionCandidate(*this);
|
---|
| 256 | // else TODO
|
---|
[1237] | 257 | }
|
---|
| 258 |
|
---|
| 259 | bool GlobalTerminationCriteriaMet() const
|
---|
| 260 | {
|
---|
| 261 | return sOspTree->GlobalTerminationCriteriaMet(mParentData);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[1633] | 264 | bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet)
|
---|
| 265 | {
|
---|
| 266 | KdNode *n = sOspTree->Subdivide(splitQueue ,this, terminationCriteriaMet);
|
---|
[1237] | 267 |
|
---|
[1633] | 268 | // local or global termination criteria failed
|
---|
| 269 | return !n->IsLeaf();
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
|
---|
| 273 | const bool onlyUnmailed)
|
---|
| 274 | {
|
---|
| 275 | sOspTree->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[1237] | 278 | OspSubdivisionCandidate(const AxisAlignedPlane &plane, const OspTraversalData &tData):
|
---|
| 279 | mSplitPlane(plane), mParentData(tData)
|
---|
| 280 | {}
|
---|
| 281 | };
|
---|
| 282 |
|
---|
| 283 | /** Struct for traversing line segment.
|
---|
| 284 | */
|
---|
| 285 | struct LineTraversalData
|
---|
| 286 | {
|
---|
| 287 | KdNode *mNode;
|
---|
| 288 | Vector3 mExitPoint;
|
---|
| 289 |
|
---|
| 290 | float mMaxT;
|
---|
| 291 |
|
---|
| 292 | LineTraversalData () {}
|
---|
| 293 | LineTraversalData (KdNode *n, const Vector3 &p, const float maxt):
|
---|
| 294 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
| 295 | };
|
---|
| 296 |
|
---|
| 297 | /** Default constructor creating an empty tree.
|
---|
| 298 | */
|
---|
| 299 | OspTree();
|
---|
| 300 |
|
---|
| 301 | /** Copies tree from a kd tree.
|
---|
| 302 | */
|
---|
| 303 | OspTree(const KdTree &kdTree);
|
---|
| 304 |
|
---|
| 305 | /** Default destructor.
|
---|
| 306 | */
|
---|
| 307 | ~OspTree();
|
---|
| 308 |
|
---|
| 309 | /** Returns tree statistics.
|
---|
| 310 | */
|
---|
| 311 | const OspTreeStatistics &GetStatistics() const;
|
---|
| 312 |
|
---|
| 313 | /** Returns bounding box of the specified node.
|
---|
| 314 | */
|
---|
| 315 | AxisAlignedBox3 GetBoundingBox(KdNode *node) const;
|
---|
| 316 |
|
---|
| 317 | /** Returns list of leaves with pvs smaller than
|
---|
| 318 | a certain threshold.
|
---|
| 319 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
| 320 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
| 321 | */
|
---|
| 322 |
|
---|
| 323 | void CollectLeaves(vector<KdLeaf *> &leaves) const;
|
---|
| 324 |
|
---|
| 325 | /** Returns bounding box of the whole tree (= bbox of root node)
|
---|
| 326 | */
|
---|
| 327 | AxisAlignedBox3 GetBoundingBox()const;
|
---|
| 328 |
|
---|
| 329 | /** Returns root of the view space partitioning tree.
|
---|
| 330 | */
|
---|
| 331 | KdNode *GetRoot() const;
|
---|
| 332 |
|
---|
| 333 | /** Collects the leaf view cells of the tree
|
---|
| 334 | @param viewCells returns the view cells
|
---|
| 335 | */
|
---|
| 336 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
| 337 |
|
---|
| 338 | /** A ray is cast possible intersecting the tree.
|
---|
| 339 | @param the ray that is cast.
|
---|
| 340 | @returns the number of intersections with objects stored in the tree.
|
---|
| 341 | */
|
---|
| 342 | int CastRay(Ray &ray);
|
---|
| 343 |
|
---|
| 344 | /** finds neighbouring leaves of this tree node.
|
---|
| 345 | */
|
---|
| 346 | int FindNeighbors(KdLeaf *n,
|
---|
| 347 | vector<VspLeaf *> &neighbors,
|
---|
| 348 | const bool onlyUnmailed) const;
|
---|
| 349 |
|
---|
| 350 | /** Returns random leaf of BSP tree.
|
---|
| 351 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
| 352 | */
|
---|
| 353 | KdLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
| 354 |
|
---|
| 355 | /** Returns random leaf of BSP tree.
|
---|
| 356 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
| 357 | */
|
---|
| 358 | KdLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
| 359 |
|
---|
| 360 | /** Returns epsilon of this tree.
|
---|
| 361 | */
|
---|
| 362 | float GetEpsilon() const;
|
---|
| 363 |
|
---|
| 364 | /** Casts line segment into the tree.
|
---|
| 365 | @param origin the origin of the line segment
|
---|
| 366 | @param termination the end point of the line segment
|
---|
| 367 | @returns view cells intersecting the line segment.
|
---|
| 368 | */
|
---|
| 369 | int CastLineSegment(const Vector3 &origin,
|
---|
| 370 | const Vector3 &termination,
|
---|
| 371 | ViewCellContainer &viewcells);
|
---|
| 372 |
|
---|
| 373 | /** Sets pointer to view cells manager.
|
---|
| 374 | */
|
---|
| 375 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
| 376 |
|
---|
| 377 | /** Writes tree to output stream
|
---|
| 378 | */
|
---|
| 379 | bool Export(OUT_STREAM &stream);
|
---|
| 380 |
|
---|
| 381 | /** Returns or creates a new intersectable for use in a kd based pvs.
|
---|
| 382 | The OspTree is responsible for destruction of the intersectable.
|
---|
| 383 | */
|
---|
| 384 | KdIntersectable *GetOrCreateKdIntersectable(KdNode *node);
|
---|
| 385 |
|
---|
| 386 | /** Collects rays stored in the leaves.
|
---|
| 387 | */
|
---|
| 388 | void CollectRays(VssRayContainer &rays);
|
---|
| 389 |
|
---|
| 390 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
| 391 | @returns number of view cells found
|
---|
| 392 | */
|
---|
| 393 | int ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
| 394 | ViewCellContainer &viewCells) const;
|
---|
| 395 |
|
---|
| 396 |
|
---|
| 397 | /** Returns kd leaf the point pt lies in, starting from root.
|
---|
| 398 | */
|
---|
| 399 | KdLeaf *GetLeaf(const Vector3 &pt, KdNode *root = NULL) const;
|
---|
| 400 |
|
---|
| 401 |
|
---|
| 402 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
| 403 |
|
---|
| 404 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
| 405 |
|
---|
| 406 | float EvalRenderCost(const VssRayContainer &myrays);
|
---|
| 407 | float EvalLeafCost(const OspTraversalData &tData);
|
---|
| 408 |
|
---|
| 409 | /** Adds this objects to the kd leaf objects.
|
---|
| 410 | @warning: Can corrupt the tree
|
---|
| 411 | */
|
---|
| 412 | void InsertObjects(KdNode *node, const ObjectContainer &objects);
|
---|
| 413 |
|
---|
[1259] | 414 | /** Add the leaf to the pvs of the view cell.
|
---|
| 415 | */
|
---|
| 416 | bool AddLeafToPvs(
|
---|
| 417 | KdLeaf *leaf,
|
---|
| 418 | ViewCell *vc,
|
---|
| 419 | const float pdf,
|
---|
| 420 | float &contribution);
|
---|
| 421 |
|
---|
[1237] | 422 | protected:
|
---|
| 423 |
|
---|
| 424 | // --------------------------------------------------------------
|
---|
| 425 | // For sorting objects
|
---|
| 426 | // --------------------------------------------------------------
|
---|
| 427 | struct SortableEntry
|
---|
| 428 | {
|
---|
| 429 | /** There is a 3th "event" for rays which intersect a
|
---|
| 430 | box in the middle. These "events" don't induce a change in
|
---|
| 431 | pvs size, but may induce a change in view cell volume.
|
---|
| 432 | */
|
---|
| 433 | enum EType
|
---|
| 434 | {
|
---|
| 435 | BOX_MIN,
|
---|
| 436 | BOX_MAX,
|
---|
| 437 | BOX_INTERSECT
|
---|
| 438 | };
|
---|
| 439 |
|
---|
| 440 | int mType;
|
---|
| 441 | //int mPvs;
|
---|
| 442 | float mPos;
|
---|
| 443 | VssRay *mRay;
|
---|
| 444 |
|
---|
| 445 | Intersectable *mObject;
|
---|
| 446 |
|
---|
| 447 | SortableEntry() {}
|
---|
| 448 |
|
---|
| 449 | SortableEntry(const int type,
|
---|
| 450 | //const float pvs,
|
---|
| 451 | const float pos,
|
---|
| 452 | Intersectable *obj,
|
---|
| 453 | VssRay *ray):
|
---|
| 454 | mType(type),
|
---|
| 455 | //mPvs(pvs),
|
---|
| 456 | mPos(pos),
|
---|
| 457 | mObject(obj),
|
---|
| 458 | mRay(ray)
|
---|
| 459 | {}
|
---|
| 460 |
|
---|
| 461 | bool operator<(const SortableEntry &b) const
|
---|
| 462 | {
|
---|
| 463 | return mPos < b.mPos;
|
---|
| 464 | }
|
---|
| 465 | };
|
---|
| 466 |
|
---|
| 467 |
|
---|
| 468 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
| 469 | */
|
---|
| 470 | float EvalLocalSplitCost(const OspTraversalData &data,
|
---|
| 471 | const AxisAlignedBox3 &box,
|
---|
| 472 | const int axis,
|
---|
| 473 | const float &position,
|
---|
| 474 | float &pFront,
|
---|
| 475 | float &pBack) const;
|
---|
| 476 |
|
---|
| 477 | /** Evaluates candidate for splitting.
|
---|
| 478 | */
|
---|
| 479 | void EvalSubdivisionCandidate(OspSubdivisionCandidate &splitData);
|
---|
| 480 |
|
---|
| 481 | /** Computes priority of the traversal data and stores it in tData.
|
---|
| 482 | */
|
---|
| 483 | void EvalPriority(OspTraversalData &tData) const;
|
---|
| 484 |
|
---|
| 485 | /** Evaluates render cost decrease of next split.
|
---|
| 486 | */
|
---|
| 487 | float EvalRenderCostDecrease(const AxisAlignedPlane &candidatePlane,
|
---|
| 488 | const OspTraversalData &data,
|
---|
| 489 | float &normalizedOldRenderCost) const;
|
---|
| 490 |
|
---|
| 491 |
|
---|
| 492 | /** Collects view cells in the subtree under root.
|
---|
| 493 | */
|
---|
| 494 | void CollectViewCells(KdNode *root,
|
---|
| 495 | bool onlyValid,
|
---|
| 496 | ViewCellContainer &viewCells,
|
---|
| 497 | bool onlyUnmailed = false) const;
|
---|
| 498 |
|
---|
| 499 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
| 500 | */
|
---|
| 501 | void EvaluateLeafStats(const OspTraversalData &data);
|
---|
| 502 |
|
---|
| 503 | /** Subdivides node using a best split priority queue.
|
---|
| 504 | @param tQueue the best split priority queue
|
---|
| 505 | @param splitCandidate the candidate for the next split
|
---|
| 506 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
| 507 | @returns new root of the subtree
|
---|
| 508 | */
|
---|
| 509 | KdNode *Subdivide(SplitQueue &tQueue,
|
---|
| 510 | SubdivisionCandidate *splitCandidate,
|
---|
| 511 | const bool globalCriteriaMet);
|
---|
| 512 |
|
---|
| 513 | /** Subdivides leaf.
|
---|
| 514 | @param leaf the leaf to be subdivided
|
---|
| 515 |
|
---|
| 516 | @param polys the polygons to be split
|
---|
| 517 | @param frontPolys returns the polygons in front of the split plane
|
---|
| 518 | @param backPolys returns the polygons in the back of the split plane
|
---|
| 519 |
|
---|
| 520 | @param rays the polygons to be filtered
|
---|
| 521 | @param frontRays returns the polygons in front of the split plane
|
---|
| 522 | @param backRays returns the polygons in the back of the split plane
|
---|
| 523 |
|
---|
| 524 | @returns the root of the subdivision
|
---|
| 525 | */
|
---|
| 526 | KdInterior *SubdivideNode(
|
---|
| 527 | const AxisAlignedPlane &splitPlane,
|
---|
| 528 | const OspTraversalData &tData,
|
---|
| 529 | OspTraversalData &frontData,
|
---|
| 530 | OspTraversalData &backData);
|
---|
| 531 |
|
---|
| 532 | void SplitObjects(KdLeaf *leaf,
|
---|
| 533 | const AxisAlignedPlane & splitPlane,
|
---|
| 534 | const ObjectContainer &objects,
|
---|
| 535 | ObjectContainer &front,
|
---|
| 536 | ObjectContainer &back);
|
---|
| 537 |
|
---|
| 538 | /** does some post processing on the objects in the new child leaves.
|
---|
| 539 | */
|
---|
| 540 | void ProcessMultipleRefs(KdLeaf *leaf) const;
|
---|
| 541 |
|
---|
| 542 | /** Sorts split candidates for cost heuristics using axis aligned splits.
|
---|
| 543 | @param node the current node
|
---|
| 544 | @param axis the current split axis
|
---|
| 545 | */
|
---|
| 546 | void SortSubdivisionCandidates(const OspTraversalData &data,
|
---|
| 547 | const int axis,
|
---|
| 548 | float minBand,
|
---|
| 549 | float maxBand);
|
---|
| 550 |
|
---|
| 551 | /** Computes best cost for axis aligned planes.
|
---|
| 552 | */
|
---|
| 553 | float EvalLocalCostHeuristics(const OspTraversalData &tData,
|
---|
| 554 | const AxisAlignedBox3 &box,
|
---|
| 555 | const int axis,
|
---|
| 556 | float &position,
|
---|
| 557 | int &objectsFront,
|
---|
| 558 | int &objectsBack);
|
---|
| 559 |
|
---|
| 560 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
| 561 |
|
---|
| 562 | @param plane the split plane
|
---|
| 563 | @param rays contains the rays to be split. The rays are
|
---|
| 564 | distributed into front and back rays.
|
---|
| 565 | @param frontRays returns rays on the front side of the plane
|
---|
| 566 | @param backRays returns rays on the back side of the plane
|
---|
| 567 |
|
---|
| 568 | @returns the number of splits
|
---|
| 569 | */
|
---|
| 570 | int SplitRays(const AxisAlignedPlane &plane,
|
---|
| 571 | RayInfoContainer &rays,
|
---|
| 572 | RayInfoContainer &frontRays,
|
---|
| 573 | RayInfoContainer &backRays) const;
|
---|
| 574 |
|
---|
| 575 | int FilterRays(KdLeaf *leaf, const RayInfoContainer &rays, RayInfoContainer &filteredRays);
|
---|
| 576 |
|
---|
| 577 | /** Adds the object to the pvs of the front and back leaf with a given classification.
|
---|
| 578 |
|
---|
| 579 | @param obj the object to be added
|
---|
| 580 | @param cf the ray classification regarding the split plane
|
---|
| 581 | @param frontPvs returns the PVS of the front partition
|
---|
| 582 | @param backPvs returns the PVS of the back partition
|
---|
| 583 |
|
---|
| 584 | */
|
---|
| 585 | void UpdateObjPvsContri(Intersectable *obj,
|
---|
| 586 | const int cf,
|
---|
| 587 | float &frontPvs,
|
---|
| 588 | float &backPvs,
|
---|
| 589 | float &totalPvs) const;
|
---|
| 590 |
|
---|
| 591 | /** Returns true if tree can be terminated.
|
---|
| 592 | */
|
---|
[1251] | 593 | bool LocalTerminationCriteriaMet(const OspTraversalData &data) const;
|
---|
[1237] | 594 |
|
---|
| 595 | /** Returns true if global tree can be terminated.
|
---|
| 596 | */
|
---|
[1251] | 597 | bool GlobalTerminationCriteriaMet(const OspTraversalData &data) const;
|
---|
[1237] | 598 |
|
---|
| 599 | /** Selects an axis aligned for the next split.
|
---|
| 600 | @returns cost for this split
|
---|
| 601 | */
|
---|
| 602 | float SelectSplitPlane(
|
---|
| 603 | const OspTraversalData &tData,
|
---|
[1251] | 604 | AxisAlignedPlane &plane);
|
---|
[1237] | 605 |
|
---|
| 606 | /** Propagates valid flag up the tree.
|
---|
| 607 | */
|
---|
| 608 | void PropagateUpValidity(KdNode *node);
|
---|
| 609 |
|
---|
| 610 | /** Writes the node to disk
|
---|
| 611 | @note: should be implemented as visitor.
|
---|
| 612 | */
|
---|
| 613 | void ExportNode(KdNode *node, OUT_STREAM &stream);
|
---|
| 614 |
|
---|
| 615 | /** Returns estimated memory usage of tree.
|
---|
| 616 | */
|
---|
| 617 | float GetMemUsage() const;
|
---|
| 618 |
|
---|
| 619 | /** Evaluate the contributions of view cell volume of the left and the right view cell.
|
---|
| 620 | */
|
---|
[1259] | 621 | void EvalRayContribution(
|
---|
| 622 | KdLeaf *leaf,
|
---|
| 623 | const VssRay &ray,
|
---|
| 624 | float &renderCost);
|
---|
| 625 |
|
---|
| 626 | void EvalViewCellContribution(
|
---|
| 627 | KdLeaf *leaf,
|
---|
| 628 | ViewCell *viewCell,
|
---|
| 629 | float &renderCost);
|
---|
| 630 |
|
---|
[1237] | 631 | /** Evaluates the influence on the pvs of the event.
|
---|
| 632 | @param ve the visibility event
|
---|
| 633 | @param pvsLeft updates the left pvs
|
---|
| 634 | @param rightPvs updates the right pvs
|
---|
| 635 | */
|
---|
| 636 | void EvalHeuristicsContribution(KdLeaf *leaf,
|
---|
| 637 | const SortableEntry &ci,
|
---|
| 638 | float &renderCost,
|
---|
| 639 | ViewCellContainer &touchedViewCells);
|
---|
| 640 |
|
---|
| 641 | /** Prepares objects for the cost heuristics.
|
---|
| 642 | @returns pvs size of the node
|
---|
| 643 | */
|
---|
[1287] | 644 | float PrepareHeuristics(
|
---|
| 645 | const OspTraversalData &tData,
|
---|
| 646 | ViewCellContainer &touchedViewCells);
|
---|
[1237] | 647 |
|
---|
| 648 | /** Prepares heuristics for a particular ray.
|
---|
| 649 | */
|
---|
[1287] | 650 | void PrepareHeuristics(
|
---|
| 651 | const VssRay &ray,
|
---|
| 652 | ViewCellContainer &touchedViewCells);
|
---|
[1237] | 653 |
|
---|
| 654 | /** Prepares construction for vsp and osp trees.
|
---|
| 655 | */
|
---|
[1287] | 656 | void ComputeBoundingBox(const ObjectContainer &objects);
|
---|
[1237] | 657 |
|
---|
| 658 | void CollectDirtyCandidates(OspSubdivisionCandidate *sc,
|
---|
[1633] | 659 | vector<SubdivisionCandidate *> &dirtyList,
|
---|
| 660 | const bool onlyUnmailed);
|
---|
[1237] | 661 |
|
---|
| 662 | /** Collect view cells which see this kd leaf.
|
---|
| 663 | */
|
---|
| 664 | void CollectViewCells(KdLeaf *leaf,
|
---|
| 665 | ViewCellContainer &viewCells);
|
---|
| 666 |
|
---|
| 667 | /** Rays will be clipped to the bounding box.
|
---|
| 668 | */
|
---|
| 669 | void PreprocessRays(
|
---|
| 670 | KdLeaf *root,
|
---|
| 671 | const VssRayContainer &sampleRays,
|
---|
| 672 | RayInfoContainer &rays);
|
---|
| 673 |
|
---|
| 674 | /** Reads parameters from environment singleton.
|
---|
| 675 | */
|
---|
| 676 | void ReadEnvironment();
|
---|
| 677 |
|
---|
| 678 | /** Returns true if the specified ray end points is inside the kd leaf.
|
---|
| 679 | @param isTermination if origin or termination point should be checked
|
---|
| 680 | */
|
---|
| 681 | bool EndPointInsideNode(KdLeaf *leaf, VssRay &ray, bool isTermination) const;
|
---|
| 682 |
|
---|
| 683 | void AddViewCellVolumeContri(
|
---|
| 684 | ViewCell *vc,
|
---|
| 685 | float &frontVol,
|
---|
| 686 | float &backVol,
|
---|
| 687 | float &frontAndBackVol) const;
|
---|
| 688 |
|
---|
| 689 | /** Classifies and mail view cell with respect to the heuristics contribution.
|
---|
| 690 | Set view cell mail box according to it's influence on
|
---|
| 691 | front (0), back (1) and front / back node (2).
|
---|
| 692 | */
|
---|
| 693 | void MailViewCell(ViewCell *vc, const int cf) const;
|
---|
| 694 |
|
---|
| 695 | int ClassifyRay(VssRay *ray, KdLeaf *leaf, const AxisAlignedPlane &plane) const;
|
---|
| 696 |
|
---|
| 697 | void EvalSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
| 698 |
|
---|
| 699 | void AddSubdivisionStats(const int viewCells,
|
---|
| 700 | const float renderCostDecr,
|
---|
| 701 | const float totalRenderCost);
|
---|
| 702 |
|
---|
| 703 | void EvalViewCellsForHeuristics(const VssRay &ray,
|
---|
| 704 | float &volLeft,
|
---|
| 705 | float &volRight);
|
---|
| 706 |
|
---|
| 707 | float EvalViewCellsVolume(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
| 708 |
|
---|
| 709 | int RemoveParentViewCellsPvs(KdLeaf *leaf,
|
---|
| 710 | const RayInfoContainer &rays
|
---|
| 711 | ) const;
|
---|
| 712 |
|
---|
| 713 | int UpdateViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
| 714 | int CheckViewCellsPvs(KdLeaf *leaf,
|
---|
| 715 | const RayInfoContainer &rays) const;
|
---|
| 716 | bool AddViewCellToObjectPvs(
|
---|
| 717 | Intersectable *obj,
|
---|
| 718 | ViewCell *vc,
|
---|
| 719 | float &contribution,
|
---|
[1633] | 720 | bool onlyUnmailed) const;
|
---|
[1237] | 721 |
|
---|
| 722 | int ClassifyRays(
|
---|
| 723 | const RayInfoContainer &rays,
|
---|
| 724 | KdLeaf *leaf,
|
---|
| 725 | const AxisAlignedPlane &plane,
|
---|
| 726 | RayInfoContainer &frontRays,
|
---|
| 727 | RayInfoContainer &backRays) const;
|
---|
| 728 |
|
---|
| 729 | void CollectTouchedViewCells(
|
---|
| 730 | const RayInfoContainer &rays,
|
---|
| 731 | ViewCellContainer &touchedViewCells) const;
|
---|
| 732 |
|
---|
| 733 | void AddObjectContribution(
|
---|
| 734 | KdLeaf *leaf,
|
---|
| 735 | Intersectable * obj,
|
---|
| 736 | ViewCellContainer &touchedViewCells,
|
---|
| 737 | float &renderCost);
|
---|
| 738 |
|
---|
| 739 | void SubtractObjectContribution(
|
---|
| 740 | KdLeaf *leaf,
|
---|
| 741 | Intersectable * obj,
|
---|
| 742 | ViewCellContainer &touchedViewCells,
|
---|
| 743 | float &renderCost);
|
---|
| 744 |
|
---|
| 745 | SubdivisionCandidate *PrepareConstruction(
|
---|
| 746 | const VssRayContainer &sampleRays,
|
---|
| 747 | const ObjectContainer &objects,
|
---|
| 748 | RayInfoContainer &rays);
|
---|
| 749 |
|
---|
[1259] | 750 |
|
---|
[1237] | 751 | protected:
|
---|
| 752 |
|
---|
| 753 | /// pointer to the hierarchy of view cells
|
---|
| 754 | ViewCellsTree *mViewCellsTree;
|
---|
| 755 |
|
---|
[1379] | 756 | /// pointer to the view space partition
|
---|
| 757 | /// note: should be handled over the hierarchy manager
|
---|
[1237] | 758 | VspTree *mVspTree;
|
---|
| 759 |
|
---|
[1379] | 760 | /// pointer to the hierarchy manager
|
---|
| 761 | HierarchyManager *mHierarchyManager;
|
---|
| 762 |
|
---|
[1237] | 763 | /// The view cells manager
|
---|
| 764 | ViewCellsManager *mViewCellsManager;
|
---|
| 765 |
|
---|
| 766 | /// candidates for placing split planes during cost heuristics
|
---|
| 767 | vector<SortableEntry> *mSubdivisionCandidates;
|
---|
| 768 |
|
---|
| 769 | /// Pointer to the root of the tree
|
---|
| 770 | KdNode *mRoot;
|
---|
| 771 |
|
---|
| 772 | /// Statistics for the object space partition
|
---|
| 773 | OspTreeStatistics mOspStats;
|
---|
| 774 |
|
---|
| 775 | /// box around the whole view domain
|
---|
| 776 | AxisAlignedBox3 mBoundingBox;
|
---|
| 777 |
|
---|
| 778 |
|
---|
[1379] | 779 | //////////////////////////////
|
---|
[1237] | 780 | //-- local termination
|
---|
| 781 |
|
---|
| 782 | /// maximal possible depth
|
---|
| 783 | int mTermMaxDepth;
|
---|
| 784 | /// mininum probability
|
---|
| 785 | float mTermMinProbability;
|
---|
| 786 | /// minimal number of objects
|
---|
| 787 | int mTermMinObjects;
|
---|
| 788 | /// maximal contribution per ray
|
---|
| 789 | float mTermMaxRayContribution;
|
---|
| 790 | /// maximal acceptable cost ratio
|
---|
| 791 | float mTermMaxCostRatio;
|
---|
| 792 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
| 793 | int mTermMissTolerance;
|
---|
| 794 |
|
---|
| 795 |
|
---|
[1379] | 796 | ////////////////////////
|
---|
[1237] | 797 | //-- global criteria
|
---|
| 798 |
|
---|
| 799 | float mTermMinGlobalCostRatio;
|
---|
| 800 | int mTermGlobalCostMissTolerance;
|
---|
| 801 | int mGlobalCostMisses;
|
---|
| 802 |
|
---|
| 803 | /// maximal number of view cells
|
---|
| 804 | int mTermMaxLeaves;
|
---|
| 805 | /// maximal tree memory
|
---|
| 806 | float mMaxMemory;
|
---|
| 807 | /// the tree is out of memory
|
---|
| 808 | bool mOutOfMemory;
|
---|
| 809 |
|
---|
| 810 |
|
---|
[1379] | 811 | ///////////////////////////////////////////
|
---|
[1237] | 812 | //-- split heuristics based parameters
|
---|
| 813 |
|
---|
| 814 | bool mUseCostHeuristics;
|
---|
| 815 | /// balancing factor for PVS criterium
|
---|
| 816 | float mCtDivCi;
|
---|
| 817 | /// if only driving axis should be used for split
|
---|
| 818 | bool mOnlyDrivingAxis;
|
---|
[1379] | 819 | /// represents min and max band for sweep
|
---|
| 820 | float mSplitBorder;
|
---|
| 821 |
|
---|
| 822 | ////////////////////////////////////////////////
|
---|
| 823 |
|
---|
[1237] | 824 | /// current time stamp (used for keeping split history)
|
---|
| 825 | int mTimeStamp;
|
---|
| 826 | // if rays should be stored in leaves
|
---|
| 827 | bool mStoreRays;
|
---|
| 828 | /// epsilon for geometric comparisons
|
---|
| 829 | float mEpsilon;
|
---|
| 830 | /// subdivision stats output file
|
---|
| 831 | ofstream mSubdivisionStats;
|
---|
| 832 | /// keeps track of cost during subdivision
|
---|
| 833 | float mTotalCost;
|
---|
| 834 | /// keeps track of overall pvs size during subdivision
|
---|
| 835 | int mTotalPvsSize;
|
---|
| 836 | /// number of currenly generated view cells
|
---|
| 837 | int mCreatedLeaves;
|
---|
[1379] | 838 |
|
---|
[1237] | 839 | /// weight between render cost decrease and node render cost
|
---|
| 840 | float mRenderCostDecreaseWeight;
|
---|
| 841 |
|
---|
[1379] | 842 | /// stores the kd node intersectables used for pvs
|
---|
[1594] | 843 | KdIntersectableMap mKdIntersectables;
|
---|
[1379] | 844 |
|
---|
[1237] | 845 | private:
|
---|
| 846 |
|
---|
| 847 | bool mCopyFromKdTree;
|
---|
| 848 | };
|
---|
| 849 |
|
---|
| 850 |
|
---|
| 851 | }
|
---|
| 852 |
|
---|
| 853 | #endif
|
---|