41 #ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_
42 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_
44 #include <pcl/common/copy_point.h>
45 #include <pcl/common/io.h>
49 namespace registration {
51 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
56 if (cloud->points.empty()) {
57 PCL_ERROR(
"[pcl::registration::%s::setInputTarget] Invalid or empty point cloud "
59 getClassName().c_str());
65 if (point_representation_)
66 tree_->setPointRepresentation(point_representation_);
68 target_cloud_updated_ =
true;
71 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
76 PCL_ERROR(
"[pcl::registration::%s::compute] No input target dataset was given!\n",
77 getClassName().c_str());
82 if (target_cloud_updated_ && !force_no_recompute_) {
85 tree_->setInputCloud(target_, target_indices_);
87 tree_->setInputCloud(target_);
89 target_cloud_updated_ =
false;
95 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
100 if (source_cloud_updated_ && !force_no_recompute_reciprocal_) {
101 if (point_representation_reciprocal_)
102 tree_reciprocal_->setPointRepresentation(point_representation_reciprocal_);
105 tree_reciprocal_->setInputCloud(getInputSource(), getIndicesSource());
107 tree_reciprocal_->setInputCloud(getInputSource());
109 source_cloud_updated_ =
false;
118 typename PointTarget,
119 typename PointSource,
121 typename std::enable_if_t<isSamePointType<PointSource, PointTarget>()>* =
nullptr>
125 return (*input)[idx];
129 typename PointTarget,
130 typename PointSource,
132 typename std::enable_if_t<!isSamePointType<PointSource, PointTarget>()>* =
nullptr>
144 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
152 correspondences.resize(indices_->size());
156 std::vector<pcl::Correspondences> per_thread_correspondences(num_threads_);
157 for (
auto& corrs : per_thread_correspondences) {
158 corrs.reserve(2 * indices_->size() / num_threads_);
160 double max_dist_sqr = max_distance * max_distance;
162 #pragma omp parallel for default(none) \
163 shared(max_dist_sqr, per_thread_correspondences) firstprivate(index, distance) \
164 num_threads(num_threads_)
166 for (
int i = 0; i < static_cast<int>(indices_->size()); i++) {
167 const auto& idx = (*indices_)[i];
171 const auto& pt = detail::pointCopyOrRef<PointTarget, PointSource>(input_, idx);
172 tree_->nearestKSearch(pt, 1, index,
distance);
182 const int thread_num = omp_get_thread_num();
184 const int thread_num = 0;
187 per_thread_correspondences[thread_num].emplace_back(corr);
190 if (num_threads_ == 1) {
191 correspondences = std::move(per_thread_correspondences.front());
194 const unsigned int nr_correspondences = std::accumulate(
195 per_thread_correspondences.begin(),
196 per_thread_correspondences.end(),
197 static_cast<unsigned int>(0),
198 [](
const auto sum,
const auto& corr) { return sum + corr.size(); });
199 correspondences.resize(nr_correspondences);
202 auto insert_loc = correspondences.begin();
203 for (
const auto& corrs : per_thread_correspondences) {
204 const auto new_insert_loc = std::move(corrs.begin(), corrs.end(), insert_loc);
205 std::inplace_merge(correspondences.begin(),
207 insert_loc + corrs.size(),
208 [](
const auto& lhs,
const auto& rhs) {
209 return lhs.index_query < rhs.index_query;
211 insert_loc = new_insert_loc;
217 template <
typename Po
intSource,
typename Po
intTarget,
typename Scalar>
228 if (!initComputeReciprocal())
230 double max_dist_sqr = max_distance * max_distance;
232 correspondences.resize(indices_->size());
236 std::vector<float> distance_reciprocal(1);
237 std::vector<pcl::Correspondences> per_thread_correspondences(num_threads_);
238 for (
auto& corrs : per_thread_correspondences) {
239 corrs.reserve(2 * indices_->size() / num_threads_);
242 #pragma omp parallel for default(none) \
243 shared(max_dist_sqr, per_thread_correspondences) \
244 firstprivate(index, distance, index_reciprocal, distance_reciprocal) \
245 num_threads(num_threads_)
247 for (
int i = 0; i < static_cast<int>(indices_->size()); i++) {
248 const auto& idx = (*indices_)[i];
253 const auto& pt_src = detail::pointCopyOrRef<PointTarget, PointSource>(input_, idx);
255 tree_->nearestKSearch(pt_src, 1, index,
distance);
259 const auto target_idx = index[0];
261 detail::pointCopyOrRef<PointSource, PointTarget>(target_, target_idx);
263 tree_reciprocal_->nearestKSearch(pt_tgt, 1, index_reciprocal, distance_reciprocal);
264 if (distance_reciprocal[0] > max_dist_sqr || idx != index_reciprocal[0])
273 const int thread_num = omp_get_thread_num();
275 const int thread_num = 0;
278 per_thread_correspondences[thread_num].emplace_back(corr);
281 if (num_threads_ == 1) {
282 correspondences = std::move(per_thread_correspondences.front());
285 const unsigned int nr_correspondences = std::accumulate(
286 per_thread_correspondences.begin(),
287 per_thread_correspondences.end(),
288 static_cast<unsigned int>(0),
289 [](
const auto sum,
const auto& corr) { return sum + corr.size(); });
290 correspondences.resize(nr_correspondences);
293 auto insert_loc = correspondences.begin();
294 for (
const auto& corrs : per_thread_correspondences) {
295 const auto new_insert_loc = std::move(corrs.begin(), corrs.end(), insert_loc);
296 std::inplace_merge(correspondences.begin(),
298 insert_loc + corrs.size(),
299 [](
const auto& lhs,
const auto& rhs) {
300 return lhs.index_query < rhs.index_query;
302 insert_loc = new_insert_loc;
shared_ptr< const PointCloud< PointT > > ConstPtr
bool initCompute()
Internal computation initialization.
typename PointCloudTarget::ConstPtr PointCloudTargetConstPtr
void setInputTarget(const PointCloudTargetConstPtr &cloud)
Provide a pointer to the input target (e.g., the point cloud that we want to align the input source t...
bool initComputeReciprocal()
Internal computation initialization for reciprocal correspondences.
void determineCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max()) override
Determine the correspondences between input and target cloud.
void determineReciprocalCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max()) override
Determine the reciprocal correspondences between input and target cloud.
void copyPoint(const PointInT &point_in, PointOutT &point_out)
Copy the fields of a source point into a target point.
float distance(const PointT &p1, const PointT &p2)
const PointSource & pointCopyOrRef(typename pcl::PointCloud< PointSource >::ConstPtr &input, const Index &idx)
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
IndicesAllocator<> Indices
Type used for indices in PCL.
Correspondence represents a match between two entities (e.g., points, descriptors,...
index_t index_query
Index of the query (source) point.
index_t index_match
Index of the matching (target) point.