Point Cloud Library (PCL)  1.15.1-dev
correspondence_estimation_backprojection.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  *
38  */
39 
40 #ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_BACK_PROJECTION_HPP_
41 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_BACK_PROJECTION_HPP_
42 
43 #include <pcl/common/copy_point.h>
44 
45 namespace pcl {
46 
47 namespace registration {
48 
49 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
50 bool
53 {
54  if (!source_normals_ || !target_normals_) {
55  PCL_WARN("[pcl::registration::%s::initCompute] Datasets containing normals for "
56  "source/target have not been given!\n",
57  getClassName().c_str());
58  return (false);
59  }
60 
61  return (
63 }
64 
65 ///////////////////////////////////////////////////////////////////////////////////////////
66 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
67 void
70  const double max_distance)
71 {
72  if (!initCompute())
73  return;
74 
75  correspondences.resize(indices_->size());
76 
77  pcl::Indices nn_indices(k_);
78  std::vector<float> nn_dists(k_);
79 
80  int min_index = 0;
81 
83  unsigned int nr_valid_correspondences = 0;
84 
85  // Iterate over the input set of source indices
86  for (const auto& idx_i : (*indices_)) {
87  const auto& pt = detail::pointCopyOrRef<PointTarget, PointSource>(input_, idx_i);
88  tree_->nearestKSearch(pt, k_, nn_indices, nn_dists);
89 
90  // Among the K nearest neighbours find the one with minimum perpendicular distance
91  // to the normal
92  float min_dist = std::numeric_limits<float>::max();
93 
94  // Find the best correspondence
95  for (std::size_t j = 0; j < nn_indices.size(); j++) {
96  float cos_angle = (*source_normals_)[idx_i].normal_x *
97  (*target_normals_)[nn_indices[j]].normal_x +
98  (*source_normals_)[idx_i].normal_y *
99  (*target_normals_)[nn_indices[j]].normal_y +
100  (*source_normals_)[idx_i].normal_z *
101  (*target_normals_)[nn_indices[j]].normal_z;
102  const float dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
103 
104  if (dist < min_dist) {
105  min_dist = dist;
106  min_index = static_cast<int>(j);
107  }
108  }
109  if (min_dist > max_distance)
110  continue;
111 
112  corr.index_query = idx_i;
113  corr.index_match = nn_indices[min_index];
114  corr.distance = nn_dists[min_index]; // min_dist;
115  correspondences[nr_valid_correspondences++] = corr;
116  }
117  correspondences.resize(nr_valid_correspondences);
118  deinitCompute();
119 }
120 
121 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
122 void
125  const double max_distance)
126 {
127  if (!initCompute())
128  return;
129 
130  // Set the internal point representation of choice
131  if (!initComputeReciprocal())
132  return;
133 
134  correspondences.resize(indices_->size());
135 
136  pcl::Indices nn_indices(k_);
137  std::vector<float> nn_dists(k_);
138  pcl::Indices index_reciprocal(1);
139  std::vector<float> distance_reciprocal(1);
140 
141  int min_index = 0;
142 
143  pcl::Correspondence corr;
144  unsigned int nr_valid_correspondences = 0;
145  int target_idx = 0;
146 
147  // Iterate over the input set of source indices
148  for (const auto& idx_i : (*indices_)) {
149  // Check if the template types are the same. If true, avoid a copy.
150  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT
151  // macro!
152  tree_->nearestKSearch(
153  detail::pointCopyOrRef<PointTarget, PointSource>(input_, idx_i),
154  k_,
155  nn_indices,
156  nn_dists);
157 
158  // Among the K nearest neighbours find the one with minimum perpendicular distance
159  // to the normal
160  float min_dist = std::numeric_limits<float>::max();
161 
162  // Find the best correspondence
163  for (std::size_t j = 0; j < nn_indices.size(); j++) {
164  float cos_angle = (*source_normals_)[idx_i].normal_x *
165  (*target_normals_)[nn_indices[j]].normal_x +
166  (*source_normals_)[idx_i].normal_y *
167  (*target_normals_)[nn_indices[j]].normal_y +
168  (*source_normals_)[idx_i].normal_z *
169  (*target_normals_)[nn_indices[j]].normal_z;
170  const float dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
171 
172  if (dist < min_dist) {
173  min_dist = dist;
174  min_index = static_cast<int>(j);
175  }
176  }
177  if (min_dist > max_distance)
178  continue;
179 
180  // Check if the correspondence is reciprocal
181  target_idx = nn_indices[min_index];
182  tree_reciprocal_->nearestKSearch(
183  detail::pointCopyOrRef<PointSource, PointTarget>(target_, target_idx),
184  1,
185  index_reciprocal,
186  distance_reciprocal);
187 
188  if (idx_i != index_reciprocal[0])
189  continue;
190 
191  corr.index_query = idx_i;
192  corr.index_match = nn_indices[min_index];
193  corr.distance = nn_dists[min_index]; // min_dist;
194  correspondences[nr_valid_correspondences++] = corr;
195  }
196  correspondences.resize(nr_valid_correspondences);
197  deinitCompute();
198 }
199 
200 } // namespace registration
201 } // namespace pcl
202 
203 #endif // PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_BACK_PROJECTION_HPP_
void determineCorrespondences(pcl::Correspondences &correspondences, const double max_distance=std::numeric_limits< double >::max())
Determine the correspondences between input and target cloud.
virtual void determineReciprocalCorrespondences(pcl::Correspondences &correspondences, const double max_distance=std::numeric_limits< double >::max())
Determine the reciprocal correspondences between input and target cloud.
Abstract CorrespondenceEstimationBase class.
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
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.