39 #include <pcl/segmentation/conditional_euclidean_clustering.h>
40 #include <pcl/sample_consensus/method_types.h>
41 #include <pcl/sample_consensus/model_types.h>
42 #include <pcl/segmentation/sac_segmentation.h>
49 DiagnosticNodelet::onInit();
51 srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
52 dynamic_reconfigure::Server<Config>::CallbackType
f =
55 srv_->setCallback (
f);
57 pub_polygons_ = advertise<jsk_recognition_msgs::PolygonArray>(
58 *pnh_,
"output/polygons", 1);
59 pub_inliers_ = advertise<jsk_recognition_msgs::ClusterPointIndices>(
60 *pnh_,
"output/inliers", 1);
62 *pnh_,
"output/coefficients", 1);
64 *pnh_,
"output/clustering_result", 1);
66 *pnh_,
"output/latest_time", 1);
68 *pnh_,
"output/average_time", 1);
92 sync_->registerCallback(
104 Config &config, uint32_t level)
116 =
config.ransac_refine_outlier_distance_threshold;
118 =
config.ransac_refine_max_iterations;
122 const pcl::PointCloud<pcl::PointXYZRGB>::Ptr& cloud,
123 const pcl::PointIndices::Ptr& indices,
124 pcl::PointIndices& inliers,
125 pcl::ModelCoefficients& coefficient)
127 pcl::SACSegmentation<pcl::PointXYZRGB> seg;
128 seg.setOptimizeCoefficients (
true);
129 seg.setMethodType (pcl::SAC_RANSAC);
131 seg.setInputCloud(cloud);
132 seg.setIndices(indices);
134 seg.setModelType (pcl::SACMODEL_PLANE);
135 seg.segment(inliers, coefficient);
139 const sensor_msgs::PointCloud2::ConstPtr&
msg,
140 const sensor_msgs::PointCloud2::ConstPtr& normal_msg)
146 vital_checker_->poke();
150 pcl::PointCloud<PointT>::Ptr
cloud(
new pcl::PointCloud<PointT>);
151 pcl::PointCloud<pcl::Normal>::Ptr normal(
new pcl::PointCloud<pcl::Normal>);
155 pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr
156 all_cloud (
new pcl::PointCloud<pcl::PointXYZRGBNormal>);
158 pcl::PointIndices::Ptr indices (
new pcl::PointIndices);
159 for (
size_t i = 0;
i < all_cloud->points.size();
i++) {
160 if (!std::isnan(all_cloud->points[i].x) &&
161 !std::isnan(all_cloud->points[i].y) &&
162 !std::isnan(all_cloud->points[i].z) &&
163 !std::isnan(all_cloud->points[i].normal_x) &&
164 !std::isnan(all_cloud->points[i].normal_y) &&
165 !std::isnan(all_cloud->points[i].normal_z) &&
166 !std::isnan(all_cloud->points[i].curvature)) {
168 indices->indices.push_back(i);
172 pcl::ConditionalEuclideanClustering<pcl::PointXYZRGBNormal> cec (
true);
175 pcl::IndicesClustersPtr clusters (
new pcl::IndicesClusters);
176 cec.setInputCloud (all_cloud);
177 cec.setIndices(indices);
184 cec.setConditionFunction(
187 cec.segment (*clusters);
193 jsk_recognition_msgs::ClusterPointIndices ros_clustering_result;
194 ros_clustering_result.cluster_indices
196 ros_clustering_result.header =
msg->header;
200 std::vector<pcl::PointIndices::Ptr> all_inliers;
201 std::vector<pcl::ModelCoefficients::Ptr> all_coefficients;
202 jsk_recognition_msgs::PolygonArray ros_polygon;
203 ros_polygon.header =
msg->header;
204 for (
size_t i = 0;
i < clusters->size();
i++) {
205 pcl::PointIndices::Ptr plane_inliers(
new pcl::PointIndices);
206 pcl::ModelCoefficients::Ptr plane_coefficients(
new pcl::ModelCoefficients);
207 #if ( PCL_MAJOR_VERSION >= 1 && PCL_MINOR_VERSION >= 12 )
208 pcl::PointIndices::Ptr cluster = std::make_shared<pcl::PointIndices>((*clusters)[i]);
210 pcl::PointIndices::Ptr cluster = boost::make_shared<pcl::PointIndices>((*clusters)[i]);
213 *plane_inliers, *plane_coefficients);
214 if (plane_inliers->indices.size() > 0) {
216 = jsk_recognition_utils::convexFromCoefficientsAndInliers<pcl::PointXYZRGB>(
217 cloud, plane_inliers, plane_coefficients);
224 Eigen::Vector3f coefficient_normal(plane_coefficients->values[0],
225 plane_coefficients->values[1],
226 plane_coefficients->values[2]);
227 if (convex->getNormalFromVertices().dot(coefficient_normal) < 0) {
228 convex = boost::make_shared<jsk_recognition_utils::ConvexPolygon>(convex->flipConvex());
233 Eigen::Vector3f
p = convex->getPointOnPlane();
234 Eigen::Vector3f
n = convex->getNormal();
236 convex = boost::make_shared<jsk_recognition_utils::ConvexPolygon>(convex->flipConvex());
237 Eigen::Vector3f new_normal = convex->getNormal();
238 plane_coefficients->values[0] = new_normal[0];
239 plane_coefficients->values[1] = new_normal[1];
240 plane_coefficients->values[2] = new_normal[2];
241 plane_coefficients->values[3] = convex->getD();
245 geometry_msgs::PolygonStamped
polygon;
246 polygon.polygon = convex->toROSMsg();
248 ros_polygon.polygons.push_back(
polygon);
249 all_inliers.push_back(plane_inliers);
250 all_coefficients.push_back(plane_coefficients);
255 jsk_recognition_msgs::ClusterPointIndices ros_indices;
256 ros_indices.cluster_indices =
258 ros_indices.header =
msg->header;
260 jsk_recognition_msgs::ModelCoefficientsArray ros_coefficients;
261 ros_coefficients.header =
msg->header;
262 ros_coefficients.coefficients =
264 all_coefficients,
msg->header);