Eigen  3.2.7
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AngleAxis.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <[email protected]>
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_ANGLEAXIS_H
11 #define EIGEN_ANGLEAXIS_H
12 
13 namespace Eigen {
14 
41 namespace internal {
42 template<typename _Scalar> struct traits<AngleAxis<_Scalar> >
43 {
44  typedef _Scalar Scalar;
45 };
46 }
47 
48 template<typename _Scalar>
49 class AngleAxis : public RotationBase<AngleAxis<_Scalar>,3>
50 {
51  typedef RotationBase<AngleAxis<_Scalar>,3> Base;
52 
53 public:
54 
55  using Base::operator*;
56 
57  enum { Dim = 3 };
59  typedef _Scalar Scalar;
63 
64 protected:
65 
66  Vector3 m_axis;
67  Scalar m_angle;
68 
69 public:
70 
72  AngleAxis() {}
78  template<typename Derived>
79  inline AngleAxis(const Scalar& angle, const MatrixBase<Derived>& axis) : m_axis(axis), m_angle(angle) {}
81  template<typename QuatDerived> inline explicit AngleAxis(const QuaternionBase<QuatDerived>& q) { *this = q; }
83  template<typename Derived>
84  inline explicit AngleAxis(const MatrixBase<Derived>& m) { *this = m; }
85 
87  Scalar angle() const { return m_angle; }
89  Scalar& angle() { return m_angle; }
90 
92  const Vector3& axis() const { return m_axis; }
97  Vector3& axis() { return m_axis; }
98 
100  inline QuaternionType operator* (const AngleAxis& other) const
101  { return QuaternionType(*this) * QuaternionType(other); }
102 
104  inline QuaternionType operator* (const QuaternionType& other) const
105  { return QuaternionType(*this) * other; }
106 
108  friend inline QuaternionType operator* (const QuaternionType& a, const AngleAxis& b)
109  { return a * QuaternionType(b); }
110 
113  { return AngleAxis(-m_angle, m_axis); }
114 
115  template<class QuatDerived>
116  AngleAxis& operator=(const QuaternionBase<QuatDerived>& q);
117  template<typename Derived>
118  AngleAxis& operator=(const MatrixBase<Derived>& m);
119 
120  template<typename Derived>
121  AngleAxis& fromRotationMatrix(const MatrixBase<Derived>& m);
122  Matrix3 toRotationMatrix(void) const;
123 
129  template<typename NewScalarType>
130  inline typename internal::cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type cast() const
131  { return typename internal::cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type(*this); }
132 
134  template<typename OtherScalarType>
135  inline explicit AngleAxis(const AngleAxis<OtherScalarType>& other)
136  {
137  m_axis = other.axis().template cast<Scalar>();
138  m_angle = Scalar(other.angle());
139  }
140 
141  static inline const AngleAxis Identity() { return AngleAxis(Scalar(0), Vector3::UnitX()); }
142 
147  bool isApprox(const AngleAxis& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
148  { return m_axis.isApprox(other.m_axis, prec) && internal::isApprox(m_angle,other.m_angle, prec); }
149 };
150 
157 
164 template<typename Scalar>
165 template<typename QuatDerived>
167 {
168  using std::acos;
169  using std::min;
170  using std::max;
171  using std::sqrt;
172  Scalar n2 = q.vec().squaredNorm();
174  {
175  m_angle = Scalar(0);
176  m_axis << Scalar(1), Scalar(0), Scalar(0);
177  }
178  else
179  {
180  m_angle = Scalar(2)*acos((min)((max)(Scalar(-1),q.w()),Scalar(1)));
181  m_axis = q.vec() / sqrt(n2);
182  }
183  return *this;
184 }
185 
188 template<typename Scalar>
189 template<typename Derived>
191 {
192  // Since a direct conversion would not be really faster,
193  // let's use the robust Quaternion implementation:
194  return *this = QuaternionType(mat);
195 }
196 
200 template<typename Scalar>
201 template<typename Derived>
203 {
204  return *this = QuaternionType(mat);
205 }
206 
209 template<typename Scalar>
212 {
213  using std::sin;
214  using std::cos;
215  Matrix3 res;
216  Vector3 sin_axis = sin(m_angle) * m_axis;
217  Scalar c = cos(m_angle);
218  Vector3 cos1_axis = (Scalar(1)-c) * m_axis;
219 
220  Scalar tmp;
221  tmp = cos1_axis.x() * m_axis.y();
222  res.coeffRef(0,1) = tmp - sin_axis.z();
223  res.coeffRef(1,0) = tmp + sin_axis.z();
224 
225  tmp = cos1_axis.x() * m_axis.z();
226  res.coeffRef(0,2) = tmp + sin_axis.y();
227  res.coeffRef(2,0) = tmp - sin_axis.y();
228 
229  tmp = cos1_axis.y() * m_axis.z();
230  res.coeffRef(1,2) = tmp - sin_axis.x();
231  res.coeffRef(2,1) = tmp + sin_axis.x();
232 
233  res.diagonal() = (cos1_axis.cwiseProduct(m_axis)).array() + c;
234 
235  return res;
236 }
237 
238 } // end namespace Eigen
239 
240 #endif // EIGEN_ANGLEAXIS_H
AngleAxis()
Definition: AngleAxis.h:72
Scalar angle() const
Definition: AngleAxis.h:87
AngleAxis inverse() const
Definition: AngleAxis.h:112
AngleAxis(const MatrixBase< Derived > &m)
Definition: AngleAxis.h:84
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
Matrix3 toRotationMatrix(void) const
Definition: AngleAxis.h:211
AngleAxis(const Scalar &angle, const MatrixBase< Derived > &axis)
Definition: AngleAxis.h:79
AngleAxis< double > AngleAxisd
Definition: AngleAxis.h:156
AngleAxis< float > AngleAxisf
Definition: AngleAxis.h:153
AngleAxis(const QuaternionBase< QuatDerived > &q)
Definition: AngleAxis.h:81
Scalar & angle()
Definition: AngleAxis.h:89
DiagonalReturnType diagonal()
Definition: Diagonal.h:168
QuaternionType operator*(const AngleAxis &other) const
Definition: AngleAxis.h:100
Vector3 & axis()
Definition: AngleAxis.h:97
Base class for quaternion expressions.
Definition: ForwardDeclarations.h:233
bool isApprox(const AngleAxis &other, const typename NumTraits< Scalar >::Real &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: AngleAxis.h:147
internal::cast_return_type< AngleAxis, AngleAxis< NewScalarType > >::type cast() const
Definition: AngleAxis.h:130
const Vector3 & axis() const
Definition: AngleAxis.h:92
The quaternion class used to represent 3D orientations and rotations.
Definition: ForwardDeclarations.h:263
_Scalar Scalar
Definition: AngleAxis.h:59
Scalar w() const
Definition: Quaternion.h:66
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
AngleAxis(const AngleAxis< OtherScalarType > &other)
Definition: AngleAxis.h:135
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Represents a 3D rotation as a rotation angle around an arbitrary 3D axis.
Definition: ForwardDeclarations.h:235
const VectorBlock< const Coefficients, 3 > vec() const
Definition: Quaternion.h:78