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 | return mViewCell;
|
---|
437 | }
|
---|
438 |
|
---|
439 | /** Returns number of times the max cost ratio was missed until
|
---|
440 | this leaf.
|
---|
441 | */
|
---|
442 | int GetMaxCostMisses();
|
---|
443 |
|
---|
444 | ////////////////////////////////////////////
|
---|
445 |
|
---|
446 | static void NewMail();
|
---|
447 | static int sMailId;
|
---|
448 |
|
---|
449 | ObjectPvs *mPvs;
|
---|
450 | float mVolume;
|
---|
451 |
|
---|
452 | protected:
|
---|
453 |
|
---|
454 | /** Manually sets PVS size.
|
---|
455 | @param s the PVS size
|
---|
456 | */
|
---|
457 | void SetPvsSize(const int s);
|
---|
458 |
|
---|
459 | int mMailbox;
|
---|
460 |
|
---|
461 | /// rays intersecting this leaf.
|
---|
462 | RayInfoContainer mRays;
|
---|
463 | /// true if mPvsSize is valid => PVS does not have to be updated
|
---|
464 | bool mValidPvs;
|
---|
465 | /// the view cell associated with this leaf
|
---|
466 | VspKdViewCell *mViewCell;
|
---|
467 | /// number of times the max cost ratio was missed on the way to the leaf.
|
---|
468 | int mMaxCostMisses;
|
---|
469 |
|
---|
470 | //private:
|
---|
471 | /// stores PVS size so we have to evaluate PVS size only once
|
---|
472 | int mPvsSize;
|
---|
473 | };
|
---|
474 |
|
---|
475 |
|
---|
476 | // ---------------------------------------------------------------
|
---|
477 | // Main LSDS search class
|
---|
478 | // ---------------------------------------------------------------
|
---|
479 | class VspKdTree
|
---|
480 | {
|
---|
481 | // --------------------------------------------------------------
|
---|
482 | // For sorting rays
|
---|
483 | // -------------------------------------------------------------
|
---|
484 | struct SortableEntry
|
---|
485 | {
|
---|
486 | enum EType
|
---|
487 | {
|
---|
488 | ERayMin,
|
---|
489 | ERayMax
|
---|
490 | };
|
---|
491 |
|
---|
492 | int type;
|
---|
493 | float value;
|
---|
494 | VssRay *ray;
|
---|
495 |
|
---|
496 | SortableEntry() {}
|
---|
497 | SortableEntry(const int t, const float v, VssRay *r): type(t),
|
---|
498 | value(v), ray(r)
|
---|
499 | {
|
---|
500 | }
|
---|
501 |
|
---|
502 | friend bool operator<(const SortableEntry &a, const SortableEntry &b)
|
---|
503 | {
|
---|
504 | return a.value < b.value;
|
---|
505 | }
|
---|
506 | };
|
---|
507 |
|
---|
508 | struct TraversalData
|
---|
509 | {
|
---|
510 | VspKdNode *mNode;
|
---|
511 | AxisAlignedBox3 mBox;
|
---|
512 | //TODO PolygonContainer *mPolys;
|
---|
513 |
|
---|
514 | int mDepth;
|
---|
515 | //float mPriority;
|
---|
516 |
|
---|
517 | TraversalData() {}
|
---|
518 |
|
---|
519 | TraversalData(VspKdNode *n, const float p):
|
---|
520 | mNode(n)//, mPriority(p)
|
---|
521 | {}
|
---|
522 |
|
---|
523 | TraversalData(VspKdNode *n, const AxisAlignedBox3 &b, const int d):
|
---|
524 | mNode(n), mBox(b), mDepth(d) {}
|
---|
525 |
|
---|
526 | // comparator for the priority queue
|
---|
527 | /*struct less_priority : public binary_function<const TraversalData, const TraversalData, bool>
|
---|
528 | {
|
---|
529 | bool operator()(const TraversalData a, const TraversalData b) {
|
---|
530 | return a.mPriority < b.mPriority; } };*/
|
---|
531 |
|
---|
532 | // ~TraversalData() {}
|
---|
533 | // TraversalData(const TraversalData &s):node(s.node), bbox(s.bbox), depth(s.depth) {}
|
---|
534 |
|
---|
535 | friend bool operator<(const TraversalData &a, const TraversalData &b)
|
---|
536 | {
|
---|
537 | // return a.node->queries.size() < b.node->queries.size();
|
---|
538 | VspKdLeaf *leafa = dynamic_cast<VspKdLeaf *>(a.mNode);
|
---|
539 | VspKdLeaf *leafb = dynamic_cast<VspKdLeaf *>(b.mNode);
|
---|
540 | #if 0
|
---|
541 | return
|
---|
542 | leafa->rays.size() * a.mBox.GetVolume()
|
---|
543 | <
|
---|
544 | leafb->rays.size() * b.mBox.GetVolume();
|
---|
545 | #endif
|
---|
546 | #if 0
|
---|
547 | return
|
---|
548 | leafa->GetPvsSize() * a.mBox.GetVolume()
|
---|
549 | <
|
---|
550 | leafb->GetPvsSize() * b.mBox.GetVolume();
|
---|
551 | #endif
|
---|
552 | #if 0
|
---|
553 | return
|
---|
554 | leafa->GetPvsSize()
|
---|
555 | <
|
---|
556 | leafb->GetPvsSize();
|
---|
557 | #endif
|
---|
558 | #if 0
|
---|
559 | return
|
---|
560 | leafa->GetPvsSize() / (float)(leafa->rays.size() + Limits::Small())
|
---|
561 | >
|
---|
562 | leafb->GetPvsSize() / (float)(leafb->rays.size() + Limits::Small());
|
---|
563 | #endif
|
---|
564 | #if 0
|
---|
565 | return
|
---|
566 | leafa->GetPvsSize() * (float)leafa->rays.size()
|
---|
567 | <
|
---|
568 | leafb->GetPvsSize() * (float)leafb->rays.size();
|
---|
569 | #endif
|
---|
570 | #if 1
|
---|
571 | return a.mDepth > b.mDepth;
|
---|
572 | #endif
|
---|
573 | }
|
---|
574 | };
|
---|
575 |
|
---|
576 | /** Simplified data for ray traversal only.
|
---|
577 | */
|
---|
578 | struct RayTraversalData
|
---|
579 | {
|
---|
580 | RayInfo mRayData;
|
---|
581 | VspKdNode *mNode;
|
---|
582 |
|
---|
583 | RayTraversalData() {}
|
---|
584 |
|
---|
585 | RayTraversalData(VspKdNode *n, const RayInfo &data):
|
---|
586 | mRayData(data), mNode(n) {}
|
---|
587 | };
|
---|
588 |
|
---|
589 | struct LineTraversalData
|
---|
590 | {
|
---|
591 | VspKdNode *mNode;
|
---|
592 | Vector3 mExitPoint;
|
---|
593 |
|
---|
594 | float mMaxT;
|
---|
595 |
|
---|
596 | LineTraversalData () {}
|
---|
597 | LineTraversalData (VspKdNode *n, const Vector3 &p, const float maxt):
|
---|
598 | mNode(n), mExitPoint(p), mMaxT(maxt) {}
|
---|
599 | };
|
---|
600 |
|
---|
601 | public:
|
---|
602 |
|
---|
603 | VspKdTree();
|
---|
604 | virtual ~VspKdTree();
|
---|
605 |
|
---|
606 | virtual void Construct(const VssRayContainer &rays,
|
---|
607 | AxisAlignedBox3 *forcedBoundingBox = NULL);
|
---|
608 |
|
---|
609 | /** Returns bounding box of the specified node.
|
---|
610 | */
|
---|
611 | AxisAlignedBox3 GetBBox(VspKdNode *node) const;
|
---|
612 |
|
---|
613 | const VspKdStatistics &GetStatistics() const;
|
---|
614 |
|
---|
615 | /** Get the root of the tree.
|
---|
616 | */
|
---|
617 | VspKdNode *GetRoot() const;
|
---|
618 |
|
---|
619 | /** Returns average PVS size in tree.
|
---|
620 | */
|
---|
621 | float GetAvgPvsSize();
|
---|
622 |
|
---|
623 | /** Returns memory usage in MB.
|
---|
624 | */
|
---|
625 | float GetMemUsage() const;
|
---|
626 | //?
|
---|
627 | float GetRayMemUsage() const;
|
---|
628 |
|
---|
629 | /** Collects leaves of this tree.
|
---|
630 | */
|
---|
631 | void CollectLeaves(vector<VspKdLeaf *> &leaves) const;
|
---|
632 |
|
---|
633 | /** Merges view cells created with this tree according to
|
---|
634 | some (global) cost heuristics.
|
---|
635 | */
|
---|
636 | int MergeViewCells(const VssRayContainer &rays);
|
---|
637 |
|
---|
638 | /** Finds neighbours of this node.
|
---|
639 | @param n the input node
|
---|
640 | @param neighbours the neighbors of the input node
|
---|
641 | @param onlyUnmailed if only unmailed neighbors are collected
|
---|
642 | */
|
---|
643 | int FindNeighbors(VspKdLeaf *n,
|
---|
644 | vector<VspKdLeaf *> &neighbors,
|
---|
645 | bool onlyUnmailed);
|
---|
646 |
|
---|
647 |
|
---|
648 | /** Sets pointer to view cells manager.
|
---|
649 | */
|
---|
650 | void SetViewCellsManager(ViewCellsManager *vcm);
|
---|
651 |
|
---|
652 | /** A ray is cast possible intersecting the tree.
|
---|
653 | @param the ray that is cast.
|
---|
654 | @returns the number of intersections with objects stored in the tree.
|
---|
655 | */
|
---|
656 | int CastLineSegment(const Vector3 &origin,
|
---|
657 | const Vector3 &termination,
|
---|
658 | vector<ViewCell *> &viewcells);
|
---|
659 |
|
---|
660 | /** Collects view cells generated by this tree.
|
---|
661 | */
|
---|
662 | void CollectViewCells(ViewCellContainer &viewCells) const;
|
---|
663 |
|
---|
664 | /** Refines view cells using shuffling, i.e., border leaves
|
---|
665 | of two view cells are exchanged if the resulting view cells
|
---|
666 | are tested to be "better" than the old ones.
|
---|
667 | @returns number of refined view cells
|
---|
668 | */
|
---|
669 | int RefineViewCells(const VssRayContainer &rays);
|
---|
670 |
|
---|
671 | /** Collects candidates for the merge in the merge queue.
|
---|
672 | */
|
---|
673 | void CollectMergeCandidates();
|
---|
674 |
|
---|
675 | /** Collapses the tree with respect to the view cell partition.
|
---|
676 | @returns number of collapsed nodes
|
---|
677 | */
|
---|
678 | int CollapseTree();
|
---|
679 |
|
---|
680 | protected:
|
---|
681 |
|
---|
682 | /** Collapses the tree with respect to the view cell partition,
|
---|
683 | i.e. leaves having the same view cell are collapsed.
|
---|
684 | @param node the root of the subtree to be collapsed
|
---|
685 | @param collapsed returns the number of collapsed nodes
|
---|
686 | @returns node of type leaf if the node could be collapsed,
|
---|
687 | this node otherwise
|
---|
688 | */
|
---|
689 | VspKdNode *CollapseTree(VspKdNode *node, int &collapsed);
|
---|
690 |
|
---|
691 | // incremental construction
|
---|
692 | virtual void UpdateRays(VssRayContainer &remove, VssRayContainer &add);
|
---|
693 |
|
---|
694 | virtual void AddRays(VssRayContainer &add);
|
---|
695 |
|
---|
696 | VspKdNode *Locate(const Vector3 &v);
|
---|
697 |
|
---|
698 | VspKdNode *SubdivideNode(VspKdLeaf *leaf,
|
---|
699 | const AxisAlignedBox3 &box,
|
---|
700 | AxisAlignedBox3 &backBox,
|
---|
701 | AxisAlignedBox3 &frontBox);
|
---|
702 |
|
---|
703 | VspKdNode *Subdivide(const TraversalData &tdata);
|
---|
704 |
|
---|
705 | int SelectPlane(VspKdLeaf *leaf,
|
---|
706 | const AxisAlignedBox3 &box,
|
---|
707 | float &position,
|
---|
708 | int &raysBack,
|
---|
709 | int &raysFront,
|
---|
710 | int &pvsBack,
|
---|
711 | int &pvsFront);
|
---|
712 |
|
---|
713 | void SortSplitCandidates(VspKdLeaf *node,
|
---|
714 | const int axis);
|
---|
715 |
|
---|
716 | float BestCostRatioHeuristic(VspKdLeaf *node,
|
---|
717 | const AxisAlignedBox3 &box,
|
---|
718 | int &axis,
|
---|
719 | float &position,
|
---|
720 | int &raysBack,
|
---|
721 | int &raysFront,
|
---|
722 | int &pvsBack,
|
---|
723 | int &pvsFront);
|
---|
724 |
|
---|
725 | float BestCostRatioRegular(VspKdLeaf *node,
|
---|
726 | const AxisAlignedBox3 &box,
|
---|
727 | int &axis,
|
---|
728 | float &position,
|
---|
729 | int &raysBack,
|
---|
730 | int &raysFront,
|
---|
731 | int &pvsBack,
|
---|
732 | int &pvsFront);
|
---|
733 |
|
---|
734 | float EvalCostRatio(VspKdLeaf *node,
|
---|
735 | const AxisAlignedBox3 &box,
|
---|
736 | const int axis,
|
---|
737 | const float position,
|
---|
738 | int &raysBack,
|
---|
739 | int &raysFront,
|
---|
740 | int &pvsBack,
|
---|
741 | int &pvsFront);
|
---|
742 |
|
---|
743 |
|
---|
744 | VspKdNode * SubdivideLeaf(VspKdLeaf *leaf,
|
---|
745 | const float SAThreshold);
|
---|
746 |
|
---|
747 | void RemoveRay(VssRay *ray,
|
---|
748 | vector<VspKdLeaf *> *affectedLeaves,
|
---|
749 | const bool removeAllScheduledRays);
|
---|
750 |
|
---|
751 | void AddRay(VssRay *ray);
|
---|
752 |
|
---|
753 | void TraverseInternalNode(RayTraversalData &data,
|
---|
754 | stack<RayTraversalData> &tstack);
|
---|
755 |
|
---|
756 | void EvaluateLeafStats(const TraversalData &data);
|
---|
757 |
|
---|
758 |
|
---|
759 | int GetRootPvsSize() const;
|
---|
760 |
|
---|
761 | int GetPvsSize(VspKdNode *node, const AxisAlignedBox3 &box) const;
|
---|
762 |
|
---|
763 | void GetRayContributionStatistics(float &minRayContribution,
|
---|
764 | float &maxRayContribution,
|
---|
765 | float &avgRayContribution);
|
---|
766 |
|
---|
767 | int GenerateRays(const float ratioPerLeaf,
|
---|
768 | SimpleRayContainer &rays);
|
---|
769 |
|
---|
770 | /** Collapses this subtree and releases the memory.
|
---|
771 | @returns number of collapsed rays.
|
---|
772 | */
|
---|
773 | int CollapseSubtree(VspKdNode *sroot, const int time);
|
---|
774 |
|
---|
775 | int ReleaseMemory(const int time);
|
---|
776 |
|
---|
777 | bool TerminationCriteriaMet(const VspKdLeaf *leaf,
|
---|
778 | const AxisAlignedBox3 &box) const;
|
---|
779 |
|
---|
780 | /** Computes PVS size of this node given a global ray set.
|
---|
781 | @returns PVS size of the intersection of this node bounding box with the rays.
|
---|
782 | */
|
---|
783 | int ComputePvsSize(VspKdNode *node,
|
---|
784 | const RayInfoContainer &globalRays) const;
|
---|
785 |
|
---|
786 |
|
---|
787 | /** Generates view cell for this leaf taking the ray contributions.
|
---|
788 | */
|
---|
789 | void GenerateViewCell(VspKdLeaf *leaf);
|
---|
790 |
|
---|
791 | /** Merges view cells of the two leaves.
|
---|
792 | */
|
---|
793 | bool MergeViewCells(VspKdLeaf *l1, VspKdLeaf *l2);
|
---|
794 |
|
---|
795 | /** Helper function revalidating the view cell leaf list after merge.
|
---|
796 | */
|
---|
797 | void RepairViewCellsLeafLists();
|
---|
798 |
|
---|
799 | /** Shuffles the leaves, i.e., tests if exchanging
|
---|
800 | the view cells the leaves belong to helps in improving the view cells.
|
---|
801 | */
|
---|
802 | bool ShuffleLeaves(VspKdLeaf *leaf1, VspKdLeaf *leaf2) const;
|
---|
803 |
|
---|
804 | /** Shuffles, i.e. takes border leaf from view cell 1 and adds it
|
---|
805 | to view cell 2.
|
---|
806 | */
|
---|
807 | void ShuffleLeaf(VspKdLeaf *leaf,
|
---|
808 | VspKdViewCell *vc1,
|
---|
809 | VspKdViewCell *vc2) const;
|
---|
810 | protected:
|
---|
811 |
|
---|
812 |
|
---|
813 | /////////////////////////////
|
---|
814 | // The core pointer
|
---|
815 | VspKdNode *mRoot;
|
---|
816 |
|
---|
817 | /////////////////////////////
|
---|
818 | // Basic properties
|
---|
819 |
|
---|
820 | // total number of nodes of the tree
|
---|
821 | int mNumNodes;
|
---|
822 |
|
---|
823 | // axis aligned bounding box of the scene
|
---|
824 | AxisAlignedBox3 mBox;
|
---|
825 |
|
---|
826 | // epsilon used for the construction
|
---|
827 | float mEpsilon;
|
---|
828 |
|
---|
829 | // ratio between traversal and intersection costs
|
---|
830 | float mCtDivCi;
|
---|
831 |
|
---|
832 | // type of the splitting to use for the tree construction
|
---|
833 | enum {ESplitRegular, ESplitHeuristic};
|
---|
834 | int splitType;
|
---|
835 |
|
---|
836 | // maximum alovable memory in MB
|
---|
837 | float mMaxTotalMemory;
|
---|
838 |
|
---|
839 | // maximum alovable memory for static kd tree in MB
|
---|
840 | float mMaxStaticMemory;
|
---|
841 |
|
---|
842 | // this is used during the construction depending
|
---|
843 | // on the type of the tree and queries...
|
---|
844 | float mMaxMemory;
|
---|
845 |
|
---|
846 | // minimal acess time for collapse
|
---|
847 | int mAccessTimeThreshold;
|
---|
848 |
|
---|
849 | // minimal depth at which to perform collapse
|
---|
850 | int mMinCollapseDepth;
|
---|
851 |
|
---|
852 | // reusable array of split candidates
|
---|
853 | vector<SortableEntry> *mSplitCandidates;
|
---|
854 |
|
---|
855 | ViewCellsManager *mViewCellsManager;
|
---|
856 |
|
---|
857 | priority_queue<VspKdMergeCandidate> mMergeQueue;
|
---|
858 |
|
---|
859 |
|
---|
860 | /////////////////////////////
|
---|
861 | // Construction parameters
|
---|
862 |
|
---|
863 | /// max depth of the tree
|
---|
864 | int mTermMaxDepth;
|
---|
865 | /// minimal ratio of the volume of the cell and the query volume
|
---|
866 | float mTermMinSize;
|
---|
867 | /// minimal pvs per node to still get subdivided
|
---|
868 | int mTermMinPvs;
|
---|
869 | /// minimal ray number per node to still get subdivided
|
---|
870 | int mTermMinRays;
|
---|
871 | /// maximal acceptable cost ratio to continue subdivision
|
---|
872 | float mTermMaxCostRatio;
|
---|
873 | /// maximal contribution per ray to subdivide the node
|
---|
874 | float mTermMaxRayContribution;
|
---|
875 | /// tolerance value indicating how often the max cost ratio can be failed
|
---|
876 | int mTermMissTolerance;
|
---|
877 |
|
---|
878 | /// maximal numbers of view cells
|
---|
879 | int mMaxViewCells;
|
---|
880 |
|
---|
881 | /// maximal cost ratio of a merge
|
---|
882 | float mMergeMaxCostRatio;
|
---|
883 |
|
---|
884 | /// minimal number of view cells
|
---|
885 | int mMergeMinViewCells;
|
---|
886 |
|
---|
887 | /// if only the "driving axis", i.e., the axis with the biggest extent
|
---|
888 | /// should be used for subdivision
|
---|
889 | bool mOnlyDrivingAxis;
|
---|
890 |
|
---|
891 | /////////////////////////////
|
---|
892 | VspKdStatistics mStat;
|
---|
893 | };
|
---|
894 |
|
---|
895 | }
|
---|
896 |
|
---|
897 | #endif // __VSP_KDTREE_H__
|
---|
898 |
|
---|