Point Cloud Library (PCL)  1.15.1-dev
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 namespace pcl {
47 
48 namespace registration {
49 
50 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
51 bool
54 {
55  if (!source_normals_) {
56  PCL_WARN("[pcl::registration::%s::initCompute] Datasets containing normals for "
57  "source have not been given!\n",
58  getClassName().c_str());
59  return (false);
60  }
61 
62  return (
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  PointTarget pt;
86  // Iterate over the input set of source indices
87  for (const auto& idx_i : (*indices_)) {
88  // Check if the template types are the same. If true, avoid a copy.
89  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT
90  // macro!
91  tree_->nearestKSearch(
92  detail::pointCopyOrRef<PointTarget, PointSource>(input_, idx_i),
93  k_,
94  nn_indices,
95  nn_dists);
96 
97  // Among the K nearest neighbours find the one with minimum perpendicular distance
98  // to the normal
99  double min_dist = std::numeric_limits<double>::max();
100 
101  // Find the best correspondence
102  for (std::size_t j = 0; j < nn_indices.size(); j++) {
103  // computing the distance between a point and a line in 3d.
104  // Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
105  pt.x = (*target_)[nn_indices[j]].x - (*input_)[idx_i].x;
106  pt.y = (*target_)[nn_indices[j]].y - (*input_)[idx_i].y;
107  pt.z = (*target_)[nn_indices[j]].z - (*input_)[idx_i].z;
108 
109  const NormalT& normal = (*source_normals_)[idx_i];
110  Eigen::Vector3d N(normal.normal_x, normal.normal_y, normal.normal_z);
111  Eigen::Vector3d V(pt.x, pt.y, pt.z);
112  Eigen::Vector3d C = N.cross(V);
113 
114  // Check if we have a better correspondence
115  const double dist = C.dot(C);
116  if (dist < min_dist) {
117  min_dist = dist;
118  min_index = static_cast<int>(j);
119  }
120  }
121  if (min_dist > max_distance)
122  continue;
123 
124  corr.index_query = idx_i;
125  corr.index_match = nn_indices[min_index];
126  corr.distance = nn_dists[min_index]; // min_dist;
127  correspondences[nr_valid_correspondences++] = corr;
128  }
129  correspondences.resize(nr_valid_correspondences);
130  deinitCompute();
131 }
132 
133 template <typename PointSource, typename PointTarget, typename NormalT, typename Scalar>
134 void
137  const double max_distance)
138 {
139  if (!initCompute())
140  return;
141 
142  // setup tree for reciprocal search
143  // Set the internal point representation of choice
144  if (!initComputeReciprocal())
145  return;
146 
147  correspondences.resize(indices_->size());
148 
149  pcl::Indices nn_indices(k_);
150  std::vector<float> nn_dists(k_);
151  pcl::Indices index_reciprocal(1);
152  std::vector<float> distance_reciprocal(1);
153 
154  int min_index = 0;
155 
156  pcl::Correspondence corr;
157  unsigned int nr_valid_correspondences = 0;
158  int target_idx = 0;
159 
160  PointTarget pt;
161  // Iterate over the input set of source indices
162  for (const auto& idx_i : (*indices_)) {
163  // Check if the template types are the same. If true, avoid a copy.
164  // Both point types MUST be registered using the POINT_CLOUD_REGISTER_POINT_STRUCT
165  // macro!
166  tree_->nearestKSearch(
167  detail::pointCopyOrRef<PointTarget, PointSource>(input_, idx_i),
168  k_,
169  nn_indices,
170  nn_dists);
171 
172  // Among the K nearest neighbours find the one with minimum perpendicular distance
173  // to the normal
174  double min_dist = std::numeric_limits<double>::max();
175 
176  // Find the best correspondence
177  for (std::size_t j = 0; j < nn_indices.size(); j++) {
178  // computing the distance between a point and a line in 3d.
179  // Reference - http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
180  pt.x = (*target_)[nn_indices[j]].x - (*input_)[idx_i].x;
181  pt.y = (*target_)[nn_indices[j]].y - (*input_)[idx_i].y;
182  pt.z = (*target_)[nn_indices[j]].z - (*input_)[idx_i].z;
183 
184  const NormalT& normal = (*source_normals_)[idx_i];
185  Eigen::Vector3d N(normal.normal_x, normal.normal_y, normal.normal_z);
186  Eigen::Vector3d V(pt.x, pt.y, pt.z);
187  Eigen::Vector3d C = N.cross(V);
188 
189  // Check if we have a better correspondence
190  const double dist = C.dot(C);
191  if (dist < min_dist) {
192  min_dist = dist;
193  min_index = static_cast<int>(j);
194  }
195  }
196  if (min_dist > max_distance)
197  continue;
198 
199  // Check if the correspondence is reciprocal
200  target_idx = nn_indices[min_index];
201  tree_reciprocal_->nearestKSearch(
202  detail::pointCopyOrRef<PointSource, PointTarget>(target_, target_idx),
203  1,
204  index_reciprocal,
205  distance_reciprocal);
206 
207  if (idx_i != index_reciprocal[0])
208  continue;
209 
210  // Correspondence IS reciprocal, save it and continue
211  corr.index_query = idx_i;
212  corr.index_match = nn_indices[min_index];
213  corr.distance = nn_dists[min_index]; // min_dist;
214  correspondences[nr_valid_correspondences++] = corr;
215  }
216  correspondences.resize(nr_valid_correspondences);
217  deinitCompute();
218 }
219 
220 } // namespace registration
221 } // namespace pcl
222 
223 #endif // PCL_REGISTRATION_IMPL_CORRESPONDENCE_ESTIMATION_NORMAL_SHOOTING_H_
Abstract CorrespondenceEstimationBase class.
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.
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.
A point structure representing normal coordinates and the surface curvature estimate.