test_pose.cpp
Go to the documentation of this file.
00001 /*
00002  * test_pose.cpp
00003  *
00004  *  Created on: Mar 15, 2012
00005  *      Author: Pablo IƱigo Blasco
00006  */
00007 
00008 #include <mrpt/poses/CPosePDFGaussian.h>
00009 #include <mrpt/poses/CPose3DPDFGaussian.h>
00010 #include <mrpt/math/CQuaternion.h>
00011 #include <geometry_msgs/PoseWithCovariance.h>
00012 #include <geometry_msgs/Pose.h>
00013 #include <geometry_msgs/Quaternion.h>
00014 #include <tf/tf.h>
00015 #include <mrpt_bridge/pose.h>
00016 #include <gtest/gtest.h>
00017 
00018 using namespace std;
00019 using namespace mrpt::utils;  // DEG2RAD()
00020 
00021 void checkPoseMatrixFromRotationParameters(
00022         const double roll, const double pitch, const double yaw)
00023 {
00024         // TF-BULLET ROTATION
00025         tf::Pose original_pose;
00026         tf::Quaternion rotation;
00027         rotation.setRPY(roll, pitch, yaw);
00028         original_pose.setRotation(rotation);
00029         tf::Matrix3x3 basis = original_pose.getBasis();
00030 
00031         // MRPT-ROTATION
00032         mrpt::poses::CPose3D mrpt_original_pose;
00033         mrpt_original_pose.setYawPitchRoll(yaw, pitch, roll);
00034         mrpt::math::CMatrixDouble33 mrpt_basis =
00035                 mrpt_original_pose.getRotationMatrix();
00036 
00037         for (int i = 0; i < 3; i++)
00038                 for (int j = 0; j < 3; j++)
00039                         EXPECT_NEAR(basis[i][j], mrpt_basis(i, j), 0.01);
00040 }
00041 
00042 TEST(PoseConversions, copyMatrix3x3ToCMatrixDouble33)
00043 {
00044         tf::Matrix3x3 src(0, 1, 2, 3, 4, 5, 6, 7, 8);
00045         mrpt::math::CMatrixDouble33 des;
00046         mrpt_bridge::convert(src, des);
00047         for (int r = 0; r < 3; r++)
00048                 for (int c = 0; c < 3; c++) EXPECT_FLOAT_EQ(des(r, c), src[r][c]);
00049 }
00050 TEST(PoseConversions, copyCMatrixDouble33ToMatrix3x3)
00051 {
00052         mrpt::math::CMatrixDouble33 src;
00053         src << 0, 1, 2, 3, 4, 5, 6, 7, 8;
00054         tf::Matrix3x3 des;
00055         mrpt_bridge::convert(src, des);
00056         for (int r = 0; r < 3; r++)
00057                 for (int c = 0; c < 3; c++) EXPECT_FLOAT_EQ(des[r][c], src(r, c));
00058 }
00059 
00060 TEST(PoseConversions, checkPoseMatrixFromRotationParameters)
00061 {
00062         checkPoseMatrixFromRotationParameters(0, 0, 0);
00063         checkPoseMatrixFromRotationParameters(0.2, 0, 0);
00064         checkPoseMatrixFromRotationParameters(0, 0.2, 0);
00065         checkPoseMatrixFromRotationParameters(0, 0, 0.2);
00066         checkPoseMatrixFromRotationParameters(0.4, 0.3, 0.2);
00067         checkPoseMatrixFromRotationParameters(M_PI, -M_PI / 2.0, 0);
00068 }
00069 
00070 // Declare a test
00071 TEST(PoseConversions, reference_frame_change_with_rotations)
00072 {
00073         geometry_msgs::PoseWithCovariance ros_msg_original_pose;
00074 
00075         ros_msg_original_pose.pose.position.x = 1;
00076         ros_msg_original_pose.pose.position.y = 0;
00077         ros_msg_original_pose.pose.position.z = 0;
00078         ros_msg_original_pose.pose.orientation.x = 0;
00079         ros_msg_original_pose.pose.orientation.y = 0;
00080         ros_msg_original_pose.pose.orientation.z = 0;
00081         ros_msg_original_pose.pose.orientation.w = 1;
00082 
00083         // to mrpt
00084         mrpt::poses::CPose3DPDFGaussian mrpt_original_pose;
00085         mrpt_bridge::convert(ros_msg_original_pose, mrpt_original_pose);
00086         EXPECT_EQ(
00087                 ros_msg_original_pose.pose.position.x, mrpt_original_pose.mean[0]);
00088 
00089         // to tf
00090         tf::Pose tf_original_pose;
00091         tf::poseMsgToTF(ros_msg_original_pose.pose, tf_original_pose);
00092 
00093         // rotate yaw pi in MRPT
00094         mrpt::poses::CPose3D rotation_mrpt;
00095         double yaw = M_PI / 2.0;
00096         rotation_mrpt.setFromValues(0, 0, 0, yaw, 0, 0);
00097         mrpt::poses::CPose3D mrpt_result = rotation_mrpt + mrpt_original_pose.mean;
00098         EXPECT_NEAR(mrpt_result[1], 1.0, 0.01);
00099 
00100         // rotate yaw pi in TF
00101         tf::Quaternion rotation_tf;
00102         rotation_tf.setRPY(0, 0, yaw);
00103         tf::Pose rotation_pose_tf;
00104         rotation_pose_tf.setIdentity();
00105         rotation_pose_tf.setRotation(rotation_tf);
00106         tf::Pose tf_result = rotation_pose_tf * tf_original_pose;
00107         EXPECT_NEAR(tf_result.getOrigin()[1], 1.0, 0.01);
00108 
00109         geometry_msgs::Pose mrpt_ros_result;
00110         mrpt_bridge::convert(mrpt_result, mrpt_ros_result);
00111 
00112         EXPECT_NEAR(mrpt_ros_result.position.x, tf_result.getOrigin()[0], 0.01);
00113         EXPECT_NEAR(mrpt_ros_result.position.y, tf_result.getOrigin()[1], 0.01);
00114         EXPECT_NEAR(mrpt_ros_result.position.z, tf_result.getOrigin()[2], 0.01);
00115 }
00116 
00117 void check_CPose3D_tofrom_ROS(
00118         double x, double y, double z, double yaw, double pitch, double roll)
00119 {
00120         const mrpt::poses::CPose3D p3D(x, y, z, yaw, pitch, roll);
00121 
00122         // Convert MRPT->ROS
00123         geometry_msgs::Pose ros_p3D;
00124         mrpt_bridge::convert(p3D, ros_p3D);
00125 
00126         // Compare ROS quat vs. MRPT quat:
00127         mrpt::math::CQuaternionDouble q;
00128         p3D.getAsQuaternion(q);
00129 
00130         EXPECT_NEAR(ros_p3D.position.x, p3D.x(), 1e-4) << "p: " << p3D << endl;
00131         EXPECT_NEAR(ros_p3D.position.y, p3D.y(), 1e-4) << "p: " << p3D << endl;
00132         EXPECT_NEAR(ros_p3D.position.z, p3D.z(), 1e-4) << "p: " << p3D << endl;
00133 
00134         EXPECT_NEAR(ros_p3D.orientation.x, q.x(), 1e-4) << "p: " << p3D << endl;
00135         EXPECT_NEAR(ros_p3D.orientation.y, q.y(), 1e-4) << "p: " << p3D << endl;
00136         EXPECT_NEAR(ros_p3D.orientation.z, q.z(), 1e-4) << "p: " << p3D << endl;
00137         EXPECT_NEAR(ros_p3D.orientation.w, q.r(), 1e-4) << "p: " << p3D << endl;
00138 
00139         // Test the other path: ROS->MRPT
00140         mrpt::poses::CPose3D p_bis;
00141         mrpt_bridge::convert(ros_p3D, p_bis);
00142 
00143         // p_bis==p3D?
00144         EXPECT_NEAR(
00145                 (p_bis.getAsVectorVal() - p3D.getAsVectorVal())
00146                         .array()
00147                         .abs()
00148                         .maxCoeff(),
00149                 0, 1e-4)
00150                 << "p_bis: " << p_bis << endl
00151                 << "p3D: " << p3D << endl;
00152 }
00153 
00154 // Declare a test
00155 TEST(PoseConversions, check_CPose3D_tofrom_ROS)
00156 {
00157         check_CPose3D_tofrom_ROS(0, 0, 0, DEG2RAD(0), DEG2RAD(0), DEG2RAD(0));
00158         check_CPose3D_tofrom_ROS(1, 2, 3, DEG2RAD(0), DEG2RAD(0), DEG2RAD(0));
00159 
00160         check_CPose3D_tofrom_ROS(1, 2, 3, DEG2RAD(30), DEG2RAD(0), DEG2RAD(0));
00161         check_CPose3D_tofrom_ROS(1, 2, 3, DEG2RAD(0), DEG2RAD(30), DEG2RAD(0));
00162         check_CPose3D_tofrom_ROS(1, 2, 3, DEG2RAD(0), DEG2RAD(0), DEG2RAD(30));
00163 
00164         check_CPose3D_tofrom_ROS(1, 2, 3, DEG2RAD(-5), DEG2RAD(15), DEG2RAD(-30));
00165 }
00166 
00167 // Declare a test
00168 TEST(PoseConversions, check_CPose2D_to_ROS)
00169 {
00170         const mrpt::poses::CPose2D p2D(1, 2, 0.56);
00171 
00172         // Convert MRPT->ROS
00173         geometry_msgs::Pose ros_p2D;
00174         mrpt_bridge::convert(p2D, ros_p2D);
00175 
00176         // Compare vs. 3D pose:
00177         const mrpt::poses::CPose3D p3D = mrpt::poses::CPose3D(p2D);
00178         mrpt::poses::CPose3D p3D_ros;
00179         mrpt_bridge::convert(ros_p2D, p3D_ros);
00180 
00181         // p3D_ros should equal p3D
00182         EXPECT_NEAR(
00183                 (p3D_ros.getAsVectorVal() - p3D.getAsVectorVal())
00184                         .array()
00185                         .abs()
00186                         .maxCoeff(),
00187                 0, 1e-4)
00188                 << "p3D_ros: " << p3D_ros << endl
00189                 << "p3D: " << p3D << endl;
00190 }


mrpt_bridge
Author(s): Markus Bader , Raphael Zack
autogenerated on Mon Sep 18 2017 03:12:06