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