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