LBIBCell
 All Classes Functions Variables Friends Pages
QuadTree.hpp
1 /* Copyright (c) 2012 David Sichau <mail"at"sichau"dot"eu>
2 
3  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
4  "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish,
5  distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to
6  the following conditions:
7 
8  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
11  LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
12  NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
13  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
14  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15  */
16 #ifndef UTILIB_GEOMETRY_QUADTREE_HPP
17 #define UTILIB_GEOMETRY_QUADTREE_HPP
18 
19 #include <UtilLib/include/geometry/QuadTreeNode.hpp>
20 #include <UtilLib/include/geometry/Rectangle.hpp>
21 #include <memory>
22 #include <vector>
23 namespace UtilLib {
24 
25 namespace geometry {
30 template<class Node>
31 class QuadTree {
32  public:
38  explicit QuadTree(
39  const Rectangle& rect,
40  int maxItems = 1);
44  ~QuadTree();
45 
51  bool put(std::shared_ptr<Node> pt);
52 
58  std::vector<std::shared_ptr<Node> > get(const Rectangle& rect);
59 
63  void clear();
64 
65  private:
69  QuadTreeNode<Node>* top_;
70 };
71 
72 
73 
74 template<class Node>
76  const Rectangle& rect,
77  int maxItems) {
78  top_ = new QuadTreeNode<Node>(rect, maxItems);
79 }
80 
81 template<class Node>
83  delete top_;
84  top_ = nullptr;
85 }
86 
87 template<class Node>
88 bool QuadTree<Node>::put(std::shared_ptr<Node> pt) {
89  return top_->put(pt);
90 }
91 
92 template<class Node>
93 std::vector<std::shared_ptr<Node> > QuadTree<Node>::get(
94  const Rectangle& rect) {
95  std::vector<std::shared_ptr<Node> > tempVec;
96  return top_->get(rect, tempVec);
97 }
98 
99 template<class Node>
101  delete top_;
102  top_ = nullptr;
103 }
104 
105 } // end namespace
106 } // end namespace
107 
108 #endif // UTILIB_GEOMETRY_QUADTREE_HPP
The class representing a quadtree The template parameter Node needs to provide the methods getXPos() ...
Definition: QuadTree.hpp:31
bool put(std::shared_ptr< Node > pt)
put Adds a point into the tree. This method runs in log(n)
Definition: QuadTree.hpp:88
~QuadTree()
~QuadTree Destructor of this tree. At the moment no inheritance is wanted.
Definition: QuadTree.hpp:82
std::vector< std::shared_ptr< Node > > get(const Rectangle &rect)
get Get all points in the provided rectange. This method runs in log(n)
Definition: QuadTree.hpp:93
void clear()
clear Delete all nodes inside this quadTree
Definition: QuadTree.hpp:100
QuadTree(const Rectangle &rect, int maxItems=1)
QuadTree A Quad tree on the area defined by the rectangle.
Definition: QuadTree.hpp:75
a class representing a rectangle
Definition: Rectangle.hpp:30
class representing a node of a quadtree The template parameter Node needs to provide the methods getX...