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