1 | // ================================================================
|
---|
2 | // $Id$
|
---|
3 | // ****************************************************************
|
---|
4 | //
|
---|
5 | // Initial coding by
|
---|
6 | /**
|
---|
7 | @author Jiri Bittner
|
---|
8 | */
|
---|
9 |
|
---|
10 | #ifndef __VSPKDTREE_H__
|
---|
11 | #define __VSPKDTREE_H__
|
---|
12 |
|
---|
13 | // Standard headers
|
---|
14 | #include <iomanip>
|
---|
15 | #include <vector>
|
---|
16 | #include <functional>
|
---|
17 | #include <stack>
|
---|
18 |
|
---|
19 |
|
---|
20 | // User headers
|
---|
21 | #include "VssRay.h"
|
---|
22 | #include "AxisAlignedBox3.h"
|
---|
23 |
|
---|
24 |
|
---|
25 | #define USE_KDNODE_VECTORS 1
|
---|
26 | #define _RSS_STATISTICS
|
---|
27 | #define _RSS_TRAVERSAL_STATISTICS
|
---|
28 |
|
---|
29 |
|
---|
30 | #include "Statistics.h"
|
---|
31 | #include "Ray.h"
|
---|
32 |
|
---|
33 | #include "RayInfo.h"
|
---|
34 | #include "Containers.h"
|
---|
35 | #include "ViewCell.h"
|
---|
36 |
|
---|
37 | class VspKdLeaf;
|
---|
38 | class ViewCellsManager;
|
---|
39 | class ViewCellsStatistics;
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | Candidate for leaf merging based on priority.
|
---|
44 | */
|
---|
45 | class VspKdMergeCandidate
|
---|
46 | {
|
---|
47 | public:
|
---|
48 |
|
---|
49 | VspKdMergeCandidate(VspKdLeaf *l1, VspKdLeaf *l2);
|
---|
50 |
|
---|
51 | /** Computes PVS difference between the leaves.
|
---|
52 | */
|
---|
53 | //int ComputePvsDifference() const;
|
---|
54 |
|
---|
55 | /** If this merge pair is still valid.
|
---|
56 | */
|
---|
57 | bool Valid() const;
|
---|
58 |
|
---|
59 | /** Sets this merge candidate to be valid.
|
---|
60 | */
|
---|
61 | void SetValid();
|
---|
62 |
|
---|
63 | friend bool operator<(const VspKdMergeCandidate &leafa, const VspKdMergeCandidate &leafb)
|
---|
64 | {
|
---|
65 | return leafb.GetMergeCost() < leafa.GetMergeCost();
|
---|
66 | }
|
---|
67 |
|
---|
68 | void SetLeaf1(VspKdLeaf *l);
|
---|
69 | void SetLeaf2(VspKdLeaf *l);
|
---|
70 |
|
---|
71 | VspKdLeaf *GetLeaf1();
|
---|
72 | VspKdLeaf *GetLeaf2();
|
---|
73 |
|
---|
74 | /** Merge cost of this candidate pair.
|
---|
75 | */
|
---|
76 | float GetMergeCost() const;
|
---|
77 |
|
---|
78 | /** Returns cost of leaf 1.
|
---|
79 | */
|
---|
80 | float GetLeaf1Cost() const;
|
---|
81 | /** Returns cost of leaf 2.
|
---|
82 | */
|
---|
83 | float GetLeaf2Cost() const;
|
---|
84 |
|
---|
85 | /// maximal pvs size
|
---|
86 | static int sMaxPvsSize;
|
---|
87 | /// overall cost used to normalize cost ratio
|
---|
88 | static float sOverallCost;
|
---|
89 |
|
---|
90 | protected:
|
---|
91 |
|
---|
92 | /** Evaluates the merge costs of the leaves.
|
---|
93 | */
|
---|
94 | void EvalMergeCost();
|
---|
95 |
|
---|
96 | int mLeaf1Id;
|
---|
97 | int mLeaf2Id;
|
---|
98 |
|
---|
99 | float mMergeCost;
|
---|
100 |
|
---|
101 | VspKdLeaf *mLeaf1;
|
---|
102 | VspKdLeaf *mLeaf2;
|
---|
103 | };
|
---|
104 |
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | Static statistics for vsp kd-tree search
|
---|
109 | */
|
---|
110 | class VspKdStatistics: public StatisticsBase
|
---|
111 | {
|
---|
112 | public:
|
---|
113 | // total number of nodes
|
---|
114 | int nodes;
|
---|
115 | // number of splits along each of the axes
|
---|
116 | int splits[3];
|
---|
117 | // totals number of rays
|
---|
118 | int rays;
|
---|
119 | // initial size of the pvs
|
---|
120 | int initialPvsSize;
|
---|
121 | // total number of query domains
|
---|
122 | int queryDomains;
|
---|
123 | // total number of ray references
|
---|
124 | int rayRefs;
|
---|
125 |
|
---|
126 | // max depth nodes
|
---|
127 | int maxDepthNodes;
|
---|
128 | // max depth nodes
|
---|
129 | int minPvsNodes;
|
---|
130 | // nodes with minimum PVS
|
---|
131 | int minRaysNodes;
|
---|
132 | // max ray contribution nodes
|
---|
133 | int maxRayContribNodes;
|
---|
134 | // max depth nodes
|
---|
135 | int minSizeNodes;
|
---|
136 |
|
---|
137 | // max number of rays per node
|
---|
138 | int maxRayRefs;
|
---|
139 | // maximal PVS size / leaf
|
---|
140 | int maxPvsSize;
|
---|
141 |
|
---|
142 | // max cost ratio
|
---|
143 | int maxCostNodes;
|
---|
144 |
|
---|
145 | // number of dynamically added ray refs
|
---|
146 | int addedRayRefs;
|
---|
147 | // number of dynamically removed ray refs
|
---|
148 | int removedRayRefs;
|
---|
149 |
|
---|
150 | /// for average pvs
|
---|
151 | int accPvsSize;
|
---|
152 |
|
---|
153 | /** Default constructor.
|
---|
154 | */
|
---|
155 | VspKdStatistics() { Reset(); }
|
---|
156 | int Nodes() const { return nodes; }
|
---|
157 | int Interior() const { return nodes / 2; }
|
---|
158 | int Leaves() const { return (nodes / 2) + 1; }
|
---|
159 |
|
---|
160 | void Reset()
|
---|
161 | {
|
---|
162 | nodes = 0;
|
---|
163 |
|
---|
164 | for (int i=0; i<3; i++)
|
---|
165 | splits[i] = 0;
|
---|
166 |
|
---|
167 | rays = queryDomains = 0;
|
---|
168 | rayRefs = 0;
|
---|
169 |
|
---|
170 | maxDepthNodes = 0;
|
---|
171 | minPvsNodes = 0;
|
---|
172 | minRaysNodes = 0;
|
---|
173 | maxRayContribNodes = 0;
|
---|
174 | minSizeNodes = 0;
|
---|
175 | maxCostNodes = 0;
|
---|
176 |
|
---|
177 | maxRayRefs = 0;
|
---|
178 | addedRayRefs = removedRayRefs = 0;
|
---|
179 |
|
---|
180 | initialPvsSize = 0;
|
---|
181 | maxPvsSize = 0;
|
---|
182 | accPvsSize = 0;
|
---|
183 | }
|
---|
184 |
|
---|
185 | void Print(ostream &app) const;
|
---|
186 | friend ostream &operator<<(ostream &s, const VspKdStatistics &stat)
|
---|
187 | {
|
---|
188 | stat.Print(s);
|
---|
189 | return s;
|
---|
190 | }
|
---|
191 | };
|
---|
192 |
|
---|
193 |
|
---|
194 | class VspKdInterior;
|
---|
195 |
|
---|
196 |
|
---|
197 | /** Abstract superclass of Vsp-Kd-Tree nodes.
|
---|
198 | */
|
---|
199 | class VspKdNode
|
---|
200 | {
|
---|
201 | public:
|
---|
202 |
|
---|
203 | friend class VspKdTree;
|
---|
204 |
|
---|
205 | enum {EInterior, EIntermediate, ELeaf};
|
---|
206 |
|
---|
207 | /** Constructs new interior node from the parent node.
|
---|
208 | */
|
---|
209 | inline VspKdNode(VspKdNode *p);
|
---|
210 |
|
---|
211 | /** Destroys this node and the subtree.
|
---|
212 | */
|
---|
213 | virtual ~VspKdNode();
|
---|
214 |
|
---|
215 | /** Type of the node (Einterior or ELeaf)
|
---|
216 | */
|
---|
217 | virtual int Type() const = 0;
|
---|
218 |
|
---|
219 | /** Returns true if this node is a leaf.
|
---|
220 | */
|
---|
221 | bool IsLeaf() const;
|
---|
222 |
|
---|
223 | /** Prints node stats.
|
---|
224 | */
|
---|
225 | virtual void Print(ostream &s) const = 0;
|
---|
226 |
|
---|
227 | /** Returns time needed to access this node.
|
---|
228 | @NOTE: don't really know how it is used!
|
---|
229 | */
|
---|
230 | virtual int GetAccessTime();
|
---|
231 |
|
---|
232 | /** Returns parent node.
|
---|
233 | */
|
---|
234 | VspKdNode *GetParent() const;
|
---|
235 |
|
---|
236 | /** Sets parent node.
|
---|
237 | */
|
---|
238 | void SetParent(VspKdNode *p);
|
---|
239 |
|
---|
240 | protected:
|
---|
241 | /////////////////////////////////
|
---|
242 | // The actual data goes here
|
---|
243 |
|
---|
244 | /// link to the parent
|
---|
245 | VspKdNode *mParent;
|
---|
246 |
|
---|
247 | enum {SPLIT_X = 0, SPLIT_Y, SPLIT_Z};
|
---|
248 |
|
---|
249 | /// splitting axis
|
---|
250 | char mAxis;
|
---|
251 |
|
---|
252 | /// depth
|
---|
253 | unsigned char mDepth;
|
---|
254 | };
|
---|
255 |
|
---|
256 | // --------------------------------------------------------------
|
---|
257 | // KD-tree node - interior node
|
---|
258 | // --------------------------------------------------------------
|
---|
259 | class VspKdInterior: public VspKdNode
|
---|
260 | {
|
---|
261 | public:
|
---|
262 | friend class VspKdTree;
|
---|
263 |
|
---|
264 | /** Constructs new interior node from parent node.
|
---|
265 | */
|
---|
266 | VspKdInterior(VspKdInterior *p);
|
---|
267 |
|
---|
268 | virtual int GetAccessTime();
|
---|
269 |
|
---|
270 | virtual int Type() const;
|
---|
271 |
|
---|
272 | virtual ~VspKdInterior();
|
---|
273 |
|
---|
274 | virtual void Print(ostream &s) const;
|
---|
275 |
|
---|
276 | /** Returns back child.
|
---|
277 | */
|
---|
278 | inline VspKdNode *GetBack() const;
|
---|
279 | /** Returns front child.
|
---|
280 | */
|
---|
281 | inline VspKdNode *GetFront() const;
|
---|
282 |
|
---|
283 | protected:
|
---|
284 |
|
---|
285 | /** Sets pointers to back child and front child.
|
---|
286 | */
|
---|
287 | void SetupChildLinks(VspKdNode *b, VspKdNode *f);
|
---|
288 | /** Replaces the pointer to oldChild with a pointer to newChild.
|
---|
289 | */
|
---|
290 | void ReplaceChildLink(VspKdNode *oldChild, VspKdNode *newChild);
|
---|
291 | /** Computes intersection of the ray with the node boundaries.
|
---|
292 | */
|
---|
293 | int ComputeRayIntersection(const RayInfo &rayData, float &t);
|
---|
294 |
|
---|
295 | // plane in local modelling coordinates
|
---|
296 | float mPosition;
|
---|
297 |
|
---|
298 | // pointers to children
|
---|
299 | VspKdNode *mBack;
|
---|
300 | VspKdNode *mFront;
|
---|
301 |
|
---|
302 | // the bbox of the node
|
---|
303 | AxisAlignedBox3 mBox;
|
---|
304 |
|
---|
305 | // data for caching
|
---|
306 | long mAccesses;
|
---|
307 | long mLastAccessTime;
|
---|
308 | };
|
---|
309 |
|
---|
310 |
|
---|
311 | /**
|
---|
312 | Node type just before leaf holding abitrary split plane
|
---|
313 | */
|
---|
314 | class VspKdIntermediate: public VspKdNode
|
---|
315 | {
|
---|
316 | public:
|
---|
317 | friend class VspKdTree;
|
---|
318 |
|
---|
319 | /** Constructs new interior node from parent node.
|
---|
320 | */
|
---|
321 | VspKdIntermediate(VspKdInterior *p);
|
---|
322 |
|
---|
323 | //virtual int GetAccessTime();
|
---|
324 |
|
---|
325 | virtual int Type() const;
|
---|
326 |
|
---|
327 | virtual ~VspKdIntermediate();
|
---|
328 |
|
---|
329 | virtual void Print(ostream &s) const;
|
---|
330 |
|
---|
331 | /** Returns back child.
|
---|
332 | */
|
---|
333 | inline VspKdLeaf *GetBack() const;
|
---|
334 | /** Returns front child.
|
---|
335 | */
|
---|
336 | inline VspKdLeaf *GetFront() const;
|
---|
337 |
|
---|
338 | protected:
|
---|
339 |
|
---|
340 | /** Sets pointers to back child and front child.
|
---|
341 | */
|
---|
342 | void SetupChildLinks(VspKdLeaf *b, VspKdLeaf *f);
|
---|
343 | /** Computes intersection of the ray with the node boundaries.
|
---|
344 | */
|
---|
345 | int ComputeRayIntersection(const RayInfo &rayData, float &t);
|
---|
346 |
|
---|
347 | // plane in local modelling coordinates
|
---|
348 | Plane3 mSplitPlane;
|
---|
349 |
|
---|
350 | // pointers to children
|
---|
351 | VspKdLeaf *mBack;
|
---|
352 | VspKdLeaf *mFront;
|
---|
353 |
|
---|
354 | // the bbox of the node
|
---|
355 | AxisAlignedBox3 mBox;
|
---|
356 |
|
---|
357 | // data for caching
|
---|
358 | long mAccesses;
|
---|
359 | long mLastAccessTime;
|
---|
360 | };
|
---|
361 |
|
---|
362 | // --------------------------------------------------------------
|
---|
363 | // KD-tree node - leaf node
|
---|
364 | // --------------------------------------------------------------
|
---|
365 | class VspKdLeaf: public VspKdNode
|
---|
366 | {
|
---|
367 | public:
|
---|
368 |
|
---|
369 | friend class VspKdTree;
|
---|
370 |
|
---|
371 | /** Constructs leaf from parent node.
|
---|
372 | @param p the parent node
|
---|
373 | @param nRays preallocates memory to store this number of rays
|
---|
374 | @parma maxMisses how many times the max cost ratio was missed on the path to the leaf
|
---|
375 | */
|
---|
376 | VspKdLeaf(VspKdNode *p, const int nRays, const int maxCostMisses = 0);
|
---|
377 |
|
---|
378 | virtual ~VspKdLeaf();
|
---|
379 |
|
---|
380 | virtual int Type() const;
|
---|
381 |
|
---|
382 | virtual void Print(ostream &s) const;
|
---|
383 |
|
---|
384 | /** Adds a ray to the leaf ray container.
|
---|
385 | */
|
---|
386 | void AddRay(const RayInfo &data);
|
---|
387 | /** Returns size of the leaf PVS as induced by the rays
|
---|
388 | @note returns precomputed PVS size, which may not be valid
|
---|
389 | anymore. Run UpdatePvsSize to get a valid PVS.
|
---|
390 | */
|
---|
391 | int GetPvsSize() const;
|
---|
392 |
|
---|
393 | /** If PVS is not valid, this function recomputes the leaf
|
---|
394 | PVS as induced by the rays.
|
---|
395 | */
|
---|
396 | void UpdatePvsSize();
|
---|
397 |
|
---|
398 | /** Returns stored rays.
|
---|
399 | */
|
---|
400 | RayInfoContainer &GetRays();
|
---|
401 |
|
---|
402 | /** Returns rays into this ray container.
|
---|
403 | */
|
---|
404 | void GetRays(VssRayContainer &rays);
|
---|
405 | /** Returns average contribution of a ray to the PVS
|
---|
406 | */
|
---|
407 | inline float GetAvgRayContribution() const;
|
---|
408 | /** Returns square of average contribution of a ray.
|
---|
409 | */
|
---|
410 | inline float GetSqrRayContribution() const;
|
---|
411 |
|
---|
412 | /** Extracts PVS from ray set.
|
---|
413 | */
|
---|
414 | void ExtractPvs(ObjectContainer &objects) const;
|
---|
415 |
|
---|
416 | //-- mailing options
|
---|
417 | void Mail();
|
---|
418 |
|
---|
419 | bool Mailed() const;
|
---|
420 |
|
---|
421 | /** Returns true if mail equals the leaf mail box.
|
---|
422 | */
|
---|
423 | bool Mailed(const int mail) const;
|
---|
424 |
|
---|
425 | void SetViewCell(VspKdViewCell *viewCell);
|
---|
426 |
|
---|
427 | /** Returns mail box of this leaf.
|
---|
428 | */
|
---|
429 | int GetMailbox() const;
|
---|
430 |
|
---|
431 | /** Returns view cell associated with this leaf.
|
---|
432 | */
|
---|
433 | VspKdViewCell *GetViewCell();
|
---|
434 |
|
---|
435 | /** Returns number of times the max cost ratio was missed until
|
---|
436 | this leaf.
|
---|
437 | */
|
---|
438 | int GetMaxCostMisses();
|
---|
439 |
|
---|
440 | ////////////////////////////////////////////
|
---|
441 |
|
---|
442 | static void NewMail();
|
---|
443 | static int sMailId;
|
---|
444 |
|
---|
445 | ObjectPvs *mPvs;
|
---|
446 | float mVolume;
|
---|
447 |
|
---|
448 | protected:
|
---|
449 |
|
---|
450 | /** Manually sets PVS size.
|
---|
451 | @param s the PVS size
|
---|
452 | */
|
---|
453 | void SetPvsSize(const int s);
|
---|
454 |
|
---|
455 | int mMailbox;
|
---|
456 |
|
---|
457 | /// rays intersecting this leaf.
|
---|
458 | RayInfoContainer mRays;
|
---|
459 | /// true if mPvsSize is valid => PVS does not have to be updated
|
---|
460 | bool mValidPvs;
|
---|
461 | /// the view cell associated with this leaf
|
---|
462 | VspKdViewCell *mViewCell;
|
---|
463 | /// number of times the max cost ratio was missed on the way to the leaf.
|
---|
464 | int mMaxCostMisses;
|
---|
465 |
|
---|
466 | //private:
|
---|
467 | /// stores PVS size so we have to evaluate PVS size only once
|
---|
468 | int mPvsSize;
|
---|
469 | };
|
---|
470 |
|
---|
471 |
|
---|
472 | // ---------------------------------------------------------------
|
---|
473 | // Main LSDS search class
|
---|
474 | // ---------------------------------------------------------------
|
---|
475 | class VspKdTree
|
---|
476 | {
|
---|
477 | // --------------------------------------------------------------
|
---|
478 | // For sorting rays
|
---|
479 | // -------------------------------------------------------------
|
---|
480 | struct SortableEntry
|
---|
481 | {
|
---|
482 | enum EType
|
---|
483 | {
|
---|
484 | ERayMin,
|
---|
485 | ERayMax
|
---|
486 | };
|
---|
487 |
|
---|
488 | int type;
|
---|
489 | float value;
|
---|
490 | VssRay *ray;
|
---|
491 |
|
---|
492 | SortableEntry() {}
|
---|
493 | SortableEntry(const int t, const float v, VssRay *r): type(t),
|
---|
494 | value(v), ray(r)
|
---|
495 | {
|
---|
496 | }
|
---|
497 |
|
---|
498 | friend bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
499 | {
|
---|
500 | return a.value < b.value;
|
---|
501 | }
|
---|
502 | };
|
---|
503 |
|
---|
504 | struct TraversalData
|
---|
505 | {
|
---|
506 | VspKdNode *mNode;
|
---|
507 | AxisAlignedBox3 mBox;
|
---|
508 | //TODO PolygonContainer *mPolys;
|
---|
509 |
|
---|
510 | int mDepth;
|
---|
511 | //float mPriority;
|
---|
512 |
|
---|
513 | TraversalData() {}
|
---|
514 |
|
---|
515 | TraversalData(VspKdNode *n, const float p):
|
---|
516 | mNode(n)//, mPriority(p)
|
---|
517 | {}
|
---|
518 |
|
---|
519 | TraversalData(VspKdNode *n, const AxisAlignedBox3 &b, const int d):
|
---|
520 | mNode(n), mBox(b), mDepth(d) {}
|
---|
521 |
|
---|
522 | // comparator for the priority queue
|
---|
523 | /*struct less_priority : public binary_function<const TraversalData, const TraversalData, bool>
|
---|
524 | {
|
---|
525 | bool operator()(const TraversalData a, const TraversalData b) {
|
---|
526 | return a.mPriority < b.mPriority; } };*/
|
---|
527 |
|
---|
528 | // ~TraversalData() {}
|
---|
529 | // TraversalData(const TraversalData &s):node(s.node), bbox(s.bbox), depth(s.depth) {}
|
---|
530 |
|
---|
531 | friend bool operator<(const TraversalData &a, const TraversalData &b)
|
---|
532 | {
|
---|
533 | // return a.node->queries.size() < b.node->queries.size();
|
---|
534 | VspKdLeaf *leafa = dynamic_cast<VspKdLeaf *>(a.mNode);
|
---|
535 | VspKdLeaf *leafb = dynamic_cast<VspKdLeaf *>(b.mNode);
|
---|
536 | #if 0
|
---|
537 | return
|
---|
538 | leafa->rays.size() * a.mBox.GetVolume()
|
---|
539 | <
|
---|
540 | leafb->rays.size() * b.mBox.GetVolume();
|
---|
541 | #endif
|
---|
542 | #if 1
|
---|
543 | return
|
---|
544 | leafa->GetPvsSize() * a.mBox.GetVolume()
|
---|
545 | <
|
---|
546 | leafb->GetPvsSize() * b.mBox.GetVolume();
|
---|
547 | #endif
|
---|
548 | #if 0
|
---|
549 | return
|
---|
550 | leafa->GetPvsSize()
|
---|
551 | <
|
---|
552 | leafb->GetPvsSize();
|
---|
553 | #endif
|
---|
554 | #if 0
|
---|
555 | return
|
---|
556 | leafa->GetPvsSize() / (float)(leafa->rays.size() + Limits::Small())
|
---|
557 | >
|
---|
558 | leafb->GetPvsSize() / (float)(leafb->rays.size() + Limits::Small());
|
---|
559 | #endif
|
---|
560 | #if 0
|
---|
561 | return
|
---|
562 | leafa->GetPvsSize() * (float)leafa->rays.size()
|
---|
563 | <
|
---|
564 | leafb->GetPvsSize() * (float)leafb->rays.size();
|
---|
565 | #endif
|
---|
566 | }
|
---|
567 | };
|
---|
568 |
|
---|
569 | /** Simplified data for ray traversal only.
|
---|
570 | */
|
---|
571 | struct RayTraversalData
|
---|
572 | {
|
---|
573 | RayInfo mRayData;
|
---|
574 | VspKdNode *mNode;
|
---|
575 |
|
---|
576 | RayTraversalData() {}
|
---|
577 |
|
---|
578 | RayTraversalData(VspKdNode *n, const RayInfo &data):
|
---|
579 | mRayData(data), mNode(n) {}
|
---|
580 | };
|
---|
581 |
|
---|
582 | struct LineTraversalData
|
---|
583 | {
|
---|
584 | VspKdNode *mNode;
|
---|
585 | Vector3 mExitPoint;
|
---|
586 |
|
---|
587 | float mMaxT;
|
---|
588 |
|
---|
589 | LineTraversalData () {}
|
---|
590 | LineTraversalData (VspKdNode *n,
|
---|
591 | const Vector3 &p,
|
---|
592 | const float maxt):
|
---|
593 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
594 | };
|
---|
595 |
|
---|
596 | public:
|
---|
597 |
|
---|
598 | VspKdTree();
|
---|
599 | virtual ~VspKdTree();
|
---|
600 |
|
---|
601 | virtual void Construct(const VssRayContainer &rays,
|
---|
602 | AxisAlignedBox3 *forcedBoundingBox = NULL);
|
---|
603 |
|
---|
604 | /** Returns bounding box of the specified node.
|
---|
605 | */
|
---|
606 | AxisAlignedBox3 GetBBox(VspKdNode *node) const;
|
---|
607 |
|
---|
608 | const VspKdStatistics &GetStatistics() const;
|
---|
609 |
|
---|
610 | /** Get the root of the tree.
|
---|
611 | */
|
---|
612 | VspKdNode *GetRoot() const;
|
---|
613 |
|
---|
614 | /** Returns average PVS size in tree.
|
---|
615 | */
|
---|
616 | float GetAvgPvsSize();
|
---|
617 |
|
---|
618 | /** Returns memory usage in MB.
|
---|
619 | */
|
---|
620 | float GetMemUsage() const;
|
---|
621 | //?
|
---|
622 | float GetRayMemUsage() const;
|
---|
623 |
|
---|
624 | /** Collects leaves of this tree.
|
---|
625 | */
|
---|
626 | void CollectLeaves(vector<VspKdLeaf *> &leaves) const;
|
---|
627 |
|
---|
628 | /** Merges view cells created with this tree according to
|
---|
629 | some (global) cost heuristics.
|
---|
630 | */
|
---|
631 | int MergeViewCells(const VssRayContainer &rays);
|
---|
632 |
|
---|
633 | /** Finds neighbours of this node.
|
---|
634 | @param n the input node
|
---|
635 | @param neighbours the neighbors of the input node
|
---|
636 | @param onlyUnmailed if only unmailed neighbors are collected
|
---|
637 | */
|
---|
638 | int FindNeighbors(VspKdLeaf *n,
|
---|
639 | vector<VspKdLeaf *> &neighbors,
|
---|
640 | bool onlyUnmailed);
|
---|
641 |
|
---|
642 |
|
---|
643 | /** Sets pointer to view cells manager.
|
---|
644 | */
|
---|
645 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
646 |
|
---|
647 | /** A ray is cast possible intersecting the tree.
|
---|
648 | @param the ray that is cast.
|
---|
649 | @returns the number of intersections with objects stored in the tree.
|
---|
650 | */
|
---|
651 | int CastLineSegment(const Vector3 &origin,
|
---|
652 | const Vector3 &termination,
|
---|
653 | vector<ViewCell *> &viewcells);
|
---|
654 |
|
---|
655 | /** Collects view cells generated by this tree.
|
---|
656 | */
|
---|
657 | void CollectViewCells(ViewCellContainer &viewCells) const;
|
---|
658 |
|
---|
659 | /** Refines view cells using shuffling, i.e., border leaves
|
---|
660 | of two view cells are exchanged if the resulting view cells
|
---|
661 | are tested to be "better" than the old ones.
|
---|
662 | @returns number of refined view cells
|
---|
663 | */
|
---|
664 | int RefineViewCells(const VssRayContainer &rays);
|
---|
665 |
|
---|
666 | /** Collects candidates for the merge in the merge queue.
|
---|
667 | */
|
---|
668 | void CollectMergeCandidates();
|
---|
669 |
|
---|
670 | /** Collapses the tree with respect to the view cell partition.
|
---|
671 | @returns number of collapsed nodes
|
---|
672 | */
|
---|
673 | int CollapseTree();
|
---|
674 |
|
---|
675 | protected:
|
---|
676 |
|
---|
677 | /** Collapses the tree with respect to the view cell partition,
|
---|
678 | i.e. leaves having the same view cell are collapsed.
|
---|
679 | @param node the root of the subtree to be collapsed
|
---|
680 | @param collapsed returns the number of collapsed nodes
|
---|
681 | @returns node of type leaf if the node could be collapsed,
|
---|
682 | this node otherwise
|
---|
683 | */
|
---|
684 | VspKdNode *CollapseTree(VspKdNode *node, int &collapsed);
|
---|
685 |
|
---|
686 | // incremental construction
|
---|
687 | virtual void UpdateRays(VssRayContainer &remove, VssRayContainer &add);
|
---|
688 |
|
---|
689 | virtual void AddRays(VssRayContainer &add);
|
---|
690 |
|
---|
691 | VspKdNode *Locate(const Vector3 &v);
|
---|
692 |
|
---|
693 | VspKdNode *SubdivideNode(VspKdLeaf *leaf,
|
---|
694 | const AxisAlignedBox3 &box,
|
---|
695 | AxisAlignedBox3 &backBox,
|
---|
696 | AxisAlignedBox3 &frontBox);
|
---|
697 |
|
---|
698 | VspKdNode *Subdivide(const TraversalData &tdata);
|
---|
699 |
|
---|
700 | int SelectPlane(VspKdLeaf *leaf,
|
---|
701 | const AxisAlignedBox3 &box,
|
---|
702 | float &position,
|
---|
703 | int &raysBack,
|
---|
704 | int &raysFront,
|
---|
705 | int &pvsBack,
|
---|
706 | int &pvsFront);
|
---|
707 |
|
---|
708 | void SortSplitCandidates(VspKdLeaf *node,
|
---|
709 | const int axis);
|
---|
710 |
|
---|
711 | float BestCostRatioHeuristic(VspKdLeaf *node,
|
---|
712 | const AxisAlignedBox3 &box,
|
---|
713 | int &axis,
|
---|
714 | float &position,
|
---|
715 | int &raysBack,
|
---|
716 | int &raysFront,
|
---|
717 | int &pvsBack,
|
---|
718 | int &pvsFront);
|
---|
719 |
|
---|
720 | float BestCostRatioRegular(VspKdLeaf *node,
|
---|
721 | const AxisAlignedBox3 &box,
|
---|
722 | int &axis,
|
---|
723 | float &position,
|
---|
724 | int &raysBack,
|
---|
725 | int &raysFront,
|
---|
726 | int &pvsBack,
|
---|
727 | int &pvsFront);
|
---|
728 |
|
---|
729 | float EvalCostRatio(VspKdLeaf *node,
|
---|
730 | const AxisAlignedBox3 &box,
|
---|
731 | const int axis,
|
---|
732 | const float position,
|
---|
733 | int &raysBack,
|
---|
734 | int &raysFront,
|
---|
735 | int &pvsBack,
|
---|
736 | int &pvsFront);
|
---|
737 |
|
---|
738 |
|
---|
739 | VspKdNode * SubdivideLeaf(VspKdLeaf *leaf,
|
---|
740 | const float SAThreshold);
|
---|
741 |
|
---|
742 | void RemoveRay(VssRay *ray,
|
---|
743 | vector<VspKdLeaf *> *affectedLeaves,
|
---|
744 | const bool removeAllScheduledRays);
|
---|
745 |
|
---|
746 | void AddRay(VssRay *ray);
|
---|
747 |
|
---|
748 | void TraverseInternalNode(RayTraversalData &data,
|
---|
749 | stack<RayTraversalData> &tstack);
|
---|
750 |
|
---|
751 | void EvaluateLeafStats(const TraversalData &data);
|
---|
752 |
|
---|
753 |
|
---|
754 | int GetRootPvsSize() const;
|
---|
755 |
|
---|
756 | int GetPvsSize(VspKdNode *node, const AxisAlignedBox3 &box) const;
|
---|
757 |
|
---|
758 | void GetRayContributionStatistics(float &minRayContribution,
|
---|
759 | float &maxRayContribution,
|
---|
760 | float &avgRayContribution);
|
---|
761 |
|
---|
762 | int GenerateRays(const float ratioPerLeaf,
|
---|
763 | SimpleRayContainer &rays);
|
---|
764 |
|
---|
765 | /** Collapses this subtree and releases the memory.
|
---|
766 | @returns number of collapsed rays.
|
---|
767 | */
|
---|
768 | int CollapseSubtree(VspKdNode *sroot, const int time);
|
---|
769 |
|
---|
770 | int ReleaseMemory(const int time);
|
---|
771 |
|
---|
772 | bool TerminationCriteriaMet(const VspKdLeaf *leaf,
|
---|
773 | const AxisAlignedBox3 &box) const;
|
---|
774 |
|
---|
775 | /** Computes PVS size of this node given a global ray set.
|
---|
776 | @returns PVS size of the intersection of this node bounding box with the rays.
|
---|
777 | */
|
---|
778 | int ComputePvsSize(VspKdNode *node,
|
---|
779 | const RayInfoContainer &globalRays) const;
|
---|
780 |
|
---|
781 |
|
---|
782 | /** Generates view cell for this leaf taking the ray contributions.
|
---|
783 | */
|
---|
784 | void GenerateViewCell(VspKdLeaf *leaf);
|
---|
785 |
|
---|
786 | /** Merges view cells of the two leaves.
|
---|
787 | */
|
---|
788 | bool MergeViewCells(VspKdLeaf *l1, VspKdLeaf *l2);
|
---|
789 |
|
---|
790 | /** Helper function revalidating the view cell leaf list after merge.
|
---|
791 | */
|
---|
792 | void RepairViewCellsLeafLists();
|
---|
793 |
|
---|
794 | /** Shuffles the leaves, i.e., tests if exchanging
|
---|
795 | the view cells the leaves belong to helps in improving the view cells.
|
---|
796 | */
|
---|
797 | bool ShuffleLeaves(VspKdLeaf *leaf1, VspKdLeaf *leaf2) const;
|
---|
798 |
|
---|
799 | /** Shuffles, i.e. takes border leaf from view cell 1 and adds it
|
---|
800 | to view cell 2.
|
---|
801 | */
|
---|
802 | void ShuffleLeaf(VspKdLeaf *leaf,
|
---|
803 | VspKdViewCell *vc1,
|
---|
804 | VspKdViewCell *vc2) const;
|
---|
805 | protected:
|
---|
806 |
|
---|
807 |
|
---|
808 | /////////////////////////////
|
---|
809 | // The core pointer
|
---|
810 | VspKdNode *mRoot;
|
---|
811 |
|
---|
812 | /////////////////////////////
|
---|
813 | // Basic properties
|
---|
814 |
|
---|
815 | // total number of nodes of the tree
|
---|
816 | int mNumNodes;
|
---|
817 |
|
---|
818 | // axis aligned bounding box of the scene
|
---|
819 | AxisAlignedBox3 mBox;
|
---|
820 |
|
---|
821 | // epsilon used for the construction
|
---|
822 | float mEpsilon;
|
---|
823 |
|
---|
824 | // ratio between traversal and intersection costs
|
---|
825 | float mCtDivCi;
|
---|
826 |
|
---|
827 | // type of the splitting to use for the tree construction
|
---|
828 | enum {ESplitRegular, ESplitHeuristic};
|
---|
829 | int splitType;
|
---|
830 |
|
---|
831 | // maximum alovable memory in MB
|
---|
832 | float mMaxTotalMemory;
|
---|
833 |
|
---|
834 | // maximum alovable memory for static kd tree in MB
|
---|
835 | float mMaxStaticMemory;
|
---|
836 |
|
---|
837 | // this is used during the construction depending
|
---|
838 | // on the type of the tree and queries...
|
---|
839 | float mMaxMemory;
|
---|
840 |
|
---|
841 | // minimal acess time for collapse
|
---|
842 | int mAccessTimeThreshold;
|
---|
843 |
|
---|
844 | // minimal depth at which to perform collapse
|
---|
845 | int mMinCollapseDepth;
|
---|
846 |
|
---|
847 | // reusable array of split candidates
|
---|
848 | vector<SortableEntry> *mSplitCandidates;
|
---|
849 |
|
---|
850 | ViewCellsManager *mViewCellsManager;
|
---|
851 |
|
---|
852 | priority_queue<VspKdMergeCandidate> mMergeQueue;
|
---|
853 |
|
---|
854 |
|
---|
855 | /////////////////////////////
|
---|
856 | // Construction parameters
|
---|
857 |
|
---|
858 | /// max depth of the tree
|
---|
859 | int mTermMaxDepth;
|
---|
860 | /// minimal ratio of the volume of the cell and the query volume
|
---|
861 | float mTermMinSize;
|
---|
862 | /// minimal pvs per node to still get subdivided
|
---|
863 | int mTermMinPvs;
|
---|
864 | /// minimal ray number per node to still get subdivided
|
---|
865 | int mTermMinRays;
|
---|
866 | /// maximal acceptable cost ratio to continue subdivision
|
---|
867 | float mTermMaxCostRatio;
|
---|
868 | /// maximal contribution per ray to subdivide the node
|
---|
869 | float mTermMaxRayContribution;
|
---|
870 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
871 | int mTermMissTolerance;
|
---|
872 |
|
---|
873 | /// maximal numbers of view cells
|
---|
874 | int mMaxViewCells;
|
---|
875 |
|
---|
876 | /// maximal cost ratio of a merge
|
---|
877 | float mMergeMaxCostRatio;
|
---|
878 |
|
---|
879 | /// minimal number of view cells
|
---|
880 | int mMergeMinViewCells;
|
---|
881 |
|
---|
882 | /// if only the "driving axis", i.e., the axis with the biggest extent
|
---|
883 | /// should be used for subdivision
|
---|
884 | bool mOnlyDrivingAxis;
|
---|
885 |
|
---|
886 | /////////////////////////////
|
---|
887 | VspKdStatistics mStat;
|
---|
888 | };
|
---|
889 |
|
---|
890 |
|
---|
891 | #endif // __VSP_KDTREE_H__
|
---|
892 |
|
---|