[1237] | 1 | #ifndef _VspTree_H__
|
---|
| 2 | #define _VspTree_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"
|
---|
[1237] | 13 |
|
---|
| 14 |
|
---|
| 15 | namespace GtpVisibilityPreprocessor {
|
---|
| 16 |
|
---|
| 17 | class ViewCellLeaf;
|
---|
| 18 | class Plane3;
|
---|
| 19 | class AxisAlignedBox3;
|
---|
| 20 | class Ray;
|
---|
| 21 | class ViewCellsStatistics;
|
---|
| 22 | class ViewCellsManager;
|
---|
| 23 | class MergeCandidate;
|
---|
| 24 | class Beam;
|
---|
| 25 | class ViewCellsTree;
|
---|
| 26 | class Environment;
|
---|
| 27 | class VspInterior;
|
---|
| 28 | class VspLeaf;
|
---|
| 29 | class VspNode;
|
---|
| 30 | class KdNode;
|
---|
| 31 | class KdInterior;
|
---|
| 32 | class KdLeaf;
|
---|
[1259] | 33 | class HierarchyManager;
|
---|
[1237] | 34 | class KdIntersectable;
|
---|
| 35 | class KdTree;
|
---|
| 36 | class VspTree;
|
---|
| 37 | class KdTreeStatistics;
|
---|
| 38 | class SubdivisionCandidate;
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | /** View space partition statistics.
|
---|
| 42 | */
|
---|
| 43 | class VspTreeStatistics: public StatisticsBase
|
---|
| 44 | {
|
---|
| 45 | public:
|
---|
| 46 | // total number of nodes
|
---|
| 47 | int nodes;
|
---|
| 48 | // number of splits
|
---|
| 49 | int splits[3];
|
---|
| 50 |
|
---|
| 51 | // totals number of rays
|
---|
| 52 | int rays;
|
---|
| 53 | // maximal reached depth
|
---|
| 54 | int maxDepth;
|
---|
| 55 | // minimal depth
|
---|
| 56 | int minDepth;
|
---|
| 57 |
|
---|
| 58 | // max depth nodes
|
---|
| 59 | int maxDepthNodes;
|
---|
| 60 | // minimum depth nodes
|
---|
| 61 | int minDepthNodes;
|
---|
| 62 | // max depth nodes
|
---|
| 63 | int minPvsNodes;
|
---|
| 64 | // nodes with minimum PVS
|
---|
| 65 | int minRaysNodes;
|
---|
| 66 | // max ray contribution nodes
|
---|
| 67 | int maxRayContribNodes;
|
---|
| 68 | // minimum area nodes
|
---|
| 69 | int minProbabilityNodes;
|
---|
| 70 | /// nodes termination because of max cost ratio;
|
---|
| 71 | int maxCostNodes;
|
---|
| 72 | // max number of rays per node
|
---|
| 73 | int maxObjectRefs;
|
---|
| 74 | /// samples contributing to pvs
|
---|
| 75 | int contributingSamples;
|
---|
| 76 | /// sample contributions to pvs
|
---|
| 77 | int sampleContributions;
|
---|
| 78 | /// largest pvs
|
---|
| 79 | int maxPvs;
|
---|
| 80 | /// number of invalid leaves
|
---|
| 81 | int invalidLeaves;
|
---|
| 82 | /// accumulated number of rays refs
|
---|
| 83 | int accumRays;
|
---|
| 84 | int pvs;
|
---|
| 85 | // accumulated depth (used to compute average)
|
---|
| 86 | int accumDepth;
|
---|
| 87 |
|
---|
| 88 | // Constructor
|
---|
| 89 | VspTreeStatistics()
|
---|
| 90 | {
|
---|
| 91 | Reset();
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | int Nodes() const {return nodes;}
|
---|
| 95 | int Interior() const { return nodes / 2; }
|
---|
| 96 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
| 97 |
|
---|
| 98 | // TODO: computation wrong
|
---|
| 99 | double AvgDepth() const { return accumDepth / (double)Leaves();};
|
---|
| 100 | double AvgRays() const { return accumRays / (double)Leaves();};
|
---|
| 101 |
|
---|
| 102 | void Reset()
|
---|
| 103 | {
|
---|
| 104 | nodes = 0;
|
---|
| 105 | for (int i = 0; i < 3; ++ i)
|
---|
| 106 | splits[i] = 0;
|
---|
| 107 |
|
---|
| 108 | maxDepth = 0;
|
---|
| 109 | minDepth = 99999;
|
---|
| 110 | accumDepth = 0;
|
---|
| 111 | pvs = 0;
|
---|
| 112 | maxDepthNodes = 0;
|
---|
| 113 | minPvsNodes = 0;
|
---|
| 114 | minRaysNodes = 0;
|
---|
| 115 | maxRayContribNodes = 0;
|
---|
| 116 | minProbabilityNodes = 0;
|
---|
| 117 | maxCostNodes = 0;
|
---|
| 118 |
|
---|
| 119 | contributingSamples = 0;
|
---|
| 120 | sampleContributions = 0;
|
---|
| 121 |
|
---|
| 122 | maxPvs = 0;
|
---|
| 123 | invalidLeaves = 0;
|
---|
| 124 | accumRays = 0;
|
---|
| 125 | maxObjectRefs = 0;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void Print(ostream &app) const;
|
---|
| 129 |
|
---|
| 130 | friend ostream &operator<<(ostream &s, const VspTreeStatistics &stat)
|
---|
| 131 | {
|
---|
| 132 | stat.Print(s);
|
---|
| 133 | return s;
|
---|
| 134 | }
|
---|
| 135 | };
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | VspNode abstract class serving for interior and leaf node implementation
|
---|
| 140 | */
|
---|
| 141 | class VspNode
|
---|
| 142 | {
|
---|
| 143 | public:
|
---|
| 144 |
|
---|
| 145 | // types of vsp nodes
|
---|
| 146 | enum {Interior, Leaf};
|
---|
| 147 |
|
---|
| 148 | VspNode();
|
---|
| 149 | virtual ~VspNode(){};
|
---|
| 150 | VspNode(VspInterior *parent);
|
---|
| 151 |
|
---|
| 152 | /** Determines whether this node is a leaf or not
|
---|
| 153 | @return true if leaf
|
---|
| 154 | */
|
---|
| 155 | virtual bool IsLeaf() const = 0;
|
---|
| 156 |
|
---|
| 157 | virtual int Type() const = 0;
|
---|
| 158 |
|
---|
| 159 | /** Determines whether this node is a root
|
---|
| 160 | @return true if root
|
---|
| 161 | */
|
---|
| 162 | virtual bool IsRoot() const;
|
---|
| 163 |
|
---|
| 164 | /** Returns parent node.
|
---|
| 165 | */
|
---|
| 166 | VspInterior *GetParent();
|
---|
| 167 |
|
---|
| 168 | /** Sets parent node.
|
---|
| 169 | */
|
---|
| 170 | void SetParent(VspInterior *parent);
|
---|
| 171 |
|
---|
| 172 | /** Returns true if this node is a sibling of node n.
|
---|
| 173 | */
|
---|
| 174 | bool IsSibling(VspNode *n) const;
|
---|
| 175 |
|
---|
| 176 | /** returns depth of the node.
|
---|
| 177 | */
|
---|
| 178 | int GetDepth() const;
|
---|
| 179 |
|
---|
| 180 | /** returns true if the whole subtree is valid
|
---|
| 181 | */
|
---|
| 182 | bool TreeValid() const;
|
---|
| 183 |
|
---|
| 184 | void SetTreeValid(const bool v);
|
---|
| 185 |
|
---|
| 186 | //-- mailing options
|
---|
| 187 |
|
---|
| 188 | void Mail() { mMailbox = sMailId; }
|
---|
| 189 | static void NewMail() { ++ sMailId; }
|
---|
| 190 | bool Mailed() const { return mMailbox == sMailId; }
|
---|
| 191 |
|
---|
| 192 | static int sMailId;
|
---|
| 193 | int mMailbox;
|
---|
| 194 |
|
---|
| 195 | int mTimeStamp;
|
---|
| 196 |
|
---|
| 197 | protected:
|
---|
| 198 |
|
---|
| 199 | /// if this sub tree is a completely valid view space region
|
---|
| 200 | bool mTreeValid;
|
---|
| 201 | /// parent of this node
|
---|
| 202 | VspInterior *mParent;
|
---|
| 203 | };
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 | /** BSP interior node implementation
|
---|
| 207 | */
|
---|
| 208 | class VspInterior: public VspNode
|
---|
| 209 | {
|
---|
| 210 | public:
|
---|
| 211 | /** Standard contructor taking split plane as argument.
|
---|
| 212 | */
|
---|
| 213 | VspInterior(const AxisAlignedPlane &plane);
|
---|
| 214 |
|
---|
| 215 | ~VspInterior();
|
---|
| 216 | /** @return false since it is an interior node
|
---|
| 217 | */
|
---|
| 218 | bool IsLeaf() const;
|
---|
| 219 |
|
---|
| 220 | int Type() const;
|
---|
| 221 |
|
---|
| 222 | VspNode *GetBack();
|
---|
| 223 | VspNode *GetFront();
|
---|
| 224 |
|
---|
| 225 | /** Returns split plane.
|
---|
| 226 | */
|
---|
| 227 | AxisAlignedPlane GetPlane() const;
|
---|
| 228 |
|
---|
| 229 | /** Returns position of split plane.
|
---|
| 230 | */
|
---|
| 231 | float GetPosition() const;
|
---|
| 232 |
|
---|
| 233 | /** Returns split axis.
|
---|
| 234 | */
|
---|
| 235 | int GetAxis() const;
|
---|
| 236 |
|
---|
| 237 | /** Replace front or back child with new child.
|
---|
| 238 | */
|
---|
| 239 | void ReplaceChildLink(VspNode *oldChild, VspNode *newChild);
|
---|
| 240 |
|
---|
| 241 | /** Replace front and back child.
|
---|
| 242 | */
|
---|
| 243 | void SetupChildLinks(VspNode *front, VspNode *back);
|
---|
| 244 |
|
---|
| 245 | friend ostream &operator<<(ostream &s, const VspInterior &A)
|
---|
| 246 | {
|
---|
| 247 | return s << A.mPlane.mAxis << " " << A.mPlane.mPosition;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
| 251 | void SetBoundingBox(const AxisAlignedBox3 &box);
|
---|
| 252 |
|
---|
| 253 | /** Computes intersection of this plane with the ray segment.
|
---|
| 254 | */
|
---|
| 255 | int ComputeRayIntersection(const RayInfo &rayData, float &t) const
|
---|
| 256 | {
|
---|
| 257 | return rayData.ComputeRayIntersection(mPlane.mAxis, mPlane.mPosition, t);
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 |
|
---|
| 261 | protected:
|
---|
| 262 |
|
---|
| 263 | AxisAlignedBox3 mBoundingBox;
|
---|
| 264 |
|
---|
| 265 | /// Splitting plane corresponding to this node
|
---|
| 266 | AxisAlignedPlane mPlane;
|
---|
| 267 |
|
---|
| 268 | /// back node
|
---|
| 269 | VspNode *mBack;
|
---|
| 270 | /// front node
|
---|
| 271 | VspNode *mFront;
|
---|
| 272 | };
|
---|
| 273 |
|
---|
| 274 |
|
---|
| 275 | /** BSP leaf node implementation.
|
---|
| 276 | */
|
---|
| 277 | class VspLeaf: public VspNode
|
---|
| 278 | {
|
---|
| 279 | friend VspTree;
|
---|
| 280 |
|
---|
| 281 | public:
|
---|
| 282 | VspLeaf();
|
---|
| 283 | VspLeaf(ViewCellLeaf *viewCell);
|
---|
| 284 | VspLeaf(VspInterior *parent);
|
---|
| 285 | VspLeaf(VspInterior *parent, ViewCellLeaf *viewCell);
|
---|
| 286 |
|
---|
| 287 | ~VspLeaf();
|
---|
| 288 |
|
---|
| 289 | /** @return true since it is an interior node
|
---|
| 290 | */
|
---|
| 291 | bool IsLeaf() const;
|
---|
| 292 |
|
---|
| 293 | int Type() const;
|
---|
| 294 |
|
---|
| 295 | /** Returns pointer of view cell.
|
---|
| 296 | */
|
---|
| 297 | ViewCellLeaf *GetViewCell() const;
|
---|
| 298 |
|
---|
| 299 | /** Sets pointer to view cell.
|
---|
| 300 | */
|
---|
| 301 | void SetViewCell(ViewCellLeaf *viewCell);
|
---|
| 302 |
|
---|
| 303 | SubdivisionCandidate *GetSubdivisionCandidate() const
|
---|
| 304 | {
|
---|
| 305 | return mSubdivisionCandidate;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | public:
|
---|
| 309 |
|
---|
| 310 | /// Rays piercing this leaf.
|
---|
| 311 | VssRayContainer mVssRays;
|
---|
| 312 |
|
---|
| 313 | /// leaf pvs
|
---|
| 314 | ObjectPvs *mPvs;
|
---|
| 315 |
|
---|
| 316 | /// Probability that the view point lies in this leaf
|
---|
| 317 | float mProbability;
|
---|
| 318 |
|
---|
| 319 | protected:
|
---|
| 320 |
|
---|
| 321 | /// pointer to a split plane candidate splitting this leaf
|
---|
| 322 | SubdivisionCandidate *mSubdivisionCandidate;
|
---|
| 323 |
|
---|
| 324 | /// if NULL this does not correspond to feasible viewcell
|
---|
| 325 | ViewCellLeaf *mViewCell;
|
---|
| 326 | };
|
---|
| 327 |
|
---|
| 328 |
|
---|
| 329 | /** View Space Partitioning tree.
|
---|
| 330 | */
|
---|
| 331 | class VspTree
|
---|
| 332 | {
|
---|
| 333 | friend class ViewCellsParseHandlers;
|
---|
| 334 | friend class HierarchyManager;
|
---|
| 335 |
|
---|
| 336 | public:
|
---|
| 337 |
|
---|
| 338 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
| 339 | */
|
---|
| 340 | class VspTraversalData
|
---|
| 341 | {
|
---|
| 342 | public:
|
---|
| 343 | /// the current node
|
---|
| 344 | VspLeaf *mNode;
|
---|
| 345 | /// current depth
|
---|
| 346 | int mDepth;
|
---|
| 347 | /// rays piercing this node
|
---|
| 348 | RayInfoContainer *mRays;
|
---|
| 349 | /// the probability that this node contains view point
|
---|
| 350 | float mProbability;
|
---|
| 351 | /// the bounding box of the node
|
---|
| 352 | AxisAlignedBox3 mBoundingBox;
|
---|
| 353 | /// pvs size
|
---|
| 354 | int mPvs;
|
---|
| 355 | /// how often this branch has missed the max-cost ratio
|
---|
| 356 | int mMaxCostMisses;
|
---|
| 357 | // current priority
|
---|
| 358 | float mPriority;
|
---|
| 359 |
|
---|
| 360 |
|
---|
| 361 | /** Returns average ray contribution.
|
---|
| 362 | */
|
---|
| 363 | float GetAvgRayContribution() const
|
---|
| 364 | {
|
---|
| 365 | return (float)mPvs / ((float)mRays->size() + Limits::Small);
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 |
|
---|
| 369 | VspTraversalData():
|
---|
| 370 | mNode(NULL),
|
---|
| 371 | mDepth(0),
|
---|
| 372 | mRays(NULL),
|
---|
| 373 | mPvs(0),
|
---|
| 374 | mProbability(0.0),
|
---|
| 375 | mMaxCostMisses(0),
|
---|
| 376 | mPriority(0)
|
---|
| 377 | {}
|
---|
| 378 |
|
---|
| 379 | VspTraversalData(VspLeaf *node,
|
---|
| 380 | const int depth,
|
---|
| 381 | RayInfoContainer *rays,
|
---|
| 382 | const int pvs,
|
---|
| 383 | const float p,
|
---|
| 384 | const AxisAlignedBox3 &box):
|
---|
| 385 | mNode(node),
|
---|
| 386 | mDepth(depth),
|
---|
| 387 | mRays(rays),
|
---|
| 388 | mPvs(pvs),
|
---|
| 389 | mProbability(p),
|
---|
| 390 | mBoundingBox(box),
|
---|
| 391 | mMaxCostMisses(0),
|
---|
| 392 | mPriority(0)
|
---|
| 393 | {}
|
---|
| 394 |
|
---|
| 395 | VspTraversalData(const int depth,
|
---|
| 396 | RayInfoContainer *rays,
|
---|
| 397 | const AxisAlignedBox3 &box):
|
---|
| 398 | mNode(NULL),
|
---|
| 399 | mDepth(depth),
|
---|
| 400 | mRays(rays),
|
---|
| 401 | mPvs(0),
|
---|
| 402 | mProbability(0),
|
---|
| 403 | mMaxCostMisses(0),
|
---|
| 404 | mBoundingBox(box)
|
---|
| 405 | {}
|
---|
| 406 |
|
---|
| 407 | /** Returns cost of the traversal data.
|
---|
| 408 | */
|
---|
| 409 | float GetCost() const
|
---|
| 410 | {
|
---|
| 411 | //cout << mPriority << endl;
|
---|
| 412 | return mPriority;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | /// deletes contents and sets them to NULL
|
---|
| 416 | void Clear()
|
---|
| 417 | {
|
---|
| 418 | DEL_PTR(mRays);
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | friend bool operator<(const VspTraversalData &a, const VspTraversalData &b)
|
---|
| 422 | {
|
---|
| 423 | return a.GetCost() < b.GetCost();
|
---|
| 424 | }
|
---|
| 425 | };
|
---|
| 426 |
|
---|
| 427 | /** Candidate for a view space split.
|
---|
| 428 | */
|
---|
| 429 | class VspSubdivisionCandidate: public SubdivisionCandidate
|
---|
| 430 | {
|
---|
| 431 | public:
|
---|
| 432 |
|
---|
| 433 | static VspTree* sVspTree;
|
---|
| 434 |
|
---|
| 435 | /// the current split plane
|
---|
| 436 | AxisAlignedPlane mSplitPlane;
|
---|
| 437 | /// parent node traversal data
|
---|
| 438 | VspTraversalData mParentData;
|
---|
| 439 |
|
---|
| 440 | VspSubdivisionCandidate(const VspTraversalData &tData): mParentData(tData)
|
---|
| 441 | {};
|
---|
| 442 |
|
---|
| 443 | int Type() const { return VIEW_SPACE; }
|
---|
| 444 |
|
---|
| 445 | void EvalPriority()
|
---|
| 446 | {
|
---|
| 447 | sVspTree->EvalSubdivisionCandidate(*this);
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | bool GlobalTerminationCriteriaMet() const
|
---|
| 451 | {
|
---|
| 452 | return sVspTree->GlobalTerminationCriteriaMet(mParentData);
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | VspSubdivisionCandidate(
|
---|
| 456 | const AxisAlignedPlane &plane,
|
---|
| 457 | const VspTraversalData &tData):
|
---|
| 458 | mSplitPlane(plane), mParentData(tData)
|
---|
| 459 | {}
|
---|
| 460 | };
|
---|
| 461 |
|
---|
| 462 | /** Struct for traversing line segment.
|
---|
| 463 | */
|
---|
| 464 | struct LineTraversalData
|
---|
| 465 | {
|
---|
| 466 | VspNode *mNode;
|
---|
| 467 | Vector3 mExitPoint;
|
---|
| 468 |
|
---|
| 469 | float mMaxT;
|
---|
| 470 |
|
---|
| 471 | LineTraversalData () {}
|
---|
| 472 | LineTraversalData (VspNode *n, const Vector3 &p, const float maxt):
|
---|
| 473 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
| 474 | };
|
---|
| 475 |
|
---|
| 476 | //typedef std::priority_queue<VspTraversalData> VspOspTraversalQueue;
|
---|
| 477 |
|
---|
| 478 | /** Default constructor creating an empty tree.
|
---|
| 479 | */
|
---|
| 480 | VspTree();
|
---|
| 481 |
|
---|
| 482 | /** Default destructor.
|
---|
| 483 | */
|
---|
| 484 | ~VspTree();
|
---|
| 485 |
|
---|
| 486 | /** Returns BSP Tree statistics.
|
---|
| 487 | */
|
---|
| 488 | const VspTreeStatistics &GetStatistics() const;
|
---|
| 489 |
|
---|
| 490 | /** Returns bounding box of the specified node.
|
---|
| 491 | */
|
---|
| 492 | AxisAlignedBox3 GetBoundingBox(VspNode *node) const;
|
---|
| 493 |
|
---|
| 494 | /** Returns list of BSP leaves with pvs smaller than
|
---|
| 495 | a certain threshold.
|
---|
| 496 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
| 497 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
| 498 | */
|
---|
| 499 | void CollectLeaves(vector<VspLeaf *> &leaves,
|
---|
| 500 | const bool onlyUnmailed = false,
|
---|
| 501 | const int maxPvs = -1) const;
|
---|
| 502 |
|
---|
| 503 | /** Returns box which bounds the whole tree.
|
---|
| 504 | */
|
---|
| 505 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
| 506 |
|
---|
| 507 | /** Returns root of the view space partitioning tree.
|
---|
| 508 | */
|
---|
| 509 | VspNode *GetRoot() const;
|
---|
| 510 |
|
---|
| 511 | /** Collects the leaf view cells of the tree
|
---|
| 512 | @param viewCells returns the view cells
|
---|
| 513 | */
|
---|
| 514 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
| 515 |
|
---|
| 516 | /** A ray is cast possible intersecting the tree.
|
---|
| 517 | @param the ray that is cast.
|
---|
| 518 | @returns the number of intersections with objects stored in the tree.
|
---|
| 519 | */
|
---|
| 520 | int CastRay(Ray &ray);
|
---|
| 521 |
|
---|
| 522 |
|
---|
| 523 | /** finds neighbouring leaves of this tree node.
|
---|
| 524 | */
|
---|
| 525 | int FindNeighbors(VspLeaf *n,
|
---|
| 526 | vector<VspLeaf *> &neighbors,
|
---|
| 527 | const bool onlyUnmailed) const;
|
---|
| 528 |
|
---|
| 529 | /** Returns random leaf of BSP tree.
|
---|
| 530 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
| 531 | */
|
---|
| 532 | VspLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
| 533 |
|
---|
| 534 | /** Returns random leaf of BSP tree.
|
---|
| 535 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
| 536 | */
|
---|
| 537 | VspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
| 538 |
|
---|
| 539 | /** Returns epsilon of this tree.
|
---|
| 540 | */
|
---|
| 541 | float GetEpsilon() const;
|
---|
| 542 |
|
---|
| 543 | /** Casts line segment into the tree.
|
---|
| 544 | @param origin the origin of the line segment
|
---|
| 545 | @param termination the end point of the line segment
|
---|
| 546 | @returns view cells intersecting the line segment.
|
---|
| 547 | */
|
---|
| 548 | int CastLineSegment(const Vector3 &origin,
|
---|
| 549 | const Vector3 &termination,
|
---|
| 550 | ViewCellContainer &viewcells);
|
---|
| 551 |
|
---|
| 552 |
|
---|
| 553 | /** Sets pointer to view cells manager.
|
---|
| 554 | */
|
---|
| 555 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
| 556 |
|
---|
| 557 | /** Returns view cell the current point is located in.
|
---|
| 558 | @param point the current view point
|
---|
| 559 | @param active if currently active view cells should be returned or
|
---|
| 560 | elementary view cell
|
---|
| 561 | */
|
---|
| 562 | ViewCell *GetViewCell(const Vector3 &point, const bool active = false);
|
---|
| 563 |
|
---|
| 564 |
|
---|
| 565 | /** Returns true if this view point is in a valid view space,
|
---|
| 566 | false otherwise.
|
---|
| 567 | */
|
---|
| 568 | bool ViewPointValid(const Vector3 &viewPoint) const;
|
---|
| 569 |
|
---|
| 570 | /** Returns view cell corresponding to
|
---|
| 571 | the invalid view space.
|
---|
| 572 | */
|
---|
| 573 | VspViewCell *GetOutOfBoundsCell();
|
---|
| 574 |
|
---|
| 575 | /** Writes tree to output stream
|
---|
| 576 | */
|
---|
| 577 | bool Export(OUT_STREAM &stream);
|
---|
| 578 |
|
---|
| 579 | /** Casts beam, i.e. a 5D frustum of rays, into tree.
|
---|
| 580 | Tests conservative using the bounding box of the nodes.
|
---|
| 581 | @returns number of view cells it intersected
|
---|
| 582 | */
|
---|
| 583 | int CastBeam(Beam &beam);
|
---|
| 584 |
|
---|
| 585 |
|
---|
| 586 | /** Checks if tree validity-flags are right
|
---|
| 587 | with respect to view cell valitiy.
|
---|
| 588 | If not, marks subtree as invalid.
|
---|
| 589 | */
|
---|
| 590 | void ValidateTree();
|
---|
| 591 |
|
---|
| 592 | /** Invalid view cells are added to the unbounded space
|
---|
| 593 | */
|
---|
| 594 | void CollapseViewCells();
|
---|
| 595 |
|
---|
| 596 | /** Collects rays stored in the leaves.
|
---|
| 597 | */
|
---|
| 598 | void CollectRays(VssRayContainer &rays);
|
---|
| 599 |
|
---|
| 600 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
| 601 | @returns number of view cells found
|
---|
| 602 | */
|
---|
| 603 | int ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
| 604 | ViewCellContainer &viewCells) const;
|
---|
| 605 |
|
---|
| 606 | /** Remove the references of the parent view cell from the kd nodes associated with
|
---|
| 607 | the objects.
|
---|
| 608 | */
|
---|
| 609 | void RemoveParentViewCellReferences(ViewCell *parent) const;
|
---|
| 610 |
|
---|
| 611 | /** Adds references to the view cell to the kd nodes associated with the objects.
|
---|
| 612 | */
|
---|
| 613 | void AddViewCellReferences(ViewCell *vc) const;
|
---|
| 614 |
|
---|
| 615 | /** Returns view cells of this ray, either taking precomputed cells
|
---|
| 616 | or by recomputation.
|
---|
| 617 | */
|
---|
| 618 | void GetViewCells(const VssRay &ray, ViewCellContainer &viewCells);
|
---|
| 619 |
|
---|
| 620 |
|
---|
| 621 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
| 622 |
|
---|
| 623 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
| 624 |
|
---|
| 625 |
|
---|
| 626 | protected:
|
---|
| 627 |
|
---|
| 628 | // --------------------------------------------------------------
|
---|
| 629 | // For sorting objects
|
---|
| 630 | // --------------------------------------------------------------
|
---|
| 631 | struct SortableEntry
|
---|
| 632 | {
|
---|
| 633 | enum EType
|
---|
| 634 | {
|
---|
| 635 | ERayMin,
|
---|
| 636 | ERayMax
|
---|
| 637 | };
|
---|
| 638 |
|
---|
| 639 | int type;
|
---|
| 640 | float value;
|
---|
| 641 | VssRay *ray;
|
---|
| 642 |
|
---|
| 643 | SortableEntry() {}
|
---|
| 644 | SortableEntry(const int t, const float v, VssRay *r):
|
---|
| 645 | type(t), value(v), ray(r)
|
---|
| 646 | {
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | friend bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
| 650 | {
|
---|
| 651 | return a.value < b.value;
|
---|
| 652 | }
|
---|
| 653 | };
|
---|
| 654 |
|
---|
| 655 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
| 656 | */
|
---|
| 657 | float EvalLocalSplitCost(const VspTraversalData &data,
|
---|
| 658 | const AxisAlignedBox3 &box,
|
---|
| 659 | const int axis,
|
---|
| 660 | const float &position,
|
---|
| 661 | float &pFront,
|
---|
| 662 | float &pBack) const;
|
---|
| 663 |
|
---|
| 664 | void ComputeBoundingBox(const VssRayContainer &rays,
|
---|
| 665 | AxisAlignedBox3 *forcedBoundingBox);
|
---|
| 666 |
|
---|
| 667 | /** Evaluates candidate for splitting.
|
---|
| 668 | */
|
---|
| 669 | void EvalSubdivisionCandidate(VspSubdivisionCandidate &splitData);
|
---|
| 670 |
|
---|
| 671 | /** Evaluates render cost decrease of next split.
|
---|
| 672 | */
|
---|
| 673 | float EvalRenderCostDecrease(const AxisAlignedPlane &candidatePlane,
|
---|
| 674 | const VspTraversalData &data,
|
---|
| 675 | float &normalizedOldRenderCost) const;
|
---|
| 676 |
|
---|
| 677 | /** Collects view cells in the subtree under root.
|
---|
| 678 | */
|
---|
| 679 | void CollectViewCells(VspNode *root,
|
---|
| 680 | bool onlyValid,
|
---|
| 681 | ViewCellContainer &viewCells,
|
---|
| 682 | bool onlyUnmailed = false) const;
|
---|
| 683 |
|
---|
| 684 | /** Returns view cell corresponding to
|
---|
| 685 | the invalid view space. If it does not exist, it is created.
|
---|
| 686 | */
|
---|
| 687 | VspViewCell *GetOrCreateOutOfBoundsCell();
|
---|
| 688 |
|
---|
| 689 | /** Collapses the tree with respect to the view cell partition,
|
---|
| 690 | i.e. leaves having the same view cell are collapsed.
|
---|
| 691 | @param node the root of the subtree to be collapsed
|
---|
| 692 | @param collapsed returns the number of collapsed nodes
|
---|
| 693 | @returns node of type leaf if the node could be collapsed,
|
---|
| 694 | this node otherwise
|
---|
| 695 | */
|
---|
| 696 | VspNode *CollapseTree(VspNode *node, int &collapsed);
|
---|
| 697 |
|
---|
| 698 | /** Helper function revalidating the view cell leaf list after merge.
|
---|
| 699 | */
|
---|
| 700 | void RepairViewCellsLeafLists();
|
---|
| 701 |
|
---|
| 702 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
| 703 | */
|
---|
| 704 | void EvaluateLeafStats(const VspTraversalData &data);
|
---|
| 705 |
|
---|
| 706 | /** Subdivides node using a best split priority queue.
|
---|
| 707 | @param tQueue the best split priority queue
|
---|
| 708 | @param splitCandidate the candidate for the next split
|
---|
| 709 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
| 710 | @returns new root of the subtree
|
---|
| 711 | */
|
---|
| 712 | VspNode *Subdivide(SplitQueue &tQueue,
|
---|
| 713 | SubdivisionCandidate *splitCandidate,
|
---|
| 714 | const bool globalCriteriaMet);
|
---|
| 715 |
|
---|
| 716 | /** Adds stats to subdivision log file.
|
---|
| 717 | */
|
---|
| 718 | void AddSubdivisionStats(const int viewCells,
|
---|
| 719 | const float renderCostDecr,
|
---|
| 720 | const float totalRenderCost,
|
---|
| 721 | const float avgRenderCost);
|
---|
| 722 |
|
---|
| 723 | /** Subdivides leaf.
|
---|
| 724 |
|
---|
| 725 | @param tData data object holding, e.g., a pointer to the leaf
|
---|
| 726 | @param frontData returns the data (e.g., pointer to the leaf) in front of the split plane
|
---|
| 727 | @param backData returns the data (e.g., pointer to the leaf) in the back of the split plane
|
---|
| 728 |
|
---|
| 729 | @param rays the polygons to be filtered
|
---|
| 730 | @param frontRays returns the polygons in front of the split plane
|
---|
| 731 |
|
---|
| 732 | @returns the root of the subdivision
|
---|
| 733 | */
|
---|
| 734 | VspInterior *SubdivideNode(const AxisAlignedPlane &splitPlane,
|
---|
| 735 | VspTraversalData &tData,
|
---|
| 736 | VspTraversalData &frontData,
|
---|
| 737 | VspTraversalData &backData);
|
---|
| 738 |
|
---|
| 739 | /** Selects an axis aligned for the next split.
|
---|
| 740 | @returns cost for this split
|
---|
| 741 | */
|
---|
| 742 | float SelectSplitPlane(const VspTraversalData &tData,
|
---|
| 743 | AxisAlignedPlane &plane,
|
---|
| 744 | float &pFront,
|
---|
| 745 | float &pBack);
|
---|
| 746 |
|
---|
| 747 | /** Sorts split candidates along the specified axis.
|
---|
| 748 | The split candidates are generated on possible visibility
|
---|
| 749 | events (i.e., where ray segments intersect the ray boundaries).
|
---|
| 750 | The sorted candidates are needed to compute the heuristics.
|
---|
| 751 |
|
---|
| 752 | @param polys the input for choosing split candidates
|
---|
| 753 | @param axis the current split axis
|
---|
| 754 | @param splitCandidates returns sorted list of split candidates
|
---|
| 755 | */
|
---|
| 756 | void SortSubdivisionCandidates(const RayInfoContainer &rays,
|
---|
| 757 | const int axis,
|
---|
| 758 | float minBand,
|
---|
| 759 | float maxBand);
|
---|
| 760 |
|
---|
[1259] | 761 | /** Evaluate pvs size as induced by the samples.
|
---|
[1237] | 762 | */
|
---|
| 763 | int EvalPvsSize(const RayInfoContainer &rays) const;
|
---|
| 764 |
|
---|
| 765 | /** Computes pvs increase with respect to the previous pvs for heuristics.
|
---|
| 766 | */
|
---|
| 767 | int GetPvsIncr(Intersectable *object, const KdPvsMap &activeNodes);
|
---|
| 768 |
|
---|
| 769 | /** Returns absolute pvs contribution of this object.
|
---|
| 770 | */
|
---|
| 771 | int GetPvsContribution(Intersectable *object) const;
|
---|
| 772 |
|
---|
| 773 | /** Computes best cost for axis aligned planes.
|
---|
| 774 | */
|
---|
| 775 | float EvalLocalCostHeuristics(const VspTraversalData &tData,
|
---|
| 776 | const AxisAlignedBox3 &box,
|
---|
| 777 | const int axis,
|
---|
| 778 | float &position);
|
---|
| 779 |
|
---|
[1259] | 780 | /** Evaluates the contribution to left and right pvs at a visibility event ve.
|
---|
[1237] | 781 | @param ve the visibility event
|
---|
| 782 | @param pvsLeft updates the left pvs
|
---|
| 783 | @param rightPvs updates the right pvs
|
---|
| 784 | */
|
---|
[1259] | 785 | void EvalHeuristicsContribution(
|
---|
| 786 | const SortableEntry &ve,
|
---|
| 787 | int &pvsLeft,
|
---|
| 788 | int &pvsRight) const;
|
---|
[1237] | 789 |
|
---|
[1259] | 790 | /** Evaluates contribution of the ray to the left and right pvs.
|
---|
| 791 | */
|
---|
| 792 | int EvalMinEventContribution(
|
---|
| 793 | const VssRay &ray, const bool isTermination) const;
|
---|
[1237] | 794 |
|
---|
[1259] | 795 | int EvalMaxEventContribution(
|
---|
| 796 | const VssRay &ray, const bool isTermination) const;
|
---|
| 797 |
|
---|
| 798 | int EvalMinEventContribution(KdLeaf *leaf) const;
|
---|
| 799 | int EvalMaxEventContribution(KdLeaf *leaf) const;
|
---|
| 800 |
|
---|
[1237] | 801 | /** Prepares objects for the heuristics.
|
---|
| 802 | @returns pvs size of the ray container
|
---|
| 803 | */
|
---|
| 804 | int PrepareHeuristics(const RayInfoContainer &rays);
|
---|
[1259] | 805 | int PrepareHeuristics(const VssRay &ray, const bool isTermination);
|
---|
[1237] | 806 | int PrepareHeuristics(KdLeaf *leaf);
|
---|
| 807 |
|
---|
| 808 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
| 809 |
|
---|
| 810 | @param plane the split plane
|
---|
| 811 | @param rays contains the rays to be split. The rays are
|
---|
| 812 | distributed into front and back rays.
|
---|
| 813 | @param frontRays returns rays on the front side of the plane
|
---|
| 814 | @param backRays returns rays on the back side of the plane
|
---|
| 815 |
|
---|
| 816 | @returns the number of splits
|
---|
| 817 | */
|
---|
| 818 | int SplitRays(const AxisAlignedPlane &plane,
|
---|
| 819 | RayInfoContainer &rays,
|
---|
| 820 | RayInfoContainer &frontRays,
|
---|
| 821 | RayInfoContainer &backRays) const;
|
---|
| 822 |
|
---|
| 823 | /** Classfifies the object with respect to the
|
---|
| 824 | pvs of the front and back leaf and updates pvs size
|
---|
| 825 | accordingly.
|
---|
| 826 |
|
---|
| 827 | @param obj the object to be added
|
---|
| 828 | @param cf the ray classification regarding the split plane
|
---|
| 829 | @param frontPvs returns the PVS of the front partition
|
---|
| 830 | @param backPvs returns the PVS of the back partition
|
---|
| 831 |
|
---|
| 832 | */
|
---|
| 833 | void UpdateObjPvsContri(Intersectable *obj,
|
---|
| 834 | const int cf,
|
---|
| 835 | float &frontPvs,
|
---|
| 836 | float &backPvs,
|
---|
| 837 | float &totalPvs) const;
|
---|
| 838 |
|
---|
| 839 | /** See UpdateObjPvsContri.
|
---|
| 840 | */
|
---|
| 841 | void UpdateKdLeafPvsContri(KdLeaf *leaf,
|
---|
| 842 | const int cf,
|
---|
| 843 | float &frontPvs,
|
---|
| 844 | float &backPvs,
|
---|
| 845 | float &totalPvs) const;
|
---|
| 846 |
|
---|
| 847 | /** Collects pvs from rays.
|
---|
| 848 | */
|
---|
| 849 | void CollectPvs(const RayInfoContainer &rays,
|
---|
| 850 | ObjectContainer &objects) const;
|
---|
| 851 |
|
---|
| 852 | /** Returns true if tree can be terminated.
|
---|
| 853 | */
|
---|
[1251] | 854 | bool LocalTerminationCriteriaMet(const VspTraversalData &data) const;
|
---|
[1237] | 855 |
|
---|
| 856 | /** Returns true if global tree can be terminated.
|
---|
| 857 | */
|
---|
[1251] | 858 | bool GlobalTerminationCriteriaMet(const VspTraversalData &data) const;
|
---|
[1237] | 859 |
|
---|
| 860 | /** Adds ray sample contributions to the PVS.
|
---|
| 861 | @param sampleContributions the number contributions of the samples
|
---|
| 862 | @param contributingSampels the number of contributing rays
|
---|
| 863 |
|
---|
| 864 | */
|
---|
| 865 | void AddSamplesToPvs(VspLeaf *leaf,
|
---|
| 866 | const RayInfoContainer &rays,
|
---|
| 867 | float &sampleContributions,
|
---|
| 868 | int &contributingSamples);
|
---|
| 869 |
|
---|
| 870 | /** Propagates valid flag up the tree.
|
---|
| 871 | */
|
---|
| 872 | void PropagateUpValidity(VspNode *node);
|
---|
| 873 |
|
---|
| 874 | /** Writes the node to disk
|
---|
| 875 | @note: should be implemented as visitor.
|
---|
| 876 | */
|
---|
| 877 | void ExportNode(VspNode *node, OUT_STREAM &stream);
|
---|
| 878 |
|
---|
| 879 | /** Returns estimated memory usage of tree.
|
---|
| 880 | */
|
---|
| 881 | float GetMemUsage() const;
|
---|
| 882 |
|
---|
| 883 | /** Updates view cell pvs of objects.
|
---|
| 884 | */
|
---|
| 885 | void ProcessViewCellObjects(ViewCell *parent,
|
---|
| 886 | ViewCell *front,
|
---|
| 887 | ViewCell *back) const;
|
---|
| 888 |
|
---|
| 889 | void CreateViewCell(VspTraversalData &tData, const bool updatePvs);
|
---|
| 890 |
|
---|
| 891 | /** Collect split candidates which are affected by the last split
|
---|
| 892 | and must be reevaluated.
|
---|
| 893 | */
|
---|
| 894 | void CollectDirtyCandidates(VspSubdivisionCandidate *sc, vector<SubdivisionCandidate *> &dirtyList);
|
---|
| 895 |
|
---|
| 896 | /** Rays will be clipped to the bounding box.
|
---|
| 897 | */
|
---|
| 898 | void PreprocessRays(const VssRayContainer &sampleRays, RayInfoContainer &rays);
|
---|
| 899 |
|
---|
| 900 | /** Evaluate subdivision statistics.
|
---|
| 901 | */
|
---|
| 902 | void EvalSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
| 903 |
|
---|
[1259] | 904 | SubdivisionCandidate *PrepareConstruction(
|
---|
| 905 | const VssRayContainer &sampleRays,
|
---|
| 906 | AxisAlignedBox3 *forcedViewSpace,
|
---|
| 907 | RayInfoContainer &rays);
|
---|
[1237] | 908 |
|
---|
[1259] | 909 | /** Add pvs contribution of this ray.
|
---|
| 910 | */
|
---|
| 911 | int EvalPvsContribution(const VssRay &ray, const bool isTermination) const;
|
---|
| 912 |
|
---|
[1237] | 913 | protected:
|
---|
| 914 |
|
---|
| 915 | /// pointer to the hierarchy of view cells
|
---|
| 916 | ViewCellsTree *mViewCellsTree;
|
---|
| 917 |
|
---|
[1259] | 918 | HierarchyManager *mHierarchyManager;
|
---|
| 919 | //OspTree *mOspTree;
|
---|
| 920 | //bool mUseKdPvsForHeuristics;
|
---|
| 921 |
|
---|
[1237] | 922 | ViewCellsManager *mViewCellsManager;
|
---|
| 923 |
|
---|
| 924 | vector<SortableEntry> *mLocalSubdivisionCandidates;
|
---|
| 925 |
|
---|
| 926 | /// Pointer to the root of the tree
|
---|
| 927 | VspNode *mRoot;
|
---|
| 928 |
|
---|
| 929 | VspTreeStatistics mVspStats;
|
---|
| 930 |
|
---|
| 931 | /// View cell corresponding to the space outside the valid view space
|
---|
| 932 | VspViewCell *mOutOfBoundsCell;
|
---|
| 933 |
|
---|
| 934 | /// box around the whole view domain
|
---|
| 935 | AxisAlignedBox3 mBoundingBox;
|
---|
| 936 |
|
---|
| 937 |
|
---|
| 938 | //-- local termination
|
---|
| 939 |
|
---|
| 940 | /// minimal number of rays before subdivision termination
|
---|
| 941 | int mTermMinRays;
|
---|
| 942 | /// maximal possible depth
|
---|
| 943 | int mTermMaxDepth;
|
---|
| 944 | /// mininum probability
|
---|
| 945 | float mTermMinProbability;
|
---|
| 946 | /// mininum PVS
|
---|
| 947 | int mTermMinPvs;
|
---|
| 948 | /// maximal contribution per ray
|
---|
| 949 | float mTermMaxRayContribution;
|
---|
| 950 | /// maximal acceptable cost ratio
|
---|
| 951 | float mTermMaxCostRatio;
|
---|
| 952 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
| 953 | int mTermMissTolerance;
|
---|
| 954 |
|
---|
| 955 |
|
---|
| 956 | //-- global criteria
|
---|
| 957 | float mTermMinGlobalCostRatio;
|
---|
| 958 | int mTermGlobalCostMissTolerance;
|
---|
| 959 | int mGlobalCostMisses;
|
---|
| 960 |
|
---|
| 961 | /// maximal number of view cells
|
---|
| 962 | int mMaxViewCells;
|
---|
| 963 | /// maximal tree memory
|
---|
| 964 | float mMaxMemory;
|
---|
| 965 | /// the tree is out of memory
|
---|
| 966 | bool mOutOfMemory;
|
---|
| 967 |
|
---|
| 968 |
|
---|
| 969 | //-- split heuristics based parameters
|
---|
| 970 |
|
---|
| 971 | bool mUseCostHeuristics;
|
---|
| 972 | /// balancing factor for PVS criterium
|
---|
| 973 | float mCtDivCi;
|
---|
| 974 | /// if only driving axis should be used for split
|
---|
| 975 | bool mOnlyDrivingAxis;
|
---|
| 976 | /// if random split axis should be used
|
---|
| 977 | bool mUseRandomAxis;
|
---|
| 978 | /// if vsp bsp tree should simulate octree
|
---|
| 979 | bool mCirculatingAxis;
|
---|
| 980 | /// minimal relative position where the split axis can be placed
|
---|
| 981 | float mMinBand;
|
---|
| 982 | /// maximal relative position where the split axis can be placed
|
---|
| 983 | float mMaxBand;
|
---|
| 984 |
|
---|
| 985 |
|
---|
| 986 | /// current time stamp (used for keeping split history)
|
---|
| 987 | int mTimeStamp;
|
---|
| 988 | // if rays should be stored in leaves
|
---|
| 989 | bool mStoreRays;
|
---|
| 990 |
|
---|
| 991 | /// epsilon for geometric comparisons
|
---|
| 992 | float mEpsilon;
|
---|
| 993 |
|
---|
| 994 |
|
---|
| 995 | /// subdivision stats output file
|
---|
| 996 | ofstream mSubdivisionStats;
|
---|
| 997 | /// keeps track of cost during subdivision
|
---|
| 998 | float mTotalCost;
|
---|
| 999 | /// keeps track of overall pvs size during subdivision
|
---|
| 1000 | int mTotalPvsSize;
|
---|
| 1001 | /// number of currenly generated view cells
|
---|
| 1002 | int mCreatedViewCells;
|
---|
| 1003 |
|
---|
| 1004 | /// weight between render cost decrease and node render cost
|
---|
| 1005 | float mRenderCostDecreaseWeight;
|
---|
| 1006 |
|
---|
| 1007 | int mMaxTests;
|
---|
| 1008 | };
|
---|
| 1009 |
|
---|
| 1010 |
|
---|
| 1011 | }
|
---|
| 1012 |
|
---|
| 1013 | #endif
|
---|