AACubeTree.h
Go to the documentation of this file.
1 #ifndef GfxTL__AACUBETREE_HEADER__
2 #define GfxTL__AACUBETREE_HEADER__
3 #include <deque>
4 #include <GfxTL/BaseTree.h>
5 #include <GfxTL/AACube.h>
6 #include <GfxTL/AABox.h>
7 #include <GfxTL/VectorKernel.h>
8 #include <GfxTL/NullClass.h>
9 #include <iostream>
10 
11 namespace GfxTL
12 {
13  template< unsigned int DimT, class BaseT >
15  : public BaseT
16  {
17  public:
18  enum { Dim = DimT, NChildren = 1 << DimT };
19  typedef BaseT BaseType;
21 
23  {
24  for (size_t i = 0; i < 1 << DimT; ++i)
25  {
26  m_children[i] = NULL;
27  }
28  }
29 
31  {
32  for (size_t i = 0; i < 1 << DimT; ++i)
33  if (m_children[i] > (ThisType*)1)
34  {
35  delete m_children[i];
36  }
37  }
38 
39  ThisType& operator[](unsigned int i)
40  {
41  return *m_children[i];
42  }
43 
44  const ThisType& operator[](unsigned int i) const
45  {
46  return *m_children[i];
47  }
48 
49  void Child(unsigned int i, ThisType* cell)
50  {
51  m_children[i] = cell;
52  }
53 
54  ThisType** const Children()
55  {
56  return m_children;
57  }
58 
59  const ThisType* const* const Children() const
60  {
61  return m_children;
62  }
63 
64  private:
65  ThisType* m_children[1 << DimT];
66  };
67 
68  template< class DataStrategyT >
70  {
71  typedef typename DataStrategyT::value_type value_type;
72 
73  class CellData
74  : public DataStrategyT::CellData
75  {};
76 
77  template< class BaseT >
79  : public DataStrategyT::template StrategyBase< BaseT >
80  {
81 public:
82  typedef typename DataStrategyT::template StrategyBase< BaseT > BaseType;
83  typedef typename BaseType::CellType CellType;
84  enum { Dim = CellType::Dim };
89 
90  const CubeType& RootCube() const
91  {
92  return m_rootCube;
93  }
94 
95 protected:
96  template< class BuildInformationT >
97  bool ShouldSubdivide(const CellType& cell, BuildInformationT& bi) const
98  {
99  return false;
100  }
101 
102  template< class BuildInformationT >
103  void InitRootBuildInformation(BuildInformationT* bi)
104  {
105  BaseType::InitRootBuildInformation(bi);
106  m_rootCube = bi->Cube();
107  }
108 
109  template< class BuildInformationT >
111  const BuildInformationT& bi)
112  {}
113 
114  template< class BuildInformationT >
116  BuildInformationT* bi) const
117  {}
118 
119  template< class BuildInformationT >
121  const BuildInformationT& bi) const
122  {}
123 
124  template< class BuildInformationT >
125  void InitLeaf(CellType* cell, const BuildInformationT& bi)
126  {}
127 
128  template< class BuildInformationT >
129  void InitSubdivided(const BuildInformationT& bi, CellType* cell)
130  {}
131 
132  template< class TraversalBaseT >
134  : public TraversalBaseT
135  {
136  public:
139  {
140  return m_cube;
141  }
142  const CubeType& Cube() const
143  {
144  return m_cube;
145  }
146 
147  private:
148  CubeType m_cube;
149  };
150 
151  template< class TraversalBaseT >
153  : public TraversalBaseT
154  {
155  public:
156  typedef typename TraversalBaseT::CubeType CubeType;
158  {
159  return m_cube;
160  }
161  const CubeType& Cube() const
162  {
163  return m_cube;
164  }
165 
166  private:
167  CubeType m_cube;
168  };
169 
170  template< class TraversalBaseT >
172  : public DataStrategyT::template StrategyBase< BaseT >
173  ::template TraversalInformation< TraversalBaseT >
174  {};
175 
176  template< class TraversalInformationT >
178  TraversalInformationT* ti) const
179  {
180  BaseType::InitRootTraversalInformation(root, ti);
181  InitCubeRootTraversalInformation(root, ti);
182  InitCenterRootTraversalInformation(root, ti);
183  }
184 
185  template< class TraversalInformationT >
186  void InitTraversalInformation(const CellType& parent,
187  const TraversalInformationT& pTi, unsigned int childIdx,
188  TraversalInformationT* ti) const
189  {
190  BaseType::InitTraversalInformation(parent, pTi, childIdx, ti);
191  InitCubeTraversalInformation(parent, pTi, childIdx, ti);
192  InitCenterTraversalInformation(parent, pTi, childIdx, ti);
193  }
194 
195  template< class TraversalBaseT >
196  void CellCube(const CellType& cell,
198  CellCubeType* cube) const
199  {
200  *cube = ti.Cube();
201  }
202 
203  template< class TraversalBaseT >
204  void CellCenter(const CellType& cell,
206  CellCenterType* center) const
207  {
208  ti.Cube().Center(center);
209  }
210 
211 private:
212  template< class TraversalInformationT >
213  void InitCubeRootTraversalInformation(const CellType& root,
214  TraversalInformationT* ti) const
215  {}
216 
217  template< class TraversalBaseT >
218  void InitCubeRootTraversalInformation(const CellType& root,
219  CellCubeTraversalInformation< TraversalBaseT >* ti) const
220  {
221  ti->Cube() = m_rootCube;
222  }
223 
224  template< class TraversalInformationT >
225  void InitCubeTraversalInformation(const CellType& parent,
226  const TraversalInformationT& pTi,
227  unsigned int childIdx, TraversalInformationT* ti) const
228  {}
229 
230  template< class TraversalBaseT >
231  void InitCubeTraversalInformation(const CellType& parent,
232  const CellCubeTraversalInformation< TraversalBaseT >& pTi,
233  unsigned int childIdx, CellCubeTraversalInformation< TraversalBaseT >* ti) const
234  {
235  ti->Cube().Width(pTi.Cube().Width() / ScalarType(2));
236  for (unsigned int i = 0; i < Dim; ++i)
237  if (childIdx & (1 << (Dim - 1 - i)))
238  ti->Cube().Min()[i] =
239  pTi.Cube().Min()[i] + ti->Cube().Width();
240  else
241  {
242  ti->Cube().Min()[i] = pTi.Cube().Min()[i];
243  }
244  }
245 
246  template< class TraversalInformationT >
247  void InitCenterRootTraversalInformation(const CellType& root,
248  TraversalInformationT* ti) const
249  {}
250 
251  template< class TraversalBaseT >
252  void InitCenterRootTraversalInformation(const CellType& root,
253  CellCenterTraversalInformation< TraversalBaseT >* ti) const
254  {
255  ti->Cube() = m_rootCube;
256  }
257 
258  template< class TraversalInformationT >
259  void InitCenterTraversalInformation(const CellType& parent,
260  const TraversalInformationT& pTi,
261  unsigned int childIdx, TraversalInformationT* ti) const
262  {}
263 
264  template< class TraversalBaseT >
265  void InitCenterTraversalInformation(const CellType& parent,
266  const CellCenterTraversalInformation< TraversalBaseT >& pTi,
267  unsigned int childIdx, CellCenterTraversalInformation< TraversalBaseT >* ti) const
268  {
269  ti->Cube().Width(pTi.Cube().Width() / ScalarType(2));
270  for (unsigned int i = 0; i < Dim; ++i)
271  if (childIdx & (1 << (Dim - 1 - i)))
272  ti->Cube().Min()[i] =
273  pTi.Cube().Min()[i] + ti->Cube().Width();
274  else
275  {
276  ti->Cube().Min()[i] = pTi.Cube().Min()[i];
277  }
278  }
279 
280 private:
281  CubeType m_rootCube;
282  };
283  };
284 
285  template< unsigned int DimT, class StrategiesT,
286  template< unsigned int > class VectorKernelT = VectorKernelD >
288  : public StrategiesT::template StrategyBase
289  <
290  typename VectorKernelT< DimT >::template VectorKernelType
291  <
292  BaseTree
293  <
294  AACubeTreeCell< DimT, typename StrategiesT::CellData >
295  >
296  >
297  >
298  {
299 public:
300  typedef StrategiesT StrategiesType;
302  typedef typename StrategiesT::template StrategyBase
303  <
304  typename VectorKernelT< DimT >::template VectorKernelType
305  <
306  BaseTree
307  <
309  >
310  >
313  typedef typename BaseType::value_type value_type;
315  typedef typename BaseType::CellRange CellRange;
316 
317  void Build()
318  {
320  bbox.Bound(BaseType::begin(), BaseType::end());
321  ScalarType width = bbox.Max()[0] - bbox.Min()[0];
322  for (unsigned int i = 1; i < DimT; ++i)
323  {
324  width = std::max(width, bbox.Max()[i] - bbox.Min()[i]);
325  }
326  AACube< VectorXD< DimT, ScalarType > > bcube(bbox.Min(), width);
327  Build(bcube);
328  }
329 
331  {
332  typedef std::pair< CellType*, BuildInformation > Pair;
333  BaseType::Clear();
334  BaseType::Root() = new CellType;
335  std::deque< Pair > stack(1);
336  // init build information directly on stack to avoid
337  // copying.
338  stack.back().first = BaseType::Root();
339  this->InitRootBuildInformation(bcube, &stack.back().second);
340  this->InitRoot(stack.back().second, BaseType::Root());
341  BaseType::InitGlobalBuildInformation(*BaseType::Root(), stack.back().second);
342  while (stack.size())
343  {
344  Pair& p = stack.back();
345  if (p.second.CreateChild() == 1 << DimT)
346  {
347  BaseType::LeaveGlobalBuildInformation(*p.first, p.second);
348  stack.pop_back();
349  continue;
350  }
351  if (this->IsLeaf(*p.first))
352  {
353  if (!this->ShouldSubdivide(*p.first, p.second))
354  {
355  BaseType::InitLeaf(p.first, p.second);
356  stack.pop_back();
357  continue;
358  }
359  Subdivide(p.second, p.first);
360  if (this->IsLeaf(*p.first)) // couldn't subdivide?
361  {
362  BaseType::InitLeaf(p.first, p.second);
363  stack.pop_back();
364  continue;
365  }
366  BaseType::InitSubdivided(p.second, p.first);
367  }
368  else
369  {
370  BaseType::LeaveGlobalBuildInformation(*p.first, p.second);
371  }
372  while (p.second.CreateChild() < (1 << DimT) &&
373  !this->ExistChild(*p.first, p.second.CreateChild()))
374  {
375  ++p.second.CreateChild();
376  }
377  if (p.second.CreateChild() == (1 << DimT))
378  {
379  stack.pop_back();
380  continue;
381  }
382  BaseType::EnterGlobalBuildInformation(*p.first, &p.second);
383  stack.resize(stack.size() + 1); // create new entry
384  stack.back().first = &(*p.first)[p.second.CreateChild()];
385  this->InitBuildInformation(*p.first, p.second,
386  p.second.CreateChild(), &stack.back().second);
387  this->InitCell(*p.first, p.second, p.second.CreateChild(),
388  stack.back().second, &(*p.first)[p.second.CreateChild()]);
389  do
390  {
391  ++p.second.CreateChild();
392  }
393  while (p.second.CreateChild() < (1 << DimT) &&
394  !this->ExistChild(*p.first, p.second.CreateChild()));
395  }
396  }
397 
398  template< class PointT >
399  const CellType* NodeContainingPoint(const PointT& point,
400  size_t maxLevel, size_t minSize, CellRange* range) const
401  {
402  if (!BaseType::Root())
403  {
404  range->first = 0;
405  range->second = 0;
406  return NULL;
407  }
408  typedef typename BaseType::template CellLevelTraversalInformation
409  <
410  typename BaseType::template CellCenterTraversalInformation
411  <
412  typename BaseType::template TraversalInformationBase< NullClass >
413  >
414  > TraversalInfoType;
415  TraversalInfoType rti;
416  BaseType::InitRootTraversalInformation(*BaseType::Root(), &rti);
417  BaseType::GetCellRange(*BaseType::Root(), rti, range);
418  return NodeContainingPoint(point, maxLevel, minSize, *BaseType::Root(), rti,
419  range);
420  }
421 
422  void Serialize(std::ostream* out) const
423  {
424  typedef std::pair< const CellType*, SerializeInformation > Pair;
425  std::deque< Pair > stack(1);
426  // init build information directly on stack to avoid
427  // copying.
428  stack.back().first = BaseType::Root();
429  InitRootSerializeInformation(&stack.back().second);
430  while (stack.size())
431  {
432  Pair& p = stack.back();
433  if (p.second.CreateChild() == 1 << DimT)
434  {
435  stack.pop_back();
436  continue;
437  }
438  if (!ShouldSubdivide(*p.first, p.second))
439  {
440  stack.pop_back();
441  continue;
442  }
443  if (!p.second.Written())
444  {
445  const size_t byteCount =
446  ((1 << BaseType::m_dim) / 8) + (((1 << BaseType::m_dim) % 8) ? 1 : 0);
447  char b[byteCount];
448  for (unsigned int i = 0; i < (1 << DimT); ++i)
449  if (ExistChild(*p.first, i))
450  {
451  b[i >> 3] |= 1 << (i - ((i >> 3) << 3));
452  }
453  else
454  {
455  b[i >> 3] &= ~(1 << (i - ((i >> 3) << 3)));
456  }
457  out->write(b, byteCount);
458  p.second.Written(true);
459  }
460  else
461  {
462  LeaveGlobalBuildInformation(*p.first, p.second);
463  }
464  while (p.second.CreateChild() < (1 << DimT) &&
465  !ExistChild(*p.first, p.second.CreateChild()))
466  {
467  ++p.second.CreateChild();
468  }
469  if (p.second.CreateChild() == (1 << DimT))
470  {
471  continue;
472  }
473  EnterGlobalBuildInformation(*p.first, &p.second);
474  stack.resize(stack.size() + 1); // create new entry
475  stack.back().first = &(*p.first)[p.second.CreateChild()];
476  InitSerializeInformation(*p.first, p.second,
477  p.second.CreateChild(), &stack.back().second);
478  do
479  {
480  ++p.second.CreateChild();
481  }
482  while (p.second.CreateChild() < (1 << DimT) &&
483  !ExistChild(*p.first, p.second.CreateChild()));
484  }
485  }
486 
487  void SerializeBreadthFirst(std::ostream* out)
488  {
489  typedef std::pair< const CellType*, SerializeInformation > Pair;
490  std::deque< Pair > queue(1);
491  // init build information directly on stack to avoid
492  // copying.
493  queue.back().first = BaseType::Root();
494  InitRootSerializeInformation(&queue.back().second);
495  while (queue.size())
496  {
497  Pair& p = queue.front();
498  if (p.second.CreateChild() == 1 << DimT)
499  {
500  queue.pop_front();
501  continue;
502  }
503  if (!ShouldSubdivide(*p.first, p.second))
504  {
505  queue.pop_front();
506  continue;
507  }
508  if (!p.second.Written())
509  {
510  const size_t byteCount =
511  ((1 << BaseType::m_dim) / 8) + (((1 << BaseType::m_dim) % 8) ? 1 : 0);
512  char b[byteCount];
513  for (unsigned int i = 0; i < (1 << DimT); ++i)
514  if (ExistChild(*p.first, i))
515  {
516  b[i >> 3] |= 1 << (i - ((i >> 3) << 3));
517  }
518  else
519  {
520  b[i >> 3] &= ~(1 << (i - ((i >> 3) << 3)));
521  }
522  out->write(b, byteCount);
523  p.second.Written(true);
524  }
525  while (p.second.CreateChild() < (1 << DimT) &&
526  !ExistChild(*p.first, p.second.CreateChild()))
527  {
528  ++p.second.CreateChild();
529  }
530  if (p.second.CreateChild() == (1 << DimT))
531  {
532  continue;
533  }
534  queue.resize(queue.size() + 1); // create new entry
535  queue.back().first = &(*p.first)[p.second.CreateChild()];
536  InitSerializeInformation(*p.first, p.second,
537  p.second.CreateChild(), &queue.back().second);
538  do
539  {
540  ++p.second.CreateChild();
541  }
542  while (p.second.CreateChild() < (1 << DimT) &&
543  !ExistChild(*p.first, p.second.CreateChild()));
544  }
545  }
546 
547  void Serialize(std::istream* in)
548  {
550  min.Zero();
552  Serialize(bcube, in);
553  }
554 
556  & bcube, std::istream* in)
557  {
558  typedef std::pair< CellType*, BuildInformation > Pair;
559  BaseType::Clear();
560  BaseType::Root() = new CellType();
561  std::deque< Pair > stack(1);
562  // init build information directly on stack to avoid
563  // copying.
564  stack.back().first = BaseType::Root();
565  InitRootBuildInformation(bcube, &stack.back().second);
566  InitRoot(stack.back().second, BaseType::Root());
567  while (stack.size())
568  {
569  Pair& p = stack.back();
570  if (p.second.CreateChild() == 1 << DimT)
571  {
572  LeaveGlobalBuildInformation(*p.first, p.second);
573  stack.pop_back();
574  continue;
575  }
576  if (IsLeaf(*p.first))
577  {
578  if (!ShouldSubdivide(*p.first, p.second))
579  {
580  stack.pop_back();
581  continue;
582  }
583  // create the children
584  const size_t byteCount =
585  ((1 << DimT) / 8) + (((1 << DimT) % 8) ? 1 : 0);
586  char b[byteCount];
587  in->read(b, byteCount);
588  for (size_t i = 0; i < (1 << DimT); ++i)
589  {
590  const size_t cmpB = 1 << (i - ((i >> 3) << 3));
591  if (b[i >> 3] & cmpB)
592  {
593  p.first->Children()[i] = new CellType();
594  }
595  else
596  {
597  p.first->Children()[i] = (CellType*)1;
598  }
599  }
600  Distribute(p.second, p.first);
601  if (IsLeaf(*p.first)) // couldn't subdivide?
602  {
603  stack.pop_back();
604  continue;
605  }
606  }
607  else
608  {
609  LeaveGlobalBuildInformation(*p.first, p.second);
610  }
611  while (p.second.CreateChild() < (1 << DimT) &&
612  !ExistChild(*p.first, p.second.CreateChild()))
613  {
614  ++p.second.CreateChild();
615  }
616  if (p.second.CreateChild() == (1 << DimT))
617  {
618  continue;
619  }
620  EnterGlobalBuildInformation(*p.first, &p.second);
621  stack.resize(stack.size() + 1); // create new entry
622  stack.back().first = &(*p.first)[p.second.CreateChild()];
623  InitBuildInformation(*p.first, p.second,
624  p.second.CreateChild(), &stack.back().second);
625  InitCell(*p.first, p.second, p.second.CreateChild(),
626  stack.back().second, &(*p.first)[p.second.CreateChild()]);
627  do
628  {
629  ++p.second.CreateChild();
630  }
631  while (p.second.CreateChild() < (1 << DimT) &&
632  !ExistChild(*p.first, p.second.CreateChild()));
633  }
634  }
635 
636  void SerializeBreadthFirst(std::istream* in)
637  {
639  min.Zero();
641  SerializeBreadthFirst(bcube, in);
642  }
643 
645  & bcube, std::istream* in)
646  {
647  typedef std::pair< CellType*, BuildInformation > Pair;
648  BaseType::Clear();
649  BaseType::Root() = new CellType();
650  std::deque< Pair > queue(1);
651  // init build information directly on stack to avoid
652  // copying.
653  queue.back().first = BaseType::Root();
654  InitRootBuildInformation(bcube, &queue.back().second);
655  InitRoot(queue.back().second, BaseType::Root());
656  while (queue.size())
657  {
658  Pair& p = queue.front();
659  if (p.second.CreateChild() == 1 << DimT)
660  {
661  queue.pop_front();
662  continue;
663  }
664  if (IsLeaf(*p.first))
665  {
666  if (!ShouldSubdivide(*p.first, p.second))
667  {
668  queue.pop_front();
669  continue;
670  }
671  // create the children
672  const size_t byteCount =
673  ((1 << DimT) / 8) + (((1 << DimT) % 8) ? 1 : 0);
674  char b[byteCount];
675  in->read(b, byteCount);
676  for (size_t i = 0; i < (1 << DimT); ++i)
677  {
678  const size_t cmpB = 1 << (i - ((i >> 3) << 3));
679  if (b[i >> 3] & cmpB)
680  {
681  p.first->Children()[i] = new CellType();
682  }
683  else
684  {
685  p.first->Children()[i] = (CellType*)1;
686  }
687  }
688  Distribute(p.second, p.first);
689  if (IsLeaf(*p.first)) // couldn't subdivide?
690  {
691  queue.pop_front();
692  continue;
693  }
694  }
695  while (p.second.CreateChild() < (1 << DimT) &&
696  !ExistChild(*p.first, p.second.CreateChild()))
697  {
698  ++p.second.CreateChild();
699  }
700  if (p.second.CreateChild() == (1 << DimT))
701  {
702  continue;
703  }
704  queue.resize(queue.size() + 1); // create new entry
705  queue.back().first = &(*p.first)[p.second.CreateChild()];
706  InitBuildInformation(*p.first, p.second,
707  p.second.CreateChild(), &queue.back().second);
708  InitCell(*p.first, p.second, p.second.CreateChild(),
709  queue.back().second, &(*p.first)[p.second.CreateChild()]);
710  do
711  {
712  ++p.second.CreateChild();
713  }
714  while (p.second.CreateChild() < (1 << DimT) &&
715  !ExistChild(*p.first, p.second.CreateChild()));
716  }
717  }
718 
719 protected:
721  : public BaseType::BuildInformation
722  {
723  public:
724  unsigned int& CreateChild()
725  {
726  return m_createChild;
727  }
728  const unsigned int CreateChild() const
729  {
730  return m_createChild;
731  }
733  {
734  return m_cube;
735  }
737  {
738  return m_cube;
739  }
740 
741  private:
742  unsigned int m_createChild;
744  };
745 
747  : public BaseType::BuildInformation
748  {
749  public:
750  unsigned int& CreateChild()
751  {
752  return m_createChild;
753  }
754  const unsigned int CreateChild() const
755  {
756  return m_createChild;
757  }
758  bool Written() const
759  {
760  return m_written;
761  }
762  void Written(bool written)
763  {
764  m_written = written;
765  }
766 
767  private:
768  unsigned int m_createChild;
769  bool m_written;
770  };
771 
773  {
774  public:
776  AxisSplitter(unsigned int axis, ScalarType value)
777  : m_axis(axis)
778  , m_value(value)
779  {}
780  unsigned int& Axis()
781  {
782  return m_axis;
783  }
785  {
786  return m_value;
787  }
788  template< class VectorT >
789  bool operator()(const VectorT& v) const
790  {
791  return v[m_axis] <= m_value;
792  }
793 
794  private:
795  unsigned int m_axis;
796  ScalarType m_value;
797  };
798 
800  const AACube< VectorXD< DimT, ScalarType > >& bcube,
801  BuildInformation* bi)
802  {
803  bi->CreateChild() = 0;
804  bi->Cube() = bcube;
805  BaseType::InitRootBuildInformation(bi);
806  }
807 
808  void InitBuildInformation(const CellType& parent,
809  const BuildInformation& parentInfo, unsigned int childIdx,
810  BuildInformation* bi)
811  {
812  bi->CreateChild() = 0;
813  bi->Cube().Width(parentInfo.Cube().Width() / ScalarType(2));
814  for (unsigned int i = 0; i < DimT; ++i)
815  if (childIdx & (1 << (DimT - 1 - i)))
816  bi->Cube().Min()[i] =
817  parentInfo.Cube().Min()[i] + bi->Cube().Width();
818  else
819  {
820  bi->Cube().Min()[i] = parentInfo.Cube().Min()[i];
821  }
822  BaseType::InitBuildInformation(parent, parentInfo, childIdx,
823  bi);
824  }
825 
827  {
828  bi->CreateChild() = 0;
829  bi->Written(false);
830  BaseType::InitRootBuildInformation(bi);
831  }
832 
833  void InitSerializeInformation(const CellType& parent,
834  const SerializeInformation& parentInfo, unsigned int childIdx,
835  SerializeInformation* bi) const
836  {
837  bi->CreateChild() = 0;
838  bi->Written(false);
839  BaseType::InitBuildInformation(parent, parentInfo, childIdx,
840  bi);
841  }
842 
844  {
845  // get the array of splitters
847  bi.Cube().Center(&center);
848  AxisSplitter splitters[DimT];
849  for (unsigned int i = 0; i < DimT; ++i)
850  {
851  splitters[i].Axis() = i;
852  splitters[i].Value() = center[i];
853  }
854  BaseType::SplitData(splitters, DimT, *cell, bi, cell->Children());
855  }
856 
858  {
859  const size_t byteCount =
860  ((1 << BaseType::m_dim) / 8) + (((1 << DimT) % 8) ? 1 : 0);
861  char b[byteCount];
862  // create missing cells
863  for (unsigned int i = 0; i < (1 << DimT); ++i)
864  if (!ExistChild(*cell, i))
865  {
866  cell->Child(i, new CellType);
867  b[i >> 3] |= 1 << (i - ((i >> 3) << 3));
868  }
869  else
870  {
871  b[i >> 3] &= ~(1 << (i - ((i >> 3) << 3)));
872  }
873  // get the array of splitters
875  bi.Cube().Center(&center);
876  AxisSplitter splitters[DimT];
877  for (unsigned int i = 0; i < DimT; ++i)
878  {
879  splitters[i].Axis() = i;
880  splitters[i].Value() = center[i];
881  }
882  SplitData(splitters, DimT, bi, cell->Children());
883  for (unsigned int i = 0; i < (1 << DimT); ++i)
884  {
885  if (cell->Children()[i]->Size() &&
886  (b[i >> 3] & (1 << (i - ((i >> 3) << 3)))))
887  {
888  std::cout << "Bug in distribute!" << std::endl;
889  }
890  if (!cell->Children()[i]->Size() &&
891  (b[i >> 3] & (1 << (i - ((i >> 3) << 3)))))
892  {
893  delete cell->Children()[i];
894  cell->Children()[i] = (CellType*)1;
895  }
896  }
897  }
898 
899  template< class PointT, class TraversalInformationT >
900  const CellType* NodeContainingPoint(const PointT& point,
901  size_t maxLevel, size_t minSize, const CellType& cell,
902  const TraversalInformationT& ti, CellRange* range) const
903  {
904  if (this->IsLeaf(cell))
905  {
906  this->GetCellRange(cell, ti, range);
907  return &cell;
908  }
909  if (this->CellLevel(cell, ti) == maxLevel)
910  {
911  this->GetCellRange(cell, ti, range);
912  return &cell;
913  }
914  // find the child containing the point
915  //typename BaseType::CellCenterType center;
916  //CellCenter(cell, ti, &center);
917  size_t childIdx = 0;
918  for (unsigned int i = 0; i < DimT; ++i)
919  {
920  if (point[i] > cell.Center()[i]) //center[i])
921  {
922  childIdx |= 1 << (DimT - i - 1);
923  }
924  }
925  if (this->ExistChild(cell, childIdx)
926  && cell[childIdx].Size() >= minSize)
927  {
928  TraversalInformationT cti;
929  BaseType::InitTraversalInformation(cell, ti, childIdx, &cti);
930  return NodeContainingPoint(point, maxLevel, minSize,
931  cell[childIdx], cti, range);
932  }
933  this->GetCellRange(cell, ti, range);
934  return &cell;
935  }
936  };
937 };
938 
939 #endif
GfxTL::AACube
Definition: AACube.h:11
GfxTL::AACubeTreeCell::Dim
@ Dim
Definition: AACubeTree.h:18
GfxTL::AACubeTree::SerializeBreadthFirst
void SerializeBreadthFirst(const AACube< VectorXD< DimT, ScalarType > > &bcube, std::istream *in)
Definition: AACubeTree.h:644
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellType
BaseType::CellType CellType
Definition: AACubeTree.h:83
GfxTL::VectorXD< Dim, ScalarType >
GfxTL::AACubeTree::Build
void Build(const AACube< VectorXD< DimT, ScalarType > > &bcube)
Definition: AACubeTree.h:330
GfxTL::AACubeTreeCell::~AACubeTreeCell
~AACubeTreeCell()
Definition: AACubeTree.h:30
GfxTL::ScalarTypeDeferer
Definition: ScalarTypeDeferer.h:12
GfxTL::AABox::Max
Point & Max()
Definition: AABox.hpp:74
GfxTL::AACubeTree::NodeContainingPoint
const CellType * NodeContainingPoint(const PointT &point, size_t maxLevel, size_t minSize, CellRange *range) const
Definition: AACubeTree.h:399
GfxTL::AACubeTree::StrategiesType
StrategiesT StrategiesType
Definition: AACubeTree.h:300
GfxTL::BaseAACubeTreeStrategy::CellData
Definition: AACubeTree.h:73
GfxTL::AACubeTreeCell::AACubeTreeCell
AACubeTreeCell()
Definition: AACubeTree.h:22
GfxTL::BaseAACubeTreeStrategy::value_type
DataStrategyT::value_type value_type
Definition: AACubeTree.h:71
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCenterTraversalInformation
Definition: AACubeTree.h:152
GfxTL::AACubeTree::Distribute
void Distribute(BuildInformation &bi, CellType *cell)
Definition: AACubeTree.h:857
GfxTL::AACubeTreeCell::NChildren
@ NChildren
Definition: AACubeTree.h:18
GfxTL::AACubeTree::BuildInformation
Definition: AACubeTree.h:720
GfxTL::BaseAACubeTreeStrategy::StrategyBase::EnterGlobalBuildInformation
void EnterGlobalBuildInformation(const CellType &cell, BuildInformationT *bi) const
Definition: AACubeTree.h:115
GfxTL::AACubeTree::Serialize
void Serialize(std::istream *in)
Definition: AACubeTree.h:547
GfxTL::AACubeTree::InitRootBuildInformation
void InitRootBuildInformation(const AACube< VectorXD< DimT, ScalarType > > &bcube, BuildInformation *bi)
Definition: AACubeTree.h:799
GfxTL::BaseAACubeTreeStrategy::StrategyBase::ScalarType
ScalarTypeDeferer< value_type >::ScalarType ScalarType
Definition: AACubeTree.h:85
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCubeTraversalInformation::Cube
const CubeType & Cube() const
Definition: AACubeTree.h:142
GfxTL::AACubeTree::Serialize
void Serialize(std::ostream *out) const
Definition: AACubeTree.h:422
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCenter
void CellCenter(const CellType &cell, const CellCenterTraversalInformation< TraversalBaseT > &ti, CellCenterType *center) const
Definition: AACubeTree.h:204
GfxTL::min
MatrixXX< C, R, T > min(const MatrixXX< C, R, T > &a, const MatrixXX< C, R, T > &b)
Definition: MatrixXX.h:568
AACube.h
GfxTL::BaseAACubeTreeStrategy::StrategyBase::LeaveGlobalBuildInformation
void LeaveGlobalBuildInformation(const CellType &cell, const BuildInformationT &bi) const
Definition: AACubeTree.h:120
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCenterTraversalInformation::Cube
CubeType & Cube()
Definition: AACubeTree.h:157
GfxTL::AACubeTree::CellType
AACubeTreeCell< DimT, typename StrategiesT::CellData > CellType
Definition: AACubeTree.h:301
GfxTL::AACubeTree::AxisSplitter::AxisSplitter
AxisSplitter(unsigned int axis, ScalarType value)
Definition: AACubeTree.h:776
GfxTL::BaseAACubeTreeStrategy::StrategyBase::BaseType
DataStrategyT::template StrategyBase< BaseT > BaseType
Definition: AACubeTree.h:82
GfxTL::AACube::Center
void Center(Point *c) const
Definition: AACube.hpp:53
GfxTL::AACubeTree::BuildInformation::Cube
AACube< VectorXD< DimT, ScalarType > > & Cube()
Definition: AACubeTree.h:736
GfxTL::BaseAACubeTreeStrategy::StrategyBase::InitRootBuildInformation
void InitRootBuildInformation(BuildInformationT *bi)
Definition: AACubeTree.h:103
GfxTL::AACubeTree::AxisSplitter::operator()
bool operator()(const VectorT &v) const
Definition: AACubeTree.h:789
GfxTL::AACubeTree::NodeContainingPoint
const CellType * NodeContainingPoint(const PointT &point, size_t maxLevel, size_t minSize, const CellType &cell, const TraversalInformationT &ti, CellRange *range) const
Definition: AACubeTree.h:900
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCenterTraversalInformation::Cube
const CubeType & Cube() const
Definition: AACubeTree.h:161
GfxTL::AACubeTree::InitBuildInformation
void InitBuildInformation(const CellType &parent, const BuildInformation &parentInfo, unsigned int childIdx, BuildInformation *bi)
Definition: AACubeTree.h:808
GfxTL::AACubeTree::AxisSplitter
Definition: AACubeTree.h:772
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
GfxTL::BaseAACubeTreeStrategy::StrategyBase::RootCube
const CubeType & RootCube() const
Definition: AACubeTree.h:90
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCubeTraversalInformation
Definition: AACubeTree.h:133
GfxTL::BaseAACubeTreeStrategy::StrategyBase::InitTraversalInformation
void InitTraversalInformation(const CellType &parent, const TraversalInformationT &pTi, unsigned int childIdx, TraversalInformationT *ti) const
Definition: AACubeTree.h:186
GfxTL::AACubeTreeCell::Child
void Child(unsigned int i, ThisType *cell)
Definition: AACubeTree.h:49
GfxTL::AACubeTreeCell::ThisType
AACubeTreeCell< DimT, BaseT > ThisType
Definition: AACubeTree.h:20
GfxTL::BaseAACubeTreeStrategy
Definition: AACubeTree.h:69
GfxTL::BaseTree
Definition: BaseTree.h:9
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCenterType
GfxTL::VectorXD< Dim, ScalarType > CellCenterType
Definition: AACubeTree.h:88
GfxTL::AACubeTree::Serialize
void Serialize(const AACube< VectorXD< DimT, ScalarType > > &bcube, std::istream *in)
Definition: AACubeTree.h:555
AABox.h
BaseTree.h
armarx::PointT
pcl::PointXYZRGBL PointT
Definition: Common.h:28
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCubeTraversalInformation::CubeType
AACube< VectorXD< Dim, ScalarType > > CubeType
Definition: AACubeTree.h:137
GfxTL::AACubeTree::ScalarType
ScalarTypeDeferer< value_type >::ScalarType ScalarType
Definition: AACubeTree.h:314
GfxTL::AACubeTreeCell::Children
const ThisType *const *const Children() const
Definition: AACubeTree.h:59
max
T max(T t1, T t2)
Definition: gdiam.h:48
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCube
void CellCube(const CellType &cell, const CellCubeTraversalInformation< TraversalBaseT > &ti, CellCubeType *cube) const
Definition: AACubeTree.h:196
GfxTL::AACubeTree::SerializeInformation::Written
void Written(bool written)
Definition: AACubeTree.h:762
GfxTL::AACubeTree::CellRange
BaseType::CellRange CellRange
Definition: AACubeTree.h:315
GfxTL::AACubeTree::BuildInformation::CreateChild
unsigned int & CreateChild()
Definition: AACubeTree.h:724
GfxTL::AACubeTree::value_type
BaseType::value_type value_type
Definition: AACubeTree.h:313
GfxTL
Definition: AABox.h:8
GfxTL::AACubeTree::Subdivide
void Subdivide(BuildInformation &bi, CellType *cell)
Definition: AACubeTree.h:843
GfxTL::AABox::Bound
void Bound(const Points &points, size_t size)
Definition: AABox.h:74
GfxTL::BaseAACubeTreeStrategy::StrategyBase::InitGlobalBuildInformation
void InitGlobalBuildInformation(const CellType &root, const BuildInformationT &bi)
Definition: AACubeTree.h:110
GfxTL::BaseAACubeTreeStrategy::StrategyBase
Definition: AACubeTree.h:78
GfxTL::AACubeTreeCell
Definition: AACubeTree.h:14
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
GfxTL::AACubeTree::SerializeBreadthFirst
void SerializeBreadthFirst(std::ostream *out)
Definition: AACubeTree.h:487
GfxTL::BaseAACubeTreeStrategy::StrategyBase::InitRootTraversalInformation
void InitRootTraversalInformation(const CellType &root, TraversalInformationT *ti) const
Definition: AACubeTree.h:177
VectorKernel.h
GfxTL::AABox::Min
Point & Min()
Definition: AABox.hpp:62
GfxTL::BaseAACubeTreeStrategy::StrategyBase::Dim
@ Dim
Definition: AACubeTree.h:84
GfxTL::AACubeTree::BuildInformation::Cube
const AACube< VectorXD< DimT, ScalarType > > & Cube() const
Definition: AACubeTree.h:732
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCenterTraversalInformation::CubeType
TraversalBaseT::CubeType CubeType
Definition: AACubeTree.h:156
NullClass.h
GfxTL::AACubeTree::SerializeInformation
Definition: AACubeTree.h:746
GfxTL::AACubeTree::BaseType
StrategiesT::template StrategyBase< typename VectorKernelT< DimT >::template VectorKernelType< BaseTree< AACubeTreeCell< DimT, typename StrategiesT::CellData > > > > BaseType
Definition: AACubeTree.h:311
GfxTL::AACubeTree::SerializeInformation::Written
bool Written() const
Definition: AACubeTree.h:758
GfxTL::AACubeTree::AxisSplitter::AxisSplitter
AxisSplitter()
Definition: AACubeTree.h:775
GfxTL::AACube::Width
ScalarType Width() const
Definition: AACube.hpp:131
GfxTL::AACubeTreeCell::BaseType
BaseT BaseType
Definition: AACubeTree.h:19
GfxTL::AACubeTree::InitRootSerializeInformation
void InitRootSerializeInformation(SerializeInformation *bi) const
Definition: AACubeTree.h:826
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCubeTraversalInformation::Cube
CubeType & Cube()
Definition: AACubeTree.h:138
GfxTL::AACubeTreeCell::operator[]
ThisType & operator[](unsigned int i)
Definition: AACubeTree.h:39
GfxTL::BaseAACubeTreeStrategy::StrategyBase::ShouldSubdivide
bool ShouldSubdivide(const CellType &cell, BuildInformationT &bi) const
Definition: AACubeTree.h:97
GfxTL::BaseAACubeTreeStrategy::StrategyBase::InitSubdivided
void InitSubdivided(const BuildInformationT &bi, CellType *cell)
Definition: AACubeTree.h:129
GfxTL::AACubeTree::SerializeInformation::CreateChild
unsigned int & CreateChild()
Definition: AACubeTree.h:750
GfxTL::AACubeTree::AxisSplitter::Value
ScalarType & Value()
Definition: AACubeTree.h:784
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CellCubeType
CubeType CellCubeType
Definition: AACubeTree.h:87
GfxTL::AACubeTree::SerializeInformation::CreateChild
const unsigned int CreateChild() const
Definition: AACubeTree.h:754
GfxTL::AACubeTree::InitSerializeInformation
void InitSerializeInformation(const CellType &parent, const SerializeInformation &parentInfo, unsigned int childIdx, SerializeInformation *bi) const
Definition: AACubeTree.h:833
GfxTL::BaseAACubeTreeStrategy::StrategyBase::InitLeaf
void InitLeaf(CellType *cell, const BuildInformationT &bi)
Definition: AACubeTree.h:125
GfxTL::BaseAACubeTreeStrategy::StrategyBase::CubeType
AACube< VectorXD< Dim, ScalarType > > CubeType
Definition: AACubeTree.h:86
GfxTL::AACubeTreeCell::operator[]
const ThisType & operator[](unsigned int i) const
Definition: AACubeTree.h:44
GfxTL::AACubeTree::Build
void Build()
Definition: AACubeTree.h:317
GfxTL::AACubeTreeCell::Children
ThisType **const Children()
Definition: AACubeTree.h:54
GfxTL::AACubeTree::ThisType
AACubeTree< DimT, StrategiesT, VectorKernelT > ThisType
Definition: AACubeTree.h:312
GfxTL::AABox
Definition: AABox.h:18
GfxTL::BaseAACubeTreeStrategy::StrategyBase::TraversalInformationBase
Definition: AACubeTree.h:171
GfxTL::AACubeTree::AxisSplitter::Axis
unsigned int & Axis()
Definition: AACubeTree.h:780
GfxTL::AACubeTree::BuildInformation::CreateChild
const unsigned int CreateChild() const
Definition: AACubeTree.h:728
GfxTL::AACubeTree
Definition: AACubeTree.h:287
GfxTL::AACubeTree::SerializeBreadthFirst
void SerializeBreadthFirst(std::istream *in)
Definition: AACubeTree.h:636