AABox.h
Go to the documentation of this file.
1#ifndef __GfxTL_AABOX_HEADER__
2#define __GfxTL_AABOX_HEADER__
3#include <algorithm>
4#include <limits>
5
6#include <GfxTL/AACube.h>
7#include <GfxTL/MathHelper.h>
8
9namespace GfxTL
10{
11
12 // AABox - Axis aligned box in N-dimensions
13 // N is deferred from Point
14 // requirements for Point:
15 // Point must declare public type ScalarType
16 // Point::Dim must be the dimension
17 // Point must support array style indexing with operator[]
18 template <class Point>
19 class AABox
20 {
21 public:
23 typedef typename Point::ScalarType ScalarType;
24
25 enum
26 {
28 };
29
31 {
32 }
33
34 // constructs box with given min and max corners
35 AABox(const PointType& pmin, const PointType& pmax);
36
37 AABox(const AACube<PointType>& cube) : _pmin(cube.Min()), _pmax(cube.Max())
38 {
39 }
40
41 // constructs infinitely large box, that is all elements of
42 // pmin == -infinity and of pmax == infinity
43 void Infinite();
44 // splits the box along the plane perpendicular to axis and
45 // passing through s
46 // If s is not within the box left and right will be set
47 // to box
48 void Split(unsigned int axis, ScalarType s, AABox<Point>* left, AABox<Point>* right) const;
49 inline void Center(Point* center) const;
50 inline Point operator[](unsigned int index) const;
51 inline Point& Min();
52 inline const Point& Min() const;
53 inline Point& Max();
54 inline const Point& Max() const;
55 inline ScalarType DiagLength() const;
56 inline bool IsInside(const Point& p) const;
57 template <class Points>
58 void Bound(const Points& points, size_t size);
59 template <class PointForwardIterator>
60 void Bound(PointForwardIterator begin, PointForwardIterator end);
61 void Bound(const PointType& p, ScalarType r);
63
66 {
67 _pmin = cube.Min();
68 _pmax = cube.Max();
69 return *this;
70 }
71
75 template <class PointT>
76 ScalarType MaxSqrDist(const PointT& p) const;
77 template <class PointT>
78 ScalarType MinSqrDist(const PointT& p) const;
79 template <class PointT>
80 ScalarType Distance(const PointT& p) const;
81
82 private:
83 PointType _pmin, _pmax;
84 };
85
86 template <class Point>
87 template <class Points>
88 void
89 AABox<Point>::Bound(const Points& points, size_t size)
90 {
91 for (unsigned int i = 0; i < Dim; ++i)
92 {
93 _pmin[i] = -std::numeric_limits<ScalarType>::infinity();
94 _pmax[i] = std::numeric_limits<ScalarType>::infinity();
95 }
96 if (size > 0)
97 {
98 for (unsigned int i = 0; i < Dim; ++i)
99 {
100 _pmax[i] = _pmin[i] = points[0][i];
101 }
102 for (size_t i = 1; i < size; ++i)
103 {
104 for (unsigned int j = 0; j < Dim; ++j)
105 {
106 if (_pmin[j] > points[i][j])
107 {
108 _pmin[j] = points[i][j];
109 }
110 else if (_pmax[j] < points[i][j])
111 {
112 _pmax[j] = points[i][j];
113 }
114 }
115 }
116 }
117 }
118
119 template <class Point>
120 template <class PointForwardIterator>
121 void
122 AABox<Point>::Bound(PointForwardIterator begin, PointForwardIterator end)
123 {
124 for (unsigned int i = 0; i < Dim; ++i)
125 {
126 _pmin[i] = -std::numeric_limits<ScalarType>::infinity();
127 _pmax[i] = std::numeric_limits<ScalarType>::infinity();
128 }
129 if (begin == end)
130 {
131 return;
132 }
133 for (unsigned int i = 0; i < Dim; ++i)
134 {
135 _pmax[i] = _pmin[i] = (*begin)[i];
136 }
137 for (++begin; begin != end; ++begin)
138 {
139 for (unsigned int j = 0; j < Dim; ++j)
140 {
141 if (_pmin[j] > (*begin)[j])
142 {
143 _pmin[j] = (*begin)[j];
144 }
145 else if (_pmax[j] < (*begin)[j])
146 {
147 _pmax[j] = (*begin)[j];
148 }
149 }
150 }
151 }
152
153 template <class Point>
154 void
156 {
157 for (unsigned int j = 0; j < Dim; ++j)
158 {
159 _pmin[j] = p[j] - r;
160 _pmax[j] = p[j] + r;
161 }
162 }
163
164 template <class Point>
167 {
168 for (unsigned int i = 0; i < Dim; ++i)
169 {
170 if (_pmin[i] > p[i])
171 {
172 _pmin[i] = p[i];
173 }
174 if (_pmax[i] < p[i])
175 {
176 _pmax[i] = p[i];
177 }
178 }
179 return *this;
180 }
181
182 template <class Point>
185 {
186 ScalarType v;
187 for (unsigned int j = 0; j < Dim; ++j)
188 {
189 if (_pmin[j] > (v = p[j] - r))
190 {
191 _pmin[j] = v;
192 }
193 if (_pmax[j] < (v = p[j] + r))
194 {
195 _pmax[j] = v;
196 }
197 }
198 return *this;
199 }
200
201 template <class Point>
204 {
205 IncrementalBound(box.Min());
206 IncrementalBound(box.Max());
207 return *this;
208 }
209
210 template <class Point>
211 template <class PointT>
213 AABox<Point>::MaxSqrDist(const PointT& p) const
214 {
215 ScalarType maxDist = 0, tmp;
216 for (unsigned int i = 0; i < Dim; ++i)
217 maxDist += (tmp = std::max(Math<ScalarType>::Abs(Min()[i] - p[i]),
218 Math<ScalarType>::Abs(Max()[i] - p[i]))) *
219 tmp;
220 return maxDist;
221 }
222
223 template <class Point>
224 template <class PointT>
226 AABox<Point>::MinSqrDist(const PointT& p) const
227 {
228 ScalarType sqrDist = 0, t;
229 for (unsigned int i = 0; i < Dim; ++i)
230 {
231 if (p[i] < Min()[i])
232 {
233 t = Min()[i] - p[i];
234 sqrDist += t * t;
235 }
236 else if (p[i] > Max()[i])
237 {
238 t = p[i] - Max()[i];
239 sqrDist += t * t;
240 }
241 }
242 return sqrDist;
243 }
244
245 template <class Point>
246 template <class PointT>
248 AABox<Point>::Distance(const PointT& p) const
249 {
250 return std::sqrt(MinSqrDist(p));
251 }
252}; // namespace GfxTL
253
254#include "AABox.hpp"
255
256#endif
uint8_t index
void Bound(const PointType &p, ScalarType r)
Definition AABox.h:155
AABox(const AACube< PointType > &cube)
Definition AABox.h:37
AABox< Point > & IncrementalBound(const AABox< Point > &box)
Definition AABox.h:203
void Center(Point *center) const
Definition AABox.hpp:40
AABox< Point > & operator=(const AACube< PointType > &cube)
Definition AABox.h:65
ScalarType MinSqrDist(const PointT &p) const
Definition AABox.h:226
Point::ScalarType ScalarType
Definition AABox.h:23
void Bound(const Points &points, size_t size)
Definition AABox.h:89
void Split(unsigned int axis, ScalarType s, AABox< Point > *left, AABox< Point > *right) const
Definition AABox.hpp:22
Point operator[](unsigned int index) const
Definition AABox.hpp:49
ScalarType MaxSqrDist(const PointT &p) const
Definition AABox.h:213
AABox< Point > & IncrementalBound(const PointType &p)
Definition AABox.h:166
bool IsInside(const Point &p) const
Definition AABox.hpp:103
AABox< Point > & IncrementalBound(const PointType &p, ScalarType r)
Definition AABox.h:184
AABox< Point > & operator+=(const PointType &p)
Definition AABox.hpp:115
Point PointType
Definition AABox.h:22
ScalarType Distance(const PointT &p) const
Definition AABox.h:248
ScalarType DiagLength() const
Definition AABox.hpp:96
void Bound(PointForwardIterator begin, PointForwardIterator end)
Definition AABox.h:122
Point & Min()
Definition AABox.hpp:68
Point & Max()
Definition AABox.hpp:82
void Infinite()
Definition AABox.hpp:11
const Point & Min() const
Definition AACube.h:56
Point Max() const
Definition AACube.h:68
static ScalarT Abs(ScalarT s)
Definition MathHelper.h:15
Definition AABox.h:10
float ScalarType
Definition PointCloud.h:28