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,
273 const CellCubeTraversalInformation<TraversalBaseT>& pTi,
274 unsigned int childIdx,
275 CellCubeTraversalInformation<TraversalBaseT>* ti)
const
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(
298 CellCenterTraversalInformation<TraversalBaseT>* ti)
const
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(
316 const CellCenterTraversalInformation<TraversalBaseT>& pTi,
317 unsigned int childIdx,
318 CellCenterTraversalInformation<TraversalBaseT>* ti)
const
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,
337 template <
unsigned int>
class VectorKernelT = VectorKernelD>
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)
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;
870 BaseType::InitRootBuildInformation(bi);
876 unsigned int childIdx,
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);
896 BaseType::InitRootBuildInformation(bi);
902 unsigned int childIdx,
907 BaseType::InitBuildInformation(parent, parentInfo, childIdx, bi);
917 for (
unsigned int i = 0; i < DimT; ++i)
919 splitters[i].
Axis() = 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)));
945 for (
unsigned int i = 0; i < DimT; ++i)
947 splitters[i].
Axis() = 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);