OpenLexocad  27.1
QuadTree.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Base/Color.h>
4 #include <Geom/Pnt.h>
5 #include <Geom/Rect.h>
6 #include <Geom/Bnd_Box.h>
7 
8 #include <deque>
9 
10 
11 #include <concurrent_vector.h>
12 #include <mutex>
13 #include <tbb/spin_mutex.h>
14 
15 struct FloatPoint
16 {
17  FloatPoint(const float v[3]) { vec[0] = v[0]; vec[1] = v[1]; vec[2] = v[2]; }
18  FloatPoint(float x, float y, float z) { vec[0] = x; vec[1] = y; vec[2] = z; }
19  float vec[3];
20  float x() const { return vec[0]; }
21  float y() const { return vec[1]; }
22  float z() const { return vec[2]; }
23 };
24 
25 namespace Geom
26 {
27 class QuadTree;
28 
30 {
31  QuadTreeIterator(QuadTree* q) : _start(q), _current(q){};
32 
33  bool operator==(const QuadTreeIterator& b) const { return (b._current == _current); }
34 
35  QuadTreeIterator& operator++() { return *this; }
36 
37 
38  QuadTree* _current;
39  const QuadTree* _start;
40 };
41 
42 class LX_GEOM_EXPORT ColorPoint
43 {
44 public:
46  ColorPoint(const Geom::Pnt& p, const Base::MColor& c) : p(p), c(c){};
47  Geom::Pnt p;
49 };
50 
51 
53 {
54 public:
55  using container = std::deque<ColorPoint>;
56  using iterator = typename container::iterator;
57  using const_iterator = typename container::const_iterator;
58 
59  void addPoint(const ColorPoint& p) { mData.emplace_back(p); };
60  iterator begin() { return mData.begin(); }
61  iterator end() { return mData.end(); }
62  const_iterator begin() const { return mData.begin(); }
63  const_iterator end() const { return mData.end(); }
64  const_iterator cbegin() const { return mData.cbegin(); }
65  const_iterator cend() const { return mData.cend(); }
66 
67  void clear() { mData.clear(); };
68  bool empty() const { return mData.empty(); };
69 
70  const container& getData() const { return mData; }
71 
72 
73 private:
74 
75  container mData;
76 
77 
78 };
79 
80 class LX_GEOM_EXPORT QuadTree
81 {
82 public:
83  QuadTree(Geom::Rect boundary, size_t capacity, int myDeep = 1);
84  virtual ~QuadTree();
85 
86  bool insert(const Geom::ColorPoint& cp);
87 
88  const Geom::Rect& getBoundary() const;
89  const std::deque<Geom::ColorPoint>& getPoints() const;
90  const bool hasPoints() const;
91  const size_t getPointCount() const;
92  std::vector<QuadTree*> getChildren() const;
93 
94  void getPointsRecursive(std::deque<Geom::ColorPoint>& points);
95  const size_t getPointCountRecursive() const;
96  std::vector<QuadTree*> getChildrenRecursive() const;
97  void removePointsRecursive();
98 
99  void setAutoSplit(bool on);
100  void split();
101  void setDeep(int deep);
102  int getDeep();
103 
104 
105  // Children
110 
111  QuadTreeIterator begin();
112  QuadTreeIterator end();
113 
114 
115 private:
116  void getDeep(int& deep);
117 
118  QuadTree();
119 
120  // Arbitrary constant to indicate how many elements can be stored in this quad tree node capacity
121  size_t _capacity;
122 
123  int m_myDeep;
124  int m_maxDeep;
125 
126  // Axis-aligned bounding box stored as a center with half-dimensions
127  // to represent the boundaries of this quad tree
128  Geom::Rect _boundary;
129 
130  // Points in this quad tree node
131  PointStorage _points;
132 
133  size_t _pointCount;
134 
135  bool _autoSplit;
136 };
137 
138 
140 {
141 public:
142  using container = concurrency::concurrent_vector<ColorPoint>;
143  using iterator = typename container::iterator;
144  using const_iterator = typename container::const_iterator;
145 
146  const_iterator begin() const { return mData.begin(); }
147  const_iterator end() const { return mData.end(); }
148  const_iterator cbegin() const { return mData.cbegin(); }
149  const_iterator cend() const { return mData.cend(); }
150 
151  void addPoint(const ColorPoint& p) { mData.push_back(p); };
152 
153  void clear() { mData.clear(); };
154  bool empty() const { return mData.empty(); };
155 
156  const container& getData() const { return mData; }
157 
158 
159 private:
160 
161  container mData;
162 
163 
164 };
165 
166 class LX_GEOM_EXPORT QuadTreeMT
167 {
168 public:
169  QuadTreeMT(Geom::Rect boundary, size_t capacity, int myDeep = 1);
170  QuadTreeMT* findQuadTree(const FloatPoint& cp);
171  QuadTreeMT* findQuadTree(const Geom::Pnt &p);
172  virtual ~QuadTreeMT();
173 
174  bool insert(const FloatPoint& fp, const uint32_t color );
175 
176  const Geom::Rect& getBoundary() const;
177  std::vector<FloatPoint> getPoints() const;
178  std::vector<uint32_t> getColors() const;
179  const bool hasPoints() const;
180  const size_t getPointCount() const;
181  std::vector<QuadTreeMT*> getChildren() const;
182 
183  const size_t getPointCountRecursive() const;
184  std::vector<QuadTreeMT*> getChildrenRecursive() const;
185  void removePointsRecursive();
186 
187  void setBBox( Geom::Bnd_Box b ) { _bbox = b; }
188  Geom::Bnd_Box getBBox( ) { return _bbox; }
189  void setDeep(int deep);
190  int getDeep();
191 
192 
193  // Children
198 
199 
200 private:
201 
202  void getDeep(int& deep);
203  void split();
204  QuadTreeMT();
205 
206  // Arbitrary constant to indicate how many elements can be stored in this quad tree node capacity
207  size_t _capacity;
208 
209  int m_myDeep;
210  int m_maxDeep;
211 
212  // Axis-aligned bounding box stored as a center with half-dimensions
213  // to represent the boundaries of this quad tree
214  Geom::Rect _boundary;
215  Geom::Bnd_Box _bbox;
216 
217  // Points in this quad tree node
218  concurrency::concurrent_vector<FloatPoint> _points;
219  concurrency::concurrent_vector<uint32_t> _colors;
220 
221  std::mutex m_mutex;
222 };
223 
224 
225 
226 
227 } // namespace Geom
Definition: Variant.h:60
iterator end()
Definition: QuadTree.h:61
void clear()
Definition: QuadTree.h:67
Base::MColor c
Definition: QuadTree.h:48
QuadTree * northEast
Definition: QuadTree.h:107
Defines a non-persistent 3D Cartesian point.
Definition: Pnt.h:43
float y() const
Definition: QuadTree.h:21
std::deque< ColorPoint > container
Definition: QuadTree.h:55
QuadTreeMT * northEast
Definition: QuadTree.h:195
void addPoint(const ColorPoint &p)
Definition: QuadTree.h:151
Definition: QuadTree.h:52
Definition: QuadTree.h:29
const container & getData() const
Definition: QuadTree.h:156
typename container::const_iterator const_iterator
Definition: QuadTree.h:144
QuadTree * southWest
Definition: QuadTree.h:108
ColorPoint()
Definition: QuadTree.h:45
iterator begin()
Definition: QuadTree.h:60
Definition: QuadTree.h:15
Definition: Bnd_Box.h:63
const_iterator cbegin() const
Definition: QuadTree.h:148
typename container::iterator iterator
Definition: QuadTree.h:56
Definition: Rect.h:27
QuadTreeMT * southWest
Definition: QuadTree.h:196
void addPoint(const ColorPoint &p)
Definition: QuadTree.h:59
bool empty() const
Definition: QuadTree.h:68
FloatPoint(float x, float y, float z)
Definition: QuadTree.h:18
Definition: QuadTree.h:166
const_iterator cbegin() const
Definition: QuadTree.h:64
const container & getData() const
Definition: QuadTree.h:70
Geom::Bnd_Box getBBox()
Definition: QuadTree.h:188
QuadTree * southEast
Definition: QuadTree.h:109
QuadTreeMT * northWest
Definition: QuadTree.h:194
float x() const
Definition: QuadTree.h:20
FloatPoint(const float v[3])
Definition: QuadTree.h:17
const_iterator begin() const
Definition: QuadTree.h:146
const_iterator cend() const
Definition: QuadTree.h:149
QuadTree * northWest
Definition: QuadTree.h:106
QuadTreeMT * southEast
Definition: QuadTree.h:197
bool empty() const
Definition: QuadTree.h:154
void clear()
Definition: QuadTree.h:153
const_iterator end() const
Definition: QuadTree.h:147
Definition: QuadTree.h:80
Definition: Color.h:21
const_iterator cend() const
Definition: QuadTree.h:65
float vec[3]
Definition: QuadTree.h:19
const_iterator begin() const
Definition: QuadTree.h:62
typename container::const_iterator const_iterator
Definition: QuadTree.h:57
typename container::iterator iterator
Definition: QuadTree.h:143
const_iterator end() const
Definition: QuadTree.h:63
concurrency::concurrent_vector< ColorPoint > container
Definition: QuadTree.h:142
Definition: QuadTree.h:42
void setBBox(Geom::Bnd_Box b)
Definition: QuadTree.h:187
float z() const
Definition: QuadTree.h:22
Definition: QuadTree.h:139