source: pacpussensors/trunk/Vislab/lib3dv/eigen/Eigen/src/Geometry/AngleAxis.h@ 136

Last change on this file since 136 was 136, checked in by ldecherf, 7 years ago

Doc

File size: 7.7 KB
Line 
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
15/** \geometry_module \ingroup Geometry_Module
16 *
17 * \class AngleAxis
18 *
19 * \brief Represents a 3D rotation as a rotation angle around an arbitrary 3D axis
20 *
21 * \param _Scalar the scalar type, i.e., the type of the coefficients.
22 *
23 * \warning When setting up an AngleAxis object, the axis vector \b must \b be \b normalized.
24 *
25 * The following two typedefs are provided for convenience:
26 * \li \c AngleAxisf for \c float
27 * \li \c AngleAxisd for \c double
28 *
29 * Combined with MatrixBase::Unit{X,Y,Z}, AngleAxis can be used to easily
30 * mimic Euler-angles. Here is an example:
31 * \include AngleAxis_mimic_euler.cpp
32 * Output: \verbinclude AngleAxis_mimic_euler.out
33 *
34 * \note This class is not aimed to be used to store a rotation transformation,
35 * but rather to make easier the creation of other rotation (Quaternion, rotation Matrix)
36 * and transformation objects.
37 *
38 * \sa class Quaternion, class Transform, MatrixBase::UnitX()
39 */
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 };
58 /** the scalar type of the coefficients */
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
71 /** Default constructor without initialization. */
72 AngleAxis() {}
73 /** Constructs and initialize the angle-axis rotation from an \a angle in radian
74 * and an \a axis which \b must \b be \b normalized.
75 *
76 * \warning If the \a axis vector is not normalized, then the angle-axis object
77 * represents an invalid rotation. */
78 template<typename Derived>
79 inline AngleAxis(const Scalar& angle, const MatrixBase<Derived>& axis) : m_axis(axis), m_angle(angle) {}
80 /** Constructs and initialize the angle-axis rotation from a quaternion \a q. */
81 template<typename QuatDerived> inline explicit AngleAxis(const QuaternionBase<QuatDerived>& q) { *this = q; }
82 /** Constructs and initialize the angle-axis rotation from a 3x3 rotation matrix. */
83 template<typename Derived>
84 inline explicit AngleAxis(const MatrixBase<Derived>& m) { *this = m; }
85
86 /** \returns the value of the rotation angle in radian */
87 Scalar angle() const { return m_angle; }
88 /** \returns a read-write reference to the stored angle in radian */
89 Scalar& angle() { return m_angle; }
90
91 /** \returns the rotation axis */
92 const Vector3& axis() const { return m_axis; }
93 /** \returns a read-write reference to the stored rotation axis.
94 *
95 * \warning The rotation axis must remain a \b unit vector.
96 */
97 Vector3& axis() { return m_axis; }
98
99 /** Concatenates two rotations */
100 inline QuaternionType operator* (const AngleAxis& other) const
101 { return QuaternionType(*this) * QuaternionType(other); }
102
103 /** Concatenates two rotations */
104 inline QuaternionType operator* (const QuaternionType& other) const
105 { return QuaternionType(*this) * other; }
106
107 /** Concatenates two rotations */
108 friend inline QuaternionType operator* (const QuaternionType& a, const AngleAxis& b)
109 { return a * QuaternionType(b); }
110
111 /** \returns the inverse rotation, i.e., an angle-axis with opposite rotation angle */
112 AngleAxis inverse() const
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
124 /** \returns \c *this with scalar type casted to \a NewScalarType
125 *
126 * Note that if \a NewScalarType is equal to the current scalar type of \c *this
127 * then this function smartly returns a const reference to \c *this.
128 */
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
133 /** Copy constructor with scalar type conversion */
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
143 /** \returns \c true if \c *this is approximately equal to \a other, within the precision
144 * determined by \a prec.
145 *
146 * \sa MatrixBase::isApprox() */
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
151/** \ingroup Geometry_Module
152 * single precision angle-axis type */
153typedef AngleAxis<float> AngleAxisf;
154/** \ingroup Geometry_Module
155 * double precision angle-axis type */
156typedef AngleAxis<double> AngleAxisd;
157
158/** Set \c *this from a \b unit quaternion.
159 * The axis is normalized.
160 *
161 * \warning As any other method dealing with quaternion, if the input quaternion
162 * is not normalized then the result is undefined.
163 */
164template<typename Scalar>
165template<typename QuatDerived>
166AngleAxis<Scalar>& AngleAxis<Scalar>::operator=(const QuaternionBase<QuatDerived>& q)
167{
168 using std::acos;
169 using std::min;
170 using std::max;
171 using std::sqrt;
172 Scalar n2 = q.vec().squaredNorm();
173 if (n2 < NumTraits<Scalar>::dummy_precision()*NumTraits<Scalar>::dummy_precision())
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
186/** Set \c *this from a 3x3 rotation matrix \a mat.
187 */
188template<typename Scalar>
189template<typename Derived>
190AngleAxis<Scalar>& AngleAxis<Scalar>::operator=(const MatrixBase<Derived>& mat)
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
197/**
198* \brief Sets \c *this from a 3x3 rotation matrix.
199**/
200template<typename Scalar>
201template<typename Derived>
202AngleAxis<Scalar>& AngleAxis<Scalar>::fromRotationMatrix(const MatrixBase<Derived>& mat)
203{
204 return *this = QuaternionType(mat);
205}
206
207/** Constructs and \returns an equivalent 3x3 rotation matrix.
208 */
209template<typename Scalar>
210typename AngleAxis<Scalar>::Matrix3
211AngleAxis<Scalar>::toRotationMatrix(void) const
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
Note: See TracBrowser for help on using the repository browser.