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