[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 |
|
---|
| 131 | void Print(ostream &app) const;
|
---|
| 132 |
|
---|
| 133 | friend ostream &operator<<(ostream &s, const OspTreeStatistics &stat)
|
---|
| 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 | {
|
---|
[1667] | 256 | //if (computeSplitplane) TODO
|
---|
| 257 | sOspTree->EvalSubdivisionCandidate(*this);
|
---|
[1237] | 258 | }
|
---|
| 259 |
|
---|
| 260 | bool GlobalTerminationCriteriaMet() const
|
---|
| 261 | {
|
---|
| 262 | return sOspTree->GlobalTerminationCriteriaMet(mParentData);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[1633] | 265 | bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet)
|
---|
| 266 | {
|
---|
| 267 | KdNode *n = sOspTree->Subdivide(splitQueue ,this, terminationCriteriaMet);
|
---|
[1237] | 268 |
|
---|
[1633] | 269 | // local or global termination criteria failed
|
---|
| 270 | return !n->IsLeaf();
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
|
---|
| 274 | const bool onlyUnmailed)
|
---|
| 275 | {
|
---|
| 276 | sOspTree->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[1667] | 279 | float GetPriority() const
|
---|
| 280 | {
|
---|
| 281 | HierarchyManager *hm = sOspTree->mHierarchyManager;
|
---|
| 282 | if (hm->ConsiderMemory())
|
---|
| 283 | {
|
---|
| 284 | const float rc = hm->GetHierarchyStats().mTotalCost - mRenderCostDecrease;
|
---|
| 285 | const float mc = hm->GetHierarchyStats().mMemory +
|
---|
| 286 | (float)mPvsEntriesIncr * ObjectPvs::GetEntrySizeByte();
|
---|
| 287 |
|
---|
| 288 | return - (rc * mc);
|
---|
| 289 | }
|
---|
| 290 | else
|
---|
| 291 | {
|
---|
| 292 | return mPriority;
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[1237] | 296 | OspSubdivisionCandidate(const AxisAlignedPlane &plane, const OspTraversalData &tData):
|
---|
| 297 | mSplitPlane(plane), mParentData(tData)
|
---|
| 298 | {}
|
---|
| 299 | };
|
---|
| 300 |
|
---|
| 301 | /** Struct for traversing line segment.
|
---|
| 302 | */
|
---|
| 303 | struct LineTraversalData
|
---|
| 304 | {
|
---|
| 305 | KdNode *mNode;
|
---|
| 306 | Vector3 mExitPoint;
|
---|
| 307 |
|
---|
| 308 | float mMaxT;
|
---|
| 309 |
|
---|
| 310 | LineTraversalData () {}
|
---|
| 311 | LineTraversalData (KdNode *n, const Vector3 &p, const float maxt):
|
---|
| 312 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
| 313 | };
|
---|
| 314 |
|
---|
| 315 | /** Default constructor creating an empty tree.
|
---|
| 316 | */
|
---|
| 317 | OspTree();
|
---|
| 318 |
|
---|
| 319 | /** Copies tree from a kd tree.
|
---|
| 320 | */
|
---|
| 321 | OspTree(const KdTree &kdTree);
|
---|
| 322 |
|
---|
| 323 | /** Default destructor.
|
---|
| 324 | */
|
---|
| 325 | ~OspTree();
|
---|
| 326 |
|
---|
| 327 | /** Returns tree statistics.
|
---|
| 328 | */
|
---|
| 329 | const OspTreeStatistics &GetStatistics() const;
|
---|
| 330 |
|
---|
| 331 | /** Returns bounding box of the specified node.
|
---|
| 332 | */
|
---|
| 333 | AxisAlignedBox3 GetBoundingBox(KdNode *node) const;
|
---|
| 334 |
|
---|
| 335 | /** Returns list of leaves with pvs smaller than
|
---|
| 336 | a certain threshold.
|
---|
| 337 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
| 338 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
| 339 | */
|
---|
| 340 |
|
---|
| 341 | void CollectLeaves(vector<KdLeaf *> &leaves) const;
|
---|
| 342 |
|
---|
| 343 | /** Returns bounding box of the whole tree (= bbox of root node)
|
---|
| 344 | */
|
---|
| 345 | AxisAlignedBox3 GetBoundingBox()const;
|
---|
| 346 |
|
---|
| 347 | /** Returns root of the view space partitioning tree.
|
---|
| 348 | */
|
---|
| 349 | KdNode *GetRoot() const;
|
---|
| 350 |
|
---|
| 351 | /** Collects the leaf view cells of the tree
|
---|
| 352 | @param viewCells returns the view cells
|
---|
| 353 | */
|
---|
| 354 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
| 355 |
|
---|
| 356 | /** A ray is cast possible intersecting the tree.
|
---|
| 357 | @param the ray that is cast.
|
---|
| 358 | @returns the number of intersections with objects stored in the tree.
|
---|
| 359 | */
|
---|
| 360 | int CastRay(Ray &ray);
|
---|
| 361 |
|
---|
| 362 | /** finds neighbouring leaves of this tree node.
|
---|
| 363 | */
|
---|
| 364 | int FindNeighbors(KdLeaf *n,
|
---|
| 365 | vector<VspLeaf *> &neighbors,
|
---|
| 366 | const bool onlyUnmailed) const;
|
---|
| 367 |
|
---|
| 368 | /** Returns random leaf of BSP tree.
|
---|
| 369 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
| 370 | */
|
---|
| 371 | KdLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
| 372 |
|
---|
| 373 | /** Returns random leaf of BSP tree.
|
---|
| 374 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
| 375 | */
|
---|
| 376 | KdLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
| 377 |
|
---|
| 378 | /** Returns epsilon of this tree.
|
---|
| 379 | */
|
---|
| 380 | float GetEpsilon() const;
|
---|
| 381 |
|
---|
| 382 | /** Casts line segment into the tree.
|
---|
| 383 | @param origin the origin of the line segment
|
---|
| 384 | @param termination the end point of the line segment
|
---|
| 385 | @returns view cells intersecting the line segment.
|
---|
| 386 | */
|
---|
| 387 | int CastLineSegment(const Vector3 &origin,
|
---|
| 388 | const Vector3 &termination,
|
---|
| 389 | ViewCellContainer &viewcells);
|
---|
| 390 |
|
---|
| 391 | /** Sets pointer to view cells manager.
|
---|
| 392 | */
|
---|
| 393 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
| 394 |
|
---|
| 395 | /** Writes tree to output stream
|
---|
| 396 | */
|
---|
| 397 | bool Export(OUT_STREAM &stream);
|
---|
| 398 |
|
---|
| 399 | /** Returns or creates a new intersectable for use in a kd based pvs.
|
---|
| 400 | The OspTree is responsible for destruction of the intersectable.
|
---|
| 401 | */
|
---|
| 402 | KdIntersectable *GetOrCreateKdIntersectable(KdNode *node);
|
---|
| 403 |
|
---|
| 404 | /** Collects rays stored in the leaves.
|
---|
| 405 | */
|
---|
| 406 | void CollectRays(VssRayContainer &rays);
|
---|
| 407 |
|
---|
| 408 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
| 409 | @returns number of view cells found
|
---|
| 410 | */
|
---|
| 411 | int ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
| 412 | ViewCellContainer &viewCells) const;
|
---|
| 413 |
|
---|
| 414 |
|
---|
| 415 | /** Returns kd leaf the point pt lies in, starting from root.
|
---|
| 416 | */
|
---|
| 417 | KdLeaf *GetLeaf(const Vector3 &pt, KdNode *root = NULL) const;
|
---|
| 418 |
|
---|
| 419 |
|
---|
| 420 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
| 421 |
|
---|
| 422 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
| 423 |
|
---|
| 424 | float EvalRenderCost(const VssRayContainer &myrays);
|
---|
| 425 | float EvalLeafCost(const OspTraversalData &tData);
|
---|
| 426 |
|
---|
| 427 | /** Adds this objects to the kd leaf objects.
|
---|
| 428 | @warning: Can corrupt the tree
|
---|
| 429 | */
|
---|
| 430 | void InsertObjects(KdNode *node, const ObjectContainer &objects);
|
---|
| 431 |
|
---|
[1259] | 432 | /** Add the leaf to the pvs of the view cell.
|
---|
| 433 | */
|
---|
| 434 | bool AddLeafToPvs(
|
---|
| 435 | KdLeaf *leaf,
|
---|
| 436 | ViewCell *vc,
|
---|
| 437 | const float pdf,
|
---|
| 438 | float &contribution);
|
---|
| 439 |
|
---|
[1237] | 440 | protected:
|
---|
| 441 |
|
---|
| 442 | // --------------------------------------------------------------
|
---|
| 443 | // For sorting objects
|
---|
| 444 | // --------------------------------------------------------------
|
---|
| 445 | struct SortableEntry
|
---|
| 446 | {
|
---|
| 447 | /** There is a 3th "event" for rays which intersect a
|
---|
| 448 | box in the middle. These "events" don't induce a change in
|
---|
| 449 | pvs size, but may induce a change in view cell volume.
|
---|
| 450 | */
|
---|
| 451 | enum EType
|
---|
| 452 | {
|
---|
| 453 | BOX_MIN,
|
---|
| 454 | BOX_MAX,
|
---|
| 455 | BOX_INTERSECT
|
---|
| 456 | };
|
---|
| 457 |
|
---|
| 458 | int mType;
|
---|
| 459 | //int mPvs;
|
---|
| 460 | float mPos;
|
---|
| 461 | VssRay *mRay;
|
---|
| 462 |
|
---|
| 463 | Intersectable *mObject;
|
---|
| 464 |
|
---|
| 465 | SortableEntry() {}
|
---|
| 466 |
|
---|
| 467 | SortableEntry(const int type,
|
---|
| 468 | //const float pvs,
|
---|
| 469 | const float pos,
|
---|
| 470 | Intersectable *obj,
|
---|
| 471 | VssRay *ray):
|
---|
| 472 | mType(type),
|
---|
| 473 | //mPvs(pvs),
|
---|
| 474 | mPos(pos),
|
---|
| 475 | mObject(obj),
|
---|
| 476 | mRay(ray)
|
---|
| 477 | {}
|
---|
| 478 |
|
---|
| 479 | bool operator<(const SortableEntry &b) const
|
---|
| 480 | {
|
---|
| 481 | return mPos < b.mPos;
|
---|
| 482 | }
|
---|
| 483 | };
|
---|
| 484 |
|
---|
| 485 |
|
---|
| 486 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
| 487 | */
|
---|
| 488 | float EvalLocalSplitCost(const OspTraversalData &data,
|
---|
| 489 | const AxisAlignedBox3 &box,
|
---|
| 490 | const int axis,
|
---|
| 491 | const float &position,
|
---|
| 492 | float &pFront,
|
---|
| 493 | float &pBack) const;
|
---|
| 494 |
|
---|
| 495 | /** Evaluates candidate for splitting.
|
---|
| 496 | */
|
---|
| 497 | void EvalSubdivisionCandidate(OspSubdivisionCandidate &splitData);
|
---|
| 498 |
|
---|
| 499 | /** Computes priority of the traversal data and stores it in tData.
|
---|
| 500 | */
|
---|
| 501 | void EvalPriority(OspTraversalData &tData) const;
|
---|
| 502 |
|
---|
| 503 | /** Evaluates render cost decrease of next split.
|
---|
| 504 | */
|
---|
| 505 | float EvalRenderCostDecrease(const AxisAlignedPlane &candidatePlane,
|
---|
| 506 | const OspTraversalData &data,
|
---|
| 507 | float &normalizedOldRenderCost) const;
|
---|
| 508 |
|
---|
| 509 |
|
---|
| 510 | /** Collects view cells in the subtree under root.
|
---|
| 511 | */
|
---|
| 512 | void CollectViewCells(KdNode *root,
|
---|
| 513 | bool onlyValid,
|
---|
| 514 | ViewCellContainer &viewCells,
|
---|
| 515 | bool onlyUnmailed = false) const;
|
---|
| 516 |
|
---|
| 517 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
| 518 | */
|
---|
| 519 | void EvaluateLeafStats(const OspTraversalData &data);
|
---|
| 520 |
|
---|
| 521 | /** Subdivides node using a best split priority queue.
|
---|
| 522 | @param tQueue the best split priority queue
|
---|
| 523 | @param splitCandidate the candidate for the next split
|
---|
| 524 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
| 525 | @returns new root of the subtree
|
---|
| 526 | */
|
---|
| 527 | KdNode *Subdivide(SplitQueue &tQueue,
|
---|
| 528 | SubdivisionCandidate *splitCandidate,
|
---|
| 529 | const bool globalCriteriaMet);
|
---|
| 530 |
|
---|
| 531 | /** Subdivides leaf.
|
---|
| 532 | @param leaf the leaf to be subdivided
|
---|
| 533 |
|
---|
| 534 | @param polys the polygons to be split
|
---|
| 535 | @param frontPolys returns the polygons in front of the split plane
|
---|
| 536 | @param backPolys returns the polygons in the back of the split plane
|
---|
| 537 |
|
---|
| 538 | @param rays the polygons to be filtered
|
---|
| 539 | @param frontRays returns the polygons in front of the split plane
|
---|
| 540 | @param backRays returns the polygons in the back of the split plane
|
---|
| 541 |
|
---|
| 542 | @returns the root of the subdivision
|
---|
| 543 | */
|
---|
| 544 | KdInterior *SubdivideNode(
|
---|
| 545 | const AxisAlignedPlane &splitPlane,
|
---|
| 546 | const OspTraversalData &tData,
|
---|
| 547 | OspTraversalData &frontData,
|
---|
| 548 | OspTraversalData &backData);
|
---|
| 549 |
|
---|
| 550 | void SplitObjects(KdLeaf *leaf,
|
---|
| 551 | const AxisAlignedPlane & splitPlane,
|
---|
| 552 | const ObjectContainer &objects,
|
---|
| 553 | ObjectContainer &front,
|
---|
| 554 | ObjectContainer &back);
|
---|
| 555 |
|
---|
| 556 | /** does some post processing on the objects in the new child leaves.
|
---|
| 557 | */
|
---|
| 558 | void ProcessMultipleRefs(KdLeaf *leaf) const;
|
---|
| 559 |
|
---|
| 560 | /** Sorts split candidates for cost heuristics using axis aligned splits.
|
---|
| 561 | @param node the current node
|
---|
| 562 | @param axis the current split axis
|
---|
| 563 | */
|
---|
| 564 | void SortSubdivisionCandidates(const OspTraversalData &data,
|
---|
| 565 | const int axis,
|
---|
| 566 | float minBand,
|
---|
| 567 | float maxBand);
|
---|
| 568 |
|
---|
| 569 | /** Computes best cost for axis aligned planes.
|
---|
| 570 | */
|
---|
| 571 | float EvalLocalCostHeuristics(const OspTraversalData &tData,
|
---|
| 572 | const AxisAlignedBox3 &box,
|
---|
| 573 | const int axis,
|
---|
| 574 | float &position,
|
---|
| 575 | int &objectsFront,
|
---|
| 576 | int &objectsBack);
|
---|
| 577 |
|
---|
| 578 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
| 579 |
|
---|
| 580 | @param plane the split plane
|
---|
| 581 | @param rays contains the rays to be split. The rays are
|
---|
| 582 | distributed into front and back rays.
|
---|
| 583 | @param frontRays returns rays on the front side of the plane
|
---|
| 584 | @param backRays returns rays on the back side of the plane
|
---|
| 585 |
|
---|
| 586 | @returns the number of splits
|
---|
| 587 | */
|
---|
| 588 | int SplitRays(const AxisAlignedPlane &plane,
|
---|
| 589 | RayInfoContainer &rays,
|
---|
| 590 | RayInfoContainer &frontRays,
|
---|
| 591 | RayInfoContainer &backRays) const;
|
---|
| 592 |
|
---|
| 593 | int FilterRays(KdLeaf *leaf, const RayInfoContainer &rays, RayInfoContainer &filteredRays);
|
---|
| 594 |
|
---|
| 595 | /** Adds the object to the pvs of the front and back leaf with a given classification.
|
---|
| 596 |
|
---|
| 597 | @param obj the object to be added
|
---|
| 598 | @param cf the ray classification regarding the split plane
|
---|
| 599 | @param frontPvs returns the PVS of the front partition
|
---|
| 600 | @param backPvs returns the PVS of the back partition
|
---|
| 601 |
|
---|
| 602 | */
|
---|
| 603 | void UpdateObjPvsContri(Intersectable *obj,
|
---|
| 604 | const int cf,
|
---|
| 605 | float &frontPvs,
|
---|
| 606 | float &backPvs,
|
---|
| 607 | float &totalPvs) const;
|
---|
| 608 |
|
---|
| 609 | /** Returns true if tree can be terminated.
|
---|
| 610 | */
|
---|
[1251] | 611 | bool LocalTerminationCriteriaMet(const OspTraversalData &data) const;
|
---|
[1237] | 612 |
|
---|
| 613 | /** Returns true if global tree can be terminated.
|
---|
| 614 | */
|
---|
[1251] | 615 | bool GlobalTerminationCriteriaMet(const OspTraversalData &data) const;
|
---|
[1237] | 616 |
|
---|
| 617 | /** Selects an axis aligned for the next split.
|
---|
| 618 | @returns cost for this split
|
---|
| 619 | */
|
---|
| 620 | float SelectSplitPlane(
|
---|
| 621 | const OspTraversalData &tData,
|
---|
[1251] | 622 | AxisAlignedPlane &plane);
|
---|
[1237] | 623 |
|
---|
| 624 | /** Propagates valid flag up the tree.
|
---|
| 625 | */
|
---|
| 626 | void PropagateUpValidity(KdNode *node);
|
---|
| 627 |
|
---|
| 628 | /** Writes the node to disk
|
---|
| 629 | @note: should be implemented as visitor.
|
---|
| 630 | */
|
---|
| 631 | void ExportNode(KdNode *node, OUT_STREAM &stream);
|
---|
| 632 |
|
---|
| 633 | /** Returns estimated memory usage of tree.
|
---|
| 634 | */
|
---|
| 635 | float GetMemUsage() const;
|
---|
| 636 |
|
---|
| 637 | /** Evaluate the contributions of view cell volume of the left and the right view cell.
|
---|
| 638 | */
|
---|
[1259] | 639 | void EvalRayContribution(
|
---|
| 640 | KdLeaf *leaf,
|
---|
| 641 | const VssRay &ray,
|
---|
| 642 | float &renderCost);
|
---|
| 643 |
|
---|
| 644 | void EvalViewCellContribution(
|
---|
| 645 | KdLeaf *leaf,
|
---|
| 646 | ViewCell *viewCell,
|
---|
| 647 | float &renderCost);
|
---|
| 648 |
|
---|
[1237] | 649 | /** Evaluates the influence on the pvs of the event.
|
---|
| 650 | @param ve the visibility event
|
---|
| 651 | @param pvsLeft updates the left pvs
|
---|
| 652 | @param rightPvs updates the right pvs
|
---|
| 653 | */
|
---|
| 654 | void EvalHeuristicsContribution(KdLeaf *leaf,
|
---|
| 655 | const SortableEntry &ci,
|
---|
| 656 | float &renderCost,
|
---|
| 657 | ViewCellContainer &touchedViewCells);
|
---|
| 658 |
|
---|
| 659 | /** Prepares objects for the cost heuristics.
|
---|
| 660 | @returns pvs size of the node
|
---|
| 661 | */
|
---|
[1287] | 662 | float PrepareHeuristics(
|
---|
| 663 | const OspTraversalData &tData,
|
---|
| 664 | ViewCellContainer &touchedViewCells);
|
---|
[1237] | 665 |
|
---|
| 666 | /** Prepares heuristics for a particular ray.
|
---|
| 667 | */
|
---|
[1287] | 668 | void PrepareHeuristics(
|
---|
| 669 | const VssRay &ray,
|
---|
| 670 | ViewCellContainer &touchedViewCells);
|
---|
[1237] | 671 |
|
---|
| 672 | /** Prepares construction for vsp and osp trees.
|
---|
| 673 | */
|
---|
[1287] | 674 | void ComputeBoundingBox(const ObjectContainer &objects);
|
---|
[1237] | 675 |
|
---|
| 676 | void CollectDirtyCandidates(OspSubdivisionCandidate *sc,
|
---|
[1633] | 677 | vector<SubdivisionCandidate *> &dirtyList,
|
---|
| 678 | const bool onlyUnmailed);
|
---|
[1237] | 679 |
|
---|
| 680 | /** Collect view cells which see this kd leaf.
|
---|
| 681 | */
|
---|
| 682 | void CollectViewCells(KdLeaf *leaf,
|
---|
| 683 | ViewCellContainer &viewCells);
|
---|
| 684 |
|
---|
| 685 | /** Rays will be clipped to the bounding box.
|
---|
| 686 | */
|
---|
| 687 | void PreprocessRays(
|
---|
| 688 | KdLeaf *root,
|
---|
| 689 | const VssRayContainer &sampleRays,
|
---|
| 690 | RayInfoContainer &rays);
|
---|
| 691 |
|
---|
| 692 | /** Reads parameters from environment singleton.
|
---|
| 693 | */
|
---|
| 694 | void ReadEnvironment();
|
---|
| 695 |
|
---|
| 696 | /** Returns true if the specified ray end points is inside the kd leaf.
|
---|
| 697 | @param isTermination if origin or termination point should be checked
|
---|
| 698 | */
|
---|
| 699 | bool EndPointInsideNode(KdLeaf *leaf, VssRay &ray, bool isTermination) const;
|
---|
| 700 |
|
---|
| 701 | void AddViewCellVolumeContri(
|
---|
| 702 | ViewCell *vc,
|
---|
| 703 | float &frontVol,
|
---|
| 704 | float &backVol,
|
---|
| 705 | float &frontAndBackVol) const;
|
---|
| 706 |
|
---|
| 707 | /** Classifies and mail view cell with respect to the heuristics contribution.
|
---|
| 708 | Set view cell mail box according to it's influence on
|
---|
| 709 | front (0), back (1) and front / back node (2).
|
---|
| 710 | */
|
---|
| 711 | void MailViewCell(ViewCell *vc, const int cf) const;
|
---|
| 712 |
|
---|
| 713 | int ClassifyRay(VssRay *ray, KdLeaf *leaf, const AxisAlignedPlane &plane) const;
|
---|
| 714 |
|
---|
| 715 | void EvalSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
| 716 |
|
---|
| 717 | void AddSubdivisionStats(const int viewCells,
|
---|
[1667] | 718 | const float renderCostDecr,
|
---|
| 719 | const float totalRenderCost);
|
---|
[1237] | 720 |
|
---|
| 721 | void EvalViewCellsForHeuristics(const VssRay &ray,
|
---|
[1667] | 722 | float &volLeft,
|
---|
| 723 | float &volRight);
|
---|
[1237] | 724 |
|
---|
| 725 | float EvalViewCellsVolume(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
| 726 |
|
---|
[1667] | 727 | int RemoveParentViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
[1237] | 728 |
|
---|
| 729 | int UpdateViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
[1667] | 730 |
|
---|
| 731 | int CheckViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
[1237] | 732 |
|
---|
[1667] | 733 | bool AddViewCellToObjectPvs(Intersectable *obj,
|
---|
| 734 | ViewCell *vc,
|
---|
| 735 | float &contribution,
|
---|
| 736 | bool onlyUnmailed) const;
|
---|
[1237] | 737 |
|
---|
[1667] | 738 | int ClassifyRays(const RayInfoContainer &rays,
|
---|
| 739 | KdLeaf *leaf,
|
---|
| 740 | const AxisAlignedPlane &plane,
|
---|
| 741 | RayInfoContainer &frontRays,
|
---|
| 742 | RayInfoContainer &backRays) const;
|
---|
[1237] | 743 |
|
---|
[1667] | 744 | void CollectTouchedViewCells(const RayInfoContainer &rays,
|
---|
| 745 | ViewCellContainer &touchedViewCells) const;
|
---|
[1237] | 746 |
|
---|
[1667] | 747 | void AddObjectContribution(KdLeaf *leaf,
|
---|
| 748 | Intersectable * obj,
|
---|
| 749 | ViewCellContainer &touchedViewCells,
|
---|
| 750 | float &renderCost);
|
---|
[1237] | 751 |
|
---|
[1667] | 752 | void SubtractObjectContribution(KdLeaf *leaf,
|
---|
| 753 | Intersectable * obj,
|
---|
| 754 | ViewCellContainer &touchedViewCells,
|
---|
| 755 | float &renderCost);
|
---|
[1237] | 756 |
|
---|
[1667] | 757 | SubdivisionCandidate *PrepareConstruction(const VssRayContainer &sampleRays,
|
---|
| 758 | const ObjectContainer &objects,
|
---|
| 759 | RayInfoContainer &rays);
|
---|
[1259] | 760 |
|
---|
[1667] | 761 |
|
---|
[1237] | 762 | protected:
|
---|
| 763 |
|
---|
| 764 | /// pointer to the hierarchy of view cells
|
---|
| 765 | ViewCellsTree *mViewCellsTree;
|
---|
| 766 |
|
---|
[1379] | 767 | /// pointer to the view space partition
|
---|
| 768 | /// note: should be handled over the hierarchy manager
|
---|
[1237] | 769 | VspTree *mVspTree;
|
---|
| 770 |
|
---|
[1379] | 771 | /// pointer to the hierarchy manager
|
---|
| 772 | HierarchyManager *mHierarchyManager;
|
---|
| 773 |
|
---|
[1237] | 774 | /// The view cells manager
|
---|
| 775 | ViewCellsManager *mViewCellsManager;
|
---|
| 776 |
|
---|
| 777 | /// candidates for placing split planes during cost heuristics
|
---|
| 778 | vector<SortableEntry> *mSubdivisionCandidates;
|
---|
| 779 |
|
---|
| 780 | /// Pointer to the root of the tree
|
---|
| 781 | KdNode *mRoot;
|
---|
| 782 |
|
---|
| 783 | /// Statistics for the object space partition
|
---|
| 784 | OspTreeStatistics mOspStats;
|
---|
| 785 |
|
---|
| 786 | /// box around the whole view domain
|
---|
| 787 | AxisAlignedBox3 mBoundingBox;
|
---|
| 788 |
|
---|
| 789 |
|
---|
[1379] | 790 | //////////////////////////////
|
---|
[1237] | 791 | //-- local termination
|
---|
| 792 |
|
---|
| 793 | /// maximal possible depth
|
---|
| 794 | int mTermMaxDepth;
|
---|
| 795 | /// mininum probability
|
---|
| 796 | float mTermMinProbability;
|
---|
| 797 | /// minimal number of objects
|
---|
| 798 | int mTermMinObjects;
|
---|
| 799 | /// maximal contribution per ray
|
---|
| 800 | float mTermMaxRayContribution;
|
---|
| 801 | /// maximal acceptable cost ratio
|
---|
| 802 | float mTermMaxCostRatio;
|
---|
| 803 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
| 804 | int mTermMissTolerance;
|
---|
| 805 |
|
---|
| 806 |
|
---|
[1379] | 807 | ////////////////////////
|
---|
[1237] | 808 | //-- global criteria
|
---|
| 809 |
|
---|
| 810 | float mTermMinGlobalCostRatio;
|
---|
| 811 | int mTermGlobalCostMissTolerance;
|
---|
| 812 | int mGlobalCostMisses;
|
---|
| 813 |
|
---|
| 814 | /// maximal number of view cells
|
---|
| 815 | int mTermMaxLeaves;
|
---|
| 816 | /// maximal tree memory
|
---|
| 817 | float mMaxMemory;
|
---|
| 818 | /// the tree is out of memory
|
---|
| 819 | bool mOutOfMemory;
|
---|
| 820 |
|
---|
| 821 |
|
---|
[1379] | 822 | ///////////////////////////////////////////
|
---|
[1237] | 823 | //-- split heuristics based parameters
|
---|
| 824 |
|
---|
| 825 | bool mUseCostHeuristics;
|
---|
| 826 | /// balancing factor for PVS criterium
|
---|
| 827 | float mCtDivCi;
|
---|
| 828 | /// if only driving axis should be used for split
|
---|
| 829 | bool mOnlyDrivingAxis;
|
---|
[1379] | 830 | /// represents min and max band for sweep
|
---|
| 831 | float mSplitBorder;
|
---|
| 832 |
|
---|
| 833 | ////////////////////////////////////////////////
|
---|
| 834 |
|
---|
[1237] | 835 | /// current time stamp (used for keeping split history)
|
---|
| 836 | int mTimeStamp;
|
---|
| 837 | // if rays should be stored in leaves
|
---|
| 838 | bool mStoreRays;
|
---|
| 839 | /// epsilon for geometric comparisons
|
---|
| 840 | float mEpsilon;
|
---|
| 841 | /// subdivision stats output file
|
---|
| 842 | ofstream mSubdivisionStats;
|
---|
| 843 | /// keeps track of cost during subdivision
|
---|
| 844 | float mTotalCost;
|
---|
| 845 | /// keeps track of overall pvs size during subdivision
|
---|
| 846 | int mTotalPvsSize;
|
---|
| 847 | /// number of currenly generated view cells
|
---|
| 848 | int mCreatedLeaves;
|
---|
[1379] | 849 |
|
---|
[1237] | 850 | /// weight between render cost decrease and node render cost
|
---|
| 851 | float mRenderCostDecreaseWeight;
|
---|
| 852 |
|
---|
[1379] | 853 | /// stores the kd node intersectables used for pvs
|
---|
[1594] | 854 | KdIntersectableMap mKdIntersectables;
|
---|
[1379] | 855 |
|
---|
[1237] | 856 | private:
|
---|
| 857 |
|
---|
| 858 | bool mCopyFromKdTree;
|
---|
| 859 | };
|
---|
| 860 |
|
---|
| 861 |
|
---|
| 862 | }
|
---|
| 863 |
|
---|
| 864 | #endif
|
---|