Point Cloud Library (PCL)  1.7.1
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 #ifndef PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_
41 #define PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_
42 
43 ///////////////////////////////////////////////////////////////////////////////////////////
44 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> bool
46 {
47  if (!source_normals_)
48  {
49  PCL_WARN ("[pcl::registration::%s::initCompute] Datasets containing normals for source have not been given!\n", getClassName ().c_str ());
50  return (false);
51  }
52 
54 }
55 
56 ///////////////////////////////////////////////////////////////////////////////////////////
57 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
59  pcl::Correspondences &correspondences, double max_distance)
60 {
61  if (!initCompute ())
62  return;
63 
64  typedef typename pcl::traits::fieldList<PointTarget>::type FieldListTarget;
65  correspondences.resize (indices_->size ());
66 
67  std::vector<int> nn_indices (k_);
68  std::vector<float> nn_dists (k_);
69 
70  double min_dist = std::numeric_limits<double>::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<double>::max ();
88 
89  // Find the best correspondence
90  for (size_t j = 0; j < nn_indices.size (); j++)
91  {
92  // computing the distance between a point and a line in 3d.
93  // Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
94  pt.x = target_->points[nn_indices[j]].x - input_->points[*idx_i].x;
95  pt.y = target_->points[nn_indices[j]].y - input_->points[*idx_i].y;
96  pt.z = target_->points[nn_indices[j]].z - input_->points[*idx_i].z;
97 
98  const NormalT &normal = source_normals_->points[*idx_i];
99  Eigen::Vector3d N (normal.normal_x, normal.normal_y, normal.normal_z);
100  Eigen::Vector3d V (pt.x, pt.y, pt.z);
101  Eigen::Vector3d C = N.cross (V);
102 
103  // Check if we have a better correspondence
104  double dist = C.dot (C);
105  if (dist < min_dist)
106  {
107  min_dist = dist;
108  min_index = static_cast<int> (j);
109  }
110  }
111  if (min_dist > max_distance)
112  continue;
113 
114  corr.index_query = *idx_i;
115  corr.index_match = nn_indices[min_index];
116  corr.distance = nn_dists[min_index];//min_dist;
117  correspondences[nr_valid_correspondences++] = corr;
118  }
119  }
120  else
121  {
122  PointTarget pt;
123 
124  // Iterate over the input set of source indices
125  for (std::vector<int>::const_iterator idx_i = indices_->begin (); idx_i != indices_->end (); ++idx_i)
126  {
127  tree_->nearestKSearch (input_->points[*idx_i], k_, nn_indices, nn_dists);
128 
129  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
130  min_dist = std::numeric_limits<double>::max ();
131 
132  // Find the best correspondence
133  for (size_t j = 0; j < nn_indices.size (); j++)
134  {
135  PointSource pt_src;
136  // Copy the source data to a target PointTarget format so we can search in the tree
137  pcl::for_each_type <FieldListTarget> (pcl::NdConcatenateFunctor <PointSource, PointTarget> (
138  input_->points[*idx_i],
139  pt_src));
140 
141  // computing the distance between a point and a line in 3d.
142  // Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
143  pt.x = target_->points[nn_indices[j]].x - pt_src.x;
144  pt.y = target_->points[nn_indices[j]].y - pt_src.y;
145  pt.z = target_->points[nn_indices[j]].z - pt_src.z;
146 
147  const NormalT &normal = source_normals_->points[*idx_i];
148  Eigen::Vector3d N (normal.normal_x, normal.normal_y, normal.normal_z);
149  Eigen::Vector3d V (pt.x, pt.y, pt.z);
150  Eigen::Vector3d C = N.cross (V);
151 
152  // Check if we have a better correspondence
153  double dist = C.dot (C);
154  if (dist < min_dist)
155  {
156  min_dist = dist;
157  min_index = static_cast<int> (j);
158  }
159  }
160  if (min_dist > max_distance)
161  continue;
162 
163  corr.index_query = *idx_i;
164  corr.index_match = nn_indices[min_index];
165  corr.distance = nn_dists[min_index];//min_dist;
166  correspondences[nr_valid_correspondences++] = corr;
167  }
168  }
169  correspondences.resize (nr_valid_correspondences);
170  deinitCompute ();
171 }
172 
173 ///////////////////////////////////////////////////////////////////////////////////////////
174 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar> void
176  pcl::Correspondences &correspondences, double max_distance)
177 {
178  if (!initCompute ())
179  return;
180 
181  typedef typename pcl::traits::fieldList<PointTarget>::type FieldListTarget;
182 
183  // setup tree for reciprocal search
184  // Set the internal point representation of choice
185  if (!initComputeReciprocal ())
186  return;
187 
188  correspondences.resize (indices_->size ());
189 
190  std::vector<int> nn_indices (k_);
191  std::vector<float> nn_dists (k_);
192  std::vector<int> index_reciprocal (1);
193  std::vector<float> distance_reciprocal (1);
194 
195  double min_dist = std::numeric_limits<double>::max ();
196  int min_index = 0;
197 
198  pcl::Correspondence corr;
199  unsigned int nr_valid_correspondences = 0;
200  int target_idx = 0;
201 
202  // Check if the template types are the same. If true, avoid a copy.
203  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT macro!
204  if (isSamePointType<PointSource, PointTarget> ())
205  {
206  PointTarget pt;
207  // Iterate over the input set of source indices
208  for (std::vector<int>::const_iterator idx_i = indices_->begin (); idx_i != indices_->end (); ++idx_i)
209  {
210  tree_->nearestKSearch (input_->points[*idx_i], k_, nn_indices, nn_dists);
211 
212  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
213  min_dist = std::numeric_limits<double>::max ();
214 
215  // Find the best correspondence
216  for (size_t j = 0; j < nn_indices.size (); j++)
217  {
218  // computing the distance between a point and a line in 3d.
219  // Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
220  pt.x = target_->points[nn_indices[j]].x - input_->points[*idx_i].x;
221  pt.y = target_->points[nn_indices[j]].y - input_->points[*idx_i].y;
222  pt.z = target_->points[nn_indices[j]].z - input_->points[*idx_i].z;
223 
224  const NormalT &normal = source_normals_->points[*idx_i];
225  Eigen::Vector3d N (normal.normal_x, normal.normal_y, normal.normal_z);
226  Eigen::Vector3d V (pt.x, pt.y, pt.z);
227  Eigen::Vector3d C = N.cross (V);
228 
229  // Check if we have a better correspondence
230  double dist = C.dot (C);
231  if (dist < min_dist)
232  {
233  min_dist = dist;
234  min_index = static_cast<int> (j);
235  }
236  }
237  if (min_dist > max_distance)
238  continue;
239 
240  // Check if the correspondence is reciprocal
241  target_idx = nn_indices[min_index];
242  tree_reciprocal_->nearestKSearch (target_->points[target_idx], 1, index_reciprocal, distance_reciprocal);
243 
244  if (*idx_i != index_reciprocal[0])
245  continue;
246 
247  // Correspondence IS reciprocal, save it and continue
248  corr.index_query = *idx_i;
249  corr.index_match = nn_indices[min_index];
250  corr.distance = nn_dists[min_index];//min_dist;
251  correspondences[nr_valid_correspondences++] = corr;
252  }
253  }
254  else
255  {
256  PointTarget pt;
257 
258  // Iterate over the input set of source indices
259  for (std::vector<int>::const_iterator idx_i = indices_->begin (); idx_i != indices_->end (); ++idx_i)
260  {
261  tree_->nearestKSearch (input_->points[*idx_i], k_, nn_indices, nn_dists);
262 
263  // Among the K nearest neighbours find the one with minimum perpendicular distance to the normal
264  min_dist = std::numeric_limits<double>::max ();
265 
266  // Find the best correspondence
267  for (size_t j = 0; j < nn_indices.size (); j++)
268  {
269  PointSource pt_src;
270  // Copy the source data to a target PointTarget format so we can search in the tree
271  pcl::for_each_type <FieldListTarget> (pcl::NdConcatenateFunctor <PointSource, PointTarget> (
272  input_->points[*idx_i],
273  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 #endif // PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_