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 
9 namespace 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:
22  typedef Point PointType;
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>
165  AABox<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>
183  AABox<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>
202  AABox<Point>&
204  {
205  IncrementalBound(box.Min());
206  IncrementalBound(box.Max());
207  return *this;
208  }
209 
210  template <class Point>
211  template <class PointT>
212  typename AABox<Point>::ScalarType
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>
225  typename AABox<Point>::ScalarType
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>
247  typename AABox<Point>::ScalarType
249  {
250  return std::sqrt(MinSqrDist(p));
251  }
252 }; // namespace GfxTL
253 
254 #include "AABox.hpp"
255 
256 #endif
GfxTL::AACube
Definition: AACube.h:11
armarx::view_selection::skills::direction::state::right
state::Type right(state::Type previous)
Definition: LookDirection.cpp:264
GfxTL::AABox::operator[]
Point operator[](unsigned int index) const
Definition: AABox.hpp:49
GfxTL::AABox::MinSqrDist
ScalarType MinSqrDist(const PointT &p) const
Definition: AABox.h:226
GfxTL::VectorXD
Definition: MatrixXX.h:24
GfxTL::AABox::Max
Point & Max()
Definition: AABox.hpp:82
GfxTL::AABox::IncrementalBound
AABox< Point > & IncrementalBound(const PointType &p)
Definition: AABox.h:166
AABox.hpp
GfxTL::AABox::Dim
@ Dim
Definition: AABox.h:27
index
uint8_t index
Definition: EtherCATFrame.h:59
visionx::Points
std::vector< Point > Points
Definition: ObjectShapeClassification.h:71
GfxTL::AABox::PointType
Point PointType
Definition: AABox.h:22
GfxTL::AABox::IsInside
bool IsInside(const Point &p) const
Definition: AABox.hpp:103
AACube.h
Point::ScalarType
float ScalarType
Definition: PointCloud.h:28
GfxTL::AABox::Split
void Split(unsigned int axis, ScalarType s, AABox< Point > *left, AABox< Point > *right) const
Definition: AABox.hpp:22
GfxTL::AABox::operator=
AABox< Point > & operator=(const AACube< PointType > &cube)
Definition: AABox.h:65
GfxTL::AABox::operator+=
AABox< Point > & operator+=(const PointType &p)
Definition: AABox.hpp:115
GfxTL::AABox::Infinite
void Infinite()
Definition: AABox.hpp:11
GfxTL::AABox::Distance
ScalarType Distance(const PointT &p) const
Definition: AABox.h:248
GfxTL::AACube::Max
Point Max() const
Definition: AACube.h:68
GfxTL::AABox::Center
void Center(Point *center) const
Definition: AABox.hpp:40
Point
Definition: PointCloud.h:21
armarx::PointT
pcl::PointXYZRGBL PointT
Definition: Common.h:30
max
T max(T t1, T t2)
Definition: gdiam.h:51
GfxTL::Math
Definition: MathHelper.h:11
GfxTL
Definition: AABox.h:9
GfxTL::AABox::Bound
void Bound(const Points &points, size_t size)
Definition: AABox.h:89
GfxTL::AABox::DiagLength
ScalarType DiagLength() const
Definition: AABox.hpp:96
GfxTL::AACube::Min
const Point & Min() const
Definition: AACube.h:56
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:704
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
GfxTL::AABox::AABox
AABox(const AACube< PointType > &cube)
Definition: AABox.h:37
GfxTL::AABox::Min
Point & Min()
Definition: AABox.hpp:68
armarx::view_selection::skills::direction::state::center
state::Type center(state::Type previous)
Definition: LookDirection.cpp:233
GfxTL::AABox::AABox
AABox()
Definition: AABox.h:30
armarx::view_selection::skills::direction::state::left
state::Type left(state::Type previous)
Definition: LookDirection.cpp:258
Point::Dim
@ Dim
Definition: PointCloud.h:25
MathHelper.h
GfxTL::AABox::ScalarType
Point::ScalarType ScalarType
Definition: AABox.h:23
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
GfxTL::AABox
Definition: AABox.h:19
GfxTL::AABox::MaxSqrDist
ScalarType MaxSqrDist(const PointT &p) const
Definition: AABox.h:213