Point Cloud Library (PCL)  1.11.0
correspondence_estimation_normal_shooting.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, 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_NORMAL_SHOOTING_H_
42 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_
43 
44 #include <pcl/common/copy_point.h>
45 
46 
47 namespace pcl
48 {
49 
50 namespace registration
51 {
52 
53 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> bool
55 {
56  if (!source_normals_)
57  {
58  PCL_WARN ("[pcl::registration::%s::initCompute] Datasets containing normals for source have not been given!\n", getClassName ().c_str ());
59  return (false);
60  }
61 
63 }
64 
65 
66 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
68  pcl::Correspondences &correspondences, double max_distance)
69 {
70  if (!initCompute ())
71  return;
72 
73  correspondences.resize (indices_->size ());
74 
75  std::vector<int> nn_indices (k_);
76  std::vector<float> nn_dists (k_);
77 
78  int min_index = 0;
79 
81  unsigned int nr_valid_correspondences = 0;
82 
83  // Check if the template types are the same. If true, avoid a copy.
84  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT macro!
85  if (isSamePointType<PointSource, PointTarget> ())
86  {
87  PointTarget pt;
88  // Iterate over the input set of source indices
89  for (std::vector<int>::const_iterator idx_i = indices_->begin (); idx_i != indices_->end (); ++idx_i)
90  {
91  tree_->nearestKSearch (input_->points[*idx_i], k_, nn_indices, nn_dists);
92 
93  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
94  double min_dist = std::numeric_limits<double>::max ();
95 
96  // Find the best correspondence
97  for (std::size_t j = 0; j < nn_indices.size (); j++)
98  {
99  // computing the distance between a point and a line in 3d.
100  // Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
101  pt.x = target_->points[nn_indices[j]].x - input_->points[*idx_i].x;
102  pt.y = target_->points[nn_indices[j]].y - input_->points[*idx_i].y;
103  pt.z = target_->points[nn_indices[j]].z - input_->points[*idx_i].z;
104 
105  const NormalT &normal = source_normals_->points[*idx_i];
106  Eigen::Vector3d N (normal.normal_x, normal.normal_y, normal.normal_z);
107  Eigen::Vector3d V (pt.x, pt.y, pt.z);
108  Eigen::Vector3d C = N.cross (V);
109 
110  // Check if we have a better correspondence
111  double dist = C.dot (C);
112  if (dist < min_dist)
113  {
114  min_dist = dist;
115  min_index = static_cast<int> (j);
116  }
117  }
118  if (min_dist > max_distance)
119  continue;
120 
121  corr.index_query = *idx_i;
122  corr.index_match = nn_indices[min_index];
123  corr.distance = nn_dists[min_index];//min_dist;
124  correspondences[nr_valid_correspondences++] = corr;
125  }
126  }
127  else
128  {
129  PointTarget pt;
130 
131  // Iterate over the input set of source indices
132  for (std::vector<int>::const_iterator idx_i = indices_->begin (); idx_i != indices_->end (); ++idx_i)
133  {
134  tree_->nearestKSearch (input_->points[*idx_i], k_, nn_indices, nn_dists);
135 
136  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
137  double min_dist = std::numeric_limits<double>::max ();
138 
139  // Find the best correspondence
140  for (std::size_t j = 0; j < nn_indices.size (); j++)
141  {
142  PointSource pt_src;
143  // Copy the source data to a target PointTarget format so we can search in the tree
144  copyPoint (input_->points[*idx_i], pt_src);
145 
146  // computing the distance between a point and a line in 3d.
147  // Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
148  pt.x = target_->points[nn_indices[j]].x - pt_src.x;
149  pt.y = target_->points[nn_indices[j]].y - pt_src.y;
150  pt.z = target_->points[nn_indices[j]].z - pt_src.z;
151 
152  const NormalT &normal = source_normals_->points[*idx_i];
153  Eigen::Vector3d N (normal.normal_x, normal.normal_y, normal.normal_z);
154  Eigen::Vector3d V (pt.x, pt.y, pt.z);
155  Eigen::Vector3d C = N.cross (V);
156 
157  // Check if we have a better correspondence
158  double dist = C.dot (C);
159  if (dist < min_dist)
160  {
161  min_dist = dist;
162  min_index = static_cast<int> (j);
163  }
164  }
165  if (min_dist > max_distance)
166  continue;
167 
168  corr.index_query = *idx_i;
169  corr.index_match = nn_indices[min_index];
170  corr.distance = nn_dists[min_index];//min_dist;
171  correspondences[nr_valid_correspondences++] = corr;
172  }
173  }
174  correspondences.resize (nr_valid_correspondences);
175  deinitCompute ();
176 }
177 
178 
179 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
181  pcl::Correspondences &correspondences, double max_distance)
182 {
183  if (!initCompute ())
184  return;
185 
186  // setup tree for reciprocal search
187  // Set the internal point representation of choice
188  if (!initComputeReciprocal ())
189  return;
190 
191  correspondences.resize (indices_->size ());
192 
193  std::vector<int> nn_indices (k_);
194  std::vector<float> nn_dists (k_);
195  std::vector<int> index_reciprocal (1);
196  std::vector<float> distance_reciprocal (1);
197 
198  int min_index = 0;
199 
200  pcl::Correspondence corr;
201  unsigned int nr_valid_correspondences = 0;
202  int target_idx = 0;
203 
204  // Check if the template types are the same. If true, avoid a copy.
205  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT macro!
206  if (isSamePointType<PointSource, PointTarget> ())
207  {
208  PointTarget pt;
209  // Iterate over the input set of source indices
210  for (std::vector<int>::const_iterator idx_i = indices_->begin (); idx_i != indices_->end (); ++idx_i)
211  {
212  tree_->nearestKSearch (input_->points[*idx_i], k_, nn_indices, nn_dists);
213 
214  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
215  double min_dist = std::numeric_limits<double>::max ();
216 
217  // Find the best correspondence
218  for (std::size_t j = 0; j < nn_indices.size (); j++)
219  {
220  // computing the distance between a point and a line in 3d.
221  // Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
222  pt.x = target_->points[nn_indices[j]].x - input_->points[*idx_i].x;
223  pt.y = target_->points[nn_indices[j]].y - input_->points[*idx_i].y;
224  pt.z = target_->points[nn_indices[j]].z - input_->points[*idx_i].z;
225 
226  const NormalT &normal = source_normals_->points[*idx_i];
227  Eigen::Vector3d N (normal.normal_x, normal.normal_y, normal.normal_z);
228  Eigen::Vector3d V (pt.x, pt.y, pt.z);
229  Eigen::Vector3d C = N.cross (V);
230 
231  // Check if we have a better correspondence
232  double dist = C.dot (C);
233  if (dist < min_dist)
234  {
235  min_dist = dist;
236  min_index = static_cast<int> (j);
237  }
238  }
239  if (min_dist > max_distance)
240  continue;
241 
242  // Check if the correspondence is reciprocal
243  target_idx = nn_indices[min_index];
244  tree_reciprocal_->nearestKSearch (target_->points[target_idx], 1, index_reciprocal, distance_reciprocal);
245 
246  if (*idx_i != index_reciprocal[0])
247  continue;
248 
249  // Correspondence IS reciprocal, save it and continue
250  corr.index_query = *idx_i;
251  corr.index_match = nn_indices[min_index];
252  corr.distance = nn_dists[min_index];//min_dist;
253  correspondences[nr_valid_correspondences++] = corr;
254  }
255  }
256  else
257  {
258  PointTarget pt;
259 
260  // Iterate over the input set of source indices
261  for (std::vector<int>::const_iterator idx_i = indices_->begin (); idx_i != indices_->end (); ++idx_i)
262  {
263  tree_->nearestKSearch (input_->points[*idx_i], k_, nn_indices, nn_dists);
264 
265  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
266  double min_dist = std::numeric_limits<double>::max ();
267 
268  // Find the best correspondence
269  for (std::size_t j = 0; j < nn_indices.size (); j++)
270  {
271  PointSource pt_src;
272  // Copy the source data to a target PointTarget format so we can search in the tree
273  copyPoint (input_->points[*idx_i], pt_src);
274 
275  // computing the distance between a point and a line in 3d.
276  // Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
277  pt.x = target_->points[nn_indices[j]].x - pt_src.x;
278  pt.y = target_->points[nn_indices[j]].y - pt_src.y;
279  pt.z = target_->points[nn_indices[j]].z - pt_src.z;
280 
281  const NormalT &normal = source_normals_->points[*idx_i];
282  Eigen::Vector3d N (normal.normal_x, normal.normal_y, normal.normal_z);
283  Eigen::Vector3d V (pt.x, pt.y, pt.z);
284  Eigen::Vector3d C = N.cross (V);
285 
286  // Check if we have a better correspondence
287  double dist = C.dot (C);
288  if (dist < min_dist)
289  {
290  min_dist = dist;
291  min_index = static_cast<int> (j);
292  }
293  }
294  if (min_dist > max_distance)
295  continue;
296 
297  // Check if the correspondence is reciprocal
298  target_idx = nn_indices[min_index];
299  tree_reciprocal_->nearestKSearch (target_->points[target_idx], 1, index_reciprocal, distance_reciprocal);
300 
301  if (*idx_i != index_reciprocal[0])
302  continue;
303 
304  // Correspondence IS reciprocal, save it and continue
305  corr.index_query = *idx_i;
306  corr.index_match = nn_indices[min_index];
307  corr.distance = nn_dists[min_index];//min_dist;
308  correspondences[nr_valid_correspondences++] = corr;
309  }
310  }
311  correspondences.resize (nr_valid_correspondences);
312  deinitCompute ();
313 }
314 
315 } // namespace registration
316 } // namespace pcl
317 
318 #endif // PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_
A point structure representing normal coordinates and the surface curvature estimate.
void determineCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max()) override
Determine the correspondences between input and target cloud.
int index_match
Index of the matching (target) point.
void determineReciprocalCorrespondences(pcl::Correspondences &correspondences, double max_distance=std::numeric_limits< double >::max()) override
Determine the reciprocal correspondences between input and target cloud.
Correspondence represents a match between two entities (e.g., points, descriptors, etc).
int index_query
Index of the query (source) point.
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
void copyPoint(const PointInT &point_in, PointOutT &point_out)
Copy the fields of a source point into a target point.
Definition: copy_point.hpp:137
Abstract CorrespondenceEstimationBase class.