37 #include <pcl/sample_consensus/method_types.h>
38 #include <pcl/sample_consensus/model_types.h>
39 #include <pcl/segmentation/sac_segmentation.h>
40 #include <pcl/filters/extract_indices.h>
48 ConnectionBasedNodelet::onInit();
49 srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
50 dynamic_reconfigure::Server<Config>::CallbackType
f =
52 srv_->setCallback (
f);
59 NODELET_ERROR(
"Cannot use ~use_imu_perpendicular and ~use_imu_parallel at the same time");
68 pub_inliers_ = advertise<jsk_recognition_msgs::ClusterPointIndices>(*pnh_,
"output_indices", 1);
70 = advertise<jsk_recognition_msgs::ModelCoefficientsArray>(*pnh_,
"output_coefficients", 1);
71 pub_polygons_ = advertise<jsk_recognition_msgs::PolygonArray>(*pnh_,
"output_polygons", 1);
107 sync_imu_ = boost::make_shared<message_filters::Synchronizer<SyncImuPolicy> >(100);
117 sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(100);
127 = boost::make_shared<message_filters::Synchronizer<SyncClusterPolicy> >(100);
169 const pcl::PointCloud<PointT>::Ptr&
input,
170 const pcl::PointCloud<pcl::Normal>::Ptr& normal,
171 const Eigen::Vector3f& imu_vector,
172 std::vector<pcl::PointIndices::Ptr>& output_inliers,
173 std::vector<pcl::ModelCoefficients::Ptr>& output_coefficients,
174 std::vector<jsk_recognition_utils::ConvexPolygon::Ptr>& output_polygons)
176 pcl::PointCloud<PointT>::Ptr rest_cloud (
new pcl::PointCloud<PointT>);
177 pcl::PointCloud<pcl::Normal>::Ptr rest_normal (
new pcl::PointCloud<pcl::Normal>);
178 *rest_cloud = *
input;
179 *rest_normal = *normal;
184 pcl::PointIndices::Ptr inliers (
new pcl::PointIndices);
185 pcl::ModelCoefficients::Ptr
coefficients (
new pcl::ModelCoefficients);
187 pcl::SACSegmentation<PointT> seg;
188 seg.setOptimizeCoefficients (
true);
189 seg.setRadiusLimits(0.3, std::numeric_limits<double>::max ());
190 seg.setMethodType (pcl::SAC_RANSAC);
192 seg.setInputCloud(rest_cloud);
195 seg.setModelType(pcl::SACMODEL_PARALLEL_PLANE);
196 seg.setAxis(imu_vector);
200 seg.setModelType(pcl::SACMODEL_PERPENDICULAR_PLANE);
201 seg.setAxis(imu_vector);
205 seg.setModelType (pcl::SACMODEL_PLANE);
207 seg.segment (*inliers, *coefficients);
210 pcl::SACSegmentationFromNormals<PointT, pcl::Normal> seg;
211 seg.setOptimizeCoefficients (
true);
213 seg.setMethodType (pcl::SAC_RANSAC);
216 seg.setInputCloud(rest_cloud);
217 seg.setInputNormals(rest_normal);
220 seg.setModelType (pcl::SACMODEL_NORMAL_PARALLEL_PLANE);
221 seg.setAxis(imu_vector);
225 seg.setModelType (pcl::SACMODEL_NORMAL_PLANE);
226 seg.setAxis(imu_vector);
230 seg.setModelType (pcl::SACMODEL_NORMAL_PLANE);
232 seg.segment (*inliers, *coefficients);
236 output_inliers.push_back(inliers);
237 output_coefficients.push_back(coefficients);
239 input, inliers, coefficients);
240 output_polygons.push_back(convex);
249 pcl::PointCloud<PointT>::Ptr next_rest_cloud
250 (
new pcl::PointCloud<PointT>);
251 pcl::PointCloud<pcl::Normal>::Ptr next_rest_normal
252 (
new pcl::PointCloud<pcl::Normal>);
253 pcl::ExtractIndices<PointT>
ex;
254 ex.setInputCloud(rest_cloud);
255 ex.setIndices(inliers);
256 ex.setNegative(
true);
257 ex.setKeepOrganized(
true);
258 ex.filter(*next_rest_cloud);
260 pcl::ExtractIndices<pcl::Normal> ex_normal;
261 ex_normal.setInputCloud(rest_normal);
262 ex_normal.setIndices(inliers);
263 ex_normal.setNegative(
true);
264 ex_normal.setKeepOrganized(
true);
265 ex_normal.filter(*next_rest_normal);
267 if (next_rest_cloud->points.size() <
min_points_) {
271 rest_cloud = next_rest_cloud;
272 rest_normal = next_rest_normal;
277 const sensor_msgs::PointCloud2::ConstPtr&
msg,
278 const jsk_recognition_msgs::ClusterPointIndices::ConstPtr& clusters)
281 pcl::PointCloud<PointT>::Ptr
input (
new pcl::PointCloud<PointT>);
283 std::vector<pcl::PointIndices::Ptr> cluster_indices
285 pcl::ExtractIndices<PointT>
ex;
287 ex.setKeepOrganized(
true);
288 std::vector<pcl::PointIndices::Ptr> all_inliers;
289 std::vector<pcl::ModelCoefficients::Ptr> all_coefficients;
290 std::vector<jsk_recognition_utils::ConvexPolygon::Ptr> all_convexes;
291 for (
size_t i = 0;
i < cluster_indices.size();
i++) {
292 pcl::PointIndices::Ptr indices = cluster_indices[
i];
293 pcl::PointCloud<PointT>::Ptr cluster_cloud (
new pcl::PointCloud<PointT>);
294 pcl::PointCloud<pcl::Normal>::Ptr normal (
new pcl::PointCloud<pcl::Normal>);
295 ex.setIndices(indices);
297 ex.filter(*cluster_cloud);
298 std::vector<pcl::PointIndices::Ptr> inliers;
300 std::vector<jsk_recognition_utils::ConvexPolygon::Ptr> convexes;
301 Eigen::Vector3f dummy_imu_vector;
311 const std_msgs::Header&
header,
312 const std::vector<pcl::PointIndices::Ptr>& inliers,
313 const std::vector<pcl::ModelCoefficients::Ptr>& coefficients,
314 const std::vector<jsk_recognition_utils::ConvexPolygon::Ptr>& convexes)
316 jsk_recognition_msgs::ClusterPointIndices ros_indices_output;
317 jsk_recognition_msgs::ModelCoefficientsArray ros_coefficients_output;
318 jsk_recognition_msgs::PolygonArray ros_polygon_output;
319 ros_indices_output.header =
header;
320 ros_coefficients_output.header =
header;
321 ros_polygon_output.header =
header;
322 ros_indices_output.cluster_indices
324 ros_coefficients_output.coefficients
328 for (
size_t i = 0;
i < convexes.size();
i++) {
329 geometry_msgs::PolygonStamped
polygon;
331 polygon.polygon = convexes[
i]->toROSMsg();
332 ros_polygon_output.polygons.push_back(
polygon);
338 const sensor_msgs::PointCloud2::ConstPtr&
msg,
339 const sensor_msgs::Imu::ConstPtr& imu_msg)
345 const sensor_msgs::PointCloud2::ConstPtr&
msg,
346 const sensor_msgs::PointCloud2::ConstPtr& normal_msg,
347 const sensor_msgs::Imu::ConstPtr& imu_msg)
350 pcl::PointCloud<PointT>::Ptr
input (
new pcl::PointCloud<PointT>);
351 pcl::PointCloud<pcl::Normal>::Ptr
352 normal (
new pcl::PointCloud<pcl::Normal>);
357 geometry_msgs::Vector3Stamped stamped_imu, transformed_stamped_imu;
358 stamped_imu.header = imu_msg->header;
359 stamped_imu.vector = imu_msg->linear_acceleration;
362 msg->header.frame_id, imu_msg->header.frame_id, imu_msg->header.stamp,
365 msg->header.frame_id, stamped_imu, transformed_stamped_imu);
366 Eigen::Vector3d imu_vectord;
367 Eigen::Vector3f imu_vector;
369 jsk_recognition_utils::pointFromVectorToVector<Eigen::Vector3d, Eigen::Vector3f>(
370 imu_vectord, imu_vector);
371 imu_vector = - imu_vector;
373 std::vector<pcl::PointIndices::Ptr> inliers;
375 std::vector<jsk_recognition_utils::ConvexPolygon::Ptr> convexes;
377 inliers, coefficients, convexes);
381 NODELET_ERROR(
"[%s] Transform error: %s", __PRETTY_FUNCTION__, e.what());
384 NODELET_ERROR(
"[%s] Transform error: %s", __PRETTY_FUNCTION__, e.what());
387 NODELET_ERROR(
"[%s] Transform error: %s", __PRETTY_FUNCTION__, e.what());
393 const sensor_msgs::PointCloud2::ConstPtr&
msg,
394 const sensor_msgs::PointCloud2::ConstPtr& msg_normal)
397 pcl::PointCloud<PointT>::Ptr
input (
new pcl::PointCloud<PointT>);
398 pcl::PointCloud<pcl::Normal>::Ptr normal (
new pcl::PointCloud<pcl::Normal>);
403 std::vector<pcl::PointIndices::Ptr> inliers;
405 std::vector<jsk_recognition_utils::ConvexPolygon::Ptr> convexes;
406 Eigen::Vector3f dummy_imu_vector;
408 inliers, coefficients, convexes);
413 const sensor_msgs::PointCloud2::ConstPtr&
msg)
415 segment(
msg, sensor_msgs::PointCloud2::ConstPtr());