41 #include <pcl/common/eigen.h>
42 #include <pcl/console/print.h>
52 template <
typename Scalar,
typename Roots>
inline void
55 roots (0) = Scalar (0);
56 Scalar d = Scalar (b * b - 4.0 * c);
60 Scalar sd = std::sqrt (d);
62 roots (2) = 0.5f * (b + sd);
63 roots (1) = 0.5f * (b - sd);
67 template <
typename Matrix,
typename Roots>
inline void
70 using Scalar =
typename Matrix::Scalar;
75 Scalar c0 = m (0, 0) * m (1, 1) * m (2, 2)
76 + Scalar (2) * m (0, 1) * m (0, 2) * m (1, 2)
77 - m (0, 0) * m (1, 2) * m (1, 2)
78 - m (1, 1) * m (0, 2) * m (0, 2)
79 - m (2, 2) * m (0, 1) * m (0, 1);
80 Scalar c1 = m (0, 0) * m (1, 1) -
86 Scalar c2 = m (0, 0) + m (1, 1) + m (2, 2);
88 if (std::abs (c0) < Eigen::NumTraits < Scalar > ::epsilon ())
92 constexpr Scalar s_inv3 = Scalar(1.0 / 3.0);
93 const Scalar s_sqrt3 = std::sqrt (Scalar (3.0));
96 Scalar c2_over_3 = c2 * s_inv3;
97 Scalar a_over_3 = (c1 - c2 * c2_over_3) * s_inv3;
98 if (a_over_3 > Scalar (0))
99 a_over_3 = Scalar (0);
101 Scalar half_b = Scalar (0.5) * (c0 + c2_over_3 * (Scalar (2) * c2_over_3 * c2_over_3 - c1));
103 Scalar q = half_b * half_b + a_over_3 * a_over_3 * a_over_3;
108 Scalar rho = std::sqrt (-a_over_3);
109 Scalar theta = std::atan2 (std::sqrt (-q), half_b) * s_inv3;
110 Scalar cos_theta = std::cos (theta);
111 Scalar sin_theta = std::sin (theta);
112 roots (0) = c2_over_3 + Scalar (2) * rho * cos_theta;
113 roots (1) = c2_over_3 - rho * (cos_theta + s_sqrt3 * sin_theta);
114 roots (2) = c2_over_3 - rho * (cos_theta - s_sqrt3 * sin_theta);
117 if (roots (0) >= roots (1))
118 std::swap (roots (0), roots (1));
119 if (roots (1) >= roots (2))
121 std::swap (roots (1), roots (2));
122 if (roots (0) >= roots (1))
123 std::swap (roots (0), roots (1));
132 template <
typename Matrix,
typename Vector>
inline void
133 eigen22 (
const Matrix& mat,
typename Matrix::Scalar& eigenvalue, Vector& eigenvector)
137 if (std::abs (mat.coeff (1)) <= std::numeric_limits<typename Matrix::Scalar>::min ())
139 if (mat.coeff (0) < mat.coeff (2))
141 eigenvalue = mat.coeff (0);
142 eigenvector[0] = 1.0;
143 eigenvector[1] = 0.0;
147 eigenvalue = mat.coeff (2);
148 eigenvector[0] = 0.0;
149 eigenvector[1] = 1.0;
155 typename Matrix::Scalar trace =
static_cast<typename Matrix::Scalar
> (0.5) * (mat.coeff (0) + mat.coeff (3));
156 typename Matrix::Scalar determinant = mat.coeff (0) * mat.coeff (3) - mat.coeff (1) * mat.coeff (1);
158 typename Matrix::Scalar temp = trace * trace - determinant;
163 eigenvalue = trace - std::sqrt (temp);
165 eigenvector[0] = -mat.coeff (1);
166 eigenvector[1] = mat.coeff (0) - eigenvalue;
167 eigenvector.normalize ();
171 template <
typename Matrix,
typename Vector>
inline void
172 eigen22 (
const Matrix& mat, Matrix& eigenvectors, Vector& eigenvalues)
176 if (std::abs (mat.coeff (1)) <= std::numeric_limits<typename Matrix::Scalar>::min ())
178 if (mat.coeff (0) < mat.coeff (3))
180 eigenvalues.coeffRef (0) = mat.coeff (0);
181 eigenvalues.coeffRef (1) = mat.coeff (3);
182 eigenvectors.coeffRef (0) = 1.0;
183 eigenvectors.coeffRef (1) = 0.0;
184 eigenvectors.coeffRef (2) = 0.0;
185 eigenvectors.coeffRef (3) = 1.0;
189 eigenvalues.coeffRef (0) = mat.coeff (3);
190 eigenvalues.coeffRef (1) = mat.coeff (0);
191 eigenvectors.coeffRef (0) = 0.0;
192 eigenvectors.coeffRef (1) = 1.0;
193 eigenvectors.coeffRef (2) = 1.0;
194 eigenvectors.coeffRef (3) = 0.0;
200 typename Matrix::Scalar trace =
static_cast<typename Matrix::Scalar
> (0.5) * (mat.coeff (0) + mat.coeff (3));
201 typename Matrix::Scalar determinant = mat.coeff (0) * mat.coeff (3) - mat.coeff (1) * mat.coeff (1);
203 typename Matrix::Scalar temp = trace * trace - determinant;
208 temp = std::sqrt (temp);
210 eigenvalues.coeffRef (0) = trace - temp;
211 eigenvalues.coeffRef (1) = trace + temp;
214 eigenvectors.coeffRef (0) = -mat.coeff (1);
215 eigenvectors.coeffRef (2) = mat.coeff (0) - eigenvalues.coeff (0);
216 typename Matrix::Scalar norm =
static_cast<typename Matrix::Scalar
> (1.0)
217 /
static_cast<typename Matrix::Scalar
> (std::sqrt (eigenvectors.coeffRef (0) * eigenvectors.coeffRef (0) + eigenvectors.coeffRef (2) * eigenvectors.coeffRef (2)));
218 eigenvectors.coeffRef (0) *= norm;
219 eigenvectors.coeffRef (2) *= norm;
220 eigenvectors.coeffRef (1) = eigenvectors.coeffRef (2);
221 eigenvectors.coeffRef (3) = -eigenvectors.coeffRef (0);
225 template <
typename Matrix,
typename Vector>
inline void
228 using Scalar =
typename Matrix::Scalar;
232 Scalar scale = mat.cwiseAbs ().maxCoeff ();
233 if (scale <= std::numeric_limits < Scalar > ::min ())
234 scale = Scalar (1.0);
236 Matrix scaledMat = mat / scale;
238 scaledMat.diagonal ().array () -= eigenvalue / scale;
240 Vector vec1 = scaledMat.row (0).cross (scaledMat.row (1));
241 Vector vec2 = scaledMat.row (0).cross (scaledMat.row (2));
242 Vector vec3 = scaledMat.row (1).cross (scaledMat.row (2));
244 Scalar len1 = vec1.squaredNorm ();
245 Scalar len2 = vec2.squaredNorm ();
246 Scalar len3 = vec3.squaredNorm ();
248 if (len1 >= len2 && len1 >= len3)
249 eigenvector = vec1 / std::sqrt (len1);
250 else if (len2 >= len1 && len2 >= len3)
251 eigenvector = vec2 / std::sqrt (len2);
253 eigenvector = vec3 / std::sqrt (len3);
259 template <
typename Vector,
typename Scalar>
275 using Scalar =
typename Matrix::Scalar;
276 using Index =
typename Matrix::Index;
279 crossProduct << scaledMatrix.row (0).cross (scaledMatrix.row (1)),
280 scaledMatrix.row (0).cross (scaledMatrix.row (2)),
281 scaledMatrix.row (1).cross (scaledMatrix.row (2));
284 const auto len = crossProduct.rowwise ().norm ();
287 const Scalar length = len.maxCoeff (&index);
288 return {crossProduct.row (index) / length, length};
294 template <
typename Matrix,
typename Vector>
inline void
295 eigen33 (
const Matrix& mat,
typename Matrix::Scalar& eigenvalue, Vector& eigenvector)
297 using Scalar =
typename Matrix::Scalar;
301 Scalar scale = mat.cwiseAbs ().maxCoeff ();
302 if (scale <= std::numeric_limits < Scalar > ::min ())
303 scale = Scalar (1.0);
305 Matrix scaledMat = mat / scale;
310 eigenvalue = eigenvalues (0) * scale;
312 scaledMat.diagonal ().array () -= eigenvalues (0);
314 eigenvector = detail::getLargest3x3Eigenvector<Vector> (scaledMat).vector;
318 template <
typename Matrix,
typename Vector>
inline void
321 using Scalar =
typename Matrix::Scalar;
322 Scalar scale = mat.cwiseAbs ().maxCoeff ();
323 if (scale <= std::numeric_limits < Scalar > ::min ())
324 scale = Scalar (1.0);
326 Matrix scaledMat = mat / scale;
332 template <
typename Matrix,
typename Vector>
inline void
333 eigen33 (
const Matrix& mat, Matrix& evecs, Vector& evals)
335 using Scalar =
typename Matrix::Scalar;
339 Scalar scale = mat.cwiseAbs ().maxCoeff ();
340 if (scale <= std::numeric_limits < Scalar > ::min ())
341 scale = Scalar (1.0);
343 Matrix scaledMat = mat / scale;
348 if ( (evals (2) - evals (0)) <= Eigen::NumTraits < Scalar > ::epsilon ())
351 evecs.setIdentity ();
353 else if ( (evals (1) - evals (0)) <= Eigen::NumTraits < Scalar > ::epsilon ())
358 tmp.diagonal ().array () -= evals (2);
360 evecs.col (2) = detail::getLargest3x3Eigenvector<Vector> (tmp).vector;
361 evecs.col (1) = evecs.col (2).unitOrthogonal ();
362 evecs.col (0) = evecs.col (1).cross (evecs.col (2));
364 else if ( (evals (2) - evals (1)) <= Eigen::NumTraits < Scalar > ::epsilon ())
369 tmp.diagonal ().array () -= evals (0);
371 evecs.col (0) = detail::getLargest3x3Eigenvector<Vector> (tmp).vector;
372 evecs.col (1) = evecs.col (0).unitOrthogonal ();
373 evecs.col (2) = evecs.col (0).cross (evecs.col (1));
377 std::array<Scalar, 3> eigenVecLen;
379 for (
int i = 0; i < 3; ++i)
381 Matrix tmp = scaledMat;
382 tmp.diagonal ().array () -= evals (i);
383 const auto vec_len = detail::getLargest3x3Eigenvector<Vector> (tmp);
384 evecs.col (i) = vec_len.vector;
385 eigenVecLen[i] = vec_len.length;
390 const auto minmax_it = std::minmax_element (eigenVecLen.cbegin (), eigenVecLen.cend ());
391 int min_idx =
std::distance (eigenVecLen.cbegin (), minmax_it.first);
392 int max_idx =
std::distance (eigenVecLen.cbegin (), minmax_it.second);
393 int mid_idx = 3 - min_idx - max_idx;
395 evecs.col (min_idx) = evecs.col ( (min_idx + 1) % 3).cross (evecs.col ( (min_idx + 2) % 3)).normalized ();
396 evecs.col (mid_idx) = evecs.col ( (mid_idx + 1) % 3).cross (evecs.col ( (mid_idx + 2) % 3)).normalized ();
403 template <
typename Matrix>
inline typename Matrix::Scalar
406 using Scalar =
typename Matrix::Scalar;
407 Scalar det = matrix.coeff (0) * matrix.coeff (3) - matrix.coeff (1) * matrix.coeff (2);
412 inverse.coeffRef (0) = matrix.coeff (3);
413 inverse.coeffRef (1) = -matrix.coeff (1);
414 inverse.coeffRef (2) = -matrix.coeff (2);
415 inverse.coeffRef (3) = matrix.coeff (0);
422 template <
typename Matrix>
inline typename Matrix::Scalar
425 using Scalar =
typename Matrix::Scalar;
436 Scalar fd_ee = matrix.coeff (4) * matrix.coeff (8) - matrix.coeff (7) * matrix.coeff (5);
437 Scalar ce_bf = matrix.coeff (2) * matrix.coeff (5) - matrix.coeff (1) * matrix.coeff (8);
438 Scalar be_cd = matrix.coeff (1) * matrix.coeff (5) - matrix.coeff (2) * matrix.coeff (4);
440 Scalar det = matrix.coeff (0) * fd_ee + matrix.coeff (1) * ce_bf + matrix.coeff (2) * be_cd;
445 inverse.coeffRef (0) = fd_ee;
446 inverse.coeffRef (1) = inverse.coeffRef (3) = ce_bf;
447 inverse.coeffRef (2) = inverse.coeffRef (6) = be_cd;
448 inverse.coeffRef (4) = (matrix.coeff (0) * matrix.coeff (8) - matrix.coeff (2) * matrix.coeff (2));
449 inverse.coeffRef (5) = inverse.coeffRef (7) = (matrix.coeff (1) * matrix.coeff (2) - matrix.coeff (0) * matrix.coeff (5));
450 inverse.coeffRef (8) = (matrix.coeff (0) * matrix.coeff (4) - matrix.coeff (1) * matrix.coeff (1));
457 template <
typename Matrix>
inline typename Matrix::Scalar
460 using Scalar =
typename Matrix::Scalar;
467 Scalar ie_hf = matrix.coeff (8) * matrix.coeff (4) - matrix.coeff (7) * matrix.coeff (5);
468 Scalar hc_ib = matrix.coeff (7) * matrix.coeff (2) - matrix.coeff (8) * matrix.coeff (1);
469 Scalar fb_ec = matrix.coeff (5) * matrix.coeff (1) - matrix.coeff (4) * matrix.coeff (2);
470 Scalar det = matrix.coeff (0) * (ie_hf) + matrix.coeff (3) * (hc_ib) + matrix.coeff (6) * (fb_ec);
474 inverse.coeffRef (0) = ie_hf;
475 inverse.coeffRef (1) = hc_ib;
476 inverse.coeffRef (2) = fb_ec;
477 inverse.coeffRef (3) = matrix.coeff (6) * matrix.coeff (5) - matrix.coeff (8) * matrix.coeff (3);
478 inverse.coeffRef (4) = matrix.coeff (8) * matrix.coeff (0) - matrix.coeff (6) * matrix.coeff (2);
479 inverse.coeffRef (5) = matrix.coeff (3) * matrix.coeff (2) - matrix.coeff (5) * matrix.coeff (0);
480 inverse.coeffRef (6) = matrix.coeff (7) * matrix.coeff (3) - matrix.coeff (6) * matrix.coeff (4);
481 inverse.coeffRef (7) = matrix.coeff (6) * matrix.coeff (1) - matrix.coeff (7) * matrix.coeff (0);
482 inverse.coeffRef (8) = matrix.coeff (4) * matrix.coeff (0) - matrix.coeff (3) * matrix.coeff (1);
490 template <
typename Matrix>
inline typename Matrix::Scalar
494 return matrix.coeff (0) * (matrix.coeff (4) * matrix.coeff (8) - matrix.coeff (5) * matrix.coeff (7)) +
495 matrix.coeff (1) * (matrix.coeff (5) * matrix.coeff (6) - matrix.coeff (3) * matrix.coeff (8)) +
496 matrix.coeff (2) * (matrix.coeff (3) * matrix.coeff (7) - matrix.coeff (4) * matrix.coeff (6)) ;
502 const Eigen::Vector3f& y_direction,
503 Eigen::Affine3f& transformation)
505 Eigen::Vector3f tmp0 = (y_direction.cross(z_axis)).normalized();
506 Eigen::Vector3f tmp1 = (z_axis.cross(tmp0)).normalized();
507 Eigen::Vector3f tmp2 = z_axis.normalized();
509 transformation(0,0)=tmp0[0]; transformation(0,1)=tmp0[1]; transformation(0,2)=tmp0[2]; transformation(0,3)=0.0f;
510 transformation(1,0)=tmp1[0]; transformation(1,1)=tmp1[1]; transformation(1,2)=tmp1[2]; transformation(1,3)=0.0f;
511 transformation(2,0)=tmp2[0]; transformation(2,1)=tmp2[1]; transformation(2,2)=tmp2[2]; transformation(2,3)=0.0f;
512 transformation(3,0)=0.0f; transformation(3,1)=0.0f; transformation(3,2)=0.0f; transformation(3,3)=1.0f;
518 const Eigen::Vector3f& y_direction)
520 Eigen::Affine3f transformation;
522 return (transformation);
528 const Eigen::Vector3f& y_direction,
529 Eigen::Affine3f& transformation)
531 Eigen::Vector3f tmp2 = (x_axis.cross(y_direction)).normalized();
532 Eigen::Vector3f tmp1 = (tmp2.cross(x_axis)).normalized();
533 Eigen::Vector3f tmp0 = x_axis.normalized();
535 transformation(0,0)=tmp0[0]; transformation(0,1)=tmp0[1]; transformation(0,2)=tmp0[2]; transformation(0,3)=0.0f;
536 transformation(1,0)=tmp1[0]; transformation(1,1)=tmp1[1]; transformation(1,2)=tmp1[2]; transformation(1,3)=0.0f;
537 transformation(2,0)=tmp2[0]; transformation(2,1)=tmp2[1]; transformation(2,2)=tmp2[2]; transformation(2,3)=0.0f;
538 transformation(3,0)=0.0f; transformation(3,1)=0.0f; transformation(3,2)=0.0f; transformation(3,3)=1.0f;
544 const Eigen::Vector3f& y_direction)
546 Eigen::Affine3f transformation;
548 return (transformation);
554 const Eigen::Vector3f& z_axis,
555 Eigen::Affine3f& transformation)
563 const Eigen::Vector3f& z_axis)
565 Eigen::Affine3f transformation;
567 return (transformation);
573 const Eigen::Vector3f& z_axis,
574 const Eigen::Vector3f& origin,
575 Eigen::Affine3f& transformation)
578 Eigen::Vector3f translation = transformation*origin;
579 transformation(0,3)=-translation[0]; transformation(1,3)=-translation[1]; transformation(2,3)=-translation[2];
583 template <
typename Scalar>
void
584 getEulerAngles (
const Eigen::Transform<Scalar, 3, Eigen::Affine> &t, Scalar &roll, Scalar &pitch, Scalar &yaw)
586 roll = std::atan2 (t (2, 1), t (2, 2));
587 pitch = asin (-t (2, 0));
588 yaw = std::atan2 (t (1, 0), t (0, 0));
592 template <
typename Scalar>
void
594 Scalar &x, Scalar &y, Scalar &z,
595 Scalar &roll, Scalar &pitch, Scalar &yaw)
600 roll = std::atan2 (t (2, 1), t (2, 2));
601 pitch = asin (-t (2, 0));
602 yaw = std::atan2 (t (1, 0), t (0, 0));
606 template <
typename Scalar>
void
608 Scalar roll, Scalar pitch, Scalar yaw,
609 Eigen::Transform<Scalar, 3, Eigen::Affine> &t)
611 Scalar A = std::cos (yaw),
B = sin (yaw), C = std::cos (pitch), D = sin (pitch),
612 E = std::cos (roll), F = sin (roll), DE = D*E, DF = D*F;
614 t (0, 0) = A*C; t (0, 1) = A*DF -
B*E; t (0, 2) =
B*F + A*DE; t (0, 3) = x;
615 t (1, 0) =
B*C; t (1, 1) = A*E +
B*DF; t (1, 2) =
B*DE - A*F; t (1, 3) = y;
616 t (2, 0) = -D; t (2, 1) = C*F; t (2, 2) = C*E; t (2, 3) = z;
617 t (3, 0) = 0; t (3, 1) = 0; t (3, 2) = 0; t (3, 3) = 1;
621 template <
typename Derived>
void
622 saveBinary (
const Eigen::MatrixBase<Derived>& matrix, std::ostream& file)
624 std::uint32_t rows =
static_cast<std::uint32_t
> (matrix.rows ()), cols =
static_cast<std::uint32_t
> (matrix.cols ());
625 file.write (
reinterpret_cast<char*
> (&rows),
sizeof (rows));
626 file.write (
reinterpret_cast<char*
> (&cols),
sizeof (cols));
627 for (std::uint32_t i = 0; i < rows; ++i)
628 for (std::uint32_t j = 0; j < cols; ++j)
630 typename Derived::Scalar tmp = matrix(i,j);
631 file.write (
reinterpret_cast<const char*
> (&tmp),
sizeof (tmp));
636 template <
typename Derived>
void
637 loadBinary (Eigen::MatrixBase<Derived>
const & matrix_, std::istream& file)
639 Eigen::MatrixBase<Derived> &matrix =
const_cast<Eigen::MatrixBase<Derived> &
> (matrix_);
641 std::uint32_t rows, cols;
642 file.read (
reinterpret_cast<char*
> (&rows),
sizeof (rows));
643 file.read (
reinterpret_cast<char*
> (&cols),
sizeof (cols));
644 if (matrix.rows () !=
static_cast<int>(rows) || matrix.cols () !=
static_cast<int>(cols))
645 matrix.derived().resize(rows, cols);
647 for (std::uint32_t i = 0; i < rows; ++i)
648 for (std::uint32_t j = 0; j < cols; ++j)
650 typename Derived::Scalar tmp;
651 file.read (
reinterpret_cast<char*
> (&tmp),
sizeof (tmp));
657 template <
typename Derived,
typename OtherDerived>
658 typename Eigen::internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type
659 umeyama (
const Eigen::MatrixBase<Derived>& src,
const Eigen::MatrixBase<OtherDerived>& dst,
bool with_scaling)
661 #if EIGEN_VERSION_AT_LEAST (3, 3, 0)
662 return Eigen::umeyama (src, dst, with_scaling);
664 using TransformationMatrixType =
typename Eigen::internal::umeyama_transform_matrix_type<Derived, OtherDerived>::type;
665 using Scalar =
typename Eigen::internal::traits<TransformationMatrixType>::Scalar;
666 using RealScalar =
typename Eigen::NumTraits<Scalar>::Real;
667 using Index =
typename Derived::Index;
669 static_assert (!Eigen::NumTraits<Scalar>::IsComplex,
"Numeric type must be real.");
670 static_assert ((Eigen::internal::is_same<Scalar,
typename Eigen::internal::traits<OtherDerived>::Scalar>::value),
671 "You mixed different numeric types. You need to use the cast method of matrixbase to cast numeric types explicitly.");
673 enum { Dimension = PCL_EIGEN_SIZE_MIN_PREFER_DYNAMIC (Derived::RowsAtCompileTime, OtherDerived::RowsAtCompileTime) };
675 using VectorType = Eigen::Matrix<Scalar, Dimension, 1>;
676 using MatrixType = Eigen::Matrix<Scalar, Dimension, Dimension>;
677 using RowMajorMatrixType =
typename Eigen::internal::plain_matrix_type_row_major<Derived>::type;
679 const Index m = src.rows ();
680 const Index n = src.cols ();
683 const RealScalar one_over_n = 1 /
static_cast<RealScalar
> (n);
686 const VectorType src_mean = src.rowwise ().sum () * one_over_n;
687 const VectorType dst_mean = dst.rowwise ().sum () * one_over_n;
690 const RowMajorMatrixType src_demean = src.colwise () - src_mean;
691 const RowMajorMatrixType dst_demean = dst.colwise () - dst_mean;
694 const Scalar src_var = src_demean.rowwise ().squaredNorm ().sum () * one_over_n;
697 const MatrixType sigma (one_over_n * dst_demean * src_demean.transpose ());
699 Eigen::JacobiSVD<MatrixType> svd (sigma, Eigen::ComputeFullU | Eigen::ComputeFullV);
702 TransformationMatrixType Rt = TransformationMatrixType::Identity (m + 1, m + 1);
705 VectorType S = VectorType::Ones (m);
707 if ( svd.matrixU ().determinant () * svd.matrixV ().determinant () < 0 )
711 Rt.block (0,0,m,m).noalias () = svd.matrixU () * S.asDiagonal () * svd.matrixV ().transpose ();
716 const Scalar c = Scalar (1)/ src_var * svd.singularValues ().dot (S);
719 Rt.col (m).head (m) = dst_mean;
720 Rt.col (m).head (m).noalias () -= c * Rt.topLeftCorner (m, m) * src_mean;
721 Rt.block (0, 0, m, m) *= c;
725 Rt.col (m).head (m) = dst_mean;
726 Rt.col (m).head (m).noalias () -= Rt.topLeftCorner (m, m) * src_mean;
734 template <
typename Scalar>
bool
736 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> &line_out,
737 const Eigen::Transform<Scalar, 3, Eigen::Affine> &transformation)
739 if (line_in.innerSize () != 6 || line_out.innerSize () != 6)
741 PCL_DEBUG (
"transformLine: lines size != 6\n");
745 Eigen::Matrix<Scalar, 3, 1> point, vector;
746 point << line_in.template head<3> ();
747 vector << line_out.template tail<3> ();
751 line_out << point, vector;
756 template <
typename Scalar>
void
758 Eigen::Matrix<Scalar, 4, 1> &plane_out,
759 const Eigen::Transform<Scalar, 3, Eigen::Affine> &transformation)
761 Eigen::Hyperplane < Scalar, 3 > plane;
762 plane.coeffs () << plane_in;
763 plane.transform (transformation);
764 plane_out << plane.coeffs ();
767 #if !EIGEN_VERSION_AT_LEAST (3, 3, 2)
768 plane_out /= plane_out.template head<3> ().norm ();
773 template <
typename Scalar>
void
776 const Eigen::Transform<Scalar, 3, Eigen::Affine> &transformation)
778 std::vector<Scalar> values (plane_in->values.begin (), plane_in->values.end ());
779 Eigen::Matrix < Scalar, 4, 1 > v_plane_in (values.data ());
781 plane_out->values.resize (4);
782 std::copy_n(v_plane_in.data (), 4, plane_out->values.begin ());
786 template <
typename Scalar>
bool
788 const Eigen::Matrix<Scalar, Eigen::Dynamic, 1> &line_y,
789 const Scalar norm_limit,
790 const Scalar dot_limit)
792 if (line_x.innerSize () != 6 || line_y.innerSize () != 6)
794 PCL_DEBUG (
"checkCoordinateSystem: lines size != 6\n");
798 if (line_x.template head<3> () != line_y.template head<3> ())
800 PCL_DEBUG (
"checkCoorZdinateSystem: vector origins are different !\n");
806 Eigen::Matrix<Scalar, 3, 1> v_line_x (line_x.template tail<3> ()),
807 v_line_y (line_y.template tail<3> ()),
808 v_line_z (v_line_x.cross (v_line_y));
811 if (v_line_x.norm () < 1 - norm_limit || v_line_x.norm () > 1 + norm_limit)
813 PCL_DEBUG (
"checkCoordinateSystem: line_x norm %d != 1\n", v_line_x.norm ());
817 if (v_line_y.norm () < 1 - norm_limit || v_line_y.norm () > 1 + norm_limit)
819 PCL_DEBUG (
"checkCoordinateSystem: line_y norm %d != 1\n", v_line_y.norm ());
823 if (v_line_z.norm () < 1 - norm_limit || v_line_z.norm () > 1 + norm_limit)
825 PCL_DEBUG (
"checkCoordinateSystem: line_z norm %d != 1\n", v_line_z.norm ());
830 if (std::abs (v_line_x.dot (v_line_y)) > dot_limit)
832 PCL_DEBUG (
"checkCSAxis: line_x dot line_y %e = > %e\n", v_line_x.dot (v_line_y), dot_limit);
836 if (std::abs (v_line_x.dot (v_line_z)) > dot_limit)
838 PCL_DEBUG (
"checkCSAxis: line_x dot line_z = %e > %e\n", v_line_x.dot (v_line_z), dot_limit);
842 if (std::abs (v_line_y.dot (v_line_z)) > dot_limit)
844 PCL_DEBUG (
"checkCSAxis: line_y dot line_z = %e > %e\n", v_line_y.dot (v_line_z), dot_limit);
852 template <
typename Scalar>
bool
854 const Eigen::Matrix<Scalar, Eigen::Dynamic, 1> from_line_y,
855 const Eigen::Matrix<Scalar, Eigen::Dynamic, 1> to_line_x,
856 const Eigen::Matrix<Scalar, Eigen::Dynamic, 1> to_line_y,
857 Eigen::Transform<Scalar, 3, Eigen::Affine> &transformation)
859 if (from_line_x.innerSize () != 6 || from_line_y.innerSize () != 6 || to_line_x.innerSize () != 6 || to_line_y.innerSize () != 6)
861 PCL_DEBUG (
"transformBetween2CoordinateSystems: lines size != 6\n");
868 PCL_DEBUG (
"transformBetween2CoordinateSystems: coordinate systems invalid !\n");
873 Eigen::Matrix<Scalar, 3, 1> fr0 (from_line_x.template head<3>()),
874 fr1 (from_line_x.template head<3>() + from_line_x.template tail<3>()),
875 fr2 (from_line_y.template head<3>() + from_line_y.template tail<3>()),
877 to0 (to_line_x.template head<3>()),
878 to1 (to_line_x.template head<3>() + to_line_x.template tail<3>()),
879 to2 (to_line_y.template head<3>() + to_line_y.template tail<3>());
883 Eigen::Transform<Scalar, 3, Eigen::Affine> T2, T3 = Eigen::Transform<Scalar, 3, Eigen::Affine>::Identity ();
884 Eigen::Matrix<Scalar, 3, 1> x1, y1, z1, x2, y2, z2;
887 x1 = (fr1 - fr0).normalized ();
888 y1 = (fr2 - fr0).normalized ();
891 x2 = (to1 - to0).normalized ();
892 y2 = (to2 - to0).normalized ();
896 T2.linear () << x1, y1, x1.cross (y1);
899 T3.linear () << x2, y2, x2.cross (y2);
903 transformation = Eigen::Transform<Scalar, 3, Eigen::Affine>::Identity ();
904 transformation.linear () = T3.linear () * T2.linear ().inverse ();
905 transformation.translation () = to0 - (transformation.linear () * fr0);
void computeCorrespondingEigenVector(const Matrix &mat, const typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the corresponding eigenvector to the given eigenvalue of the symmetric positive semi defin...
void getTranslationAndEulerAngles(const Eigen::Transform< Scalar, 3, Eigen::Affine > &t, Scalar &x, Scalar &y, Scalar &z, Scalar &roll, Scalar &pitch, Scalar &yaw)
Extract x,y,z and the Euler angles (intrinsic rotations, ZYX-convention) from the given transformatio...
void getTransformationFromTwoUnitVectorsAndOrigin(const Eigen::Vector3f &y_direction, const Eigen::Vector3f &z_axis, const Eigen::Vector3f &origin, Eigen::Affine3f &transformation)
Get the transformation that will translate origin to (0,0,0) and rotate z_axis into (0,...
Matrix::Scalar determinant3x3Matrix(const Matrix &matrix)
Calculate the determinant of a 3x3 matrix.
Matrix::Scalar invert3x3SymMatrix(const Matrix &matrix, Matrix &inverse)
Calculate the inverse of a 3x3 symmetric matrix.
void loadBinary(Eigen::MatrixBase< Derived > const &matrix, std::istream &file)
Read a matrix from an input stream.
void getTransformation(Scalar x, Scalar y, Scalar z, Scalar roll, Scalar pitch, Scalar yaw, Eigen::Transform< Scalar, 3, Eigen::Affine > &t)
Create a transformation from the given translation and Euler angles (intrinsic rotations,...
void getEulerAngles(const Eigen::Transform< Scalar, 3, Eigen::Affine > &t, Scalar &roll, Scalar &pitch, Scalar &yaw)
Extract the Euler angles (intrinsic rotations, ZYX-convention) from the given transformation.
void eigen22(const Matrix &mat, typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determine the smallest eigenvalue and its corresponding eigenvector
void getTransformationFromTwoUnitVectors(const Eigen::Vector3f &y_direction, const Eigen::Vector3f &z_axis, Eigen::Affine3f &transformation)
Get the unique 3D rotation that will rotate z_axis into (0,0,1) and y_direction into a vector with x=...
void getTransFromUnitVectorsXY(const Eigen::Vector3f &x_axis, const Eigen::Vector3f &y_direction, Eigen::Affine3f &transformation)
Get the unique 3D rotation that will rotate x_axis into (1,0,0) and y_direction into a vector with z=...
Matrix::Scalar invert3x3Matrix(const Matrix &matrix, Matrix &inverse)
Calculate the inverse of a general 3x3 matrix.
void eigen33(const Matrix &mat, typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the eigenvector and eigenvalue of the smallest eigenvalue of the symmetric positive semi d...
void saveBinary(const Eigen::MatrixBase< Derived > &matrix, std::ostream &file)
Write a matrix to an output stream.
Matrix::Scalar invert2x2(const Matrix &matrix, Matrix &inverse)
Calculate the inverse of a 2x2 matrix.
void getTransFromUnitVectorsZY(const Eigen::Vector3f &z_axis, const Eigen::Vector3f &y_direction, Eigen::Affine3f &transformation)
Get the unique 3D rotation that will rotate z_axis into (0,0,1) and y_direction into a vector with x=...
static EigenVector< Vector, typename Matrix::Scalar > getLargest3x3Eigenvector(const Matrix scaledMatrix)
returns the unit vector along the largest eigen value as well as the length of the largest eigenvecto...
float distance(const PointT &p1, const PointT &p2)
bool checkCoordinateSystem(const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &line_x, const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &line_y, const Scalar norm_limit=1e-3, const Scalar dot_limit=1e-3)
Check coordinate system integrity.
void transformPlane(const Eigen::Matrix< Scalar, 4, 1 > &plane_in, Eigen::Matrix< Scalar, 4, 1 > &plane_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transformation)
Transform plane vectors using an affine matrix.
void transformPoint(const Eigen::Matrix< Scalar, 3, 1 > &point_in, Eigen::Matrix< Scalar, 3, 1 > &point_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transformation)
Transform a point using an affine matrix.
void transformVector(const Eigen::Matrix< Scalar, 3, 1 > &vector_in, Eigen::Matrix< Scalar, 3, 1 > &vector_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transformation)
Transform a vector using an affine matrix.
Eigen::internal::umeyama_transform_matrix_type< Derived, OtherDerived >::type umeyama(const Eigen::MatrixBase< Derived > &src, const Eigen::MatrixBase< OtherDerived > &dst, bool with_scaling=false)
Returns the transformation between two point sets.
bool transformBetween2CoordinateSystems(const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > from_line_x, const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > from_line_y, const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > to_line_x, const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > to_line_y, Eigen::Transform< Scalar, 3, Eigen::Affine > &transformation)
Compute the transformation between two coordinate systems.
bool transformLine(const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &line_in, Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &line_out, const Eigen::Transform< Scalar, 3, Eigen::Affine > &transformation)
Transform a line using an affine matrix.
void computeRoots2(const Scalar &b, const Scalar &c, Roots &roots)
Compute the roots of a quadratic polynom x^2 + b*x + c = 0.
void computeRoots(const Matrix &m, Roots &roots)
computes the roots of the characteristic polynomial of the input matrix m, which are the eigenvalues
shared_ptr< ::pcl::ModelCoefficients > Ptr
shared_ptr< const ::pcl::ModelCoefficients > ConstPtr