mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
misc.h
Go to the documentation of this file.
1// clang: MatousFormat
8#ifndef GEOMETRY_MISC_H
9#define GEOMETRY_MISC_H
10
11#include <cmath>
12#include <Eigen/Dense>
13#include <geometry_msgs/msg/point.h>
14#include <geometry_msgs/msg/quaternion.h>
16
17namespace mrs_lib
18{
19 namespace geometry
20 {
21 template <int dims>
22 using vec_t = Eigen::Matrix<double, dims, 1>;
23
24 using pt2_t = vec_t<2>;
25 using vec2_t = vec_t<2>;
26 using pt3_t = vec_t<3>;
27 using vec3_t = vec_t<3>;
28
29 using mat3_t = Eigen::Matrix3d;
30
31 using quat_t = Eigen::Quaterniond;
32 using anax_t = Eigen::AngleAxisd;
33
34 template <int dims>
35 vec_t<dims + 1> toHomogenous(const vec_t<dims>& vec)
36 {
37 const Eigen::Matrix<double, dims + 1, 1> ret((Eigen::Matrix<double, dims + 1, 1>() << vec, 1).finished());
38 return ret;
39 }
40
41 // | ----------------- Angle-related functions ---------------- |
42
43 /* angle-related functions //{ */
44
45 /* headingNEDtoENU() //{ */
46
61 template <typename OutRangeT = sradians>
62 double headingNEDtoENU(const double& heading_ned)
63 {
64 const OutRangeT heading_enu(M_PI_2 - heading_ned);
65 return heading_enu.value();
66 }
67
68 //}
69
70 /* headingENUtoNED() //{ */
71
85 template <typename OutRangeT = radians>
86 double headingENUtoNED(const double& heading_enu)
87 {
88 const OutRangeT heading_ned(M_PI_2 - heading_enu);
89 return heading_ned.value();
90 }
91
92 //}
93
94 /* headingFromRot() //{ */
95
106 template <typename T>
107 double headingFromRot(const T& rot)
108 {
109 const vec3_t rot_vec = rot * vec3_t::UnitX();
110 return std::atan2(rot_vec.y(), rot_vec.x());
111 }
112
113 //}
114
115 /* angleBetween() //{ */
116
129 double angleBetween(const vec2_t& a, const vec2_t& b);
130
143 double angleBetween(const vec3_t& a, const vec3_t& b);
144
145 //}
146
147 /* angleaxisBetween() //{ */
148
162 anax_t angleaxisBetween(const vec3_t& a, const vec3_t& b, const double tolerance = 1e-9);
163
164 //}
165
166 /* quaternionBetween() //{ */
167
179 quat_t quaternionBetween(const vec3_t& a, const vec3_t& b, const double tolerance = 1e-9);
180
181 //}
182
183 /* rotationBetween() //{ */
184
196 Eigen::Matrix3d rotationBetween(const vec3_t& a, const vec3_t& b, const double tolerance = 1e-9);
197
198 //}
199
200 /* haversin() //{ */
201
209 double haversin(const double angle);
210
211 //}
212
213 /* invHaversin() //{ */
214
222 double invHaversin(const double value);
223
224 //}
225
226 /* solidAngle //{ */
227
237 double solidAngle(double a, double b, double c);
238
239 //}
240
241 /* quaternionFromEuler() //{ */
242
252 quat_t quaternionFromEuler(double x, double y, double z);
253
261 quat_t quaternionFromEuler(const Eigen::Vector3d& euler);
262
263 //}
264
274 quat_t quaternionFromHeading(const double heading);
275
276 //}
277
278 // | ----------------- Miscellaneous functions ---------------- |
279
280 /* 2D cross() //{ */
281
293 double cross(const vec2_t& a, const vec2_t b);
294
295 //}
296
297 /* vector distance //{ */
298
307 double dist(const vec2_t& a, const vec2_t& b);
308
317 double dist(const vec3_t& a, const vec3_t& b);
318
319 //}
320
321 /* triangleArea() //{ */
322
332 double triangleArea(const double a, const double b, const double c);
333
334 //}
335
336 /* sphericalTriangleArea //{ */
337
347 double sphericalTriangleArea(Eigen::Vector3d a, Eigen::Vector3d b, Eigen::Vector3d c);
348
349 //}
350
351 /* rotateCovariance() //{ */
352
362 template <typename T>
363 mat3_t rotateCovariance(const mat3_t& cov, const T& rot)
364 {
365 const mat3_t matrot(rot);
366 return matrot * cov * matrot.transpose();
367 }
368
369 //}
370
371 } // namespace geometry
372} // namespace mrs_lib
373
374#endif
Defines the cyclic class for calculations with cyclic quantities.
anax_t angleaxisBetween(const vec3_t &a, const vec3_t &b, const double tolerance=1e-9)
Returns the rotation between two vectors, represented as angle-axis.
double solidAngle(double a, double b, double c)
computes the solid angle for a spherical surface corresponding to a 'triangle' with edge lengths a,...
Definition misc.cpp:129
double dist(const vec2_t &a, const vec2_t &b)
distnace between two 2D Eigen vectors
Definition misc.cpp:169
double headingNEDtoENU(const double &heading_ned)
Converts the heading angle from the North-East-Down to the East-North-Up frame.
Definition misc.h:62
Eigen::Matrix3d rotationBetween(const vec3_t &a, const vec3_t &b, const double tolerance=1e-9)
Returns the rotation between two vectors, represented as a rotation matrix.
double invHaversin(const double value)
computes the inverse haversine angle for a given value
Definition misc.cpp:121
double haversin(const double angle)
computes the haversine (half of versine) for a given angle
Definition misc.cpp:112
double triangleArea(const double a, const double b, const double c)
uses Heron's formula to compute area of a given triangle using side lengths
Definition misc.cpp:139
double sphericalTriangleArea(Eigen::Vector3d a, Eigen::Vector3d b, Eigen::Vector3d c)
compute the area of a 'triangle' drawn on a spherical surface
Definition misc.cpp:148
quat_t quaternionFromHeading(const double heading)
create a quaternion from heading
Definition misc.cpp:91
double headingFromRot(const T &rot)
Returns the heading angle from the rotation.
Definition misc.h:107
quat_t quaternionFromEuler(double x, double y, double z)
create a quaternion from 3 provided Euler angles
Definition misc.cpp:79
double headingENUtoNED(const double &heading_enu)
Converts the heading angle from the East-North-Up to the North-East-Down frame.
Definition misc.h:86
mat3_t rotateCovariance(const mat3_t &cov, const T &rot)
Returns the covariance rotated using the specified rotation.
Definition misc.h:363
double cross(const vec2_t &a, const vec2_t b)
Implementation of cross product for 2D vectors.
Definition misc.cpp:19
quat_t quaternionBetween(const vec3_t &a, const vec3_t &b, const double tolerance=1e-9)
Returns the rotation between two vectors, represented as a quaternion.
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24