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