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