15 #ifndef BELUGA_ROS_TF2_SOPHUS_HPP
16 #define BELUGA_ROS_TF2_SOPHUS_HPP
18 #include <tf2/convert.h>
19 #include <tf2/utils.h>
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>
28 #error BELUGA_ROS_VERSION is not defined or invalid
31 #include <sophus/se2.hpp>
32 #include <sophus/se3.hpp>
33 #include <sophus/types.hpp>
35 #include <beluga_ros/messages.hpp>
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();
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.);
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();
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.);
107 msg.rotation.z = std::sin(theta / 2.);
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();
138 template <
class Scalar>
139 inline void fromMsg(
const beluga_ros::msg::Transform& msg, Sophus::SE2<Scalar>& out) {
140 out.translation() = Sophus::Vector2<Scalar>{
144 out.so2() = Sophus::SO2<Scalar>{
static_cast<Scalar
>(tf2::getYaw(msg.rotation))};
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),
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),
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),
182 out.so2() = Sophus::SO2<Scalar>{
static_cast<Scalar
>(tf2::getYaw(msg.orientation))};
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),
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),
213 template <
template <
typename, std::
size_t>
class Array,
typename Scalar>
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);
234 template <
template <
typename, std::
size_t>
class Array,
typename Scalar>
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];
254 template <
class Scalar>
255 inline beluga_ros::msg::Transform
toMsg(
const Sophus::SE2<Scalar>& pose) {
260 template <
class Scalar>
261 inline beluga_ros::msg::Transform
toMsg(
const Sophus::SE3<Scalar>& in) {
266 template <
class Scalar>
267 inline void fromMsg(
const beluga_ros::msg::Transform& message, Sophus::SE2<Scalar>& pose) {
272 template <
class Scalar>
273 inline void fromMsg(
const beluga_ros::msg::Transform& message, Sophus::SE3<Scalar>& pose) {
278 template <
class Scalar>
279 inline void fromMsg(
const beluga_ros::msg::Pose& message, Sophus::SE2<Scalar>& pose) {
284 template <
class Scalar>
285 inline void fromMsg(
const beluga_ros::msg::Pose& message, Sophus::SE3<Scalar>& pose) {
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