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 <gael.guennebaud@inria.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_ANGLEAXIS_H
11#define EIGEN_ANGLEAXIS_H
12
13namespace Eigen {
14
40
41namespace internal {
42template<typename _Scalar> struct traits<AngleAxis<_Scalar> >
43{
44 typedef _Scalar Scalar;
45};
46}
47
48template<typename _Scalar>
49class AngleAxis : public RotationBase<AngleAxis<_Scalar>,3>
50{
51 typedef RotationBase<AngleAxis<_Scalar>,3> Base;
52
53public:
54
55 using Base::operator*;
56
57 enum { Dim = 3 };
59 typedef _Scalar Scalar;
60 typedef Matrix<Scalar,3,3> Matrix3;
61 typedef Matrix<Scalar,3,1> Vector3;
62 typedef Quaternion<Scalar> QuaternionType;
63
64protected:
65
66 Vector3 m_axis;
67 Scalar m_angle;
68
69public:
70
78 template<typename Derived>
79 inline AngleAxis(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
86 Scalar angle() const { return m_angle; }
87 Scalar& angle() { return m_angle; }
88
89 const Vector3& axis() const { return m_axis; }
90 Vector3& axis() { return m_axis; }
91
93 inline QuaternionType operator* (const AngleAxis& other) const
94 { return QuaternionType(*this) * QuaternionType(other); }
95
97 inline QuaternionType operator* (const QuaternionType& other) const
98 { return QuaternionType(*this) * other; }
99
101 friend inline QuaternionType operator* (const QuaternionType& a, const AngleAxis& b)
102 { return a * QuaternionType(b); }
103
106 { return AngleAxis(-m_angle, m_axis); }
107
108 template<class QuatDerived>
109 AngleAxis& operator=(const QuaternionBase<QuatDerived>& q);
110 template<typename Derived>
111 AngleAxis& operator=(const MatrixBase<Derived>& m);
112
113 template<typename Derived>
114 AngleAxis& fromRotationMatrix(const MatrixBase<Derived>& m);
115 Matrix3 toRotationMatrix(void) const;
116
122 template<typename NewScalarType>
123 inline typename internal::cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type cast() const
124 { return typename internal::cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type(*this); }
125
127 template<typename OtherScalarType>
128 inline explicit AngleAxis(const AngleAxis<OtherScalarType>& other)
129 {
130 m_axis = other.axis().template cast<Scalar>();
131 m_angle = Scalar(other.angle());
132 }
133
134 static inline const AngleAxis Identity() { return AngleAxis(0, Vector3::UnitX()); }
135
140 bool isApprox(const AngleAxis& other, typename NumTraits<Scalar>::Real prec = NumTraits<Scalar>::dummy_precision()) const
141 { return m_axis.isApprox(other.m_axis, prec) && internal::isApprox(m_angle,other.m_angle, prec); }
142};
143
150
157template<typename Scalar>
158template<typename QuatDerived>
159AngleAxis<Scalar>& AngleAxis<Scalar>::operator=(const QuaternionBase<QuatDerived>& q)
160{
161 using std::acos;
162 using std::min;
163 using std::max;
164 Scalar n2 = q.vec().squaredNorm();
165 if (n2 < NumTraits<Scalar>::dummy_precision()*NumTraits<Scalar>::dummy_precision())
166 {
167 m_angle = 0;
168 m_axis << 1, 0, 0;
169 }
170 else
171 {
172 m_angle = Scalar(2)*acos((min)((max)(Scalar(-1),q.w()),Scalar(1)));
173 m_axis = q.vec() / internal::sqrt(n2);
174 }
175 return *this;
176}
177
180template<typename Scalar>
181template<typename Derived>
182AngleAxis<Scalar>& AngleAxis<Scalar>::operator=(const MatrixBase<Derived>& mat)
183{
184 // Since a direct conversion would not be really faster,
185 // let's use the robust Quaternion implementation:
186 return *this = QuaternionType(mat);
187}
188
192template<typename Scalar>
193template<typename Derived>
194AngleAxis<Scalar>& AngleAxis<Scalar>::fromRotationMatrix(const MatrixBase<Derived>& mat)
195{
196 return *this = QuaternionType(mat);
197}
198
201template<typename Scalar>
202typename AngleAxis<Scalar>::Matrix3
204{
205 Matrix3 res;
206 Vector3 sin_axis = internal::sin(m_angle) * m_axis;
207 Scalar c = internal::cos(m_angle);
208 Vector3 cos1_axis = (Scalar(1)-c) * m_axis;
209
210 Scalar tmp;
211 tmp = cos1_axis.x() * m_axis.y();
212 res.coeffRef(0,1) = tmp - sin_axis.z();
213 res.coeffRef(1,0) = tmp + sin_axis.z();
214
215 tmp = cos1_axis.x() * m_axis.z();
216 res.coeffRef(0,2) = tmp + sin_axis.y();
217 res.coeffRef(2,0) = tmp - sin_axis.y();
218
219 tmp = cos1_axis.y() * m_axis.z();
220 res.coeffRef(1,2) = tmp - sin_axis.x();
221 res.coeffRef(2,1) = tmp + sin_axis.x();
222
223 res.diagonal() = (cos1_axis.cwiseProduct(m_axis)).array() + c;
224
225 return res;
226}
227
228} // end namespace Eigen
229
230#endif // EIGEN_ANGLEAXIS_H
Represents a 3D rotation as a rotation angle around an arbitrary 3D axis.
Definition AngleAxis.h:50
AngleAxis(Scalar angle, const MatrixBase< Derived > &axis)
Definition AngleAxis.h:79
Matrix3 toRotationMatrix(void) const
Definition AngleAxis.h:203
AngleAxis()
Definition AngleAxis.h:72
AngleAxis(const MatrixBase< Derived > &m)
Definition AngleAxis.h:84
bool isApprox(const AngleAxis &other, typename NumTraits< Scalar >::Real prec=NumTraits< Scalar >::dummy_precision()) const
Definition AngleAxis.h:140
internal::cast_return_type< AngleAxis, AngleAxis< NewScalarType > >::type cast() const
Definition AngleAxis.h:123
QuaternionType operator*(const AngleAxis &other) const
Definition AngleAxis.h:93
AngleAxis(const QuaternionBase< QuatDerived > &q)
Definition AngleAxis.h:81
AngleAxis inverse() const
Definition AngleAxis.h:105
AngleAxis(const AngleAxis< OtherScalarType > &other)
Definition AngleAxis.h:128
_Scalar Scalar
Definition AngleAxis.h:59
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
static const BasisReturnType UnitX()
Definition CwiseNullaryOp.h:829
MatrixBase< Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::template DiagonalIndexReturnType< Index >::Type diagonal()
Definition Diagonal.h:220
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:129
Base class for quaternion expressions.
Definition Quaternion.h:36
Scalar w() const
Definition Quaternion.h:66
const VectorBlock< const Coefficients, 3 > vec() const
Definition Quaternion.h:78
The quaternion class used to represent 3D orientations and rotations.
Definition Quaternion.h:229
Common base class for compact rotation representations.
Definition RotationBase.h:30
AngleAxis< double > AngleAxisd
Definition AngleAxis.h:149
AngleAxis< float > AngleAxisf
Definition AngleAxis.h:146
Definition LDLT.h:18