cluster_ros_conversion.h
Go to the documentation of this file.
00001 
00063 #ifndef CLUSTER_ROS_CONVERSION_H__
00064 #define CLUSTER_ROS_CONVERSION_H__
00065 
00066 #include <cob_3d_segmentation/cluster_conversion.h>
00067 #include <cob_3d_mapping_msgs/ShapeArray.h>
00068 
00069 namespace cob_3d_segmentation
00070 {
00071   template<
00072     typename PointT = pcl::PointXYZRGB,
00073     typename ClusterHdlT = DepthClusterHandler<PointLabel, PointT, pcl::Normal>
00074     >
00075   class ClusterROSConversion : public ClusterConversion<PointT, ClusterHdlT>
00076   {
00077   public:
00078     typedef ClusterConversion<PointT, ClusterHdlT> Base;
00079 
00080     using Base::min_size_;
00081     using Base::max_dist_;
00082 
00083     using Base::pe_;
00084     using Base::ch_;
00085     using Base::p_;
00086 
00087     bool clusterToShapeMsg(const typename Base::ClusterPtr& c, cob_3d_mapping_msgs::Shape& s)
00088     {
00089       PolygonContours<PolygonPoint> poly;
00090       pe_.outline(p_->width, p_->height, c->border_points, poly);
00091       if(!poly.polys_.size()) false; // continue, if no contours were found
00092       int max_idx=0, max_size=0;
00093       for (int i = 0; i < (int)poly.polys_.size(); ++i)
00094       {
00095         if ((int)poly.polys_[i].size() > max_size) { max_idx = i; max_size = poly.polys_[i].size(); }
00096       }
00097 
00098       Eigen::Vector3f centroid = c->getCentroid();
00099 
00100       s.id = 0;
00101       s.points.resize(poly.polys_.size());
00102       s.header.frame_id = p_->header.frame_id.c_str();
00103 
00104       typename Base::PointCloud::Ptr hull(new typename Base::PointCloud);
00105       for (int i = 0; i < (int)poly.polys_.size(); ++i)
00106       {
00107         if (i == max_idx)
00108         {
00109           s.holes.push_back(false);
00110           std::vector<PolygonPoint>::iterator it = poly.polys_[i].begin();
00111           for ( ; it != poly.polys_[i].end(); ++it) {
00112             hull->push_back( p_->points[ it->x + it->y * p_->width ] );
00113           }
00114         }
00115         else
00116         {
00117           s.holes.push_back(true);
00118           std::vector<PolygonPoint>::reverse_iterator it = poly.polys_[i].rbegin();
00119           for ( ; it != poly.polys_[i].rend(); ++it) {
00120             hull->push_back( p_->points[ it->x + it->y * p_->width ] );
00121           }
00122         }
00123         hull->height = 1;
00124         hull->width = hull->size();
00125         pcl::toROSMsg(*hull, s.points[i]);
00126         hull->clear();
00127       }
00128 
00129       s.centroid.x = centroid[0];
00130       s.centroid.y = centroid[1];
00131       s.centroid.z = centroid[2];
00132       s.type = cob_3d_mapping_msgs::Shape::POLYGON;
00133       s.params.resize(4);
00134       s.params[0] = c->pca_point_comp3(0);
00135       s.params[1] = c->pca_point_comp3(1);
00136       s.params[2] = c->pca_point_comp3(2);
00137       s.params[3] = fabs(centroid.dot(c->pca_point_comp3)); // d
00138       return true;
00139     }
00140 
00141     void convertToShapeArray(cob_3d_mapping_msgs::ShapeArray& sa)
00142     {
00143       sa.header = p_->header;
00144       sa.header.frame_id = p_->header.frame_id.c_str();
00145 
00146       for(typename Base::ClusterPtr c = ch_->begin(); c != ch_->end(); ++c)
00147       {
00148         if(c->size() < min_size_) continue;
00149         if(c->getCentroid()(2) > max_dist_) continue;
00150         if(c->size() <= ceil(1.1f * (float)c->border_points.size())) continue;
00151 
00152         ch_->computeClusterComponents(c);
00153         if(!c->is_save_plane) continue;
00154 
00155         sa.shapes.push_back(cob_3d_mapping_msgs::Shape());
00156         if(!clusterToShapeMsg(c, sa.shapes.back())) {
00157           sa.shapes.pop_back();
00158         }
00159       }
00160     }
00161   };
00162 }
00163 
00164 #endif


cob_3d_segmentation
Author(s): Georg Arbeiter
autogenerated on Wed Aug 26 2015 11:03:03