AACubeTree.hpp
Go to the documentation of this file.
1 namespace GfxTL
2 {
3 
4  //-- AACubeCell
5 
6  template< class Point, class Base >
7  AACubeCell< Point, Base >::AACubeCell()
8  : _parent(NULL)
9  {
10  memset(_children, 0, sizeof(_children));
11  }
12 
13  template< class Point, class Base >
14  AACubeCell< Point, Base >::AACubeCell(ThisType* parent,
15  const CubeType& cube)
16  : _parent(parent)
17  , _cube(cube)
18  {
19  memset(_children, 0, sizeof(_children));
20  }
21 
22  template< class Point, class Base >
23  AACubeCell< Point, Base >::~AACubeCell()
24  {
25  for (int i = 0; i < NChildren; ++i)
26  {
27  if (_children[i])
28  {
29  delete _children[i];
30  }
31  }
32  }
33 
34  template< class Point, class Base >
35  const AACubeCell< Point, Base >*
36  AACubeCell< Point, Base >::operator[](unsigned int index) const
37  {
38  return _children[index];
39  }
40 
41  template< class Point, class Base >
42  AACubeCell< Point, Base >*
43  AACubeCell< Point, Base >::operator[](unsigned int index)
44  {
45  return _children[index];
46  }
47 
48  template< class Point, class Base >
49  void AACubeCell< Point, Base >::Child(unsigned int index, ThisType* child)
50  {
51  _children[index] = child;
52  }
53 
54  template< class Point, class Base >
55  typename AACubeCell< Point, Base >::CubeType&
56  AACubeCell< Point, Base >::Cube()
57  {
58  return _cube;
59  }
60 
61  template< class Point, class Base >
62  const typename AACubeCell< Point, Base >::CubeType&
63  AACubeCell< Point, Base >::Cube() const
64  {
65  return _cube;
66  }
67 
68  template< class Point, class Base >
69  AACubeCell< Point, Base >*
70  AACubeCell< Point, Base >::Parent()
71  {
72  return _parent;
73  }
74 
75  template< class Point, class Base >
76  const AACubeCell< Point, Base >*
77  AACubeCell< Point, Base >::Parent() const
78  {
79  return _parent;
80  }
81 
82  template< class Point, class Base >
83  void AACubeCell< Point, Base >::Parent(ThisType* parent)
84  {
85  _parent = parent;
86  }
87 
88  template< class Point, class Base >
89  AACubeCell< Point, Base >*
90  AACubeCell< Point, Base >::FaceNeighborIndexed(unsigned int index,
91  unsigned int* level)
92  {
93  int axis = index >> 1;
94  ++axis;
95  if (index & 1)
96  {
97  axis = -axis;
98  }
99  return FaceNeighbor(axis, level);
100  }
101 
102  template< class Point, class Base >
103  const AACubeCell< Point, Base >*
104  AACubeCell< Point, Base >::FaceNeighborIndexed(unsigned int index,
105  unsigned int* level) const
106  {
107  int axis = index >> 1;
108  ++axis;
109  if (index & 1)
110  {
111  axis = -axis;
112  }
113  return FaceNeighbor(axis, level);
114  }
115 
116  template< class Point, class Base >
117  AACubeCell< Point, Base >*
118  AACubeCell< Point, Base >::FaceNeighbor(int axis, unsigned int* level)
119  {
120  int box = SubBox();
121  if (box < 0)
122  {
123  return NULL;
124  }
125  int a = axis;
126  if (a > 0)
127  {
128  a -= 1;
129  if (box & (1 << a))
130  {
131  return (*_parent)[box & ~(1 << a)];
132  }
133  }
134  if (a < 0)
135  {
136  a = (-a) - 1;
137  if (!(box & (1 << a)))
138  {
139  return (*_parent)[box | (1 << a)];
140  }
141  }
142  unsigned int l = *level;
143  ++(*level);
144  ThisType* c = _parent->FaceNeighbor(axis, level);
145  if (!c)
146  {
147  return NULL;
148  }
149  int invBox = box;
150  if (invBox & (1 << a))
151  {
152  invBox &= ~(1 << a);
153  }
154  else
155  {
156  invBox |= 1 << a;
157  }
158  if (*level == l + 1 && (*c)[invBox])
159  {
160  --(*level);
161  c = (*c)[invBox];
162  }
163  return c;
164  }
165 
166  template< class Point, class Base >
167  const AACubeCell< Point, Base >*
168  AACubeCell< Point, Base >::
169  FaceNeighbor(int axis, unsigned int* level) const
170  {
171  return const_cast< ThisType* >(this)->FaceNeighbor(axis, level);
172  }
173 
174  template< class Point, class Base >
175  int AACubeCell< Point, Base >::SubBox() const
176  {
177  unsigned int box;
178  if (!_parent)
179  {
180  return -1;
181  }
182  if (!(_parent->Cube().IsSubCube(&box, Cube())))
183  {
184  assert(false);
185  return -1;
186  }
187  return (int)box;
188  }
189 
190  //-- AACubeTree
191 
192  template< class Strategies >
193  void AACubeTree< Strategies >::Clear()
194  {
195  StrategyBaseType::Clear();
196  _cellCount = 0;
197  }
198 
199  template< class Strategies >
201  {
202  AABox< PointType > bbox;
203  BoundingVolume(&bbox);
204  PointType center;
205  bbox.Center(&center);
206  ScalarType w = (bbox.Max() - bbox.Min()).Length();
207  PointType bbl;
208  for (unsigned int i = 0; i < PointType::Dim; ++i)
209  {
210  bbl[i] = center[i] - (w / 2);
211  }
212  CubeType bc(bbl, w);
213  /*CubeType bc;
214  BoundingCube(&bc);*/
215  CubeType cc(bc[CubeType::NCorners - 1] * (ScalarType)1.2,
216  bc.Width() * (ScalarType)1.2);
217  Build(cc);
218  }
219 
220  template< class Strategies >
221  void AACubeTree< Strategies >::Build(const CubeType& bc)
222  {
223  Clear();
224 
225  std::queue< CellType* >* q;
226  std::queue< CellType* >* qq;
227  std::queue< CellType* > q1, q2, level;
228 
229  CellType* c = new CellType(NULL, bc);
230  RootCellData(bc, c); // implemented by TreeData
231  q1.push(c);
232  Root(c); // implemented by BaseTree
233  InitCellData(c); // implemented by Strategies
234  q = &q1;
235  qq = &q2;
236 
237  do
238  {
239  while (q->size())
240  {
241  c = q->front();
242  level.push(c);
243  q->pop();
244  //InitCellData(c); // implemented by Strategies
245  if (ShouldSubdivide(*c)) // implemented by Strategies
246  {
247  Subdivide(c);
248  for (int i = 0; i < CellType::NChildren; ++i)
249  {
250  qq->push((*c)[i]);
251  }
252  _cellCount += CellType::NChildren;
253  }
254  }
255  std::queue< CellType* >* qqq = q;
256  q = qq;
257  qq = qqq;
258 
259  while (level.size())
260  {
261  c = level.front();
262  level.pop();
263  InitLevelDependentCellData(c); // implemented by Strategies
264  }
265  }
266  while (q->size());
267  }
268 
269  template< class Strategies >
270  void AACubeTree< Strategies >::Subdivide(CellType* cell)
271  {
272  Subdivide(cell, 0, 0, cell);
273  for (unsigned int i = 0; i < CellType::NChildren; ++i)
274  {
275  InitCellData((*cell)[i]); // implemented by Strategies
276  }
277  }
278 
279  template< class Strategies >
280  void AACubeTree< Strategies >::RefreshWithNewTreeData(const CubeType& bc)
281  {
282  // iterate over all cells and readjust cell data
283  CellType* c = Root();
284  if (!c)
285  {
286  return;
287  }
288  RootCellData(c->Cube(), c);
289  std::list< CellType* > stack;
290  stack.push_back(c);
291  while (stack.size())
292  {
293  c = stack.back();
294  stack.pop_back();
295  if (!IsLeaf(c))
296  {
297  ReadjustChildrenData(c, 0, 0, c);
298  for (unsigned int i = 0; i < CellType::NChildren; ++i)
299  {
300  stack.push_back((*c)[i]);
301  }
302  }
303  }
304  StrategyBaseType::RefreshWithNewTreeData(bc);
305  }
306 
307  template< class Strategies >
308  void AACubeTree< Strategies >::ReadjustChildrenData(CellType* cell,
309  unsigned int axis, unsigned int box, CellType* data)
310  {
311  if (axis == Dim)
312  {
313  (*cell)[box]->Data(*data);
314  }
315  else
316  {
317  ScalarType s;
318  cell->Cube().DividingPlane(axis, &s);
319  CellType left, right;
320  SplitAlongAxis(*data, axis, s, &left, &right); // implemented by TreeData
321 
322  ReadjustChildrenData(cell, axis + 1, box | (1 << axis), &left);
323  ReadjustChildrenData(cell, axis + 1, box & (~(1 << axis)), &right);
324  }
325  }
326 
327  template< class Strategies >
328  void AACubeTree< Strategies >::Subdivide(CellType* cell, unsigned int axis,
329  unsigned int box, CellType* data)
330  {
331  if (axis == Dim)
332  {
333  CellType* child = new CellType;
334  child->Data(*data);
335  child->Parent(cell);
336  child->Cube() = CubeType(box, cell->Cube());
337  cell->Child(box, child);
338  }
339  else
340  {
341  ScalarType s;
342  cell->Cube().DividingPlane(axis, &s);
343  CellType left, right;
344  SplitAlongAxis(*data, axis, s, &left, &right); // implemented by TreeData
345 
346  Subdivide(cell, axis + 1, box | (1 << axis), &left);
347  Subdivide(cell, axis + 1, box & (~(1 << axis)), &right);
348  }
349  }
350 
351 };
visionx::armem::pointcloud::PointType
PointType
Definition: constants.h:78
index
uint8_t index
Definition: EtherCATFrame.h:59
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
q
#define q
GfxTL
Definition: AABox.h:8
GfxTL::AACubeTree::Subdivide
void Subdivide(BuildInformation &bi, CellType *cell)
Definition: AACubeTree.h:843
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
GfxTL::AACubeTree::Build
void Build()
Definition: AACubeTree.h:317