dfbd16108db8b2fcc0ed5abb701473651c603e04
[pcl.git] /
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  * $Id$
37  *
38  */
39 #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_POINT_TO_PLANE_LLS_WEIGHTED_HPP_
40 #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_POINT_TO_PLANE_LLS_WEIGHTED_HPP_
41 #include <pcl/cloud_iterator.h>
42
43 //////////////////////////////////////////////////////////////////////////////////////////////
44 template <typename PointSource, typename PointTarget, typename Scalar> inline void
45 pcl::registration::TransformationEstimationPointToPlaneLLSWeighted<PointSource, PointTarget, Scalar>::
46 estimateRigidTransformation (const pcl::PointCloud<PointSource> &cloud_src,
47                              const pcl::PointCloud<PointTarget> &cloud_tgt,
48                              Matrix4 &transformation_matrix) const
49 {
50   std::size_t nr_points = cloud_src.points.size ();
51   if (cloud_tgt.points.size () != nr_points)
52   {
53     PCL_ERROR ("[pcl::TransformationEstimationPointToPlaneLLSWeighted::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", nr_points, cloud_tgt.points.size ());
54     return;
55   }
56
57   if (weights_.size () != nr_points)
58   {
59     PCL_ERROR ("[pcl::TransformationEstimationPointToPlaneLLSWeighted::estimateRigidTransformation] Number or weights from the number of correspondences! Use setWeights () to set them.\n");
60     return;
61   }
62
63   ConstCloudIterator<PointSource> source_it (cloud_src);
64   ConstCloudIterator<PointTarget> target_it (cloud_tgt);
65   typename std::vector<Scalar>::const_iterator weights_it = weights_.begin ();
66   estimateRigidTransformation (source_it, target_it, weights_it, transformation_matrix);
67 }
68
69 //////////////////////////////////////////////////////////////////////////////////////////////
70 template <typename PointSource, typename PointTarget, typename Scalar> void
71 pcl::registration::TransformationEstimationPointToPlaneLLSWeighted<PointSource, PointTarget, Scalar>::
72 estimateRigidTransformation (const pcl::PointCloud<PointSource> &cloud_src,
73                              const std::vector<int> &indices_src,
74                              const pcl::PointCloud<PointTarget> &cloud_tgt,
75                              Matrix4 &transformation_matrix) const
76 {
77   std::size_t nr_points = indices_src.size ();
78   if (cloud_tgt.points.size () != nr_points)
79   {
80     PCL_ERROR ("[pcl::TransformationEstimationPointToPlaneLLSWeighted::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), cloud_tgt.points.size ());
81     return;
82   }
83
84   if (weights_.size () != nr_points)
85   {
86     PCL_ERROR ("[pcl::TransformationEstimationPointToPlaneLLSWeighted::estimateRigidTransformation] Number or weights from the number of correspondences! Use setWeights () to set them.\n");
87     return;
88   }
89
90
91   ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
92   ConstCloudIterator<PointTarget> target_it (cloud_tgt);
93   typename std::vector<Scalar>::const_iterator weights_it = weights_.begin ();
94   estimateRigidTransformation (source_it, target_it, weights_it, transformation_matrix);
95 }
96
97
98 //////////////////////////////////////////////////////////////////////////////////////////////
99 template <typename PointSource, typename PointTarget, typename Scalar> inline void
100 pcl::registration::TransformationEstimationPointToPlaneLLSWeighted<PointSource, PointTarget, Scalar>::
101 estimateRigidTransformation (const pcl::PointCloud<PointSource> &cloud_src,
102                              const std::vector<int> &indices_src,
103                              const pcl::PointCloud<PointTarget> &cloud_tgt,
104                              const std::vector<int> &indices_tgt,
105                              Matrix4 &transformation_matrix) const
106 {
107   std::size_t nr_points = indices_src.size ();
108   if (indices_tgt.size () != nr_points)
109   {
110     PCL_ERROR ("[pcl::TransformationEstimationPointToPlaneLLSWeighted::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), indices_tgt.size ());
111     return;
112   }
113
114   if (weights_.size () != nr_points)
115   {
116     PCL_ERROR ("[pcl::TransformationEstimationPointToPlaneLLSWeighted::estimateRigidTransformation] Number or weights from the number of correspondences! Use setWeights () to set them.\n");
117     return;
118   }
119
120   ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
121   ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt);
122   typename std::vector<Scalar>::const_iterator weights_it = weights_.begin ();
123   estimateRigidTransformation (source_it, target_it, weights_it, transformation_matrix);
124 }
125
126 //////////////////////////////////////////////////////////////////////////////////////////////
127 template <typename PointSource, typename PointTarget, typename Scalar> inline void
128 pcl::registration::TransformationEstimationPointToPlaneLLSWeighted<PointSource, PointTarget, Scalar>::
129 estimateRigidTransformation (const pcl::PointCloud<PointSource> &cloud_src,
130                              const pcl::PointCloud<PointTarget> &cloud_tgt,
131                              const pcl::Correspondences &correspondences,
132                              Matrix4 &transformation_matrix) const
133 {
134   ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
135   ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
136   std::vector<Scalar> weights (correspondences.size ());
137   for (std::size_t i = 0; i < correspondences.size (); ++i)
138     weights[i] = correspondences[i].weight;
139   typename std::vector<Scalar>::const_iterator weights_it = weights.begin ();
140   estimateRigidTransformation (source_it, target_it, weights_it, transformation_matrix);
141 }
142
143 //////////////////////////////////////////////////////////////////////////////////////////////
144 template <typename PointSource, typename PointTarget, typename Scalar> inline void
145 pcl::registration::TransformationEstimationPointToPlaneLLSWeighted<PointSource, PointTarget, Scalar>::
146 constructTransformationMatrix (const double & alpha, const double & beta, const double & gamma,
147                                const double & tx,    const double & ty,   const double & tz,
148                                Matrix4 &transformation_matrix) const
149 {
150   // Construct the transformation matrix from rotation and translation 
151   transformation_matrix = Eigen::Matrix<Scalar, 4, 4>::Zero ();
152   transformation_matrix (0, 0) = static_cast<Scalar> ( std::cos (gamma) * std::cos (beta));
153   transformation_matrix (0, 1) = static_cast<Scalar> (-sin (gamma) * std::cos (alpha) + std::cos (gamma) * sin (beta) * sin (alpha));
154   transformation_matrix (0, 2) = static_cast<Scalar> ( sin (gamma) * sin (alpha) + std::cos (gamma) * sin (beta) * std::cos (alpha));
155   transformation_matrix (1, 0) = static_cast<Scalar> ( sin (gamma) * std::cos (beta));
156   transformation_matrix (1, 1) = static_cast<Scalar> ( std::cos (gamma) * std::cos (alpha) + sin (gamma) * sin (beta) * sin (alpha));
157   transformation_matrix (1, 2) = static_cast<Scalar> (-std::cos (gamma) * sin (alpha) + sin (gamma) * sin (beta) * std::cos (alpha));
158   transformation_matrix (2, 0) = static_cast<Scalar> (-sin (beta));
159   transformation_matrix (2, 1) = static_cast<Scalar> ( std::cos (beta) * sin (alpha));
160   transformation_matrix (2, 2) = static_cast<Scalar> ( std::cos (beta) * std::cos (alpha));
161
162   transformation_matrix (0, 3) = static_cast<Scalar> (tx);
163   transformation_matrix (1, 3) = static_cast<Scalar> (ty);
164   transformation_matrix (2, 3) = static_cast<Scalar> (tz);
165   transformation_matrix (3, 3) = static_cast<Scalar> (1);
166 }
167
168 //////////////////////////////////////////////////////////////////////////////////////////////
169 template <typename PointSource, typename PointTarget, typename Scalar> inline void
170 pcl::registration::TransformationEstimationPointToPlaneLLSWeighted<PointSource, PointTarget, Scalar>::
171 estimateRigidTransformation (ConstCloudIterator<PointSource>& source_it,
172                              ConstCloudIterator<PointTarget>& target_it,
173                              typename std::vector<Scalar>::const_iterator& weights_it,
174                              Matrix4 &transformation_matrix) const
175 {
176   using Vector6d = Eigen::Matrix<double, 6, 1>;
177   using Matrix6d = Eigen::Matrix<double, 6, 6>;
178
179   Matrix6d ATA;
180   Vector6d ATb;
181   ATA.setZero ();
182   ATb.setZero ();
183
184   while (source_it.isValid () && target_it.isValid ())
185   {
186     if (!std::isfinite (source_it->x) ||
187         !std::isfinite (source_it->y) ||
188         !std::isfinite (source_it->z) ||
189         !std::isfinite (target_it->x) ||
190         !std::isfinite (target_it->y) ||
191         !std::isfinite (target_it->z) ||
192         !std::isfinite (target_it->normal_x) ||
193         !std::isfinite (target_it->normal_y) ||
194         !std::isfinite (target_it->normal_z))
195     {
196       ++ source_it;
197       ++ target_it;
198       ++ weights_it;
199       continue;
200     }
201
202     const float & sx = source_it->x;
203     const float & sy = source_it->y;
204     const float & sz = source_it->z;
205     const float & dx = target_it->x;
206     const float & dy = target_it->y;
207     const float & dz = target_it->z;
208     const float & nx = target_it->normal[0] * (*weights_it);
209     const float & ny = target_it->normal[1] * (*weights_it);
210     const float & nz = target_it->normal[2] * (*weights_it);
211
212     double a = nz*sy - ny*sz;
213     double b = nx*sz - nz*sx;
214     double c = ny*sx - nx*sy;
215
216     //    0  1  2  3  4  5
217     //    6  7  8  9 10 11
218     //   12 13 14 15 16 17
219     //   18 19 20 21 22 23
220     //   24 25 26 27 28 29
221     //   30 31 32 33 34 35
222
223     ATA.coeffRef (0) += a * a;
224     ATA.coeffRef (1) += a * b;
225     ATA.coeffRef (2) += a * c;
226     ATA.coeffRef (3) += a * nx;
227     ATA.coeffRef (4) += a * ny;
228     ATA.coeffRef (5) += a * nz;
229     ATA.coeffRef (7) += b * b;
230     ATA.coeffRef (8) += b * c;
231     ATA.coeffRef (9) += b * nx;
232     ATA.coeffRef (10) += b * ny;
233     ATA.coeffRef (11) += b * nz;
234     ATA.coeffRef (14) += c * c;
235     ATA.coeffRef (15) += c * nx;
236     ATA.coeffRef (16) += c * ny;
237     ATA.coeffRef (17) += c * nz;
238     ATA.coeffRef (21) += nx * nx;
239     ATA.coeffRef (22) += nx * ny;
240     ATA.coeffRef (23) += nx * nz;
241     ATA.coeffRef (28) += ny * ny;
242     ATA.coeffRef (29) += ny * nz;
243     ATA.coeffRef (35) += nz * nz;
244
245     double d = nx*dx + ny*dy + nz*dz - nx*sx - ny*sy - nz*sz;
246     ATb.coeffRef (0) += a * d;
247     ATb.coeffRef (1) += b * d;
248     ATb.coeffRef (2) += c * d;
249     ATb.coeffRef (3) += nx * d;
250     ATb.coeffRef (4) += ny * d;
251     ATb.coeffRef (5) += nz * d;
252
253     ++ source_it;
254     ++ target_it;
255     ++ weights_it;
256   }
257
258   ATA.coeffRef (6) = ATA.coeff (1);
259   ATA.coeffRef (12) = ATA.coeff (2);
260   ATA.coeffRef (13) = ATA.coeff (8);
261   ATA.coeffRef (18) = ATA.coeff (3);
262   ATA.coeffRef (19) = ATA.coeff (9);
263   ATA.coeffRef (20) = ATA.coeff (15);
264   ATA.coeffRef (24) = ATA.coeff (4);
265   ATA.coeffRef (25) = ATA.coeff (10);
266   ATA.coeffRef (26) = ATA.coeff (16);
267   ATA.coeffRef (27) = ATA.coeff (22);
268   ATA.coeffRef (30) = ATA.coeff (5);
269   ATA.coeffRef (31) = ATA.coeff (11);
270   ATA.coeffRef (32) = ATA.coeff (17);
271   ATA.coeffRef (33) = ATA.coeff (23);
272   ATA.coeffRef (34) = ATA.coeff (29);
273
274   // Solve A*x = b
275   Vector6d x = static_cast<Vector6d> (ATA.inverse () * ATb);
276
277   // Construct the transformation matrix from x
278   constructTransformationMatrix (x (0), x (1), x (2), x (3), x (4), x (5), transformation_matrix);
279 }
280 #endif /* PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_POINT_TO_PLANE_LLS_WEIGHTED_HPP_ */