1 #ifndef GfxTL__AACUBETREE_HEADER__
2 #define GfxTL__AACUBETREE_HEADER__
13 template<
unsigned int DimT,
class BaseT >
24 for (
size_t i = 0; i < 1 << DimT; ++i)
32 for (
size_t i = 0; i < 1 << DimT; ++i)
41 return *m_children[i];
46 return *m_children[i];
68 template<
class DataStrategyT >
74 :
public DataStrategyT::CellData
77 template<
class BaseT >
84 enum {
Dim = CellType::Dim };
96 template<
class BuildInformationT >
102 template<
class BuildInformationT >
105 BaseType::InitRootBuildInformation(bi);
106 m_rootCube = bi->Cube();
109 template<
class BuildInformationT >
111 const BuildInformationT& bi)
114 template<
class BuildInformationT >
116 BuildInformationT* bi)
const
119 template<
class BuildInformationT >
121 const BuildInformationT& bi)
const
124 template<
class BuildInformationT >
128 template<
class BuildInformationT >
132 template<
class TraversalBaseT >
134 :
public TraversalBaseT
151 template<
class TraversalBaseT >
153 :
public TraversalBaseT
156 typedef typename TraversalBaseT::CubeType
CubeType;
170 template<
class TraversalBaseT >
173 ::template TraversalInformation< TraversalBaseT >
176 template<
class TraversalInformationT >
178 TraversalInformationT* ti)
const
180 BaseType::InitRootTraversalInformation(root, ti);
181 InitCubeRootTraversalInformation(root, ti);
182 InitCenterRootTraversalInformation(root, ti);
185 template<
class TraversalInformationT >
187 const TraversalInformationT& pTi,
unsigned int childIdx,
188 TraversalInformationT* ti)
const
190 BaseType::InitTraversalInformation(parent, pTi, childIdx, ti);
191 InitCubeTraversalInformation(parent, pTi, childIdx, ti);
192 InitCenterTraversalInformation(parent, pTi, childIdx, ti);
195 template<
class TraversalBaseT >
203 template<
class TraversalBaseT >
212 template<
class TraversalInformationT >
213 void InitCubeRootTraversalInformation(
const CellType& root,
214 TraversalInformationT* ti)
const
217 template<
class TraversalBaseT >
218 void InitCubeRootTraversalInformation(
const CellType& root,
219 CellCubeTraversalInformation< TraversalBaseT >* ti)
const
221 ti->Cube() = m_rootCube;
224 template<
class TraversalInformationT >
225 void InitCubeTraversalInformation(
const CellType& parent,
226 const TraversalInformationT& pTi,
227 unsigned int childIdx, TraversalInformationT* ti)
const
230 template<
class TraversalBaseT >
231 void InitCubeTraversalInformation(
const CellType& parent,
232 const CellCubeTraversalInformation< TraversalBaseT >& pTi,
233 unsigned int childIdx, CellCubeTraversalInformation< TraversalBaseT >* ti)
const
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();
242 ti->Cube().Min()[i] = pTi.Cube().Min()[i];
246 template<
class TraversalInformationT >
247 void InitCenterRootTraversalInformation(
const CellType& root,
248 TraversalInformationT* ti)
const
251 template<
class TraversalBaseT >
252 void InitCenterRootTraversalInformation(
const CellType& root,
253 CellCenterTraversalInformation< TraversalBaseT >* ti)
const
255 ti->Cube() = m_rootCube;
258 template<
class TraversalInformationT >
259 void InitCenterTraversalInformation(
const CellType& parent,
260 const TraversalInformationT& pTi,
261 unsigned int childIdx, TraversalInformationT* ti)
const
264 template<
class TraversalBaseT >
265 void InitCenterTraversalInformation(
const CellType& parent,
266 const CellCenterTraversalInformation< TraversalBaseT >& pTi,
267 unsigned int childIdx, CellCenterTraversalInformation< TraversalBaseT >* ti)
const
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();
276 ti->Cube().Min()[i] = pTi.Cube().Min()[i];
285 template<
unsigned int DimT,
class StrategiesT,
286 template<
unsigned int >
class VectorKernelT = VectorKernelD >
288 :
public StrategiesT::template StrategyBase
290 typename VectorKernelT< DimT >::template VectorKernelType
294 AACubeTreeCell< DimT, typename StrategiesT::CellData >
302 typedef typename StrategiesT::template StrategyBase
304 typename VectorKernelT< DimT >::template VectorKernelType
320 bbox.
Bound(BaseType::begin(), BaseType::end());
322 for (
unsigned int i = 1; i < DimT; ++i)
332 typedef std::pair< CellType*, BuildInformation > Pair;
335 std::deque< Pair > stack(1);
338 stack.back().first = BaseType::Root();
340 this->InitRoot(stack.back().second, BaseType::Root());
341 BaseType::InitGlobalBuildInformation(*BaseType::Root(), stack.back().second);
344 Pair& p = stack.back();
345 if (p.second.CreateChild() == 1 << DimT)
347 BaseType::LeaveGlobalBuildInformation(*p.first, p.second);
351 if (this->IsLeaf(*p.first))
353 if (!this->ShouldSubdivide(*p.first, p.second))
355 BaseType::InitLeaf(p.first, p.second);
360 if (this->IsLeaf(*p.first))
362 BaseType::InitLeaf(p.first, p.second);
366 BaseType::InitSubdivided(p.second, p.first);
370 BaseType::LeaveGlobalBuildInformation(*p.first, p.second);
372 while (p.second.CreateChild() < (1 << DimT) &&
373 !this->ExistChild(*p.first, p.second.CreateChild()))
375 ++p.second.CreateChild();
377 if (p.second.CreateChild() == (1 << DimT))
382 BaseType::EnterGlobalBuildInformation(*p.first, &p.second);
383 stack.resize(stack.size() + 1);
384 stack.back().first = &(*p.first)[p.second.CreateChild()];
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()]);
391 ++p.second.CreateChild();
393 while (p.second.CreateChild() < (1 << DimT) &&
394 !this->ExistChild(*p.first, p.second.CreateChild()));
398 template<
class Po
intT >
400 size_t maxLevel,
size_t minSize,
CellRange* range)
const
402 if (!BaseType::Root())
408 typedef typename BaseType::template CellLevelTraversalInformation
410 typename BaseType::template CellCenterTraversalInformation
412 typename BaseType::template TraversalInformationBase< NullClass >
415 TraversalInfoType rti;
416 BaseType::InitRootTraversalInformation(*BaseType::Root(), &rti);
417 BaseType::GetCellRange(*BaseType::Root(), rti, range);
424 typedef std::pair< const CellType*, SerializeInformation > Pair;
425 std::deque< Pair > stack(1);
428 stack.back().first = BaseType::Root();
432 Pair& p = stack.back();
433 if (p.second.CreateChild() == 1 << DimT)
438 if (!ShouldSubdivide(*p.first, p.second))
443 if (!p.second.Written())
445 const size_t byteCount =
446 ((1 << BaseType::m_dim) / 8) + (((1 << BaseType::m_dim) % 8) ? 1 : 0);
448 for (
unsigned int i = 0; i < (1 << DimT); ++i)
449 if (ExistChild(*p.first, i))
451 b[i >> 3] |= 1 << (i - ((i >> 3) << 3));
455 b[i >> 3] &= ~(1 << (i - ((i >> 3) << 3)));
457 out->write(b, byteCount);
458 p.second.Written(
true);
462 LeaveGlobalBuildInformation(*p.first, p.second);
464 while (p.second.CreateChild() < (1 << DimT) &&
465 !ExistChild(*p.first, p.second.CreateChild()))
467 ++p.second.CreateChild();
469 if (p.second.CreateChild() == (1 << DimT))
473 EnterGlobalBuildInformation(*p.first, &p.second);
474 stack.resize(stack.size() + 1);
475 stack.back().first = &(*p.first)[p.second.CreateChild()];
477 p.second.CreateChild(), &stack.back().second);
480 ++p.second.CreateChild();
482 while (p.second.CreateChild() < (1 << DimT) &&
483 !ExistChild(*p.first, p.second.CreateChild()));
489 typedef std::pair< const CellType*, SerializeInformation > Pair;
490 std::deque< Pair > queue(1);
493 queue.back().first = BaseType::Root();
497 Pair& p = queue.front();
498 if (p.second.CreateChild() == 1 << DimT)
503 if (!ShouldSubdivide(*p.first, p.second))
508 if (!p.second.Written())
510 const size_t byteCount =
511 ((1 << BaseType::m_dim) / 8) + (((1 << BaseType::m_dim) % 8) ? 1 : 0);
513 for (
unsigned int i = 0; i < (1 << DimT); ++i)
514 if (ExistChild(*p.first, i))
516 b[i >> 3] |= 1 << (i - ((i >> 3) << 3));
520 b[i >> 3] &= ~(1 << (i - ((i >> 3) << 3)));
522 out->write(b, byteCount);
523 p.second.Written(
true);
525 while (p.second.CreateChild() < (1 << DimT) &&
526 !ExistChild(*p.first, p.second.CreateChild()))
528 ++p.second.CreateChild();
530 if (p.second.CreateChild() == (1 << DimT))
534 queue.resize(queue.size() + 1);
535 queue.back().first = &(*p.first)[p.second.CreateChild()];
537 p.second.CreateChild(), &queue.back().second);
540 ++p.second.CreateChild();
542 while (p.second.CreateChild() < (1 << DimT) &&
543 !ExistChild(*p.first, p.second.CreateChild()));
556 & bcube, std::istream* in)
558 typedef std::pair< CellType*, BuildInformation > Pair;
561 std::deque< Pair > stack(1);
564 stack.back().first = BaseType::Root();
566 InitRoot(stack.back().second, BaseType::Root());
569 Pair& p = stack.back();
570 if (p.second.CreateChild() == 1 << DimT)
572 LeaveGlobalBuildInformation(*p.first, p.second);
576 if (IsLeaf(*p.first))
578 if (!ShouldSubdivide(*p.first, p.second))
584 const size_t byteCount =
585 ((1 << DimT) / 8) + (((1 << DimT) % 8) ? 1 : 0);
587 in->read(b, byteCount);
588 for (
size_t i = 0; i < (1 << DimT); ++i)
590 const size_t cmpB = 1 << (i - ((i >> 3) << 3));
591 if (b[i >> 3] & cmpB)
593 p.first->Children()[i] =
new CellType();
597 p.first->Children()[i] = (
CellType*)1;
601 if (IsLeaf(*p.first))
609 LeaveGlobalBuildInformation(*p.first, p.second);
611 while (p.second.CreateChild() < (1 << DimT) &&
612 !ExistChild(*p.first, p.second.CreateChild()))
614 ++p.second.CreateChild();
616 if (p.second.CreateChild() == (1 << DimT))
620 EnterGlobalBuildInformation(*p.first, &p.second);
621 stack.resize(stack.size() + 1);
622 stack.back().first = &(*p.first)[p.second.CreateChild()];
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()]);
629 ++p.second.CreateChild();
631 while (p.second.CreateChild() < (1 << DimT) &&
632 !ExistChild(*p.first, p.second.CreateChild()));
645 & bcube, std::istream* in)
647 typedef std::pair< CellType*, BuildInformation > Pair;
650 std::deque< Pair > queue(1);
653 queue.back().first = BaseType::Root();
655 InitRoot(queue.back().second, BaseType::Root());
658 Pair& p = queue.front();
659 if (p.second.CreateChild() == 1 << DimT)
664 if (IsLeaf(*p.first))
666 if (!ShouldSubdivide(*p.first, p.second))
672 const size_t byteCount =
673 ((1 << DimT) / 8) + (((1 << DimT) % 8) ? 1 : 0);
675 in->read(b, byteCount);
676 for (
size_t i = 0; i < (1 << DimT); ++i)
678 const size_t cmpB = 1 << (i - ((i >> 3) << 3));
679 if (b[i >> 3] & cmpB)
681 p.first->Children()[i] =
new CellType();
685 p.first->Children()[i] = (
CellType*)1;
689 if (IsLeaf(*p.first))
695 while (p.second.CreateChild() < (1 << DimT) &&
696 !ExistChild(*p.first, p.second.CreateChild()))
698 ++p.second.CreateChild();
700 if (p.second.CreateChild() == (1 << DimT))
704 queue.resize(queue.size() + 1);
705 queue.back().first = &(*p.first)[p.second.CreateChild()];
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()]);
712 ++p.second.CreateChild();
714 while (p.second.CreateChild() < (1 << DimT) &&
715 !ExistChild(*p.first, p.second.CreateChild()));
721 :
public BaseType::BuildInformation
726 return m_createChild;
730 return m_createChild;
742 unsigned int m_createChild;
747 :
public BaseType::BuildInformation
752 return m_createChild;
756 return m_createChild;
768 unsigned int m_createChild;
788 template<
class VectorT >
791 return v[m_axis] <= m_value;
805 BaseType::InitRootBuildInformation(bi);
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();
820 bi->
Cube().Min()[i] = parentInfo.
Cube().Min()[i];
822 BaseType::InitBuildInformation(parent, parentInfo, childIdx,
830 BaseType::InitRootBuildInformation(bi);
839 BaseType::InitBuildInformation(parent, parentInfo, childIdx,
847 bi.
Cube().Center(¢er);
849 for (
unsigned int i = 0; i < DimT; ++i)
851 splitters[i].
Axis() = i;
852 splitters[i].
Value() = center[i];
854 BaseType::SplitData(splitters, DimT, *cell, bi, cell->
Children());
859 const size_t byteCount =
860 ((1 << BaseType::m_dim) / 8) + (((1 << DimT) % 8) ? 1 : 0);
863 for (
unsigned int i = 0; i < (1 << DimT); ++i)
864 if (!ExistChild(*cell, i))
867 b[i >> 3] |= 1 << (i - ((i >> 3) << 3));
871 b[i >> 3] &= ~(1 << (i - ((i >> 3) << 3)));
875 bi.
Cube().Center(¢er);
877 for (
unsigned int i = 0; i < DimT; ++i)
879 splitters[i].
Axis() = i;
880 splitters[i].
Value() = center[i];
882 SplitData(splitters, DimT, bi, cell->
Children());
883 for (
unsigned int i = 0; i < (1 << DimT); ++i)
886 (b[i >> 3] & (1 << (i - ((i >> 3) << 3)))))
888 std::cout <<
"Bug in distribute!" << std::endl;
891 (b[i >> 3] & (1 << (i - ((i >> 3) << 3)))))
899 template<
class Po
intT,
class TraversalInformationT >
901 size_t maxLevel,
size_t minSize,
const CellType& cell,
902 const TraversalInformationT& ti,
CellRange* range)
const
904 if (this->IsLeaf(cell))
906 this->GetCellRange(cell, ti, range);
909 if (this->CellLevel(cell, ti) == maxLevel)
911 this->GetCellRange(cell, ti, range);
918 for (
unsigned int i = 0; i < DimT; ++i)
920 if (point[i] > cell.Center()[i])
922 childIdx |= 1 << (DimT - i - 1);
925 if (this->ExistChild(cell, childIdx)
926 && cell[childIdx].Size() >= minSize)
928 TraversalInformationT cti;
929 BaseType::InitTraversalInformation(cell, ti, childIdx, &cti);
931 cell[childIdx], cti, range);
933 this->GetCellRange(cell, ti, range);