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