Rotations.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009-2013 CEA LIST (DIASI/LSI) <xde-support@saxifrage.cea.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_LGSM_ROTATIONS_H
11 #define EIGEN_LGSM_ROTATIONS_H
12 
13 template <typename QuatDerived, typename Derived>
14 inline typename internal::traits<QuatDerived>::PlainObject projectRotation(
15  const QuaternionBase<QuatDerived>& q,
16  const MatrixBase<Derived>& n,
17  typename internal::traits<Derived>::Scalar& theta)
18 {
19  typedef typename internal::traits<Derived>::Scalar Scalar;
20  typedef typename internal::traits<QuatDerived>::PlainObject Quaternion;
21  typedef Matrix<Scalar, 3, 3> AdjointMatrix;
22 
23  const AdjointMatrix& R = q.toRotationMatrix();
24 
25  //tr(R^t*[n])
26  Scalar tr1 = (R(2,1)-R(1,2))*n[0] + (R(0,2)-R(2,0))*n[1] + (R(1,0)-R(0,1))*n[2];
27  //tr(R^t*[n]^2) = tr(R^t*n*n^t) - tr(R^t) since n^2 = 1
28  Scalar tr2 = n[0] * (R(0,0)*n[0]+R(1,0)*n[1]+R(2,0)*n[2])
29  + n[1] * (R(0,1)*n[0]+R(1,1)*n[1]+R(2,1)*n[2])
30  + n[2] * (R(0,2)*n[0]+R(1,2)*n[1]+R(2,2)*n[2])
31  - R(0,0) - R(1,1) - R(2,2);
32 
33  Scalar alpha = atan2(tr1, tr2);
34  theta = M_PI - alpha;
35  Scalar s = sin(theta);
36  Scalar c = cos(theta);
37  Scalar ic = 1-c;
38 
39  AdjointMatrix Rp;
40  Rp(0,0) = + ic*n[0]*n[0] + c;
41  Rp(0,1) = -s*n[2] + ic*n[0]*n[1];
42  Rp(0,2) = s*n[1] + ic*n[0]*n[2];
43  Rp(1,0) = s*n[2] + ic*n[1]*n[0];
44  Rp(1,1) = + ic*n[1]*n[1] + c;
45  Rp(1,2) = -s*n[0] + ic*n[1]*n[2];
46  Rp(2,0) = -s*n[1] + ic*n[2]*n[0];
47  Rp(2,1) = s*n[0] + ic*n[2]*n[1];
48  Rp(2,2) = + ic*n[2]*n[2] + c;
49 
50  return Quaternion(Rp);
51 }
52 
53 template <typename QuatDerived, typename Derived>
54 inline typename internal::traits<QuatDerived>::PlainObject projectRotation(
55  const QuaternionBase<QuatDerived>& q,
56  const MatrixBase<Derived>& v)
57 {
58  typename internal::traits<Derived>::Scalar tmp;
59  return projectRotation(q, v, tmp);
60 }
61 
62  template <typename QuatDerived, typename Derived>
63 inline bool isRotationAround(const QuaternionBase<QuatDerived>& q, const MatrixBase<Derived>& v)
64 {
65  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
66  typedef typename QuaternionBase<QuatDerived>::Scalar Scalar;
67  AngleAxis<Scalar> aa(q);
68  return aa.axis().isApprox(v) || aa.axis().isApprox(-v) || aa.angle() == 0; //if the rotation is very small, eigen put axis to (1,0,0) and angle to 0
69 }
70 
71  template <typename QuatDerived, typename Derived>
72 inline typename QuaternionBase<QuatDerived>::Scalar getAngleAround(const QuaternionBase<QuatDerived>& q, const MatrixBase<Derived>& v)
73 {
74  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
75  assert(isRotationAround(q,v));
76  typedef typename QuaternionBase<QuatDerived>::Scalar Scalar;
77  AngleAxis<Scalar> aa(q);
78  return aa.angle()*(aa.axis().dot(v));
79 }
80 
81 #endif
QuaternionBase< QuatDerived >::Scalar getAngleAround(const QuaternionBase< QuatDerived > &q, const MatrixBase< Derived > &v)
Definition: Rotations.h:72
internal::traits< QuatDerived >::PlainObject projectRotation(const QuaternionBase< QuatDerived > &q, const MatrixBase< Derived > &n, typename internal::traits< Derived >::Scalar &theta)
Definition: Rotations.h:14
bool isRotationAround(const QuaternionBase< QuatDerived > &q, const MatrixBase< Derived > &v)
Definition: Rotations.h:63


lgsm
Author(s): Roberto Martín-Martín
autogenerated on Mon Jun 10 2019 14:05:58