1#ifndef GfxTL__AACUBETREE_HEADER__
2#define GfxTL__AACUBETREE_HEADER__
14 template <
unsigned int DimT,
class BaseT>
29 for (
size_t i = 0; i < 1 << DimT; ++i)
37 for (
size_t i = 0; i < 1 << DimT; ++i)
47 return *m_children[i];
53 return *m_children[i];
78 template <
class DataStrategyT>
83 class CellData :
public DataStrategyT::CellData
87 template <
class BaseT>
111 template <
class BuildInformationT>
118 template <
class BuildInformationT>
122 BaseType::InitRootBuildInformation(bi);
123 m_rootCube = bi->Cube();
126 template <
class BuildInformationT>
132 template <
class BuildInformationT>
138 template <
class BuildInformationT>
144 template <
class BuildInformationT>
150 template <
class BuildInformationT>
156 template <
class TraversalBaseT>
178 template <
class TraversalBaseT>
182 typedef typename TraversalBaseT::CubeType
CubeType;
200 template <
class TraversalBaseT>
202 public DataStrategyT::template
StrategyBase<BaseT>::template TraversalInformation<
207 template <
class TraversalInformationT>
211 BaseType::InitRootTraversalInformation(root, ti);
212 InitCubeRootTraversalInformation(root, ti);
213 InitCenterRootTraversalInformation(root, ti);
216 template <
class TraversalInformationT>
219 const TraversalInformationT& pTi,
220 unsigned int childIdx,
221 TraversalInformationT* ti)
const
223 BaseType::InitTraversalInformation(parent, pTi, childIdx, ti);
224 InitCubeTraversalInformation(parent, pTi, childIdx, ti);
225 InitCenterTraversalInformation(parent, pTi, childIdx, ti);
228 template <
class TraversalBaseT>
237 template <
class TraversalBaseT>
247 template <
class TraversalInformationT>
249 InitCubeRootTraversalInformation(
const CellType& root, TraversalInformationT* ti)
const
253 template <
class TraversalBaseT>
255 InitCubeRootTraversalInformation(
const CellType& root,
256 CellCubeTraversalInformation<TraversalBaseT>* ti)
const
258 ti->Cube() = m_rootCube;
261 template <
class TraversalInformationT>
263 InitCubeTraversalInformation(
const CellType& parent,
264 const TraversalInformationT& pTi,
265 unsigned int childIdx,
266 TraversalInformationT* ti)
const
270 template <
class TraversalBaseT>
272 InitCubeTraversalInformation(
const CellType& parent,
274 unsigned int childIdx,
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();
283 ti->Cube().Min()[i] = pTi.Cube().Min()[i];
287 template <
class TraversalInformationT>
289 InitCenterRootTraversalInformation(
const CellType& root,
290 TraversalInformationT* ti)
const
294 template <
class TraversalBaseT>
296 InitCenterRootTraversalInformation(
300 ti->Cube() = m_rootCube;
303 template <
class TraversalInformationT>
305 InitCenterTraversalInformation(
const CellType& parent,
306 const TraversalInformationT& pTi,
307 unsigned int childIdx,
308 TraversalInformationT* ti)
const
312 template <
class TraversalBaseT>
314 InitCenterTraversalInformation(
317 unsigned int childIdx,
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();
326 ti->Cube().Min()[i] = pTi.Cube().Min()[i];
335 template <
unsigned int DimT,
339 public StrategiesT::template StrategyBase<
340 typename VectorKernelT<DimT>::template VectorKernelType<
341 BaseTree<AACubeTreeCell<DimT, typename StrategiesT::CellData>>>>
346 typedef typename StrategiesT::template StrategyBase<
347 typename VectorKernelT<DimT>::template VectorKernelType<
359 bbox.
Bound(BaseType::begin(), BaseType::end());
361 for (
unsigned int i = 1; i < DimT; ++i)
363 width = std::max(width, bbox.
Max()[i] - bbox.
Min()[i]);
372 typedef std::pair<CellType*, BuildInformation> Pair;
375 std::deque<Pair> stack(1);
378 stack.back().first = BaseType::Root();
380 this->InitRoot(stack.back().second, BaseType::Root());
381 BaseType::InitGlobalBuildInformation(*BaseType::Root(), stack.back().second);
384 Pair& p = stack.back();
385 if (p.second.CreateChild() == 1 << DimT)
387 BaseType::LeaveGlobalBuildInformation(*p.first, p.second);
391 if (this->IsLeaf(*p.first))
393 if (!this->ShouldSubdivide(*p.first, p.second))
395 BaseType::InitLeaf(p.first, p.second);
400 if (this->IsLeaf(*p.first))
402 BaseType::InitLeaf(p.first, p.second);
406 BaseType::InitSubdivided(p.second, p.first);
410 BaseType::LeaveGlobalBuildInformation(*p.first, p.second);
412 while (p.second.CreateChild() < (1 << DimT) &&
413 !this->ExistChild(*p.first, p.second.CreateChild()))
415 ++p.second.CreateChild();
417 if (p.second.CreateChild() == (1 << DimT))
422 BaseType::EnterGlobalBuildInformation(*p.first, &p.second);
423 stack.resize(stack.size() + 1);
424 stack.back().first = &(*p.first)[p.second.CreateChild()];
426 *p.first, p.second, p.second.CreateChild(), &stack.back().second);
427 this->InitCell(*p.first,
429 p.second.CreateChild(),
431 &(*p.first)[p.second.CreateChild()]);
434 ++p.second.CreateChild();
435 }
while (p.second.CreateChild() < (1 << DimT) &&
436 !this->ExistChild(*p.first, p.second.CreateChild()));
440 template <
class Po
intT>
447 if (!BaseType::Root())
453 typedef typename BaseType::template CellLevelTraversalInformation<
454 typename BaseType::template CellCenterTraversalInformation<
455 typename BaseType::template TraversalInformationBase<NullClass>>>
457 TraversalInfoType rti;
458 BaseType::InitRootTraversalInformation(*BaseType::Root(), &rti);
459 BaseType::GetCellRange(*BaseType::Root(), rti, range);
466 typedef std::pair<const CellType*, SerializeInformation> Pair;
467 std::deque<Pair> stack(1);
470 stack.back().first = BaseType::Root();
474 Pair& p = stack.back();
475 if (p.second.CreateChild() == 1 << DimT)
480 if (!ShouldSubdivide(*p.first, p.second))
485 if (!p.second.Written())
487 const size_t byteCount =
488 ((1 << BaseType::m_dim) / 8) + (((1 << BaseType::m_dim) % 8) ? 1 : 0);
490 for (
unsigned int i = 0; i < (1 << DimT); ++i)
491 if (ExistChild(*p.first, i))
493 b[i >> 3] |= 1 << (i - ((i >> 3) << 3));
497 b[i >> 3] &= ~(1 << (i - ((i >> 3) << 3)));
499 out->write(b, byteCount);
500 p.second.Written(
true);
504 LeaveGlobalBuildInformation(*p.first, p.second);
506 while (p.second.CreateChild() < (1 << DimT) &&
507 !ExistChild(*p.first, p.second.CreateChild()))
509 ++p.second.CreateChild();
511 if (p.second.CreateChild() == (1 << DimT))
515 EnterGlobalBuildInformation(*p.first, &p.second);
516 stack.resize(stack.size() + 1);
517 stack.back().first = &(*p.first)[p.second.CreateChild()];
519 *p.first, p.second, p.second.CreateChild(), &stack.back().second);
522 ++p.second.CreateChild();
523 }
while (p.second.CreateChild() < (1 << DimT) &&
524 !ExistChild(*p.first, p.second.CreateChild()));
531 typedef std::pair<const CellType*, SerializeInformation> Pair;
532 std::deque<Pair> queue(1);
535 queue.back().first = BaseType::Root();
539 Pair& p = queue.front();
540 if (p.second.CreateChild() == 1 << DimT)
545 if (!ShouldSubdivide(*p.first, p.second))
550 if (!p.second.Written())
552 const size_t byteCount =
553 ((1 << BaseType::m_dim) / 8) + (((1 << BaseType::m_dim) % 8) ? 1 : 0);
555 for (
unsigned int i = 0; i < (1 << DimT); ++i)
556 if (ExistChild(*p.first, i))
558 b[i >> 3] |= 1 << (i - ((i >> 3) << 3));
562 b[i >> 3] &= ~(1 << (i - ((i >> 3) << 3)));
564 out->write(b, byteCount);
565 p.second.Written(
true);
567 while (p.second.CreateChild() < (1 << DimT) &&
568 !ExistChild(*p.first, p.second.CreateChild()))
570 ++p.second.CreateChild();
572 if (p.second.CreateChild() == (1 << DimT))
576 queue.resize(queue.size() + 1);
577 queue.back().first = &(*p.first)[p.second.CreateChild()];
579 *p.first, p.second, p.second.CreateChild(), &queue.back().second);
582 ++p.second.CreateChild();
583 }
while (p.second.CreateChild() < (1 << DimT) &&
584 !ExistChild(*p.first, p.second.CreateChild()));
600 typedef std::pair<CellType*, BuildInformation> Pair;
603 std::deque<Pair> stack(1);
606 stack.back().first = BaseType::Root();
608 InitRoot(stack.back().second, BaseType::Root());
611 Pair& p = stack.back();
612 if (p.second.CreateChild() == 1 << DimT)
614 LeaveGlobalBuildInformation(*p.first, p.second);
618 if (IsLeaf(*p.first))
620 if (!ShouldSubdivide(*p.first, p.second))
626 const size_t byteCount = ((1 << DimT) / 8) + (((1 << DimT) % 8) ? 1 : 0);
628 in->read(b, byteCount);
629 for (
size_t i = 0; i < (1 << DimT); ++i)
631 const size_t cmpB = 1 << (i - ((i >> 3) << 3));
632 if (b[i >> 3] & cmpB)
634 p.first->Children()[i] =
new CellType();
638 p.first->Children()[i] = (
CellType*)1;
642 if (IsLeaf(*p.first))
650 LeaveGlobalBuildInformation(*p.first, p.second);
652 while (p.second.CreateChild() < (1 << DimT) &&
653 !ExistChild(*p.first, p.second.CreateChild()))
655 ++p.second.CreateChild();
657 if (p.second.CreateChild() == (1 << DimT))
661 EnterGlobalBuildInformation(*p.first, &p.second);
662 stack.resize(stack.size() + 1);
663 stack.back().first = &(*p.first)[p.second.CreateChild()];
665 *p.first, p.second, p.second.CreateChild(), &stack.back().second);
668 p.second.CreateChild(),
670 &(*p.first)[p.second.CreateChild()]);
673 ++p.second.CreateChild();
674 }
while (p.second.CreateChild() < (1 << DimT) &&
675 !ExistChild(*p.first, p.second.CreateChild()));
691 typedef std::pair<CellType*, BuildInformation> Pair;
694 std::deque<Pair> queue(1);
697 queue.back().first = BaseType::Root();
699 InitRoot(queue.back().second, BaseType::Root());
702 Pair& p = queue.front();
703 if (p.second.CreateChild() == 1 << DimT)
708 if (IsLeaf(*p.first))
710 if (!ShouldSubdivide(*p.first, p.second))
716 const size_t byteCount = ((1 << DimT) / 8) + (((1 << DimT) % 8) ? 1 : 0);
718 in->read(b, byteCount);
719 for (
size_t i = 0; i < (1 << DimT); ++i)
721 const size_t cmpB = 1 << (i - ((i >> 3) << 3));
722 if (b[i >> 3] & cmpB)
724 p.first->Children()[i] =
new CellType();
728 p.first->Children()[i] = (
CellType*)1;
732 if (IsLeaf(*p.first))
738 while (p.second.CreateChild() < (1 << DimT) &&
739 !ExistChild(*p.first, p.second.CreateChild()))
741 ++p.second.CreateChild();
743 if (p.second.CreateChild() == (1 << DimT))
747 queue.resize(queue.size() + 1);
748 queue.back().first = &(*p.first)[p.second.CreateChild()];
750 *p.first, p.second, p.second.CreateChild(), &queue.back().second);
753 p.second.CreateChild(),
755 &(*p.first)[p.second.CreateChild()]);
758 ++p.second.CreateChild();
759 }
while (p.second.CreateChild() < (1 << DimT) &&
760 !ExistChild(*p.first, p.second.CreateChild()));
771 return m_createChild;
777 return m_createChild;
793 unsigned int m_createChild;
803 return m_createChild;
809 return m_createChild;
825 unsigned int m_createChild;
852 template <
class VectorT>
856 return v[m_axis] <= m_value;
866 BuildInformation* bi)
868 bi->CreateChild() = 0;
870 BaseType::InitRootBuildInformation(bi);
875 const BuildInformation& parentInfo,
876 unsigned int childIdx,
877 BuildInformation* bi)
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();
886 bi->Cube().Min()[i] = parentInfo.Cube().Min()[i];
888 BaseType::InitBuildInformation(parent, parentInfo, childIdx, bi);
894 bi->CreateChild() = 0;
896 BaseType::InitRootBuildInformation(bi);
901 const SerializeInformation& parentInfo,
902 unsigned int childIdx,
903 SerializeInformation* bi)
const
905 bi->CreateChild() = 0;
907 BaseType::InitBuildInformation(parent, parentInfo, childIdx, bi);
915 bi.Cube().Center(¢er);
916 AxisSplitter splitters[DimT];
917 for (
unsigned int i = 0; i < DimT; ++i)
919 splitters[i].Axis() = i;
920 splitters[i].Value() = center[i];
922 BaseType::SplitData(splitters, DimT, *cell, bi, cell->
Children());
928 const size_t byteCount = ((1 << BaseType::m_dim) / 8) + (((1 << DimT) % 8) ? 1 : 0);
931 for (
unsigned int i = 0; i < (1 << DimT); ++i)
932 if (!ExistChild(*cell, i))
935 b[i >> 3] |= 1 << (i - ((i >> 3) << 3));
939 b[i >> 3] &= ~(1 << (i - ((i >> 3) << 3)));
943 bi.Cube().Center(¢er);
944 AxisSplitter splitters[DimT];
945 for (
unsigned int i = 0; i < DimT; ++i)
947 splitters[i].Axis() = i;
948 splitters[i].Value() = center[i];
950 SplitData(splitters, DimT, bi, cell->
Children());
951 for (
unsigned int i = 0; i < (1 << DimT); ++i)
953 if (cell->
Children()[i]->Size() && (b[i >> 3] & (1 << (i - ((i >> 3) << 3)))))
955 std::cout <<
"Bug in distribute!" << std::endl;
957 if (!cell->
Children()[i]->Size() && (b[i >> 3] & (1 << (i - ((i >> 3) << 3)))))
965 template <
class Po
intT,
class TraversalInformationT>
971 const TraversalInformationT& ti,
974 if (this->IsLeaf(cell))
976 this->GetCellRange(cell, ti, range);
979 if (this->CellLevel(cell, ti) == maxLevel)
981 this->GetCellRange(cell, ti, range);
988 for (
unsigned int i = 0; i < DimT; ++i)
990 if (point[i] > cell.Center()[i])
992 childIdx |= 1 << (DimT - i - 1);
995 if (this->ExistChild(cell, childIdx) && cell[childIdx].Size() >= minSize)
997 TraversalInformationT cti;
998 BaseType::InitTraversalInformation(cell, ti, childIdx, &cti);
1001 this->GetCellRange(cell, ti, range);
void Bound(const Points &points, size_t size)
const ThisType & operator[](unsigned int i) const
const ThisType *const *const Children() const
void Child(unsigned int i, ThisType *cell)
AACubeTreeCell< DimT, BaseT > ThisType
ThisType & operator[](unsigned int i)
ThisType **const Children()
AxisSplitter(unsigned int axis, ScalarType value)
bool operator()(const VectorT &v) const
ScalarTypeDeferer< value_type >::ScalarType ScalarType
BaseType::value_type value_type
void Serialize(std::ostream *out) const
void InitRootBuildInformation(const AACube< VectorXD< DimT, ScalarType > > &bcube, BuildInformation *bi)
StrategiesT StrategiesType
void Build(const AACube< VectorXD< DimT, ScalarType > > &bcube)
void InitRootSerializeInformation(SerializeInformation *bi) const
const CellType * NodeContainingPoint(const PointT &point, size_t maxLevel, size_t minSize, CellRange *range) const
void SerializeBreadthFirst(std::ostream *out)
void Distribute(BuildInformation &bi, CellType *cell)
void InitSerializeInformation(const CellType &parent, const SerializeInformation &parentInfo, unsigned int childIdx, SerializeInformation *bi) const
void Subdivide(BuildInformation &bi, CellType *cell)
const CellType * NodeContainingPoint(const PointT &point, size_t maxLevel, size_t minSize, const CellType &cell, const TraversalInformationT &ti, CellRange *range) const
AACubeTree< DimT, StrategiesT, VectorKernelT > ThisType
void SerializeBreadthFirst(std::istream *in)
void Serialize(std::istream *in)
void SerializeBreadthFirst(const AACube< VectorXD< DimT, ScalarType > > &bcube, std::istream *in)
AACubeTreeCell< DimT, typename StrategiesT::CellData > CellType
void InitBuildInformation(const CellType &parent, const BuildInformation &parentInfo, unsigned int childIdx, BuildInformation *bi)
BaseType::CellRange CellRange
StrategiesT::template StrategyBase< typename VectorKernelT< DimT >::template VectorKernelType< BaseTree< AACubeTreeCell< DimT, typename StrategiesT::CellData > > > > BaseType
void Serialize(const AACube< VectorXD< DimT, ScalarType > > &bcube, std::istream *in)
void Center(Point *c) const
ScalarTypeDeferer< value_type >::ScalarType ScalarType
GfxTL::VectorXD< Dim, ScalarType > CellCenterType
BaseType::CellType CellType
void CellCenter(const CellType &cell, const CellCenterTraversalInformation< TraversalBaseT > &ti, CellCenterType *center) const
void EnterGlobalBuildInformation(const CellType &cell, BuildInformationT *bi) const
void InitRootTraversalInformation(const CellType &root, TraversalInformationT *ti) const
const CubeType & RootCube() const
DataStrategyT::template StrategyBase< BaseT > BaseType
void InitLeaf(CellType *cell, const BuildInformationT &bi)
void InitSubdivided(const BuildInformationT &bi, CellType *cell)
bool ShouldSubdivide(const CellType &cell, BuildInformationT &bi) const
void CellCube(const CellType &cell, const CellCubeTraversalInformation< TraversalBaseT > &ti, CellCubeType *cube) const
void LeaveGlobalBuildInformation(const CellType &cell, const BuildInformationT &bi) const
void InitRootBuildInformation(BuildInformationT *bi)
void InitGlobalBuildInformation(const CellType &root, const BuildInformationT &bi)
AACube< VectorXD< Dim, ScalarType > > CubeType
void InitTraversalInformation(const CellType &parent, const TraversalInformationT &pTi, unsigned int childIdx, TraversalInformationT *ti) const
MatrixXX< C, R, T > min(const MatrixXX< C, R, T > &a, const MatrixXX< C, R, T > &b)
DataStrategyT::value_type value_type
PointT::value_type ScalarType