Main MRPT website > C++ reference
MRPT logo
wrap2pi.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2014, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef MRPT_MATH_WRAP2PI_H
10 #define MRPT_MATH_WRAP2PI_H
11 
12 #define _USE_MATH_DEFINES // (For VS to define M_PI, etc. in cmath)
13 #include <cmath>
14 
15 namespace mrpt
16 {
17  namespace math
18  {
19  /** \addtogroup container_ops_grp
20  * @{ */
21 
22  /** Modifies the given angle to translate it into the [0,2pi[ range.
23  * \note Take care of not instancing this template for integer numbers, since it only works for float, double and long double.
24  * \sa wrapToPi, wrapTo2Pi, unwrap2PiSequence
25  */
26  template <class T>
27  inline void wrapTo2PiInPlace(T &a)
28  {
29  bool was_neg = a<0;
30  a = fmod(a, static_cast<T>(2.0*M_PI) );
31  if (was_neg) a+=static_cast<T>(2.0*M_PI);
32  }
33 
34  /** Modifies the given angle to translate it into the [0,2pi[ range.
35  * \note Take care of not instancing this template for integer numbers, since it only works for float, double and long double.
36  * \sa wrapToPi, wrapTo2Pi, unwrap2PiSequence
37  */
38  template <class T>
39  inline T wrapTo2Pi(T a)
40  {
42  return a;
43  }
44 
45  /** Modifies the given angle to translate it into the ]-pi,pi] range.
46  * \note Take care of not instancing this template for integer numbers, since it only works for float, double and long double.
47  * \sa wrapTo2Pi, wrapToPiInPlace, unwrap2PiSequence
48  */
49  template <class T>
50  inline T wrapToPi(T a)
51  {
52  return wrapTo2Pi( a + static_cast<T>(M_PI) )-static_cast<T>(M_PI);
53  }
54 
55  /** Modifies the given angle to translate it into the ]-pi,pi] range.
56  * \note Take care of not instancing this template for integer numbers, since it only works for float, double and long double.
57  * \sa wrapToPi,wrapTo2Pi, unwrap2PiSequence
58  */
59  template <class T>
60  inline void wrapToPiInPlace(T &a)
61  {
62  a = wrapToPi(a);
63  }
64 
65  /** Modify a sequence of angle values such as no consecutive values have a jump larger than PI in absolute value.
66  * \sa wrapToPi
67  */
68  template <class VECTOR>
69  void unwrap2PiSequence(VECTOR &x)
70  {
71  const size_t N=x.size();
72  for (size_t i=0;i<N;i++)
73  {
74  mrpt::math::wrapToPiInPlace(x[i]); // assure it's in the -pi,pi range.
75  if (!i) continue;
76  double Ap = x[i]-x[i-1];
77  if (Ap>M_PI) x[i]-=2.*M_PI;
78  if (Ap<-M_PI) x[i]+=2.*M_PI;
79  }
80  }
81 
82 
83  /** @} */
84 
85  } // End of MATH namespace
86 } // End of namespace
87 
88 #endif
#define M_PI
Definition: bits.h:65
void wrapToPiInPlace(T &a)
Modifies the given angle to translate it into the ]-pi,pi] range.
Definition: wrap2pi.h:60
T wrapTo2Pi(T a)
Modifies the given angle to translate it into the [0,2pi[ range.
Definition: wrap2pi.h:39
T wrapToPi(T a)
Modifies the given angle to translate it into the ]-pi,pi] range.
Definition: wrap2pi.h:50
void wrapTo2PiInPlace(T &a)
Modifies the given angle to translate it into the [0,2pi[ range.
Definition: wrap2pi.h:27
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void unwrap2PiSequence(VECTOR &x)
Modify a sequence of angle values such as no consecutive values have a jump larger than PI in absolut...
Definition: wrap2pi.h:69



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014