7 const double hx,
const double hy)
8 : m_x0(x0), m_y0(y0), m_hx(hx), m_hy(hy) {
15 for (
int i = 0; i < 4; ++i) children[i] =
nullptr;
19 for (
int i = 0; i < 4; ++i)
delete children[i];
22bool QuadTree::DoesBoxOverlap(
const double bb[4])
const {
23 if (m_xmax < bb[0] || m_ymax < bb[1])
return false;
24 if (m_xmin > bb[2] || m_ymin > bb[3])
return false;
28int QuadTree::GetQuadrant(
const double x,
const double y)
const {
30 if (x >= m_x0) quad |= 2;
31 if (y >= m_y0) quad |= 1;
35bool QuadTree::IsLeafNode()
const {
38 return children[0] ==
nullptr;
46 int quad = GetQuadrant(x, y);
52 if (nodes.size() < BlockCapacity) {
53 nodes.push_back(std::make_tuple(x, y, index));
58 for (
int i = 0; i < 4; ++i) {
60 const double xi = m_x0 + m_hx * (i & 2 ? 0.5 : -0.5);
61 const double yi = m_y0 + m_hy * (i & 1 ? 0.5 : -0.5);
62 children[i] =
new QuadTree(xi, yi, 0.5 * m_hx, 0.5 * m_hy);
67 while (!nodes.empty()) {
68 auto node = nodes.back();
70 const double xn = std::get<0>(node);
71 const double yn = std::get<1>(node);
72 const int quad = GetQuadrant(xn, yn);
82 elements.push_back(index);
86 for (
int i = 0; i < 4; ++i) {
87 if (!children[i]->DoesBoxOverlap(bb))
continue;
93 const double y)
const {
94 const auto node = GetBlockFromPoint(x, y);
95 if (node)
return node->elements;
96 return std::vector<int>();
99const QuadTree* QuadTree::GetBlockFromPoint(
100 const double x,
const double y)
const {
101 if (x < m_xmin || x > m_xmax || y < m_ymin || y > m_ymax) {
104 return GetBlockFromPointHelper(x, y);
107const QuadTree* QuadTree::GetBlockFromPointHelper(
108 const double x,
const double y)
const {
110 if (IsLeafNode())
return this;
112 int quad = GetQuadrant(x, y);
113 return children[quad]->GetBlockFromPointHelper(x, y);
QuadTree(const double x0, const double y0, const double hx, const double hy)
Constructor.
std::vector< int > GetElementsInBlock(const double x, const double y) const
Get all elements linked to a block corresponding to the given point.
void InsertMeshElement(const double bb[4], const int index)
Insert a mesh element with given bounding box and index to the tree.
void InsertMeshNode(const double x, const double y, const int index)
Insert a mesh node (a vertex/point) to the tree.