marching_cubes.hpp
Go to the documentation of this file.
00001 /*
00002  * ransac.hpp
00003  *
00004  *  Created on: 14.01.2013
00005  *      Author: josh
00006  */
00007 
00008 #include <cob_3d_segmentation/eval.h>
00009 
00010 #ifndef PCL_VERSION_COMPARE
00011 #define KDTREE pcl::KdTreeFLANN
00012 #else
00013 #define KDTREE pcl::search::KdTree
00014 #endif
00015 
00016 
00017 template <typename Point, typename PointTypeNormal, typename PointLabel>
00018 bool Segmentation_MarchingCubes<Point, PointTypeNormal, PointLabel>::compute() {
00019   pcl::PolygonMesh mesh;
00020   // Normal estimation*
00021   pcl::NormalEstimation<Point, PointTypeNormal> norm_est;
00022   boost::shared_ptr<pcl::PointCloud<PointTypeNormal> > pointcloudNormal (new pcl::PointCloud<PointTypeNormal>);
00023   boost::shared_ptr<KDTREE<Point> > tree (new KDTREE<Point>);
00024   tree->setInputCloud (input_);
00025   norm_est.setInputCloud (input_);
00026   norm_est.setSearchMethod (tree);
00027   norm_est.setKSearch (30);
00028   norm_est.compute (*pointcloudNormal);
00029 
00030   // Concatenate the XYZ and normal fields*
00031   pcl::copyPointCloud (*input_, *pointcloudNormal);
00032 
00033   // Create the search method
00034   boost::shared_ptr<KDTREE<PointTypeNormal> > tree2 (new KDTREE<PointTypeNormal>);
00035   tree2->setInputCloud (pointcloudNormal);
00036 
00037   // Initialize objects
00038   pcl::MARCHING_CUBES_INST<PointTypeNormal> mc;
00039   // Set parameters
00040 #ifdef USE_GREEDY
00041   mc.setLeafSize(leafSize_);
00042 #else
00043   mc.setOffSurfaceDisplacement(leafSize_);
00044   mc.setGridResolution(1,1,1);
00045   mc.setPercentageExtendGrid(0.01f);
00046 #endif
00047   mc.setIsoLevel(isoLevel_);   //ISO: must be between 0 and 1.0
00048   mc.setSearchMethod(tree2);
00049   mc.setInputCloud(pointcloudNormal);
00050   // Reconstruct
00051   mc.reconstruct (mesh);
00052 
00053   mesh_ = mesh;
00054 
00055 
00056   std::cout<<mesh_.polygons.size()<<"\n";
00057 
00058   return true;
00059 }
00060 
00061 template <typename Point, typename PointTypeNormal, typename PointLabel>
00062 boost::shared_ptr<const pcl::PointCloud<PointLabel> > Segmentation_MarchingCubes<Point, PointTypeNormal, PointLabel>::getReconstructedOutputCloud() {
00063   boost::shared_ptr<pcl::PointCloud<PointLabel> > cloud (new pcl::PointCloud<PointLabel>);
00064   cloud->header = input_->header;
00065 
00066   pcl::PointCloud<pcl::PointXYZ> tmp;
00067   pcl::fromROSMsg(mesh_.cloud, tmp);
00068 
00069   pcl::copyPointCloud (tmp, *cloud);
00070 
00071   return cloud;
00072 }
00073 
00074 template <typename Point, typename PointTypeNormal, typename PointLabel>
00075 void Segmentation_MarchingCubes<Point, PointTypeNormal, PointLabel>::compute_accuracy(float &mean, float &var, size_t &used, size_t &mem, size_t &points, float &avg_dist) {
00076   RunningStat rstat;
00077   points = 0;
00078   avg_dist = 0;
00079   mem = 0;
00080 
00081   pcl::PointCloud<pcl::PointXYZ> tmp;
00082   pcl::fromROSMsg(mesh_.cloud, tmp);
00083 
00084   for(size_t j=0; j<input_->size(); j++) {
00085     Eigen::Vector3f s = (*input_)[j].getVector3fMap();
00086 
00087     float dmax = std::numeric_limits<float>::max();
00088     for(size_t i=0; i<mesh_.polygons.size(); i++) {
00089       Eigen::Vector3f a,b,c,d;
00090 
00091       a = tmp[mesh_.polygons[i].vertices[0]].getVector3fMap();
00092       b = tmp[mesh_.polygons[i].vertices[1]].getVector3fMap();
00093       c = tmp[mesh_.polygons[i].vertices[2]].getVector3fMap();
00094 
00095       float x = (s-a).dot(b-a)/(b-a).squaredNorm();
00096       float y = (s-a).dot(c-a)/(c-a).squaredNorm();
00097 
00098       if(x<0 || x>1 || y<0 || y>1) continue;
00099 
00100       d = (b-a).cross(c-a);
00101       float t = std::abs(d.dot(s-a))/d.norm();
00102 
00103       dmax = std::min(dmax,t);
00104 
00105       //(b-a) (c-a)
00106     }
00107 
00108     if(dmax<0.5) {
00109       mem+= 3*3*4;
00110       rstat.Push(dmax);
00111     }
00112 
00113     if(pcl_isfinite(s(2))) {
00114       avg_dist += s(2);
00115       ++points;
00116     }
00117 
00118     /*boost::shared_ptr<pcl::SampleConsensusModel<Point> >
00119       model;
00120     pcl::PointCloud<PointLabel> temp;
00121 
00122     if(shapes_[i].type_==SHAPE_S::PLANE)
00123       model.reset(new pcl::SampleConsensusModelPlane<Point> (input_));
00124     //else if(shapes_[i].type_==SHAPE_S::CYLINDER)
00125     //  model.reset(new pcl::SampleConsensusModelCylinder<Point,Point> (input_));
00126     else if(shapes_[i].type_==SHAPE_S::SPHERE)
00127       model.reset(new pcl::SampleConsensusModelSphere<Point> (input_));
00128     else
00129       ROS_ASSERT(0);
00130 
00131     mem+=shapes_[i].inliers_.size()*4;
00132     mem+=shapes_[i].coeff_.rows()*shapes_[i].coeff_.cols()*4+8;
00133 
00134     model->projectPoints(shapes_[i].inliers_, shapes_[i].coeff_, temp, false);
00135 
00136     for(size_t j=0; j<shapes_[i].inliers_.size(); j++) {
00137       Point pr = (*input_)[shapes_[i].inliers_[j]];
00138       Point pm = temp[j];
00139       const float z = pr.z;
00140       const float d = (pr.getVector3fMap()-pm.getVector3fMap()).norm();
00141 
00142       if(!pcl_isfinite(d)) continue;
00143 
00144       rstat.Push(d);
00145       avg_dist += z;
00146       ++points;
00147     }*/
00148   }
00149 
00150   //points = levels_[0].w*levels_[0].h;
00151   used = rstat.NumDataValues();
00152 
00153   mean = rstat.Mean();
00154   var = rstat.Variance();
00155   avg_dist /= points;
00156   used = tmp.size();
00157 }


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