Point Cloud Library (PCL)  1.11.0
sac_model_registration_2d.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, 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 the copyright holder(s) 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  */
37 
38 #ifndef PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_REGISTRATION_2D_HPP_
39 #define PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_REGISTRATION_2D_HPP_
40 
41 #include <pcl/sample_consensus/sac_model_registration_2d.h>
42 #include <pcl/common/eigen.h>
43 
44 //////////////////////////////////////////////////////////////////////////
45 template <typename PointT> bool
47 {
48  if (samples.size () != sample_size_)
49  {
50  PCL_ERROR ("[pcl::SampleConsensusModelRegistration2D::isSampleGood] Wrong number of samples (is %lu, should be %lu)!\n", samples.size (), sample_size_);
51  return (false);
52  }
53  return (true);
54  //using namespace pcl::common;
55  //using namespace pcl::traits;
56 
57  //PointT p10 = input_->points[samples[1]] - input_->points[samples[0]];
58  //PointT p20 = input_->points[samples[2]] - input_->points[samples[0]];
59  //PointT p21 = input_->points[samples[2]] - input_->points[samples[1]];
60 
61  //return ((p10.x * p10.x + p10.y * p10.y + p10.z * p10.z) > sample_dist_thresh_ &&
62  // (p20.x * p20.x + p20.y * p20.y + p20.z * p20.z) > sample_dist_thresh_ &&
63  // (p21.x * p21.x + p21.y * p21.y + p21.z * p21.z) > sample_dist_thresh_);
64 }
65 
66 //////////////////////////////////////////////////////////////////////////
67 template <typename PointT> void
68 pcl::SampleConsensusModelRegistration2D<PointT>::getDistancesToModel (const Eigen::VectorXf &model_coefficients, std::vector<double> &distances) const
69 {
70  PCL_INFO ("[pcl::SampleConsensusModelRegistration2D<PointT>::getDistancesToModel]\n");
71  if (indices_->size () != indices_tgt_->size ())
72  {
73  PCL_ERROR ("[pcl::SampleConsensusModelRegistration2D::getDistancesToModel] Number of source indices (%lu) differs than number of target indices (%lu)!\n", indices_->size (), indices_tgt_->size ());
74  distances.clear ();
75  return;
76  }
77  if (!target_)
78  {
79  PCL_ERROR ("[pcl::SampleConsensusModelRegistration2D::getDistanceToModel] No target dataset given!\n");
80  return;
81  }
82 
83  distances.resize (indices_->size ());
84 
85  // Get the 4x4 transformation
86  Eigen::Matrix4f transform;
87  transform.row (0).matrix () = model_coefficients.segment<4>(0);
88  transform.row (1).matrix () = model_coefficients.segment<4>(4);
89  transform.row (2).matrix () = model_coefficients.segment<4>(8);
90  transform.row (3).matrix () = model_coefficients.segment<4>(12);
91 
92  for (std::size_t i = 0; i < indices_->size (); ++i)
93  {
94  Eigen::Vector4f pt_src (input_->points[(*indices_)[i]].x,
95  input_->points[(*indices_)[i]].y,
96  input_->points[(*indices_)[i]].z, 1.0f);
97 
98  Eigen::Vector4f p_tr (transform * pt_src);
99 
100  // Project the point on the image plane
101  Eigen::Vector3f p_tr3 (p_tr[0], p_tr[1], p_tr[2]);
102  Eigen::Vector3f uv (projection_matrix_ * p_tr3);
103 
104  if (uv[2] < 0.0f)
105  {
106  continue;
107  }
108 
109  uv /= uv[2];
110 
111  // Calculate the distance from the transformed point to its correspondence
112  // need to compute the real norm here to keep MSAC and friends general
113  distances[i] = std::sqrt ((uv[0] - target_->points[(*indices_tgt_)[i]].u) *
114  (uv[0] - target_->points[(*indices_tgt_)[i]].u) +
115  (uv[1] - target_->points[(*indices_tgt_)[i]].v) *
116  (uv[1] - target_->points[(*indices_tgt_)[i]].v));
117  }
118 }
119 
120 //////////////////////////////////////////////////////////////////////////
121 template <typename PointT> void
122 pcl::SampleConsensusModelRegistration2D<PointT>::selectWithinDistance (const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers)
123 {
124  if (indices_->size () != indices_tgt_->size ())
125  {
126  PCL_ERROR ("[pcl::SampleConsensusModelRegistration2D::selectWithinDistance] Number of source indices (%lu) differs than number of target indices (%lu)!\n", indices_->size (), indices_tgt_->size ());
127  inliers.clear ();
128  return;
129  }
130  if (!target_)
131  {
132  PCL_ERROR ("[pcl::SampleConsensusModelRegistration2D::selectWithinDistance] No target dataset given!\n");
133  return;
134  }
135 
136  double thresh = threshold * threshold;
137 
138  inliers.clear ();
139  error_sqr_dists_.clear ();
140  inliers.reserve (indices_->size ());
141  error_sqr_dists_.reserve (indices_->size ());
142 
143  Eigen::Matrix4f transform;
144  transform.row (0).matrix () = model_coefficients.segment<4>(0);
145  transform.row (1).matrix () = model_coefficients.segment<4>(4);
146  transform.row (2).matrix () = model_coefficients.segment<4>(8);
147  transform.row (3).matrix () = model_coefficients.segment<4>(12);
148 
149  for (std::size_t i = 0; i < indices_->size (); ++i)
150  {
151  Eigen::Vector4f pt_src (input_->points[(*indices_)[i]].x,
152  input_->points[(*indices_)[i]].y,
153  input_->points[(*indices_)[i]].z, 1.0f);
154 
155  Eigen::Vector4f p_tr (transform * pt_src);
156 
157  // Project the point on the image plane
158  Eigen::Vector3f p_tr3 (p_tr[0], p_tr[1], p_tr[2]);
159  Eigen::Vector3f uv (projection_matrix_ * p_tr3);
160 
161  if (uv[2] < 0.0f)
162  continue;
163 
164  uv /= uv[2];
165 
166  double distance = ((uv[0] - target_->points[(*indices_tgt_)[i]].u) *
167  (uv[0] - target_->points[(*indices_tgt_)[i]].u) +
168  (uv[1] - target_->points[(*indices_tgt_)[i]].v) *
169  (uv[1] - target_->points[(*indices_tgt_)[i]].v));
170 
171  // Calculate the distance from the transformed point to its correspondence
172  if (distance < thresh)
173  {
174  inliers.push_back ((*indices_)[i]);
175  error_sqr_dists_.push_back (distance);
176  }
177  }
178 }
179 
180 //////////////////////////////////////////////////////////////////////////
181 template <typename PointT> std::size_t
183  const Eigen::VectorXf &model_coefficients, const double threshold) const
184 {
185  if (indices_->size () != indices_tgt_->size ())
186  {
187  PCL_ERROR ("[pcl::SampleConsensusModelRegistration2D::countWithinDistance] Number of source indices (%lu) differs than number of target indices (%lu)!\n", indices_->size (), indices_tgt_->size ());
188  return (0);
189  }
190  if (!target_)
191  {
192  PCL_ERROR ("[pcl::SampleConsensusModelRegistration2D::countWithinDistance] No target dataset given!\n");
193  return (0);
194  }
195 
196  double thresh = threshold * threshold;
197 
198  Eigen::Matrix4f transform;
199  transform.row (0).matrix () = model_coefficients.segment<4>(0);
200  transform.row (1).matrix () = model_coefficients.segment<4>(4);
201  transform.row (2).matrix () = model_coefficients.segment<4>(8);
202  transform.row (3).matrix () = model_coefficients.segment<4>(12);
203 
204  std::size_t nr_p = 0;
205 
206  for (std::size_t i = 0; i < indices_->size (); ++i)
207  {
208  Eigen::Vector4f pt_src (input_->points[(*indices_)[i]].x,
209  input_->points[(*indices_)[i]].y,
210  input_->points[(*indices_)[i]].z, 1.0f);
211 
212  Eigen::Vector4f p_tr (transform * pt_src);
213 
214  // Project the point on the image plane
215  Eigen::Vector3f p_tr3 (p_tr[0], p_tr[1], p_tr[2]);
216  Eigen::Vector3f uv (projection_matrix_ * p_tr3);
217 
218  if (uv[2] < 0.0f)
219  {
220  continue;
221  }
222 
223  uv /= uv[2];
224 
225  // Calculate the distance from the transformed point to its correspondence
226  if (((uv[0] - target_->points[(*indices_tgt_)[i]].u) *
227  (uv[0] - target_->points[(*indices_tgt_)[i]].u) +
228  (uv[1] - target_->points[(*indices_tgt_)[i]].v) *
229  (uv[1] - target_->points[(*indices_tgt_)[i]].v)) < thresh)
230  {
231  ++nr_p;
232  }
233  }
234  return (nr_p);
235 }
236 
237 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_REGISTRATION_2D_HPP_
238 
void selectWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold, Indices &inliers)
Select all the points which respect the given model coefficients as inliers.
std::vector< index_t > Indices
Type used for indices in PCL.
Definition: types.h:142
bool isSampleGood(const Indices &samples) const
Check if a sample of indices results in a good sample of points indices.
virtual std::size_t countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold) const
Count all the points which respect the given model coefficients as inliers.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances) const
Compute all distances from the transformed points to their correspondences.