16 #ifndef UTILLIB_GEOMETRY_QUADTREENODE_HPP
17 #define UTILLIB_GEOMETRY_QUADTREENODE_HPP
19 #include <UtilLib/include/geometry/Rectangle.hpp>
20 #include <UtilLib/include/Log.hpp>
55 bool put(std::shared_ptr<Node> pt);
63 std::vector<std::shared_ptr<Node> >&
get(
65 std::vector<std::shared_ptr<Node> >& vector);
92 enum Regions {NORTHWEST = 0, NORTHEAST = 1, SOUTHEAST = 2, SOUTHWEST = 3};
97 std::vector<std::shared_ptr<Node> > items_;
102 std::array<QuadTreeNode*, 4>* children_;
107 unsigned int maxItems_;
116 const double minSize_;
122 int maximumItems) : children_(nullptr),
123 maxItems_(maximumItems),
126 LOG(UtilLib::logDEBUG2) <<
"new node generated with boarder n: " <<
127 bounds_.
north <<
" s: " << bounds_.
south <<
" w: " <<
128 bounds_.
west <<
" e: " << bounds_.
east;
133 if (children_ !=
nullptr) {
134 for (
const auto& child :* children_) {
144 return children_ !=
nullptr;
148 void QuadTreeNode<Node>::split() {
149 if ((std::abs(bounds_.north - bounds_.south) < minSize_) &&
150 (std::abs(bounds_.east - bounds_.west) < minSize_) ) {
151 LOG(UtilLib::logDEBUG4) <<
"below min size therefore no split";
156 LOG(UtilLib::logDEBUG4) <<
"split executed";
159 static_cast<double>(bounds_.north -
160 (bounds_.north - bounds_.south) / 2.0);
162 static_cast<double>(bounds_.east - (bounds_.east - bounds_.west) / 2.0);
164 children_ =
new std::array<QuadTreeNode*, 4>;
166 (*children_)[NORTHWEST] =
167 new QuadTreeNode(Rectangle(bounds_.north, nsHalf, bounds_.west,
169 (*children_)[NORTHEAST] =
170 new QuadTreeNode(Rectangle(bounds_.north, nsHalf, ewHalf,
171 bounds_.east), maxItems_);
172 (*children_)[SOUTHEAST] =
173 new QuadTreeNode(Rectangle(nsHalf, bounds_.south, ewHalf,
174 bounds_.east), maxItems_);
175 (*children_)[SOUTHWEST] =
176 new QuadTreeNode(Rectangle(nsHalf, bounds_.south, bounds_.west,
179 for (
auto item : items_) {
186 QuadTreeNode<Node>* QuadTreeNode<Node>::getChild(
190 if (bounds_.pointWithinBounds(x, y))
192 if (children_ !=
nullptr) {
193 for (QuadTreeNode<Node>* child :* children_)
195 if (child->bounds_.pointWithinBounds(x, y))
196 return child->getChild(x, y);
207 if (children_ ==
nullptr) {
208 items_.push_back(pt);
209 if (items_.size() > maxItems_)
214 QuadTreeNode* node = getChild(pt->getXPos(), pt->getYPos());
215 if (node !=
nullptr) {
216 return node->
put(pt);
225 std::vector<std::shared_ptr<Node> >& vector) {
226 if (children_ ==
nullptr) {
227 for (std::shared_ptr<Node> pt : items_) {
229 vector.push_back(pt);
230 LOG(UtilLib::logDEBUG4) <<
"point added: " << pt->getXPos() <<
231 " " << pt->getYPos();
236 if (child->bounds_.within(rect)) {
237 LOG(UtilLib::logDEBUG4) <<
"went to children";
238 child->get(rect, vector);
247 #endif // UTILLIB_GEOMETRY_QUADTREENODE_HPP
bool put(std::shared_ptr< Node > pt)
put Inserts a point into the Node
const double north
the north border
QuadTreeNode(const Rectangle &rect, int maximumItems)
QuadTreeNode Constructs a new QuadTreeNode.
const double south
the north border
const double west
the north border
bool pointWithinBounds(double x, double y) const
pointWithinBounds Checks if point is contained inside this rectangle
const double east
the north border
std::vector< std::shared_ptr< Node > > & get(const Rectangle &rect, std::vector< std::shared_ptr< Node > > &vector)
get Get all Points in the defined rectangle
a class representing a rectangle
class representing a node of a quadtree The template parameter Node needs to provide the methods getX...