/home/runner/work/beluga/beluga/beluga_ros/include/beluga_ros/tf2_sophus.hpp Source File

/home/runner/work/beluga/beluga/beluga_ros/include/beluga_ros/tf2_sophus.hpp Source File#

Beluga ROS: /home/runner/work/beluga/beluga/beluga_ros/include/beluga_ros/tf2_sophus.hpp Source File
Beluga ROS
tf2_sophus.hpp
Go to the documentation of this file.
1 // Copyright 2022-2023 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BELUGA_ROS_TF2_SOPHUS_HPP
16 #define BELUGA_ROS_TF2_SOPHUS_HPP
17 
18 #include <tf2/convert.h>
19 #include <tf2/utils.h>
20 
21 #if BELUGA_ROS_VERSION == 2
22 #include <tf2_eigen/tf2_eigen.hpp>
23 #include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
24 #elif BELUGA_ROS_VERSION == 1
25 #include <tf2_eigen/tf2_eigen.h>
26 #include <tf2_geometry_msgs/tf2_geometry_msgs.h>
27 #else
28 #error BELUGA_ROS_VERSION is not defined or invalid
29 #endif
30 
31 #include <sophus/se2.hpp>
32 #include <sophus/se3.hpp>
33 #include <sophus/types.hpp>
34 
35 #include <beluga_ros/messages.hpp>
36 
43 namespace tf2 {
44 
46 
55 template <class Scalar>
56 inline beluga_ros::msg::Pose& toMsg(const Sophus::SE2<Scalar>& in, beluga_ros::msg::Pose& out) {
57  const double theta = in.so2().log();
58  out.position.x = in.translation().x();
59  out.position.y = in.translation().y();
60  out.position.z = 0;
61  out.orientation.w = std::cos(theta / 2.);
62  out.orientation.x = 0;
63  out.orientation.y = 0;
64  out.orientation.z = std::sin(theta / 2.);
65  return out;
66 }
67 
69 
78 template <class Scalar>
79 inline beluga_ros::msg::Pose& toMsg(const Sophus::SE3<Scalar>& in, beluga_ros::msg::Pose& out) {
80  out.position.x = in.translation().x();
81  out.position.y = in.translation().y();
82  out.position.z = in.translation().z();
83  out.orientation.w = in.so3().unit_quaternion().w();
84  out.orientation.x = in.so3().unit_quaternion().x();
85  out.orientation.y = in.so3().unit_quaternion().y();
86  out.orientation.z = in.so3().unit_quaternion().z();
87  return out;
88 }
89 
91 
97 template <class Scalar>
98 inline beluga_ros::msg::Transform toMsg(const Sophus::SE2<Scalar>& in) {
99  auto msg = beluga_ros::msg::Transform{};
100  const double theta = in.so2().log();
101  msg.translation.x = in.translation().x();
102  msg.translation.y = in.translation().y();
103  msg.translation.z = 0;
104  msg.rotation.w = std::cos(theta / 2.);
105  msg.rotation.x = 0;
106  msg.rotation.y = 0;
107  msg.rotation.z = std::sin(theta / 2.);
108  return msg;
109 }
110 
112 
118 template <class Scalar>
119 inline beluga_ros::msg::Transform toMsg(const Sophus::SE3<Scalar>& in) {
120  auto msg = beluga_ros::msg::Transform{};
121  msg.translation.x = in.translation().x();
122  msg.translation.y = in.translation().y();
123  msg.translation.z = in.translation().z();
124  msg.rotation.w = in.so3().unit_quaternion().w();
125  msg.rotation.x = in.so3().unit_quaternion().x();
126  msg.rotation.y = in.so3().unit_quaternion().y();
127  msg.rotation.z = in.so3().unit_quaternion().z();
128  return msg;
129 }
130 
132 
138 template <class Scalar>
139 inline void fromMsg(const beluga_ros::msg::Transform& msg, Sophus::SE2<Scalar>& out) {
140  out.translation() = Sophus::Vector2<Scalar>{
141  msg.translation.x,
142  msg.translation.y,
143  };
144  out.so2() = Sophus::SO2<Scalar>{static_cast<Scalar>(tf2::getYaw(msg.rotation))};
145 }
146 
148 
154 template <class Scalar>
155 inline void fromMsg(const beluga_ros::msg::Transform& msg, Sophus::SE3<Scalar>& out) {
156  out.translation() = Sophus::Vector3<Scalar>{
157  static_cast<Scalar>(msg.translation.x),
158  static_cast<Scalar>(msg.translation.y),
159  static_cast<Scalar>(msg.translation.z),
160  };
161  out.so3() = Sophus::SO3<Scalar>{Eigen::Quaternion<Scalar>{
162  static_cast<Scalar>(msg.rotation.w),
163  static_cast<Scalar>(msg.rotation.x),
164  static_cast<Scalar>(msg.rotation.y),
165  static_cast<Scalar>(msg.rotation.z),
166  }};
167 }
168 
170 
176 template <class Scalar>
177 inline void fromMsg(const beluga_ros::msg::Pose& msg, Sophus::SE2<Scalar>& out) {
178  out.translation() = Sophus::Vector2<Scalar>{
179  static_cast<Scalar>(msg.position.x),
180  static_cast<Scalar>(msg.position.y),
181  };
182  out.so2() = Sophus::SO2<Scalar>{static_cast<Scalar>(tf2::getYaw(msg.orientation))};
183 }
184 
186 
192 template <class Scalar>
193 inline void fromMsg(const beluga_ros::msg::Pose& msg, Sophus::SE3<Scalar>& out) {
194  out.translation() = Sophus::Vector3<Scalar>{
195  static_cast<Scalar>(msg.position.x),
196  static_cast<Scalar>(msg.position.y),
197  static_cast<Scalar>(msg.position.z),
198  };
199  out.so3() = Sophus::SO3<Scalar>{Eigen::Quaternion<Scalar>{
200  static_cast<Scalar>(msg.orientation.w),
201  static_cast<Scalar>(msg.orientation.x),
202  static_cast<Scalar>(msg.orientation.y),
203  static_cast<Scalar>(msg.orientation.z),
204  }};
205 }
206 
208 
213 template <template <typename, std::size_t> class Array, typename Scalar>
214 inline Array<Scalar, 36>& covarianceEigenToRowMajor(const Sophus::Matrix3<Scalar>& in, Array<Scalar, 36>& out) {
215  out.fill(0);
216  out[0] = in.coeff(0, 0);
217  out[1] = in.coeff(0, 1);
218  out[5] = in.coeff(0, 2);
219  out[6] = in.coeff(1, 0);
220  out[7] = in.coeff(1, 1);
221  out[11] = in.coeff(1, 2);
222  out[30] = in.coeff(2, 0);
223  out[31] = in.coeff(2, 1);
224  out[35] = in.coeff(2, 2);
225  return out;
226 }
227 
229 
234 template <template <typename, std::size_t> class Array, typename Scalar>
235 inline Sophus::Matrix3<Scalar>& covarianceRowMajorToEigen(const Array<Scalar, 36>& in, Sophus::Matrix3<Scalar>& out) {
236  out.coeffRef(0, 0) = in[0];
237  out.coeffRef(0, 1) = in[1];
238  out.coeffRef(0, 2) = in[5];
239  out.coeffRef(1, 0) = in[6];
240  out.coeffRef(1, 1) = in[7];
241  out.coeffRef(1, 2) = in[11];
242  out.coeffRef(2, 0) = in[30];
243  out.coeffRef(2, 1) = in[31];
244  out.coeffRef(2, 2) = in[35];
245  return out;
246 }
247 
248 } // namespace tf2
249 
251 namespace Sophus { // NOLINT(readability-identifier-naming)
252 
254 template <class Scalar>
255 inline beluga_ros::msg::Transform toMsg(const Sophus::SE2<Scalar>& pose) {
256  return tf2::toMsg(pose);
257 }
258 
260 template <class Scalar>
261 inline beluga_ros::msg::Transform toMsg(const Sophus::SE3<Scalar>& in) {
262  return tf2::toMsg(in);
263 }
264 
266 template <class Scalar>
267 inline void fromMsg(const beluga_ros::msg::Transform& message, Sophus::SE2<Scalar>& pose) {
268  tf2::fromMsg(message, pose);
269 }
270 
272 template <class Scalar>
273 inline void fromMsg(const beluga_ros::msg::Transform& message, Sophus::SE3<Scalar>& pose) {
274  tf2::fromMsg(message, pose);
275 }
276 
278 template <class Scalar>
279 inline void fromMsg(const beluga_ros::msg::Pose& message, Sophus::SE2<Scalar>& pose) {
280  tf2::fromMsg(message, pose);
281 }
282 
284 template <class Scalar>
285 inline void fromMsg(const beluga_ros::msg::Pose& message, Sophus::SE3<Scalar>& pose) {
286  tf2::fromMsg(message, pose);
287 }
288 
289 } // namespace Sophus
290 
291 #endif // BELUGA_ROS_TF2_SOPHUS_HPP
Sophus namespace extension for message conversion function overload resolution (ADL enabled).
Definition: tf2_sophus.hpp:251
beluga_ros::msg::Transform toMsg(const Sophus::SE2< Scalar > &pose)
Converts an SE2 pose to a geometry_msgs/Transform message.
Definition: tf2_sophus.hpp:255
void fromMsg(const beluga_ros::msg::Transform &message, Sophus::SE2< Scalar > &pose)
Extracts an SE2 pose from a geometry_msgs/Transform message.
Definition: tf2_sophus.hpp:267
tf2 namespace extension for message conversion overload resolution.
Definition: tf2_eigen.hpp:38
beluga_ros::msg::Point toMsg(const Eigen::Matrix< Scalar, 2, 1 > &in)
Converts an Eigen Vector2 type to a Point message.
Definition: tf2_eigen.hpp:46
void fromMsg(const beluga_ros::msg::Transform &msg, Sophus::SE2< Scalar > &out)
Converts a Transform message type to a Sophus SE2 type.
Definition: tf2_sophus.hpp:139
Sophus::Matrix3< Scalar > & covarianceRowMajorToEigen(const Array< Scalar, 36 > &in, Sophus::Matrix3< Scalar > &out)
Converts a 6x6 row-major array to an Eigen 3x3 covariance matrix.
Definition: tf2_sophus.hpp:235
Array< Scalar, 36 > & covarianceEigenToRowMajor(const Sophus::Matrix3< Scalar > &in, Array< Scalar, 36 > &out)
Converts a Sophus (ie. Eigen) 3x3 covariance matrix to a 6x6 row-major array.
Definition: tf2_sophus.hpp:214