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