math.cpp
Go to the documentation of this file.
1 #include "math.hpp"
2 
3 
4 
5 using namespace astra_ros;
6 
7 double astra_ros::sum(const geometry_msgs::Point &v)
8 {
9  return v.x + v.y + v.z;
10 }
11 
12 double astra_ros::distance(const geometry_msgs::Point &lhs, const geometry_msgs::Point &rhs)
13 {
14  const auto d = lhs - rhs;
15  return sqrt(sum(d * d));
16 }
17 
18 geometry_msgs::Point astra_ros::operator +(const geometry_msgs::Point &lhs, const geometry_msgs::Point &rhs)
19 {
20  geometry_msgs::Point ret;
21  ret.x = lhs.x + rhs.x;
22  ret.y = lhs.y + rhs.y;
23  ret.z = lhs.z + rhs.z;
24  return ret;
25 }
26 
27 geometry_msgs::Point astra_ros::operator -(const geometry_msgs::Point &lhs, const geometry_msgs::Point &rhs)
28 {
29  geometry_msgs::Point ret;
30  ret.x = lhs.x - rhs.x;
31  ret.y = lhs.y - rhs.y;
32  ret.z = lhs.z - rhs.z;
33  return ret;
34 }
35 
36 geometry_msgs::Point astra_ros::operator *(const geometry_msgs::Point &lhs, const geometry_msgs::Point &rhs)
37 {
38  geometry_msgs::Point ret;
39  ret.x = lhs.x * rhs.x;
40  ret.y = lhs.y * rhs.y;
41  ret.z = lhs.z * rhs.z;
42  return ret;
43 }
44 
45 geometry_msgs::Point astra_ros::operator /(const geometry_msgs::Point &lhs, const double rhs)
46 {
47  geometry_msgs::Point ret;
48  ret.x = lhs.x / rhs;
49  ret.y = lhs.y / rhs;
50  ret.z = lhs.z / rhs;
51  return ret;
52 }
53 
54 Eigen::Vector3d astra_ros::toEigen(const geometry_msgs::Point &point)
55 {
56  return Eigen::Vector3d(point.x, point.y, point.z);
57 }
58 
59 Eigen::Vector3d astra_ros::toEigen(const geometry_msgs::Vector3 &vector)
60 {
61  return Eigen::Vector3d(vector.x, vector.y, vector.z);
62 }
63 
64 Eigen::Quaterniond astra_ros::toEigen(const geometry_msgs::Quaternion &quaternion)
65 {
66  return Eigen::Quaterniond(quaternion.w, quaternion.x, quaternion.y, quaternion.z);
67 }
68 
69 Eigen::Affine3d astra_ros::toEigen(const geometry_msgs::Transform &transform)
70 {
71  Eigen::Affine3d ret = Eigen::Translation3d(toEigen(transform.translation)) * toEigen(transform.rotation);
72  return ret;
73 }
74 
75 geometry_msgs::Point astra_ros::fromEigen(const Eigen::Vector3d &point)
76 {
77  geometry_msgs::Point ret;
78  ret.x = point.x();
79  ret.y = point.y();
80  ret.z = point.z();
81  return ret;
82 }
83 
84 geometry_msgs::Quaternion astra_ros::fromEigen(const Eigen::Quaterniond &quaternion)
85 {
86  geometry_msgs::Quaternion ret;
87  ret.x = quaternion.x();
88  ret.y = quaternion.y();
89  ret.z = quaternion.z();
90  ret.w = quaternion.w();
91  return ret;
92 }
93 
94 geometry_msgs::Transform astra_ros::fromEigen(const Eigen::Affine3d &transform)
95 {
96  geometry_msgs::Transform ret;
97  Eigen::Quaterniond rotation(transform.rotation());
98  Eigen::Vector3d translation = transform.translation();
99 
100  ret.translation.x = translation.x();
101  ret.translation.y = translation.y();
102  ret.translation.z = translation.z();
103 
104  ret.rotation = fromEigen(rotation);
105 
106  return ret;
107 }
108 
109 namespace
110 {
111  const static double EPSILON = 0.000001;
112 }
113 
114 // Adapted from https://stackoverflow.com/questions/12435671/quaternion-lookat-function
115 Eigen::Quaterniond astra_ros::lookAt(const Eigen::Vector3d &source_point, const Eigen::Vector3d &dest_point, const Eigen::Vector3d &forward, const Eigen::Vector3d &up)
116 {
117  using namespace Eigen;
118 
119  const Vector3d forward_vec = (dest_point - source_point).normalized();
120  const double dot = forward.dot(forward_vec);
121 
122  if (std::abs(dot + 1.0f) < EPSILON)
123  {
124  return Quaterniond(M_PI, up.x(), up.y(), up.z());
125  }
126 
127  if (std::abs(dot - 1.0f) < EPSILON)
128  {
129  return Quaterniond::Identity();
130  }
131 
132  const double angle = acos(dot);
133  const Vector3d axis = forward.cross(forward_vec).normalized();
134 
135  Quaterniond ret;
136  ret = AngleAxisd(angle, axis);
137  return ret;
138 }
dot
TF2SIMD_FORCE_INLINE tf2Scalar dot(const Quaternion &q1, const Quaternion &q2)
astra_ros::operator-
geometry_msgs::Point operator-(const geometry_msgs::Point &lhs, const geometry_msgs::Point &rhs)
Definition: math.cpp:27
astra_ros::distance
double distance(const geometry_msgs::Point &lhs, const geometry_msgs::Point &rhs)
Definition: math.cpp:12
astra_ros::lookAt
Eigen::Quaterniond lookAt(const Eigen::Vector3d &source_point, const Eigen::Vector3d &dest_point, const Eigen::Vector3d &forward, const Eigen::Vector3d &up)
Definition: math.cpp:115
f
f
astra_ros::operator/
geometry_msgs::Point operator/(const geometry_msgs::Point &lhs, const double rhs)
Definition: math.cpp:45
astra_ros::operator*
geometry_msgs::Point operator*(const geometry_msgs::Point &lhs, const geometry_msgs::Point &rhs)
Definition: math.cpp:36
astra_ros::operator+
geometry_msgs::Point operator+(const geometry_msgs::Point &lhs, const geometry_msgs::Point &rhs)
Definition: math.cpp:18
d
d
astra_ros::fromEigen
geometry_msgs::Point fromEigen(const Eigen::Vector3d &point)
Definition: math.cpp:75
angle
TF2SIMD_FORCE_INLINE tf2Scalar angle(const Quaternion &q1, const Quaternion &q2)
astra_ros::sum
double sum(const geometry_msgs::Point &v)
Definition: math.cpp:7
math.hpp
astra_ros::toEigen
Eigen::Vector3d toEigen(const geometry_msgs::Point &point)
Definition: math.cpp:54
astra_ros
Definition: Device.hpp:14


astra_ros
Author(s): Braden McDorman
autogenerated on Wed Mar 2 2022 00:53:06