Point Cloud Library (PCL)  1.7.1
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)
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 (%zu) differs than number of target indices (%zu)!\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  Eigen::Vector4f pt_tgt (target_->points[(*indices_tgt_)[i]].x,
94  target_->points[(*indices_tgt_)[i]].y,
95  target_->points[(*indices_tgt_)[i]].z, 1);
96 
97  Eigen::Vector4f p_tr (transform * pt_src);
98 
99  // Project the point on the image plane
100  Eigen::Vector3f p_tr3 (p_tr[0], p_tr[1], p_tr[2]);
101  Eigen::Vector3f uv (projection_matrix_ * p_tr3);
102 
103  if (uv[2] < 0)
104  continue;
105 
106  uv /= uv[2];
107 
108  // Calculate the distance from the transformed point to its correspondence
109  // need to compute the real norm here to keep MSAC and friends general
110  distances[i] = std::sqrt ((uv[0] - target_->points[(*indices_tgt_)[i]].u) *
111  (uv[0] - target_->points[(*indices_tgt_)[i]].u) +
112  (uv[1] - target_->points[(*indices_tgt_)[i]].v) *
113  (uv[1] - target_->points[(*indices_tgt_)[i]].v));
114  }
115 }
116 
117 //////////////////////////////////////////////////////////////////////////
118 template <typename PointT> void
119 pcl::SampleConsensusModelRegistration2D<PointT>::selectWithinDistance (const Eigen::VectorXf &model_coefficients, const double threshold, std::vector<int> &inliers)
120 {
121  if (indices_->size () != indices_tgt_->size ())
122  {
123  PCL_ERROR ("[pcl::SampleConsensusModelRegistration2D::selectWithinDistance] Number of source indices (%zu) differs than number of target indices (%zu)!\n", indices_->size (), indices_tgt_->size ());
124  inliers.clear ();
125  return;
126  }
127  if (!target_)
128  {
129  PCL_ERROR ("[pcl::SampleConsensusModelRegistration2D::selectWithinDistance] No target dataset given!\n");
130  return;
131  }
132 
133  double thresh = threshold * threshold;
134 
135  int nr_p = 0;
136  inliers.resize (indices_->size ());
137  error_sqr_dists_.resize (indices_->size ());
138 
139  Eigen::Matrix4f transform;
140  transform.row (0).matrix () = model_coefficients.segment<4>(0);
141  transform.row (1).matrix () = model_coefficients.segment<4>(4);
142  transform.row (2).matrix () = model_coefficients.segment<4>(8);
143  transform.row (3).matrix () = model_coefficients.segment<4>(12);
144 
145  for (size_t i = 0; i < indices_->size (); ++i)
146  {
147  Eigen::Vector4f pt_src (input_->points[(*indices_)[i]].x,
148  input_->points[(*indices_)[i]].y,
149  input_->points[(*indices_)[i]].z, 1);
150  Eigen::Vector4f pt_tgt (target_->points[(*indices_tgt_)[i]].x,
151  target_->points[(*indices_tgt_)[i]].y,
152  target_->points[(*indices_tgt_)[i]].z, 1);
153 
154  Eigen::Vector4f p_tr (transform * pt_src);
155 
156  // Project the point on the image plane
157  Eigen::Vector3f p_tr3 (p_tr[0], p_tr[1], p_tr[2]);
158  Eigen::Vector3f uv (projection_matrix_ * p_tr3);
159 
160  if (uv[2] < 0)
161  continue;
162 
163  uv /= uv[2];
164 
165  double distance = ((uv[0] - target_->points[(*indices_tgt_)[i]].u) *
166  (uv[0] - target_->points[(*indices_tgt_)[i]].u) +
167  (uv[1] - target_->points[(*indices_tgt_)[i]].v) *
168  (uv[1] - target_->points[(*indices_tgt_)[i]].v));
169 
170  // Calculate the distance from the transformed point to its correspondence
171  if (distance < thresh)
172  {
173  inliers[nr_p] = (*indices_)[i];
174  error_sqr_dists_[nr_p] = distance;
175  ++nr_p;
176  }
177  }
178  inliers.resize (nr_p);
179  error_sqr_dists_.resize (nr_p);
180 }
181 
182 //////////////////////////////////////////////////////////////////////////
183 template <typename PointT> int
185  const Eigen::VectorXf &model_coefficients, const double threshold)
186 {
187  if (indices_->size () != indices_tgt_->size ())
188  {
189  PCL_ERROR ("[pcl::SampleConsensusModelRegistration2D::countWithinDistance] Number of source indices (%zu) differs than number of target indices (%zu)!\n", indices_->size (), indices_tgt_->size ());
190  return (0);
191  }
192  if (!target_)
193  {
194  PCL_ERROR ("[pcl::SampleConsensusModelRegistration2D::countWithinDistance] No target dataset given!\n");
195  return (0);
196  }
197 
198  double thresh = threshold * threshold;
199 
200  Eigen::Matrix4f transform;
201  transform.row (0).matrix () = model_coefficients.segment<4>(0);
202  transform.row (1).matrix () = model_coefficients.segment<4>(4);
203  transform.row (2).matrix () = model_coefficients.segment<4>(8);
204  transform.row (3).matrix () = model_coefficients.segment<4>(12);
205 
206  int nr_p = 0;
207 
208  for (size_t i = 0; i < indices_->size (); ++i)
209  {
210  Eigen::Vector4f pt_src (input_->points[(*indices_)[i]].x,
211  input_->points[(*indices_)[i]].y,
212  input_->points[(*indices_)[i]].z, 1);
213  Eigen::Vector4f pt_tgt (target_->points[(*indices_tgt_)[i]].x,
214  target_->points[(*indices_tgt_)[i]].y,
215  target_->points[(*indices_tgt_)[i]].z, 1);
216 
217  Eigen::Vector4f p_tr (transform * pt_src);
218 
219  // Project the point on the image plane
220  Eigen::Vector3f p_tr3 (p_tr[0], p_tr[1], p_tr[2]);
221  Eigen::Vector3f uv (projection_matrix_ * p_tr3);
222 
223  if (uv[2] < 0)
224  continue;
225 
226  uv /= uv[2];
227 
228  // Calculate the distance from the transformed point to its correspondence
229  if (((uv[0] - target_->points[(*indices_tgt_)[i]].u) *
230  (uv[0] - target_->points[(*indices_tgt_)[i]].u) +
231  (uv[1] - target_->points[(*indices_tgt_)[i]].v) *
232  (uv[1] - target_->points[(*indices_tgt_)[i]].v)) < thresh)
233  ++nr_p;
234  }
235  return (nr_p);
236 }
237 
238 #endif // PCL_SAMPLE_CONSENSUS_IMPL_SAC_MODEL_REGISTRATION_2D_HPP_
239 
virtual int countWithinDistance(const Eigen::VectorXf &model_coefficients, const double threshold)
Count all the points which respect the given model coefficients as inliers.
void getDistancesToModel(const Eigen::VectorXf &model_coefficients, std::vector< double > &distances)
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.
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.