38 #include <pcl/segmentation/sac_segmentation.h>
39 #include <pcl/filters/extract_indices.h>
40 #include <pcl/io/io.h>
48 ConnectionBasedNodelet::onInit();
53 pub_indices_ = advertise<jsk_recognition_msgs::ClusterPointIndices>(
56 *pnh_,
"output_coefficients", 1);
57 pub_edges_ = advertise<jsk_recognition_msgs::SegmentArray>(
58 *pnh_,
"output_edges", 1);
60 *pnh_,
"output_outlier_removed", 1);
62 *pnh_,
"output_outlier_removed_coefficients", 1);
64 *pnh_,
"output_outlier_removed_edges", 1);
68 srv_ = boost::make_shared <dynamic_reconfigure::Server<Config> > (*pnh_);
69 dynamic_reconfigure::Server<Config>::CallbackType
f =
71 srv_->setCallback (
f);
94 sync_ = boost::make_shared<message_filters::Synchronizer<SyncPolicy> >(100);
116 const pcl::ModelCoefficients::Ptr coefficients)
129 const int width,
const int height,
130 const std::vector<int>& indices)
133 int min_y_index, max_y_index, min_x_index, max_x_index;
138 for (
size_t i = 0;
i < indices.size();
i++) {
161 if (min_x_index != max_x_index) {
162 return boost::make_tuple(
163 min_x_index, max_x_index);
166 return boost::make_tuple(
167 min_y_index, max_y_index);
173 const pcl::PointCloud<PointT>::Ptr& cloud,
174 const std::vector<int>& indices,
177 boost::tuple<int, int> min_max
179 PointT min_point =
cloud->points[min_max.get<0>()];
180 PointT max_point =
cloud->points[min_max.get<1>()];
181 Eigen::Vector3f min_point_f = min_point.getVector3fMap();
182 Eigen::Vector3f max_point_f = max_point.getVector3fMap();
183 Eigen::Vector3f min_foot, max_foot;
184 line->foot(min_point_f, min_foot);
185 line->foot(max_point_f, max_foot);
191 const pcl::PointCloud<PointT>::Ptr& cloud,
192 const std::set<int>& duplicated_set,
193 const std::vector<pcl::PointIndices::Ptr> all_inliers,
194 pcl::PointIndices::Ptr& output_indices)
196 std::vector<int> integrated_indices;
197 for (std::set<int>::iterator it = duplicated_set.begin();
198 it != duplicated_set.end();
203 output_indices->indices = integrated_indices;
207 const pcl::PointCloud<PointT>::Ptr& cloud,
208 const std::vector<pcl::PointIndices::Ptr> all_inliers,
209 const std::vector<pcl::ModelCoefficients::Ptr> all_coefficients,
210 std::vector<pcl::PointIndices::Ptr>& output_inliers,
211 std::vector<pcl::ModelCoefficients::Ptr>& output_coefficients)
213 if (all_inliers.size() == 0) {
219 std::vector<jsk_recognition_utils::Line::Ptr>
lines;
220 std::vector<jsk_recognition_utils::Segment::Ptr> segments;
222 for (
size_t i = 0;
i < all_inliers.size();
i++) {
223 pcl::PointIndices::Ptr the_inliers = all_inliers[
i];
224 pcl::ModelCoefficients::Ptr the_coefficients = all_coefficients[
i];
228 lines.push_back(the_line);
229 segments.push_back(the_segment);
236 std::map<int, std::vector<int> > duplication_map;
237 for (
size_t i = 0;
i < all_inliers.size() - 1;
i++) {
238 duplication_map[
i] = std::vector<int>();
241 for (
size_t j = i + 1; j < all_inliers.size(); j++) {
244 Eigen::Vector3f candidate_midpoint;
245 candidate_segment->midpoint(candidate_midpoint);
247 double angle_diff = the_line->angle(*candidate_line);
249 double distance_diff = the_segment->distance(candidate_midpoint);
251 duplication_map[
i].push_back(j);
260 std::vector<std::set<int> > duplication_set_list;
261 std::set<int> duplicated_indices;
262 for (
size_t i = 0;
i < all_inliers.size();
i++) {
263 std::vector<int> duplication_list;
264 if (i < all_inliers.size() - 1) {
265 duplication_list = duplication_map[
i];
267 if (duplicated_indices.find(i) == duplicated_indices.end()) {
268 if (i == all_inliers.size() - 1 || duplication_list.size() == 0) {
269 std::set<int> no_duplication_set;
270 no_duplication_set.insert(i);
271 duplication_set_list.push_back(no_duplication_set);
272 jsk_recognition_utils::addSet<int>(duplicated_indices, no_duplication_set);
275 std::set<int> new_duplication_set;
279 new_duplication_set);
280 duplication_set_list.push_back(new_duplication_set);
282 jsk_recognition_utils::addSet<int>(duplicated_indices, new_duplication_set);
288 for (
size_t i = 0;
i < duplication_set_list.size();
i++) {
289 pcl::PointIndices::Ptr integrated_indices (
new pcl::PointIndices);
293 output_inliers.push_back(integrated_indices);
296 pcl::ModelCoefficients::Ptr integrated_coefficients
297 = all_coefficients[(*duplication_set_list[
i].begin())];
298 output_coefficients.push_back(integrated_coefficients);
327 const pcl::PointCloud<PointT>::Ptr& cloud,
328 const std::vector<PCLIndicesMsg>& indices,
329 std::vector<pcl::PointIndices::Ptr>& output_inliers,
330 std::vector<pcl::ModelCoefficients::Ptr>& output_coefficients)
334 for (
size_t i = 0;
i < indices.size();
i++) {
335 std::vector<int> cluster_indices = indices[
i].indices;
336 pcl::PointIndices::Ptr inliers (
new pcl::PointIndices);
337 pcl::ModelCoefficients::Ptr
coefficients (
new pcl::ModelCoefficients);
340 output_inliers.push_back(inliers);
341 output_coefficients.push_back(coefficients);
347 const pcl::PointCloud<PointT>::Ptr& cloud,
348 const std::vector<int>& indices,
349 pcl::PointIndices& inliers,
350 pcl::ModelCoefficients& coefficients)
354 pcl::SACSegmentation<PointT> seg;
355 seg.setOptimizeCoefficients (
true);
356 seg.setModelType (pcl::SACMODEL_LINE);
357 seg.setMethodType (pcl::SAC_RANSAC);
359 seg.setInputCloud(cloud);
360 pcl::PointIndices::Ptr indices_ptr (
new pcl::PointIndices);
361 indices_ptr->indices = indices;
362 seg.setIndices(indices_ptr);
363 seg.segment (inliers, coefficients);
370 const std::vector<pcl::PointIndices::Ptr> inliers,
371 const std::vector<pcl::ModelCoefficients::Ptr> coefficients,
372 const std_msgs::Header&
header)
374 jsk_recognition_msgs::ClusterPointIndices output_ros_msg;
375 jsk_recognition_msgs::ModelCoefficientsArray output_ros_coefficients_msg;
376 jsk_recognition_msgs::SegmentArray output_ros_edges_msg;
377 output_ros_msg.header =
header;
378 output_ros_coefficients_msg.header =
header;
379 output_ros_edges_msg.header =
header;
380 for (
size_t i = 0;
i < inliers.size();
i++) {
383 jsk_recognition_msgs::Segment output_edge_msg;
384 output_indices_msg.header =
header;
385 output_indices_msg.indices = inliers[
i]->indices;
386 output_ros_msg.cluster_indices.push_back(output_indices_msg);
388 output_coefficients_msg.header =
header;
390 output_ros_coefficients_msg.coefficients.push_back(output_coefficients_msg);
398 output_ros_edges_msg.segments.push_back(output_edge_msg);
400 pub.publish(output_ros_msg);
401 pub_coefficients.
publish(output_ros_coefficients_msg);
402 pub_edges.
publish(output_ros_edges_msg);
415 const sensor_msgs::PointCloud2ConstPtr &
input,
416 const jsk_recognition_msgs::ClusterPointIndicesConstPtr &indices)
419 pcl::PointCloud<PointT>::Ptr
cloud (
new pcl::PointCloud<PointT>);
422 std::vector<pcl::PointIndices::Ptr> inliers;
425 removeOutliers(cloud, indices->cluster_indices, inliers, coefficients);
426 std::vector<pcl::PointIndices::Ptr> non_duplicated_inliers;
427 std::vector<pcl::ModelCoefficients::Ptr> non_duplicated_coefficients;
429 non_duplicated_inliers,
430 non_duplicated_coefficients);
434 inliers, coefficients,
439 non_duplicated_inliers,
440 non_duplicated_coefficients,