[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"
|
---|
[1667] | 13 | #include "HierarchyManager.h"
|
---|
[2575] | 14 | #ifdef PERFTIMER
|
---|
[2227] | 15 | #include "Timer/PerfTimer.h"
|
---|
[2575] | 16 | #endif // PERFTIMER
|
---|
[1237] | 17 |
|
---|
[2342] | 18 |
|
---|
[2288] | 19 | #ifdef USE_SSE
|
---|
| 20 | #include <xmmintrin.h>
|
---|
| 21 | #endif
|
---|
[1237] | 22 |
|
---|
[2288] | 23 |
|
---|
[1237] | 24 | namespace GtpVisibilityPreprocessor {
|
---|
| 25 |
|
---|
| 26 | class ViewCellLeaf;
|
---|
| 27 | class Plane3;
|
---|
| 28 | class AxisAlignedBox3;
|
---|
| 29 | class Ray;
|
---|
| 30 | class ViewCellsStatistics;
|
---|
| 31 | class ViewCellsManager;
|
---|
| 32 | class MergeCandidate;
|
---|
| 33 | class Beam;
|
---|
| 34 | class ViewCellsTree;
|
---|
| 35 | class Environment;
|
---|
| 36 | class VspInterior;
|
---|
| 37 | class VspLeaf;
|
---|
| 38 | class VspNode;
|
---|
| 39 | class KdNode;
|
---|
| 40 | class KdInterior;
|
---|
| 41 | class KdLeaf;
|
---|
[1259] | 42 | class HierarchyManager;
|
---|
[1237] | 43 | class KdIntersectable;
|
---|
| 44 | class KdTree;
|
---|
| 45 | class VspTree;
|
---|
| 46 | class KdTreeStatistics;
|
---|
[2543] | 47 | class BoundingBoxConverter;
|
---|
[1237] | 48 |
|
---|
[1576] | 49 |
|
---|
[1237] | 50 | /** View space partition statistics.
|
---|
| 51 | */
|
---|
| 52 | class VspTreeStatistics: public StatisticsBase
|
---|
| 53 | {
|
---|
| 54 | public:
|
---|
| 55 | // total number of nodes
|
---|
| 56 | int nodes;
|
---|
| 57 | // number of splits
|
---|
| 58 | int splits[3];
|
---|
| 59 |
|
---|
| 60 | // totals number of rays
|
---|
| 61 | int rays;
|
---|
| 62 | // maximal reached depth
|
---|
| 63 | int maxDepth;
|
---|
| 64 | // minimal depth
|
---|
| 65 | int minDepth;
|
---|
| 66 |
|
---|
| 67 | // max depth nodes
|
---|
| 68 | int maxDepthNodes;
|
---|
| 69 | // minimum depth nodes
|
---|
| 70 | int minDepthNodes;
|
---|
| 71 | // max depth nodes
|
---|
| 72 | int minPvsNodes;
|
---|
| 73 | // nodes with minimum PVS
|
---|
| 74 | int minRaysNodes;
|
---|
| 75 | // max ray contribution nodes
|
---|
| 76 | int maxRayContribNodes;
|
---|
| 77 | // minimum area nodes
|
---|
| 78 | int minProbabilityNodes;
|
---|
| 79 | /// nodes termination because of max cost ratio;
|
---|
| 80 | int maxCostNodes;
|
---|
| 81 | // max number of rays per node
|
---|
| 82 | int maxObjectRefs;
|
---|
| 83 | /// samples contributing to pvs
|
---|
| 84 | int contributingSamples;
|
---|
| 85 | /// sample contributions to pvs
|
---|
| 86 | int sampleContributions;
|
---|
| 87 | /// largest pvs
|
---|
| 88 | int maxPvs;
|
---|
| 89 | /// number of invalid leaves
|
---|
| 90 | int invalidLeaves;
|
---|
[1640] | 91 | /// number of rays refs
|
---|
| 92 | int rayRefs;
|
---|
[1449] | 93 | /// overall pvs size
|
---|
[1237] | 94 | int pvs;
|
---|
| 95 | // accumulated depth (used to compute average)
|
---|
| 96 | int accumDepth;
|
---|
[1449] | 97 | // global cost ratio violations
|
---|
| 98 | int mGlobalCostMisses;
|
---|
[1237] | 99 |
|
---|
| 100 | // Constructor
|
---|
| 101 | VspTreeStatistics()
|
---|
| 102 | {
|
---|
| 103 | Reset();
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | int Nodes() const {return nodes;}
|
---|
| 107 | int Interior() const { return nodes / 2; }
|
---|
| 108 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
| 109 |
|
---|
| 110 | // TODO: computation wrong
|
---|
| 111 | double AvgDepth() const { return accumDepth / (double)Leaves();};
|
---|
[1640] | 112 | double AvgRays() const { return rayRefs / (double)Leaves();};
|
---|
[1237] | 113 |
|
---|
| 114 | void Reset()
|
---|
| 115 | {
|
---|
| 116 | nodes = 0;
|
---|
| 117 | for (int i = 0; i < 3; ++ i)
|
---|
| 118 | splits[i] = 0;
|
---|
| 119 |
|
---|
| 120 | maxDepth = 0;
|
---|
| 121 | minDepth = 99999;
|
---|
| 122 | accumDepth = 0;
|
---|
| 123 | pvs = 0;
|
---|
| 124 | maxDepthNodes = 0;
|
---|
| 125 | minPvsNodes = 0;
|
---|
| 126 | minRaysNodes = 0;
|
---|
| 127 | maxRayContribNodes = 0;
|
---|
| 128 | minProbabilityNodes = 0;
|
---|
| 129 | maxCostNodes = 0;
|
---|
| 130 |
|
---|
| 131 | contributingSamples = 0;
|
---|
| 132 | sampleContributions = 0;
|
---|
| 133 |
|
---|
| 134 | maxPvs = 0;
|
---|
| 135 | invalidLeaves = 0;
|
---|
[1640] | 136 | rayRefs = 0;
|
---|
[1237] | 137 | maxObjectRefs = 0;
|
---|
[1449] | 138 | mGlobalCostMisses = 0;
|
---|
[1237] | 139 | }
|
---|
| 140 |
|
---|
[2176] | 141 | void Print(std::ostream &app) const;
|
---|
[1237] | 142 |
|
---|
[2176] | 143 | friend std::ostream &operator<<(std::ostream &s, const VspTreeStatistics &stat)
|
---|
[1237] | 144 | {
|
---|
| 145 | stat.Print(s);
|
---|
| 146 | return s;
|
---|
| 147 | }
|
---|
| 148 | };
|
---|
| 149 |
|
---|
| 150 |
|
---|
[2539] | 151 | /** VspNode abstract class serving for interior and leaf node implementation
|
---|
[1237] | 152 | */
|
---|
| 153 | class VspNode
|
---|
| 154 | {
|
---|
[2539] | 155 | friend class VspTree;
|
---|
| 156 |
|
---|
[1237] | 157 | public:
|
---|
| 158 |
|
---|
[1679] | 159 | /// types of vsp nodes
|
---|
[1237] | 160 | enum {Interior, Leaf};
|
---|
| 161 |
|
---|
| 162 | VspNode();
|
---|
| 163 | virtual ~VspNode(){};
|
---|
| 164 | VspNode(VspInterior *parent);
|
---|
| 165 |
|
---|
| 166 | /** Determines whether this node is a leaf or not
|
---|
| 167 | @return true if leaf
|
---|
| 168 | */
|
---|
| 169 | virtual bool IsLeaf() const = 0;
|
---|
| 170 |
|
---|
| 171 | virtual int Type() const = 0;
|
---|
| 172 |
|
---|
| 173 | /** Determines whether this node is a root
|
---|
| 174 | @return true if root
|
---|
| 175 | */
|
---|
| 176 | virtual bool IsRoot() const;
|
---|
| 177 | /** Returns parent node.
|
---|
| 178 | */
|
---|
| 179 | VspInterior *GetParent();
|
---|
| 180 | /** Sets parent node.
|
---|
| 181 | */
|
---|
| 182 | void SetParent(VspInterior *parent);
|
---|
| 183 | /** Returns true if this node is a sibling of node n.
|
---|
| 184 | */
|
---|
| 185 | bool IsSibling(VspNode *n) const;
|
---|
| 186 | /** returns depth of the node.
|
---|
| 187 | */
|
---|
| 188 | int GetDepth() const;
|
---|
[2539] | 189 | /** Returns true if this node (which represents the whole subtree) is valid
|
---|
[1237] | 190 | */
|
---|
| 191 | bool TreeValid() const;
|
---|
[2539] | 192 | /** Sets this node to valid.
|
---|
| 193 | */
|
---|
[1237] | 194 | void SetTreeValid(const bool v);
|
---|
[1679] | 195 | /** Cost of mergin this node.
|
---|
| 196 | */
|
---|
| 197 | float GetMergeCost() {return (float)-mTimeStamp; }
|
---|
[1237] | 198 |
|
---|
[2539] | 199 | /// timestamp of this node
|
---|
[1732] | 200 | int mTimeStamp;
|
---|
| 201 |
|
---|
[1679] | 202 | /////////
|
---|
[1237] | 203 | //-- mailing options
|
---|
| 204 |
|
---|
| 205 | void Mail() { mMailbox = sMailId; }
|
---|
| 206 | static void NewMail() { ++ sMailId; }
|
---|
| 207 | bool Mailed() const { return mMailbox == sMailId; }
|
---|
| 208 |
|
---|
[1292] | 209 |
|
---|
[1237] | 210 | static int sMailId;
|
---|
| 211 | int mMailbox;
|
---|
| 212 |
|
---|
[2539] | 213 |
|
---|
[1237] | 214 | protected:
|
---|
| 215 |
|
---|
| 216 | /// if this sub tree is a completely valid view space region
|
---|
| 217 | bool mTreeValid;
|
---|
| 218 | /// parent of this node
|
---|
| 219 | VspInterior *mParent;
|
---|
| 220 | };
|
---|
| 221 |
|
---|
| 222 |
|
---|
| 223 | /** BSP interior node implementation
|
---|
| 224 | */
|
---|
| 225 | class VspInterior: public VspNode
|
---|
| 226 | {
|
---|
[2539] | 227 | friend class VspTree;
|
---|
| 228 |
|
---|
[1237] | 229 | public:
|
---|
[1679] | 230 |
|
---|
[1237] | 231 | /** Standard contructor taking split plane as argument.
|
---|
| 232 | */
|
---|
| 233 | VspInterior(const AxisAlignedPlane &plane);
|
---|
| 234 |
|
---|
| 235 | ~VspInterior();
|
---|
| 236 | /** @return false since it is an interior node
|
---|
| 237 | */
|
---|
[2539] | 238 | bool IsLeaf() const { return false; }
|
---|
[1237] | 239 |
|
---|
| 240 | int Type() const;
|
---|
[2539] | 241 | /** Returns back child.
|
---|
| 242 | */
|
---|
| 243 | VspNode *GetBack() { return mBack; }
|
---|
| 244 | /** Returns front child.
|
---|
| 245 | */
|
---|
| 246 | VspNode *GetFront() { return mFront; }
|
---|
[1237] | 247 | /** Returns split plane.
|
---|
| 248 | */
|
---|
[2539] | 249 | AxisAlignedPlane GetPlane() const { return mPlane; }
|
---|
[1237] | 250 | /** Returns position of split plane.
|
---|
| 251 | */
|
---|
[2539] | 252 | float GetPosition() const { return mPlane.mPosition; }
|
---|
[1237] | 253 | /** Returns split axis.
|
---|
| 254 | */
|
---|
[2539] | 255 | int GetAxis() const { return mPlane.mAxis; }
|
---|
[1237] | 256 | /** Replace front or back child with new child.
|
---|
| 257 | */
|
---|
| 258 | void ReplaceChildLink(VspNode *oldChild, VspNode *newChild);
|
---|
| 259 | /** Replace front and back child.
|
---|
| 260 | */
|
---|
| 261 | void SetupChildLinks(VspNode *front, VspNode *back);
|
---|
[2544] | 262 | /** Returns bounding box of this node.
|
---|
[2539] | 263 | */
|
---|
[1237] | 264 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
[2539] | 265 | /** Sets boundiong box of this node.
|
---|
| 266 | */
|
---|
[1237] | 267 | void SetBoundingBox(const AxisAlignedBox3 &box);
|
---|
| 268 | /** Computes intersection of this plane with the ray segment.
|
---|
| 269 | */
|
---|
| 270 | int ComputeRayIntersection(const RayInfo &rayData, float &t) const
|
---|
| 271 | {
|
---|
| 272 | return rayData.ComputeRayIntersection(mPlane.mAxis, mPlane.mPosition, t);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[2539] | 275 | friend std::ostream &operator<<(std::ostream &s, const VspInterior &A)
|
---|
| 276 | {
|
---|
| 277 | return s << A.mPlane.mAxis << " " << A.mPlane.mPosition;
|
---|
| 278 | }
|
---|
[1237] | 279 |
|
---|
[2539] | 280 |
|
---|
[1237] | 281 | protected:
|
---|
[2539] | 282 |
|
---|
[1357] | 283 | /// bounding box for this interior node: should we really store this?
|
---|
[1237] | 284 | AxisAlignedBox3 mBoundingBox;
|
---|
| 285 | /// Splitting plane corresponding to this node
|
---|
| 286 | AxisAlignedPlane mPlane;
|
---|
| 287 | /// back node
|
---|
| 288 | VspNode *mBack;
|
---|
| 289 | /// front node
|
---|
| 290 | VspNode *mFront;
|
---|
| 291 | };
|
---|
| 292 |
|
---|
| 293 |
|
---|
| 294 | /** BSP leaf node implementation.
|
---|
| 295 | */
|
---|
| 296 | class VspLeaf: public VspNode
|
---|
| 297 | {
|
---|
[2575] | 298 | friend class VspTree;
|
---|
[1237] | 299 |
|
---|
| 300 | public:
|
---|
| 301 | VspLeaf();
|
---|
| 302 | VspLeaf(ViewCellLeaf *viewCell);
|
---|
| 303 | VspLeaf(VspInterior *parent);
|
---|
| 304 | VspLeaf(VspInterior *parent, ViewCellLeaf *viewCell);
|
---|
| 305 |
|
---|
| 306 | ~VspLeaf();
|
---|
| 307 |
|
---|
| 308 | /** @return true since it is an interior node
|
---|
| 309 | */
|
---|
[2539] | 310 | bool IsLeaf() const { return true; }
|
---|
[1237] | 311 |
|
---|
| 312 | int Type() const;
|
---|
| 313 |
|
---|
| 314 | /** Returns pointer of view cell.
|
---|
| 315 | */
|
---|
[2539] | 316 | ViewCellLeaf *GetViewCell() const { return mViewCell; }
|
---|
[1237] | 317 | /** Sets pointer to view cell.
|
---|
| 318 | */
|
---|
| 319 | void SetViewCell(ViewCellLeaf *viewCell);
|
---|
| 320 |
|
---|
[1297] | 321 | SubdivisionCandidate *GetSubdivisionCandidate()
|
---|
[1237] | 322 | {
|
---|
| 323 | return mSubdivisionCandidate;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
[1297] | 326 | void SetSubdivisionCandidate(SubdivisionCandidate *candidate)
|
---|
| 327 | {
|
---|
| 328 | mSubdivisionCandidate = candidate;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 |
|
---|
[1237] | 332 | public:
|
---|
| 333 |
|
---|
| 334 | /// Rays piercing this leaf.
|
---|
| 335 | VssRayContainer mVssRays;
|
---|
[1679] | 336 |
|
---|
[1237] | 337 | /// Probability that the view point lies in this leaf
|
---|
| 338 | float mProbability;
|
---|
| 339 |
|
---|
| 340 | protected:
|
---|
| 341 |
|
---|
| 342 | /// pointer to a split plane candidate splitting this leaf
|
---|
| 343 | SubdivisionCandidate *mSubdivisionCandidate;
|
---|
[1679] | 344 |
|
---|
[1237] | 345 | /// if NULL this does not correspond to feasible viewcell
|
---|
| 346 | ViewCellLeaf *mViewCell;
|
---|
| 347 | };
|
---|
| 348 |
|
---|
| 349 |
|
---|
| 350 | /** View Space Partitioning tree.
|
---|
| 351 | */
|
---|
| 352 | class VspTree
|
---|
| 353 | {
|
---|
| 354 | friend class ViewCellsParseHandlers;
|
---|
| 355 | friend class HierarchyManager;
|
---|
| 356 |
|
---|
| 357 | public:
|
---|
| 358 |
|
---|
[2539] | 359 | ///////////
|
---|
| 360 | //-- Helper structs / classes
|
---|
| 361 |
|
---|
[1237] | 362 | /** Additional data which is passed down the BSP tree during traversal.
|
---|
| 363 | */
|
---|
[2539] | 364 | struct VspTraversalData
|
---|
[1237] | 365 | {
|
---|
| 366 | public:
|
---|
| 367 |
|
---|
[2539] | 368 | VspTraversalData();
|
---|
[1237] | 369 |
|
---|
| 370 | VspTraversalData(VspLeaf *node,
|
---|
| 371 | const int depth,
|
---|
| 372 | RayInfoContainer *rays,
|
---|
[1765] | 373 | const float pvs,
|
---|
[1237] | 374 | const float p,
|
---|
[2539] | 375 | const AxisAlignedBox3 &box);
|
---|
[1237] | 376 |
|
---|
[2539] | 377 |
|
---|
[1237] | 378 | /** Returns cost of the traversal data.
|
---|
| 379 | */
|
---|
[2539] | 380 | inline float GetCost() const;
|
---|
| 381 | /** Returns average ray contribution.
|
---|
| 382 | */
|
---|
| 383 | inline float GetAvgRayContribution() const;
|
---|
| 384 | /** Returns average rays per object.
|
---|
| 385 | */
|
---|
| 386 | inline float GetAvgRaysPerObject() const;
|
---|
| 387 | /** deletes contents and sets them to NULL.
|
---|
| 388 | */
|
---|
| 389 | void Clear();
|
---|
[1237] | 390 |
|
---|
[1297] | 391 |
|
---|
[2539] | 392 | //////////////////////
|
---|
[1912] | 393 |
|
---|
[1294] | 394 | /// the current node
|
---|
| 395 | VspLeaf *mNode;
|
---|
| 396 | /// current depth
|
---|
| 397 | int mDepth;
|
---|
| 398 | /// rays piercing this node
|
---|
| 399 | RayInfoContainer *mRays;
|
---|
| 400 | /// the probability that this node contains view point
|
---|
| 401 | float mProbability;
|
---|
| 402 | /// the bounding box of the node
|
---|
| 403 | AxisAlignedBox3 mBoundingBox;
|
---|
| 404 | /// how often this branch has missed the max-cost ratio
|
---|
| 405 | int mMaxCostMisses;
|
---|
| 406 | // current priority
|
---|
| 407 | float mPriority;
|
---|
[1912] | 408 | /// pvs size
|
---|
| 409 | float mPvs;
|
---|
| 410 | /// the correction factor for this pvs
|
---|
| 411 | float mCorrectedPvs;
|
---|
| 412 | /// pvs size
|
---|
| 413 | float mRenderCost;
|
---|
| 414 | /// the correction factor for this pvs
|
---|
| 415 | float mCorrectedRenderCost;
|
---|
[1294] | 416 |
|
---|
[1237] | 417 | friend bool operator<(const VspTraversalData &a, const VspTraversalData &b)
|
---|
| 418 | {
|
---|
| 419 | return a.GetCost() < b.GetCost();
|
---|
| 420 | }
|
---|
| 421 | };
|
---|
| 422 |
|
---|
[2539] | 423 | /** Helper struct for data that comes with a split of a node.
|
---|
| 424 | */
|
---|
[2347] | 425 | struct SplitData
|
---|
| 426 | {
|
---|
| 427 | SplitData():
|
---|
| 428 | mFrontRenderCost(0),
|
---|
| 429 | mBackRenderCost(0),
|
---|
| 430 | mTotalRenderCost(0),
|
---|
| 431 | mFrontObjects(0),
|
---|
| 432 | mBackObjects(0),
|
---|
| 433 | mTotalObjects(0),
|
---|
| 434 | mFrontTriangles(0),
|
---|
| 435 | mBackTriangles(0),
|
---|
[2539] | 436 | mTotalTriangles(0)
|
---|
| 437 | {}
|
---|
[2347] | 438 |
|
---|
[2539] | 439 | //////////////
|
---|
[2347] | 440 |
|
---|
| 441 | float mFrontRenderCost;
|
---|
| 442 | float mBackRenderCost;
|
---|
| 443 | float mTotalRenderCost;
|
---|
| 444 |
|
---|
| 445 | int mFrontObjects;
|
---|
| 446 | int mBackObjects;
|
---|
| 447 | int mTotalObjects;
|
---|
| 448 |
|
---|
| 449 | int mFrontTriangles;
|
---|
| 450 | int mBackTriangles;
|
---|
| 451 | int mTotalTriangles;
|
---|
| 452 | };
|
---|
| 453 |
|
---|
| 454 |
|
---|
[1237] | 455 | /** Candidate for a view space split.
|
---|
| 456 | */
|
---|
| 457 | class VspSubdivisionCandidate: public SubdivisionCandidate
|
---|
| 458 | {
|
---|
| 459 | public:
|
---|
| 460 |
|
---|
| 461 | VspSubdivisionCandidate(const VspTraversalData &tData): mParentData(tData)
|
---|
| 462 | {};
|
---|
| 463 |
|
---|
[1305] | 464 | ~VspSubdivisionCandidate()
|
---|
| 465 | {
|
---|
| 466 | mParentData.Clear();
|
---|
| 467 | }
|
---|
[1294] | 468 |
|
---|
[1237] | 469 | int Type() const { return VIEW_SPACE; }
|
---|
| 470 |
|
---|
[1667] | 471 | void EvalCandidate(bool computeSplitplane = true)
|
---|
[1237] | 472 | {
|
---|
[1695] | 473 | mDirty = false;
|
---|
[1667] | 474 | sVspTree->EvalSubdivisionCandidate(*this, computeSplitplane);
|
---|
[1237] | 475 | }
|
---|
| 476 |
|
---|
| 477 | bool GlobalTerminationCriteriaMet() const
|
---|
| 478 | {
|
---|
| 479 | return sVspTree->GlobalTerminationCriteriaMet(mParentData);
|
---|
| 480 | }
|
---|
| 481 |
|
---|
[2233] | 482 | bool Apply(SplitQueue &splitQueue,
|
---|
| 483 | bool terminationCriteriaMet,
|
---|
| 484 | SubdivisionCandidateContainer &dirtyList)
|
---|
[1633] | 485 | {
|
---|
| 486 | VspNode *n = sVspTree->Subdivide(splitQueue, this, terminationCriteriaMet);
|
---|
| 487 |
|
---|
| 488 | // local or global termination criteria failed
|
---|
[2224] | 489 | const bool success = !n->IsLeaf();
|
---|
| 490 |
|
---|
| 491 | if (success)
|
---|
| 492 | CollectDirtyCandidates(dirtyList, true);
|
---|
| 493 |
|
---|
| 494 | return success;
|
---|
[1633] | 495 | }
|
---|
| 496 |
|
---|
| 497 | void CollectDirtyCandidates(SubdivisionCandidateContainer &dirtyList,
|
---|
| 498 | const bool onlyUnmailed)
|
---|
| 499 | {
|
---|
| 500 | sVspTree->CollectDirtyCandidates(this, dirtyList, onlyUnmailed);
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[1912] | 503 | VspSubdivisionCandidate(const AxisAlignedPlane &plane, const VspTraversalData &tData):
|
---|
| 504 | mSplitPlane(plane), mParentData(tData)
|
---|
[1237] | 505 | {}
|
---|
[1667] | 506 |
|
---|
[2539] | 507 | float GetPriority() const { return mPriority; }
|
---|
[1912] | 508 |
|
---|
[2539] | 509 |
|
---|
[1912] | 510 | ////////////////////
|
---|
| 511 |
|
---|
| 512 | static VspTree* sVspTree;
|
---|
| 513 | /// the current split plane
|
---|
| 514 | AxisAlignedPlane mSplitPlane;
|
---|
| 515 | /// parent node traversal data
|
---|
| 516 | VspTraversalData mParentData;
|
---|
| 517 |
|
---|
| 518 | float mCorrectedFrontPvs;
|
---|
| 519 | float mCorrectedBackPvs;
|
---|
| 520 |
|
---|
[2210] | 521 | float mFrontPvs;
|
---|
| 522 | float mBackPvs;
|
---|
| 523 |
|
---|
[2332] | 524 | float mFrontTriangles;
|
---|
| 525 | float mBackTriangles;
|
---|
| 526 |
|
---|
[1912] | 527 | float mCorrectedFrontRenderCost;
|
---|
| 528 | float mCorrectedBackRenderCost;
|
---|
[2224] | 529 |
|
---|
[2210] | 530 | float mFrontRenderCost;
|
---|
| 531 | float mBackRenderCost;
|
---|
[1237] | 532 | };
|
---|
| 533 |
|
---|
| 534 | /** Struct for traversing line segment.
|
---|
| 535 | */
|
---|
| 536 | struct LineTraversalData
|
---|
| 537 | {
|
---|
| 538 | VspNode *mNode;
|
---|
| 539 | Vector3 mExitPoint;
|
---|
| 540 | float mMaxT;
|
---|
| 541 |
|
---|
| 542 | LineTraversalData () {}
|
---|
| 543 | LineTraversalData (VspNode *n, const Vector3 &p, const float maxt):
|
---|
| 544 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
| 545 | };
|
---|
| 546 |
|
---|
[2539] | 547 |
|
---|
| 548 | //////////////////////
|
---|
| 549 |
|
---|
| 550 |
|
---|
[1237] | 551 | /** Default constructor creating an empty tree.
|
---|
| 552 | */
|
---|
| 553 | VspTree();
|
---|
[2547] | 554 | /** Constructor just setting the bounds of the tree.
|
---|
| 555 | */
|
---|
| 556 | VspTree(const AxisAlignedBox3 &bbox);
|
---|
[1237] | 557 | /** Default destructor.
|
---|
| 558 | */
|
---|
| 559 | ~VspTree();
|
---|
| 560 | /** Returns BSP Tree statistics.
|
---|
| 561 | */
|
---|
| 562 | const VspTreeStatistics &GetStatistics() const;
|
---|
| 563 | /** Returns bounding box of the specified node.
|
---|
| 564 | */
|
---|
| 565 | AxisAlignedBox3 GetBoundingBox(VspNode *node) const;
|
---|
| 566 | /** Returns list of BSP leaves with pvs smaller than
|
---|
| 567 | a certain threshold.
|
---|
| 568 | @param onlyUnmailed if only the unmailed leaves should be considered
|
---|
| 569 | @param maxPvs the maximal pvs of a leaf to be added (-1 means unlimited)
|
---|
| 570 | */
|
---|
| 571 | void CollectLeaves(vector<VspLeaf *> &leaves,
|
---|
| 572 | const bool onlyUnmailed = false,
|
---|
| 573 | const int maxPvs = -1) const;
|
---|
| 574 | /** Returns box which bounds the whole tree.
|
---|
| 575 | */
|
---|
| 576 | AxisAlignedBox3 GetBoundingBox() const;
|
---|
| 577 | /** Returns root of the view space partitioning tree.
|
---|
| 578 | */
|
---|
| 579 | VspNode *GetRoot() const;
|
---|
| 580 | /** Collects the leaf view cells of the tree
|
---|
| 581 | @param viewCells returns the view cells
|
---|
| 582 | */
|
---|
| 583 | void CollectViewCells(ViewCellContainer &viewCells, bool onlyValid) const;
|
---|
| 584 | /** A ray is cast possible intersecting the tree.
|
---|
| 585 | @param the ray that is cast.
|
---|
| 586 | @returns the number of intersections with objects stored in the tree.
|
---|
| 587 | */
|
---|
| 588 | int CastRay(Ray &ray);
|
---|
| 589 | /** finds neighbouring leaves of this tree node.
|
---|
| 590 | */
|
---|
| 591 | int FindNeighbors(VspLeaf *n,
|
---|
| 592 | vector<VspLeaf *> &neighbors,
|
---|
| 593 | const bool onlyUnmailed) const;
|
---|
| 594 | /** Returns random leaf of BSP tree.
|
---|
| 595 | @param halfspace defines the halfspace from which the leaf is taken.
|
---|
| 596 | */
|
---|
| 597 | VspLeaf *GetRandomLeaf(const Plane3 &halfspace);
|
---|
| 598 |
|
---|
| 599 | /** Returns random leaf of BSP tree.
|
---|
| 600 | @param onlyUnmailed if only unmailed leaves should be returned.
|
---|
| 601 | */
|
---|
| 602 | VspLeaf *GetRandomLeaf(const bool onlyUnmailed = false);
|
---|
| 603 | /** Returns epsilon of this tree.
|
---|
| 604 | */
|
---|
| 605 | float GetEpsilon() const;
|
---|
| 606 | /** Casts line segment into the tree.
|
---|
| 607 | @param origin the origin of the line segment
|
---|
| 608 | @param termination the end point of the line segment
|
---|
| 609 | @returns view cells intersecting the line segment.
|
---|
| 610 | */
|
---|
| 611 | int CastLineSegment(const Vector3 &origin,
|
---|
| 612 | const Vector3 &termination,
|
---|
[1291] | 613 | ViewCellContainer &viewcells,
|
---|
| 614 | const bool useMailboxing = true);
|
---|
[1237] | 615 | /** Sets pointer to view cells manager.
|
---|
| 616 | */
|
---|
| 617 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
| 618 | /** Returns view cell the current point is located in.
|
---|
| 619 | @param point the current view point
|
---|
| 620 | @param active if currently active view cells should be returned or
|
---|
| 621 | elementary view cell
|
---|
| 622 | */
|
---|
| 623 | ViewCell *GetViewCell(const Vector3 &point, const bool active = false);
|
---|
| 624 | /** Returns true if this view point is in a valid view space,
|
---|
| 625 | false otherwise.
|
---|
| 626 | */
|
---|
| 627 | bool ViewPointValid(const Vector3 &viewPoint) const;
|
---|
| 628 | /** Returns view cell corresponding to
|
---|
| 629 | the invalid view space.
|
---|
| 630 | */
|
---|
| 631 | VspViewCell *GetOutOfBoundsCell();
|
---|
| 632 | /** Casts beam, i.e. a 5D frustum of rays, into tree.
|
---|
| 633 | Tests conservative using the bounding box of the nodes.
|
---|
| 634 | @returns number of view cells it intersected
|
---|
| 635 | */
|
---|
| 636 | int CastBeam(Beam &beam);
|
---|
| 637 | /** Checks if tree validity-flags are right
|
---|
| 638 | with respect to view cell valitiy.
|
---|
| 639 | If not, marks subtree as invalid.
|
---|
| 640 | */
|
---|
| 641 | void ValidateTree();
|
---|
| 642 | /** Collects rays stored in the leaves.
|
---|
| 643 | */
|
---|
| 644 | void CollectRays(VssRayContainer &rays);
|
---|
| 645 | /** Intersects box with the tree and returns the number of intersected boxes.
|
---|
| 646 | @returns number of view cells found
|
---|
| 647 | */
|
---|
| 648 | int ComputeBoxIntersections(const AxisAlignedBox3 &box,
|
---|
| 649 | ViewCellContainer &viewCells) const;
|
---|
| 650 | /** Returns view cells of this ray, either taking precomputed cells
|
---|
| 651 | or by recomputation.
|
---|
| 652 | */
|
---|
| 653 | void GetViewCells(const VssRay &ray, ViewCellContainer &viewCells);
|
---|
[1291] | 654 | /** Returns view cells tree.
|
---|
| 655 | */
|
---|
[1237] | 656 | ViewCellsTree *GetViewCellsTree() const { return mViewCellsTree; }
|
---|
[1291] | 657 | /** Sets the view cells tree.
|
---|
| 658 | */
|
---|
[1237] | 659 | void SetViewCellsTree(ViewCellsTree *vt) { mViewCellsTree = vt; }
|
---|
[2539] | 660 | /** Writes vsp tree to output stream
|
---|
| 661 | */
|
---|
| 662 | bool Export(OUT_STREAM &stream);
|
---|
| 663 | /** Writes vsp tree to binary output stream.
|
---|
| 664 | */
|
---|
| 665 | bool ExportBinary(OUT_STREAM &stream);
|
---|
| 666 | /** Imports tree from binary format.
|
---|
| 667 | */
|
---|
[2544] | 668 | bool ImportBinary(IN_STREAM &stream);
|
---|
[1237] | 669 |
|
---|
[2543] | 670 | void TestOutput(const std::string &filename);
|
---|
[1237] | 671 |
|
---|
[2539] | 672 | ////////////
|
---|
| 673 |
|
---|
[2575] | 674 | #ifdef PERFTIMER
|
---|
[2187] | 675 | PerfTimer mSortTimer;
|
---|
| 676 | PerfTimer mSplitTimer;
|
---|
| 677 | PerfTimer mNodeTimer;
|
---|
| 678 | PerfTimer mSubdivTimer;
|
---|
| 679 | PerfTimer mEvalTimer;
|
---|
[2198] | 680 | PerfTimer mPlaneTimer;
|
---|
| 681 | PerfTimer mViewCellsTimer;
|
---|
[2575] | 682 | #endif
|
---|
| 683 |
|
---|
[1237] | 684 | protected:
|
---|
| 685 |
|
---|
| 686 | // --------------------------------------------------------------
|
---|
| 687 | // For sorting objects
|
---|
| 688 | // --------------------------------------------------------------
|
---|
| 689 | struct SortableEntry
|
---|
| 690 | {
|
---|
| 691 | enum EType
|
---|
| 692 | {
|
---|
| 693 | ERayMin,
|
---|
| 694 | ERayMax
|
---|
| 695 | };
|
---|
| 696 |
|
---|
| 697 | int type;
|
---|
| 698 | float value;
|
---|
| 699 | VssRay *ray;
|
---|
| 700 |
|
---|
| 701 | SortableEntry() {}
|
---|
| 702 | SortableEntry(const int t, const float v, VssRay *r):
|
---|
| 703 | type(t), value(v), ray(r)
|
---|
| 704 | {
|
---|
| 705 | }
|
---|
| 706 |
|
---|
[1314] | 707 | friend inline bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
| 708 | { // prefer max event
|
---|
| 709 | //if (EpsilonEqual(a.value, b.value, 0.0001f))
|
---|
| 710 | // return (a.type == ERayMax);
|
---|
| 711 |
|
---|
| 712 | return (a.value < b.value);
|
---|
[1237] | 713 | }
|
---|
| 714 | };
|
---|
[2547] | 715 | /** Parse the environment settings.
|
---|
| 716 | */
|
---|
| 717 | void ParseEnvironment();
|
---|
[1237] | 718 | /** faster evaluation of split plane cost for kd axis aligned cells.
|
---|
| 719 | */
|
---|
| 720 | float EvalLocalSplitCost(const VspTraversalData &data,
|
---|
| 721 | const AxisAlignedBox3 &box,
|
---|
| 722 | const int axis,
|
---|
| 723 | const float &position,
|
---|
| 724 | float &pFront,
|
---|
| 725 | float &pBack) const;
|
---|
[2539] | 726 | /** If != NULL, sets the bounding box to the forced bounding box.
|
---|
| 727 | Else computes the bounding box of the view space using the
|
---|
| 728 | hit points gathered from the ray-object intersections.
|
---|
| 729 | */
|
---|
[1237] | 730 | void ComputeBoundingBox(const VssRayContainer &rays,
|
---|
| 731 | AxisAlignedBox3 *forcedBoundingBox);
|
---|
| 732 | /** Evaluates candidate for splitting.
|
---|
| 733 | */
|
---|
[2539] | 734 | void EvalSubdivisionCandidate(VspSubdivisionCandidate &candidate,
|
---|
[1667] | 735 | bool computeSplitPlane = true);
|
---|
[1237] | 736 | /** Evaluates render cost decrease of next split.
|
---|
| 737 | */
|
---|
[2539] | 738 | float EvalRenderCostDecrease(VspSubdivisionCandidate &candidate,
|
---|
[2237] | 739 | float &normalizedOldRenderCost,
|
---|
[2347] | 740 | const SplitData &data) const;
|
---|
[1237] | 741 | /** Collects view cells in the subtree under root.
|
---|
| 742 | */
|
---|
| 743 | void CollectViewCells(VspNode *root,
|
---|
| 744 | bool onlyValid,
|
---|
| 745 | ViewCellContainer &viewCells,
|
---|
| 746 | bool onlyUnmailed = false) const;
|
---|
| 747 | /** Returns view cell corresponding to
|
---|
| 748 | the invalid view space. If it does not exist, it is created.
|
---|
| 749 | */
|
---|
| 750 | VspViewCell *GetOrCreateOutOfBoundsCell();
|
---|
| 751 | /** Collapses the tree with respect to the view cell partition,
|
---|
| 752 | i.e. leaves having the same view cell are collapsed.
|
---|
| 753 | @param node the root of the subtree to be collapsed
|
---|
| 754 | @param collapsed returns the number of collapsed nodes
|
---|
| 755 | @returns node of type leaf if the node could be collapsed,
|
---|
| 756 | this node otherwise
|
---|
| 757 | */
|
---|
| 758 | VspNode *CollapseTree(VspNode *node, int &collapsed);
|
---|
| 759 | /** Helper function revalidating the view cell leaf list after merge.
|
---|
| 760 | */
|
---|
| 761 | void RepairViewCellsLeafLists();
|
---|
| 762 | /** Evaluates tree stats in the BSP tree leafs.
|
---|
| 763 | */
|
---|
| 764 | void EvaluateLeafStats(const VspTraversalData &data);
|
---|
| 765 | /** Subdivides node using a best split priority queue.
|
---|
| 766 | @param tQueue the best split priority queue
|
---|
| 767 | @param splitCandidate the candidate for the next split
|
---|
| 768 | @param globalCriteriaMet if the global termination criteria were already met
|
---|
| 769 | @returns new root of the subtree
|
---|
| 770 | */
|
---|
| 771 | VspNode *Subdivide(SplitQueue &tQueue,
|
---|
| 772 | SubdivisionCandidate *splitCandidate,
|
---|
| 773 | const bool globalCriteriaMet);
|
---|
| 774 | /** Adds stats to subdivision log file.
|
---|
| 775 | */
|
---|
| 776 | void AddSubdivisionStats(const int viewCells,
|
---|
| 777 | const float renderCostDecr,
|
---|
| 778 | const float totalRenderCost,
|
---|
| 779 | const float avgRenderCost);
|
---|
| 780 | /** Subdivides leaf.
|
---|
| 781 | @param tData data object holding, e.g., a pointer to the leaf
|
---|
[2124] | 782 | @param frontData returns the data (e.g., pointer to the leaf) in front of the split plane
|
---|
| 783 | @param backData returns the data (e.g., pointer to the leaf) in the back of the split plane
|
---|
[1237] | 784 |
|
---|
| 785 | @returns the root of the subdivision
|
---|
| 786 | */
|
---|
[1912] | 787 | VspInterior *SubdivideNode(const VspSubdivisionCandidate &sc,
|
---|
[1237] | 788 | VspTraversalData &frontData,
|
---|
| 789 | VspTraversalData &backData);
|
---|
| 790 |
|
---|
| 791 | /** Selects an axis aligned for the next split.
|
---|
| 792 | @returns cost for this split
|
---|
| 793 | */
|
---|
| 794 | float SelectSplitPlane(const VspTraversalData &tData,
|
---|
| 795 | AxisAlignedPlane &plane,
|
---|
| 796 | float &pFront,
|
---|
| 797 | float &pBack);
|
---|
| 798 | /** Sorts split candidates along the specified axis.
|
---|
| 799 | The split candidates are generated on possible visibility
|
---|
| 800 | events (i.e., where ray segments intersect the ray boundaries).
|
---|
| 801 | The sorted candidates are needed to compute the heuristics.
|
---|
| 802 |
|
---|
| 803 | @param polys the input for choosing split candidates
|
---|
| 804 | @param axis the current split axis
|
---|
| 805 | @param splitCandidates returns sorted list of split candidates
|
---|
| 806 | */
|
---|
| 807 | void SortSubdivisionCandidates(const RayInfoContainer &rays,
|
---|
[1912] | 808 | const int axis,
|
---|
| 809 | float minBand,
|
---|
| 810 | float maxBand);
|
---|
[1765] | 811 | /** Evaluate render cost of this pvs.
|
---|
[1237] | 812 | */
|
---|
[1765] | 813 | float EvalPvsCost(const RayInfoContainer &rays) const;
|
---|
[2547] | 814 | /** Helper function for split cost evaluation.
|
---|
| 815 | */
|
---|
[2237] | 816 | int EvalPvsEntriesIncr(VspSubdivisionCandidate &splitCandidate,
|
---|
[2347] | 817 | const SplitData &sData) const;
|
---|
[1576] | 818 | /** Returns number of effective entries in the pvs.
|
---|
| 819 | */
|
---|
| 820 | int EvalPvsEntriesSize(const RayInfoContainer &rays) const;
|
---|
[1765] | 821 |
|
---|
[1576] | 822 | int EvalPvsEntriesContribution(const VssRay &ray, const bool isTermination) const;
|
---|
[1237] | 823 | /** Computes best cost for axis aligned planes.
|
---|
| 824 | */
|
---|
| 825 | float EvalLocalCostHeuristics(const VspTraversalData &tData,
|
---|
| 826 | const AxisAlignedBox3 &box,
|
---|
| 827 | const int axis,
|
---|
[1765] | 828 | float &position,
|
---|
| 829 | const RayInfoContainer &usedRays);
|
---|
[1237] | 830 |
|
---|
[1291] | 831 |
|
---|
[2539] | 832 | ///////////////////////
|
---|
| 833 | //-- Helper function for computing heuristics
|
---|
[1291] | 834 |
|
---|
[1259] | 835 | /** Evaluates the contribution to left and right pvs at a visibility event ve.
|
---|
[1237] | 836 | @param ve the visibility event
|
---|
| 837 | @param pvsLeft updates the left pvs
|
---|
| 838 | @param rightPvs updates the right pvs
|
---|
| 839 | */
|
---|
[2199] | 840 | inline void EvalHeuristics(const SortableEntry &ve, float &pvsLeft, float &pvsRight) const;
|
---|
[1291] | 841 | /** Evaluates contribution of min event to pvs
|
---|
[1259] | 842 | */
|
---|
[2237] | 843 | inline float EvalMinEventContribution(const VssRay &ray, const bool isTermination) const;
|
---|
[1291] | 844 | /** Evaluates contribution of max event to pvs
|
---|
| 845 | */
|
---|
[2237] | 846 | inline float EvalMaxEventContribution(const VssRay &ray, const bool isTermination) const;
|
---|
[1291] | 847 | /** Evaluates contribution of kd leaf when encountering a min event
|
---|
| 848 | */
|
---|
[2237] | 849 | inline float EvalMinEventContribution(KdLeaf *leaf) const;
|
---|
[1291] | 850 | /** Evaluates contribution of kd leaf when encountering a max event
|
---|
| 851 | */
|
---|
[2237] | 852 | inline float EvalMaxEventContribution(KdLeaf *leaf) const;
|
---|
[1237] | 853 | /** Prepares objects for the heuristics.
|
---|
[1291] | 854 | @returns pvs size as seen by the rays.
|
---|
[1237] | 855 | */
|
---|
[1765] | 856 | float PrepareHeuristics(const RayInfoContainer &rays);
|
---|
[1291] | 857 | /** Prepare a single ray for heuristics.
|
---|
| 858 | */
|
---|
[1765] | 859 | float PrepareHeuristics(const VssRay &ray, const bool isTermination);
|
---|
[1291] | 860 | /** Prepare a single kd leaf for heuristics.
|
---|
| 861 | */
|
---|
[1765] | 862 | float PrepareHeuristics(KdLeaf *leaf);
|
---|
[2539] | 863 | /** Evaluates minimal and maximal depth in the tree.
|
---|
| 864 | */
|
---|
[2170] | 865 | void EvalMinMaxDepth(int &minDepth, int &maxDepth);
|
---|
[2539] | 866 | /** Writes the node to disk
|
---|
| 867 | @note: should be implemented as visitor.
|
---|
| 868 | */
|
---|
| 869 | void ExportNode(VspNode *node, OUT_STREAM &stream);
|
---|
| 870 |
|
---|
[2170] | 871 |
|
---|
[2539] | 872 | ////////////////
|
---|
[1291] | 873 |
|
---|
[2539] | 874 | void ExportBinInterior(OUT_STREAM &stream, VspInterior *interior);
|
---|
| 875 | void ExportBinLeaf(OUT_STREAM &stream, VspLeaf *leaf);
|
---|
[1291] | 876 |
|
---|
[2539] | 877 | VspInterior *ImportBinInterior(IN_STREAM &stream, VspInterior *parent);
|
---|
[2544] | 878 | VspLeaf *ImportBinLeaf(IN_STREAM &stream, VspInterior *parent);
|
---|
[2288] | 879 |
|
---|
[2539] | 880 | VspNode *ImportNextNode(IN_STREAM &stream,
|
---|
[2544] | 881 | VspInterior *parent);
|
---|
[2288] | 882 |
|
---|
[2539] | 883 |
|
---|
| 884 | //////////////////////////////////////////////////
|
---|
| 885 |
|
---|
[1237] | 886 | /** Subdivides the rays into front and back rays according to the split plane.
|
---|
| 887 | @param plane the split plane
|
---|
| 888 | @param rays contains the rays to be split. The rays are
|
---|
| 889 | distributed into front and back rays.
|
---|
| 890 | @param frontRays returns rays on the front side of the plane
|
---|
| 891 | @param backRays returns rays on the back side of the plane
|
---|
| 892 |
|
---|
| 893 | @returns the number of splits
|
---|
| 894 | */
|
---|
| 895 | int SplitRays(const AxisAlignedPlane &plane,
|
---|
| 896 | RayInfoContainer &rays,
|
---|
| 897 | RayInfoContainer &frontRays,
|
---|
| 898 | RayInfoContainer &backRays) const;
|
---|
[2539] | 899 | /** Add contributions of this ray to fron, back, and overall pvs of the parent.
|
---|
| 900 | */
|
---|
[2288] | 901 | inline void UpdatePvsEntriesContribution(const VssRay &ray,
|
---|
| 902 | const bool isTermination,
|
---|
| 903 | const int cf,
|
---|
| 904 | float &frontPvs,
|
---|
| 905 | float &backPvs,
|
---|
| 906 | float &totalPvs) const;
|
---|
[1237] | 907 | /** Classfifies the object with respect to the
|
---|
| 908 | pvs of the front and back leaf and updates pvs size
|
---|
| 909 | accordingly.
|
---|
| 910 |
|
---|
| 911 | @param obj the object to be added
|
---|
| 912 | @param cf the ray classification regarding the split plane
|
---|
| 913 | @param frontPvs returns the PVS of the front partition
|
---|
| 914 | @param backPvs returns the PVS of the back partition
|
---|
| 915 |
|
---|
| 916 | */
|
---|
[2288] | 917 | inline void UpdateContributionsToPvs(const VssRay &ray,
|
---|
| 918 | const bool isTermination,
|
---|
| 919 | const int cf,
|
---|
| 920 | float &frontPvs,
|
---|
| 921 | float &backPvs,
|
---|
| 922 | float &totalPvs) const;
|
---|
[1291] | 923 | /** Evaluates the contribution for objects.
|
---|
| 924 | */
|
---|
[2288] | 925 | inline void UpdateContributionsToPvs(Intersectable *obj,
|
---|
| 926 | const int cf,
|
---|
| 927 | float &frontPvs,
|
---|
| 928 | float &backPvs,
|
---|
| 929 | float &totalPvs) const;
|
---|
[1291] | 930 | /** Evaluates the contribution for bounding volume leaves.
|
---|
| 931 | */
|
---|
[2288] | 932 | inline void UpdateContributionsToPvs(BvhLeaf *leaf,
|
---|
| 933 | const int cf,
|
---|
| 934 | float &frontPvs,
|
---|
| 935 | float &backPvs,
|
---|
| 936 | float &totalPvs,
|
---|
| 937 | const bool countEntries) const;
|
---|
[2539] | 938 | /** Updates contribution to pvs caused by this bvh leaf.
|
---|
| 939 | */
|
---|
[2237] | 940 | inline void UpdateContributionsToPvs(BvhLeaf *leaf,
|
---|
| 941 | const int cf,
|
---|
[2347] | 942 | SplitData &sdata) const;
|
---|
[1291] | 943 | /** Evaluates the contribution for kd leaves.
|
---|
[1237] | 944 | */
|
---|
[2288] | 945 | inline void UpdateContributionsToPvs(KdLeaf *leaf,
|
---|
| 946 | const int cf,
|
---|
| 947 | float &frontPvs,
|
---|
| 948 | float &backPvs,
|
---|
| 949 | float &totalPvs) const;
|
---|
[1237] | 950 | /** Returns true if tree can be terminated.
|
---|
| 951 | */
|
---|
[2224] | 952 | inline bool LocalTerminationCriteriaMet(const VspTraversalData &data) const;
|
---|
[1237] | 953 | /** Returns true if global tree can be terminated.
|
---|
| 954 | */
|
---|
[2228] | 955 | bool GlobalTerminationCriteriaMet(const VspTraversalData &data) const;
|
---|
[1237] | 956 | /** Adds ray sample contributions to the PVS.
|
---|
| 957 | @param sampleContributions the number contributions of the samples
|
---|
| 958 | @param contributingSampels the number of contributing rays
|
---|
| 959 |
|
---|
| 960 | */
|
---|
| 961 | void AddSamplesToPvs(VspLeaf *leaf,
|
---|
| 962 | const RayInfoContainer &rays,
|
---|
| 963 | float &sampleContributions,
|
---|
| 964 | int &contributingSamples);
|
---|
| 965 | /** Propagates valid flag up the tree.
|
---|
| 966 | */
|
---|
| 967 | void PropagateUpValidity(VspNode *node);
|
---|
| 968 | /** Returns estimated memory usage of tree.
|
---|
| 969 | */
|
---|
| 970 | float GetMemUsage() const;
|
---|
| 971 | /** Updates view cell pvs of objects.
|
---|
| 972 | */
|
---|
| 973 | void ProcessViewCellObjects(ViewCell *parent,
|
---|
[1633] | 974 | ViewCell *front,
|
---|
| 975 | ViewCell *back) const;
|
---|
[2539] | 976 | /** Creates a new view cell from the traversal data.
|
---|
| 977 | */
|
---|
[2332] | 978 | void CreateViewCell(VspTraversalData &tData,
|
---|
| 979 | const bool updatePvs,
|
---|
| 980 | const float renderCost,
|
---|
| 981 | const int pvs);
|
---|
[1237] | 982 | /** Collect split candidates which are affected by the last split
|
---|
| 983 | and must be reevaluated.
|
---|
| 984 | */
|
---|
[1633] | 985 | void CollectDirtyCandidates(VspSubdivisionCandidate *sc,
|
---|
| 986 | vector<SubdivisionCandidate *> &dirtyList,
|
---|
| 987 | const bool onlyUnmailed);
|
---|
[2539] | 988 | /** Adds the split candidate associated with this ray to the dirty list.
|
---|
| 989 | */
|
---|
| 990 | void AddCandidateToDirtyList(const VssRay &ray,
|
---|
[1633] | 991 | const bool isTermination,
|
---|
| 992 | vector<SubdivisionCandidate *> &dirtyList,
|
---|
| 993 | const bool onlyUnmailed) const;
|
---|
[1237] | 994 | /** Rays will be clipped to the bounding box.
|
---|
| 995 | */
|
---|
| 996 | void PreprocessRays(const VssRayContainer &sampleRays, RayInfoContainer &rays);
|
---|
| 997 | /** Evaluate subdivision statistics.
|
---|
| 998 | */
|
---|
| 999 | void EvalSubdivisionStats(const SubdivisionCandidate &tData);
|
---|
[2539] | 1000 | /** Prepares the construction of the vsp tree.
|
---|
| 1001 | */
|
---|
[1779] | 1002 | void PrepareConstruction(SplitQueue &tQueue,
|
---|
| 1003 | const VssRayContainer &sampleRays,
|
---|
| 1004 | RayInfoContainer &rays);
|
---|
[1291] | 1005 | /** Evaluates pvs contribution of this ray.
|
---|
[1259] | 1006 | */
|
---|
[2237] | 1007 | float EvalContributionToPvs(const VssRay &ray, const bool isTermination) const;
|
---|
[1291] | 1008 | /** Evaluates pvs contribution of a kd node.
|
---|
| 1009 | */
|
---|
[2237] | 1010 | float EvalContributionToPvs(KdLeaf *leaf) const;
|
---|
[1640] | 1011 | /** Creates new root of hierarchy and computes bounding box.
|
---|
| 1012 | Has to be called before the preparation of the subdivision.
|
---|
| 1013 | */
|
---|
| 1014 | void Initialise(const VssRayContainer &rays,
|
---|
[2332] | 1015 | AxisAlignedBox3 *forcedBoundingBox,
|
---|
| 1016 | const ObjectContainer &objects);
|
---|
[1291] | 1017 |
|
---|
[1845] | 1018 | int CompressObjects();
|
---|
[1844] | 1019 |
|
---|
[1845] | 1020 | int CompressObjects(VspLeaf *leaf);
|
---|
[1844] | 1021 |
|
---|
[2353] | 1022 | int TraverseRayPacket(RayPacket &rp, const bool useMailboxing);
|
---|
[2288] | 1023 |
|
---|
[2543] | 1024 |
|
---|
[2342] | 1025 | #ifdef USE_SSE
|
---|
[2332] | 1026 | struct PacketTraversalData
|
---|
| 1027 | {
|
---|
| 1028 | PacketTraversalData () {}
|
---|
[2353] | 1029 |
|
---|
[2332] | 1030 | PacketTraversalData (VspNode *n,
|
---|
[2353] | 1031 | const __m128 &px, const __m128 &py, const __m128 &pz,
|
---|
| 1032 | const __m128 &maxt,
|
---|
| 1033 | const __m128 &mask):
|
---|
[2332] | 1034 | mNode(n),
|
---|
[2353] | 1035 | mExitPointX4(px), mExitPointY4(py), mExitPointZ4(pz),
|
---|
| 1036 | mMaxT4(maxt),
|
---|
| 1037 | mMask4(mask)
|
---|
[2332] | 1038 | {}
|
---|
[2353] | 1039 |
|
---|
| 1040 | VspNode *mNode;
|
---|
| 1041 |
|
---|
| 1042 | union { __m128 mExitPointX4; float mExitPointX[4]; };
|
---|
| 1043 | union { __m128 mExitPointY4; float mExitPointY[4]; };
|
---|
| 1044 | union { __m128 mExitPointZ4; float mExitPointZ[4]; };
|
---|
| 1045 |
|
---|
| 1046 | union { __m128 mMaxT4; float mMaxT[4]; };
|
---|
| 1047 | union { __m128 mMask4; float mMask[4]; };
|
---|
[2332] | 1048 | };
|
---|
| 1049 |
|
---|
| 1050 |
|
---|
[2353] | 1051 | struct __declspec(align(16)) PacketTraversalStack
|
---|
| 1052 | {
|
---|
| 1053 | // for performance
|
---|
| 1054 | friend class VspTree;
|
---|
[2342] | 1055 |
|
---|
[2353] | 1056 | public:
|
---|
| 1057 | PacketTraversalStack(const int depth):
|
---|
| 1058 | mStackPtr(0),
|
---|
| 1059 | //mDepth(depth)
|
---|
| 1060 | mDepth(1000)
|
---|
| 1061 | {
|
---|
| 1062 | //mData = new PacketTraversalData[mDepth];
|
---|
| 1063 | }
|
---|
| 1064 |
|
---|
| 1065 | ~PacketTraversalStack()
|
---|
| 1066 | {
|
---|
| 1067 | //delete [] mData;
|
---|
| 1068 | }
|
---|
| 1069 |
|
---|
| 1070 | //PacketTraversalStack() {}
|
---|
| 1071 |
|
---|
| 1072 | void Pop() {-- mStackPtr;}
|
---|
| 1073 | const PacketTraversalData &Top() const { return mData[mStackPtr - 1];}
|
---|
| 1074 |
|
---|
| 1075 | void Push(const PacketTraversalData &tData)
|
---|
| 1076 | {
|
---|
| 1077 | // note: faster with direct assignments
|
---|
| 1078 | mData[mStackPtr ++] = tData;
|
---|
| 1079 | //if (mStackPtr == mDepth) std::cerr << "this can never happen!!" << std::endl;
|
---|
| 1080 | }
|
---|
| 1081 |
|
---|
| 1082 | bool Empty() const {return mStackPtr <= 0;}
|
---|
| 1083 |
|
---|
| 1084 | protected:
|
---|
| 1085 |
|
---|
| 1086 | // static array to avoid using operator new
|
---|
| 1087 | //PacketTraversalData *mData;
|
---|
| 1088 | PacketTraversalData mData[1000];
|
---|
| 1089 |
|
---|
| 1090 | int mStackPtr;
|
---|
| 1091 | int mDepth;
|
---|
| 1092 | };
|
---|
| 1093 |
|
---|
[2288] | 1094 | #endif
|
---|
[2547] | 1095 |
|
---|
[2288] | 1096 |
|
---|
[1237] | 1097 | protected:
|
---|
| 1098 |
|
---|
| 1099 | /// pointer to the hierarchy of view cells
|
---|
| 1100 | ViewCellsTree *mViewCellsTree;
|
---|
[2539] | 1101 | /// Pointer to the hierarchy manager which is responsible for both
|
---|
| 1102 | /// view and object space
|
---|
[1259] | 1103 | HierarchyManager *mHierarchyManager;
|
---|
[2539] | 1104 | /// Pointer to the bv hierarchy
|
---|
[2210] | 1105 | BvHierarchy *mBvHierarchy;
|
---|
[2539] | 1106 | /// Pointer to the view cells manager
|
---|
[1237] | 1107 | ViewCellsManager *mViewCellsManager;
|
---|
[2539] | 1108 | /// Buffer for subdivision candidates used for split heuristics evaluation
|
---|
[1237] | 1109 | vector<SortableEntry> *mLocalSubdivisionCandidates;
|
---|
| 1110 | /// Pointer to the root of the tree
|
---|
| 1111 | VspNode *mRoot;
|
---|
[2539] | 1112 | /// The statistics
|
---|
[1237] | 1113 | VspTreeStatistics mVspStats;
|
---|
| 1114 | /// View cell corresponding to the space outside the valid view space
|
---|
| 1115 | VspViewCell *mOutOfBoundsCell;
|
---|
| 1116 | /// box around the whole view domain
|
---|
| 1117 | AxisAlignedBox3 mBoundingBox;
|
---|
| 1118 |
|
---|
| 1119 |
|
---|
[2288] | 1120 | /////////
|
---|
[1237] | 1121 | //-- local termination
|
---|
| 1122 |
|
---|
| 1123 | /// minimal number of rays before subdivision termination
|
---|
| 1124 | int mTermMinRays;
|
---|
| 1125 | /// maximal possible depth
|
---|
| 1126 | int mTermMaxDepth;
|
---|
| 1127 | /// mininum probability
|
---|
| 1128 | float mTermMinProbability;
|
---|
| 1129 | /// mininum PVS
|
---|
| 1130 | int mTermMinPvs;
|
---|
| 1131 | /// maximal contribution per ray
|
---|
| 1132 | float mTermMaxRayContribution;
|
---|
| 1133 | /// maximal acceptable cost ratio
|
---|
| 1134 | float mTermMaxCostRatio;
|
---|
| 1135 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
| 1136 | int mTermMissTolerance;
|
---|
| 1137 |
|
---|
[2288] | 1138 | ////////////
|
---|
| 1139 | //-- global criteria
|
---|
[1237] | 1140 |
|
---|
| 1141 | float mTermMinGlobalCostRatio;
|
---|
| 1142 | int mTermGlobalCostMissTolerance;
|
---|
[1449] | 1143 |
|
---|
[1237] | 1144 |
|
---|
| 1145 | /// maximal number of view cells
|
---|
| 1146 | int mMaxViewCells;
|
---|
| 1147 | /// maximal tree memory
|
---|
| 1148 | float mMaxMemory;
|
---|
| 1149 | /// the tree is out of memory
|
---|
| 1150 | bool mOutOfMemory;
|
---|
| 1151 |
|
---|
[1673] | 1152 | ////////////
|
---|
[1237] | 1153 | //-- split heuristics based parameters
|
---|
| 1154 |
|
---|
| 1155 | bool mUseCostHeuristics;
|
---|
| 1156 | /// balancing factor for PVS criterium
|
---|
| 1157 | float mCtDivCi;
|
---|
| 1158 | /// if only driving axis should be used for split
|
---|
| 1159 | bool mOnlyDrivingAxis;
|
---|
| 1160 | /// if random split axis should be used
|
---|
| 1161 | bool mUseRandomAxis;
|
---|
| 1162 | /// if vsp bsp tree should simulate octree
|
---|
| 1163 | bool mCirculatingAxis;
|
---|
| 1164 | /// minimal relative position where the split axis can be placed
|
---|
| 1165 | float mMinBand;
|
---|
| 1166 | /// maximal relative position where the split axis can be placed
|
---|
| 1167 | float mMaxBand;
|
---|
| 1168 |
|
---|
| 1169 |
|
---|
| 1170 | /// current time stamp (used for keeping split history)
|
---|
| 1171 | int mTimeStamp;
|
---|
| 1172 | // if rays should be stored in leaves
|
---|
| 1173 | bool mStoreRays;
|
---|
| 1174 | /// epsilon for geometric comparisons
|
---|
| 1175 | float mEpsilon;
|
---|
| 1176 | /// subdivision stats output file
|
---|
[2176] | 1177 | std::ofstream mSubdivisionStats;
|
---|
[1237] | 1178 | /// keeps track of cost during subdivision
|
---|
| 1179 | float mTotalCost;
|
---|
[2539] | 1180 | /// keeps track of #pvs entries during subdivison
|
---|
[1662] | 1181 | int mPvsEntries;
|
---|
[1237] | 1182 | /// keeps track of overall pvs size during subdivision
|
---|
| 1183 | int mTotalPvsSize;
|
---|
| 1184 | /// number of currenly generated view cells
|
---|
| 1185 | int mCreatedViewCells;
|
---|
| 1186 |
|
---|
| 1187 | /// weight between render cost decrease and node render cost
|
---|
| 1188 | float mRenderCostDecreaseWeight;
|
---|
[2539] | 1189 | /// maximal #of rays used for heuristics evalution
|
---|
[1237] | 1190 | int mMaxTests;
|
---|
[1732] | 1191 | /// constant value for driving the heuristics
|
---|
| 1192 | float mMemoryConst;
|
---|
[1237] | 1193 | };
|
---|
| 1194 |
|
---|
| 1195 |
|
---|
| 1196 | }
|
---|
| 1197 |
|
---|
| 1198 | #endif
|
---|