Point Cloud Library (PCL)  1.10.1
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 #ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_BACK_PROJECTION_HPP_
40 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_BACK_PROJECTION_HPP_
41 
42 #include <pcl/common/copy_point.h>
43 
44 ///////////////////////////////////////////////////////////////////////////////////////////
45 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> bool
47 {
48  if (!source_normals_ || !target_normals_)
49  {
50  PCL_WARN ("[pcl::registration::%s::initCompute] Datasets containing normals for source/target have not been given!\n", getClassName ().c_str ());
51  return (false);
52  }
53 
55 }
56 
57 ///////////////////////////////////////////////////////////////////////////////////////////
58 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
60  pcl::Correspondences &correspondences, double max_distance)
61 {
62  if (!initCompute ())
63  return;
64 
65  correspondences.resize (indices_->size ());
66 
67  std::vector<int> nn_indices (k_);
68  std::vector<float> nn_dists (k_);
69 
70  float min_dist = std::numeric_limits<float>::max ();
71  int min_index = 0;
72 
74  unsigned int nr_valid_correspondences = 0;
75 
76  // Check if the template types are the same. If true, avoid a copy.
77  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT macro!
78  if (isSamePointType<PointSource, PointTarget> ())
79  {
80  PointTarget pt;
81  // Iterate over the input set of source indices
82  for (std::vector<int>::const_iterator idx_i = indices_->begin (); idx_i != indices_->end (); ++idx_i)
83  {
84  tree_->nearestKSearch (input_->points[*idx_i], k_, nn_indices, nn_dists);
85 
86  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
87  min_dist = std::numeric_limits<float>::max ();
88 
89  // Find the best correspondence
90  for (std::size_t j = 0; j < nn_indices.size (); j++)
91  {
92  float cos_angle = source_normals_->points[*idx_i].normal_x * target_normals_->points[nn_indices[j]].normal_x +
93  source_normals_->points[*idx_i].normal_y * target_normals_->points[nn_indices[j]].normal_y +
94  source_normals_->points[*idx_i].normal_z * target_normals_->points[nn_indices[j]].normal_z ;
95  float dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
96 
97  if (dist < min_dist)
98  {
99  min_dist = dist;
100  min_index = static_cast<int> (j);
101  }
102  }
103  if (min_dist > max_distance)
104  continue;
105 
106  corr.index_query = *idx_i;
107  corr.index_match = nn_indices[min_index];
108  corr.distance = nn_dists[min_index];//min_dist;
109  correspondences[nr_valid_correspondences++] = corr;
110  }
111  }
112  else
113  {
114  PointTarget pt;
115 
116  // Iterate over the input set of source indices
117  for (std::vector<int>::const_iterator idx_i = indices_->begin (); idx_i != indices_->end (); ++idx_i)
118  {
119  tree_->nearestKSearch (input_->points[*idx_i], k_, nn_indices, nn_dists);
120 
121  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
122  min_dist = std::numeric_limits<float>::max ();
123 
124  // Find the best correspondence
125  for (std::size_t j = 0; j < nn_indices.size (); j++)
126  {
127  PointSource pt_src;
128  // Copy the source data to a target PointTarget format so we can search in the tree
129  copyPoint (input_->points[*idx_i], pt_src);
130 
131  float cos_angle = source_normals_->points[*idx_i].normal_x * target_normals_->points[nn_indices[j]].normal_x +
132  source_normals_->points[*idx_i].normal_y * target_normals_->points[nn_indices[j]].normal_y +
133  source_normals_->points[*idx_i].normal_z * target_normals_->points[nn_indices[j]].normal_z ;
134  float dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
135 
136  if (dist < min_dist)
137  {
138  min_dist = dist;
139  min_index = static_cast<int> (j);
140  }
141  }
142  if (min_dist > max_distance)
143  continue;
144 
145  corr.index_query = *idx_i;
146  corr.index_match = nn_indices[min_index];
147  corr.distance = nn_dists[min_index];//min_dist;
148  correspondences[nr_valid_correspondences++] = corr;
149  }
150  }
151  correspondences.resize (nr_valid_correspondences);
152  deinitCompute ();
153 }
154 
155 ///////////////////////////////////////////////////////////////////////////////////////////
156 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
158  pcl::Correspondences &correspondences, double max_distance)
159 {
160  if (!initCompute ())
161  return;
162 
163  // Set the internal point representation of choice
164  if(!initComputeReciprocal())
165  return;
166 
167  correspondences.resize (indices_->size ());
168 
169  std::vector<int> nn_indices (k_);
170  std::vector<float> nn_dists (k_);
171  std::vector<int> index_reciprocal (1);
172  std::vector<float> distance_reciprocal (1);
173 
174  float min_dist = std::numeric_limits<float>::max ();
175  int min_index = 0;
176 
177  pcl::Correspondence corr;
178  unsigned int nr_valid_correspondences = 0;
179  int target_idx = 0;
180 
181  // Check if the template types are the same. If true, avoid a copy.
182  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT macro!
183  if (isSamePointType<PointSource, PointTarget> ())
184  {
185  PointTarget pt;
186  // Iterate over the input set of source indices
187  for (std::vector<int>::const_iterator idx_i = indices_->begin (); idx_i != indices_->end (); ++idx_i)
188  {
189  tree_->nearestKSearch (input_->points[*idx_i], k_, nn_indices, nn_dists);
190 
191  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
192  min_dist = std::numeric_limits<float>::max ();
193 
194  // Find the best correspondence
195  for (std::size_t j = 0; j < nn_indices.size (); j++)
196  {
197  float cos_angle = source_normals_->points[*idx_i].normal_x * target_normals_->points[nn_indices[j]].normal_x +
198  source_normals_->points[*idx_i].normal_y * target_normals_->points[nn_indices[j]].normal_y +
199  source_normals_->points[*idx_i].normal_z * target_normals_->points[nn_indices[j]].normal_z ;
200  float dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
201 
202  if (dist < min_dist)
203  {
204  min_dist = dist;
205  min_index = static_cast<int> (j);
206  }
207  }
208  if (min_dist > max_distance)
209  continue;
210 
211  // Check if the correspondence is reciprocal
212  target_idx = nn_indices[min_index];
213  tree_reciprocal_->nearestKSearch (target_->points[target_idx], 1, index_reciprocal, distance_reciprocal);
214 
215  if (*idx_i != index_reciprocal[0])
216  continue;
217 
218  corr.index_query = *idx_i;
219  corr.index_match = nn_indices[min_index];
220  corr.distance = nn_dists[min_index];//min_dist;
221  correspondences[nr_valid_correspondences++] = corr;
222  }
223  }
224  else
225  {
226  PointTarget pt;
227 
228  // Iterate over the input set of source indices
229  for (std::vector<int>::const_iterator idx_i = indices_->begin (); idx_i != indices_->end (); ++idx_i)
230  {
231  tree_->nearestKSearch (input_->points[*idx_i], k_, nn_indices, nn_dists);
232 
233  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
234  min_dist = std::numeric_limits<float>::max ();
235 
236  // Find the best correspondence
237  for (std::size_t j = 0; j < nn_indices.size (); j++)
238  {
239  PointSource pt_src;
240  // Copy the source data to a target PointTarget format so we can search in the tree
241  copyPoint (input_->points[*idx_i], pt_src);
242 
243  float cos_angle = source_normals_->points[*idx_i].normal_x * target_normals_->points[nn_indices[j]].normal_x +
244  source_normals_->points[*idx_i].normal_y * target_normals_->points[nn_indices[j]].normal_y +
245  source_normals_->points[*idx_i].normal_z * target_normals_->points[nn_indices[j]].normal_z ;
246  float dist = nn_dists[j] * (2.0f - cos_angle * cos_angle);
247 
248  if (dist < min_dist)
249  {
250  min_dist = dist;
251  min_index = static_cast<int> (j);
252  }
253  }
254  if (min_dist > max_distance)
255  continue;
256 
257  // Check if the correspondence is reciprocal
258  target_idx = nn_indices[min_index];
259  tree_reciprocal_->nearestKSearch (target_->points[target_idx], 1, index_reciprocal, distance_reciprocal);
260 
261  if (*idx_i != index_reciprocal[0])
262  continue;
263 
264  corr.index_query = *idx_i;
265  corr.index_match = nn_indices[min_index];
266  corr.distance = nn_dists[min_index];//min_dist;
267  correspondences[nr_valid_correspondences++] = corr;
268  }
269  }
270  correspondences.resize (nr_valid_correspondences);
271  deinitCompute ();
272 }
273 
274 #endif // PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_BACK_PROJECTION_HPP_
pcl::Correspondence::index_match
int index_match
Index of the matching (target) point.
Definition: correspondence.h:64
pcl::Correspondence::distance
float distance
Definition: correspondence.h:68
pcl::registration::CorrespondenceEstimationBase
Abstract CorrespondenceEstimationBase class.
Definition: correspondence_estimation.h:62
pcl::copyPoint
void copyPoint(const PointInT &point_in, PointOutT &point_out)
Copy the fields of a source point into a target point.
Definition: copy_point.hpp:138
pcl::registration::CorrespondenceEstimationBackProjection::determineReciprocalCorrespondences
virtual void determineReciprocalCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max())
Determine the reciprocal correspondences between input and target cloud.
Definition: correspondence_estimation_backprojection.hpp:157
pcl::Correspondence::index_query
int index_query
Index of the query (source) point.
Definition: correspondence.h:62
pcl::registration::CorrespondenceEstimationBackProjection::determineCorrespondences
void determineCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max())
Determine the correspondences between input and target cloud.
Definition: correspondence_estimation_backprojection.hpp:59
pcl::Correspondences
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Definition: correspondence.h:88
pcl::Correspondence
Correspondence represents a match between two entities (e.g., points, descriptors,...
Definition: correspondence.h:59
pcl::registration::CorrespondenceEstimationBackProjection::initCompute
bool initCompute()
Internal computation initialization.
Definition: correspondence_estimation_backprojection.hpp:46