[372] | 1 | // ================================================================
|
---|
| 2 | // $Id$
|
---|
| 3 | // ****************************************************************
|
---|
| 4 | //
|
---|
| 5 | // Initial coding by
|
---|
| 6 | /**
|
---|
| 7 | @author Jiri Bittner
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #ifndef __VSSTREE_H__
|
---|
| 11 | #define __VSSTREE_H__
|
---|
| 12 |
|
---|
| 13 | // Standard headers
|
---|
| 14 | #include <iomanip>
|
---|
| 15 | #include <vector>
|
---|
| 16 | #include <functional>
|
---|
| 17 | #include <stack>
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | // User headers
|
---|
| 21 | #include "VssRay.h"
|
---|
| 22 | #include "AxisAlignedBox3.h"
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | #define USE_KDNODE_VECTORS 1
|
---|
| 26 | #define _RSS_STATISTICS
|
---|
| 27 | #define _RSS_TRAVERSAL_STATISTICS
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | #include "Statistics.h"
|
---|
[401] | 31 | #include "Ray.h"
|
---|
[372] | 32 |
|
---|
| 33 | // --------------------------------------------------------------
|
---|
| 34 | // Static statistics for kd-tree search
|
---|
| 35 | // --------------------------------------------------------------
|
---|
| 36 | class VssStatistics :
|
---|
| 37 | public StatisticsBase
|
---|
| 38 | {
|
---|
| 39 | public:
|
---|
| 40 | // total number of nodes
|
---|
| 41 | int nodes;
|
---|
| 42 | // number of splits along each of the axes
|
---|
| 43 | int splits[7];
|
---|
| 44 | // totals number of rays
|
---|
| 45 | int rays;
|
---|
[438] | 46 | // initial size of the pvs
|
---|
[382] | 47 | int initialPvsSize;
|
---|
[438] | 48 | // total number of query domains
|
---|
[372] | 49 | int queryDomains;
|
---|
| 50 | // total number of ray references
|
---|
| 51 | int rayRefs;
|
---|
[382] | 52 |
|
---|
[438] | 53 | // max depth nodes
|
---|
[372] | 54 | int maxDepthNodes;
|
---|
| 55 | // max depth nodes
|
---|
[403] | 56 | int minPvsNodes;
|
---|
| 57 | int minRaysNodes;
|
---|
[438] | 58 | // max ray contribution nodes
|
---|
[382] | 59 | int maxRayContribNodes;
|
---|
[438] | 60 | // max depth nodes
|
---|
[382] | 61 | int minSizeNodes;
|
---|
[427] | 62 | int maxCostRatioNodes;
|
---|
| 63 |
|
---|
[372] | 64 | // max number of rays per node
|
---|
| 65 | int maxRayRefs;
|
---|
| 66 | // number of dynamically added ray refs
|
---|
| 67 | int addedRayRefs;
|
---|
| 68 | // number of dynamically removed ray refs
|
---|
| 69 | int removedRayRefs;
|
---|
| 70 |
|
---|
| 71 | // Constructor
|
---|
| 72 | VssStatistics() {
|
---|
| 73 | Reset();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | int Nodes() const {return nodes;}
|
---|
| 77 | int Interior() const { return nodes/2; }
|
---|
| 78 | int Leaves() const { return (nodes/2) + 1; }
|
---|
| 79 |
|
---|
| 80 | void Reset() {
|
---|
| 81 | nodes = 0;
|
---|
| 82 | for (int i=0; i<7; i++)
|
---|
| 83 | splits[i] = 0;
|
---|
| 84 | rays = queryDomains = 0;
|
---|
[382] | 85 | rayRefs = 0;
|
---|
[372] | 86 | maxDepthNodes = 0;
|
---|
[403] | 87 | minPvsNodes = 0;
|
---|
| 88 | minRaysNodes = 0;
|
---|
[372] | 89 | maxRayRefs = 0;
|
---|
| 90 | addedRayRefs = removedRayRefs = 0;
|
---|
[438] | 91 | initialPvsSize = 0;
|
---|
| 92 | maxRayContribNodes = 0;
|
---|
| 93 | minSizeNodes = 0;
|
---|
| 94 | maxCostRatioNodes = 0;
|
---|
[372] | 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | void
|
---|
| 99 | Print(ostream &app) const;
|
---|
| 100 |
|
---|
| 101 | friend ostream &operator<<(ostream &s, const VssStatistics &stat) {
|
---|
| 102 | stat.Print(s);
|
---|
| 103 | return s;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | };
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | class VssTreeInterior;
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | // --------------------------------------------------------------
|
---|
| 115 | // KD-tree node - abstract class
|
---|
| 116 | // --------------------------------------------------------------
|
---|
| 117 | class VssTreeNode
|
---|
| 118 | {
|
---|
| 119 | public:
|
---|
| 120 |
|
---|
[427] | 121 | #define USE_FIXEDPOINT_T 0
|
---|
[372] | 122 |
|
---|
[438] | 123 | struct RayInfo
|
---|
| 124 | {
|
---|
[372] | 125 | // pointer to the actual ray
|
---|
| 126 | VssRay *mRay;
|
---|
| 127 |
|
---|
| 128 | // endpoints - do we need them?
|
---|
| 129 | #if USE_FIXEDPOINT_T
|
---|
| 130 | short mMinT, mMaxT;
|
---|
| 131 | #else
|
---|
| 132 | float mMinT, mMaxT;
|
---|
| 133 | #endif
|
---|
| 134 |
|
---|
| 135 | RayInfo():mRay(NULL) {}
|
---|
| 136 |
|
---|
| 137 | RayInfo(VssRay *r):mRay(r), mMinT(0),
|
---|
| 138 | #if USE_FIXEDPOINT_T
|
---|
| 139 | #define FIXEDPOINT_ONE 0x7FFE
|
---|
[438] | 140 | // mMaxT(0xFFFF)
|
---|
| 141 | mMaxT(FIXEDPOINT_ONE)
|
---|
[372] | 142 | #else
|
---|
[438] | 143 | mMaxT(1.0f)
|
---|
[372] | 144 | #endif
|
---|
| 145 | {}
|
---|
| 146 |
|
---|
| 147 | RayInfo(VssRay *r,
|
---|
[438] | 148 | const float _min,
|
---|
| 149 | const float _max
|
---|
| 150 | ):mRay(r) {
|
---|
| 151 | SetMinT(_min);
|
---|
| 152 | SetMaxT(_max);
|
---|
[372] | 153 | }
|
---|
| 154 |
|
---|
| 155 | RayInfo(VssRay *r,
|
---|
[438] | 156 | const short _min,
|
---|
| 157 | const float _max
|
---|
| 158 | ):mRay(r), mMinT(_min) {
|
---|
| 159 | SetMaxT(_max);
|
---|
[372] | 160 | }
|
---|
| 161 |
|
---|
| 162 | RayInfo(VssRay *r,
|
---|
[438] | 163 | const float _min,
|
---|
| 164 | const short _max
|
---|
| 165 | ):mRay(r), mMaxT(_max) {
|
---|
| 166 | SetMinT(_min);
|
---|
[372] | 167 | }
|
---|
[427] | 168 |
|
---|
| 169 | enum {
|
---|
[438] | 170 | SOURCE_RAY = 0,
|
---|
| 171 | TERMINATION_RAY,
|
---|
| 172 | PASSING_RAY,
|
---|
| 173 | CONTAINED_RAY
|
---|
[427] | 174 | };
|
---|
[372] | 175 |
|
---|
[427] | 176 | int GetRayClass() const {
|
---|
| 177 |
|
---|
[438] | 178 | bool startsBefore = GetMinT() > 0.0f;
|
---|
| 179 | bool terminatesAfter = GetMaxT() < 1.0f;
|
---|
[427] | 180 |
|
---|
[438] | 181 | if (startsBefore && terminatesAfter)
|
---|
| 182 | return PASSING_RAY;
|
---|
[427] | 183 |
|
---|
[438] | 184 | if (!startsBefore && !terminatesAfter)
|
---|
| 185 | return CONTAINED_RAY;
|
---|
[427] | 186 |
|
---|
[438] | 187 | if (!startsBefore)
|
---|
| 188 | return SOURCE_RAY;
|
---|
[427] | 189 |
|
---|
[438] | 190 | return TERMINATION_RAY;
|
---|
[427] | 191 | }
|
---|
| 192 |
|
---|
[372] | 193 | friend bool operator<(const RayInfo &a, const RayInfo &b) {
|
---|
[438] | 194 | return a.mRay < b.mRay;
|
---|
[372] | 195 | }
|
---|
| 196 |
|
---|
| 197 |
|
---|
| 198 | float ExtrapOrigin(const int axis) const {
|
---|
[438] | 199 | return mRay->GetOrigin(axis) + GetMinT()*mRay->GetDir(axis);
|
---|
[372] | 200 | }
|
---|
| 201 |
|
---|
| 202 | float ExtrapTermination(const int axis) const {
|
---|
[438] | 203 | return mRay->GetOrigin(axis) + GetMaxT()*mRay->GetDir(axis);
|
---|
[372] | 204 | }
|
---|
[434] | 205 |
|
---|
| 206 | Vector3 Extrap(const float t) const {
|
---|
[438] | 207 | return mRay->Extrap(t);
|
---|
[434] | 208 | }
|
---|
[372] | 209 |
|
---|
| 210 | #if USE_FIXEDPOINT_T
|
---|
| 211 | float GetMinT () const { return mMinT/(float)(FIXEDPOINT_ONE); }
|
---|
| 212 | float GetMaxT () const { return mMaxT/(float)(FIXEDPOINT_ONE); }
|
---|
| 213 |
|
---|
| 214 | void SetMinT (const float t) {
|
---|
[438] | 215 | mMinT = (short) (t*(float)(FIXEDPOINT_ONE));
|
---|
[372] | 216 | }
|
---|
| 217 |
|
---|
| 218 | void SetMaxT (const float t) {
|
---|
[438] | 219 | mMaxT = (short) (t*(float)(FIXEDPOINT_ONE));
|
---|
| 220 | mMaxT++;
|
---|
| 221 | // if (mMaxT!=0xFFFF)
|
---|
| 222 | // mMaxT++;
|
---|
[372] | 223 | }
|
---|
| 224 | #else
|
---|
| 225 | float GetMinT () const { return mMinT; }
|
---|
| 226 | float GetMaxT () const { return mMaxT; }
|
---|
| 227 |
|
---|
| 228 | void SetMinT (const float t) { mMinT = t; }
|
---|
| 229 | void SetMaxT (const float t) { mMaxT = t; }
|
---|
| 230 | #endif
|
---|
[382] | 231 |
|
---|
| 232 |
|
---|
[438] | 233 | int ComputeRayIntersection(const int axis,
|
---|
| 234 | const float position,
|
---|
| 235 | float &t
|
---|
| 236 | ) const {
|
---|
[382] | 237 |
|
---|
[446] | 238 | #if 1
|
---|
[438] | 239 | // intersect the ray with the plane
|
---|
| 240 | float denom = mRay->GetDir(axis);
|
---|
[382] | 241 |
|
---|
[438] | 242 | if (fabs(denom) < 1e-20)
|
---|
| 243 | //if (denom == 0.0f)
|
---|
| 244 | return (mRay->GetOrigin(axis) > position) ? 1 : -1;
|
---|
[382] | 245 |
|
---|
[438] | 246 | t = (position - mRay->GetOrigin(axis))/denom;
|
---|
[382] | 247 |
|
---|
[438] | 248 | if (t < GetMinT())
|
---|
| 249 | return (denom > 0) ? 1 : -1;
|
---|
[382] | 250 |
|
---|
[438] | 251 | if (t > GetMaxT())
|
---|
| 252 | return (denom > 0) ? -1 : 1;
|
---|
[382] | 253 |
|
---|
[438] | 254 | return 0;
|
---|
[446] | 255 | #else
|
---|
| 256 | // subbdivision based only on the origin
|
---|
| 257 | t = 0;
|
---|
| 258 | float rpos = mRay->GetOrigin(axis);
|
---|
| 259 | return (rpos < position) ? -1 : 1;
|
---|
| 260 |
|
---|
| 261 | #endif
|
---|
[382] | 262 | }
|
---|
| 263 |
|
---|
| 264 |
|
---|
[438] | 265 | };
|
---|
[372] | 266 |
|
---|
| 267 |
|
---|
[438] | 268 | typedef vector<RayInfo> RayInfoContainer;
|
---|
[372] | 269 |
|
---|
| 270 | enum { EInterior, ELeaf };
|
---|
| 271 |
|
---|
| 272 | /////////////////////////////////
|
---|
| 273 | // The actual data goes here
|
---|
| 274 |
|
---|
| 275 | // link to the parent
|
---|
| 276 | VssTreeInterior *parent;
|
---|
| 277 |
|
---|
| 278 | enum {SPLIT_X=0, SPLIT_Y, SPLIT_Z, SPLIT_DIRX, SPLIT_DIRY, SPLIT_DIRZ};
|
---|
| 279 |
|
---|
| 280 | // splitting axis
|
---|
| 281 | char axis;
|
---|
| 282 |
|
---|
| 283 | // depth
|
---|
| 284 | unsigned char depth;
|
---|
| 285 |
|
---|
| 286 | // short depth;
|
---|
| 287 | //
|
---|
| 288 | /////////////////////////////////
|
---|
| 289 |
|
---|
| 290 | inline VssTreeNode(VssTreeInterior *p);
|
---|
| 291 |
|
---|
| 292 |
|
---|
| 293 | virtual ~VssTreeNode() {};
|
---|
| 294 | virtual int Type() const = 0;
|
---|
| 295 |
|
---|
| 296 |
|
---|
| 297 | bool IsLeaf() const { return axis == -1; }
|
---|
| 298 |
|
---|
| 299 | virtual void Print(ostream &s) const = 0;
|
---|
| 300 |
|
---|
| 301 | virtual int GetAccessTime() {
|
---|
| 302 | return 0x7FFFFFF;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 |
|
---|
| 306 |
|
---|
| 307 | };
|
---|
| 308 |
|
---|
| 309 | // --------------------------------------------------------------
|
---|
| 310 | // KD-tree node - interior node
|
---|
| 311 | // --------------------------------------------------------------
|
---|
| 312 | class VssTreeInterior :
|
---|
| 313 | public VssTreeNode
|
---|
| 314 | {
|
---|
| 315 | public:
|
---|
| 316 | // plane in local modelling coordinates
|
---|
| 317 | float position;
|
---|
| 318 |
|
---|
| 319 | // pointers to children
|
---|
| 320 | VssTreeNode *back, *front;
|
---|
| 321 |
|
---|
| 322 | // the bbox of the node
|
---|
| 323 | AxisAlignedBox3 bbox;
|
---|
| 324 |
|
---|
| 325 | // the bbox of the node
|
---|
| 326 | AxisAlignedBox3 dirBBox;
|
---|
| 327 |
|
---|
| 328 | // data for caching
|
---|
| 329 | long accesses;
|
---|
| 330 | long lastAccessTime;
|
---|
| 331 |
|
---|
| 332 | VssTreeInterior(VssTreeInterior *p):VssTreeNode(p),
|
---|
[438] | 333 | back(NULL),
|
---|
| 334 | front(NULL),
|
---|
| 335 | accesses(0),
|
---|
| 336 | lastAccessTime(-1)
|
---|
[372] | 337 | { }
|
---|
| 338 |
|
---|
| 339 | virtual int GetAccessTime() {
|
---|
| 340 | return lastAccessTime;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | void SetupChildLinks(VssTreeNode *b, VssTreeNode *f) {
|
---|
| 344 | back = b;
|
---|
| 345 | front = f;
|
---|
| 346 | b->parent = f->parent = this;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | void ReplaceChildLink(VssTreeNode *oldChild, VssTreeNode *newChild) {
|
---|
| 350 | if (back == oldChild)
|
---|
| 351 | back = newChild;
|
---|
| 352 | else
|
---|
| 353 | front = newChild;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | virtual int Type() const { return EInterior; }
|
---|
| 357 |
|
---|
| 358 | virtual ~VssTreeInterior() {
|
---|
| 359 | if (back)
|
---|
| 360 | delete back;
|
---|
| 361 | if (front)
|
---|
| 362 | delete front;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | virtual void Print(ostream &s) const {
|
---|
| 366 | if (axis == 0)
|
---|
| 367 | s<<"x ";
|
---|
| 368 | else
|
---|
| 369 | if (axis == 1)
|
---|
[438] | 370 | s<<"y ";
|
---|
[372] | 371 | else
|
---|
[438] | 372 | s<<"z ";
|
---|
[372] | 373 | s<<position<<" ";
|
---|
| 374 | back->Print(s);
|
---|
| 375 | front->Print(s);
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 |
|
---|
[382] | 379 |
|
---|
[372] | 380 | int ComputeRayIntersection(const RayInfo &rayData,
|
---|
[438] | 381 | float &t
|
---|
| 382 | ) {
|
---|
| 383 | return rayData.ComputeRayIntersection(axis, position, t);
|
---|
[372] | 384 | }
|
---|
| 385 |
|
---|
| 386 | };
|
---|
| 387 |
|
---|
| 388 |
|
---|
| 389 | // --------------------------------------------------------------
|
---|
| 390 | // KD-tree node - leaf node
|
---|
| 391 | // --------------------------------------------------------------
|
---|
| 392 | class VssTreeLeaf :
|
---|
| 393 | public VssTreeNode
|
---|
| 394 | {
|
---|
[395] | 395 | private:
|
---|
[438] | 396 | int mPvsSize;
|
---|
[372] | 397 | public:
|
---|
| 398 | static int mailID;
|
---|
| 399 | int mailbox;
|
---|
| 400 |
|
---|
| 401 | RayInfoContainer rays;
|
---|
[438] | 402 | int mPassingRays;
|
---|
[434] | 403 |
|
---|
[438] | 404 | bool mValidPvs;
|
---|
| 405 | float mEntropyImportance;
|
---|
| 406 |
|
---|
[395] | 407 |
|
---|
[382] | 408 | VssTreeLeaf(VssTreeInterior *p,
|
---|
[438] | 409 | const int nRays
|
---|
| 410 | ):VssTreeNode(p), rays(), mPvsSize(0), mPassingRays(0), mValidPvs(false) {
|
---|
[382] | 411 | rays.reserve(nRays);
|
---|
[372] | 412 | }
|
---|
| 413 |
|
---|
| 414 | virtual ~VssTreeLeaf() { }
|
---|
| 415 |
|
---|
| 416 | virtual int Type() const { return ELeaf; }
|
---|
| 417 |
|
---|
| 418 | virtual void Print(ostream &s) const {
|
---|
[444] | 419 | s<<endl<<"L: r="<<(int)rays.size()<<endl;
|
---|
[372] | 420 | };
|
---|
| 421 |
|
---|
| 422 | void AddRay(const RayInfo &data) {
|
---|
[438] | 423 | mValidPvs = false;
|
---|
[372] | 424 | rays.push_back(data);
|
---|
| 425 | data.mRay->Ref();
|
---|
[438] | 426 | if (data.GetRayClass() == RayInfo::PASSING_RAY)
|
---|
| 427 | mPassingRays++;
|
---|
[372] | 428 | }
|
---|
[395] | 429 |
|
---|
[438] | 430 | int GetPvsSize() const {
|
---|
| 431 | return mPvsSize;
|
---|
| 432 | }
|
---|
[372] | 433 |
|
---|
[438] | 434 | void SetPvsSize(const int s) {
|
---|
| 435 | mPvsSize = s;
|
---|
| 436 | mValidPvs = true;
|
---|
| 437 | }
|
---|
[395] | 438 |
|
---|
[438] | 439 | void
|
---|
| 440 | UpdatePvsSize();
|
---|
| 441 |
|
---|
| 442 | float
|
---|
| 443 | ComputePvsEntropy();
|
---|
| 444 |
|
---|
| 445 | float
|
---|
| 446 | ComputeRayLengthEntropy();
|
---|
| 447 |
|
---|
| 448 | float
|
---|
| 449 | ComputeRayTerminationEntropy();
|
---|
| 450 |
|
---|
| 451 | void
|
---|
| 452 | ComputeEntropyImportance();
|
---|
| 453 |
|
---|
[372] | 454 | void Mail() { mailbox = mailID; }
|
---|
| 455 | static void NewMail() { mailID++; }
|
---|
| 456 | bool Mailed() const { return mailbox == mailID; }
|
---|
| 457 |
|
---|
| 458 | bool Mailed(const int mail) {
|
---|
| 459 | return mailbox >= mailID + mail;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
[438] | 462 | float GetAvgRayContribution() const {
|
---|
| 463 | return GetPvsSize()/((float)rays.size() + Limits::Small);
|
---|
| 464 | }
|
---|
[403] | 465 |
|
---|
[435] | 466 | float GetImportance() const;
|
---|
[438] | 467 |
|
---|
| 468 | float GetSqrRayContribution() const {
|
---|
| 469 | return sqr(GetPvsSize()/((float)rays.size() + Limits::Small));
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | // comparator for the
|
---|
| 473 | struct less_contribution : public
|
---|
| 474 | binary_function<const VssTreeLeaf *, const VssTreeLeaf *, bool> {
|
---|
| 475 |
|
---|
| 476 | bool operator()(const VssTreeLeaf * a, const VssTreeLeaf *b) {
|
---|
| 477 | return a->GetAvgRayContribution() < b->GetAvgRayContribution();
|
---|
[403] | 478 | }
|
---|
[438] | 479 | };
|
---|
| 480 |
|
---|
| 481 | struct greater_contribution : public
|
---|
| 482 | binary_function<const VssTreeLeaf *, const VssTreeLeaf *, bool> {
|
---|
[427] | 483 |
|
---|
[438] | 484 | bool operator()(const VssTreeLeaf * a, const VssTreeLeaf *b) {
|
---|
| 485 | return a->GetAvgRayContribution() > b->GetAvgRayContribution();
|
---|
[427] | 486 | }
|
---|
[438] | 487 | };
|
---|
| 488 |
|
---|
| 489 | friend bool GreaterContribution(const VssTreeLeaf * a, const VssTreeLeaf *b) {
|
---|
| 490 | return a->GetAvgRayContribution() > b->GetAvgRayContribution();
|
---|
| 491 | }
|
---|
| 492 |
|
---|
[372] | 493 | };
|
---|
| 494 |
|
---|
| 495 | // Inline functions
|
---|
| 496 | inline
|
---|
| 497 | VssTreeNode::VssTreeNode(VssTreeInterior *p):
|
---|
| 498 | parent(p), axis(-1), depth(p ? p->depth + 1 : 0) {}
|
---|
| 499 |
|
---|
| 500 |
|
---|
| 501 |
|
---|
| 502 | // ---------------------------------------------------------------
|
---|
| 503 | // Main LSDS search class
|
---|
| 504 | // ---------------------------------------------------------------
|
---|
| 505 | class VssTree
|
---|
| 506 | {
|
---|
| 507 | struct TraversalData
|
---|
| 508 | {
|
---|
| 509 | VssTreeNode *node;
|
---|
| 510 | AxisAlignedBox3 bbox;
|
---|
| 511 | int depth;
|
---|
| 512 | float priority;
|
---|
| 513 |
|
---|
| 514 | TraversalData() {}
|
---|
| 515 |
|
---|
| 516 | TraversalData(VssTreeNode *n, const float p):
|
---|
| 517 | node(n), priority(p)
|
---|
| 518 | {}
|
---|
| 519 |
|
---|
| 520 | TraversalData(VssTreeNode *n,
|
---|
[438] | 521 | const AxisAlignedBox3 &b,
|
---|
| 522 | const int d):
|
---|
[372] | 523 | node(n), bbox(b), depth(d) {}
|
---|
| 524 |
|
---|
| 525 |
|
---|
| 526 | // comparator for the
|
---|
| 527 | struct less_priority : public
|
---|
| 528 | binary_function<const TraversalData, const TraversalData, bool> {
|
---|
| 529 |
|
---|
| 530 | bool operator()(const TraversalData a, const TraversalData b) {
|
---|
[438] | 531 | return a.priority < b.priority;
|
---|
[372] | 532 | }
|
---|
| 533 |
|
---|
| 534 | };
|
---|
| 535 |
|
---|
| 536 | // ~TraversalData() {}
|
---|
| 537 | // TraversalData(const TraversalData &s):node(s.node), bbox(s.bbox), depth(s.depth) {}
|
---|
| 538 |
|
---|
| 539 | friend bool operator<(const TraversalData &a,
|
---|
[438] | 540 | const TraversalData &b) {
|
---|
[372] | 541 | // return a.node->queries.size() < b.node->queries.size();
|
---|
| 542 | VssTreeLeaf *leafa = (VssTreeLeaf *) a.node;
|
---|
| 543 | VssTreeLeaf *leafb = (VssTreeLeaf *) b.node;
|
---|
[386] | 544 | #if 0
|
---|
[438] | 545 | return
|
---|
| 546 | leafa->rays.size()*a.bbox.GetVolume()
|
---|
| 547 | <
|
---|
| 548 | leafb->rays.size()*b.bbox.GetVolume();
|
---|
[386] | 549 | #endif
|
---|
[434] | 550 | #if 0
|
---|
[438] | 551 | return
|
---|
| 552 | leafa->GetPvsSize()*a.bbox.GetVolume()
|
---|
| 553 | <
|
---|
| 554 | leafb->GetPvsSize()*b.bbox.GetVolume();
|
---|
[386] | 555 | #endif
|
---|
| 556 | #if 0
|
---|
[438] | 557 | return
|
---|
| 558 | leafa->GetPvsSize()
|
---|
| 559 | <
|
---|
| 560 | leafb->GetPvsSize();
|
---|
[386] | 561 | #endif
|
---|
[387] | 562 | #if 0
|
---|
[438] | 563 | return
|
---|
| 564 | leafa->GetPvsSize()/(leafa->rays.size()+1)
|
---|
| 565 | >
|
---|
| 566 | leafb->GetPvsSize()/(leafb->rays.size()+1);
|
---|
[386] | 567 | #endif
|
---|
[434] | 568 | #if 1
|
---|
[438] | 569 | return
|
---|
| 570 | leafa->GetPvsSize()*leafa->rays.size()
|
---|
| 571 | <
|
---|
| 572 | leafb->GetPvsSize()*leafb->rays.size();
|
---|
[386] | 573 | #endif
|
---|
[372] | 574 | }
|
---|
| 575 | };
|
---|
| 576 |
|
---|
| 577 | // simplified data for ray traversal only...
|
---|
| 578 |
|
---|
| 579 | struct RayTraversalData {
|
---|
| 580 |
|
---|
| 581 | VssTreeNode::RayInfo rayData;
|
---|
| 582 | VssTreeNode *node;
|
---|
| 583 |
|
---|
| 584 | RayTraversalData() {}
|
---|
| 585 | RayTraversalData(VssTreeNode *n,
|
---|
[438] | 586 | const VssTreeNode::RayInfo &data):
|
---|
[372] | 587 | rayData(data), node(n) {}
|
---|
| 588 | };
|
---|
| 589 |
|
---|
| 590 | public:
|
---|
| 591 | /////////////////////////////
|
---|
| 592 | // The core pointer
|
---|
| 593 | VssTreeNode *root;
|
---|
| 594 |
|
---|
| 595 | /////////////////////////////
|
---|
| 596 | // Basic properties
|
---|
| 597 |
|
---|
| 598 | // total number of nodes of the tree
|
---|
| 599 | int nodes;
|
---|
| 600 | // axis aligned bounding box of the scene
|
---|
| 601 | AxisAlignedBox3 bbox;
|
---|
| 602 |
|
---|
| 603 | // axis aligned bounding box of directions
|
---|
| 604 | AxisAlignedBox3 dirBBox;
|
---|
| 605 |
|
---|
| 606 | /////////////////////////////
|
---|
| 607 | // Construction parameters
|
---|
| 608 |
|
---|
| 609 | // epsilon used for the construction
|
---|
| 610 | float epsilon;
|
---|
| 611 |
|
---|
| 612 | // ratio between traversal and intersection costs
|
---|
| 613 | float ct_div_ci;
|
---|
| 614 | // max depth of the tree
|
---|
| 615 | int termMaxDepth;
|
---|
| 616 | // minimal ratio of the volume of the cell and the query volume
|
---|
| 617 | float termMinSize;
|
---|
| 618 |
|
---|
[438] | 619 | // minimal pvs per node to still get subdivided
|
---|
[382] | 620 | int termMinPvs;
|
---|
| 621 |
|
---|
[438] | 622 | // minimal ray number per node to still get subdivided
|
---|
[403] | 623 | int termMinRays;
|
---|
| 624 |
|
---|
[382] | 625 | // maximal cost ration to subdivide a node
|
---|
| 626 | float termMaxCostRatio;
|
---|
| 627 |
|
---|
[438] | 628 | // maximal contribution per ray to subdivide the node
|
---|
| 629 | float termMaxRayContribution;
|
---|
[382] | 630 |
|
---|
| 631 |
|
---|
[372] | 632 | // randomized construction
|
---|
| 633 | bool randomize;
|
---|
[438] | 634 |
|
---|
[372] | 635 | // type of the splitting to use fo rthe tree construction
|
---|
[434] | 636 | enum {ESplitRegular, ESplitHeuristic, ESplitHybrid };
|
---|
[372] | 637 | int splitType;
|
---|
[434] | 638 |
|
---|
[438] | 639 | bool mSplitUseOnlyDrivingAxis;
|
---|
| 640 |
|
---|
| 641 | // use ray space subdivision instead of view space subdivision
|
---|
| 642 | bool mUseRss;
|
---|
[434] | 643 |
|
---|
[438] | 644 | // interleave directional and spatial splits based on their costs
|
---|
| 645 | // if false directional splits are only performed after spatial splits
|
---|
| 646 | bool mInterleaveDirSplits;
|
---|
| 647 |
|
---|
| 648 | // depth at which directional splits are performed if mInterleaveDirSplits is false
|
---|
| 649 | int mDirSplitDepth;
|
---|
| 650 |
|
---|
[372] | 651 | // maximal size of the box on which the refdir splitting can be performed
|
---|
| 652 | // (relative to the scene bbox
|
---|
| 653 | float refDirBoxMaxSize;
|
---|
| 654 |
|
---|
| 655 | // maximum alovable memory in MB
|
---|
| 656 | float maxTotalMemory;
|
---|
| 657 |
|
---|
| 658 | // maximum alovable memory for static kd tree in MB
|
---|
| 659 | float maxStaticMemory;
|
---|
| 660 |
|
---|
| 661 | // this is used during the construction depending
|
---|
| 662 | // on the type of the tree and queries...
|
---|
| 663 | float maxMemory;
|
---|
| 664 |
|
---|
| 665 |
|
---|
| 666 | // minimal acess time for collapse
|
---|
| 667 | int accessTimeThreshold;
|
---|
| 668 |
|
---|
[438] | 669 | // minimal depth at which to perform collapse
|
---|
[372] | 670 | int minCollapseDepth;
|
---|
| 671 |
|
---|
| 672 |
|
---|
| 673 | // reusable array of split candidates
|
---|
[382] | 674 | vector<SortableEntry> *splitCandidates;
|
---|
[372] | 675 | /////////////////////////////
|
---|
| 676 |
|
---|
[438] | 677 | VssStatistics stat;
|
---|
[372] | 678 |
|
---|
| 679 |
|
---|
| 680 | VssTree();
|
---|
| 681 | virtual ~VssTree();
|
---|
| 682 |
|
---|
| 683 | virtual void
|
---|
| 684 | Construct(
|
---|
[438] | 685 | VssRayContainer &rays,
|
---|
| 686 | AxisAlignedBox3 *forcedBoundingBox = NULL
|
---|
| 687 | );
|
---|
[372] | 688 |
|
---|
| 689 | // incemental construction
|
---|
| 690 | virtual void UpdateRays(VssRayContainer &remove,
|
---|
[438] | 691 | VssRayContainer &add
|
---|
| 692 | );
|
---|
[401] | 693 |
|
---|
[438] | 694 | virtual void AddRays(
|
---|
| 695 | VssRayContainer &add
|
---|
| 696 | )
|
---|
| 697 | {
|
---|
| 698 | VssRayContainer remove;
|
---|
| 699 | UpdateRays(remove, add);
|
---|
| 700 | }
|
---|
[401] | 701 |
|
---|
[372] | 702 |
|
---|
| 703 |
|
---|
| 704 | VssTreeNode *
|
---|
| 705 | Locate(const Vector3 &v);
|
---|
| 706 |
|
---|
| 707 | VssTreeNode *
|
---|
| 708 | SubdivideNode(VssTreeLeaf *leaf,
|
---|
[438] | 709 | const AxisAlignedBox3 &box,
|
---|
| 710 | AxisAlignedBox3 &backBox,
|
---|
| 711 | AxisAlignedBox3 &frontBox
|
---|
| 712 | );
|
---|
[372] | 713 |
|
---|
| 714 | VssTreeNode *
|
---|
| 715 | Subdivide(const TraversalData &tdata);
|
---|
| 716 |
|
---|
| 717 | int
|
---|
| 718 | SelectPlane(VssTreeLeaf *leaf,
|
---|
[438] | 719 | const AxisAlignedBox3 &box,
|
---|
| 720 | float &position,
|
---|
| 721 | int &raysBack,
|
---|
| 722 | int &raysFront,
|
---|
| 723 | int &pvsBack,
|
---|
| 724 | int &pvsFront
|
---|
| 725 | );
|
---|
[372] | 726 |
|
---|
| 727 | void
|
---|
| 728 | SortSplitCandidates(
|
---|
[438] | 729 | VssTreeLeaf *node,
|
---|
| 730 | const int axis
|
---|
| 731 | );
|
---|
[372] | 732 |
|
---|
| 733 |
|
---|
| 734 | // return memory usage in MB
|
---|
| 735 | float GetMemUsage() const {
|
---|
| 736 | return
|
---|
| 737 | (sizeof(VssTree) +
|
---|
| 738 | stat.Leaves()*sizeof(VssTreeLeaf) +
|
---|
| 739 | stat.Interior()*sizeof(VssTreeInterior) +
|
---|
| 740 | stat.rayRefs*sizeof(VssTreeNode::RayInfo))/(1024.0f*1024.0f);
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | float GetRayMemUsage() const {
|
---|
| 744 | return
|
---|
| 745 | stat.rays*(sizeof(VssRay))/(1024.0f*1024.0f);
|
---|
| 746 | }
|
---|
| 747 |
|
---|
[386] | 748 |
|
---|
[438] | 749 | float
|
---|
| 750 | BestCostRatio(
|
---|
| 751 | VssTreeLeaf *node,
|
---|
| 752 | int &axis,
|
---|
| 753 | float &position,
|
---|
| 754 | int &raysBack,
|
---|
| 755 | int &raysFront,
|
---|
| 756 | int &pvsBack,
|
---|
| 757 | int &pvsFront
|
---|
| 758 | );
|
---|
[382] | 759 |
|
---|
[438] | 760 | float
|
---|
| 761 | EvalCostRatio(
|
---|
| 762 | VssTreeLeaf *node,
|
---|
| 763 | const int axis,
|
---|
| 764 | const float position,
|
---|
| 765 | int &raysBack,
|
---|
| 766 | int &raysFront,
|
---|
| 767 | int &pvsBack,
|
---|
| 768 | int &pvsFront
|
---|
| 769 | );
|
---|
[372] | 770 |
|
---|
[438] | 771 | float
|
---|
| 772 | EvalCostRatioHeuristic(
|
---|
| 773 | VssTreeLeaf *node,
|
---|
| 774 | const int axis,
|
---|
| 775 | float &position,
|
---|
| 776 | int &raysBack,
|
---|
| 777 | int &raysFront,
|
---|
| 778 | int &pvsBack,
|
---|
| 779 | int &pvsFront
|
---|
| 780 | );
|
---|
[434] | 781 |
|
---|
[438] | 782 | float
|
---|
| 783 | GetCostRatio(
|
---|
| 784 | VssTreeLeaf *leaf,
|
---|
| 785 | const int axis,
|
---|
| 786 | const float position,
|
---|
| 787 | const int raysBack,
|
---|
| 788 | const int raysFront,
|
---|
| 789 | const int pvsBack,
|
---|
| 790 | const int pvsFront
|
---|
| 791 | );
|
---|
[434] | 792 |
|
---|
| 793 | AxisAlignedBox3 GetBBox(const VssTreeNode *node) const {
|
---|
[372] | 794 | if (node->parent == NULL)
|
---|
| 795 | return bbox;
|
---|
| 796 |
|
---|
| 797 | if (!node->IsLeaf())
|
---|
| 798 | return ((VssTreeInterior *)node)->bbox;
|
---|
| 799 |
|
---|
| 800 | if (node->parent->axis >= 3)
|
---|
| 801 | return node->parent->bbox;
|
---|
| 802 |
|
---|
| 803 | AxisAlignedBox3 box(node->parent->bbox);
|
---|
| 804 | if (node->parent->front == node)
|
---|
| 805 | box.SetMin(node->parent->axis, node->parent->position);
|
---|
| 806 | else
|
---|
| 807 | box.SetMax(node->parent->axis, node->parent->position);
|
---|
| 808 | return box;
|
---|
| 809 | }
|
---|
| 810 |
|
---|
[434] | 811 | AxisAlignedBox3 GetDirBBox(const VssTreeNode *node) const {
|
---|
[372] | 812 |
|
---|
| 813 | if (node->parent == NULL)
|
---|
| 814 | return dirBBox;
|
---|
| 815 |
|
---|
| 816 | if (!node->IsLeaf() )
|
---|
| 817 | return ((VssTreeInterior *)node)->dirBBox;
|
---|
| 818 |
|
---|
| 819 | if (node->parent->axis < 3)
|
---|
| 820 | return node->parent->dirBBox;
|
---|
| 821 |
|
---|
| 822 | AxisAlignedBox3 dBBox(node->parent->dirBBox);
|
---|
| 823 |
|
---|
| 824 | if (node->parent->front == node)
|
---|
| 825 | dBBox.SetMin(node->parent->axis - 3, node->parent->position);
|
---|
| 826 | else
|
---|
| 827 | dBBox.SetMax(node->parent->axis - 3, node->parent->position);
|
---|
| 828 | return dBBox;
|
---|
| 829 | }
|
---|
| 830 |
|
---|
| 831 | int
|
---|
| 832 | ReleaseMemory(const int time);
|
---|
| 833 |
|
---|
| 834 | int
|
---|
| 835 | CollapseSubtree(VssTreeNode *node, const int time);
|
---|
| 836 |
|
---|
| 837 | void
|
---|
| 838 | CountAccess(VssTreeInterior *node, const long time) {
|
---|
| 839 | node->accesses++;
|
---|
| 840 | node->lastAccessTime = time;
|
---|
| 841 | }
|
---|
| 842 |
|
---|
| 843 | VssTreeNode *
|
---|
| 844 | SubdivideLeaf(
|
---|
[438] | 845 | VssTreeLeaf *leaf
|
---|
| 846 | );
|
---|
[372] | 847 |
|
---|
| 848 | void
|
---|
| 849 | RemoveRay(VssRay *ray,
|
---|
[438] | 850 | vector<VssTreeLeaf *> *affectedLeaves,
|
---|
| 851 | const bool removeAllScheduledRays
|
---|
| 852 | );
|
---|
[372] | 853 |
|
---|
[438] | 854 | // void
|
---|
| 855 | // AddRay(VssRay *ray);
|
---|
[372] | 856 | void
|
---|
[438] | 857 | AddRay(VssTreeNode::RayInfo &info);
|
---|
| 858 |
|
---|
| 859 | void
|
---|
[372] | 860 | TraverseInternalNode(
|
---|
[438] | 861 | RayTraversalData &data,
|
---|
| 862 | stack<RayTraversalData> &tstack);
|
---|
[372] | 863 |
|
---|
[438] | 864 | void
|
---|
[372] | 865 | EvaluateLeafStats(const TraversalData &data);
|
---|
| 866 |
|
---|
[382] | 867 |
|
---|
[438] | 868 | int
|
---|
| 869 | GetRootPvsSize() const {
|
---|
| 870 | return GetPvsSize(bbox);
|
---|
| 871 | }
|
---|
| 872 |
|
---|
| 873 | int
|
---|
| 874 | GetPvsSize(const AxisAlignedBox3 &box) const;
|
---|
[401] | 875 |
|
---|
[438] | 876 | void
|
---|
| 877 | GetRayContributionStatistics(
|
---|
| 878 | float &minRayContribution,
|
---|
| 879 | float &maxRayContribution,
|
---|
| 880 | float &avgRayContribution
|
---|
| 881 | );
|
---|
[401] | 882 |
|
---|
[438] | 883 | int
|
---|
| 884 | GenerateRays(const float ratioPerLeaf,
|
---|
| 885 | SimpleRayContainer &rays);
|
---|
[401] | 886 |
|
---|
[438] | 887 | int
|
---|
| 888 | GenerateRays(const int numberOfRays,
|
---|
| 889 | const int numberOfLeaves,
|
---|
| 890 | SimpleRayContainer &rays);
|
---|
[427] | 891 |
|
---|
[438] | 892 | float
|
---|
| 893 | GetAvgPvsSize();
|
---|
[401] | 894 |
|
---|
[438] | 895 | int
|
---|
| 896 | UpdateSubdivision();
|
---|
[427] | 897 |
|
---|
[438] | 898 | bool
|
---|
| 899 | TerminationCriteriaSatisfied(VssTreeLeaf *leaf);
|
---|
[427] | 900 |
|
---|
[438] | 901 | void
|
---|
| 902 | CollectLeaves(vector<VssTreeLeaf *> &leaves);
|
---|
[427] | 903 |
|
---|
[438] | 904 | bool
|
---|
| 905 | ClipRay(
|
---|
| 906 | VssTreeNode::RayInfo &rayInfo,
|
---|
| 907 | const AxisAlignedBox3 &box
|
---|
| 908 | );
|
---|
[427] | 909 |
|
---|
[438] | 910 | VssTreeNode *GetRoot() const { return root; }
|
---|
[434] | 911 |
|
---|
[438] | 912 | bool
|
---|
| 913 | ValidLeaf(VssTreeLeaf *leaf) const;
|
---|
[434] | 914 |
|
---|
[438] | 915 | void
|
---|
| 916 | GenerateLeafRays(VssTreeLeaf *leaf,
|
---|
| 917 | const int numberOfRays,
|
---|
| 918 | SimpleRayContainer &rays);
|
---|
[434] | 919 |
|
---|
[466] | 920 | int
|
---|
| 921 | CollectRays(VssRayContainer &rays,
|
---|
| 922 | const int number);
|
---|
[434] | 923 |
|
---|
[466] | 924 | int
|
---|
| 925 | CollectRays(VssRayContainer &rays
|
---|
| 926 | );
|
---|
| 927 |
|
---|
[372] | 928 | };
|
---|
| 929 |
|
---|
| 930 |
|
---|
| 931 | #endif // __LSDS_KDTREE_H__
|
---|
| 932 |
|
---|