Point Cloud Library (PCL) 1.15.1-dev
Loading...
Searching...
No Matches
correspondence_estimation.hpp
1/*
2 * Software License Agreement (BSD License)
3 *
4 * Point Cloud Library (PCL) - www.pointclouds.org
5 * Copyright (c) 2010-2011, Willow Garage, Inc.
6 * Copyright (c) 2012-, Open Perception, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 * * Neither the name of the copyright holder(s) nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 * $Id$
38 *
39 */
40
41#ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_
42#define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_
43
44#include <pcl/common/copy_point.h>
45#include <pcl/common/io.h>
46#include <pcl/common/point_tests.h> // for isXYZFinite
47#include <pcl/search/auto.h>
48
49namespace pcl {
50
51namespace registration {
52
53template <typename PointSource, typename PointTarget, typename Scalar>
54void
56 const PointCloudTargetConstPtr& cloud)
57{
58 if (cloud->points.empty()) {
59 PCL_ERROR("[pcl::registration::%s::setInputTarget] Invalid or empty point cloud "
60 "dataset given!\n",
61 getClassName().c_str());
62 return;
63 }
64 target_ = cloud;
65 target_cloud_updated_ = true;
66}
67
68template <typename PointSource, typename PointTarget, typename Scalar>
69bool
71{
72 if (!target_) {
73 PCL_ERROR("[pcl::registration::%s::compute] No input target dataset was given!\n",
74 getClassName().c_str());
75 return (false);
76 }
77
78 // Only update target kd-tree if a new target cloud was set
79 if (target_cloud_updated_ && !force_no_recompute_) {
80 // If the target indices have been given via setIndicesTarget
81 if (point_representation_) {
82 if (!tree_ || !tree_->setPointRepresentation(point_representation_) ||
83 (target_indices_ ? !tree_->setInputCloud(target_, target_indices_)
84 : !tree_->setInputCloud(target_)))
85 tree_.reset(pcl::search::autoSelectMethod<PointTarget>(
86 target_,
87 (target_indices_ ? target_indices_ : pcl::IndicesConstPtr()),
88 point_representation_,
89 false,
91 }
92 else if (target_indices_) {
93 if (!tree_ || !tree_->setInputCloud(target_, target_indices_))
94 tree_.reset(pcl::search::autoSelectMethod<PointTarget>(
95 target_, target_indices_, false, pcl::search::Purpose::one_knn_search));
96 }
97 else {
98 if (!tree_ || !tree_->setInputCloud(target_))
99 tree_.reset(pcl::search::autoSelectMethod<PointTarget>(
100 target_, false, pcl::search::Purpose::one_knn_search));
101 }
102
103 target_cloud_updated_ = false;
104 }
105
107}
108
109template <typename PointSource, typename PointTarget, typename Scalar>
110bool
112{
113 // Only update source kd-tree if a new target cloud was set
114 if (source_cloud_updated_ && !force_no_recompute_reciprocal_) {
115 if (point_representation_reciprocal_) {
116 if (!tree_reciprocal_ ||
117 !tree_reciprocal_->setPointRepresentation(point_representation_reciprocal_) ||
118 (indices_
119 ? !tree_reciprocal_->setInputCloud(getInputSource(), getIndicesSource())
120 : !tree_reciprocal_->setInputCloud(getInputSource())))
121 tree_reciprocal_.reset(pcl::search::autoSelectMethod<PointSource>(
122 getInputSource(),
123 (indices_ ? getIndicesSource() : pcl::IndicesConstPtr()),
124 point_representation_reciprocal_,
125 false,
127 }
128 else if (indices_) {
129 if (!tree_reciprocal_ ||
130 !tree_reciprocal_->setInputCloud(getInputSource(), getIndicesSource()))
131 tree_reciprocal_.reset(pcl::search::autoSelectMethod<PointSource>(
132 getInputSource(),
133 getIndicesSource(),
134 false,
136 }
137 else {
138 if (!tree_reciprocal_ || !tree_reciprocal_->setInputCloud(getInputSource()))
139 tree_reciprocal_.reset(pcl::search::autoSelectMethod<PointSource>(
140 getInputSource(), false, pcl::search::Purpose::one_knn_search));
141 }
142 source_cloud_updated_ = false;
143 }
144
145 return (true);
146}
147
148namespace detail {
149
150template <
151 typename PointTarget,
152 typename PointSource,
153 typename Index,
154 typename std::enable_if_t<isSamePointType<PointSource, PointTarget>()>* = nullptr>
155const PointSource&
157{
158 return (*input)[idx];
159}
160
161template <
162 typename PointTarget,
163 typename PointSource,
164 typename Index,
165 typename std::enable_if_t<!isSamePointType<PointSource, PointTarget>()>* = nullptr>
166PointTarget
168{
169 // Copy the source data to a target PointTarget format so we can search in the tree
170 PointTarget pt;
171 copyPoint((*input)[idx], pt);
172 return pt;
173}
174
175} // namespace detail
176
177template <typename PointSource, typename PointTarget, typename Scalar>
178void
180 pcl::Correspondences& correspondences, const double max_distance)
181{
182 if (!initCompute())
183 return;
184
185 correspondences.resize(indices_->size());
186
187 pcl::Indices index(1);
188 std::vector<float> distance(1);
189 std::vector<pcl::Correspondences> per_thread_correspondences(num_threads_);
190 for (auto& corrs : per_thread_correspondences) {
191 corrs.reserve(2 * indices_->size() / num_threads_);
192 }
193 const double max_dist_sqr = max_distance * max_distance;
194
195#pragma omp parallel for default(none) \
196 shared(max_dist_sqr, per_thread_correspondences) firstprivate(index, distance) \
197 num_threads(num_threads_)
198 // Iterate over the input set of source indices
199 for (int i = 0; i < static_cast<int>(indices_->size()); i++) {
200 const auto& idx = (*indices_)[i];
201 // Check if the template types are the same. If true, avoid a copy.
202 // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT
203 // macro!
204 const auto& pt = detail::pointCopyOrRef<PointTarget, PointSource>(input_, idx);
205 if (!input_->is_dense && !pcl::isXYZFinite(pt))
206 continue;
207 tree_->nearestKSearch(pt, 1, index, distance);
208 if (distance[0] > max_dist_sqr)
209 continue;
210
212 corr.index_query = idx;
213 corr.index_match = index[0];
214 corr.distance = distance[0];
215
216#ifdef _OPENMP
217 const int thread_num = omp_get_thread_num();
218#else
219 const int thread_num = 0;
220#endif
221
222 per_thread_correspondences[thread_num].emplace_back(corr);
223 }
224
225 if (num_threads_ == 1) {
226 correspondences = std::move(per_thread_correspondences.front());
227 }
228 else {
229 const unsigned int nr_correspondences = std::accumulate(
230 per_thread_correspondences.begin(),
231 per_thread_correspondences.end(),
232 static_cast<unsigned int>(0),
233 [](const auto sum, const auto& corr) { return sum + corr.size(); });
234 correspondences.resize(nr_correspondences);
235
236 // Merge per-thread correspondences while keeping them ordered
237 auto insert_loc = correspondences.begin();
238 for (const auto& corrs : per_thread_correspondences) {
239 const auto new_insert_loc = std::move(corrs.begin(), corrs.end(), insert_loc);
240 std::inplace_merge(correspondences.begin(),
241 insert_loc,
242 insert_loc + corrs.size(),
243 [](const auto& lhs, const auto& rhs) {
244 return lhs.index_query < rhs.index_query;
245 });
246 insert_loc = new_insert_loc;
247 }
248 }
249 deinitCompute();
250}
251
252template <typename PointSource, typename PointTarget, typename Scalar>
253void
256 const double max_distance)
257{
258 if (!initCompute())
259 return;
260
261 // setup tree for reciprocal search
262 // Set the internal point representation of choice
263 if (!initComputeReciprocal())
264 return;
265
266 correspondences.resize(indices_->size());
267 pcl::Indices index(1);
268 std::vector<float> distance(1);
269 pcl::Indices index_reciprocal(1);
270 std::vector<float> distance_reciprocal(1);
271 std::vector<pcl::Correspondences> per_thread_correspondences(num_threads_);
272 for (auto& corrs : per_thread_correspondences) {
273 corrs.reserve(2 * indices_->size() / num_threads_);
274 }
275 const double max_dist_sqr = max_distance * max_distance;
276
277#pragma omp parallel for default(none) \
278 shared(max_dist_sqr, per_thread_correspondences) \
279 firstprivate(index, distance, index_reciprocal, distance_reciprocal) \
280 num_threads(num_threads_)
281 // Iterate over the input set of source indices
282 for (int i = 0; i < static_cast<int>(indices_->size()); i++) {
283 const auto& idx = (*indices_)[i];
284 // Check if the template types are the same. If true, avoid a copy.
285 // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT
286 // macro!
287
288 const auto& pt_src = detail::pointCopyOrRef<PointTarget, PointSource>(input_, idx);
289 if (!input_->is_dense && !pcl::isXYZFinite(pt_src))
290 continue;
291 tree_->nearestKSearch(pt_src, 1, index, distance);
292 if (distance[0] > max_dist_sqr)
293 continue;
294
295 const auto target_idx = index[0];
296 const auto& pt_tgt =
297 detail::pointCopyOrRef<PointSource, PointTarget>(target_, target_idx);
298
299 tree_reciprocal_->nearestKSearch(pt_tgt, 1, index_reciprocal, distance_reciprocal);
300 if (distance_reciprocal[0] > max_dist_sqr || idx != index_reciprocal[0])
301 continue;
302
304 corr.index_query = idx;
305 corr.index_match = index[0];
306 corr.distance = distance[0];
307
308#ifdef _OPENMP
309 const int thread_num = omp_get_thread_num();
310#else
311 const int thread_num = 0;
312#endif
313
314 per_thread_correspondences[thread_num].emplace_back(corr);
315 }
316
317 if (num_threads_ == 1) {
318 correspondences = std::move(per_thread_correspondences.front());
319 }
320 else {
321 const unsigned int nr_correspondences = std::accumulate(
322 per_thread_correspondences.begin(),
323 per_thread_correspondences.end(),
324 static_cast<unsigned int>(0),
325 [](const auto sum, const auto& corr) { return sum + corr.size(); });
326 correspondences.resize(nr_correspondences);
327
328 // Merge per-thread correspondences while keeping them ordered
329 auto insert_loc = correspondences.begin();
330 for (const auto& corrs : per_thread_correspondences) {
331 const auto new_insert_loc = std::move(corrs.begin(), corrs.end(), insert_loc);
332 std::inplace_merge(correspondences.begin(),
333 insert_loc,
334 insert_loc + corrs.size(),
335 [](const auto& lhs, const auto& rhs) {
336 return lhs.index_query < rhs.index_query;
337 });
338 insert_loc = new_insert_loc;
339 }
340 }
341
342 deinitCompute();
343}
344
345} // namespace registration
346} // namespace pcl
347
348//#define PCL_INSTANTIATE_CorrespondenceEstimation(T,U) template class PCL_EXPORTS
349// pcl::registration::CorrespondenceEstimation<T,U>;
350
351#endif /* PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_H_ */
PCL base class.
Definition pcl_base.h:70
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 determineReciprocalCorrespondences(pcl::Correspondences &correspondences, const double max_distance=std::numeric_limits< double >::max()) override
Determine the reciprocal correspondences between input and target cloud.
void determineCorrespondences(pcl::Correspondences &correspondences, const double max_distance=std::numeric_limits< double >::max()) override
Determine the 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.
const PointSource & pointCopyOrRef(typename pcl::PointCloud< PointSource >::ConstPtr &input, const Index &idx)
@ one_knn_search
The search method will mainly be used for nearestKSearch where k is 1.
shared_ptr< const Indices > IndicesConstPtr
Definition pcl_base.h:59
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133
constexpr bool isXYZFinite(const PointT &) noexcept
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.