Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sac_model_registration.hpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2011, Willow Garage, 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 Willow Garage, Inc. 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: sac_model_registration.hpp 5026 2012-03-12 02:51:44Z rusu $
37  *
38  */
39 
40 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_REGISTRATION_H_
41 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_REGISTRATION_H_
42 
44 
46 template <typename PointT> bool
47 pcl::SampleConsensusModelRegistration<PointT>::isSampleGood (const std::vector<int> &samples) const
48 {
49  return ((input_->points[samples[1]].getArray4fMap () - input_->points[samples[0]].getArray4fMap ()).matrix ().squaredNorm () > sample_dist_thresh_ &&
50  (input_->points[samples[2]].getArray4fMap () - input_->points[samples[0]].getArray4fMap ()).matrix ().squaredNorm () > sample_dist_thresh_ &&
51  (input_->points[samples[2]].getArray4fMap () - input_->points[samples[1]].getArray4fMap ()).matrix ().squaredNorm () > sample_dist_thresh_);
52 }
53 
55 template <typename PointT> bool
56 pcl::SampleConsensusModelRegistration<PointT>::computeModelCoefficients (const std::vector<int> &samples, Eigen::VectorXf &model_coefficients)
57 {
58  if (!target_)
59  {
60  PCL_ERROR ("[pcl::SampleConsensusModelRegistration::computeModelCoefficients] No target dataset given!\n");
61  return (false);
62  }
63  // Need 3 samples
64  if (samples.size () != 3)
65  return (false);
66 
67  std::vector<int> indices_tgt (3);
68  for (int i = 0; i < 3; ++i)
69  indices_tgt[i] = correspondences_[samples[i]];
70 
71  estimateRigidTransformationSVD (*input_, samples, *target_, indices_tgt, model_coefficients);
72  return (true);
73 }
74 
76 template <typename PointT> void
77 pcl::SampleConsensusModelRegistration<PointT>::getDistancesToModel (const Eigen::VectorXf &model_coefficients, std::vector<double> &distances)
78 {
79  if (indices_->size () != indices_tgt_->size ())
80  {
81  PCL_ERROR ("[pcl::SampleConsensusModelRegistration::getDistancesToModel] Number of source indices (%zu) differs than number of target indices (%zu)!\n", indices_->size (), indices_tgt_->size ());
82  distances.clear ();
83  return;
84  }
85  if (!target_)
86  {
87  PCL_ERROR ("[pcl::SampleConsensusModelRegistration::getDistanceToModel] No target dataset given!\n");
88  return;
89  }
90  // Check if the model is valid given the user constraints
91  if (!isModelValid (model_coefficients))
92  {
93  distances.clear ();
94  return;
95  }
96  distances.resize (indices_->size ());
97 
98  // Get the 4x4 transformation
99  Eigen::Matrix4f transform;
100  transform.row (0) = model_coefficients.segment<4>(0);
101  transform.row (1) = model_coefficients.segment<4>(4);
102  transform.row (2) = model_coefficients.segment<4>(8);
103  transform.row (3) = model_coefficients.segment<4>(12);
104 
105  for (size_t i = 0; i < indices_->size (); ++i)
106  {
107  Eigen::Vector4f pt_src = input_->points[(*indices_)[i]].getVector4fMap ();
108  pt_src[3] = 1;
109  Eigen::Vector4f pt_tgt = target_->points[(*indices_tgt_)[i]].getVector4fMap ();
110  pt_tgt[3] = 1;
111 
112  Eigen::Vector4f p_tr (transform * pt_src);
113  // Calculate the distance from the transformed point to its correspondence
114  // need to compute the real norm here to keep MSAC and friends general
115  distances[i] = (p_tr - pt_tgt).norm ();
116  }
117 }
118 
120 template <typename PointT> void
121 pcl::SampleConsensusModelRegistration<PointT>::selectWithinDistance (const Eigen::VectorXf &model_coefficients, const double threshold, std::vector<int> &inliers)
122 {
123  if (indices_->size () != indices_tgt_->size ())
124  {
125  PCL_ERROR ("[pcl::SampleConsensusModelRegistration::selectWithinDistance] Number of source indices (%zu) differs than number of target indices (%zu)!\n", indices_->size (), indices_tgt_->size ());
126  inliers.clear ();
127  return;
128  }
129  if (!target_)
130  {
131  PCL_ERROR ("[pcl::SampleConsensusModelRegistration::selectWithinDistance] No target dataset given!\n");
132  return;
133  }
134 
135  double thresh = threshold * threshold;
136 
137  // Check if the model is valid given the user constraints
138  if (!isModelValid (model_coefficients))
139  {
140  inliers.clear ();
141  return;
142  }
143 
144  inliers.resize (indices_->size ());
145 
146  Eigen::Matrix4f transform;
147  transform.row (0) = model_coefficients.segment<4>(0);
148  transform.row (1) = model_coefficients.segment<4>(4);
149  transform.row (2) = model_coefficients.segment<4>(8);
150  transform.row (3) = model_coefficients.segment<4>(12);
151 
152  int nr_p = 0;
153  for (size_t i = 0; i < indices_->size (); ++i)
154  {
155  Eigen::Vector4f pt_src = input_->points[(*indices_)[i]].getVector4fMap ();
156  pt_src[3] = 1;
157  Eigen::Vector4f pt_tgt = target_->points[(*indices_tgt_)[i]].getVector4fMap ();
158  pt_tgt[3] = 1;
159 
160  Eigen::Vector4f p_tr (transform * pt_src);
161  // Calculate the distance from the transformed point to its correspondence
162  if ((p_tr - pt_tgt).squaredNorm () < thresh)
163  inliers[nr_p++] = (*indices_)[i];
164  }
165  inliers.resize (nr_p);
166 }
167 
169 template <typename PointT> int
171  const Eigen::VectorXf &model_coefficients, const double threshold)
172 {
173  if (indices_->size () != indices_tgt_->size ())
174  {
175  PCL_ERROR ("[pcl::SampleConsensusModelRegistration::countWithinDistance] Number of source indices (%zu) differs than number of target indices (%zu)!\n", indices_->size (), indices_tgt_->size ());
176  return (0);
177  }
178  if (!target_)
179  {
180  PCL_ERROR ("[pcl::SampleConsensusModelRegistration::countWithinDistance] No target dataset given!\n");
181  return (0);
182  }
183 
184  double thresh = threshold * threshold;
185 
186  // Check if the model is valid given the user constraints
187  if (!isModelValid (model_coefficients))
188  return (0);
189 
190  Eigen::Matrix4f transform;
191  transform.row (0) = model_coefficients.segment<4>(0);
192  transform.row (1) = model_coefficients.segment<4>(4);
193  transform.row (2) = model_coefficients.segment<4>(8);
194  transform.row (3) = model_coefficients.segment<4>(12);
195 
196  int nr_p = 0;
197  for (size_t i = 0; i < indices_->size (); ++i)
198  {
199  Eigen::Vector4f pt_src = input_->points[(*indices_)[i]].getVector4fMap ();
200  pt_src[3] = 1;
201  Eigen::Vector4f pt_tgt = target_->points[(*indices_tgt_)[i]].getVector4fMap ();
202  pt_tgt[3] = 1;
203 
204  Eigen::Vector4f p_tr (transform * pt_src);
205  // Calculate the distance from the transformed point to its correspondence
206  if ((p_tr - pt_tgt).squaredNorm () < thresh)
207  nr_p++;
208  }
209  return (nr_p);
210 }
211 
213 template <typename PointT> void
214 pcl::SampleConsensusModelRegistration<PointT>::optimizeModelCoefficients (const std::vector<int> &inliers, const Eigen::VectorXf &model_coefficients, Eigen::VectorXf &optimized_coefficients)
215 {
216  if (indices_->size () != indices_tgt_->size ())
217  {
218  PCL_ERROR ("[pcl::SampleConsensusModelRegistration::optimizeModelCoefficients] Number of source indices (%zu) differs than number of target indices (%zu)!\n", indices_->size (), indices_tgt_->size ());
219  optimized_coefficients = model_coefficients;
220  return;
221  }
222 
223  // Check if the model is valid given the user constraints
224  if (!isModelValid (model_coefficients) || !target_)
225  {
226  optimized_coefficients = model_coefficients;
227  return;
228  }
229 
230  std::vector<int> indices_src (inliers.size ());
231  std::vector<int> indices_tgt (inliers.size ());
232  for (size_t i = 0; i < inliers.size (); ++i)
233  {
234  // NOTE: not tested!
235  indices_src[i] = (*indices_)[inliers[i]];
236  indices_tgt[i] = (*indices_tgt_)[inliers[i]];
237  }
238 
239  estimateRigidTransformationSVD (*input_, indices_src, *target_, indices_tgt, optimized_coefficients);
240 }
241 
243 template <typename PointT> void
245  const pcl::PointCloud<PointT> &cloud_src,
246  const std::vector<int> &indices_src,
247  const pcl::PointCloud<PointT> &cloud_tgt,
248  const std::vector<int> &indices_tgt,
249  Eigen::VectorXf &transform)
250 {
251  transform.resize (16);
252  Eigen::Vector4f centroid_src, centroid_tgt;
253  // Estimate the centroids of source, target
254  compute3DCentroid (cloud_src, indices_src, centroid_src);
255  compute3DCentroid (cloud_tgt, indices_tgt, centroid_tgt);
256 
257  // Subtract the centroids from source, target
258  Eigen::MatrixXf cloud_src_demean;
259  demeanPointCloud (cloud_src, indices_src, centroid_src, cloud_src_demean);
260 
261  Eigen::MatrixXf cloud_tgt_demean;
262  demeanPointCloud (cloud_tgt, indices_tgt, centroid_tgt, cloud_tgt_demean);
263 
264  // Assemble the correlation matrix H = source * target'
265  Eigen::Matrix3f H = (cloud_src_demean * cloud_tgt_demean.transpose ()).topLeftCorner<3, 3>();
266 
267  // Compute the Singular Value Decomposition
268  Eigen::JacobiSVD<Eigen::Matrix3f> svd (H, Eigen::ComputeFullU | Eigen::ComputeFullV);
269  Eigen::Matrix3f u = svd.matrixU ();
270  Eigen::Matrix3f v = svd.matrixV ();
271 
272  // Compute R = V * U'
273  if (u.determinant () * v.determinant () < 0)
274  {
275  for (int x = 0; x < 3; ++x)
276  v (x, 2) *= -1;
277  }
278 
279  Eigen::Matrix3f R = v * u.transpose ();
280 
281  // Return the correct transformation
282  transform.segment<3> (0) = R.row (0); transform[12] = 0;
283  transform.segment<3> (4) = R.row (1); transform[13] = 0;
284  transform.segment<3> (8) = R.row (2); transform[14] = 0;
285 
286  Eigen::Vector3f t = centroid_tgt.head<3> () - R * centroid_src.head<3> ();
287  transform[3] = t[0]; transform[7] = t[1]; transform[11] = t[2]; transform[15] = 1.0;
288 }
289 
290 #define PCL_INSTANTIATE_SampleConsensusModelRegistration(T) template class PCL_EXPORTS pcl::SampleConsensusModelRegistration<T>;
291 
292 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_REGISTRATION_H_
293