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