Point Cloud Library (PCL)  1.7.1
eigen.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR a PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
39 #ifndef PCL_COMMON_EIGEN_IMPL_HPP_
40 #define PCL_COMMON_EIGEN_IMPL_HPP_
41 
42 #include <pcl/pcl_macros.h>
43 
44 //////////////////////////////////////////////////////////////////////////////////////////
45 void
46 pcl::getTransFromUnitVectorsZY (const Eigen::Vector3f& z_axis,
47  const Eigen::Vector3f& y_direction,
48  Eigen::Affine3f& transformation)
49 {
50  Eigen::Vector3f tmp0 = (y_direction.cross(z_axis)).normalized();
51  Eigen::Vector3f tmp1 = (z_axis.cross(tmp0)).normalized();
52  Eigen::Vector3f tmp2 = z_axis.normalized();
53 
54  transformation(0,0)=tmp0[0]; transformation(0,1)=tmp0[1]; transformation(0,2)=tmp0[2]; transformation(0,3)=0.0f;
55  transformation(1,0)=tmp1[0]; transformation(1,1)=tmp1[1]; transformation(1,2)=tmp1[2]; transformation(1,3)=0.0f;
56  transformation(2,0)=tmp2[0]; transformation(2,1)=tmp2[1]; transformation(2,2)=tmp2[2]; transformation(2,3)=0.0f;
57  transformation(3,0)=0.0f; transformation(3,1)=0.0f; transformation(3,2)=0.0f; transformation(3,3)=1.0f;
58 }
59 
60 //////////////////////////////////////////////////////////////////////////////////////////
61 Eigen::Affine3f
62 pcl::getTransFromUnitVectorsZY (const Eigen::Vector3f& z_axis,
63  const Eigen::Vector3f& y_direction)
64 {
65  Eigen::Affine3f transformation;
66  getTransFromUnitVectorsZY (z_axis, y_direction, transformation);
67  return (transformation);
68 }
69 
70 //////////////////////////////////////////////////////////////////////////////////////////
71 void
72 pcl::getTransFromUnitVectorsXY (const Eigen::Vector3f& x_axis,
73  const Eigen::Vector3f& y_direction,
74  Eigen::Affine3f& transformation)
75 {
76  Eigen::Vector3f tmp2 = (x_axis.cross(y_direction)).normalized();
77  Eigen::Vector3f tmp1 = (tmp2.cross(x_axis)).normalized();
78  Eigen::Vector3f tmp0 = x_axis.normalized();
79 
80  transformation(0,0)=tmp0[0]; transformation(0,1)=tmp0[1]; transformation(0,2)=tmp0[2]; transformation(0,3)=0.0f;
81  transformation(1,0)=tmp1[0]; transformation(1,1)=tmp1[1]; transformation(1,2)=tmp1[2]; transformation(1,3)=0.0f;
82  transformation(2,0)=tmp2[0]; transformation(2,1)=tmp2[1]; transformation(2,2)=tmp2[2]; transformation(2,3)=0.0f;
83  transformation(3,0)=0.0f; transformation(3,1)=0.0f; transformation(3,2)=0.0f; transformation(3,3)=1.0f;
84 }
85 
86 //////////////////////////////////////////////////////////////////////////////////////////
87 Eigen::Affine3f
88 pcl::getTransFromUnitVectorsXY (const Eigen::Vector3f& x_axis,
89  const Eigen::Vector3f& y_direction)
90 {
91  Eigen::Affine3f transformation;
92  getTransFromUnitVectorsXY (x_axis, y_direction, transformation);
93  return (transformation);
94 }
95 
96 //////////////////////////////////////////////////////////////////////////////////////////
97 void
98 pcl::getTransformationFromTwoUnitVectors (const Eigen::Vector3f& y_direction,
99  const Eigen::Vector3f& z_axis,
100  Eigen::Affine3f& transformation)
101 {
102  getTransFromUnitVectorsZY (z_axis, y_direction, transformation);
103 }
104 
105 //////////////////////////////////////////////////////////////////////////////////////////
106 Eigen::Affine3f
107 pcl::getTransformationFromTwoUnitVectors (const Eigen::Vector3f& y_direction,
108  const Eigen::Vector3f& z_axis)
109 {
110  Eigen::Affine3f transformation;
111  getTransformationFromTwoUnitVectors (y_direction, z_axis, transformation);
112  return (transformation);
113 }
114 
115 void
116 pcl::getTransformationFromTwoUnitVectorsAndOrigin (const Eigen::Vector3f& y_direction,
117  const Eigen::Vector3f& z_axis,
118  const Eigen::Vector3f& origin,
119  Eigen::Affine3f& transformation)
120 {
121  getTransformationFromTwoUnitVectors(y_direction, z_axis, transformation);
122  Eigen::Vector3f translation = transformation*origin;
123  transformation(0,3)=-translation[0]; transformation(1,3)=-translation[1]; transformation(2,3)=-translation[2];
124 }
125 
126 //////////////////////////////////////////////////////////////////////////////////////////
127 void
128 pcl::getEulerAngles (const Eigen::Affine3f& t, float& roll, float& pitch, float& yaw)
129 {
130  roll = atan2f(t(2,1), t(2,2));
131  pitch = asinf(-t(2,0));
132  yaw = atan2f(t(1,0), t(0,0));
133 }
134 
135 //////////////////////////////////////////////////////////////////////////////////////////
136 void
137 pcl::getTranslationAndEulerAngles (const Eigen::Affine3f& t,
138  float& x, float& y, float& z,
139  float& roll, float& pitch, float& yaw)
140 {
141  x = t(0,3);
142  y = t(1,3);
143  z = t(2,3);
144  roll = atan2f(t(2,1), t(2,2));
145  pitch = asinf(-t(2,0));
146  yaw = atan2f(t(1,0), t(0,0));
147 }
148 
149 //////////////////////////////////////////////////////////////////////////////////////////
150 template <typename Scalar> void
151 pcl::getTransformation (Scalar x, Scalar y, Scalar z,
152  Scalar roll, Scalar pitch, Scalar yaw,
153  Eigen::Transform<Scalar, 3, Eigen::Affine> &t)
154 {
155  Scalar A = cos (yaw), B = sin (yaw), C = cos (pitch), D = sin (pitch),
156  E = cos (roll), F = sin (roll), DE = D*E, DF = D*F;
157 
158  t (0, 0) = A*C; t (0, 1) = A*DF - B*E; t (0, 2) = B*F + A*DE; t (0, 3) = x;
159  t (1, 0) = B*C; t (1, 1) = A*E + B*DF; t (1, 2) = B*DE - A*F; t (1, 3) = y;
160  t (2, 0) = -D; t (2, 1) = C*F; t (2, 2) = C*E; t (2, 3) = z;
161  t (3, 0) = 0; t (3, 1) = 0; t (3, 2) = 0; t (3, 3) = 1;
162 }
163 
164 //////////////////////////////////////////////////////////////////////////////////////////
165 Eigen::Affine3f
166 pcl::getTransformation (float x, float y, float z, float roll, float pitch, float yaw)
167 {
168  Eigen::Affine3f t;
169  getTransformation (x, y, z, roll, pitch, yaw, t);
170  return (t);
171 }
172 
173 //////////////////////////////////////////////////////////////////////////////////////////
174 template <typename Derived> void
175 pcl::saveBinary (const Eigen::MatrixBase<Derived>& matrix, std::ostream& file)
176 {
177  uint32_t rows = static_cast<uint32_t> (matrix.rows ()), cols = static_cast<uint32_t> (matrix.cols ());
178  file.write (reinterpret_cast<char*> (&rows), sizeof (rows));
179  file.write (reinterpret_cast<char*> (&cols), sizeof (cols));
180  for (uint32_t i = 0; i < rows; ++i)
181  for (uint32_t j = 0; j < cols; ++j)
182  {
183  typename Derived::Scalar tmp = matrix(i,j);
184  file.write (reinterpret_cast<const char*> (&tmp), sizeof (tmp));
185  }
186 }
187 
188 //////////////////////////////////////////////////////////////////////////////////////////
189 template <typename Derived> void
190 pcl::loadBinary (Eigen::MatrixBase<Derived> const & matrix_, std::istream& file)
191 {
192  Eigen::MatrixBase<Derived> &matrix = const_cast<Eigen::MatrixBase<Derived> &> (matrix_);
193 
194  uint32_t rows, cols;
195  file.read (reinterpret_cast<char*> (&rows), sizeof (rows));
196  file.read (reinterpret_cast<char*> (&cols), sizeof (cols));
197  if (matrix.rows () != static_cast<int>(rows) || matrix.cols () != static_cast<int>(cols))
198  matrix.derived().resize(rows, cols);
199 
200  for (uint32_t i = 0; i < rows; ++i)
201  for (uint32_t j = 0; j < cols; ++j)
202  {
203  typename Derived::Scalar tmp;
204  file.read (reinterpret_cast<char*> (&tmp), sizeof (tmp));
205  matrix (i, j) = tmp;
206  }
207 }
208 
209 //////////////////////////////////////////////////////////////////////////////////////////
210 template <typename Derived, typename OtherDerived>
211 typename Eigen::internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type
212 pcl::umeyama (const Eigen::MatrixBase<Derived>& src, const Eigen::MatrixBase<OtherDerived>& dst, bool with_scaling)
213 {
214  typedef typename Eigen::internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type TransformationMatrixType;
215  typedef typename Eigen::internal::traits<TransformationMatrixType>::Scalar Scalar;
216  typedef typename Eigen::NumTraits<Scalar>::Real RealScalar;
217  typedef typename Derived::Index Index;
218 
219  EIGEN_STATIC_ASSERT (!Eigen::NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
220  EIGEN_STATIC_ASSERT ((Eigen::internal::is_same<Scalar, typename Eigen::internal::traits<OtherDerived>::Scalar>::value),
221  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
222 
223  enum { Dimension = PCL_EIGEN_SIZE_MIN_PREFER_DYNAMIC (Derived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime) };
224 
225  typedef Eigen::Matrix<Scalar, Dimension, 1> VectorType;
226  typedef Eigen::Matrix<Scalar, Dimension, Dimension> MatrixType;
227  typedef typename Eigen::internal::plain_matrix_type_row_major<Derived>::type RowMajorMatrixType;
228 
229  const Index m = src.rows (); // dimension
230  const Index n = src.cols (); // number of measurements
231 
232  // required for demeaning ...
233  const RealScalar one_over_n = 1 / static_cast<RealScalar> (n);
234 
235  // computation of mean
236  const VectorType src_mean = src.rowwise ().sum () * one_over_n;
237  const VectorType dst_mean = dst.rowwise ().sum () * one_over_n;
238 
239  // demeaning of src and dst points
240  const RowMajorMatrixType src_demean = src.colwise () - src_mean;
241  const RowMajorMatrixType dst_demean = dst.colwise () - dst_mean;
242 
243  // Eq. (36)-(37)
244  const Scalar src_var = src_demean.rowwise ().squaredNorm ().sum () * one_over_n;
245 
246  // Eq. (38)
247  const MatrixType sigma (one_over_n * dst_demean * src_demean.transpose ());
248 
249  Eigen::JacobiSVD<MatrixType> svd (sigma, Eigen::ComputeFullU | Eigen::ComputeFullV);
250 
251  // Initialize the resulting transformation with an identity matrix...
252  TransformationMatrixType Rt = TransformationMatrixType::Identity (m + 1, m + 1);
253 
254  // Eq. (39)
255  VectorType S = VectorType::Ones (m);
256  if (sigma.determinant () < 0)
257  S (m - 1) = -1;
258 
259  // Eq. (40) and (43)
260  const VectorType& d = svd.singularValues ();
261  Index rank = 0;
262  for (Index i = 0; i < m; ++i)
263  if (!Eigen::internal::isMuchSmallerThan (d.coeff (i), d.coeff (0)))
264  ++rank;
265  if (rank == m - 1)
266  {
267  if (svd.matrixU ().determinant () * svd.matrixV ().determinant () > 0)
268  Rt.block (0, 0, m, m).noalias () = svd.matrixU () * svd.matrixV ().transpose ();
269  else
270  {
271  const Scalar s = S (m - 1);
272  S (m - 1) = -1;
273  Rt.block (0, 0, m, m).noalias () = svd.matrixU () * S.asDiagonal () * svd.matrixV ().transpose ();
274  S (m - 1) = s;
275  }
276  }
277  else
278  {
279  Rt.block (0, 0, m, m).noalias () = svd.matrixU () * S.asDiagonal () * svd.matrixV ().transpose ();
280  }
281 
282  // Eq. (42)
283  if (with_scaling)
284  {
285  // Eq. (42)
286  const Scalar c = 1 / src_var * svd.singularValues ().dot (S);
287 
288  // Eq. (41)
289  Rt.col (m).head (m) = dst_mean;
290  Rt.col (m).head (m).noalias () -= c * Rt.topLeftCorner (m, m) * src_mean;
291  Rt.block (0, 0, m, m) *= c;
292  }
293  else
294  {
295  Rt.col (m).head (m) = dst_mean;
296  Rt.col (m).head (m).noalias () -= Rt.topLeftCorner (m, m) * src_mean;
297  }
298 
299  return (Rt);
300 }
301 
302 #endif //PCL_COMMON_EIGEN_IMPL_HPP_
303