[408] | 1 | // ================================================================
|
---|
| 2 | // $Id$
|
---|
| 3 | // ****************************************************************
|
---|
| 4 | //
|
---|
| 5 | // Initial coding by
|
---|
| 6 | /**
|
---|
| 7 | @author Jiri Bittner
|
---|
| 8 | */
|
---|
[405] | 9 |
|
---|
[408] | 10 | #ifndef __VSPKDTREE_H__
|
---|
| 11 | #define __VSPKDTREE_H__
|
---|
| 12 |
|
---|
| 13 | // Standard headers
|
---|
| 14 | #include <iomanip>
|
---|
| 15 | #include <vector>
|
---|
[405] | 16 | #include <functional>
|
---|
[408] | 17 | #include <stack>
|
---|
[405] | 18 |
|
---|
[408] | 19 |
|
---|
| 20 | // User headers
|
---|
| 21 | #include "VssRay.h"
|
---|
[405] | 22 | #include "AxisAlignedBox3.h"
|
---|
| 23 |
|
---|
| 24 |
|
---|
[408] | 25 | #define USE_KDNODE_VECTORS 1
|
---|
| 26 | #define _RSS_STATISTICS
|
---|
| 27 | #define _RSS_TRAVERSAL_STATISTICS
|
---|
[405] | 28 |
|
---|
[408] | 29 |
|
---|
| 30 | #include "Statistics.h"
|
---|
| 31 | #include "Ray.h"
|
---|
| 32 |
|
---|
[420] | 33 | #include "RayInfo.h"
|
---|
[428] | 34 | #include "Containers.h"
|
---|
[469] | 35 | #include "ViewCell.h"
|
---|
[420] | 36 |
|
---|
[1112] | 37 |
|
---|
| 38 | namespace GtpVisibilityPreprocessor {
|
---|
| 39 |
|
---|
[462] | 40 | class VspKdLeaf;
|
---|
| 41 | class ViewCellsManager;
|
---|
| 42 | class ViewCellsStatistics;
|
---|
[452] | 43 |
|
---|
[420] | 44 | /**
|
---|
[452] | 45 | Candidate for leaf merging based on priority.
|
---|
| 46 | */
|
---|
[580] | 47 | class VspKdMergeCandidate
|
---|
[452] | 48 | {
|
---|
| 49 | public:
|
---|
| 50 |
|
---|
[580] | 51 | VspKdMergeCandidate(VspKdLeaf *l1, VspKdLeaf *l2);
|
---|
[452] | 52 |
|
---|
| 53 | /** Computes PVS difference between the leaves.
|
---|
| 54 | */
|
---|
[453] | 55 | //int ComputePvsDifference() const;
|
---|
[452] | 56 |
|
---|
[462] | 57 | /** If this merge pair is still valid.
|
---|
[452] | 58 | */
|
---|
[462] | 59 | bool Valid() const;
|
---|
[452] | 60 |
|
---|
[462] | 61 | /** Sets this merge candidate to be valid.
|
---|
| 62 | */
|
---|
| 63 | void SetValid();
|
---|
| 64 |
|
---|
[580] | 65 | friend bool operator<(const VspKdMergeCandidate &leafa, const VspKdMergeCandidate &leafb)
|
---|
[452] | 66 | {
|
---|
[462] | 67 | return leafb.GetMergeCost() < leafa.GetMergeCost();
|
---|
[452] | 68 | }
|
---|
[462] | 69 |
|
---|
| 70 | void SetLeaf1(VspKdLeaf *l);
|
---|
| 71 | void SetLeaf2(VspKdLeaf *l);
|
---|
| 72 |
|
---|
| 73 | VspKdLeaf *GetLeaf1();
|
---|
| 74 | VspKdLeaf *GetLeaf2();
|
---|
| 75 |
|
---|
| 76 | /** Merge cost of this candidate pair.
|
---|
| 77 | */
|
---|
| 78 | float GetMergeCost() const;
|
---|
| 79 |
|
---|
[472] | 80 | /** Returns cost of leaf 1.
|
---|
| 81 | */
|
---|
| 82 | float GetLeaf1Cost() const;
|
---|
| 83 | /** Returns cost of leaf 2.
|
---|
| 84 | */
|
---|
| 85 | float GetLeaf2Cost() const;
|
---|
| 86 |
|
---|
[462] | 87 | /// maximal pvs size
|
---|
| 88 | static int sMaxPvsSize;
|
---|
[472] | 89 | /// overall cost used to normalize cost ratio
|
---|
| 90 | static float sOverallCost;
|
---|
[462] | 91 |
|
---|
| 92 | protected:
|
---|
| 93 |
|
---|
| 94 | /** Evaluates the merge costs of the leaves.
|
---|
| 95 | */
|
---|
| 96 | void EvalMergeCost();
|
---|
| 97 |
|
---|
| 98 | int mLeaf1Id;
|
---|
| 99 | int mLeaf2Id;
|
---|
| 100 |
|
---|
| 101 | float mMergeCost;
|
---|
| 102 |
|
---|
| 103 | VspKdLeaf *mLeaf1;
|
---|
| 104 | VspKdLeaf *mLeaf2;
|
---|
[452] | 105 | };
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | /**
|
---|
[420] | 110 | Static statistics for vsp kd-tree search
|
---|
| 111 | */
|
---|
[408] | 112 | class VspKdStatistics: public StatisticsBase
|
---|
[405] | 113 | {
|
---|
| 114 | public:
|
---|
[420] | 115 | // total number of nodes
|
---|
| 116 | int nodes;
|
---|
| 117 | // number of splits along each of the axes
|
---|
[454] | 118 | int splits[3];
|
---|
[420] | 119 | // totals number of rays
|
---|
| 120 | int rays;
|
---|
[408] | 121 | // initial size of the pvs
|
---|
[420] | 122 | int initialPvsSize;
|
---|
[408] | 123 | // total number of query domains
|
---|
[420] | 124 | int queryDomains;
|
---|
| 125 | // total number of ray references
|
---|
| 126 | int rayRefs;
|
---|
[408] | 127 |
|
---|
| 128 | // max depth nodes
|
---|
[420] | 129 | int maxDepthNodes;
|
---|
| 130 | // max depth nodes
|
---|
| 131 | int minPvsNodes;
|
---|
[426] | 132 | // nodes with minimum PVS
|
---|
[420] | 133 | int minRaysNodes;
|
---|
[408] | 134 | // max ray contribution nodes
|
---|
[420] | 135 | int maxRayContribNodes;
|
---|
[408] | 136 | // max depth nodes
|
---|
[420] | 137 | int minSizeNodes;
|
---|
[408] | 138 |
|
---|
[420] | 139 | // max number of rays per node
|
---|
| 140 | int maxRayRefs;
|
---|
[426] | 141 | // maximal PVS size / leaf
|
---|
| 142 | int maxPvsSize;
|
---|
| 143 |
|
---|
[482] | 144 | // max cost ratio
|
---|
| 145 | int maxCostNodes;
|
---|
| 146 |
|
---|
[420] | 147 | // number of dynamically added ray refs
|
---|
| 148 | int addedRayRefs;
|
---|
| 149 | // number of dynamically removed ray refs
|
---|
| 150 | int removedRayRefs;
|
---|
[405] | 151 |
|
---|
[482] | 152 | /// for average pvs
|
---|
| 153 | int accPvsSize;
|
---|
| 154 |
|
---|
[420] | 155 | /** Default constructor.
|
---|
| 156 | */
|
---|
| 157 | VspKdStatistics() { Reset(); }
|
---|
| 158 | int Nodes() const { return nodes; }
|
---|
| 159 | int Interior() const { return nodes / 2; }
|
---|
| 160 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
[405] | 161 |
|
---|
[420] | 162 | void Reset()
|
---|
| 163 | {
|
---|
| 164 | nodes = 0;
|
---|
[405] | 165 |
|
---|
[454] | 166 | for (int i=0; i<3; i++)
|
---|
[420] | 167 | splits[i] = 0;
|
---|
[405] | 168 |
|
---|
[420] | 169 | rays = queryDomains = 0;
|
---|
| 170 | rayRefs = 0;
|
---|
[426] | 171 |
|
---|
[420] | 172 | maxDepthNodes = 0;
|
---|
| 173 | minPvsNodes = 0;
|
---|
[426] | 174 | minRaysNodes = 0;
|
---|
| 175 | maxRayContribNodes = 0;
|
---|
| 176 | minSizeNodes = 0;
|
---|
[482] | 177 | maxCostNodes = 0;
|
---|
[426] | 178 |
|
---|
[420] | 179 | maxRayRefs = 0;
|
---|
| 180 | addedRayRefs = removedRayRefs = 0;
|
---|
[426] | 181 |
|
---|
[420] | 182 | initialPvsSize = 0;
|
---|
[426] | 183 | maxPvsSize = 0;
|
---|
[482] | 184 | accPvsSize = 0;
|
---|
[420] | 185 | }
|
---|
| 186 |
|
---|
| 187 | void Print(ostream &app) const;
|
---|
| 188 | friend ostream &operator<<(ostream &s, const VspKdStatistics &stat)
|
---|
| 189 | {
|
---|
| 190 | stat.Print(s);
|
---|
| 191 | return s;
|
---|
| 192 | }
|
---|
[405] | 193 | };
|
---|
| 194 |
|
---|
[408] | 195 |
|
---|
[462] | 196 | class VspKdInterior;
|
---|
[408] | 197 |
|
---|
| 198 |
|
---|
[420] | 199 | /** Abstract superclass of Vsp-Kd-Tree nodes.
|
---|
| 200 | */
|
---|
[462] | 201 | class VspKdNode
|
---|
[408] | 202 | {
|
---|
[405] | 203 | public:
|
---|
[408] | 204 |
|
---|
[419] | 205 | friend class VspKdTree;
|
---|
[408] | 206 |
|
---|
[500] | 207 | enum {EInterior, EIntermediate, ELeaf};
|
---|
[405] | 208 |
|
---|
[420] | 209 | /** Constructs new interior node from the parent node.
|
---|
| 210 | */
|
---|
[500] | 211 | inline VspKdNode(VspKdNode *p);
|
---|
[405] | 212 |
|
---|
[420] | 213 | /** Destroys this node and the subtree.
|
---|
| 214 | */
|
---|
[462] | 215 | virtual ~VspKdNode();
|
---|
[405] | 216 |
|
---|
[420] | 217 | /** Type of the node (Einterior or ELeaf)
|
---|
| 218 | */
|
---|
| 219 | virtual int Type() const = 0;
|
---|
| 220 |
|
---|
| 221 | /** Returns true if this node is a leaf.
|
---|
| 222 | */
|
---|
| 223 | bool IsLeaf() const;
|
---|
| 224 |
|
---|
| 225 | /** Prints node stats.
|
---|
| 226 | */
|
---|
| 227 | virtual void Print(ostream &s) const = 0;
|
---|
[405] | 228 |
|
---|
[420] | 229 | /** Returns time needed to access this node.
|
---|
[500] | 230 | @NOTE: don't really know how it is used!
|
---|
[420] | 231 | */
|
---|
[500] | 232 | virtual int GetAccessTime();
|
---|
[405] | 233 |
|
---|
[420] | 234 | /** Returns parent node.
|
---|
| 235 | */
|
---|
[500] | 236 | VspKdNode *GetParent() const;
|
---|
[426] | 237 |
|
---|
[420] | 238 | /** Sets parent node.
|
---|
| 239 | */
|
---|
[500] | 240 | void SetParent(VspKdNode *p);
|
---|
[408] | 241 |
|
---|
[420] | 242 | protected:
|
---|
| 243 | /////////////////////////////////
|
---|
| 244 | // The actual data goes here
|
---|
[408] | 245 |
|
---|
[420] | 246 | /// link to the parent
|
---|
[500] | 247 | VspKdNode *mParent;
|
---|
[405] | 248 |
|
---|
[482] | 249 | enum {SPLIT_X = 0, SPLIT_Y, SPLIT_Z};
|
---|
[408] | 250 |
|
---|
[420] | 251 | /// splitting axis
|
---|
| 252 | char mAxis;
|
---|
[408] | 253 |
|
---|
[420] | 254 | /// depth
|
---|
| 255 | unsigned char mDepth;
|
---|
[405] | 256 | };
|
---|
[408] | 257 |
|
---|
| 258 | // --------------------------------------------------------------
|
---|
| 259 | // KD-tree node - interior node
|
---|
| 260 | // --------------------------------------------------------------
|
---|
[462] | 261 | class VspKdInterior: public VspKdNode
|
---|
[408] | 262 | {
|
---|
| 263 | public:
|
---|
[419] | 264 | friend class VspKdTree;
|
---|
| 265 |
|
---|
[420] | 266 | /** Constructs new interior node from parent node.
|
---|
| 267 | */
|
---|
[462] | 268 | VspKdInterior(VspKdInterior *p);
|
---|
[418] | 269 |
|
---|
| 270 | virtual int GetAccessTime();
|
---|
| 271 |
|
---|
[419] | 272 | virtual int Type() const;
|
---|
[405] | 273 |
|
---|
[462] | 274 | virtual ~VspKdInterior();
|
---|
[418] | 275 |
|
---|
| 276 | virtual void Print(ostream &s) const;
|
---|
[408] | 277 |
|
---|
[420] | 278 | /** Returns back child.
|
---|
| 279 | */
|
---|
[1004] | 280 | VspKdNode *GetBack() const;
|
---|
[420] | 281 | /** Returns front child.
|
---|
| 282 | */
|
---|
[1004] | 283 | VspKdNode *GetFront() const;
|
---|
[419] | 284 |
|
---|
[418] | 285 | protected:
|
---|
[408] | 286 |
|
---|
[420] | 287 | /** Sets pointers to back child and front child.
|
---|
| 288 | */
|
---|
[462] | 289 | void SetupChildLinks(VspKdNode *b, VspKdNode *f);
|
---|
[420] | 290 | /** Replaces the pointer to oldChild with a pointer to newChild.
|
---|
| 291 | */
|
---|
[462] | 292 | void ReplaceChildLink(VspKdNode *oldChild, VspKdNode *newChild);
|
---|
[420] | 293 | /** Computes intersection of the ray with the node boundaries.
|
---|
| 294 | */
|
---|
[418] | 295 | int ComputeRayIntersection(const RayInfo &rayData, float &t);
|
---|
[408] | 296 |
|
---|
[418] | 297 | // plane in local modelling coordinates
|
---|
| 298 | float mPosition;
|
---|
[408] | 299 |
|
---|
[418] | 300 | // pointers to children
|
---|
[462] | 301 | VspKdNode *mBack;
|
---|
| 302 | VspKdNode *mFront;
|
---|
[405] | 303 |
|
---|
[418] | 304 | // the bbox of the node
|
---|
[419] | 305 | AxisAlignedBox3 mBox;
|
---|
[418] | 306 |
|
---|
| 307 | // data for caching
|
---|
| 308 | long mAccesses;
|
---|
| 309 | long mLastAccessTime;
|
---|
[408] | 310 | };
|
---|
| 311 |
|
---|
| 312 |
|
---|
[500] | 313 | /**
|
---|
| 314 | Node type just before leaf holding abitrary split plane
|
---|
| 315 | */
|
---|
| 316 | class VspKdIntermediate: public VspKdNode
|
---|
| 317 | {
|
---|
| 318 | public:
|
---|
| 319 | friend class VspKdTree;
|
---|
| 320 |
|
---|
| 321 | /** Constructs new interior node from parent node.
|
---|
| 322 | */
|
---|
| 323 | VspKdIntermediate(VspKdInterior *p);
|
---|
| 324 |
|
---|
| 325 | //virtual int GetAccessTime();
|
---|
| 326 |
|
---|
| 327 | virtual int Type() const;
|
---|
| 328 |
|
---|
| 329 | virtual ~VspKdIntermediate();
|
---|
| 330 |
|
---|
| 331 | virtual void Print(ostream &s) const;
|
---|
| 332 |
|
---|
| 333 | /** Returns back child.
|
---|
| 334 | */
|
---|
| 335 | inline VspKdLeaf *GetBack() const;
|
---|
| 336 | /** Returns front child.
|
---|
| 337 | */
|
---|
| 338 | inline VspKdLeaf *GetFront() const;
|
---|
| 339 |
|
---|
| 340 | protected:
|
---|
| 341 |
|
---|
| 342 | /** Sets pointers to back child and front child.
|
---|
| 343 | */
|
---|
| 344 | void SetupChildLinks(VspKdLeaf *b, VspKdLeaf *f);
|
---|
| 345 | /** Computes intersection of the ray with the node boundaries.
|
---|
| 346 | */
|
---|
| 347 | int ComputeRayIntersection(const RayInfo &rayData, float &t);
|
---|
| 348 |
|
---|
| 349 | // plane in local modelling coordinates
|
---|
| 350 | Plane3 mSplitPlane;
|
---|
| 351 |
|
---|
| 352 | // pointers to children
|
---|
| 353 | VspKdLeaf *mBack;
|
---|
| 354 | VspKdLeaf *mFront;
|
---|
| 355 |
|
---|
| 356 | // the bbox of the node
|
---|
| 357 | AxisAlignedBox3 mBox;
|
---|
| 358 |
|
---|
| 359 | // data for caching
|
---|
| 360 | long mAccesses;
|
---|
| 361 | long mLastAccessTime;
|
---|
| 362 | };
|
---|
| 363 |
|
---|
[408] | 364 | // --------------------------------------------------------------
|
---|
| 365 | // KD-tree node - leaf node
|
---|
| 366 | // --------------------------------------------------------------
|
---|
[462] | 367 | class VspKdLeaf: public VspKdNode
|
---|
[408] | 368 | {
|
---|
| 369 | public:
|
---|
[465] | 370 |
|
---|
[419] | 371 | friend class VspKdTree;
|
---|
| 372 |
|
---|
[420] | 373 | /** Constructs leaf from parent node.
|
---|
| 374 | @param p the parent node
|
---|
| 375 | @param nRays preallocates memory to store this number of rays
|
---|
[472] | 376 | @parma maxMisses how many times the max cost ratio was missed on the path to the leaf
|
---|
[420] | 377 | */
|
---|
[500] | 378 | VspKdLeaf(VspKdNode *p, const int nRays, const int maxCostMisses = 0);
|
---|
[408] | 379 |
|
---|
[462] | 380 | virtual ~VspKdLeaf();
|
---|
[405] | 381 |
|
---|
[420] | 382 | virtual int Type() const;
|
---|
[408] | 383 |
|
---|
[420] | 384 | virtual void Print(ostream &s) const;
|
---|
[405] | 385 |
|
---|
[420] | 386 | /** Adds a ray to the leaf ray container.
|
---|
| 387 | */
|
---|
| 388 | void AddRay(const RayInfo &data);
|
---|
| 389 | /** Returns size of the leaf PVS as induced by the rays
|
---|
| 390 | @note returns precomputed PVS size, which may not be valid
|
---|
| 391 | anymore. Run UpdatePvsSize to get a valid PVS.
|
---|
| 392 | */
|
---|
| 393 | int GetPvsSize() const;
|
---|
[405] | 394 |
|
---|
[420] | 395 | /** If PVS is not valid, this function recomputes the leaf
|
---|
| 396 | PVS as induced by the rays.
|
---|
| 397 | */
|
---|
[410] | 398 | void UpdatePvsSize();
|
---|
[408] | 399 |
|
---|
[428] | 400 | /** Returns stored rays.
|
---|
| 401 | */
|
---|
[426] | 402 | RayInfoContainer &GetRays();
|
---|
| 403 |
|
---|
[428] | 404 | /** Returns rays into this ray container.
|
---|
| 405 | */
|
---|
| 406 | void GetRays(VssRayContainer &rays);
|
---|
[420] | 407 | /** Returns average contribution of a ray to the PVS
|
---|
| 408 | */
|
---|
| 409 | inline float GetAvgRayContribution() const;
|
---|
| 410 | /** Returns square of average contribution of a ray.
|
---|
| 411 | */
|
---|
| 412 | inline float GetSqrRayContribution() const;
|
---|
[408] | 413 |
|
---|
[428] | 414 | /** Extracts PVS from ray set.
|
---|
| 415 | */
|
---|
| 416 | void ExtractPvs(ObjectContainer &objects) const;
|
---|
| 417 |
|
---|
[422] | 418 | //-- mailing options
|
---|
| 419 | void Mail();
|
---|
| 420 |
|
---|
| 421 | bool Mailed() const;
|
---|
| 422 |
|
---|
[462] | 423 | /** Returns true if mail equals the leaf mail box.
|
---|
| 424 | */
|
---|
| 425 | bool Mailed(const int mail) const;
|
---|
[422] | 426 |
|
---|
[462] | 427 | void SetViewCell(VspKdViewCell *viewCell);
|
---|
[420] | 428 |
|
---|
[462] | 429 | /** Returns mail box of this leaf.
|
---|
| 430 | */
|
---|
| 431 | int GetMailbox() const;
|
---|
[420] | 432 |
|
---|
[462] | 433 | /** Returns view cell associated with this leaf.
|
---|
[453] | 434 | */
|
---|
[462] | 435 | VspKdViewCell *GetViewCell();
|
---|
[453] | 436 |
|
---|
[472] | 437 | /** Returns number of times the max cost ratio was missed until
|
---|
| 438 | this leaf.
|
---|
| 439 | */
|
---|
| 440 | int GetMaxCostMisses();
|
---|
| 441 |
|
---|
[462] | 442 | ////////////////////////////////////////////
|
---|
| 443 |
|
---|
| 444 | static void NewMail();
|
---|
| 445 | static int sMailId;
|
---|
| 446 |
|
---|
[495] | 447 | ObjectPvs *mPvs;
|
---|
| 448 | float mVolume;
|
---|
| 449 |
|
---|
[420] | 450 | protected:
|
---|
[426] | 451 |
|
---|
| 452 | /** Manually sets PVS size.
|
---|
| 453 | @param s the PVS size
|
---|
| 454 | */
|
---|
| 455 | void SetPvsSize(const int s);
|
---|
| 456 |
|
---|
[420] | 457 | int mMailbox;
|
---|
| 458 |
|
---|
[472] | 459 | /// rays intersecting this leaf.
|
---|
[420] | 460 | RayInfoContainer mRays;
|
---|
[462] | 461 | /// true if mPvsSize is valid => PVS does not have to be updated
|
---|
[420] | 462 | bool mValidPvs;
|
---|
[462] | 463 | /// the view cell associated with this leaf
|
---|
| 464 | VspKdViewCell *mViewCell;
|
---|
[472] | 465 | /// number of times the max cost ratio was missed on the way to the leaf.
|
---|
| 466 | int mMaxCostMisses;
|
---|
[462] | 467 |
|
---|
[500] | 468 | //private:
|
---|
[462] | 469 | /// stores PVS size so we have to evaluate PVS size only once
|
---|
[419] | 470 | int mPvsSize;
|
---|
[405] | 471 | };
|
---|
| 472 |
|
---|
| 473 |
|
---|
[408] | 474 | // ---------------------------------------------------------------
|
---|
| 475 | // Main LSDS search class
|
---|
| 476 | // ---------------------------------------------------------------
|
---|
| 477 | class VspKdTree
|
---|
| 478 | {
|
---|
[411] | 479 | // --------------------------------------------------------------
|
---|
| 480 | // For sorting rays
|
---|
| 481 | // -------------------------------------------------------------
|
---|
| 482 | struct SortableEntry
|
---|
| 483 | {
|
---|
| 484 | enum EType
|
---|
| 485 | {
|
---|
| 486 | ERayMin,
|
---|
| 487 | ERayMax
|
---|
| 488 | };
|
---|
| 489 |
|
---|
| 490 | int type;
|
---|
| 491 | float value;
|
---|
[482] | 492 | VssRay *ray;
|
---|
[411] | 493 |
|
---|
| 494 | SortableEntry() {}
|
---|
[482] | 495 | SortableEntry(const int t, const float v, VssRay *r): type(t),
|
---|
| 496 | value(v), ray(r)
|
---|
[411] | 497 | {
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 | friend bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
| 501 | {
|
---|
| 502 | return a.value < b.value;
|
---|
| 503 | }
|
---|
| 504 | };
|
---|
| 505 |
|
---|
[410] | 506 | struct TraversalData
|
---|
| 507 | {
|
---|
[462] | 508 | VspKdNode *mNode;
|
---|
[419] | 509 | AxisAlignedBox3 mBox;
|
---|
[500] | 510 | //TODO PolygonContainer *mPolys;
|
---|
| 511 |
|
---|
[419] | 512 | int mDepth;
|
---|
[423] | 513 | //float mPriority;
|
---|
[410] | 514 |
|
---|
| 515 | TraversalData() {}
|
---|
[405] | 516 |
|
---|
[462] | 517 | TraversalData(VspKdNode *n, const float p):
|
---|
[423] | 518 | mNode(n)//, mPriority(p)
|
---|
[410] | 519 | {}
|
---|
[408] | 520 |
|
---|
[462] | 521 | TraversalData(VspKdNode *n, const AxisAlignedBox3 &b, const int d):
|
---|
[419] | 522 | mNode(n), mBox(b), mDepth(d) {}
|
---|
[405] | 523 |
|
---|
[423] | 524 | // comparator for the priority queue
|
---|
| 525 | /*struct less_priority : public binary_function<const TraversalData, const TraversalData, bool>
|
---|
[410] | 526 | {
|
---|
[423] | 527 | bool operator()(const TraversalData a, const TraversalData b) {
|
---|
| 528 | return a.mPriority < b.mPriority; } };*/
|
---|
[405] | 529 |
|
---|
[410] | 530 | // ~TraversalData() {}
|
---|
| 531 | // TraversalData(const TraversalData &s):node(s.node), bbox(s.bbox), depth(s.depth) {}
|
---|
[408] | 532 |
|
---|
[472] | 533 | friend bool operator<(const TraversalData &a, const TraversalData &b)
|
---|
[410] | 534 | {
|
---|
| 535 | // return a.node->queries.size() < b.node->queries.size();
|
---|
[500] | 536 | VspKdLeaf *leafa = dynamic_cast<VspKdLeaf *>(a.mNode);
|
---|
| 537 | VspKdLeaf *leafb = dynamic_cast<VspKdLeaf *>(b.mNode);
|
---|
[408] | 538 | #if 0
|
---|
| 539 | return
|
---|
[474] | 540 | leafa->rays.size() * a.mBox.GetVolume()
|
---|
[408] | 541 | <
|
---|
[474] | 542 | leafb->rays.size() * b.mBox.GetVolume();
|
---|
[408] | 543 | #endif
|
---|
[587] | 544 | #if 0
|
---|
[408] | 545 | return
|
---|
[474] | 546 | leafa->GetPvsSize() * a.mBox.GetVolume()
|
---|
[408] | 547 | <
|
---|
[474] | 548 | leafb->GetPvsSize() * b.mBox.GetVolume();
|
---|
[408] | 549 | #endif
|
---|
| 550 | #if 0
|
---|
| 551 | return
|
---|
| 552 | leafa->GetPvsSize()
|
---|
| 553 | <
|
---|
| 554 | leafb->GetPvsSize();
|
---|
| 555 | #endif
|
---|
| 556 | #if 0
|
---|
| 557 | return
|
---|
[423] | 558 | leafa->GetPvsSize() / (float)(leafa->rays.size() + Limits::Small())
|
---|
[408] | 559 | >
|
---|
[423] | 560 | leafb->GetPvsSize() / (float)(leafb->rays.size() + Limits::Small());
|
---|
[408] | 561 | #endif
|
---|
| 562 | #if 0
|
---|
| 563 | return
|
---|
[410] | 564 | leafa->GetPvsSize() * (float)leafa->rays.size()
|
---|
[408] | 565 | <
|
---|
[410] | 566 | leafb->GetPvsSize() * (float)leafb->rays.size();
|
---|
[408] | 567 | #endif
|
---|
[587] | 568 | #if 1
|
---|
| 569 | return a.mDepth > b.mDepth;
|
---|
| 570 | #endif
|
---|
[468] | 571 | }
|
---|
| 572 | };
|
---|
[405] | 573 |
|
---|
[468] | 574 | /** Simplified data for ray traversal only.
|
---|
| 575 | */
|
---|
| 576 | struct RayTraversalData
|
---|
| 577 | {
|
---|
| 578 | RayInfo mRayData;
|
---|
| 579 | VspKdNode *mNode;
|
---|
| 580 |
|
---|
| 581 | RayTraversalData() {}
|
---|
[410] | 582 |
|
---|
[468] | 583 | RayTraversalData(VspKdNode *n, const RayInfo &data):
|
---|
| 584 | mRayData(data), mNode(n) {}
|
---|
| 585 | };
|
---|
[408] | 586 |
|
---|
[483] | 587 | struct LineTraversalData
|
---|
[468] | 588 | {
|
---|
| 589 | VspKdNode *mNode;
|
---|
| 590 | Vector3 mExitPoint;
|
---|
| 591 |
|
---|
| 592 | float mMaxT;
|
---|
| 593 |
|
---|
[483] | 594 | LineTraversalData () {}
|
---|
[1012] | 595 | LineTraversalData (VspKdNode *n, const Vector3 &p, const float maxt):
|
---|
[468] | 596 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
| 597 | };
|
---|
| 598 |
|
---|
[405] | 599 | public:
|
---|
| 600 |
|
---|
[410] | 601 | VspKdTree();
|
---|
| 602 | virtual ~VspKdTree();
|
---|
[405] | 603 |
|
---|
[440] | 604 | virtual void Construct(const VssRayContainer &rays,
|
---|
[410] | 605 | AxisAlignedBox3 *forcedBoundingBox = NULL);
|
---|
[420] | 606 |
|
---|
| 607 | /** Returns bounding box of the specified node.
|
---|
| 608 | */
|
---|
[462] | 609 | AxisAlignedBox3 GetBBox(VspKdNode *node) const;
|
---|
[420] | 610 |
|
---|
| 611 | const VspKdStatistics &GetStatistics() const;
|
---|
| 612 |
|
---|
| 613 | /** Get the root of the tree.
|
---|
| 614 | */
|
---|
[462] | 615 | VspKdNode *GetRoot() const;
|
---|
[420] | 616 |
|
---|
| 617 | /** Returns average PVS size in tree.
|
---|
| 618 | */
|
---|
| 619 | float GetAvgPvsSize();
|
---|
| 620 |
|
---|
| 621 | /** Returns memory usage in MB.
|
---|
| 622 | */
|
---|
| 623 | float GetMemUsage() const;
|
---|
[485] | 624 | //?
|
---|
[420] | 625 | float GetRayMemUsage() const;
|
---|
| 626 |
|
---|
[428] | 627 | /** Collects leaves of this tree.
|
---|
| 628 | */
|
---|
[462] | 629 | void CollectLeaves(vector<VspKdLeaf *> &leaves) const;
|
---|
[428] | 630 |
|
---|
[485] | 631 | /** Merges view cells created with this tree according to
|
---|
| 632 | some (global) cost heuristics.
|
---|
[452] | 633 | */
|
---|
[485] | 634 | int MergeViewCells(const VssRayContainer &rays);
|
---|
[452] | 635 |
|
---|
| 636 | /** Finds neighbours of this node.
|
---|
| 637 | @param n the input node
|
---|
| 638 | @param neighbours the neighbors of the input node
|
---|
| 639 | @param onlyUnmailed if only unmailed neighbors are collected
|
---|
| 640 | */
|
---|
[462] | 641 | int FindNeighbors(VspKdLeaf *n,
|
---|
| 642 | vector<VspKdLeaf *> &neighbors,
|
---|
[452] | 643 | bool onlyUnmailed);
|
---|
| 644 |
|
---|
[462] | 645 |
|
---|
| 646 | /** Sets pointer to view cells manager.
|
---|
| 647 | */
|
---|
| 648 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
| 649 |
|
---|
[463] | 650 | /** A ray is cast possible intersecting the tree.
|
---|
| 651 | @param the ray that is cast.
|
---|
| 652 | @returns the number of intersections with objects stored in the tree.
|
---|
| 653 | */
|
---|
[468] | 654 | int CastLineSegment(const Vector3 &origin,
|
---|
| 655 | const Vector3 &termination,
|
---|
| 656 | vector<ViewCell *> &viewcells);
|
---|
[462] | 657 |
|
---|
| 658 | /** Collects view cells generated by this tree.
|
---|
| 659 | */
|
---|
| 660 | void CollectViewCells(ViewCellContainer &viewCells) const;
|
---|
[486] | 661 |
|
---|
| 662 | /** Refines view cells using shuffling, i.e., border leaves
|
---|
| 663 | of two view cells are exchanged if the resulting view cells
|
---|
| 664 | are tested to be "better" than the old ones.
|
---|
| 665 | @returns number of refined view cells
|
---|
[473] | 666 | */
|
---|
[486] | 667 | int RefineViewCells(const VssRayContainer &rays);
|
---|
[473] | 668 |
|
---|
[483] | 669 | /** Collects candidates for the merge in the merge queue.
|
---|
| 670 | */
|
---|
| 671 | void CollectMergeCandidates();
|
---|
| 672 |
|
---|
[485] | 673 | /** Collapses the tree with respect to the view cell partition.
|
---|
[501] | 674 | @returns number of collapsed nodes
|
---|
[485] | 675 | */
|
---|
[501] | 676 | int CollapseTree();
|
---|
[485] | 677 |
|
---|
[420] | 678 | protected:
|
---|
[428] | 679 |
|
---|
[501] | 680 | /** Collapses the tree with respect to the view cell partition,
|
---|
| 681 | i.e. leaves having the same view cell are collapsed.
|
---|
| 682 | @param node the root of the subtree to be collapsed
|
---|
| 683 | @param collapsed returns the number of collapsed nodes
|
---|
[495] | 684 | @returns node of type leaf if the node could be collapsed,
|
---|
| 685 | this node otherwise
|
---|
| 686 | */
|
---|
[501] | 687 | VspKdNode *CollapseTree(VspKdNode *node, int &collapsed);
|
---|
| 688 |
|
---|
[420] | 689 | // incremental construction
|
---|
[410] | 690 | virtual void UpdateRays(VssRayContainer &remove, VssRayContainer &add);
|
---|
[405] | 691 |
|
---|
[420] | 692 | virtual void AddRays(VssRayContainer &add);
|
---|
[405] | 693 |
|
---|
[462] | 694 | VspKdNode *Locate(const Vector3 &v);
|
---|
[408] | 695 |
|
---|
[462] | 696 | VspKdNode *SubdivideNode(VspKdLeaf *leaf,
|
---|
[410] | 697 | const AxisAlignedBox3 &box,
|
---|
| 698 | AxisAlignedBox3 &backBox,
|
---|
| 699 | AxisAlignedBox3 &frontBox);
|
---|
[408] | 700 |
|
---|
[462] | 701 | VspKdNode *Subdivide(const TraversalData &tdata);
|
---|
[408] | 702 |
|
---|
[462] | 703 | int SelectPlane(VspKdLeaf *leaf,
|
---|
[410] | 704 | const AxisAlignedBox3 &box,
|
---|
| 705 | float &position,
|
---|
| 706 | int &raysBack,
|
---|
| 707 | int &raysFront,
|
---|
| 708 | int &pvsBack,
|
---|
| 709 | int &pvsFront);
|
---|
[405] | 710 |
|
---|
[462] | 711 | void SortSplitCandidates(VspKdLeaf *node,
|
---|
[420] | 712 | const int axis);
|
---|
[408] | 713 |
|
---|
[462] | 714 | float BestCostRatioHeuristic(VspKdLeaf *node,
|
---|
[480] | 715 | const AxisAlignedBox3 &box,
|
---|
[410] | 716 | int &axis,
|
---|
| 717 | float &position,
|
---|
| 718 | int &raysBack,
|
---|
| 719 | int &raysFront,
|
---|
| 720 | int &pvsBack,
|
---|
| 721 | int &pvsFront);
|
---|
[405] | 722 |
|
---|
[480] | 723 | float BestCostRatioRegular(VspKdLeaf *node,
|
---|
| 724 | const AxisAlignedBox3 &box,
|
---|
[410] | 725 | int &axis,
|
---|
| 726 | float &position,
|
---|
| 727 | int &raysBack,
|
---|
| 728 | int &raysFront,
|
---|
| 729 | int &pvsBack,
|
---|
| 730 | int &pvsFront);
|
---|
[408] | 731 |
|
---|
[462] | 732 | float EvalCostRatio(VspKdLeaf *node,
|
---|
[480] | 733 | const AxisAlignedBox3 &box,
|
---|
[410] | 734 | const int axis,
|
---|
| 735 | const float position,
|
---|
| 736 | int &raysBack,
|
---|
| 737 | int &raysFront,
|
---|
| 738 | int &pvsBack,
|
---|
| 739 | int &pvsFront);
|
---|
[405] | 740 |
|
---|
| 741 |
|
---|
[462] | 742 | VspKdNode * SubdivideLeaf(VspKdLeaf *leaf,
|
---|
[410] | 743 | const float SAThreshold);
|
---|
[405] | 744 |
|
---|
[410] | 745 | void RemoveRay(VssRay *ray,
|
---|
[462] | 746 | vector<VspKdLeaf *> *affectedLeaves,
|
---|
[410] | 747 | const bool removeAllScheduledRays);
|
---|
[405] | 748 |
|
---|
[410] | 749 | void AddRay(VssRay *ray);
|
---|
[408] | 750 |
|
---|
[410] | 751 | void TraverseInternalNode(RayTraversalData &data,
|
---|
| 752 | stack<RayTraversalData> &tstack);
|
---|
[408] | 753 |
|
---|
[410] | 754 | void EvaluateLeafStats(const TraversalData &data);
|
---|
[405] | 755 |
|
---|
| 756 |
|
---|
[418] | 757 | int GetRootPvsSize() const;
|
---|
[408] | 758 |
|
---|
[462] | 759 | int GetPvsSize(VspKdNode *node, const AxisAlignedBox3 &box) const;
|
---|
[405] | 760 |
|
---|
[410] | 761 | void GetRayContributionStatistics(float &minRayContribution,
|
---|
| 762 | float &maxRayContribution,
|
---|
| 763 | float &avgRayContribution);
|
---|
[405] | 764 |
|
---|
[410] | 765 | int GenerateRays(const float ratioPerLeaf,
|
---|
| 766 | SimpleRayContainer &rays);
|
---|
[405] | 767 |
|
---|
[425] | 768 | /** Collapses this subtree and releases the memory.
|
---|
| 769 | @returns number of collapsed rays.
|
---|
| 770 | */
|
---|
[462] | 771 | int CollapseSubtree(VspKdNode *sroot, const int time);
|
---|
[418] | 772 |
|
---|
[419] | 773 | int ReleaseMemory(const int time);
|
---|
| 774 |
|
---|
[462] | 775 | bool TerminationCriteriaMet(const VspKdLeaf *leaf,
|
---|
[421] | 776 | const AxisAlignedBox3 &box) const;
|
---|
| 777 |
|
---|
[425] | 778 | /** Computes PVS size of this node given a global ray set.
|
---|
| 779 | @returns PVS size of the intersection of this node bounding box with the rays.
|
---|
| 780 | */
|
---|
[462] | 781 | int ComputePvsSize(VspKdNode *node,
|
---|
| 782 | const RayInfoContainer &globalRays) const;
|
---|
[425] | 783 |
|
---|
[462] | 784 |
|
---|
| 785 | /** Generates view cell for this leaf taking the ray contributions.
|
---|
| 786 | */
|
---|
| 787 | void GenerateViewCell(VspKdLeaf *leaf);
|
---|
| 788 |
|
---|
| 789 | /** Merges view cells of the two leaves.
|
---|
| 790 | */
|
---|
| 791 | bool MergeViewCells(VspKdLeaf *l1, VspKdLeaf *l2);
|
---|
| 792 |
|
---|
[465] | 793 | /** Helper function revalidating the view cell leaf list after merge.
|
---|
| 794 | */
|
---|
[517] | 795 | void RepairViewCellsLeafLists();
|
---|
[465] | 796 |
|
---|
[486] | 797 | /** Shuffles the leaves, i.e., tests if exchanging
|
---|
| 798 | the view cells the leaves belong to helps in improving the view cells.
|
---|
| 799 | */
|
---|
| 800 | bool ShuffleLeaves(VspKdLeaf *leaf1, VspKdLeaf *leaf2) const;
|
---|
| 801 |
|
---|
| 802 | /** Shuffles, i.e. takes border leaf from view cell 1 and adds it
|
---|
| 803 | to view cell 2.
|
---|
| 804 | */
|
---|
| 805 | void ShuffleLeaf(VspKdLeaf *leaf,
|
---|
| 806 | VspKdViewCell *vc1,
|
---|
| 807 | VspKdViewCell *vc2) const;
|
---|
[418] | 808 | protected:
|
---|
[452] | 809 |
|
---|
| 810 |
|
---|
[418] | 811 | /////////////////////////////
|
---|
| 812 | // The core pointer
|
---|
[462] | 813 | VspKdNode *mRoot;
|
---|
[418] | 814 |
|
---|
| 815 | /////////////////////////////
|
---|
| 816 | // Basic properties
|
---|
| 817 |
|
---|
| 818 | // total number of nodes of the tree
|
---|
[422] | 819 | int mNumNodes;
|
---|
[418] | 820 |
|
---|
| 821 | // axis aligned bounding box of the scene
|
---|
| 822 | AxisAlignedBox3 mBox;
|
---|
| 823 |
|
---|
| 824 | // epsilon used for the construction
|
---|
[422] | 825 | float mEpsilon;
|
---|
[418] | 826 |
|
---|
| 827 | // ratio between traversal and intersection costs
|
---|
[422] | 828 | float mCtDivCi;
|
---|
[418] | 829 |
|
---|
| 830 | // type of the splitting to use for the tree construction
|
---|
[462] | 831 | enum {ESplitRegular, ESplitHeuristic};
|
---|
[418] | 832 | int splitType;
|
---|
| 833 |
|
---|
| 834 | // maximum alovable memory in MB
|
---|
[422] | 835 | float mMaxTotalMemory;
|
---|
[418] | 836 |
|
---|
| 837 | // maximum alovable memory for static kd tree in MB
|
---|
[422] | 838 | float mMaxStaticMemory;
|
---|
[418] | 839 |
|
---|
| 840 | // this is used during the construction depending
|
---|
| 841 | // on the type of the tree and queries...
|
---|
[422] | 842 | float mMaxMemory;
|
---|
[418] | 843 |
|
---|
| 844 | // minimal acess time for collapse
|
---|
[422] | 845 | int mAccessTimeThreshold;
|
---|
[418] | 846 |
|
---|
| 847 | // minimal depth at which to perform collapse
|
---|
[422] | 848 | int mMinCollapseDepth;
|
---|
[418] | 849 |
|
---|
| 850 | // reusable array of split candidates
|
---|
[422] | 851 | vector<SortableEntry> *mSplitCandidates;
|
---|
[418] | 852 |
|
---|
[462] | 853 | ViewCellsManager *mViewCellsManager;
|
---|
| 854 |
|
---|
[580] | 855 | priority_queue<VspKdMergeCandidate> mMergeQueue;
|
---|
[483] | 856 |
|
---|
| 857 |
|
---|
[418] | 858 | /////////////////////////////
|
---|
[421] | 859 | // Construction parameters
|
---|
| 860 |
|
---|
| 861 | /// max depth of the tree
|
---|
| 862 | int mTermMaxDepth;
|
---|
| 863 | /// minimal ratio of the volume of the cell and the query volume
|
---|
| 864 | float mTermMinSize;
|
---|
| 865 | /// minimal pvs per node to still get subdivided
|
---|
| 866 | int mTermMinPvs;
|
---|
| 867 | /// minimal ray number per node to still get subdivided
|
---|
| 868 | int mTermMinRays;
|
---|
[472] | 869 | /// maximal acceptable cost ratio to continue subdivision
|
---|
[421] | 870 | float mTermMaxCostRatio;
|
---|
| 871 | /// maximal contribution per ray to subdivide the node
|
---|
| 872 | float mTermMaxRayContribution;
|
---|
[472] | 873 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
| 874 | int mTermMissTolerance;
|
---|
[421] | 875 |
|
---|
[462] | 876 | /// maximal numbers of view cells
|
---|
| 877 | int mMaxViewCells;
|
---|
[452] | 878 |
|
---|
[472] | 879 | /// maximal cost ratio of a merge
|
---|
| 880 | float mMergeMaxCostRatio;
|
---|
| 881 |
|
---|
[465] | 882 | /// minimal number of view cells
|
---|
[472] | 883 | int mMergeMinViewCells;
|
---|
[465] | 884 |
|
---|
[472] | 885 | /// if only the "driving axis", i.e., the axis with the biggest extent
|
---|
| 886 | /// should be used for subdivision
|
---|
| 887 | bool mOnlyDrivingAxis;
|
---|
| 888 |
|
---|
[421] | 889 | /////////////////////////////
|
---|
[418] | 890 | VspKdStatistics mStat;
|
---|
[405] | 891 | };
|
---|
| 892 |
|
---|
[860] | 893 | }
|
---|
[408] | 894 |
|
---|
[462] | 895 | #endif // __VSP_KDTREE_H__
|
---|
[408] | 896 |
|
---|