|
34 | 34 | * |
35 | 35 | * Author: Eitan Marder-Eppstein |
36 | 36 | *********************************************************************/ |
| 37 | + |
37 | 38 | #include <gtest/gtest.h> |
38 | | -#include <geometry_msgs/msg/point_stamped.hpp> |
| 39 | + |
39 | 40 | #include <geometry_msgs/msg/point.hpp> |
| 41 | +#include <geometry_msgs/msg/point_stamped.hpp> |
40 | 42 | #include <geometry_msgs/msg/vector3.hpp> |
| 43 | +#include <geometry_msgs/msg/vector3_stamped.hpp> |
| 44 | + |
41 | 45 | #include <tf2/convert.h> |
42 | 46 | #include <tf2/LinearMath/Quaternion.h> |
43 | | -#include <tf2_kdl/tf2_kdl.hpp> |
| 47 | + |
44 | 48 | #include <tf2_bullet/tf2_bullet.hpp> |
45 | 49 | #include <tf2_eigen/tf2_eigen.hpp> |
46 | 50 | #include <tf2_geometry_msgs/tf2_geometry_msgs.hpp> |
| 51 | +#include <tf2_kdl/tf2_kdl.hpp> |
| 52 | + |
| 53 | + |
| 54 | +// test of tf2 type traits |
| 55 | +static_assert( |
| 56 | + !tf2::impl::MessageHasStdHeader<geometry_msgs::msg::Vector3>::value, |
| 57 | + "MessageHasStdHeader traits error"); |
| 58 | +static_assert( |
| 59 | + tf2::impl::MessageHasStdHeader<geometry_msgs::msg::Vector3Stamped>::value, |
| 60 | + "MessageHasStdHeader traits error"); |
| 61 | + |
| 62 | +namespace |
| 63 | +{ |
| 64 | +struct MyPODMessage |
| 65 | +{ |
| 66 | + int header; |
| 67 | +}; |
| 68 | + |
| 69 | +template<typename Alloc> |
| 70 | +struct MyAllocMessage |
| 71 | +{ |
| 72 | + int header; |
| 73 | +}; |
| 74 | + |
| 75 | +static_assert( |
| 76 | + !tf2::impl::MessageHasStdHeader<MyPODMessage>::value, |
| 77 | + "MessageHasStdHeader traits error"); |
| 78 | + |
| 79 | +template<typename T> |
| 80 | +struct MyAllocator |
| 81 | +{ |
| 82 | + using value_type = T; |
| 83 | + using size_type = unsigned int; |
| 84 | + T * allocate(size_type); |
| 85 | + void deallocate(T *, size_type); |
| 86 | + template<class U> |
| 87 | + struct rebind { typedef MyAllocator<U> other; }; |
| 88 | +}; |
| 89 | +using MyVec = geometry_msgs::msg::Vector3_<MyAllocator<void>>; |
| 90 | +using MyVecStamped = geometry_msgs::msg::Vector3Stamped_<MyAllocator<void>>; |
| 91 | +using MyMessage = MyAllocMessage<MyAllocator<void>>; |
| 92 | + |
| 93 | +static_assert(!tf2::impl::MessageHasStdHeader<MyVec>::value, "MessageHasStdHeader traits error"); |
| 94 | +static_assert( |
| 95 | + tf2::impl::MessageHasStdHeader<MyVecStamped>::value, |
| 96 | + "MessageHasStdHeader traits error"); |
| 97 | + |
| 98 | +static_assert( |
| 99 | + !tf2::impl::MessageHasStdHeader<MyMessage>::value, |
| 100 | + "MessageHasStdHeader traits error"); |
| 101 | +} // namespace |
| 102 | + |
| 103 | +using Vector6d = Eigen::Matrix<double, 6, 1>; |
47 | 104 |
|
48 | 105 | #include <Eigen/Geometry> |
49 | 106 |
|
@@ -108,8 +165,7 @@ TEST(tf2Convert, ConvertTf2Quaternion) |
108 | 165 | { |
109 | 166 | const tf2::Quaternion tq(1, 2, 3, 4); |
110 | 167 | Eigen::Quaterniond eq; |
111 | | - // TODO(gleichdick): switch to tf2::convert() when it's working |
112 | | - tf2::fromMsg(tf2::toMsg(tq), eq); |
| 168 | + tf2::convert(tq, eq); |
113 | 169 |
|
114 | 170 | EXPECT_EQ(tq.w(), eq.w()); |
115 | 171 | EXPECT_EQ(tq.x(), eq.x()); |
@@ -188,6 +244,106 @@ TEST(tf2Convert, PointVectorOtherMessagetype) |
188 | 244 | } |
189 | 245 | } |
190 | 246 |
|
| 247 | +TEST(TfEigenKdl, TestRotationQuaternion) |
| 248 | +{ |
| 249 | + const auto kdl_v = KDL::Rotation::RPY(1.5, 0.2, 0.3); |
| 250 | + Eigen::Quaterniond eigen_v = Eigen::Quaterniond::Identity(); |
| 251 | + tf2::convert(kdl_v, eigen_v); |
| 252 | + KDL::Rotation kdl_v1; |
| 253 | + tf2::convert(eigen_v, kdl_v1); |
| 254 | + EXPECT_EQ(kdl_v, kdl_v1); |
| 255 | +} |
| 256 | + |
| 257 | +TEST(TfEigenKdl, TestQuaternionRotation) |
| 258 | +{ |
| 259 | + const Eigen::Quaterniond eigen_v = Eigen::Quaterniond(1, 2, 1.5, 3).normalized(); |
| 260 | + KDL::Rotation kdl_v; |
| 261 | + tf2::convert(eigen_v, kdl_v); |
| 262 | + Eigen::Quaterniond eigen_v1; |
| 263 | + tf2::convert(kdl_v, eigen_v1); |
| 264 | + EXPECT_TRUE(eigen_v.isApprox(eigen_v1)); |
| 265 | +} |
| 266 | + |
| 267 | +TEST(TfEigenKdl, TestFrameIsometry3d) |
| 268 | +{ |
| 269 | + const auto kdl_v = KDL::Frame(KDL::Rotation::RPY(1.2, 0.2, 0), KDL::Vector(1, 2, 3)); |
| 270 | + Eigen::Isometry3d eigen_v = Eigen::Isometry3d::Identity(); |
| 271 | + tf2::convert(kdl_v, eigen_v); |
| 272 | + KDL::Frame kdl_v1; |
| 273 | + tf2::convert(eigen_v, kdl_v1); |
| 274 | + EXPECT_EQ(kdl_v, kdl_v1); |
| 275 | +} |
| 276 | + |
| 277 | +TEST(TfEigenKdl, TestIsometry3dFrame) |
| 278 | +{ |
| 279 | + const Eigen::Isometry3d eigen_v( |
| 280 | + Eigen::Translation3d(1, 2, 3) * Eigen::AngleAxisd(1, Eigen::Vector3d::UnitX())); |
| 281 | + KDL::Frame kdl_v; |
| 282 | + tf2::convert(eigen_v, kdl_v); |
| 283 | + Eigen::Isometry3d eigen_v1; |
| 284 | + tf2::convert(kdl_v, eigen_v1); |
| 285 | + EXPECT_EQ(eigen_v.translation(), eigen_v1.translation()); |
| 286 | + EXPECT_EQ(eigen_v.rotation(), eigen_v1.rotation()); |
| 287 | +} |
| 288 | + |
| 289 | +TEST(TfEigenKdl, TestFrameAffine3d) |
| 290 | +{ |
| 291 | + const auto kdl_v = KDL::Frame(KDL::Rotation::RPY(1.2, 0.2, 0), KDL::Vector(1, 2, 3)); |
| 292 | + Eigen::Affine3d eigen_v = Eigen::Affine3d::Identity(); |
| 293 | + tf2::convert(kdl_v, eigen_v); |
| 294 | + KDL::Frame kdl_v1; |
| 295 | + tf2::convert(eigen_v, kdl_v1); |
| 296 | + EXPECT_EQ(kdl_v, kdl_v1); |
| 297 | +} |
| 298 | + |
| 299 | +TEST(TfEigenKdl, TestTwistMatrix) |
| 300 | +{ |
| 301 | + const auto kdl_v = KDL::Twist(KDL::Vector(1, 2, 3), KDL::Vector(4, 5, 6)); |
| 302 | + Vector6d eigen_v; |
| 303 | + tf2::convert(kdl_v, eigen_v); |
| 304 | + KDL::Twist kdl_v1; |
| 305 | + tf2::convert(eigen_v, kdl_v1); |
| 306 | + EXPECT_EQ(kdl_v, kdl_v1); |
| 307 | +} |
| 308 | + |
| 309 | +TEST(TfEigenKdl, TestMatrixWrench) |
| 310 | +{ |
| 311 | + Vector6d eigen_v; |
| 312 | + eigen_v << 1, 2, 3, 3, 2, 1; |
| 313 | + KDL::Wrench kdl_v; |
| 314 | + tf2::convert(eigen_v, kdl_v); |
| 315 | + std::array<tf2::Vector3, 2> tf2_v; |
| 316 | + tf2::convert(kdl_v, tf2_v); |
| 317 | + Vector6d eigen_v1; |
| 318 | + tf2::convert(tf2_v, eigen_v1); |
| 319 | + std::array<tf2::Vector3, 2> tf2_v1; |
| 320 | + tf2::convert(eigen_v1, tf2_v1); |
| 321 | + Vector6d eigen_v2; |
| 322 | + tf2::convert(tf2_v1, eigen_v2); |
| 323 | + EXPECT_EQ(eigen_v, eigen_v2); |
| 324 | +} |
| 325 | + |
| 326 | +TEST(TfEigenKdl, TestVectorVector3d) |
| 327 | +{ |
| 328 | + const auto kdl_v = KDL::Vector(1, 2, 3); |
| 329 | + Eigen::Vector3d eigen_v; |
| 330 | + tf2::convert(kdl_v, eigen_v); |
| 331 | + KDL::Vector kdl_v1; |
| 332 | + tf2::convert(eigen_v, kdl_v1); |
| 333 | + EXPECT_EQ(kdl_v, kdl_v1); |
| 334 | +} |
| 335 | + |
| 336 | +TEST(TfEigenKdl, TestVector3dVector) |
| 337 | +{ |
| 338 | + Eigen::Vector3d eigen_v; |
| 339 | + eigen_v << 1, 2, 3; |
| 340 | + KDL::Vector kdl_v; |
| 341 | + tf2::convert(eigen_v, kdl_v); |
| 342 | + Eigen::Vector3d eigen_v1; |
| 343 | + tf2::convert(kdl_v, eigen_v1); |
| 344 | + EXPECT_EQ(eigen_v, eigen_v1); |
| 345 | +} |
| 346 | + |
191 | 347 | int main(int argc, char ** argv) |
192 | 348 | { |
193 | 349 | testing::InitGoogleTest(&argc, argv); |
|
0 commit comments