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


mrpt_bridge
Author(s): Markus Bader , Raphael Zack
autogenerated on Fri Apr 27 2018 05:10:54