RebuildAACubeTreeStrategy.h
Go to the documentation of this file.
1#ifndef REBUILDAACUBETREESTRATEGY_HEADER
2#define REBUILDAACUBETREESTRATEGY_HEADER
3#include <limits>
4
6#include <GfxTL/NullClass.h>
7#include <GfxTL/VectorXD.h>
8
9template <class InheritedStrategyT>
11{
12 typedef typename InheritedStrategyT::value_type value_type;
13
14 class CellData : public InheritedStrategyT::CellData
15 {
16 };
17
18 template <class BaseT>
19 class StrategyBase : public InheritedStrategyT::template StrategyBase<BaseT>
20 {
21 public:
22 typedef typename InheritedStrategyT::template StrategyBase<BaseT> BaseType;
23 typedef typename BaseType::CellType CellType;
26
27 size_t
29 {
30 if (!BaseType::Root())
31 {
32 return 0;
33 }
34 BaseType::Root()->Range() =
35 typename BaseType::CellRange(BaseType::BeginHandle(), BaseType::EndHandle());
36 //BaseType::Root()->Size(BaseType::size());
37 if (!BaseType::Root()->Size() || BaseType::Root()->Size() < BaseType::MaxBucketSize())
38 {
39 for (unsigned int i = 0; i < CellType::NChildren; ++i)
40 {
41 if (this->ExistChild(*BaseType::Root(), i))
42 {
43 delete &((*BaseType::Root())[i]);
44 }
45 BaseType::Root()->Child(i, NULL);
46 }
47 }
48 if (this->IsLeaf(*BaseType::Root()))
49 {
50 return 0;
51 }
52 typename BaseType::HandleType cur = BaseType::BeginHandle();
53 size_t maxDepth = 0;
55 for (unsigned int i = 0; i < BaseType::m_dim; ++i)
56 {
57 min[i] = -std::numeric_limits<ScalarType>::infinity();
58 max[i] = std::numeric_limits<ScalarType>::infinity();
59 }
60 for (unsigned int i = 0; i < CellType::NChildren; ++i)
61 {
62 if (!this->ExistChild(*BaseType::Root(), i))
63 {
64 continue;
65 }
66 PointType cmin, cmax;
67 for (unsigned int j = 0; j < BaseType::m_dim; ++j)
68 {
69 if (i & (1 << (BaseType::m_dim - j - 1)))
70 {
71 cmin[j] = BaseType::Root()->Center()[j];
72 cmax[j] = max[j];
73 }
74 else
75 {
76 cmin[j] = min[j];
77 cmax[j] = BaseType::Root()->Center()[j];
78 }
79 }
80 size_t d = Rebuild(*BaseType::Root(), i, cmin, cmax, &cur);
81 if (d > maxDepth)
82 {
83 maxDepth = d;
84 }
85 }
86 BaseType::Root()->Range() = typename BaseType::CellRange(BaseType::BeginHandle(), cur);
87 return maxDepth;
88 };
89
90 private:
91 size_t
92 Rebuild(CellType& parent,
93 size_t childIdx,
94 const PointType& min,
95 const PointType& max,
96 typename BaseType::HandleType* cur)
97 {
98 CellType& cell = parent[childIdx];
99 if (this->IsLeaf(cell))
100 {
101 typename BaseType::HandleType h = *cur;
102 if (h >= BaseType::EndHandle())
103 {
104 cell.Range() = typename BaseType::CellRange(h, h);
105 return cell.Level();
106 }
107 size_t s = cell.Size();
108 for (size_t i = 0; i < s && h < BaseType::EndHandle(); ++i, ++h)
109 {
110 size_t dref = this->Dereference(h);
111 bool inside = true;
112 for (unsigned int j = 0; j < BaseType::m_dim; ++j)
113 {
114 if (BaseType::at(dref)[j] <= min[j] || BaseType::at(dref)[j] > max[j])
115 {
116 inside = false;
117 break;
118 }
119 }
120 if (!inside)
121 {
122 break;
123 }
124 }
125 cell.Range() = typename BaseType::CellRange(*cur, h);
126 *cur = h;
127 return cell.Level();
128 }
129 else
130 {
131 // rebuild children
132 typename BaseType::HandleType start = *cur;
133 unsigned int numChilds = 0;
134 size_t maxDepth = 0;
135 for (unsigned int i = 0; i < CellType::NChildren; ++i)
136 {
137 if (!this->ExistChild(cell, i))
138 {
139 continue;
140 }
141 PointType cmin, cmax;
142 for (unsigned int j = 0; j < BaseType::m_dim; ++j)
143 {
144 if (i & (1 << (BaseType::m_dim - j - 1)))
145 {
146 cmin[j] = cell.Center()[j];
147 cmax[j] = max[j];
148 }
149 else
150 {
151 cmin[j] = min[j];
152 cmax[j] = cell.Center()[j];
153 }
154 }
155 size_t d = Rebuild(cell, i, cmin, cmax, cur);
156 if (d > maxDepth)
157 {
158 maxDepth = d;
159 }
160 if (cell[i].Size() == 0)
161 {
162 delete &(cell[i]);
163 cell.Child(i, (CellType*)1);
164 }
165 else
166 {
167 ++numChilds;
168 }
169 }
170 cell.Range() = typename BaseType::CellRange(start, *cur);
171 if (numChilds == 0)
172 {
173 cell.Child(0, NULL);
174 maxDepth = cell.Level();
175 }
176 else if (cell.Size() < BaseType::MaxBucketSize())
177 {
178 // make cell a leaf
179 for (unsigned int i = 0; i < CellType::NChildren; ++i)
180 {
181 if (!this->ExistChild(cell, i))
182 {
183 continue;
184 }
185 delete &(cell[i]);
186 cell.Child(i, NULL);
187 }
188 cell.Child(0, NULL);
189 maxDepth = cell.Level();
190 }
191 return maxDepth;
192 }
193 }
194 };
195};
196
197#endif
GfxTL::ScalarTypeDeferer< value_type >::ScalarType ScalarType
GfxTL::VectorXD< CellType::Dim, ScalarType > PointType
InheritedStrategyT::template StrategyBase< BaseT > BaseType
T min(T t1, T t2)
Definition gdiam.h:44
T max(T t1, T t2)
Definition gdiam.h:51
PointT::value_type ScalarType
InheritedStrategyT::value_type value_type