15 : m_origin(origin), m_halfDimension(halfDimension) {
16 m_min.
x = origin.
x - halfDimension.
x;
17 m_min.
y = origin.
y - halfDimension.
y;
18 m_min.
z = origin.
z - halfDimension.
z;
19 m_max.
x = origin.
x + halfDimension.
x;
20 m_max.
y = origin.
y + halfDimension.
y;
21 m_max.
z = origin.
z + halfDimension.
z;
24 for (
int i = 0; i < 8; ++i) children[i] =
nullptr;
29 for (
int i = 0; i < 8; ++i)
delete children[i];
33bool TetrahedralTree::DoesBoxOverlap(
const double bb[6])
const {
34 if (m_max.
x < bb[0] || m_max.
y < bb[1] || m_max.
z < bb[2])
return false;
35 if (m_min.
x > bb[3] || m_min.
y > bb[4] || m_min.
z > bb[5])
return false;
40int TetrahedralTree::GetOctantContainingPoint(
const Vec3& point)
const {
42 if (point.x >= m_origin.
x) oct |= 4;
43 if (point.y >= m_origin.
y) oct |= 2;
44 if (point.z >= m_origin.
z) oct |= 1;
48bool TetrahedralTree::IsLeafNode()
const {
51 return children[0] ==
nullptr;
59 int octant = GetOctantContainingPoint(point);
65 if (nodes.size() < BlockCapacity) {
66 nodes.push_back(std::make_pair(point, index));
71 for (
int i = 0; i < 8; ++i) {
73 Vec3 newOrigin = m_origin;
74 newOrigin.
x += m_halfDimension.
x * (i & 4 ? .5f : -.5f);
75 newOrigin.
y += m_halfDimension.
y * (i & 2 ? .5f : -.5f);
76 newOrigin.
z += m_halfDimension.
z * (i & 1 ? .5f : -.5f);
82 while (!nodes.empty()) {
83 auto node = nodes.back();
85 const int oct = GetOctantContainingPoint(node.first);
89 children[GetOctantContainingPoint(point)]->
InsertMeshNode(point, index);
95 elements.push_back(index);
99 for (
int i = 0; i < 8; i++) {
100 if (!children[i]->DoesBoxOverlap(bb))
continue;
112 return octreeNode->elements;
115 return std::vector<int>();
124 const Vec3& point)
const {
125 if (!(m_min.
x <= point.
x && point.
x <= m_max.
x &&
126 m_min.
y <= point.
y && point.
y <= m_max.
y &&
127 m_min.
z <= point.
z && point.
z <= m_max.
z))
130 return GetBlockFromPointHelper(point);
133const TetrahedralTree* TetrahedralTree::GetBlockFromPointHelper(
134 const Vec3& point)
const {
136 if (IsLeafNode())
return this;
139 int octant = GetOctantContainingPoint(point);
140 return children[octant]->GetBlockFromPointHelper(point);
Helper class for searches in field maps.
void InsertMeshElement(const double bb[6], const int index)
Insert a mesh element with given bounding box and index to the tree.
~TetrahedralTree()
Destructor.
TetrahedralTree(const Vec3 &origin, const Vec3 &halfDimension)
Constructor.
std::vector< int > GetElementsInBlock(const Vec3 &point) const
Get all elements linked to a block corresponding to the given point.
void InsertMeshNode(Vec3 point, const int index)
Insert a mesh node (a vertex/point) to the tree.