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