ros_msg_conversions.h
Go to the documentation of this file.
00001 
00060 #ifndef COB_3D_MAPPING_ROS_MSG_CONV_H_
00061 #define COB_3D_MAPPING_ROS_MSG_CONV_H_
00062 
00063 #include <pcl/conversions.h>
00064 #include <pcl_conversions/pcl_conversions.h>
00065 #include <cob_3d_mapping_msgs/Shape.h>
00066 #include <cob_3d_mapping_msgs/ShapeArray.h>
00067 #include "cob_3d_mapping_common/polygon.h"
00068 #include "cob_3d_mapping_common/cylinder.h"
00069 #include <eigen_conversions/eigen_msg.h>
00070 
00071 namespace cob_3d_mapping
00072 {
00073   //TODO: purpose? where used? is this the correct place? => cob_table_object_cluster
00074   // should work for cob_3d_mapping_msgs::Shape, Polygon, Cylinder
00075   template<typename ShapeT>
00076   inline int getHullIndex(const ShapeT& s)
00077   {
00078     int idx = -1;
00079     for(size_t i=0; i<s.holes.size(); ++i) { if(!s.holes[i]) { idx=i; break; } }
00080     return idx;
00081   }
00082 
00083   template<typename PointT>
00084   inline bool shape2hull(const cob_3d_mapping_msgs::Shape& s, pcl::PointCloud<PointT>& hull)
00085   {
00086     int idx = getHullIndex(s);
00087     if (idx<0)
00088     {
00089       std::cerr <<  "[shape2hull()]: ERROR! No positive contour!" << std::endl;
00090       return false;
00091     }
00092     pcl::PCLPointCloud2 hull2;
00093     pcl_conversions::toPCL(s.points[idx], hull2);
00094     pcl::fromPCLPointCloud2(hull2, hull);
00095     //pcl::fromROSMsg(s.points[idx], hull);
00096     return true;
00097   }
00098 
00108   inline void
00109   toROSMsg(const Polygon& p, cob_3d_mapping_msgs::Shape& s)
00110   {
00111     s.id = p.id_;
00112     s.type = cob_3d_mapping_msgs::Shape::POLYGON;
00113     s.params.resize(4);
00114     s.params[0] = p.normal_(0);
00115     s.params[1] = p.normal_(1);
00116     s.params[2] = p.normal_(2);
00117     s.params[3] = p.d_;
00118     tf::poseEigenToMsg(p.pose_.cast<double>(), s.pose);
00119     //s.centroid.x = p.centroid(0);
00120     //s.centroid.y = p.centroid(1);
00121     //s.centroid.z = p.centroid(2);
00122     s.color.r = p.color_[0];
00123     s.color.g = p.color_[1];
00124     s.color.b = p.color_[2];
00125     s.color.a = p.color_[3];
00126     s.weight = p.merge_weight_;
00127 
00128     s.points.resize(p.contours_.size());
00129     s.holes.resize(p.holes_.size());
00130 
00131     for(unsigned int i=0; i < p.contours_.size(); i++)
00132     {
00133       s.holes[i] = p.holes_[i];
00134       //s.points[i].points.resize(p.contours[i].size());
00135       pcl::PointCloud<pcl::PointXYZ> cloud;
00136       for(unsigned int j=0; j<p.contours_[i].size(); j++)
00137       {
00138         pcl::PointXYZ pt;
00139         pt.x = p.contours_[i][j](0);
00140         pt.y = p.contours_[i][j](1);
00141         pt.z = 0;//p.contours[i][j](2);
00142         cloud.points.push_back(pt);
00143         /*s.points[i].points[j].x = p.contours[i][j](0);
00144         s.points[i].points[j].y = p.contours[i][j](1);
00145         s.points[i].points[j].z = p.contours[i][j](2);*/
00146       }
00147       pcl::PCLPointCloud2 cloud2;
00148       pcl::toPCLPointCloud2(cloud, cloud2);
00149       sensor_msgs::PointCloud2 cloud_msg;
00150       pcl_conversions::fromPCL(cloud2, cloud_msg);
00151       //pcl::toROSMsg(cloud, cloud_msg);
00152       s.points[i]= cloud_msg;
00153     }
00154   }
00155 
00165   inline bool
00166   fromROSMsg(const cob_3d_mapping_msgs::Shape& s, Polygon& p)
00167   {
00168     p.id_ = s.id;
00169     /*p.centroid(0) = s.centroid.x;
00170     p.centroid(1) = s.centroid.y;
00171     p.centroid(2) = s.centroid.z;*/
00172     Eigen::Affine3d pose;
00173     tf::poseMsgToEigen(s.pose, pose);
00174     p.pose_ = pose.cast<float>();
00175     p.normal_(0) = s.params[0];
00176     p.normal_(1) = s.params[1];
00177     p.normal_(2) = s.params[2];
00178     p.d_ = s.params[3];
00179     //p.merged_ = 1;
00180     p.color_[0] = s.color.r;
00181     p.color_[1] = s.color.g;
00182     p.color_[2] = s.color.b;
00183     p.color_[3] = s.color.a;
00184     p.merge_weight_ = s.weight;
00185     //p.contours.resize(p.polygons.size());
00186     for(unsigned int i=0; i<s.points.size(); i++)
00187     {
00188       p.holes_.push_back(s.holes[i]);
00189       if(s.points[i].data.size())
00190       {
00191         pcl::PCLPointCloud2 cloud2;
00192         pcl_conversions::toPCL(s.points[i], cloud2);
00193         pcl::PointCloud<pcl::PointXYZ> cloud;
00194         pcl::fromPCLPointCloud2(cloud2, cloud);
00195         //pcl::fromROSMsg(s.points[i], cloud);
00196         std::vector<Eigen::Vector2f> pts;
00197         pts.resize(cloud.points.size());
00198         for(unsigned int j=0; j<cloud.points.size(); j++)
00199         {
00200           pts[j](0) = cloud.points[j].x;
00201           pts[j](1) = cloud.points[j].y;
00202           //pts[j](2) = cloud.points[j].z;
00203         }
00204         p.contours_.push_back(pts);
00205         //pcl::getTransformationFromTwoUnitVectorsAndOrigin(
00206         //    p.normal_.unitOrthogonal(),p.normal_,p.centroid.head(3),p.transform_from_world_to_plane);
00207       }
00208       else
00209       {
00210         std::cout << "Polygon has no points" << std::endl;
00211         return false;
00212       }
00213     }
00214     return true;
00215   }
00216 
00226   inline void
00227   toROSMsg(const Cylinder& c, cob_3d_mapping_msgs::Shape& s)
00228   {
00229     s.params.resize(6);
00230     /*s.params[0]=c.normal_[0];
00231     s.params[1]=c.normal_[1];
00232     s.params[2]=c.normal_[2];*/
00233     s.params[0] = c.sym_axis_[0];
00234     s.params[1] = c.sym_axis_[1];
00235     s.params[2] = c.sym_axis_[2];
00236     /*s.params[6]=c.origin_[0];
00237     s.params[7]=c.origin_[1];
00238     s.params[8]=c.origin_[2];*/
00239     s.params[3] = c.r_;
00240     s.params[4] = c.h_min_;
00241     s.params[5] = c.h_max_;
00242     tf::poseEigenToMsg(c.pose_.cast<double>(), s.pose);
00243     s.id = c.id_;
00244     s.type = cob_3d_mapping_msgs::Shape::CYLINDER;
00245     s.weight = c.merge_weight_;
00246     s.color.r = c.color_[0];
00247     s.color.g = c.color_[1];
00248     s.color.b = c.color_[2];
00249     s.color.a = c.color_[3];
00250     s.points.resize(c.contours_.size());
00251     s.holes.resize(c.holes_.size());
00252 
00253     for(unsigned int i=0; i<c.contours_.size(); i++)
00254     {
00255       s.holes[i] = c.holes_[i];
00256       //s.points[i].points.resize(p.contours[i].size());
00257       pcl::PointCloud<pcl::PointXYZ> cloud;
00258       for(unsigned int j=0; j<c.contours_[i].size(); j++)
00259       {
00260         pcl::PointXYZ pt;
00261         pt.x = c.contours_[i][j](0);
00262         pt.y = c.contours_[i][j](1);
00263         pt.z = 0;
00264         cloud.points.push_back(pt);
00265         //std::cout << pt.x << "," << pt.y << std::endl;
00266       }
00267       pcl::PCLPointCloud2 cloud2;
00268       pcl::toPCLPointCloud2(cloud, cloud2);
00269       sensor_msgs::PointCloud2 cloud_msg;
00270       pcl_conversions::fromPCL(cloud2, cloud_msg);
00271       //pcl::toROSMsg(cloud, cloud_msg);
00272       s.points[i]= cloud_msg;
00273     }
00274   }
00275 
00285   inline bool
00286   fromROSMsg(const cob_3d_mapping_msgs::Shape& s,Cylinder& c)
00287   {
00288     c.id_ = s.id;
00289     /*c.normal_[0] = s.params[0];
00290     c.normal_[1] = s.params[1];
00291     c.normal_[2] = s.params[2];*/
00292     c.sym_axis_[0]= s.params[0];
00293     c.sym_axis_[1] = s.params[1];
00294     c.sym_axis_[2] = s.params[2];
00295     /*c.origin_[0] = s.params[6];
00296     c.origin_[1] = s.params[7];
00297     c.origin_[2] = s.params[8];*/
00298     c.r_ = s.params[3];
00299     c.h_min_ = s.params[4];
00300     c.h_max_ = s.params[5];
00301     Eigen::Affine3d pose;
00302     tf::poseMsgToEigen(s.pose, pose);
00303     c.pose_ = pose.cast<float>();
00304     //c.merged_ = 1;
00305     c.merge_weight_ = s.weight;
00306     c.color_[0] = s.color.r;
00307     c.color_[1] = s.color.g;
00308     c.color_[2] = s.color.b;
00309     c.color_[3] = s.color.a;
00310 
00311     for(unsigned int i=0; i<s.points.size(); i++)
00312     {
00313       c.holes_.push_back(s.holes[i]);
00314       if(s.points[i].data.size())
00315       {
00316         pcl::PCLPointCloud2 cloud2;
00317         pcl_conversions::toPCL(s.points[i], cloud2);
00318         pcl::PointCloud<pcl::PointXYZ> cloud;
00319         pcl::fromPCLPointCloud2(cloud2, cloud);
00320         //pcl::fromROSMsg(s.points[i], cloud);
00321         std::vector<Eigen::Vector2f> pts;
00322         pts.resize(cloud.points.size());
00323         for(unsigned int j=0; j<cloud.points.size(); j++)
00324         {
00325           pts[j](0) = cloud.points[j].x;
00326           pts[j](1) = cloud.points[j].y;
00327           //pts[j](2) = 0;
00328         }
00329         c.contours_.push_back(pts);
00330       }
00331       else
00332       {
00333         std::cout << "Cylinder has no points" << std::endl;
00334         return false;
00335       }
00336     }
00337     return true;
00338   }
00339 }
00340 
00341 #endif
00342 


cob_3d_mapping_common
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:02:19