se2ez
math.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef SE2EZ_CORE_MATH_
4 #define SE2EZ_CORE_MATH_
5 
6 #include <type_traits>
7 #include <vector>
8 #include <map>
9 #include <unordered_map>
10 
11 #include <boost/math/constants/constants.hpp>
12 
13 #include <ompl/util/RandomNumbers.h>
14 
15 #include <Eigen/Geometry>
16 #include <Eigen/StdVector>
17 
18 #include <kdl/jntarray.hpp>
19 #include <kdl/frames.hpp>
20 
21 namespace se2ez
22 {
23  /** \brief Helper functions and constants require for mathematical operations.
24  */
25  namespace math
26  {
27  /** \brief Random number generation from OMPL. An instance of this class cannot be used by multiple
28  * threads at once.
29  */
30  static ompl::RNG rng;
31 
32  static const double pi = boost::math::constants::pi<double>();
33  static const double pipi = 2. * pi;
34  static const double pi2 = pi / 2.;
35 
36  static const double eps = std::numeric_limits<double>::epsilon();
37  static const double inf = std::numeric_limits<double>::infinity();
39 
40  /** \brief Converts a single double value into a 1 x 1 matrix.
41  * \param[in] x Value to convert.
42  * \return a 1-dimensional vector with x as the value.
43  */
44  Eigen::VectorXd toVector(double x);
45 
46  /** \brief Converts a vector of lists into a Matrix
47  * \param[in] vlist vector of lists to convert.
48  * \return a matrix with the values of the Eigen vectors.
49  */
50  Eigen::MatrixXd toMatrix(const std::vector<Eigen::VectorXd> &vlist);
51 
52  /** \brief Converts an initialize list into a vector
53  * \param[in] x Values to convert.
54  * \return a vector with the values of the list.
55  */
56  Eigen::VectorXd toVector(const std::initializer_list<double> &x);
57 
58  /** \brief Remap a value \e av in the interval \e a1, \e a2 to the interval \e b1, \e b2.
59  * \param[in] a1 Lower end of first interval.
60  * \param[in] a2 Upper end of first interval.
61  * \param[in] av Value in first interval.
62  * \param[in] b1 Lower end of second interval.
63  * \param[in] b2 Upper end of second interval.
64  * \return The remapped value in the second interval.
65  */
66  double remap(double a1, double a2, double av, double b1, double b2);
67 
68  /** \brief Clamp a value \e v between a range [a, b].
69  * \param[in] v Value to clamp.
70  * \param[in] a Lower bound.
71  * \param[in] b Upper bound.
72  * \return The clamped value.
73  */
74  double clamp(double v, double a, double b);
75 
76  /** \brief Return the minimum distance between two angles (wrapping from -pi to pi).
77  * \param[in] v1 First angle.
78  * \param[in] v2 Second angle.
79  * \return The distance between angles.
80  */
81  double angleDistance(double v1, double v2);
82 
83  /** \brief Normalize an angle between -pi to pi.
84  * \param[in] v The angle.
85  * \return The normalized angle.
86  */
87  double angleNormalize(double v);
88 
89  /** \brief Convert an angle to degrees.
90  * \param[in] v The angle in radians.
91  * \return The angle in degrees.
92  */
93  double toDegrees(double v);
94 
95  /** \brief Convert an angle to radians.
96  * \param[in] v The angle in degrees.
97  * \return The angle in radians.
98  */
99  double toRadians(double v);
100 
101  /** \brief Return a uniform random number between \e lower and \e upper.
102  * \param[in] lower Lower bound.
103  * \param[in] upper Upper bound.
104  * \return random number between lower and upper
105  */
106  double uniformReal(double lower, double upper);
107 
108  /** \brief Return a uniform random integer between lower and upper (inclusive).
109  * \param[in] lower Lower bound
110  * \param[in] upper Upper bound
111  * \return random number between lower and upper
112  */
113  int uniformInteger(int lower, int upper);
114 
115  /** \brief Return a random number sampled from a gaussain with given \e std and \e mean.
116  * \param[in] mean The mean of the gaussian.
117  * \param[in] std The standard deviation of the gaussian.
118  * \return random number sampled from gaussian
119  */
120  double gaussianReal(double mean, double std);
121  } // namespace math
122 
123  /** \brief Helper functions for transform manipulation.
124  */
125  namespace tf
126  {
127  /** \brief A template for a vector that stores Eigen objects.
128  * \tparam V Value for the vector (contains an Eigen type).
129  */
130  template <typename V>
132 
133  /** \brief A template for a map that stores Eigen objects.
134  * \tparam K Key for the map (not an Eigen type).
135  * \tparam V Value for the map (contains an Eigen type).
136  */
137  template <typename K, typename V>
138  using EigenMap = std::map<K, V, //
139  std::less<K>, //
140  Eigen::aligned_allocator<std::pair<const K, V>>>;
141 
142  /** \brief A template for an unordered map that stores Eigen objects.
143  * \tparam K Key for the map (not an Eigen type).
144  * \tparam V Value for the map (contains an Eigen type).
145  */
146  template <typename K, typename V>
147  using EigenHash = std::unordered_map<K, V, //
149  Eigen::aligned_allocator<std::pair<const K, V>>>;
150 
151  /** \name Eigen Isometry2d operations
152  \{ */
153 
154  /** \brief Converts a translation (x, y) and rotation from X-axis (theta) into a transform.
155  * \param[in] x X coordinate of translation.
156  * \param[in] y Y coordinate of translation.
157  * \param[in] theta Rotation from the X-axis.
158  * \return Transform corresponding to input.
159  */
160  Eigen::Isometry2d toIsometry(double x, double y, double theta);
161 
162  /** \brief Gets rotation from a frame from the X-axis (t)
163  * \param[in] frame Frame to get rotation from.
164  * \return Angle of transformation.
165  */
166  double getRotation(const Eigen::Isometry2d &frame);
167 
168  /** \brief Converts a vector [x, y, t] composed of a translation (x, y) and rotation from X-axis (t)
169  * into a transform.
170  * \param[in] vec Vector representation of transform.
171  * \return Transform corresponding to input.
172  */
173  Eigen::Isometry2d toIsometry(const Eigen::Vector3d &vec);
174 
175  /** \brief Return the transform distance between two transformations, \e w is the weight of the
176  * euclidean distance, and (1-\e w) the weight of the rotational distance (wrapping from -pi to pi).
177  * \param[in] t1 First transform.
178  * \param[in] t2 Second transform.
179  * \param[in] alpha Weight of the euclidean distance (must be between 0 and 1).
180  * \return The weighted distance between the transforms.
181  */
182  double transformDistance(const Eigen::Isometry2d &t1,
183  const Eigen::Isometry2d &t2 = Eigen::Isometry2d::Identity(),
184  double alpha = 0.75);
185 
186  /** \brief Converts a transformation \e frame into a vector [x, y, t] composed of a translation (x, y)
187  * and rotation from X-axis(t).
188  * \param[in] frame Transform to flatten.
189  * \return Vector representation of transform.
190  */
191  Eigen::Vector3d flattenIsometry(const Eigen::Isometry2d &frame);
192 
193  /** \} */
194 
195  /** \name KDL and Eigen Conversions
196  \{ */
197 
198  /** \brief Converts an Eigen transformation into a KDL transformation.
199  * \param[in] iso Eigen transform.
200  * \return KDL transform.
201  */
202  KDL::Frame EigenToKDLFrame(const Eigen::Isometry2d &iso);
203 
204  /** \brief Converts an KDL transformation into a Eigen transformation.
205  * \param[in] frame KDL transform.
206  * \return Eigen transform.
207  */
208  Eigen::Isometry2d KDLToEigenFrame(const KDL::Frame &frame);
209 
210  /** \brief Converts an Eigen vector into a KDL vector.
211  * \param[in] vec Eigen vector.
212  * \return KDL vector.
213  */
214  KDL::JntArray EigenToKDLVec(const Eigen::VectorXd &vec);
215 
216  /** \brief Converts an KDL vector into a Eigen vector.
217  * \param[in] array KDL vector.
218  * \return Eigen vector.
219  */
220  Eigen::VectorXd KDLToEigenVec(const KDL::JntArray &array);
221 
222  /** \} */
223 
224  /** \name Debug & Output
225  \{ */
226 
227  /** \brief Returns a string "[x, y, t]" of the translation (x, y) and rotation (t) component of a
228  * transform.
229  * \param[in] frame Frame to print.
230  * \return Printable string of transform data.
231  */
232  std::string printFrame(const Eigen::Isometry2d &frame);
233 
234  /** \brief Returns a string "[x, y, t]" of the translation (x, y) and rotation (t) component of a
235  * transform.
236  * \param[in] frame Frame to print.
237  * \return Printable string of transform data.
238  */
239  std::string printFrame(const KDL::Frame &frame);
240 
241  /** \brief Returns a string of a vector's contents.
242  * \param[in] v Vector to print.
243  * \param[in] precision Precision to print floating point values at.
244  * \return Printable string of vector.
245  */
246  std::string printVector(const Eigen::VectorXd &v, unsigned int precision = 4);
247 
248  /** \brief Returns a string of a matrix's contents.
249  * \param[in] v Matrix to print.
250  * \param[in] precision Precision to print floating point values at.
251  * \return Printable string of vector.
252  */
253  std::string printMatrix(const Eigen::MatrixXd &v, unsigned int precision = 4);
254 
255  /** \} */
256  }; // namespace tf
257 }; // namespace se2ez
258 
259 #endif
Eigen::Isometry2d KDLToEigenFrame(const KDL::Frame &frame)
Converts an KDL transformation into a Eigen transformation.
Definition: math.cpp:55
std::string printFrame(const KDL::Frame &frame)
Returns a string "[x, y, t]" of the translation (x, y) and rotation (t) component of a transform...
Definition: math.cpp:85
double gaussianReal(double mean, double std)
Return a random number sampled from a gaussain with given std and mean.
Definition: math.cpp:236
double toRadians(double v)
Convert an angle to radians.
Definition: math.cpp:204
Eigen::Isometry2d toIsometry(const Eigen::Vector3d &vec)
Converts a vector [x, y, t] composed of a translation (x, y) and rotation from X-axis (t) into a tran...
Definition: math.cpp:20
Eigen::Vector3d flattenIsometry(const Eigen::Isometry2d &frame)
Converts a transformation frame into a vector [x, y, t] composed of a translation (x...
Definition: math.cpp:30
Eigen::MatrixXd toMatrix(const std::vector< Eigen::VectorXd > &vlist)
Converts a vector of lists into a Matrix.
Definition: math.cpp:150
static const double pi
Definition: math.h:32
T epsilon(T... args)
double uniformReal(double lower, double upper)
Return a uniform random number between lower and upper.
Definition: math.cpp:217
double getRotation(const Eigen::Isometry2d &frame)
Gets rotation from a frame from the X-axis (t)
Definition: math.cpp:25
double clamp(double v, double a, double b)
Clamp a value v between a range [a, b].
Definition: math.cpp:179
std::string printMatrix(const Eigen::MatrixXd &v, unsigned int precision=4)
Returns a string of a matrix&#39;s contents.
Definition: math.cpp:109
Eigen::VectorXd KDLToEigenVec(const KDL::JntArray &array)
Converts an KDL vector into a Eigen vector.
Definition: math.cpp:74
double transformDistance(const Eigen::Isometry2d &t1, const Eigen::Isometry2d &t2=Eigen::Isometry2d::Identity(), double alpha=0.75)
Return the transform distance between two transformations, w is the weight of the euclidean distance...
Definition: math.cpp:36
static const double inf
Definition: math.h:37
static const double nan
Definition: math.h:38
static const double pipi
Definition: math.h:33
static const double eps
Definition: math.h:36
Eigen::VectorXd toVector(double x)
Converts a single double value into a 1 x 1 matrix.
Definition: math.cpp:134
T infinity(T... args)
double angleNormalize(double v)
Normalize an angle between -pi to pi.
Definition: math.cpp:189
static const double pi2
Definition: math.h:34
KDL::JntArray EigenToKDLVec(const Eigen::VectorXd &vec)
Converts an Eigen vector into a KDL vector.
Definition: math.cpp:66
std::string printVector(const Eigen::VectorXd &v, unsigned int precision=4)
Returns a string of a vector&#39;s contents.
Definition: math.cpp:90
KDL::Frame EigenToKDLFrame(const Eigen::Isometry2d &iso)
Converts an Eigen transformation into a KDL transformation.
Definition: math.cpp:48
int uniformInteger(int lower, int upper)
Return a uniform random integer between lower and upper (inclusive).
Definition: math.cpp:228
double toDegrees(double v)
Convert an angle to degrees.
Definition: math.cpp:194
Main namespace.
Definition: collision.h:11
T quiet_NaN(T... args)
double remap(double a1, double a2, double av, double b1, double b2)
Remap a value av in the interval a1, a2 to the interval b1, b2.
Definition: math.cpp:160
static ompl::RNG rng
Random number generation from OMPL. An instance of this class cannot be used by multiple threads at o...
Definition: math.h:30
double angleDistance(double v1, double v2)
Return the minimum distance between two angles (wrapping from -pi to pi).
Definition: math.cpp:184