AngleAxis.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) 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 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
43 namespace internal {
44 template<typename Scalar_> struct traits<AngleAxis<Scalar_> >
45 {
46  typedef Scalar_ Scalar;
47 };
48 }
49 
50 template<typename Scalar_>
51 class AngleAxis : public RotationBase<AngleAxis<Scalar_>,3>
52 {
54 
55 public:
56 
57  using Base::operator*;
58 
59  enum { Dim = 3 };
61  typedef Scalar_ Scalar;
65 
66 protected:
67 
70 
71 public:
72 
80  template<typename Derived>
86  template<typename QuatDerived>
87  EIGEN_DEVICE_FUNC inline explicit AngleAxis(const QuaternionBase<QuatDerived>& q) { *this = q; }
89  template<typename Derived>
90  EIGEN_DEVICE_FUNC inline explicit AngleAxis(const MatrixBase<Derived>& m) { *this = m; }
91 
93  EIGEN_DEVICE_FUNC Scalar angle() const { return m_angle; }
96 
98  EIGEN_DEVICE_FUNC const Vector3& axis() const { return m_axis; }
104 
107  { return QuaternionType(*this) * QuaternionType(other); }
108 
111  { return QuaternionType(*this) * other; }
112 
115  { return a * QuaternionType(b); }
116 
119  { return AngleAxis(-m_angle, m_axis); }
120 
121  template<class QuatDerived>
123  template<typename Derived>
125 
126  template<typename Derived>
129 
135  template<typename NewScalarType>
136  EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type cast() const
137  { return typename internal::cast_return_type<AngleAxis,AngleAxis<NewScalarType> >::type(*this); }
138 
140  template<typename OtherScalarType>
142  {
143  m_axis = other.axis().template cast<Scalar>();
144  m_angle = Scalar(other.angle());
145  }
146 
147  EIGEN_DEVICE_FUNC static inline const AngleAxis Identity() { return AngleAxis(Scalar(0), Vector3::UnitX()); }
148 
154  { return m_axis.isApprox(other.m_axis, prec) && internal::isApprox(m_angle,other.m_angle, prec); }
155 };
156 
163 
170 template<typename Scalar>
171 template<typename QuatDerived>
173 {
176  Scalar n = q.vec().norm();
178  n = q.vec().stableNorm();
179 
180  if (n != Scalar(0))
181  {
182  m_angle = Scalar(2)*atan2(n, abs(q.w()));
183  if(q.w() < Scalar(0))
184  n = -n;
185  m_axis = q.vec() / n;
186  }
187  else
188  {
189  m_angle = Scalar(0);
190  m_axis << Scalar(1), Scalar(0), Scalar(0);
191  }
192  return *this;
193 }
194 
197 template<typename Scalar>
198 template<typename Derived>
200 {
201  // Since a direct conversion would not be really faster,
202  // let's use the robust Quaternion implementation:
203  return *this = QuaternionType(mat);
204 }
205 
209 template<typename Scalar>
210 template<typename Derived>
212 {
213  return *this = QuaternionType(mat);
214 }
215 
218 template<typename Scalar>
221 {
224  Matrix3 res;
225  Vector3 sin_axis = sin(m_angle) * m_axis;
226  Scalar c = cos(m_angle);
227  Vector3 cos1_axis = (Scalar(1)-c) * m_axis;
228 
229  Scalar tmp;
230  tmp = cos1_axis.x() * m_axis.y();
231  res.coeffRef(0,1) = tmp - sin_axis.z();
232  res.coeffRef(1,0) = tmp + sin_axis.z();
233 
234  tmp = cos1_axis.x() * m_axis.z();
235  res.coeffRef(0,2) = tmp + sin_axis.y();
236  res.coeffRef(2,0) = tmp - sin_axis.y();
237 
238  tmp = cos1_axis.y() * m_axis.z();
239  res.coeffRef(1,2) = tmp - sin_axis.x();
240  res.coeffRef(2,1) = tmp + sin_axis.x();
241 
242  res.diagonal() = (cos1_axis.cwiseProduct(m_axis)).array() + c;
243 
244  return res;
245 }
246 
247 } // end namespace Eigen
248 
249 #endif // EIGEN_ANGLEAXIS_H
Matrix3f m
const CwiseBinaryOp< atan2< Scalar >, const Derived, const OtherDerived > atan2(const EIGEN_CURRENT_STORAGE_BASE_CLASS< OtherDerived > &other) const
Array< int, 3, 1 > b
int n
Array33i c
#define EIGEN_USING_STD(FUNC)
Definition: Macros.h:1080
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Represents a 3D rotation as a rotation angle around an arbitrary 3D axis.
Definition: AngleAxis.h:52
Scalar & angle()
Definition: AngleAxis.h:95
const Vector3 & axis() const
Definition: AngleAxis.h:98
AngleAxis(const AngleAxis< OtherScalarType > &other)
Definition: AngleAxis.h:141
Scalar_ Scalar
Definition: AngleAxis.h:61
QuaternionType operator*(const AngleAxis &other) const
Definition: AngleAxis.h:106
Vector3 m_axis
Definition: AngleAxis.h:68
Vector3 & axis()
Definition: AngleAxis.h:103
AngleAxis(const QuaternionBase< QuatDerived > &q)
Definition: AngleAxis.h:87
AngleAxis & operator=(const MatrixBase< Derived > &m)
AngleAxis & fromRotationMatrix(const MatrixBase< Derived > &m)
AngleAxis(const Scalar &angle, const MatrixBase< Derived > &axis)
Definition: AngleAxis.h:82
AngleAxis(const MatrixBase< Derived > &m)
Definition: AngleAxis.h:90
Quaternion< Scalar > QuaternionType
Definition: AngleAxis.h:64
AngleAxis inverse() const
Definition: AngleAxis.h:118
Matrix< Scalar, 3, 1 > Vector3
Definition: AngleAxis.h:63
internal::cast_return_type< AngleAxis, AngleAxis< NewScalarType > >::type cast() const
Definition: AngleAxis.h:136
bool isApprox(const AngleAxis &other, const typename NumTraits< Scalar >::Real &prec=NumTraits< Scalar >::dummy_precision()) const
Definition: AngleAxis.h:153
Matrix3 toRotationMatrix(void) const
Definition: AngleAxis.h:220
RotationBase< AngleAxis< Scalar_ >, 3 > Base
Definition: AngleAxis.h:53
Matrix< Scalar, 3, 3 > Matrix3
Definition: AngleAxis.h:62
Scalar angle() const
Definition: AngleAxis.h:93
static const AngleAxis Identity()
Definition: AngleAxis.h:147
AngleAxis & operator=(const QuaternionBase< QuatDerived > &q)
Scalar m_angle
Definition: AngleAxis.h:69
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
static const BasisReturnType UnitX()
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
Base class for quaternion expressions.
Definition: Quaternion.h:38
CoeffReturnType w() const
Definition: Quaternion.h:74
const VectorBlock< const Coefficients, 3 > vec() const
Definition: Quaternion.h:86
The quaternion class used to represent 3D orientations and rotations.
Definition: Quaternion.h:276
Common base class for compact rotation representations.
Definition: RotationBase.h:32
AngleAxis< float > AngleAxisf
Definition: AngleAxis.h:159
AngleAxis< double > AngleAxisd
Definition: AngleAxis.h:162
bool isApprox(const Scalar &x, const Scalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
: InteropHeaders
Definition: Core:139
std::array< T, N > array
Definition: EmulateArray.h:256
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_cos_op< typename Derived::Scalar >, const Derived > cos(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sin_op< typename Derived::Scalar >, const Derived > sin(const Eigen::ArrayBase< Derived > &x)
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231