5int Cogs::Core::SeaCurrentQuadtree::getSubQuad(
const QuadBounds& bounds,
const glm::vec2& arrow)
const
7 glm::vec2 center = (bounds.min + bounds.max) * 0.5f;
9 if (arrow.x < center.x)
11 if (arrow.y < center.y)
18 if (arrow.y < center.y)
25void Cogs::Core::SeaCurrentQuadtree::split(QuadNode* node)
27 assert(node !=
nullptr);
28 assert(isLeaf(node) &&
"Error. Trying to split a non leaf node");
32 for (
auto& child : node->mChildren)
34 child = std::make_unique<QuadNode>();
35 QuadBounds b = computeSubQuad(node->mBounds, i++);
41void Cogs::Core::SeaCurrentQuadtree::clear(QuadNode * node)
43 for (
auto& child : node->mChildren)
46 if (child.get() !=
nullptr)
54Cogs::Core::QuadBounds Cogs::Core::SeaCurrentQuadtree::computeSubQuad(
const QuadBounds& bounds,
int i)
const
56 glm::vec2 center = (bounds.min + bounds.max) * 0.5f;
61 return QuadBounds{ bounds.min, center };
64 return QuadBounds{ glm::vec2(center.x, bounds.min.y), glm::vec2(bounds.max.x, center.y) };
67 return QuadBounds{ glm::vec2(bounds.min.x, center.y), glm::vec2(center.x, bounds.max.y) };
70 return QuadBounds{ center, bounds.max };
76void Cogs::Core::SeaCurrentQuadtree::insert(QuadNode* node,
size_t depth,
const glm::vec2& arrow)
79 assert(node !=
nullptr);
80 if (node->mNearestArrow.x)
82 glm::vec2 center = (node->mBounds.min + node->mBounds.max) * 0.5f;
83 if (glm::distance(arrow, center) < glm::distance(node->mNearestArrow, center))
88 int id = getSubQuad(node->mBounds, node->mNearestArrow);
90 insert(node->mChildren[
id].get(), depth + 1, node->mNearestArrow);
92 node->mNearestArrow = arrow;
99 int id = getSubQuad(node->mBounds, arrow);
101 insert(node->mChildren[
id].get(), depth + 1, arrow);
106 node->mNearestArrow = arrow;
110int Cogs::Core::SeaCurrentQuadtree::search(QuadNode* node,
const glm::vec2& arrow,
int priority)
const
112 assert(node !=
nullptr);
115 int i = getSubQuad(node->mBounds, arrow);
117 if (i != -1 && node->mChildren[i].get())
118 return search(node->mChildren[i].get(), arrow, ++priority);