38 #ifndef PCL_SEGMENTATION_IMPL_CPC_SEGMENTATION_HPP_
39 #define PCL_SEGMENTATION_IMPL_CPC_SEGMENTATION_HPP_
41 #include <pcl/sample_consensus/sac_model_plane.h>
42 #include <pcl/segmentation/cpc_segmentation.h>
44 template <
typename Po
intT>
47 min_segment_size_for_cutting_ (400),
48 min_cut_score_ (0.16),
49 use_local_constrains_ (true),
50 use_directed_weights_ (true),
55 template <
typename Po
intT>
60 template <
typename Po
intT>
void
67 calculateConvexConnections (sv_adjacency_list_);
70 applyKconvexity (k_factor_);
75 grouping_data_valid_ =
true;
77 applyCuttingPlane (max_cuts_);
80 mergeSmallSegments ();
83 PCL_WARN (
"[pcl::CPCSegmentation::segment] WARNING: Call function setInputSupervoxels first. Nothing has been done. \n");
86 template <
typename Po
intT>
void
89 using SegLabel2ClusterMap = std::map<std::uint32_t, pcl::PointCloud<WeightSACPointType>::Ptr>;
93 if (depth_levels_left <= 0)
97 SegLabel2ClusterMap seg_to_edge_points_map;
98 std::map<std::uint32_t, std::vector<EdgeID> > seg_to_edgeIDs_map;
99 EdgeIterator edge_itr, edge_itr_end, next_edge;
100 boost::tie (edge_itr, edge_itr_end) = boost::edges (sv_adjacency_list_);
101 for (next_edge = edge_itr; edge_itr != edge_itr_end; edge_itr = next_edge)
104 std::uint32_t source_sv_label = sv_adjacency_list_[boost::source (*edge_itr, sv_adjacency_list_)];
105 std::uint32_t target_sv_label = sv_adjacency_list_[boost::target (*edge_itr, sv_adjacency_list_)];
107 std::uint32_t source_segment_label = sv_label_to_seg_label_map_[source_sv_label];
108 std::uint32_t target_segment_label = sv_label_to_seg_label_map_[target_sv_label];
111 if (source_segment_label != target_segment_label)
115 if (sv_adjacency_list_[*edge_itr].used_for_cutting)
118 const pcl::PointXYZRGBA source_centroid = sv_label_to_supervoxel_map_[source_sv_label]->centroid_;
119 const pcl::PointXYZRGBA target_centroid = sv_label_to_supervoxel_map_[target_sv_label]->centroid_;
124 WeightSACPointType edge_centroid;
125 edge_centroid.getVector3fMap () = (source_centroid.getVector3fMap () + target_centroid.getVector3fMap ()) / 2;
128 edge_centroid.getNormalVector3fMap () = (target_centroid.getVector3fMap () - source_centroid.getVector3fMap ()).normalized ();
131 edge_centroid.intensity = sv_adjacency_list_[*edge_itr].is_convex ? -sv_adjacency_list_[*edge_itr].normal_difference : sv_adjacency_list_[*edge_itr].normal_difference;
132 if (seg_to_edge_points_map.find (source_segment_label) == seg_to_edge_points_map.end ())
136 seg_to_edge_points_map[source_segment_label]->push_back (edge_centroid);
137 seg_to_edgeIDs_map[source_segment_label].push_back (*edge_itr);
139 bool cut_found =
false;
141 for (
const auto &seg_to_edge_points : seg_to_edge_points_map)
144 if (seg_to_edge_points.second->size () < min_segment_size_for_cutting_)
149 std::vector<double> weights;
150 weights.resize (seg_to_edge_points.second->size ());
151 for (std::size_t cp = 0;
cp < seg_to_edge_points.second->size (); ++
cp)
153 float& cur_weight = (*seg_to_edge_points.second)[cp].intensity;
154 cur_weight = cur_weight < concavity_tolerance_threshold_ ? 0 : 1;
155 weights[
cp] = cur_weight;
161 WeightedRandomSampleConsensus weight_sac (model_p, seed_resolution_,
true);
163 weight_sac.setWeights (weights, use_directed_weights_);
164 weight_sac.setMaxIterations (ransac_itrs_);
167 if (!weight_sac.computeModel ())
172 Eigen::VectorXf model_coefficients;
173 weight_sac.getModelCoefficients (model_coefficients);
175 model_coefficients[3] += std::numeric_limits<float>::epsilon ();
177 weight_sac.getInliers (*support_indices);
182 if (use_local_constrains_)
184 Eigen::Vector3f plane_normal (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
188 std::vector<pcl::PointIndices> cluster_indices;
191 tree->setInputCloud (edge_cloud_cluster);
197 euclidean_clusterer.
setIndices (support_indices);
198 euclidean_clusterer.
extract (cluster_indices);
201 for (
const auto &cluster_index : cluster_indices)
204 int cluster_concave_pts = 0;
205 float cluster_score = 0;
207 for (
const auto ¤t_index : cluster_index.indices)
209 double index_score = weights[current_index];
210 if (use_directed_weights_)
211 index_score *= 1.414 * (std::abs (plane_normal.dot (edge_cloud_cluster->
at (current_index).getNormalVector3fMap ())));
212 cluster_score += index_score;
213 if (weights[current_index] > 0)
214 ++cluster_concave_pts;
217 cluster_score /= cluster_index.indices.size ();
219 if (cluster_score >= min_cut_score_)
221 cut_support_indices.insert (cut_support_indices.end (), cluster_index.indices.begin (), cluster_index.indices.end ());
224 if (cut_support_indices.empty ())
232 double current_score = weight_sac.getBestScore ();
233 cut_support_indices = *support_indices;
235 if (current_score < min_cut_score_)
242 int number_connections_cut = 0;
243 for (
const auto &point_index : cut_support_indices)
245 if (use_clean_cutting_)
248 std::uint32_t source_sv_label = sv_adjacency_list_[boost::source (seg_to_edgeIDs_map[seg_to_edge_points.first][point_index], sv_adjacency_list_)];
249 std::uint32_t target_sv_label = sv_adjacency_list_[boost::target (seg_to_edgeIDs_map[seg_to_edge_points.first][point_index], sv_adjacency_list_)];
251 const pcl::PointXYZRGBA source_centroid = sv_label_to_supervoxel_map_[source_sv_label]->centroid_;
252 const pcl::PointXYZRGBA target_centroid = sv_label_to_supervoxel_map_[target_sv_label]->centroid_;
259 sv_adjacency_list_[seg_to_edgeIDs_map[seg_to_edge_points.first][point_index]].used_for_cutting =
true;
260 if (sv_adjacency_list_[seg_to_edgeIDs_map[seg_to_edge_points.first][point_index]].is_valid)
262 ++number_connections_cut;
263 sv_adjacency_list_[seg_to_edgeIDs_map[seg_to_edge_points.first][point_index]].is_valid =
false;
267 if (number_connections_cut > 0)
276 applyCuttingPlane (depth_levels_left);
285 template <
typename Po
intT>
bool
289 if (threshold_ == std::numeric_limits<double>::max ())
291 PCL_ERROR (
"[pcl::CPCSegmentation<PointT>::WeightedRandomSampleConsensus::computeModel] No threshold set!\n");
296 best_score_ = -std::numeric_limits<double>::max ();
298 std::vector<int> selection;
299 Eigen::VectorXf model_coefficients;
301 unsigned skipped_count = 0;
303 const unsigned max_skip = max_iterations_ * 10;
306 while (iterations_ < max_iterations_ && skipped_count < max_skip)
309 sac_model_->setIndices (model_pt_indices_);
310 sac_model_->getSamples (iterations_, selection);
312 if (selection.empty ())
314 PCL_ERROR (
"[pcl::CPCSegmentation<PointT>::WeightedRandomSampleConsensus::computeModel] No samples could be selected!\n");
319 if (!sac_model_->computeModelCoefficients (selection, model_coefficients))
326 sac_model_->setIndices (full_cloud_pt_indices_);
329 sac_model_->selectWithinDistance (model_coefficients, threshold_, *current_inliers);
330 double current_score = 0;
331 Eigen::Vector3f plane_normal (model_coefficients[0], model_coefficients[1], model_coefficients[2]);
332 for (
const auto ¤t_index : *current_inliers)
334 double index_score = weights_[current_index];
335 if (use_directed_weights_)
337 index_score *= 1.414 * (std::abs (plane_normal.dot (point_cloud_ptr_->at (current_index).getNormalVector3fMap ())));
339 current_score += index_score;
342 current_score /= current_inliers->size ();
345 if (current_score > best_score_)
347 best_score_ = current_score;
350 model_coefficients_ = model_coefficients;
354 PCL_DEBUG (
"[pcl::CPCSegmentation<PointT>::WeightedRandomSampleConsensus::computeModel] Trial %d (max %d): score is %f (best is: %f so far).\n", iterations_, max_iterations_, current_score, best_score_);
355 if (iterations_ > max_iterations_)
357 PCL_DEBUG (
"[pcl::CPCSegmentation<PointT>::WeightedRandomSampleConsensus::computeModel] RANSAC reached the maximum number of trials.\n");
362 PCL_DEBUG (
"[pcl::CPCSegmentation<PointT>::WeightedRandomSampleConsensus::computeModel] Model: %lu size, %f score.\n", model_.size (), best_score_);
371 sac_model_->selectWithinDistance (model_coefficients_, threshold_, inliers_);
375 #endif // PCL_SEGMENTATION_IMPL_CPC_SEGMENTATION_HPP_