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);
81 sync_->registerCallback(
93 Config &config, uint32_t level)
105 = config.ransac_refine_outlier_distance_threshold;
107 = config.ransac_refine_max_iterations;
111 const pcl::PointCloud<pcl::PointXYZRGB>::Ptr& cloud,
112 const pcl::PointIndices::Ptr& indices,
113 pcl::PointIndices& inliers,
114 pcl::ModelCoefficients& coefficient)
116 pcl::SACSegmentation<pcl::PointXYZRGB> seg;
117 seg.setOptimizeCoefficients (
true);
118 seg.setMethodType (pcl::SAC_RANSAC);
120 seg.setInputCloud(cloud);
121 seg.setIndices(indices);
123 seg.setModelType (pcl::SACMODEL_PLANE);
124 seg.segment(inliers, coefficient);
128 const sensor_msgs::PointCloud2::ConstPtr&
msg,
129 const sensor_msgs::PointCloud2::ConstPtr& normal_msg)
139 pcl::PointCloud<PointT>::Ptr
cloud(
new pcl::PointCloud<PointT>);
140 pcl::PointCloud<pcl::Normal>::Ptr normal(
new pcl::PointCloud<pcl::Normal>);
144 pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr
145 all_cloud (
new pcl::PointCloud<pcl::PointXYZRGBNormal>);
147 pcl::PointIndices::Ptr indices (
new pcl::PointIndices);
148 for (
size_t i = 0; i < all_cloud->points.size(); i++) {
149 if (!std::isnan(all_cloud->points[i].x) &&
150 !std::isnan(all_cloud->points[i].y) &&
151 !std::isnan(all_cloud->points[i].z) &&
152 !std::isnan(all_cloud->points[i].normal_x) &&
153 !std::isnan(all_cloud->points[i].normal_y) &&
154 !std::isnan(all_cloud->points[i].normal_z) &&
155 !std::isnan(all_cloud->points[i].curvature)) {
157 indices->indices.push_back(i);
161 pcl::ConditionalEuclideanClustering<pcl::PointXYZRGBNormal> cec (
true);
164 pcl::IndicesClustersPtr clusters (
new pcl::IndicesClusters);
165 cec.setInputCloud (all_cloud);
166 cec.setIndices(indices);
173 cec.setConditionFunction(
176 cec.segment (*clusters);
182 jsk_recognition_msgs::ClusterPointIndices ros_clustering_result;
183 ros_clustering_result.cluster_indices
185 ros_clustering_result.header = msg->header;
189 std::vector<pcl::PointIndices::Ptr> all_inliers;
190 std::vector<pcl::ModelCoefficients::Ptr> all_coefficients;
191 jsk_recognition_msgs::PolygonArray ros_polygon;
192 ros_polygon.header = msg->header;
193 for (
size_t i = 0; i < clusters->size(); i++) {
194 pcl::PointIndices::Ptr plane_inliers(
new pcl::PointIndices);
195 pcl::ModelCoefficients::Ptr plane_coefficients(
new pcl::ModelCoefficients);
196 pcl::PointIndices::Ptr cluster = boost::make_shared<pcl::PointIndices>((*clusters)[i]);
198 *plane_inliers, *plane_coefficients);
199 if (plane_inliers->indices.size() > 0) {
201 = jsk_recognition_utils::convexFromCoefficientsAndInliers<pcl::PointXYZRGB>(
202 cloud, plane_inliers, plane_coefficients);
209 Eigen::Vector3f coefficient_normal(plane_coefficients->values[0],
210 plane_coefficients->values[1],
211 plane_coefficients->values[2]);
212 if (convex->getNormalFromVertices().dot(coefficient_normal) < 0) {
213 convex = boost::make_shared<jsk_recognition_utils::ConvexPolygon>(convex->flipConvex());
218 Eigen::Vector3f
p = convex->getPointOnPlane();
219 Eigen::Vector3f
n = convex->getNormal();
221 convex = boost::make_shared<jsk_recognition_utils::ConvexPolygon>(convex->flipConvex());
222 Eigen::Vector3f new_normal = convex->getNormal();
223 plane_coefficients->values[0] = new_normal[0];
224 plane_coefficients->values[1] = new_normal[1];
225 plane_coefficients->values[2] = new_normal[2];
226 plane_coefficients->values[3] = convex->getD();
230 geometry_msgs::PolygonStamped
polygon;
231 polygon.polygon = convex->toROSMsg();
232 polygon.header = msg->header;
233 ros_polygon.polygons.push_back(polygon);
234 all_inliers.push_back(plane_inliers);
235 all_coefficients.push_back(plane_coefficients);
240 jsk_recognition_msgs::ClusterPointIndices ros_indices;
241 ros_indices.cluster_indices =
243 ros_indices.header = msg->header;
245 jsk_recognition_msgs::ModelCoefficientsArray ros_coefficients;
246 ros_coefficients.header = msg->header;
247 ros_coefficients.coefficients =
249 all_coefficients, msg->header);
ros::Publisher pub_inliers_
boost::shared_ptr< message_filters::Synchronizer< NormalSyncPolicy > > sync_
message_filters::Subscriber< sensor_msgs::PointCloud2 > sub_normal_
void publish(const boost::shared_ptr< M > &message) const
ros::Publisher pub_coefficients_
std::vector< PCLModelCoefficientMsg > convertToROSModelCoefficients(const std::vector< pcl::ModelCoefficients::Ptr > &coefficients, const std_msgs::Header &header)
virtual void segment(const sensor_msgs::PointCloud2::ConstPtr &msg, const sensor_msgs::PointCloud2::ConstPtr &normal_msg)
virtual ScopedWallDurationReporter reporter()
void fromROSMsg(const sensor_msgs::PointCloud2 &cloud, pcl::PointCloud< T > &pcl_cloud)
jsk_recognition_utils::WallDurationTimer timer_
static boost::mutex global_custom_condigion_function_mutex
double ransac_refine_outlier_distance_threshold_
virtual void configCallback(Config &config, uint32_t level)
ros::Publisher pub_average_time_
double distance_threshold_
void concatenateFields(PointCloud< PointXYZ > &cloud_xyz, PointCloud< RGB > &cloud_rgb, PointCloud< PointXYZRGB > &cloud)
ros::Publisher pub_polygons_
ros::Publisher pub_latest_time_
virtual void unsubscribe()
std::vector< PCLIndicesMsg > convertToROSPointIndices(const std::vector< pcl::PointIndices::Ptr > cluster_indices, const std_msgs::Header &header)
boost::shared_ptr< dynamic_reconfigure::Server< Config > > srv_
boost::mutex mutex
global mutex.
virtual void ransacEstimation(const pcl::PointCloud< pcl::PointXYZRGB >::Ptr &cloud, const pcl::PointIndices::Ptr &indices, pcl::PointIndices &inliers, pcl::ModelCoefficients &coefficient)
PLUGINLIB_EXPORT_CLASS(jsk_pcl_ros::RegionGrowingMultiplePlaneSegmentation, nodelet::Nodelet)
void subscribe(ros::NodeHandle &nh, const std::string &topic, uint32_t queue_size, const ros::TransportHints &transport_hints=ros::TransportHints(), ros::CallbackQueueInterface *callback_queue=0)
static void setCondifionFunctionParameter(const double angular_threshold, const double distance_threshold)
RegionGrowingMultiplePlaneSegmentationConfig Config
static double global_angular_threshold
int ransac_refine_max_iterations_
static double global_distance_threshold
message_filters::Subscriber< sensor_msgs::PointCloud2 > sub_input_
double angular_threshold_
static bool regionGrowingFunction(const pcl::PointXYZRGBNormal &a, const pcl::PointXYZRGBNormal &b, float distance)
ros::Publisher pub_clustering_result_
double cluster_tolerance_
bool done_initialization_