1 | // ================================================================
|
---|
2 | // $Id$
|
---|
3 | // ****************************************************************
|
---|
4 | //
|
---|
5 | // Initial coding by
|
---|
6 | /**
|
---|
7 | @author Jiri Bittner
|
---|
8 | */
|
---|
9 |
|
---|
10 | #ifndef __VSSTREE_H__
|
---|
11 | #define __VSSTREE_H__
|
---|
12 |
|
---|
13 | // Standard headers
|
---|
14 | #include <iomanip>
|
---|
15 | #include <vector>
|
---|
16 | #include <functional>
|
---|
17 | #include <stack>
|
---|
18 |
|
---|
19 |
|
---|
20 | // User headers
|
---|
21 | #include "VssRay.h"
|
---|
22 | #include "AxisAlignedBox3.h"
|
---|
23 | #include "Statistics.h"
|
---|
24 | #include "Ray.h"
|
---|
25 |
|
---|
26 | namespace GtpVisibilityPreprocessor {
|
---|
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;
|
---|
47 | // initial size of the pvs
|
---|
48 | int initialPvsSize;
|
---|
49 | // total number of query domains
|
---|
50 | int queryDomains;
|
---|
51 | // total number of ray references
|
---|
52 | int rayRefs;
|
---|
53 |
|
---|
54 | // max depth nodes
|
---|
55 | int maxDepthNodes;
|
---|
56 | // max depth nodes
|
---|
57 | int minPvsNodes;
|
---|
58 | int minRaysNodes;
|
---|
59 | // max ray contribution nodes
|
---|
60 | int maxRayContribNodes;
|
---|
61 | // max depth nodes
|
---|
62 | int minSizeNodes;
|
---|
63 | int maxCostRatioNodes;
|
---|
64 |
|
---|
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;
|
---|
86 | rayRefs = 0;
|
---|
87 | maxDepthNodes = 0;
|
---|
88 | minPvsNodes = 0;
|
---|
89 | minRaysNodes = 0;
|
---|
90 | maxRayRefs = 0;
|
---|
91 | addedRayRefs = removedRayRefs = 0;
|
---|
92 | initialPvsSize = 0;
|
---|
93 | maxRayContribNodes = 0;
|
---|
94 | minSizeNodes = 0;
|
---|
95 | maxCostRatioNodes = 0;
|
---|
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 |
|
---|
122 | #define USE_FIXEDPOINT_T 0
|
---|
123 |
|
---|
124 | struct RayInfo
|
---|
125 | {
|
---|
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
|
---|
141 | // mMaxT(0xFFFF)
|
---|
142 | mMaxT(FIXEDPOINT_ONE)
|
---|
143 | #else
|
---|
144 | mMaxT(1.0f)
|
---|
145 | #endif
|
---|
146 | {}
|
---|
147 |
|
---|
148 | RayInfo(VssRay *r,
|
---|
149 | const float _min,
|
---|
150 | const float _max
|
---|
151 | ):mRay(r) {
|
---|
152 | SetMinT(_min);
|
---|
153 | SetMaxT(_max);
|
---|
154 | }
|
---|
155 |
|
---|
156 | RayInfo(VssRay *r,
|
---|
157 | const short _min,
|
---|
158 | const float _max
|
---|
159 | ):mRay(r), mMinT(_min) {
|
---|
160 | SetMaxT(_max);
|
---|
161 | }
|
---|
162 |
|
---|
163 | RayInfo(VssRay *r,
|
---|
164 | const float _min,
|
---|
165 | const short _max
|
---|
166 | ):mRay(r), mMaxT(_max) {
|
---|
167 | SetMinT(_min);
|
---|
168 | }
|
---|
169 |
|
---|
170 | enum {
|
---|
171 | SOURCE_RAY = 0,
|
---|
172 | TERMINATION_RAY,
|
---|
173 | PASSING_RAY,
|
---|
174 | CONTAINED_RAY
|
---|
175 | };
|
---|
176 |
|
---|
177 | int GetRayClass() const {
|
---|
178 |
|
---|
179 | bool startsBefore = GetMinT() > 0.0f;
|
---|
180 | bool terminatesAfter = GetMaxT() < 1.0f;
|
---|
181 |
|
---|
182 | if (startsBefore && terminatesAfter)
|
---|
183 | return PASSING_RAY;
|
---|
184 |
|
---|
185 | if (!startsBefore && !terminatesAfter)
|
---|
186 | return CONTAINED_RAY;
|
---|
187 |
|
---|
188 | if (!startsBefore)
|
---|
189 | return SOURCE_RAY;
|
---|
190 |
|
---|
191 | return TERMINATION_RAY;
|
---|
192 | }
|
---|
193 |
|
---|
194 | friend bool operator<(const RayInfo &a, const RayInfo &b) {
|
---|
195 | return a.mRay < b.mRay;
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | float ExtrapOrigin(const int axis) const {
|
---|
200 | return mRay->GetOrigin(axis) + GetMinT()*mRay->GetDir(axis);
|
---|
201 | }
|
---|
202 |
|
---|
203 | float ExtrapTermination(const int axis) const {
|
---|
204 | return mRay->GetOrigin(axis) + GetMaxT()*mRay->GetDir(axis);
|
---|
205 | }
|
---|
206 |
|
---|
207 | Vector3 Extrap(const float t) const {
|
---|
208 | return mRay->Extrap(t);
|
---|
209 | }
|
---|
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) {
|
---|
216 | mMinT = (short) (t*(float)(FIXEDPOINT_ONE));
|
---|
217 | }
|
---|
218 |
|
---|
219 | void SetMaxT (const float t) {
|
---|
220 | mMaxT = (short) (t*(float)(FIXEDPOINT_ONE));
|
---|
221 | mMaxT++;
|
---|
222 | // if (mMaxT!=0xFFFF)
|
---|
223 | // mMaxT++;
|
---|
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
|
---|
232 |
|
---|
233 |
|
---|
234 | int ComputeRayIntersection(const int axis,
|
---|
235 | const float position,
|
---|
236 | float &t
|
---|
237 | ) const {
|
---|
238 |
|
---|
239 | #if 1
|
---|
240 | // intersect the ray with the plane
|
---|
241 | float denom = mRay->GetDir(axis);
|
---|
242 |
|
---|
243 | if (fabs(denom) < 1e-20)
|
---|
244 | //if (denom == 0.0f)
|
---|
245 | return (mRay->GetOrigin(axis) > position) ? 1 : -1;
|
---|
246 |
|
---|
247 | t = (position - mRay->GetOrigin(axis))/denom;
|
---|
248 |
|
---|
249 | if (t < GetMinT())
|
---|
250 | return (denom > 0) ? 1 : -1;
|
---|
251 |
|
---|
252 | if (t > GetMaxT())
|
---|
253 | return (denom > 0) ? -1 : 1;
|
---|
254 |
|
---|
255 | return 0;
|
---|
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
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | };
|
---|
267 |
|
---|
268 | void GetSampleData(const bool isTerminaton,
|
---|
269 | Vector3 &pt,
|
---|
270 | Intersectable **obj,
|
---|
271 | KdNode **node) const;
|
---|
272 |
|
---|
273 | typedef vector<RayInfo> RayInfoContainer;
|
---|
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),
|
---|
338 | back(NULL),
|
---|
339 | front(NULL),
|
---|
340 | accesses(0),
|
---|
341 | lastAccessTime(-1)
|
---|
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)
|
---|
375 | s<<"y ";
|
---|
376 | else
|
---|
377 | s<<"z ";
|
---|
378 | s<<position<<" ";
|
---|
379 | back->Print(s);
|
---|
380 | front->Print(s);
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 |
|
---|
385 | int ComputeRayIntersection(const RayInfo &rayData,
|
---|
386 | float &t
|
---|
387 | ) {
|
---|
388 | return rayData.ComputeRayIntersection(axis, position, t);
|
---|
389 | }
|
---|
390 |
|
---|
391 | };
|
---|
392 |
|
---|
393 |
|
---|
394 | // --------------------------------------------------------------
|
---|
395 | // KD-tree node - leaf node
|
---|
396 | // --------------------------------------------------------------
|
---|
397 | class VssTreeLeaf :
|
---|
398 | public VssTreeNode
|
---|
399 | {
|
---|
400 | private:
|
---|
401 | int mPvsSize;
|
---|
402 | public:
|
---|
403 | static int mailID;
|
---|
404 | int mailbox;
|
---|
405 |
|
---|
406 | RayInfoContainer rays;
|
---|
407 | int mPassingRays;
|
---|
408 |
|
---|
409 | bool mValidPvs;
|
---|
410 | float mEntropyImportance;
|
---|
411 |
|
---|
412 |
|
---|
413 | VssTreeLeaf(VssTreeInterior *p,
|
---|
414 | const int nRays
|
---|
415 | ):VssTreeNode(p), rays(), mPvsSize(0), mPassingRays(0), mValidPvs(false) {
|
---|
416 | rays.reserve(nRays);
|
---|
417 | }
|
---|
418 |
|
---|
419 | virtual ~VssTreeLeaf() { }
|
---|
420 |
|
---|
421 | virtual int Type() const { return ELeaf; }
|
---|
422 |
|
---|
423 | virtual void Print(ostream &s) const {
|
---|
424 | s<<endl<<"L: r="<<(int)rays.size()<<endl;
|
---|
425 | };
|
---|
426 |
|
---|
427 | void AddRay(const RayInfo &data) {
|
---|
428 | mValidPvs = false;
|
---|
429 | rays.push_back(data);
|
---|
430 | data.mRay->Ref();
|
---|
431 | if (data.GetRayClass() == RayInfo::PASSING_RAY)
|
---|
432 | mPassingRays++;
|
---|
433 | }
|
---|
434 |
|
---|
435 | int GetPvsSize() const {
|
---|
436 | return mPvsSize;
|
---|
437 | }
|
---|
438 |
|
---|
439 | void SetPvsSize(const int s) {
|
---|
440 | mPvsSize = s;
|
---|
441 | mValidPvs = true;
|
---|
442 | }
|
---|
443 |
|
---|
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 |
|
---|
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 |
|
---|
467 | float GetAvgRayContribution() const {
|
---|
468 | return GetPvsSize()/((float)rays.size() + Limits::Small);
|
---|
469 | }
|
---|
470 |
|
---|
471 | float GetImportance() const;
|
---|
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();
|
---|
483 | }
|
---|
484 | };
|
---|
485 |
|
---|
486 | struct greater_contribution : public
|
---|
487 | binary_function<const VssTreeLeaf *, const VssTreeLeaf *, bool> {
|
---|
488 |
|
---|
489 | bool operator()(const VssTreeLeaf * a, const VssTreeLeaf *b) {
|
---|
490 | return a->GetAvgRayContribution() > b->GetAvgRayContribution();
|
---|
491 | }
|
---|
492 | };
|
---|
493 |
|
---|
494 | friend bool GreaterContribution(const VssTreeLeaf * a, const VssTreeLeaf *b) {
|
---|
495 | return a->GetAvgRayContribution() > b->GetAvgRayContribution();
|
---|
496 | }
|
---|
497 |
|
---|
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,
|
---|
526 | const AxisAlignedBox3 &b,
|
---|
527 | const int d):
|
---|
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) {
|
---|
536 | return a.priority < b.priority;
|
---|
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,
|
---|
545 | const TraversalData &b) {
|
---|
546 | // return a.node->queries.size() < b.node->queries.size();
|
---|
547 | VssTreeLeaf *leafa = (VssTreeLeaf *) a.node;
|
---|
548 | VssTreeLeaf *leafb = (VssTreeLeaf *) b.node;
|
---|
549 | #if 0
|
---|
550 | return
|
---|
551 | leafa->rays.size()*a.bbox.GetVolume()
|
---|
552 | <
|
---|
553 | leafb->rays.size()*b.bbox.GetVolume();
|
---|
554 | #endif
|
---|
555 | #if 0
|
---|
556 | return
|
---|
557 | leafa->GetPvsSize()*a.bbox.GetVolume()
|
---|
558 | <
|
---|
559 | leafb->GetPvsSize()*b.bbox.GetVolume();
|
---|
560 | #endif
|
---|
561 | #if 0
|
---|
562 | return
|
---|
563 | leafa->GetPvsSize()
|
---|
564 | <
|
---|
565 | leafb->GetPvsSize();
|
---|
566 | #endif
|
---|
567 | #if 0
|
---|
568 | return
|
---|
569 | leafa->GetPvsSize()/(leafa->rays.size()+1)
|
---|
570 | >
|
---|
571 | leafb->GetPvsSize()/(leafb->rays.size()+1);
|
---|
572 | #endif
|
---|
573 | #if 1
|
---|
574 | return
|
---|
575 | leafa->GetPvsSize()*leafa->rays.size()
|
---|
576 | <
|
---|
577 | leafb->GetPvsSize()*leafb->rays.size();
|
---|
578 | #endif
|
---|
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,
|
---|
591 | const VssTreeNode::RayInfo &data):
|
---|
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 |
|
---|
624 | // minimal pvs per node to still get subdivided
|
---|
625 | int termMinPvs;
|
---|
626 |
|
---|
627 | // minimal ray number per node to still get subdivided
|
---|
628 | int termMinRays;
|
---|
629 |
|
---|
630 | // maximal cost ration to subdivide a node
|
---|
631 | float termMaxCostRatio;
|
---|
632 |
|
---|
633 | // maximal contribution per ray to subdivide the node
|
---|
634 | float termMaxRayContribution;
|
---|
635 |
|
---|
636 |
|
---|
637 | // randomized construction
|
---|
638 | bool randomize;
|
---|
639 |
|
---|
640 | // type of the splitting to use fo rthe tree construction
|
---|
641 | enum {ESplitRegular, ESplitHeuristic, ESplitHybrid };
|
---|
642 | int splitType;
|
---|
643 |
|
---|
644 | bool mSplitUseOnlyDrivingAxis;
|
---|
645 |
|
---|
646 | // use ray space subdivision instead of view space subdivision
|
---|
647 | bool mUseRss;
|
---|
648 |
|
---|
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 |
|
---|
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 |
|
---|
674 | // minimal depth at which to perform collapse
|
---|
675 | int minCollapseDepth;
|
---|
676 |
|
---|
677 |
|
---|
678 | // reusable array of split candidates
|
---|
679 | vector<SortableEntry> *splitCandidates;
|
---|
680 | /////////////////////////////
|
---|
681 |
|
---|
682 | VssStatistics stat;
|
---|
683 |
|
---|
684 |
|
---|
685 | VssTree();
|
---|
686 | virtual ~VssTree();
|
---|
687 |
|
---|
688 | virtual void
|
---|
689 | Construct(
|
---|
690 | VssRayContainer &rays,
|
---|
691 | AxisAlignedBox3 *forcedBoundingBox = NULL
|
---|
692 | );
|
---|
693 |
|
---|
694 | // incemental construction
|
---|
695 | virtual void UpdateRays(VssRayContainer &remove,
|
---|
696 | VssRayContainer &add
|
---|
697 | );
|
---|
698 |
|
---|
699 | virtual void AddRays(
|
---|
700 | VssRayContainer &add
|
---|
701 | )
|
---|
702 | {
|
---|
703 | VssRayContainer remove;
|
---|
704 | UpdateRays(remove, add);
|
---|
705 | }
|
---|
706 |
|
---|
707 |
|
---|
708 |
|
---|
709 | VssTreeNode *
|
---|
710 | Locate(const Vector3 &v);
|
---|
711 |
|
---|
712 | VssTreeNode *
|
---|
713 | SubdivideNode(VssTreeLeaf *leaf,
|
---|
714 | const AxisAlignedBox3 &box,
|
---|
715 | AxisAlignedBox3 &backBox,
|
---|
716 | AxisAlignedBox3 &frontBox
|
---|
717 | );
|
---|
718 |
|
---|
719 | VssTreeNode *
|
---|
720 | Subdivide(const TraversalData &tdata);
|
---|
721 |
|
---|
722 | int
|
---|
723 | SelectPlane(VssTreeLeaf *leaf,
|
---|
724 | const AxisAlignedBox3 &box,
|
---|
725 | float &position,
|
---|
726 | int &raysBack,
|
---|
727 | int &raysFront,
|
---|
728 | int &pvsBack,
|
---|
729 | int &pvsFront
|
---|
730 | );
|
---|
731 |
|
---|
732 | void
|
---|
733 | SortSubdivisionCandidates(
|
---|
734 | VssTreeLeaf *node,
|
---|
735 | const int axis
|
---|
736 | );
|
---|
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 |
|
---|
753 |
|
---|
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 | );
|
---|
764 |
|
---|
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 | );
|
---|
775 |
|
---|
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 | );
|
---|
786 |
|
---|
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 | );
|
---|
797 |
|
---|
798 | AxisAlignedBox3 GetBBox(const VssTreeNode *node) const {
|
---|
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 |
|
---|
816 | AxisAlignedBox3 GetDirBBox(const VssTreeNode *node) const {
|
---|
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(
|
---|
850 | VssTreeLeaf *leaf
|
---|
851 | );
|
---|
852 |
|
---|
853 | void
|
---|
854 | RemoveRay(VssRay *ray,
|
---|
855 | vector<VssTreeLeaf *> *affectedLeaves,
|
---|
856 | const bool removeAllScheduledRays
|
---|
857 | );
|
---|
858 |
|
---|
859 | // void
|
---|
860 | // AddRay(VssRay *ray);
|
---|
861 | void
|
---|
862 | AddRay(VssTreeNode::RayInfo &info);
|
---|
863 |
|
---|
864 | void
|
---|
865 | TraverseInternalNode(
|
---|
866 | RayTraversalData &data,
|
---|
867 | stack<RayTraversalData> &tstack);
|
---|
868 |
|
---|
869 | void
|
---|
870 | EvaluateLeafStats(const TraversalData &data);
|
---|
871 |
|
---|
872 |
|
---|
873 | int
|
---|
874 | GetRootPvsSize() const {
|
---|
875 | return GetPvsSize(bbox);
|
---|
876 | }
|
---|
877 |
|
---|
878 | int
|
---|
879 | GetPvsSize(const AxisAlignedBox3 &box) const;
|
---|
880 |
|
---|
881 | void
|
---|
882 | GetRayContributionStatistics(
|
---|
883 | float &minRayContribution,
|
---|
884 | float &maxRayContribution,
|
---|
885 | float &avgRayContribution
|
---|
886 | );
|
---|
887 |
|
---|
888 | int
|
---|
889 | GenerateRays(const float ratioPerLeaf,
|
---|
890 | SimpleRayContainer &rays);
|
---|
891 |
|
---|
892 | int
|
---|
893 | GenerateRays(const int numberOfRays,
|
---|
894 | const int numberOfLeaves,
|
---|
895 | SimpleRayContainer &rays);
|
---|
896 |
|
---|
897 | float
|
---|
898 | GetAvgPvsSize();
|
---|
899 |
|
---|
900 | int
|
---|
901 | UpdateSubdivision();
|
---|
902 |
|
---|
903 | bool
|
---|
904 | TerminationCriteriaSatisfied(VssTreeLeaf *leaf);
|
---|
905 |
|
---|
906 | void
|
---|
907 | CollectLeaves(vector<VssTreeLeaf *> &leaves);
|
---|
908 |
|
---|
909 | bool
|
---|
910 | ClipRay(
|
---|
911 | VssTreeNode::RayInfo &rayInfo,
|
---|
912 | const AxisAlignedBox3 &box
|
---|
913 | );
|
---|
914 |
|
---|
915 | VssTreeNode *GetRoot() const { return root; }
|
---|
916 |
|
---|
917 | bool
|
---|
918 | ValidLeaf(VssTreeLeaf *leaf) const;
|
---|
919 |
|
---|
920 | void
|
---|
921 | GenerateLeafRays(VssTreeLeaf *leaf,
|
---|
922 | const int numberOfRays,
|
---|
923 | SimpleRayContainer &rays);
|
---|
924 |
|
---|
925 | int
|
---|
926 | CollectRays(VssRayContainer &rays,
|
---|
927 | const int number);
|
---|
928 |
|
---|
929 | int
|
---|
930 | CollectRays(VssRayContainer &rays);
|
---|
931 |
|
---|
932 | };
|
---|
933 |
|
---|
934 | };
|
---|
935 |
|
---|
936 | #endif // __LSDS_KDTREE_H__
|
---|
937 |
|
---|