4343#include < pcl/io/pcd_io.h>
4444#include < pcl/features/normal_3d.h>
4545#include < pcl/segmentation/extract_clusters.h>
46+ #define PCL_NO_PRECOMPILE // ConditionalEuclideanClustering is not always instantiated with PointNormal
47+ #include < pcl/segmentation/conditional_euclidean_clustering.h>
4648#include < pcl/point_types.h>
4749
4850int
@@ -68,8 +70,6 @@ main (int argc, char **argv)
6870 pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
6971 ne.setInputCloud (cloud_ptr);
7072
71- pcl::search::KdTree<pcl::PointXYZ>::Ptr tree_n (new pcl::search::KdTree<pcl::PointXYZ>());
72- ne.setSearchMethod (tree_n);
7373 ne.setRadiusSearch (0.03 );
7474 ne.compute (*cloud_normals);
7575 std::cout << " Estimated the normals" << std::endl;
@@ -106,6 +106,40 @@ main (int argc, char **argv)
106106 writer.write <pcl::PointXYZ> (ss.str (), *cloud_cluster, false );
107107 ++j;
108108 }
109+
110+ // Alternative: using ConditionalEuclideanClustering instead of extractEuclideanClusters
111+ auto cloud_with_normals = pcl::make_shared<pcl::PointCloud<pcl::PointNormal>>();
112+ pcl::concatenateFields (*cloud_ptr, *cloud_normals, *cloud_with_normals);
113+ pcl::ConditionalEuclideanClustering<pcl::PointNormal> cec;
114+ const double cos_eps_angle = std::cos (eps_angle);
115+ std::function<bool (const pcl::PointNormal&, const pcl::PointNormal&, float )> condition_function =
116+ [cos_eps_angle](const pcl::PointNormal& a, const pcl::PointNormal& b, float /* dist*/ )
117+ { return std::abs (a.getNormalVector3fMap ().dot (b.getNormalVector3fMap ())) > cos_eps_angle; };
118+ cec.setConditionFunction (condition_function);
119+ cec.setClusterTolerance (tolerance);
120+ cec.setMinClusterSize (min_cluster_size);
121+ cec.setInputCloud (cloud_with_normals);
122+ std::vector<pcl::PointIndices> cluster_indices2;
123+ cec.segment (cluster_indices2);
124+
125+ std::cout << " No of clusters formed are " << cluster_indices2.size () << std::endl;
126+ // Saving the clusters in separate pcd files
127+ j = 0 ;
128+ for (const auto & cluster : cluster_indices2)
129+ {
130+ pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_cluster (new pcl::PointCloud<pcl::PointXYZ>);
131+ for (const auto &index : cluster.indices ) {
132+ cloud_cluster->push_back ((*cloud_ptr)[index]);
133+ }
134+ cloud_cluster->width = cloud_cluster->size ();
135+ cloud_cluster->height = 1 ;
136+ cloud_cluster->is_dense = true ;
109137
138+ std::cout << " PointCloud representing the Cluster using xyzn: " << cloud_cluster->size () << " data " << std::endl;
139+ std::stringstream ss;
140+ ss << " ./cloud_cluster2_" << j << " .pcd" ;
141+ writer.write <pcl::PointXYZ> (ss.str (), *cloud_cluster, false );
142+ ++j;
143+ }
110144 return (0 );
111145}
0 commit comments