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