[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;
|
---|
[1667] | 257 | sOspTree->EvalSubdivisionCandidate(*this);
|
---|
[1237] | 258 | }
|
---|
| 259 |
|
---|
| 260 | bool GlobalTerminationCriteriaMet() const
|
---|
| 261 | {
|
---|
| 262 | return sOspTree->GlobalTerminationCriteriaMet(mParentData);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[2224] | 265 | bool Apply(SplitQueue &splitQueue, bool terminationCriteriaMet, SubdivisionCandidateContainer &dirtyList)
|
---|
[1633] | 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 |
|
---|
[2542] | 273 | void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList, const bool onlyUnmailed)
|
---|
[1633] | 274 | {
|
---|
| 275 | sOspTree->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[1667] | 278 | float GetPriority() const
|
---|
| 279 | {
|
---|
[1758] | 280 | return mPriority;
|
---|
[1667] | 281 | }
|
---|
| 282 |
|
---|
[1237] | 283 | OspSubdivisionCandidate(const AxisAlignedPlane &plane, const OspTraversalData &tData):
|
---|
| 284 | mSplitPlane(plane), mParentData(tData)
|
---|
| 285 | {}
|
---|
| 286 | };
|
---|
| 287 |
|
---|
| 288 | /** Struct for traversing line segment.
|
---|
| 289 | */
|
---|
| 290 | struct LineTraversalData
|
---|
| 291 | {
|
---|
| 292 | KdNode *mNode;
|
---|
| 293 | Vector3 mExitPoint;
|
---|
| 294 |
|
---|
| 295 | float mMaxT;
|
---|
| 296 |
|
---|
| 297 | LineTraversalData () {}
|
---|
| 298 | LineTraversalData (KdNode *n, const Vector3 &p, const float maxt):
|
---|
| 299 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
| 300 | };
|
---|
| 301 |
|
---|
| 302 | /** Default constructor creating an empty tree.
|
---|
| 303 | */
|
---|
| 304 | OspTree();
|
---|
| 305 |
|
---|
| 306 | /** Copies tree from a kd tree.
|
---|
| 307 | */
|
---|
| 308 | OspTree(const KdTree &kdTree);
|
---|
| 309 |
|
---|
| 310 | /** Default destructor.
|
---|
| 311 | */
|
---|
| 312 | ~OspTree();
|
---|
| 313 |
|
---|
| 314 | /** Returns tree statistics.
|
---|
| 315 | */
|
---|
| 316 | const OspTreeStatistics &GetStatistics() const;
|
---|
| 317 |
|
---|
| 318 | /** Returns bounding box of the specified node.
|
---|
| 319 | */
|
---|
| 320 | AxisAlignedBox3 GetBoundingBox(KdNode *node) const;
|
---|
| 321 |
|
---|
| 322 | /** Returns list of leaves with pvs smaller than
|
---|
| 323 | a certain threshold.
|
---|
| 324 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
| 325 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
| 326 | */
|
---|
| 327 |
|
---|
| 328 | void CollectLeaves(vector<KdLeaf *> &leaves) const;
|
---|
| 329 |
|
---|
| 330 | /** Returns bounding box of the whole tree (= bbox of root node)
|
---|
| 331 | */
|
---|
| 332 | AxisAlignedBox3 GetBoundingBox()const;
|
---|
| 333 |
|
---|
| 334 | /** Returns root of the view space partitioning tree.
|
---|
| 335 | */
|
---|
| 336 | KdNode *GetRoot() const;
|
---|
| 337 |
|
---|
| 338 | /** Collects the leaf view cells of the tree
|
---|
| 339 | @param viewCells returns the view cells
|
---|
| 340 | */
|
---|
| 341 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
| 342 |
|
---|
| 343 | /** A ray is cast possible intersecting the tree.
|
---|
| 344 | @param the ray that is cast.
|
---|
| 345 | @returns the number of intersections with objects stored in the tree.
|
---|
| 346 | */
|
---|
| 347 | int CastRay(Ray &ray);
|
---|
| 348 |
|
---|
| 349 | /** finds neighbouring leaves of this tree node.
|
---|
| 350 | */
|
---|
| 351 | int FindNeighbors(KdLeaf *n,
|
---|
| 352 | vector<VspLeaf *> &neighbors,
|
---|
| 353 | const bool onlyUnmailed) const;
|
---|
| 354 |
|
---|
| 355 | /** Returns random leaf of BSP tree.
|
---|
| 356 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
| 357 | */
|
---|
| 358 | KdLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
| 359 |
|
---|
| 360 | /** Returns random leaf of BSP tree.
|
---|
| 361 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
| 362 | */
|
---|
| 363 | KdLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
| 364 |
|
---|
| 365 | /** Returns epsilon of this tree.
|
---|
| 366 | */
|
---|
| 367 | float GetEpsilon() const;
|
---|
| 368 |
|
---|
| 369 | /** Casts line segment into the tree.
|
---|
| 370 | @param origin the origin of the line segment
|
---|
| 371 | @param termination the end point of the line segment
|
---|
| 372 | @returns view cells intersecting the line segment.
|
---|
| 373 | */
|
---|
| 374 | int CastLineSegment(const Vector3 &origin,
|
---|
| 375 | const Vector3 &termination,
|
---|
| 376 | ViewCellContainer &viewcells);
|
---|
| 377 | /** Sets pointer to view cells manager.
|
---|
| 378 | */
|
---|
| 379 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
| 380 | /** Writes tree to output stream
|
---|
| 381 | */
|
---|
| 382 | bool Export(OUT_STREAM &stream);
|
---|
| 383 | /** Returns or creates a new intersectable for use in a kd based pvs.
|
---|
| 384 | The OspTree is responsible for destruction of the intersectable.
|
---|
| 385 | */
|
---|
| 386 | KdIntersectable *GetOrCreateKdIntersectable(KdNode *node);
|
---|
| 387 | /** Collects rays stored in the leaves.
|
---|
| 388 | */
|
---|
| 389 | void CollectRays(VssRayContainer &rays);
|
---|
| 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,
|
---|
[1667] | 700 | const float renderCostDecr,
|
---|
| 701 | const float totalRenderCost);
|
---|
[1237] | 702 |
|
---|
| 703 | void EvalViewCellsForHeuristics(const VssRay &ray,
|
---|
[1667] | 704 | float &volLeft,
|
---|
| 705 | float &volRight);
|
---|
[1237] | 706 |
|
---|
| 707 | float EvalViewCellsVolume(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
| 708 |
|
---|
[1667] | 709 | int RemoveParentViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
[1237] | 710 |
|
---|
| 711 | int UpdateViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
[1667] | 712 |
|
---|
| 713 | int CheckViewCellsPvs(KdLeaf *leaf, const RayInfoContainer &rays) const;
|
---|
[1237] | 714 |
|
---|
[1667] | 715 | bool AddViewCellToObjectPvs(Intersectable *obj,
|
---|
| 716 | ViewCell *vc,
|
---|
| 717 | float &contribution,
|
---|
| 718 | bool onlyUnmailed) const;
|
---|
[1237] | 719 |
|
---|
[1667] | 720 | int ClassifyRays(const RayInfoContainer &rays,
|
---|
| 721 | KdLeaf *leaf,
|
---|
| 722 | const AxisAlignedPlane &plane,
|
---|
| 723 | RayInfoContainer &frontRays,
|
---|
| 724 | RayInfoContainer &backRays) const;
|
---|
[1237] | 725 |
|
---|
[1667] | 726 | void CollectTouchedViewCells(const RayInfoContainer &rays,
|
---|
| 727 | ViewCellContainer &touchedViewCells) const;
|
---|
[1237] | 728 |
|
---|
[1667] | 729 | void AddObjectContribution(KdLeaf *leaf,
|
---|
| 730 | Intersectable * obj,
|
---|
| 731 | ViewCellContainer &touchedViewCells,
|
---|
| 732 | float &renderCost);
|
---|
[1237] | 733 |
|
---|
[1667] | 734 | void SubtractObjectContribution(KdLeaf *leaf,
|
---|
[1779] | 735 | Intersectable *obj,
|
---|
[1667] | 736 | ViewCellContainer &touchedViewCells,
|
---|
| 737 | float &renderCost);
|
---|
[1237] | 738 |
|
---|
[1779] | 739 | void PrepareConstruction(SplitQueue &tQueue,
|
---|
| 740 | const VssRayContainer &sampleRays,
|
---|
| 741 | const ObjectContainer &objects,
|
---|
| 742 | RayInfoContainer &rays);
|
---|
[1259] | 743 |
|
---|
[1667] | 744 |
|
---|
[1237] | 745 | protected:
|
---|
| 746 |
|
---|
| 747 | /// pointer to the hierarchy of view cells
|
---|
| 748 | ViewCellsTree *mViewCellsTree;
|
---|
| 749 |
|
---|
[1379] | 750 | /// pointer to the view space partition
|
---|
| 751 | /// note: should be handled over the hierarchy manager
|
---|
[1237] | 752 | VspTree *mVspTree;
|
---|
| 753 |
|
---|
[1379] | 754 | /// pointer to the hierarchy manager
|
---|
| 755 | HierarchyManager *mHierarchyManager;
|
---|
| 756 |
|
---|
[1237] | 757 | /// The view cells manager
|
---|
| 758 | ViewCellsManager *mViewCellsManager;
|
---|
| 759 |
|
---|
| 760 | /// candidates for placing split planes during cost heuristics
|
---|
| 761 | vector<SortableEntry> *mSubdivisionCandidates;
|
---|
| 762 |
|
---|
| 763 | /// Pointer to the root of the tree
|
---|
| 764 | KdNode *mRoot;
|
---|
| 765 |
|
---|
| 766 | /// Statistics for the object space partition
|
---|
| 767 | OspTreeStatistics mOspStats;
|
---|
| 768 |
|
---|
| 769 | /// box around the whole view domain
|
---|
| 770 | AxisAlignedBox3 mBoundingBox;
|
---|
| 771 |
|
---|
| 772 |
|
---|
[1379] | 773 | //////////////////////////////
|
---|
[1237] | 774 | //-- local termination
|
---|
| 775 |
|
---|
| 776 | /// maximal possible depth
|
---|
| 777 | int mTermMaxDepth;
|
---|
| 778 | /// mininum probability
|
---|
| 779 | float mTermMinProbability;
|
---|
| 780 | /// minimal number of objects
|
---|
| 781 | int mTermMinObjects;
|
---|
| 782 | /// maximal contribution per ray
|
---|
| 783 | float mTermMaxRayContribution;
|
---|
| 784 | /// maximal acceptable cost ratio
|
---|
| 785 | float mTermMaxCostRatio;
|
---|
| 786 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
| 787 | int mTermMissTolerance;
|
---|
| 788 |
|
---|
| 789 |
|
---|
[1379] | 790 | ////////////////////////
|
---|
[1237] | 791 | //-- global criteria
|
---|
| 792 |
|
---|
| 793 | float mTermMinGlobalCostRatio;
|
---|
| 794 | int mTermGlobalCostMissTolerance;
|
---|
| 795 | int mGlobalCostMisses;
|
---|
| 796 |
|
---|
| 797 | /// maximal number of view cells
|
---|
| 798 | int mTermMaxLeaves;
|
---|
| 799 | /// maximal tree memory
|
---|
| 800 | float mMaxMemory;
|
---|
| 801 | /// the tree is out of memory
|
---|
| 802 | bool mOutOfMemory;
|
---|
| 803 |
|
---|
| 804 |
|
---|
[1379] | 805 | ///////////////////////////////////////////
|
---|
[1237] | 806 | //-- split heuristics based parameters
|
---|
| 807 |
|
---|
| 808 | bool mUseCostHeuristics;
|
---|
| 809 | /// balancing factor for PVS criterium
|
---|
| 810 | float mCtDivCi;
|
---|
| 811 | /// if only driving axis should be used for split
|
---|
| 812 | bool mOnlyDrivingAxis;
|
---|
[1379] | 813 | /// represents min and max band for sweep
|
---|
| 814 | float mSplitBorder;
|
---|
| 815 |
|
---|
| 816 | ////////////////////////////////////////////////
|
---|
| 817 |
|
---|
[1237] | 818 | /// current time stamp (used for keeping split history)
|
---|
| 819 | int mTimeStamp;
|
---|
| 820 | // if rays should be stored in leaves
|
---|
| 821 | bool mStoreRays;
|
---|
| 822 | /// epsilon for geometric comparisons
|
---|
| 823 | float mEpsilon;
|
---|
| 824 | /// subdivision stats output file
|
---|
[2176] | 825 | std::ofstream mSubdivisionStats;
|
---|
[1237] | 826 | /// keeps track of cost during subdivision
|
---|
| 827 | float mTotalCost;
|
---|
| 828 | /// keeps track of overall pvs size during subdivision
|
---|
| 829 | int mTotalPvsSize;
|
---|
| 830 | /// number of currenly generated view cells
|
---|
| 831 | int mCreatedLeaves;
|
---|
[1379] | 832 |
|
---|
[1237] | 833 | /// weight between render cost decrease and node render cost
|
---|
| 834 | float mRenderCostDecreaseWeight;
|
---|
| 835 |
|
---|
[1379] | 836 | /// stores the kd node intersectables used for pvs
|
---|
[1594] | 837 | KdIntersectableMap mKdIntersectables;
|
---|
[1379] | 838 |
|
---|
[1237] | 839 | private:
|
---|
| 840 |
|
---|
| 841 | bool mCopyFromKdTree;
|
---|
| 842 | };
|
---|
| 843 |
|
---|
| 844 |
|
---|
| 845 | }
|
---|
| 846 |
|
---|
| 847 | #endif
|
---|