[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"
|
---|
[420] | 35 |
|
---|
[452] | 36 | class VspKdTreeLeaf;
|
---|
| 37 |
|
---|
[420] | 38 | /**
|
---|
[452] | 39 | Candidate for leaf merging based on priority.
|
---|
| 40 | */
|
---|
| 41 | class MergeCandidate
|
---|
| 42 | {
|
---|
| 43 | public:
|
---|
| 44 | VspKdTreeLeaf *mLeaf1;
|
---|
| 45 | VspKdTreeLeaf *mLeaf2;
|
---|
| 46 |
|
---|
| 47 | MergeCandidate(VspKdTreeLeaf *l1, VspKdTreeLeaf *l2);
|
---|
| 48 |
|
---|
| 49 | /** Computes PVS difference between the leaves.
|
---|
| 50 | */
|
---|
[453] | 51 | //int ComputePvsDifference() const;
|
---|
[452] | 52 |
|
---|
| 53 | /** Evaluates the merge costs of this leaf.
|
---|
| 54 | */
|
---|
| 55 | float EvaluateMergeCost() const;
|
---|
| 56 |
|
---|
| 57 | friend bool operator<(const MergeCandidate &leafa, const MergeCandidate &leafb)
|
---|
| 58 | {
|
---|
[453] | 59 | return leafa.EvaluateMergeCost() < leafb.EvaluateMergeCost();
|
---|
[452] | 60 | }
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
[420] | 66 | Static statistics for vsp kd-tree search
|
---|
| 67 | */
|
---|
[408] | 68 | class VspKdStatistics: public StatisticsBase
|
---|
[405] | 69 | {
|
---|
| 70 | public:
|
---|
[420] | 71 | // total number of nodes
|
---|
| 72 | int nodes;
|
---|
| 73 | // number of splits along each of the axes
|
---|
[454] | 74 | int splits[3];
|
---|
[420] | 75 | // totals number of rays
|
---|
| 76 | int rays;
|
---|
[408] | 77 | // initial size of the pvs
|
---|
[420] | 78 | int initialPvsSize;
|
---|
[408] | 79 | // total number of query domains
|
---|
[420] | 80 | int queryDomains;
|
---|
| 81 | // total number of ray references
|
---|
| 82 | int rayRefs;
|
---|
[408] | 83 |
|
---|
| 84 | // max depth nodes
|
---|
[420] | 85 | int maxDepthNodes;
|
---|
| 86 | // max depth nodes
|
---|
| 87 | int minPvsNodes;
|
---|
[426] | 88 | // nodes with minimum PVS
|
---|
[420] | 89 | int minRaysNodes;
|
---|
[408] | 90 | // max ray contribution nodes
|
---|
[420] | 91 | int maxRayContribNodes;
|
---|
[408] | 92 | // max depth nodes
|
---|
[420] | 93 | int minSizeNodes;
|
---|
[408] | 94 |
|
---|
[420] | 95 | // max number of rays per node
|
---|
| 96 | int maxRayRefs;
|
---|
[426] | 97 | // maximal PVS size / leaf
|
---|
| 98 | int maxPvsSize;
|
---|
| 99 |
|
---|
[420] | 100 | // number of dynamically added ray refs
|
---|
| 101 | int addedRayRefs;
|
---|
| 102 | // number of dynamically removed ray refs
|
---|
| 103 | int removedRayRefs;
|
---|
[405] | 104 |
|
---|
[420] | 105 | /** Default constructor.
|
---|
| 106 | */
|
---|
| 107 | VspKdStatistics() { Reset(); }
|
---|
| 108 | int Nodes() const { return nodes; }
|
---|
| 109 | int Interior() const { return nodes / 2; }
|
---|
| 110 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
[405] | 111 |
|
---|
[420] | 112 | void Reset()
|
---|
| 113 | {
|
---|
| 114 | nodes = 0;
|
---|
[405] | 115 |
|
---|
[454] | 116 | for (int i=0; i<3; i++)
|
---|
[420] | 117 | splits[i] = 0;
|
---|
[405] | 118 |
|
---|
[420] | 119 | rays = queryDomains = 0;
|
---|
| 120 | rayRefs = 0;
|
---|
[426] | 121 |
|
---|
[420] | 122 | maxDepthNodes = 0;
|
---|
| 123 | minPvsNodes = 0;
|
---|
[426] | 124 | minRaysNodes = 0;
|
---|
| 125 | maxRayContribNodes = 0;
|
---|
| 126 | minSizeNodes = 0;
|
---|
| 127 |
|
---|
[420] | 128 | maxRayRefs = 0;
|
---|
| 129 | addedRayRefs = removedRayRefs = 0;
|
---|
[426] | 130 |
|
---|
[420] | 131 | initialPvsSize = 0;
|
---|
[426] | 132 | maxPvsSize = 0;
|
---|
[420] | 133 | }
|
---|
| 134 |
|
---|
| 135 | void Print(ostream &app) const;
|
---|
| 136 | friend ostream &operator<<(ostream &s, const VspKdStatistics &stat)
|
---|
| 137 | {
|
---|
| 138 | stat.Print(s);
|
---|
| 139 | return s;
|
---|
| 140 | }
|
---|
[405] | 141 | };
|
---|
| 142 |
|
---|
[408] | 143 |
|
---|
| 144 | class VspKdTreeInterior;
|
---|
| 145 |
|
---|
| 146 |
|
---|
[420] | 147 | /** Abstract superclass of Vsp-Kd-Tree nodes.
|
---|
| 148 | */
|
---|
[408] | 149 | class VspKdTreeNode
|
---|
| 150 | {
|
---|
[405] | 151 | public:
|
---|
[408] | 152 |
|
---|
[419] | 153 | friend class VspKdTree;
|
---|
[408] | 154 |
|
---|
[420] | 155 | enum {EInterior, ELeaf};
|
---|
[405] | 156 |
|
---|
[420] | 157 | /** Constructs new interior node from the parent node.
|
---|
| 158 | */
|
---|
| 159 | inline VspKdTreeNode(VspKdTreeInterior *p);
|
---|
[405] | 160 |
|
---|
[420] | 161 | /** Destroys this node and the subtree.
|
---|
| 162 | */
|
---|
| 163 | virtual ~VspKdTreeNode();
|
---|
[405] | 164 |
|
---|
[420] | 165 | /** Type of the node (Einterior or ELeaf)
|
---|
| 166 | */
|
---|
| 167 | virtual int Type() const = 0;
|
---|
| 168 |
|
---|
| 169 | /** Returns true if this node is a leaf.
|
---|
| 170 | */
|
---|
| 171 | bool IsLeaf() const;
|
---|
| 172 |
|
---|
| 173 | /** Prints node stats.
|
---|
| 174 | */
|
---|
| 175 | virtual void Print(ostream &s) const = 0;
|
---|
[405] | 176 |
|
---|
[420] | 177 | /** Returns time needed to access this node.
|
---|
| 178 | */
|
---|
| 179 | virtual int GetAccessTime(); // NOTE: don't really know how it is used!
|
---|
[405] | 180 |
|
---|
[420] | 181 | /** Returns parent node.
|
---|
| 182 | */
|
---|
| 183 | VspKdTreeInterior *GetParent() const;
|
---|
[426] | 184 |
|
---|
[420] | 185 | /** Sets parent node.
|
---|
| 186 | */
|
---|
| 187 | void SetParent(VspKdTreeInterior *p);
|
---|
[408] | 188 |
|
---|
[420] | 189 | protected:
|
---|
| 190 | /////////////////////////////////
|
---|
| 191 | // The actual data goes here
|
---|
[408] | 192 |
|
---|
[420] | 193 | /// link to the parent
|
---|
| 194 | VspKdTreeInterior *mParent;
|
---|
[405] | 195 |
|
---|
[420] | 196 | enum {SPLIT_X = 0, SPLIT_Y, SPLIT_Z, SPLIT_DIRX, SPLIT_DIRY, SPLIT_DIRZ};
|
---|
[408] | 197 |
|
---|
[420] | 198 | /// splitting axis
|
---|
| 199 | char mAxis;
|
---|
[408] | 200 |
|
---|
[420] | 201 | /// depth
|
---|
| 202 | unsigned char mDepth;
|
---|
[405] | 203 | };
|
---|
[408] | 204 |
|
---|
| 205 | // --------------------------------------------------------------
|
---|
| 206 | // KD-tree node - interior node
|
---|
| 207 | // --------------------------------------------------------------
|
---|
[411] | 208 | class VspKdTreeInterior: public VspKdTreeNode
|
---|
[408] | 209 | {
|
---|
| 210 | public:
|
---|
[419] | 211 | friend class VspKdTree;
|
---|
| 212 |
|
---|
[420] | 213 | /** Constructs new interior node from parent node.
|
---|
| 214 | */
|
---|
[418] | 215 | VspKdTreeInterior(VspKdTreeInterior *p);
|
---|
| 216 |
|
---|
| 217 | virtual int GetAccessTime();
|
---|
| 218 |
|
---|
[419] | 219 | virtual int Type() const;
|
---|
[405] | 220 |
|
---|
[418] | 221 | virtual ~VspKdTreeInterior();
|
---|
| 222 |
|
---|
| 223 | virtual void Print(ostream &s) const;
|
---|
[408] | 224 |
|
---|
[420] | 225 | /** Returns back child.
|
---|
| 226 | */
|
---|
[419] | 227 | inline VspKdTreeNode *GetBack() const;
|
---|
[420] | 228 | /** Returns front child.
|
---|
| 229 | */
|
---|
[419] | 230 | inline VspKdTreeNode *GetFront() const;
|
---|
| 231 |
|
---|
[418] | 232 | protected:
|
---|
[408] | 233 |
|
---|
[420] | 234 | /** Sets pointers to back child and front child.
|
---|
| 235 | */
|
---|
[418] | 236 | void SetupChildLinks(VspKdTreeNode *b, VspKdTreeNode *f);
|
---|
[420] | 237 |
|
---|
| 238 | /** Replaces the pointer to oldChild with a pointer to newChild.
|
---|
| 239 | */
|
---|
[418] | 240 | void ReplaceChildLink(VspKdTreeNode *oldChild, VspKdTreeNode *newChild);
|
---|
| 241 |
|
---|
[420] | 242 | /** Computes intersection of the ray with the node boundaries.
|
---|
| 243 | */
|
---|
[418] | 244 | int ComputeRayIntersection(const RayInfo &rayData, float &t);
|
---|
[408] | 245 |
|
---|
[418] | 246 | // plane in local modelling coordinates
|
---|
| 247 | float mPosition;
|
---|
[408] | 248 |
|
---|
[418] | 249 | // pointers to children
|
---|
| 250 | VspKdTreeNode *mBack;
|
---|
| 251 | VspKdTreeNode *mFront;
|
---|
[405] | 252 |
|
---|
[418] | 253 | // the bbox of the node
|
---|
[419] | 254 | AxisAlignedBox3 mBox;
|
---|
[418] | 255 |
|
---|
| 256 | // data for caching
|
---|
| 257 | long mAccesses;
|
---|
| 258 | long mLastAccessTime;
|
---|
[408] | 259 | };
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 | // --------------------------------------------------------------
|
---|
| 263 | // KD-tree node - leaf node
|
---|
| 264 | // --------------------------------------------------------------
|
---|
[419] | 265 | class VspKdTreeLeaf: public VspKdTreeNode
|
---|
[408] | 266 | {
|
---|
| 267 | public:
|
---|
[419] | 268 | friend class VspKdTree;
|
---|
| 269 |
|
---|
[420] | 270 | /** Constructs leaf from parent node.
|
---|
| 271 | @param p the parent node
|
---|
| 272 | @param nRays preallocates memory to store this number of rays
|
---|
| 273 | */
|
---|
| 274 | VspKdTreeLeaf(VspKdTreeInterior *p, const int nRays);
|
---|
[408] | 275 |
|
---|
[420] | 276 | virtual ~VspKdTreeLeaf();
|
---|
[405] | 277 |
|
---|
[420] | 278 | virtual int Type() const;
|
---|
[408] | 279 |
|
---|
[420] | 280 | virtual void Print(ostream &s) const;
|
---|
[405] | 281 |
|
---|
[420] | 282 | /** Adds a ray to the leaf ray container.
|
---|
| 283 | */
|
---|
| 284 | void AddRay(const RayInfo &data);
|
---|
| 285 | /** Returns size of the leaf PVS as induced by the rays
|
---|
| 286 | @note returns precomputed PVS size, which may not be valid
|
---|
| 287 | anymore. Run UpdatePvsSize to get a valid PVS.
|
---|
| 288 | */
|
---|
| 289 | int GetPvsSize() const;
|
---|
[405] | 290 |
|
---|
[420] | 291 | /** If PVS is not valid, this function recomputes the leaf
|
---|
| 292 | PVS as induced by the rays.
|
---|
| 293 | */
|
---|
[410] | 294 | void UpdatePvsSize();
|
---|
[408] | 295 |
|
---|
[428] | 296 | /** Returns stored rays.
|
---|
| 297 | */
|
---|
[426] | 298 | RayInfoContainer &GetRays();
|
---|
| 299 |
|
---|
[428] | 300 | /** Returns rays into this ray container.
|
---|
| 301 | */
|
---|
| 302 | void GetRays(VssRayContainer &rays);
|
---|
[420] | 303 | /** Returns average contribution of a ray to the PVS
|
---|
| 304 | */
|
---|
| 305 | inline float GetAvgRayContribution() const;
|
---|
| 306 | /** Returns square of average contribution of a ray.
|
---|
| 307 | */
|
---|
| 308 | inline float GetSqrRayContribution() const;
|
---|
[408] | 309 |
|
---|
[428] | 310 | /** Extracts PVS from ray set.
|
---|
| 311 | */
|
---|
| 312 | void ExtractPvs(ObjectContainer &objects) const;
|
---|
| 313 |
|
---|
[422] | 314 | //-- mailing options
|
---|
| 315 | void Mail();
|
---|
| 316 |
|
---|
| 317 | bool Mailed() const;
|
---|
| 318 |
|
---|
| 319 | bool Mailed(const int mail);
|
---|
| 320 |
|
---|
[420] | 321 | static void NewMail();
|
---|
| 322 |
|
---|
| 323 | static int mailID;
|
---|
| 324 |
|
---|
[453] | 325 | /** Returns view cell.
|
---|
| 326 | */
|
---|
| 327 | ViewCell *GetViewCell();
|
---|
| 328 |
|
---|
[420] | 329 | protected:
|
---|
[426] | 330 |
|
---|
| 331 | /** Manually sets PVS size.
|
---|
| 332 | @param s the PVS size
|
---|
| 333 | */
|
---|
| 334 | void SetPvsSize(const int s);
|
---|
| 335 |
|
---|
[420] | 336 | int mMailbox;
|
---|
| 337 |
|
---|
| 338 | RayInfoContainer mRays;
|
---|
| 339 | bool mValidPvs;
|
---|
[452] | 340 |
|
---|
| 341 | ViewCell *mViewCell;
|
---|
[419] | 342 | private:
|
---|
| 343 | int mPvsSize;
|
---|
[405] | 344 | };
|
---|
| 345 |
|
---|
| 346 |
|
---|
| 347 |
|
---|
[408] | 348 | // ---------------------------------------------------------------
|
---|
| 349 | // Main LSDS search class
|
---|
| 350 | // ---------------------------------------------------------------
|
---|
| 351 | class VspKdTree
|
---|
| 352 | {
|
---|
[411] | 353 | // --------------------------------------------------------------
|
---|
| 354 | // For sorting rays
|
---|
| 355 | // -------------------------------------------------------------
|
---|
| 356 | struct SortableEntry
|
---|
| 357 | {
|
---|
| 358 | enum EType
|
---|
| 359 | {
|
---|
| 360 | ERayMin,
|
---|
| 361 | ERayMax
|
---|
| 362 | };
|
---|
| 363 |
|
---|
| 364 | int type;
|
---|
| 365 | float value;
|
---|
| 366 | void *data;
|
---|
| 367 |
|
---|
| 368 | SortableEntry() {}
|
---|
| 369 | SortableEntry(const int t, const float v, void *d):type(t),
|
---|
| 370 | value(v), data(d)
|
---|
| 371 | {
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | friend bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
| 375 | {
|
---|
| 376 | return a.value < b.value;
|
---|
| 377 | }
|
---|
| 378 | };
|
---|
| 379 |
|
---|
[410] | 380 | struct TraversalData
|
---|
| 381 | {
|
---|
[419] | 382 | VspKdTreeNode *mNode;
|
---|
| 383 | AxisAlignedBox3 mBox;
|
---|
| 384 | int mDepth;
|
---|
[423] | 385 | //float mPriority;
|
---|
[410] | 386 |
|
---|
| 387 | TraversalData() {}
|
---|
[405] | 388 |
|
---|
[410] | 389 | TraversalData(VspKdTreeNode *n, const float p):
|
---|
[423] | 390 | mNode(n)//, mPriority(p)
|
---|
[410] | 391 | {}
|
---|
[408] | 392 |
|
---|
[410] | 393 | TraversalData(VspKdTreeNode *n, const AxisAlignedBox3 &b, const int d):
|
---|
[419] | 394 | mNode(n), mBox(b), mDepth(d) {}
|
---|
[405] | 395 |
|
---|
[423] | 396 | // comparator for the priority queue
|
---|
| 397 | /*struct less_priority : public binary_function<const TraversalData, const TraversalData, bool>
|
---|
[410] | 398 | {
|
---|
[423] | 399 | bool operator()(const TraversalData a, const TraversalData b) {
|
---|
| 400 | return a.mPriority < b.mPriority; } };*/
|
---|
[405] | 401 |
|
---|
[410] | 402 | // ~TraversalData() {}
|
---|
| 403 | // TraversalData(const TraversalData &s):node(s.node), bbox(s.bbox), depth(s.depth) {}
|
---|
[408] | 404 |
|
---|
[410] | 405 | friend bool operator<(const TraversalData &a, const TraversalData &b)
|
---|
| 406 | {
|
---|
| 407 | // return a.node->queries.size() < b.node->queries.size();
|
---|
[419] | 408 | VspKdTreeLeaf *leafa = (VspKdTreeLeaf *) a.mNode;
|
---|
| 409 | VspKdTreeLeaf *leafb = (VspKdTreeLeaf *) b.mNode;
|
---|
[408] | 410 | #if 0
|
---|
| 411 | return
|
---|
[419] | 412 | leafa->rays.size()*a.mBox.GetVolume()
|
---|
[408] | 413 | <
|
---|
[419] | 414 | leafb->rays.size()*b.mBox.GetVolume();
|
---|
[408] | 415 | #endif
|
---|
| 416 | #if 1
|
---|
| 417 | return
|
---|
[419] | 418 | leafa->GetPvsSize()*a.mBox.GetVolume()
|
---|
[408] | 419 | <
|
---|
[419] | 420 | leafb->GetPvsSize()*b.mBox.GetVolume();
|
---|
[408] | 421 | #endif
|
---|
| 422 | #if 0
|
---|
| 423 | return
|
---|
| 424 | leafa->GetPvsSize()
|
---|
| 425 | <
|
---|
| 426 | leafb->GetPvsSize();
|
---|
| 427 | #endif
|
---|
| 428 | #if 0
|
---|
| 429 | return
|
---|
[423] | 430 | leafa->GetPvsSize() / (float)(leafa->rays.size() + Limits::Small())
|
---|
[408] | 431 | >
|
---|
[423] | 432 | leafb->GetPvsSize() / (float)(leafb->rays.size() + Limits::Small());
|
---|
[408] | 433 | #endif
|
---|
| 434 | #if 0
|
---|
| 435 | return
|
---|
[410] | 436 | leafa->GetPvsSize() * (float)leafa->rays.size()
|
---|
[408] | 437 | <
|
---|
[410] | 438 | leafb->GetPvsSize() * (float)leafb->rays.size();
|
---|
[408] | 439 | #endif
|
---|
| 440 | }
|
---|
[405] | 441 | };
|
---|
| 442 |
|
---|
[420] | 443 | /** Simplified data for ray traversal only.
|
---|
| 444 | */
|
---|
[410] | 445 | struct RayTraversalData
|
---|
| 446 | {
|
---|
[420] | 447 | RayInfo mRayData;
|
---|
[419] | 448 | VspKdTreeNode *mNode;
|
---|
[410] | 449 |
|
---|
| 450 | RayTraversalData() {}
|
---|
| 451 |
|
---|
[420] | 452 | RayTraversalData(VspKdTreeNode *n, const RayInfo &data):
|
---|
[419] | 453 | mRayData(data), mNode(n) {}
|
---|
[408] | 454 | };
|
---|
| 455 |
|
---|
[405] | 456 | public:
|
---|
| 457 |
|
---|
[410] | 458 | VspKdTree();
|
---|
| 459 | virtual ~VspKdTree();
|
---|
[405] | 460 |
|
---|
[440] | 461 | virtual void Construct(const VssRayContainer &rays,
|
---|
[410] | 462 | AxisAlignedBox3 *forcedBoundingBox = NULL);
|
---|
[420] | 463 |
|
---|
| 464 | /** Returns bounding box of the specified node.
|
---|
| 465 | */
|
---|
| 466 | AxisAlignedBox3 GetBBox(VspKdTreeNode *node) const;
|
---|
| 467 |
|
---|
| 468 | const VspKdStatistics &GetStatistics() const;
|
---|
| 469 |
|
---|
| 470 | /** Get the root of the tree.
|
---|
| 471 | */
|
---|
| 472 | VspKdTreeNode *GetRoot() const;
|
---|
| 473 |
|
---|
| 474 | /** Returns average PVS size in tree.
|
---|
| 475 | */
|
---|
| 476 | float GetAvgPvsSize();
|
---|
| 477 |
|
---|
| 478 | /** Returns memory usage in MB.
|
---|
| 479 | */
|
---|
| 480 | float GetMemUsage() const;
|
---|
| 481 |
|
---|
| 482 | float GetRayMemUsage() const;
|
---|
| 483 |
|
---|
[428] | 484 | /** Collects leaves of this tree.
|
---|
| 485 | */
|
---|
| 486 | void CollectLeaves(vector<VspKdTreeLeaf *> &leaves) const;
|
---|
| 487 |
|
---|
[452] | 488 | /** Merges leaves of this tree according to some criteria.
|
---|
| 489 | */
|
---|
| 490 | void MergeLeaves();
|
---|
| 491 |
|
---|
| 492 | /** Finds neighbours of this node.
|
---|
| 493 | @param n the input node
|
---|
| 494 | @param neighbours the neighbors of the input node
|
---|
| 495 | @param onlyUnmailed if only unmailed neighbors are collected
|
---|
| 496 | */
|
---|
| 497 | int FindNeighbors(VspKdTreeLeaf *n,
|
---|
| 498 | vector<VspKdTreeLeaf *> &neighbors,
|
---|
| 499 | bool onlyUnmailed);
|
---|
| 500 |
|
---|
[420] | 501 | protected:
|
---|
[428] | 502 |
|
---|
[420] | 503 | // incremental construction
|
---|
[410] | 504 | virtual void UpdateRays(VssRayContainer &remove, VssRayContainer &add);
|
---|
[405] | 505 |
|
---|
[420] | 506 | virtual void AddRays(VssRayContainer &add);
|
---|
[405] | 507 |
|
---|
[410] | 508 | VspKdTreeNode *Locate(const Vector3 &v);
|
---|
[408] | 509 |
|
---|
[410] | 510 | VspKdTreeNode *SubdivideNode(VspKdTreeLeaf *leaf,
|
---|
| 511 | const AxisAlignedBox3 &box,
|
---|
| 512 | AxisAlignedBox3 &backBox,
|
---|
| 513 | AxisAlignedBox3 &frontBox);
|
---|
[408] | 514 |
|
---|
[410] | 515 | VspKdTreeNode *Subdivide(const TraversalData &tdata);
|
---|
[408] | 516 |
|
---|
[410] | 517 | int SelectPlane(VspKdTreeLeaf *leaf,
|
---|
| 518 | const AxisAlignedBox3 &box,
|
---|
| 519 | float &position,
|
---|
| 520 | int &raysBack,
|
---|
| 521 | int &raysFront,
|
---|
| 522 | int &pvsBack,
|
---|
| 523 | int &pvsFront);
|
---|
[405] | 524 |
|
---|
[410] | 525 | void SortSplitCandidates(VspKdTreeLeaf *node,
|
---|
[420] | 526 | const int axis);
|
---|
[408] | 527 |
|
---|
[410] | 528 | float BestCostRatioHeuristic(VspKdTreeLeaf *node,
|
---|
| 529 | int &axis,
|
---|
| 530 | float &position,
|
---|
| 531 | int &raysBack,
|
---|
| 532 | int &raysFront,
|
---|
| 533 | int &pvsBack,
|
---|
| 534 | int &pvsFront);
|
---|
[405] | 535 |
|
---|
[410] | 536 | float BestCostRatioRegular(VspKdTreeLeaf *node,
|
---|
| 537 | int &axis,
|
---|
| 538 | float &position,
|
---|
| 539 | int &raysBack,
|
---|
| 540 | int &raysFront,
|
---|
| 541 | int &pvsBack,
|
---|
| 542 | int &pvsFront);
|
---|
[408] | 543 |
|
---|
[410] | 544 | float EvalCostRatio(VspKdTreeLeaf *node,
|
---|
| 545 | const int axis,
|
---|
| 546 | const float position,
|
---|
| 547 | int &raysBack,
|
---|
| 548 | int &raysFront,
|
---|
| 549 | int &pvsBack,
|
---|
| 550 | int &pvsFront);
|
---|
[405] | 551 |
|
---|
| 552 |
|
---|
[410] | 553 | VspKdTreeNode * SubdivideLeaf(VspKdTreeLeaf *leaf,
|
---|
| 554 | const float SAThreshold);
|
---|
[405] | 555 |
|
---|
[410] | 556 | void RemoveRay(VssRay *ray,
|
---|
| 557 | vector<VspKdTreeLeaf *> *affectedLeaves,
|
---|
| 558 | const bool removeAllScheduledRays);
|
---|
[405] | 559 |
|
---|
[410] | 560 | void AddRay(VssRay *ray);
|
---|
[408] | 561 |
|
---|
[410] | 562 | void TraverseInternalNode(RayTraversalData &data,
|
---|
| 563 | stack<RayTraversalData> &tstack);
|
---|
[408] | 564 |
|
---|
[410] | 565 | void EvaluateLeafStats(const TraversalData &data);
|
---|
[405] | 566 |
|
---|
| 567 |
|
---|
[418] | 568 | int GetRootPvsSize() const;
|
---|
[408] | 569 |
|
---|
[410] | 570 | int GetPvsSize(VspKdTreeNode *node, const AxisAlignedBox3 &box) const;
|
---|
[405] | 571 |
|
---|
[410] | 572 | void GetRayContributionStatistics(float &minRayContribution,
|
---|
| 573 | float &maxRayContribution,
|
---|
| 574 | float &avgRayContribution);
|
---|
[405] | 575 |
|
---|
[410] | 576 | int GenerateRays(const float ratioPerLeaf,
|
---|
| 577 | SimpleRayContainer &rays);
|
---|
[405] | 578 |
|
---|
[425] | 579 | /** Collapses this subtree and releases the memory.
|
---|
| 580 | @returns number of collapsed rays.
|
---|
| 581 | */
|
---|
[418] | 582 | int CollapseSubtree(VspKdTreeNode *sroot, const int time);
|
---|
| 583 |
|
---|
[419] | 584 | int ReleaseMemory(const int time);
|
---|
| 585 |
|
---|
[421] | 586 | bool TerminationCriteriaMet(const VspKdTreeLeaf *leaf,
|
---|
| 587 | const AxisAlignedBox3 &box) const;
|
---|
| 588 |
|
---|
[425] | 589 | /** Computes PVS size of this node given a global ray set.
|
---|
| 590 | @returns PVS size of the intersection of this node bounding box with the rays.
|
---|
| 591 | */
|
---|
| 592 | int ComputePvsSize(VspKdTreeNode *node, const RayInfoContainer &globalRays) const;
|
---|
| 593 |
|
---|
[418] | 594 | protected:
|
---|
[452] | 595 |
|
---|
| 596 |
|
---|
[418] | 597 | /////////////////////////////
|
---|
| 598 | // The core pointer
|
---|
| 599 | VspKdTreeNode *mRoot;
|
---|
| 600 |
|
---|
| 601 | /////////////////////////////
|
---|
| 602 | // Basic properties
|
---|
| 603 |
|
---|
| 604 | // total number of nodes of the tree
|
---|
[422] | 605 | int mNumNodes;
|
---|
[418] | 606 |
|
---|
| 607 | // axis aligned bounding box of the scene
|
---|
| 608 | AxisAlignedBox3 mBox;
|
---|
| 609 |
|
---|
| 610 | // epsilon used for the construction
|
---|
[422] | 611 | float mEpsilon;
|
---|
[418] | 612 |
|
---|
| 613 | // ratio between traversal and intersection costs
|
---|
[422] | 614 | float mCtDivCi;
|
---|
[418] | 615 |
|
---|
| 616 | // type of the splitting to use for the tree construction
|
---|
| 617 | enum {ESplitRegular, ESplitHeuristic };
|
---|
| 618 | int splitType;
|
---|
| 619 |
|
---|
| 620 | // maximum alovable memory in MB
|
---|
[422] | 621 | float mMaxTotalMemory;
|
---|
[418] | 622 |
|
---|
| 623 | // maximum alovable memory for static kd tree in MB
|
---|
[422] | 624 | float mMaxStaticMemory;
|
---|
[418] | 625 |
|
---|
| 626 | // this is used during the construction depending
|
---|
| 627 | // on the type of the tree and queries...
|
---|
[422] | 628 | float mMaxMemory;
|
---|
[418] | 629 |
|
---|
| 630 | // minimal acess time for collapse
|
---|
[422] | 631 | int mAccessTimeThreshold;
|
---|
[418] | 632 |
|
---|
| 633 | // minimal depth at which to perform collapse
|
---|
[422] | 634 | int mMinCollapseDepth;
|
---|
[418] | 635 |
|
---|
| 636 | // reusable array of split candidates
|
---|
[422] | 637 | vector<SortableEntry> *mSplitCandidates;
|
---|
[418] | 638 |
|
---|
| 639 | /////////////////////////////
|
---|
[421] | 640 | // Construction parameters
|
---|
| 641 |
|
---|
| 642 | /// max depth of the tree
|
---|
| 643 | int mTermMaxDepth;
|
---|
| 644 |
|
---|
| 645 | /// minimal ratio of the volume of the cell and the query volume
|
---|
| 646 | float mTermMinSize;
|
---|
| 647 |
|
---|
| 648 | /// minimal pvs per node to still get subdivided
|
---|
| 649 | int mTermMinPvs;
|
---|
| 650 |
|
---|
| 651 | /// minimal ray number per node to still get subdivided
|
---|
| 652 | int mTermMinRays;
|
---|
| 653 |
|
---|
| 654 | /// maximal cost ration to subdivide a node
|
---|
| 655 | float mTermMaxCostRatio;
|
---|
| 656 |
|
---|
| 657 | /// maximal contribution per ray to subdivide the node
|
---|
| 658 | float mTermMaxRayContribution;
|
---|
| 659 |
|
---|
[452] | 660 | bool mOnlyDrivingAxis;
|
---|
[421] | 661 |
|
---|
[452] | 662 | typedef std::pair<VspKdTreeLeaf *, VspKdTreeLeaf *> LeafPair;
|
---|
| 663 |
|
---|
[421] | 664 | /////////////////////////////
|
---|
[418] | 665 | VspKdStatistics mStat;
|
---|
[405] | 666 | };
|
---|
| 667 |
|
---|
[408] | 668 |
|
---|
| 669 | #endif // __LSDS_KDTREE_H__
|
---|
| 670 |
|
---|