base
Angle.hpp
Go to the documentation of this file.
1 #ifndef __BASE_ANGLE_HH__
2 #define __BASE_ANGLE_HH__
3 
4 #include <math.h>
5 #include <iostream>
6 #include <base/Float.hpp>
7 #include <base/Eigen.hpp>
8 
9 #include <vector>
10 
11 namespace base
12 {
13 
19 class Angle
20 {
21 public:
29  double rad;
30 
34  Angle() : rad(base::unknown<double>()) {}
35 
36 protected:
37  explicit Angle( double _rad ) : rad(_rad)
38  {
39  canonize();
40  }
41 
42  void canonize()
43  {
44  if( rad > M_PI || rad <= -M_PI )
45  {
46  double intp;
47  const double side = copysign(M_PI,rad);
48  rad = -side + 2*M_PI * modf( (rad-side) / (2*M_PI), &intp );
49  }
50  }
51 
52 public:
58  static inline double rad2Deg( double rad )
59  {
60  return rad / M_PI * 180.0;
61  }
62 
68  static inline double deg2Rad( double deg )
69  {
70  return deg / 180.0 * M_PI;
71  }
72 
75  static inline double normalizeRad( double rad )
76  {
77  return Angle(rad).rad;
78  }
79 
85  static inline Angle fromRad( double rad )
86  {
87  return Angle( rad );
88  }
89 
95  static inline Angle fromDeg( double deg )
96  {
97  return Angle( deg / 180.0 * M_PI );
98  }
99 
102  static inline Angle unknown()
103  {
104  Angle result;
105  result.rad = base::unknown<double>();
106  return result;
107  }
108 
112  static inline Angle Min()
113  {
114  return Angle( nextafter(-M_PI, 0) );
115  }
116 
120  static inline Angle Max()
121  {
122  return Angle(M_PI);
123  }
124 
128  double inline getRad() const
129  {
130  return rad;
131  }
132 
136  double inline getDeg() const
137  {
138  return rad / M_PI * 180;
139  }
140 
147  bool inline isApprox( Angle other, double prec = 1e-5 ) const
148  {
149  return fabs(Angle(other.rad - rad).getRad()) < prec;
150  }
151 
152  void operator=(const Angle &other)
153  {
154  rad = other.rad;
155  }
156 
157  inline bool operator==(const Angle &other ) const
158  {
159  return this->rad == other.rad;
160  }
161 
162  inline bool operator<(const Angle &other ) const
163  {
164  return this->rad < other.rad;
165  }
166 
167  inline bool operator>(const Angle &other ) const
168  {
169  return this->rad > other.rad;
170  }
171 
172  inline bool operator<=(const Angle &other ) const
173  {
174  return this->rad <= other.rad;
175  }
176 
177  inline bool operator>=(const Angle &other ) const
178  {
179  return this->rad >= other.rad;
180  }
181 
182  inline Angle &operator+=(const Angle &other )
183  {
184  this->rad += other.rad;
185  canonize();
186  return *this;
187  }
188 
189  inline Angle &operator-=(const Angle &other )
190  {
191  this->rad -= other.rad;
192  canonize();
193  return *this;
194  }
195 
196  inline Angle operator+( const Angle &other ) const
197  {
198  return Angle::fromRad( getRad() + other.getRad() );
199  }
200 
201  inline Angle operator-( const Angle &other ) const
202  {
203  return Angle::fromRad( getRad() - other.getRad() );
204  }
205 
206  inline Angle operator*( const Angle &other ) const
207  {
208  return Angle::fromRad( getRad() * other.getRad() );
209  }
210 
211  inline Angle operator*( const double &val ) const
212  {
213  return Angle::fromRad( getRad() * val );
214  }
215 
219  inline Angle flipped() const
220  {
221  return Angle(rad).flip();
222  }
223 
227  inline Angle &flip()
228  {
229  if(rad < 0)
230  rad += M_PI;
231  else
232  rad -=M_PI;
233  return *this;
234  }
235 
238  static Angle vectorToVector(const base::Vector3d& a, const base::Vector3d& b);
239 
240 
247  static Angle vectorToVector(const base::Vector3d& a, const base::Vector3d& b, const base::Vector3d& positive);
248 };
249 
250 static inline Angle operator*( double a, Angle b )
251 {
252  return Angle::fromRad( a * b.getRad() );
253 }
254 
255 std::ostream& operator << (std::ostream& os, Angle angle);
256 
263 {
264 public:
265  AngleSegment();
266 
267  AngleSegment(const Angle &start, double _width);
268 
274  bool isInside(const Angle &angle) const;
275 
281  bool isInside(const AngleSegment &segment) const;
282 
283  bool split(const Angle &angle, AngleSegment &rest)
284  {
285  return false;
286  }
287 
288  std::vector<AngleSegment> split(const Angle &angle)
289  {
290  return std::vector<AngleSegment>();
291  }
292 
298  std::vector<AngleSegment> getIntersections(const AngleSegment &b) const;
299 
304  double getWidth() const
305  {
306  return width;
307  }
308 
313  base::Angle getStart() const;
314 
323  base::Angle getEnd() const;
324 
328  double width;
329 
333  double startRad;
334 
338  double endRad;
339 };
340 
341 std::ostream& operator << (std::ostream& os, AngleSegment seg);
342 
343 }
344 
345 #endif
std::ostream & operator<<(std::ostream &os, Angle angle)
Definition: Angle.cpp:25
bool operator==(const Angle &other) const
Definition: Angle.hpp:157
void canonize()
Definition: Angle.hpp:42
Angle operator*(const Angle &other) const
Definition: Angle.hpp:206
std::vector< AngleSegment > split(const Angle &angle)
Definition: Angle.hpp:288
Angle & operator-=(const Angle &other)
Definition: Angle.hpp:189
Angle(double _rad)
Definition: Angle.hpp:37
Angle operator-(const Angle &other) const
Definition: Angle.hpp:201
static double normalizeRad(double rad)
Definition: Angle.hpp:75
double endRad
Definition: Angle.hpp:338
double getWidth() const
Definition: Angle.hpp:304
static Angle vectorToVector(const base::Vector3d &a, const base::Vector3d &b)
Definition: Angle.cpp:7
static Angle unknown()
Definition: Angle.hpp:102
static double deg2Rad(double deg)
Definition: Angle.hpp:68
Angle()
Definition: Angle.hpp:34
Definition: Angle.hpp:262
static Angle fromRad(double rad)
Definition: Angle.hpp:85
Angle flipped() const
Definition: Angle.hpp:219
static Angle Min()
Definition: Angle.hpp:112
bool operator<(const Angle &other) const
Definition: Angle.hpp:162
bool operator>=(const Angle &other) const
Definition: Angle.hpp:177
Angle operator*(const double &val) const
Definition: Angle.hpp:211
static double rad2Deg(double rad)
Definition: Angle.hpp:58
Definition: LinearAngular6DCommand.hpp:8
void operator=(const Angle &other)
Definition: Angle.hpp:152
double getRad() const
Definition: Angle.hpp:128
double rad
Definition: Angle.hpp:29
Angle & flip()
Definition: Angle.hpp:227
double startRad
Definition: Angle.hpp:333
bool split(const Angle &angle, AngleSegment &rest)
Definition: Angle.hpp:283
Eigen::Matrix< double, 3, 1, Eigen::DontAlign > Vector3d
Definition: Eigen.hpp:20
Angle operator+(const Angle &other) const
Definition: Angle.hpp:196
bool operator>(const Angle &other) const
Definition: Angle.hpp:167
Angle & operator+=(const Angle &other)
Definition: Angle.hpp:182
static Angle Max()
Definition: Angle.hpp:120
bool operator<=(const Angle &other) const
Definition: Angle.hpp:172
Definition: Angle.hpp:19
bool isApprox(Angle other, double prec=1e-5) const
Definition: Angle.hpp:147
double getDeg() const
Definition: Angle.hpp:136
static Angle fromDeg(double deg)
Definition: Angle.hpp:95
double width
Definition: Angle.hpp:328