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