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