AACube.h
Go to the documentation of this file.
1#ifndef __AACUBE_HEADER__
2#define __AACUBE_HEADER__
3#include <algorithm>
4#include <limits>
5#include <utility>
6
7namespace GfxTL
8{
9
10 template <class Point>
11 class AACube
12 {
13 public:
15 typedef typename Point::ScalarType ScalarType;
16
17 enum
18 {
21 };
22
24 AACube(const Point& backBottomLeft, ScalarType width);
25 AACube(const Point* points, size_t size);
26 AACube(unsigned int box, const AACube<Point>& cube);
27
28 template <class Points>
29 void Bound(const Points& points, size_t size);
30 template <class IteratorT>
31 void Bound(IteratorT begin, IteratorT end);
32 template <class IteratorT>
33 void BoundNonCentered(IteratorT begin, IteratorT end);
34 template <class Points>
35 void BoundRotationInvariant(const Points& points, size_t size);
36 void DividingPlane(unsigned int axis, Point* n, ScalarType* d) const;
37 void DividingPlane(unsigned int axis, ScalarType* s);
38 void Center(Point* c) const;
39 void Center(const Point& c);
40 void SubCube(unsigned int box, AACube<Point>* cube) const;
41 bool IsSubCube(unsigned int* box, const AACube<Point>& cube) const;
42 bool IsInside(const Point& p) const;
45 const Point& LeftBottomBack() const;
46 void LeftBottomBack(const Point& lbb);
50 ScalarType Distance(const PointType& p) const;
52 void Translate(const PointType& t);
54
55 const Point&
56 Min() const
57 {
58 return _backBottomLeft;
59 }
60
61 Point&
63 {
64 return _backBottomLeft;
65 }
66
67 Point
68 Max() const
69 {
70 Point m(_backBottomLeft);
71 for (unsigned int i = 0; i < Dim; ++i)
72 {
73 m[i] += _width;
74 }
75 return m;
76 }
77
78 private:
79 Point _backBottomLeft;
80 ScalarType _width;
81 };
82
83 template <class Point>
84 template <class Points>
85 void
86 AACube<Point>::Bound(const Points& points, size_t size)
87 {
88 _width = 0;
89 if (size > 0)
90 {
91 Point pmax, pmin;
92 for (unsigned int u = 0; u < Dim; ++u)
93 {
94 pmax[u] = pmin[u] = points[0][u];
95 }
96 for (size_t i = 1; i < size; ++i)
97 {
98 for (unsigned int j = 0; j < Dim; ++j)
99 {
100 if (pmin[j] > points[i][j])
101 {
102 pmin[j] = points[i][j];
103 }
104 else if (pmax[j] < points[i][j])
105 {
106 pmax[j] = points[i][j];
107 }
108 }
109 }
110 Point center = pmin + (ScalarType).5 * (pmax - pmin);
111 Point r = pmax - center;
112 ScalarType rmax = r[0];
113 for (unsigned int u = 1; u < Dim; ++u)
114 if (r[u] > rmax)
115 {
116 rmax = r[u];
117 }
118 _backBottomLeft = center;
119 for (unsigned int u = 0; u < Dim; ++u)
120 {
121 _backBottomLeft[u] -= rmax;
122 }
123 _width = 2 * rmax;
124 }
125 }
126
127 template <class Point>
128 template <class IteratorT>
129 void
130 AACube<Point>::Bound(IteratorT begin, IteratorT end)
131 {
132 _width = 0;
133 if (end - begin > 0)
134 {
135 Point pmax, pmin;
136 for (unsigned int u = 0; u < Dim; ++u)
137 {
138 pmax[u] = pmin[u] = (*begin)[u];
139 }
140 IteratorT i = begin;
141 for (++i; i != end; ++i)
142 {
143 for (unsigned int j = 0; j < Dim; ++j)
144 {
145 if (pmin[j] > (*i)[j])
146 {
147 pmin[j] = (*i)[j];
148 }
149 else if (pmax[j] < (*i)[j])
150 {
151 pmax[j] = (*i)[j];
152 }
153 }
154 }
155 Point center = pmin + (ScalarType).5 * (pmax - pmin);
156 Point r = pmax - center;
157 ScalarType rmax = r[0];
158 for (unsigned int u = 1; u < Dim; ++u)
159 if (r[u] > rmax)
160 {
161 rmax = r[u];
162 }
163 _backBottomLeft = center;
164 for (unsigned int u = 0; u < Dim; ++u)
165 {
166 _backBottomLeft[u] -= rmax;
167 }
168 _width = 2 * rmax;
169 }
170 }
171
172 template <class Point>
173 template <class IteratorT>
174 void
175 AACube<Point>::BoundNonCentered(IteratorT begin, IteratorT end)
176 {
177 using namespace std;
178 _width = 0;
179 if (end - begin > 0)
180 {
181 Point pmax, pmin;
182 for (unsigned int u = 0; u < Dim; ++u)
183 {
184 pmax[u] = pmin[u] = (*begin)[u];
185 }
186 IteratorT i = begin;
187 for (++i; i != end; ++i)
188 {
189 for (unsigned int j = 0; j < Dim; ++j)
190 {
191 if (pmin[j] > (*i)[j])
192 {
193 pmin[j] = (*i)[j];
194 }
195 else if (pmax[j] < (*i)[j])
196 {
197 pmax[j] = (*i)[j];
198 }
199 }
200 }
201 _backBottomLeft = pmin;
202 _width = pmax[0] - pmin[0];
203 for (unsigned int i = 1; i < Dim; ++i)
204 {
205 _width = max(_width, pmax[i] - pmin[i]);
206 }
207 }
208 }
209
210 template <class Point>
211 template <class Points>
212 void
213 AACube<Point>::BoundRotationInvariant(const Points& points, size_t size)
214 {
215 _width = 0;
216 if (size > 0)
217 {
218 Point pmax, pmin;
219 pmax = pmin = points[0];
220 for (size_t i = 1; i < size; ++i)
221 {
222 for (unsigned int j = 0; j < Dim; ++j)
223 {
224 if (pmin[j] > points[i][j])
225 {
226 pmin[j] = points[i][j];
227 }
228 else if (pmax[j] < points[i][j])
229 {
230 pmax[j] = points[i][j];
231 }
232 }
233 }
234 _width = (pmax - pmin).Length();
235 Point center = pmin + (ScalarType).5 * (pmax - pmin);
236 _backBottomLeft = center;
237 for (unsigned int u = 0; u < Dim; ++u)
238 {
239 _backBottomLeft[u] -= _width / 2;
240 }
241 }
242 }
243
244}; // namespace GfxTL
245
246#include "AACube.hpp"
247
248#endif
uint8_t index
constexpr T c
void Width(ScalarType w)
Definition AACube.hpp:139
Point operator[](int index) const
Definition AACube.hpp:160
void Center(const Point &c)
Definition AACube.hpp:64
void SubCube(unsigned int box, AACube< Point > *cube) const
Definition AACube.hpp:76
const Point & Min() const
Definition AACube.h:56
ScalarType DiagLength() const
Definition AACube.hpp:175
Point::ScalarType ScalarType
Definition AACube.h:15
void Scale(ScalarType s)
Definition AACube.hpp:250
AACube(const Point &backBottomLeft, ScalarType width)
Definition AACube.hpp:13
AACube(const Point *points, size_t size)
Definition AACube.hpp:19
void Bound(const Points &points, size_t size)
Definition AACube.h:86
void Inflate(ScalarType v)
Definition AACube.hpp:182
void Center(Point *c) const
Definition AACube.hpp:52
bool IsSubCube(unsigned int *box, const AACube< Point > &cube) const
Definition AACube.hpp:92
void DividingPlane(unsigned int axis, Point *n, ScalarType *d) const
Definition AACube.hpp:32
bool IsInside(const Point &p) const
Definition AACube.hpp:120
AACube(unsigned int box, const AACube< Point > &cube)
Definition AACube.hpp:25
void Translate(const PointType &t)
Definition AACube.hpp:243
ScalarType SqrDistance(const PointType &p) const
Definition AACube.hpp:218
ScalarType Distance(const PointType &p) const
Definition AACube.hpp:193
void LeftBottomBack(const Point &lbb)
Definition AACube.hpp:153
const Point & LeftBottomBack() const
Definition AACube.hpp:146
void BoundRotationInvariant(const Points &points, size_t size)
Definition AACube.h:213
void Bound(IteratorT begin, IteratorT end)
Definition AACube.h:130
Point PointType
Definition AACube.h:14
ScalarType Width() const
Definition AACube.hpp:132
Point Max() const
Definition AACube.h:68
void BoundNonCentered(IteratorT begin, IteratorT end)
Definition AACube.h:175
Point & Min()
Definition AACube.h:62
void DividingPlane(unsigned int axis, ScalarType *s)
Definition AACube.hpp:43
Definition AABox.h:10
MatrixXX< C, R, T > max(const MatrixXX< C, R, T > &a, const MatrixXX< C, R, T > &b)
Definition MatrixXX.h:630
float ScalarType
Definition PointCloud.h:28