pose.cpp
Go to the documentation of this file.
00001 /*
00002  * pose.cpp
00003  *
00004  *  Created on: Mar 14, 2012
00005  *      Author: Pablo IƱigo Blasco
00006  *
00007  *      To understand better how this is implemented see the references:
00008  *      - http://www.mrpt.org/2D_3D_Geometry
00009  *
00010  */
00011 
00022 #include <mrpt/poses/CPose2D.h>
00023 #include <mrpt/poses/CPose3D.h>
00024 #include <mrpt/poses/CPosePDFGaussian.h>
00025 #include <mrpt/poses/CPose3DPDFGaussian.h>
00026 #include <mrpt/poses/CPosePDFGaussianInf.h>
00027 #include <mrpt/poses/CPose3DPDFGaussianInf.h>
00028 #include <mrpt/math/CQuaternion.h>
00029 #include <mrpt/math/lightweight_geom_data.h>
00030 #include <mrpt/utils/mrpt_macros.h>
00031 #include <geometry_msgs/PoseWithCovariance.h>
00032 #include <geometry_msgs/Pose.h>
00033 #include <geometry_msgs/Quaternion.h>
00034 #include <mrpt/math/CMatrixFixedNumeric.h>
00035 #include <tf/tf.h>
00036 #include "mrpt_bridge/pose.h"
00037 
00038 mrpt::math::CMatrixDouble33& mrpt_bridge::convert(
00039         const tf::Matrix3x3& _src, mrpt::math::CMatrixDouble33& _des)
00040 {
00041         for (int r = 0; r < 3; r++)
00042                 for (int c = 0; c < 3; c++) _des(r, c) = _src[r][c];
00043         return _des;
00044 }
00045 
00046 tf::Matrix3x3& mrpt_bridge::convert(
00047         const mrpt::math::CMatrixDouble33& _src, tf::Matrix3x3& _des)
00048 {
00049         for (int r = 0; r < 3; r++)
00050                 for (int c = 0; c < 3; c++) _des[r][c] = _src(r, c);
00051         return _des;
00052 }
00053 
00054 tf::Transform& mrpt_bridge::convert(
00055         const mrpt::poses::CPose3DPDFGaussian& _src, tf::Transform& _des)
00056 {
00057         return convert(_src.mean, _des);
00058 }
00059 
00060 geometry_msgs::PoseWithCovariance& mrpt_bridge::convert(
00061         const mrpt::poses::CPose3DPDFGaussian& _src,
00062         geometry_msgs::PoseWithCovariance& _des)
00063 {
00064         convert(_src.mean, _des.pose);
00065 
00066         // Read REP103: http://ros.org/reps/rep-0103.html#covariance-representation
00067         // # Row-major representation of the 6x6 covariance matrix
00068         // # The orientation parameters use a fixed-axis representation.
00069         // # In order, the parameters are:
00070         // # (x, y, z, rotation about X axis, rotation about Y axis, rotation about
00071         // Z axis)
00072         // float64[36] covariance
00073         // Old comment: "MRPT uses non-fixed axis for 6x6 covariance: should use a
00074         // transform Jacobian here!"
00075         //           JL ==> Nope! non-fixed z-y-x equals fixed x-y-z rotations.
00076 
00077         const unsigned int indxs_map[6] = {0, 1, 2,
00078                                                                            5, 4, 3};  // X,Y,Z,YAW,PITCH,ROLL
00079 
00080         for (int i = 0; i < 6; i++)
00081         {
00082                 for (int j = 0; j < 6; j++)
00083                 {
00084                         _des.covariance[indxs_map[i] * 6 + indxs_map[j]] = _src.cov(i, j);
00085                 }
00086         }
00087         return _des;
00088 }
00089 
00090 geometry_msgs::PoseWithCovariance& mrpt_bridge::convert(
00091         const mrpt::poses::CPose3DPDFGaussianInf& _src,
00092         geometry_msgs::PoseWithCovariance& _des)
00093 {
00094         mrpt::poses::CPose3DPDFGaussian mrpt_gaussian;
00095         mrpt_gaussian.copyFrom(_src);
00096         convert(mrpt_gaussian, _des);
00097 
00098         return _des;
00099 }
00100 
00101 tf::Transform& mrpt_bridge::convert(
00102         const mrpt::poses::CPose3D& _src, tf::Transform& _des)
00103 {
00104         tf::Vector3 origin(_src[0], _src[1], _src[2]);
00105         mrpt::math::CMatrixDouble33 R;
00106         _src.getRotationMatrix(R);
00107         tf::Matrix3x3 basis;
00108         _des.setBasis(convert(R, basis));
00109         _des.setOrigin(origin);
00110 
00111         return _des;
00112 }
00113 mrpt::poses::CPose3D& mrpt_bridge::convert(
00114         const tf::Transform& _src, mrpt::poses::CPose3D& _des)
00115 {
00116         const tf::Vector3& t = _src.getOrigin();
00117         _des.x() = t[0], _des.y() = t[1], _des.z() = t[2];
00118         const tf::Matrix3x3& basis = _src.getBasis();
00119         mrpt::math::CMatrixDouble33 R;
00120         convert(basis, R);
00121         _des.setRotationMatrix(R);
00122 
00123         return _des;
00124 }
00125 
00126 geometry_msgs::Pose& mrpt_bridge::convert(
00127         const mrpt::poses::CPose3D& _src, geometry_msgs::Pose& _des)
00128 {
00129         _des.position.x = _src[0];
00130         _des.position.y = _src[1];
00131         _des.position.z = _src[2];
00132 
00133         mrpt::math::CQuaternionDouble q;
00134         _src.getAsQuaternion(q);
00135 
00136         _des.orientation.x = q.x();
00137         _des.orientation.y = q.y();
00138         _des.orientation.z = q.z();
00139         _des.orientation.w = q.r();
00140 
00141         return _des;
00142 }
00143 
00145 geometry_msgs::Pose& mrpt_bridge::convert(
00146         const mrpt::poses::CPose2D& _src, geometry_msgs::Pose& _des)
00147 {
00148         _des.position.x = _src.x();
00149         _des.position.y = _src.y();
00150         _des.position.z = 0;
00151 
00152         const double yaw = _src.phi();
00153         if (std::abs(yaw) < 1e-10)
00154         {
00155                 _des.orientation.x = 0.;
00156                 _des.orientation.y = 0.;
00157                 _des.orientation.z = .5 * yaw;
00158                 _des.orientation.w = 1.;
00159         }
00160         else
00161         {
00162                 const double s = ::sin(yaw * .5);
00163                 const double c = ::cos(yaw * .5);
00164                 _des.orientation.x = 0.;
00165                 _des.orientation.y = 0.;
00166                 _des.orientation.z = s;
00167                 _des.orientation.w = c;
00168         }
00169 
00170         return _des;
00171 }
00172 
00173 mrpt::poses::CPose2D& mrpt_bridge::convert(
00174         const geometry_msgs::Pose& _src, mrpt::poses::CPose2D& _des)
00175 {
00176         _des.x(_src.position.x);
00177         _des.y(_src.position.y);
00178 
00179         mrpt::math::CQuaternionDouble quat;
00180         convert(_src.orientation, quat);
00181 
00182         double roll, pitch, yaw;
00183         quat.rpy(roll, pitch, yaw);
00184 
00185         _des.phi(yaw);
00186 
00187         return _des;
00188 }
00189 
00190 geometry_msgs::PoseWithCovariance& mrpt_bridge::convert(
00191         const mrpt::poses::CPosePDFGaussian& _src,
00192         geometry_msgs::PoseWithCovariance& _des)
00193 {
00194         convert(_src.mean, _des.pose);
00195 
00196         // Read REP103: http://ros.org/reps/rep-0103.html#covariance-representation
00197         // Old comment: "MRPT uses non-fixed axis for 6x6 covariance: should use a
00198         // transform Jacobian here!"
00199         //           JL ==> Nope! non-fixed z-y-x equals fixed x-y-z rotations.
00200 
00201         // geometry_msgs/PoseWithCovariance msg stores the covariance matrix in
00202         // row-major representation
00203         // Indexes are :
00204         // [ 0   1   2   3   4   5  ]
00205         // [ 6   7   8   9   10  11 ]
00206         // [ 12  13  14  15  16  17 ]
00207         // [ 18  19  20  21  22  23 ]
00208         // [ 24  25  26  27  28  29 ]
00209         // [ 30  31  32  33  34  35 ]
00210 
00211         _des.covariance[0] = _src.cov(0, 0);
00212         _des.covariance[1] = _src.cov(0, 1);
00213         _des.covariance[5] = _src.cov(0, 2);
00214         _des.covariance[6] = _src.cov(1, 0);
00215         _des.covariance[7] = _src.cov(1, 1);
00216         _des.covariance[11] = _src.cov(1, 2);
00217         _des.covariance[30] = _src.cov(2, 0);
00218         _des.covariance[31] = _src.cov(2, 1);
00219         _des.covariance[35] = _src.cov(2, 2);
00220 
00221         return _des;
00222 }
00223 
00224 geometry_msgs::PoseWithCovariance& mrpt_bridge::convert(
00225         const mrpt::poses::CPosePDFGaussianInf& _src,
00226         geometry_msgs::PoseWithCovariance& _des)
00227 {
00228         mrpt::poses::CPosePDFGaussian mrpt_gaussian;
00229         mrpt_gaussian.copyFrom(_src);
00230 
00231         convert(mrpt_gaussian, _des);
00232         return _des;
00233 }
00234 
00235 mrpt::poses::CPose3DPDFGaussian& mrpt_bridge::convert(
00236         const geometry_msgs::PoseWithCovariance& _src,
00237         mrpt::poses::CPose3DPDFGaussian& _des)
00238 {
00239         convert(_src.pose, _des.mean);
00240 
00241         const unsigned int indxs_map[6] = {0, 1, 2, 5, 4, 3};
00242 
00243         for (int i = 0; i < 6; i++)
00244         {
00245                 for (int j = 0; j < 6; j++)
00246                 {
00247                         _des.cov(i, j) = _src.covariance[indxs_map[i] * 6 + indxs_map[j]];
00248                 }
00249         }
00250 
00251         return _des;
00252 }
00253 
00254 mrpt::poses::CPose3DPDFGaussianInf& mrpt_bridge::convert(
00255         const geometry_msgs::PoseWithCovariance& _src,
00256         mrpt::poses::CPose3DPDFGaussianInf& _des)
00257 {
00258         mrpt::poses::CPose3DPDFGaussian mrpt_gaussian;
00259         convert(
00260                 _src, mrpt_gaussian);  // Intermediate transform => CPose3DPDFGaussian
00261         _des.copyFrom(mrpt_gaussian);
00262 
00263         return _des;
00264 }
00265 
00266 mrpt::poses::CPosePDFGaussian& mrpt_bridge::convert(
00267         const geometry_msgs::PoseWithCovariance& _src,
00268         mrpt::poses::CPosePDFGaussian& _des)
00269 {
00270         convert(_src.pose, _des.mean);
00271 
00272         _des.cov(0, 0) = _src.covariance[0];
00273         _des.cov(0, 1) = _src.covariance[1];
00274         _des.cov(0, 2) = _src.covariance[5];
00275         _des.cov(1, 0) = _src.covariance[0 + 6];
00276         _des.cov(1, 1) = _src.covariance[1 + 6];
00277         _des.cov(1, 2) = _src.covariance[5 + 6];
00278         _des.cov(2, 0) = _src.covariance[0 + 30];
00279         _des.cov(2, 1) = _src.covariance[1 + 30];
00280         _des.cov(2, 2) = _src.covariance[5 + 30];
00281 
00282         return _des;
00283 }
00284 
00285 mrpt::poses::CPosePDFGaussianInf& mrpt_bridge::convert(
00286         const geometry_msgs::PoseWithCovariance& _src,
00287         mrpt::poses::CPosePDFGaussianInf& _des)
00288 {
00289         mrpt::poses::CPosePDFGaussian mrpt_gaussian;
00290         convert(_src, mrpt_gaussian);  // intermediate transform: PoseWithCovariance
00291         // => CPosePDFGaussian
00292         _des.copyFrom(mrpt_gaussian);
00293 
00294         return _des;
00295 }
00296 
00297 mrpt::poses::CQuaternionDouble& mrpt_bridge::convert(
00298         const geometry_msgs::Quaternion& _src, mrpt::poses::CQuaternionDouble& _des)
00299 {
00300         _des.x(_src.x);
00301         _des.y(_src.y);
00302         _des.z(_src.z);
00303         _des.r(_src.w);
00304         return _des;
00305 }
00306 
00307 geometry_msgs::Quaternion& mrpt_bridge::convert(
00308         const mrpt::poses::CQuaternionDouble& _src, geometry_msgs::Quaternion& _des)
00309 {
00310         _des.x = _src.x();
00311         _des.y = _src.y();
00312         _des.z = _src.z();
00313         _des.w = _src.r();
00314 
00315         return _des;
00316 }
00317 
00318 mrpt::poses::CPose3D& mrpt_bridge::convert(
00319         const geometry_msgs::Pose& _src, mrpt::poses::CPose3D& _des)
00320 {
00321         const mrpt::math::CQuaternionDouble q(
00322                 _src.orientation.w, _src.orientation.x, _src.orientation.y,
00323                 _src.orientation.z);
00324         _des = mrpt::poses::CPose3D(
00325                 q, _src.position.x, _src.position.y, _src.position.z);
00326 
00327         return _des;
00328 }
00329 
00330 tf::Transform& mrpt_bridge::convert(
00331         const mrpt::math::TPose3D& _src, tf::Transform& _des)
00332 {
00333         return mrpt_bridge::convert(mrpt::poses::CPose3D(_src), _des);
00334 }
00335 
00336 tf::Transform& mrpt_bridge::convert(
00337         const mrpt::poses::CPose2D& _src, tf::Transform& _des)
00338 {
00339         return mrpt_bridge::convert(mrpt::poses::CPose3D(_src), _des);
00340 }
00341 tf::Transform& mrpt_bridge::convert(
00342         const mrpt::math::TPose2D& _src, tf::Transform& _des)
00343 {
00344         return mrpt_bridge::convert(
00345                 mrpt::poses::CPose3D(mrpt::math::TPose3D(_src)), _des);
00346 }


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