Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/Eigen/src/Geometry/Quaternion.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 // Copyright (C) 2009 Mathieu Gautier <mathieu.gautier@cea.fr>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_QUATERNION_H
0012 #define EIGEN_QUATERNION_H
0013 namespace Eigen { 
0014 
0015 
0016 /***************************************************************************
0017 * Definition of QuaternionBase<Derived>
0018 * The implementation is at the end of the file
0019 ***************************************************************************/
0020 
0021 namespace internal {
0022 template<typename Other,
0023          int OtherRows=Other::RowsAtCompileTime,
0024          int OtherCols=Other::ColsAtCompileTime>
0025 struct quaternionbase_assign_impl;
0026 }
0027 
0028 /** \geometry_module \ingroup Geometry_Module
0029   * \class QuaternionBase
0030   * \brief Base class for quaternion expressions
0031   * \tparam Derived derived type (CRTP)
0032   * \sa class Quaternion
0033   */
0034 template<class Derived>
0035 class QuaternionBase : public RotationBase<Derived, 3>
0036 {
0037  public:
0038   typedef RotationBase<Derived, 3> Base;
0039 
0040   using Base::operator*;
0041   using Base::derived;
0042 
0043   typedef typename internal::traits<Derived>::Scalar Scalar;
0044   typedef typename NumTraits<Scalar>::Real RealScalar;
0045   typedef typename internal::traits<Derived>::Coefficients Coefficients;
0046   typedef typename Coefficients::CoeffReturnType CoeffReturnType;
0047   typedef typename internal::conditional<bool(internal::traits<Derived>::Flags&LvalueBit),
0048                                         Scalar&, CoeffReturnType>::type NonConstCoeffReturnType;
0049 
0050 
0051   enum {
0052     Flags = Eigen::internal::traits<Derived>::Flags
0053   };
0054 
0055  // typedef typename Matrix<Scalar,4,1> Coefficients;
0056   /** the type of a 3D vector */
0057   typedef Matrix<Scalar,3,1> Vector3;
0058   /** the equivalent rotation matrix type */
0059   typedef Matrix<Scalar,3,3> Matrix3;
0060   /** the equivalent angle-axis type */
0061   typedef AngleAxis<Scalar> AngleAxisType;
0062 
0063 
0064 
0065   /** \returns the \c x coefficient */
0066   EIGEN_DEVICE_FUNC inline CoeffReturnType x() const { return this->derived().coeffs().coeff(0); }
0067   /** \returns the \c y coefficient */
0068   EIGEN_DEVICE_FUNC inline CoeffReturnType y() const { return this->derived().coeffs().coeff(1); }
0069   /** \returns the \c z coefficient */
0070   EIGEN_DEVICE_FUNC inline CoeffReturnType z() const { return this->derived().coeffs().coeff(2); }
0071   /** \returns the \c w coefficient */
0072   EIGEN_DEVICE_FUNC inline CoeffReturnType w() const { return this->derived().coeffs().coeff(3); }
0073 
0074   /** \returns a reference to the \c x coefficient (if Derived is a non-const lvalue) */
0075   EIGEN_DEVICE_FUNC inline NonConstCoeffReturnType x() { return this->derived().coeffs().x(); }
0076   /** \returns a reference to the \c y coefficient (if Derived is a non-const lvalue) */
0077   EIGEN_DEVICE_FUNC inline NonConstCoeffReturnType y() { return this->derived().coeffs().y(); }
0078   /** \returns a reference to the \c z coefficient (if Derived is a non-const lvalue) */
0079   EIGEN_DEVICE_FUNC inline NonConstCoeffReturnType z() { return this->derived().coeffs().z(); }
0080   /** \returns a reference to the \c w coefficient (if Derived is a non-const lvalue) */
0081   EIGEN_DEVICE_FUNC inline NonConstCoeffReturnType w() { return this->derived().coeffs().w(); }
0082 
0083   /** \returns a read-only vector expression of the imaginary part (x,y,z) */
0084   EIGEN_DEVICE_FUNC inline const VectorBlock<const Coefficients,3> vec() const { return coeffs().template head<3>(); }
0085 
0086   /** \returns a vector expression of the imaginary part (x,y,z) */
0087   EIGEN_DEVICE_FUNC inline VectorBlock<Coefficients,3> vec() { return coeffs().template head<3>(); }
0088 
0089   /** \returns a read-only vector expression of the coefficients (x,y,z,w) */
0090   EIGEN_DEVICE_FUNC inline const typename internal::traits<Derived>::Coefficients& coeffs() const { return derived().coeffs(); }
0091 
0092   /** \returns a vector expression of the coefficients (x,y,z,w) */
0093   EIGEN_DEVICE_FUNC inline typename internal::traits<Derived>::Coefficients& coeffs() { return derived().coeffs(); }
0094 
0095   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE QuaternionBase<Derived>& operator=(const QuaternionBase<Derived>& other);
0096   template<class OtherDerived> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& operator=(const QuaternionBase<OtherDerived>& other);
0097 
0098 // disabled this copy operator as it is giving very strange compilation errors when compiling
0099 // test_stdvector with GCC 4.4.2. This looks like a GCC bug though, so feel free to re-enable it if it's
0100 // useful; however notice that we already have the templated operator= above and e.g. in MatrixBase
0101 // we didn't have to add, in addition to templated operator=, such a non-templated copy operator.
0102 //  Derived& operator=(const QuaternionBase& other)
0103 //  { return operator=<Derived>(other); }
0104 
0105   EIGEN_DEVICE_FUNC Derived& operator=(const AngleAxisType& aa);
0106   template<class OtherDerived> EIGEN_DEVICE_FUNC Derived& operator=(const MatrixBase<OtherDerived>& m);
0107 
0108   /** \returns a quaternion representing an identity rotation
0109     * \sa MatrixBase::Identity()
0110     */
0111   EIGEN_DEVICE_FUNC static inline Quaternion<Scalar> Identity() { return Quaternion<Scalar>(Scalar(1), Scalar(0), Scalar(0), Scalar(0)); }
0112 
0113   /** \sa QuaternionBase::Identity(), MatrixBase::setIdentity()
0114     */
0115   EIGEN_DEVICE_FUNC inline QuaternionBase& setIdentity() { coeffs() << Scalar(0), Scalar(0), Scalar(0), Scalar(1); return *this; }
0116 
0117   /** \returns the squared norm of the quaternion's coefficients
0118     * \sa QuaternionBase::norm(), MatrixBase::squaredNorm()
0119     */
0120   EIGEN_DEVICE_FUNC inline Scalar squaredNorm() const { return coeffs().squaredNorm(); }
0121 
0122   /** \returns the norm of the quaternion's coefficients
0123     * \sa QuaternionBase::squaredNorm(), MatrixBase::norm()
0124     */
0125   EIGEN_DEVICE_FUNC inline Scalar norm() const { return coeffs().norm(); }
0126 
0127   /** Normalizes the quaternion \c *this
0128     * \sa normalized(), MatrixBase::normalize() */
0129   EIGEN_DEVICE_FUNC inline void normalize() { coeffs().normalize(); }
0130   /** \returns a normalized copy of \c *this
0131     * \sa normalize(), MatrixBase::normalized() */
0132   EIGEN_DEVICE_FUNC inline Quaternion<Scalar> normalized() const { return Quaternion<Scalar>(coeffs().normalized()); }
0133 
0134     /** \returns the dot product of \c *this and \a other
0135     * Geometrically speaking, the dot product of two unit quaternions
0136     * corresponds to the cosine of half the angle between the two rotations.
0137     * \sa angularDistance()
0138     */
0139   template<class OtherDerived> EIGEN_DEVICE_FUNC inline Scalar dot(const QuaternionBase<OtherDerived>& other) const { return coeffs().dot(other.coeffs()); }
0140 
0141   template<class OtherDerived> EIGEN_DEVICE_FUNC Scalar angularDistance(const QuaternionBase<OtherDerived>& other) const;
0142 
0143   /** \returns an equivalent 3x3 rotation matrix */
0144   EIGEN_DEVICE_FUNC inline Matrix3 toRotationMatrix() const;
0145 
0146   /** \returns the quaternion which transform \a a into \a b through a rotation */
0147   template<typename Derived1, typename Derived2>
0148   EIGEN_DEVICE_FUNC Derived& setFromTwoVectors(const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b);
0149 
0150   template<class OtherDerived> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<Scalar> operator* (const QuaternionBase<OtherDerived>& q) const;
0151   template<class OtherDerived> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& operator*= (const QuaternionBase<OtherDerived>& q);
0152 
0153   /** \returns the quaternion describing the inverse rotation */
0154   EIGEN_DEVICE_FUNC Quaternion<Scalar> inverse() const;
0155 
0156   /** \returns the conjugated quaternion */
0157   EIGEN_DEVICE_FUNC Quaternion<Scalar> conjugate() const;
0158 
0159   template<class OtherDerived> EIGEN_DEVICE_FUNC Quaternion<Scalar> slerp(const Scalar& t, const QuaternionBase<OtherDerived>& other) const;
0160 
0161   /** \returns true if each coefficients of \c *this and \a other are all exactly equal.
0162     * \warning When using floating point scalar values you probably should rather use a
0163     *          fuzzy comparison such as isApprox()
0164     * \sa isApprox(), operator!= */
0165   template<class OtherDerived>
0166   EIGEN_DEVICE_FUNC inline bool operator==(const QuaternionBase<OtherDerived>& other) const
0167   { return coeffs() == other.coeffs(); }
0168 
0169   /** \returns true if at least one pair of coefficients of \c *this and \a other are not exactly equal to each other.
0170     * \warning When using floating point scalar values you probably should rather use a
0171     *          fuzzy comparison such as isApprox()
0172     * \sa isApprox(), operator== */
0173   template<class OtherDerived>
0174   EIGEN_DEVICE_FUNC inline bool operator!=(const QuaternionBase<OtherDerived>& other) const
0175   { return coeffs() != other.coeffs(); }
0176 
0177   /** \returns \c true if \c *this is approximately equal to \a other, within the precision
0178     * determined by \a prec.
0179     *
0180     * \sa MatrixBase::isApprox() */
0181   template<class OtherDerived>
0182   EIGEN_DEVICE_FUNC bool isApprox(const QuaternionBase<OtherDerived>& other, const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const
0183   { return coeffs().isApprox(other.coeffs(), prec); }
0184 
0185   /** return the result vector of \a v through the rotation*/
0186   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Vector3 _transformVector(const Vector3& v) const;
0187 
0188   #ifdef EIGEN_PARSED_BY_DOXYGEN
0189   /** \returns \c *this with scalar type casted to \a NewScalarType
0190     *
0191     * Note that if \a NewScalarType is equal to the current scalar type of \c *this
0192     * then this function smartly returns a const reference to \c *this.
0193     */
0194   template<typename NewScalarType>
0195   EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<Derived,Quaternion<NewScalarType> >::type cast() const;
0196 
0197   #else
0198 
0199   template<typename NewScalarType>
0200   EIGEN_DEVICE_FUNC inline
0201   typename internal::enable_if<internal::is_same<Scalar,NewScalarType>::value,const Derived&>::type cast() const
0202   {
0203     return derived();
0204   }
0205 
0206   template<typename NewScalarType>
0207   EIGEN_DEVICE_FUNC inline
0208   typename internal::enable_if<!internal::is_same<Scalar,NewScalarType>::value,Quaternion<NewScalarType> >::type cast() const
0209   {
0210     return Quaternion<NewScalarType>(coeffs().template cast<NewScalarType>());
0211   }
0212   #endif
0213 
0214 #ifndef EIGEN_NO_IO
0215   friend std::ostream& operator<<(std::ostream& s, const QuaternionBase<Derived>& q) {
0216     s << q.x() << "i + " << q.y() << "j + " << q.z() << "k" << " + " << q.w();
0217     return s;
0218   }
0219 #endif
0220 
0221 #ifdef EIGEN_QUATERNIONBASE_PLUGIN
0222 # include EIGEN_QUATERNIONBASE_PLUGIN
0223 #endif
0224 protected:
0225   EIGEN_DEFAULT_COPY_CONSTRUCTOR(QuaternionBase)
0226   EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(QuaternionBase)
0227 };
0228 
0229 /***************************************************************************
0230 * Definition/implementation of Quaternion<Scalar>
0231 ***************************************************************************/
0232 
0233 /** \geometry_module \ingroup Geometry_Module
0234   *
0235   * \class Quaternion
0236   *
0237   * \brief The quaternion class used to represent 3D orientations and rotations
0238   *
0239   * \tparam _Scalar the scalar type, i.e., the type of the coefficients
0240   * \tparam _Options controls the memory alignment of the coefficients. Can be \# AutoAlign or \# DontAlign. Default is AutoAlign.
0241   *
0242   * This class represents a quaternion \f$ w+xi+yj+zk \f$ that is a convenient representation of
0243   * orientations and rotations of objects in three dimensions. Compared to other representations
0244   * like Euler angles or 3x3 matrices, quaternions offer the following advantages:
0245   * \li \b compact storage (4 scalars)
0246   * \li \b efficient to compose (28 flops),
0247   * \li \b stable spherical interpolation
0248   *
0249   * The following two typedefs are provided for convenience:
0250   * \li \c Quaternionf for \c float
0251   * \li \c Quaterniond for \c double
0252   *
0253   * \warning Operations interpreting the quaternion as rotation have undefined behavior if the quaternion is not normalized.
0254   *
0255   * \sa  class AngleAxis, class Transform
0256   */
0257 
0258 namespace internal {
0259 template<typename _Scalar,int _Options>
0260 struct traits<Quaternion<_Scalar,_Options> >
0261 {
0262   typedef Quaternion<_Scalar,_Options> PlainObject;
0263   typedef _Scalar Scalar;
0264   typedef Matrix<_Scalar,4,1,_Options> Coefficients;
0265   enum{
0266     Alignment = internal::traits<Coefficients>::Alignment,
0267     Flags = LvalueBit
0268   };
0269 };
0270 }
0271 
0272 template<typename _Scalar, int _Options>
0273 class Quaternion : public QuaternionBase<Quaternion<_Scalar,_Options> >
0274 {
0275 public:
0276   typedef QuaternionBase<Quaternion<_Scalar,_Options> > Base;
0277   enum { NeedsAlignment = internal::traits<Quaternion>::Alignment>0 };
0278 
0279   typedef _Scalar Scalar;
0280 
0281   EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Quaternion)
0282   using Base::operator*=;
0283 
0284   typedef typename internal::traits<Quaternion>::Coefficients Coefficients;
0285   typedef typename Base::AngleAxisType AngleAxisType;
0286 
0287   /** Default constructor leaving the quaternion uninitialized. */
0288   EIGEN_DEVICE_FUNC inline Quaternion() {}
0289 
0290   /** Constructs and initializes the quaternion \f$ w+xi+yj+zk \f$ from
0291     * its four coefficients \a w, \a x, \a y and \a z.
0292     *
0293     * \warning Note the order of the arguments: the real \a w coefficient first,
0294     * while internally the coefficients are stored in the following order:
0295     * [\c x, \c y, \c z, \c w]
0296     */
0297   EIGEN_DEVICE_FUNC inline Quaternion(const Scalar& w, const Scalar& x, const Scalar& y, const Scalar& z) : m_coeffs(x, y, z, w){}
0298 
0299   /** Constructs and initialize a quaternion from the array data */
0300   EIGEN_DEVICE_FUNC explicit inline Quaternion(const Scalar* data) : m_coeffs(data) {}
0301 
0302   /** Copy constructor */
0303   template<class Derived> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion(const QuaternionBase<Derived>& other) { this->Base::operator=(other); }
0304 
0305   /** Constructs and initializes a quaternion from the angle-axis \a aa */
0306   EIGEN_DEVICE_FUNC explicit inline Quaternion(const AngleAxisType& aa) { *this = aa; }
0307 
0308   /** Constructs and initializes a quaternion from either:
0309     *  - a rotation matrix expression,
0310     *  - a 4D vector expression representing quaternion coefficients.
0311     */
0312   template<typename Derived>
0313   EIGEN_DEVICE_FUNC explicit inline Quaternion(const MatrixBase<Derived>& other) { *this = other; }
0314 
0315   /** Explicit copy constructor with scalar conversion */
0316   template<typename OtherScalar, int OtherOptions>
0317   EIGEN_DEVICE_FUNC explicit inline Quaternion(const Quaternion<OtherScalar, OtherOptions>& other)
0318   { m_coeffs = other.coeffs().template cast<Scalar>(); }
0319 
0320 #if EIGEN_HAS_RVALUE_REFERENCES
0321   // We define a copy constructor, which means we don't get an implicit move constructor or assignment operator.
0322   /** Default move constructor */
0323   EIGEN_DEVICE_FUNC inline Quaternion(Quaternion&& other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_constructible<Scalar>::value)
0324     : m_coeffs(std::move(other.coeffs()))
0325   {}
0326 
0327   /** Default move assignment operator */
0328   EIGEN_DEVICE_FUNC Quaternion& operator=(Quaternion&& other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_assignable<Scalar>::value)
0329   {
0330     m_coeffs = std::move(other.coeffs());
0331     return *this;
0332   }
0333 #endif
0334 
0335   EIGEN_DEVICE_FUNC static Quaternion UnitRandom();
0336 
0337   template<typename Derived1, typename Derived2>
0338   EIGEN_DEVICE_FUNC static Quaternion FromTwoVectors(const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b);
0339 
0340   EIGEN_DEVICE_FUNC inline Coefficients& coeffs() { return m_coeffs;}
0341   EIGEN_DEVICE_FUNC inline const Coefficients& coeffs() const { return m_coeffs;}
0342 
0343   EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(bool(NeedsAlignment))
0344   
0345 #ifdef EIGEN_QUATERNION_PLUGIN
0346 # include EIGEN_QUATERNION_PLUGIN
0347 #endif
0348 
0349 protected:
0350   Coefficients m_coeffs;
0351   
0352 #ifndef EIGEN_PARSED_BY_DOXYGEN
0353     static EIGEN_STRONG_INLINE void _check_template_params()
0354     {
0355       EIGEN_STATIC_ASSERT( (_Options & DontAlign) == _Options,
0356         INVALID_MATRIX_TEMPLATE_PARAMETERS)
0357     }
0358 #endif
0359 };
0360 
0361 /** \ingroup Geometry_Module
0362   * single precision quaternion type */
0363 typedef Quaternion<float> Quaternionf;
0364 /** \ingroup Geometry_Module
0365   * double precision quaternion type */
0366 typedef Quaternion<double> Quaterniond;
0367 
0368 /***************************************************************************
0369 * Specialization of Map<Quaternion<Scalar>>
0370 ***************************************************************************/
0371 
0372 namespace internal {
0373   template<typename _Scalar, int _Options>
0374   struct traits<Map<Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
0375   {
0376     typedef Map<Matrix<_Scalar,4,1>, _Options> Coefficients;
0377   };
0378 }
0379 
0380 namespace internal {
0381   template<typename _Scalar, int _Options>
0382   struct traits<Map<const Quaternion<_Scalar>, _Options> > : traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> >
0383   {
0384     typedef Map<const Matrix<_Scalar,4,1>, _Options> Coefficients;
0385     typedef traits<Quaternion<_Scalar, (int(_Options)&Aligned)==Aligned ? AutoAlign : DontAlign> > TraitsBase;
0386     enum {
0387       Flags = TraitsBase::Flags & ~LvalueBit
0388     };
0389   };
0390 }
0391 
0392 /** \ingroup Geometry_Module
0393   * \brief Quaternion expression mapping a constant memory buffer
0394   *
0395   * \tparam _Scalar the type of the Quaternion coefficients
0396   * \tparam _Options see class Map
0397   *
0398   * This is a specialization of class Map for Quaternion. This class allows to view
0399   * a 4 scalar memory buffer as an Eigen's Quaternion object.
0400   *
0401   * \sa class Map, class Quaternion, class QuaternionBase
0402   */
0403 template<typename _Scalar, int _Options>
0404 class Map<const Quaternion<_Scalar>, _Options >
0405   : public QuaternionBase<Map<const Quaternion<_Scalar>, _Options> >
0406 {
0407   public:
0408     typedef QuaternionBase<Map<const Quaternion<_Scalar>, _Options> > Base;
0409 
0410     typedef _Scalar Scalar;
0411     typedef typename internal::traits<Map>::Coefficients Coefficients;
0412     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
0413     using Base::operator*=;
0414 
0415     /** Constructs a Mapped Quaternion object from the pointer \a coeffs
0416       *
0417       * The pointer \a coeffs must reference the four coefficients of Quaternion in the following order:
0418       * \code *coeffs == {x, y, z, w} \endcode
0419       *
0420       * If the template parameter _Options is set to #Aligned, then the pointer coeffs must be aligned. */
0421     EIGEN_DEVICE_FUNC explicit EIGEN_STRONG_INLINE Map(const Scalar* coeffs) : m_coeffs(coeffs) {}
0422 
0423     EIGEN_DEVICE_FUNC inline const Coefficients& coeffs() const { return m_coeffs;}
0424 
0425   protected:
0426     const Coefficients m_coeffs;
0427 };
0428 
0429 /** \ingroup Geometry_Module
0430   * \brief Expression of a quaternion from a memory buffer
0431   *
0432   * \tparam _Scalar the type of the Quaternion coefficients
0433   * \tparam _Options see class Map
0434   *
0435   * This is a specialization of class Map for Quaternion. This class allows to view
0436   * a 4 scalar memory buffer as an Eigen's  Quaternion object.
0437   *
0438   * \sa class Map, class Quaternion, class QuaternionBase
0439   */
0440 template<typename _Scalar, int _Options>
0441 class Map<Quaternion<_Scalar>, _Options >
0442   : public QuaternionBase<Map<Quaternion<_Scalar>, _Options> >
0443 {
0444   public:
0445     typedef QuaternionBase<Map<Quaternion<_Scalar>, _Options> > Base;
0446 
0447     typedef _Scalar Scalar;
0448     typedef typename internal::traits<Map>::Coefficients Coefficients;
0449     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
0450     using Base::operator*=;
0451 
0452     /** Constructs a Mapped Quaternion object from the pointer \a coeffs
0453       *
0454       * The pointer \a coeffs must reference the four coefficients of Quaternion in the following order:
0455       * \code *coeffs == {x, y, z, w} \endcode
0456       *
0457       * If the template parameter _Options is set to #Aligned, then the pointer coeffs must be aligned. */
0458     EIGEN_DEVICE_FUNC explicit EIGEN_STRONG_INLINE Map(Scalar* coeffs) : m_coeffs(coeffs) {}
0459 
0460     EIGEN_DEVICE_FUNC inline Coefficients& coeffs() { return m_coeffs; }
0461     EIGEN_DEVICE_FUNC inline const Coefficients& coeffs() const { return m_coeffs; }
0462 
0463   protected:
0464     Coefficients m_coeffs;
0465 };
0466 
0467 /** \ingroup Geometry_Module
0468   * Map an unaligned array of single precision scalars as a quaternion */
0469 typedef Map<Quaternion<float>, 0>         QuaternionMapf;
0470 /** \ingroup Geometry_Module
0471   * Map an unaligned array of double precision scalars as a quaternion */
0472 typedef Map<Quaternion<double>, 0>        QuaternionMapd;
0473 /** \ingroup Geometry_Module
0474   * Map a 16-byte aligned array of single precision scalars as a quaternion */
0475 typedef Map<Quaternion<float>, Aligned>   QuaternionMapAlignedf;
0476 /** \ingroup Geometry_Module
0477   * Map a 16-byte aligned array of double precision scalars as a quaternion */
0478 typedef Map<Quaternion<double>, Aligned>  QuaternionMapAlignedd;
0479 
0480 /***************************************************************************
0481 * Implementation of QuaternionBase methods
0482 ***************************************************************************/
0483 
0484 // Generic Quaternion * Quaternion product
0485 // This product can be specialized for a given architecture via the Arch template argument.
0486 namespace internal {
0487 template<int Arch, class Derived1, class Derived2, typename Scalar> struct quat_product
0488 {
0489   EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Quaternion<Scalar> run(const QuaternionBase<Derived1>& a, const QuaternionBase<Derived2>& b){
0490     return Quaternion<Scalar>
0491     (
0492       a.w() * b.w() - a.x() * b.x() - a.y() * b.y() - a.z() * b.z(),
0493       a.w() * b.x() + a.x() * b.w() + a.y() * b.z() - a.z() * b.y(),
0494       a.w() * b.y() + a.y() * b.w() + a.z() * b.x() - a.x() * b.z(),
0495       a.w() * b.z() + a.z() * b.w() + a.x() * b.y() - a.y() * b.x()
0496     );
0497   }
0498 };
0499 }
0500 
0501 /** \returns the concatenation of two rotations as a quaternion-quaternion product */
0502 template <class Derived>
0503 template <class OtherDerived>
0504 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Quaternion<typename internal::traits<Derived>::Scalar>
0505 QuaternionBase<Derived>::operator* (const QuaternionBase<OtherDerived>& other) const
0506 {
0507   EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename OtherDerived::Scalar>::value),
0508    YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
0509   return internal::quat_product<Architecture::Target, Derived, OtherDerived,
0510                          typename internal::traits<Derived>::Scalar>::run(*this, other);
0511 }
0512 
0513 /** \sa operator*(Quaternion) */
0514 template <class Derived>
0515 template <class OtherDerived>
0516 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator*= (const QuaternionBase<OtherDerived>& other)
0517 {
0518   derived() = derived() * other.derived();
0519   return derived();
0520 }
0521 
0522 /** Rotation of a vector by a quaternion.
0523   * \remarks If the quaternion is used to rotate several points (>1)
0524   * then it is much more efficient to first convert it to a 3x3 Matrix.
0525   * Comparison of the operation cost for n transformations:
0526   *   - Quaternion2:    30n
0527   *   - Via a Matrix3: 24 + 15n
0528   */
0529 template <class Derived>
0530 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename QuaternionBase<Derived>::Vector3
0531 QuaternionBase<Derived>::_transformVector(const Vector3& v) const
0532 {
0533     // Note that this algorithm comes from the optimization by hand
0534     // of the conversion to a Matrix followed by a Matrix/Vector product.
0535     // It appears to be much faster than the common algorithm found
0536     // in the literature (30 versus 39 flops). It also requires two
0537     // Vector3 as temporaries.
0538     Vector3 uv = this->vec().cross(v);
0539     uv += uv;
0540     return v + this->w() * uv + this->vec().cross(uv);
0541 }
0542 
0543 template<class Derived>
0544 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE QuaternionBase<Derived>& QuaternionBase<Derived>::operator=(const QuaternionBase<Derived>& other)
0545 {
0546   coeffs() = other.coeffs();
0547   return derived();
0548 }
0549 
0550 template<class Derived>
0551 template<class OtherDerived>
0552 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(const QuaternionBase<OtherDerived>& other)
0553 {
0554   coeffs() = other.coeffs();
0555   return derived();
0556 }
0557 
0558 /** Set \c *this from an angle-axis \a aa and returns a reference to \c *this
0559   */
0560 template<class Derived>
0561 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& QuaternionBase<Derived>::operator=(const AngleAxisType& aa)
0562 {
0563   EIGEN_USING_STD(cos)
0564   EIGEN_USING_STD(sin)
0565   Scalar ha = Scalar(0.5)*aa.angle(); // Scalar(0.5) to suppress precision loss warnings
0566   this->w() = cos(ha);
0567   this->vec() = sin(ha) * aa.axis();
0568   return derived();
0569 }
0570 
0571 /** Set \c *this from the expression \a xpr:
0572   *   - if \a xpr is a 4x1 vector, then \a xpr is assumed to be a quaternion
0573   *   - if \a xpr is a 3x3 matrix, then \a xpr is assumed to be rotation matrix
0574   *     and \a xpr is converted to a quaternion
0575   */
0576 
0577 template<class Derived>
0578 template<class MatrixDerived>
0579 EIGEN_DEVICE_FUNC inline Derived& QuaternionBase<Derived>::operator=(const MatrixBase<MatrixDerived>& xpr)
0580 {
0581   EIGEN_STATIC_ASSERT((internal::is_same<typename Derived::Scalar, typename MatrixDerived::Scalar>::value),
0582    YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
0583   internal::quaternionbase_assign_impl<MatrixDerived>::run(*this, xpr.derived());
0584   return derived();
0585 }
0586 
0587 /** Convert the quaternion to a 3x3 rotation matrix. The quaternion is required to
0588   * be normalized, otherwise the result is undefined.
0589   */
0590 template<class Derived>
0591 EIGEN_DEVICE_FUNC inline typename QuaternionBase<Derived>::Matrix3
0592 QuaternionBase<Derived>::toRotationMatrix(void) const
0593 {
0594   // NOTE if inlined, then gcc 4.2 and 4.4 get rid of the temporary (not gcc 4.3 !!)
0595   // if not inlined then the cost of the return by value is huge ~ +35%,
0596   // however, not inlining this function is an order of magnitude slower, so
0597   // it has to be inlined, and so the return by value is not an issue
0598   Matrix3 res;
0599 
0600   const Scalar tx  = Scalar(2)*this->x();
0601   const Scalar ty  = Scalar(2)*this->y();
0602   const Scalar tz  = Scalar(2)*this->z();
0603   const Scalar twx = tx*this->w();
0604   const Scalar twy = ty*this->w();
0605   const Scalar twz = tz*this->w();
0606   const Scalar txx = tx*this->x();
0607   const Scalar txy = ty*this->x();
0608   const Scalar txz = tz*this->x();
0609   const Scalar tyy = ty*this->y();
0610   const Scalar tyz = tz*this->y();
0611   const Scalar tzz = tz*this->z();
0612 
0613   res.coeffRef(0,0) = Scalar(1)-(tyy+tzz);
0614   res.coeffRef(0,1) = txy-twz;
0615   res.coeffRef(0,2) = txz+twy;
0616   res.coeffRef(1,0) = txy+twz;
0617   res.coeffRef(1,1) = Scalar(1)-(txx+tzz);
0618   res.coeffRef(1,2) = tyz-twx;
0619   res.coeffRef(2,0) = txz-twy;
0620   res.coeffRef(2,1) = tyz+twx;
0621   res.coeffRef(2,2) = Scalar(1)-(txx+tyy);
0622 
0623   return res;
0624 }
0625 
0626 /** Sets \c *this to be a quaternion representing a rotation between
0627   * the two arbitrary vectors \a a and \a b. In other words, the built
0628   * rotation represent a rotation sending the line of direction \a a
0629   * to the line of direction \a b, both lines passing through the origin.
0630   *
0631   * \returns a reference to \c *this.
0632   *
0633   * Note that the two input vectors do \b not have to be normalized, and
0634   * do not need to have the same norm.
0635   */
0636 template<class Derived>
0637 template<typename Derived1, typename Derived2>
0638 EIGEN_DEVICE_FUNC inline Derived& QuaternionBase<Derived>::setFromTwoVectors(const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b)
0639 {
0640   EIGEN_USING_STD(sqrt)
0641   Vector3 v0 = a.normalized();
0642   Vector3 v1 = b.normalized();
0643   Scalar c = v1.dot(v0);
0644 
0645   // if dot == -1, vectors are nearly opposites
0646   // => accurately compute the rotation axis by computing the
0647   //    intersection of the two planes. This is done by solving:
0648   //       x^T v0 = 0
0649   //       x^T v1 = 0
0650   //    under the constraint:
0651   //       ||x|| = 1
0652   //    which yields a singular value problem
0653   if (c < Scalar(-1)+NumTraits<Scalar>::dummy_precision())
0654   {
0655     c = numext::maxi(c,Scalar(-1));
0656     Matrix<Scalar,2,3> m; m << v0.transpose(), v1.transpose();
0657     JacobiSVD<Matrix<Scalar,2,3> > svd(m, ComputeFullV);
0658     Vector3 axis = svd.matrixV().col(2);
0659 
0660     Scalar w2 = (Scalar(1)+c)*Scalar(0.5);
0661     this->w() = sqrt(w2);
0662     this->vec() = axis * sqrt(Scalar(1) - w2);
0663     return derived();
0664   }
0665   Vector3 axis = v0.cross(v1);
0666   Scalar s = sqrt((Scalar(1)+c)*Scalar(2));
0667   Scalar invs = Scalar(1)/s;
0668   this->vec() = axis * invs;
0669   this->w() = s * Scalar(0.5);
0670 
0671   return derived();
0672 }
0673 
0674 /** \returns a random unit quaternion following a uniform distribution law on SO(3)
0675   *
0676   * \note The implementation is based on http://planning.cs.uiuc.edu/node198.html
0677   */
0678 template<typename Scalar, int Options>
0679 EIGEN_DEVICE_FUNC Quaternion<Scalar,Options> Quaternion<Scalar,Options>::UnitRandom()
0680 {
0681   EIGEN_USING_STD(sqrt)
0682   EIGEN_USING_STD(sin)
0683   EIGEN_USING_STD(cos)
0684   const Scalar u1 = internal::random<Scalar>(0, 1),
0685                u2 = internal::random<Scalar>(0, 2*EIGEN_PI),
0686                u3 = internal::random<Scalar>(0, 2*EIGEN_PI);
0687   const Scalar a = sqrt(Scalar(1) - u1),
0688                b = sqrt(u1);
0689   return Quaternion (a * sin(u2), a * cos(u2), b * sin(u3), b * cos(u3));
0690 }
0691 
0692 
0693 /** Returns a quaternion representing a rotation between
0694   * the two arbitrary vectors \a a and \a b. In other words, the built
0695   * rotation represent a rotation sending the line of direction \a a
0696   * to the line of direction \a b, both lines passing through the origin.
0697   *
0698   * \returns resulting quaternion
0699   *
0700   * Note that the two input vectors do \b not have to be normalized, and
0701   * do not need to have the same norm.
0702   */
0703 template<typename Scalar, int Options>
0704 template<typename Derived1, typename Derived2>
0705 EIGEN_DEVICE_FUNC Quaternion<Scalar,Options> Quaternion<Scalar,Options>::FromTwoVectors(const MatrixBase<Derived1>& a, const MatrixBase<Derived2>& b)
0706 {
0707     Quaternion quat;
0708     quat.setFromTwoVectors(a, b);
0709     return quat;
0710 }
0711 
0712 
0713 /** \returns the multiplicative inverse of \c *this
0714   * Note that in most cases, i.e., if you simply want the opposite rotation,
0715   * and/or the quaternion is normalized, then it is enough to use the conjugate.
0716   *
0717   * \sa QuaternionBase::conjugate()
0718   */
0719 template <class Derived>
0720 EIGEN_DEVICE_FUNC inline Quaternion<typename internal::traits<Derived>::Scalar> QuaternionBase<Derived>::inverse() const
0721 {
0722   // FIXME should this function be called multiplicativeInverse and conjugate() be called inverse() or opposite()  ??
0723   Scalar n2 = this->squaredNorm();
0724   if (n2 > Scalar(0))
0725     return Quaternion<Scalar>(conjugate().coeffs() / n2);
0726   else
0727   {
0728     // return an invalid result to flag the error
0729     return Quaternion<Scalar>(Coefficients::Zero());
0730   }
0731 }
0732 
0733 // Generic conjugate of a Quaternion
0734 namespace internal {
0735 template<int Arch, class Derived, typename Scalar> struct quat_conj
0736 {
0737   EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Quaternion<Scalar> run(const QuaternionBase<Derived>& q){
0738     return Quaternion<Scalar>(q.w(),-q.x(),-q.y(),-q.z());
0739   }
0740 };
0741 }
0742                          
0743 /** \returns the conjugate of the \c *this which is equal to the multiplicative inverse
0744   * if the quaternion is normalized.
0745   * The conjugate of a quaternion represents the opposite rotation.
0746   *
0747   * \sa Quaternion2::inverse()
0748   */
0749 template <class Derived>
0750 EIGEN_DEVICE_FUNC inline Quaternion<typename internal::traits<Derived>::Scalar>
0751 QuaternionBase<Derived>::conjugate() const
0752 {
0753   return internal::quat_conj<Architecture::Target, Derived,
0754                          typename internal::traits<Derived>::Scalar>::run(*this);
0755                          
0756 }
0757 
0758 /** \returns the angle (in radian) between two rotations
0759   * \sa dot()
0760   */
0761 template <class Derived>
0762 template <class OtherDerived>
0763 EIGEN_DEVICE_FUNC inline typename internal::traits<Derived>::Scalar
0764 QuaternionBase<Derived>::angularDistance(const QuaternionBase<OtherDerived>& other) const
0765 {
0766   EIGEN_USING_STD(atan2)
0767   Quaternion<Scalar> d = (*this) * other.conjugate();
0768   return Scalar(2) * atan2( d.vec().norm(), numext::abs(d.w()) );
0769 }
0770 
0771  
0772     
0773 /** \returns the spherical linear interpolation between the two quaternions
0774   * \c *this and \a other at the parameter \a t in [0;1].
0775   * 
0776   * This represents an interpolation for a constant motion between \c *this and \a other,
0777   * see also http://en.wikipedia.org/wiki/Slerp.
0778   */
0779 template <class Derived>
0780 template <class OtherDerived>
0781 EIGEN_DEVICE_FUNC Quaternion<typename internal::traits<Derived>::Scalar>
0782 QuaternionBase<Derived>::slerp(const Scalar& t, const QuaternionBase<OtherDerived>& other) const
0783 {
0784   EIGEN_USING_STD(acos)
0785   EIGEN_USING_STD(sin)
0786   const Scalar one = Scalar(1) - NumTraits<Scalar>::epsilon();
0787   Scalar d = this->dot(other);
0788   Scalar absD = numext::abs(d);
0789 
0790   Scalar scale0;
0791   Scalar scale1;
0792 
0793   if(absD>=one)
0794   {
0795     scale0 = Scalar(1) - t;
0796     scale1 = t;
0797   }
0798   else
0799   {
0800     // theta is the angle between the 2 quaternions
0801     Scalar theta = acos(absD);
0802     Scalar sinTheta = sin(theta);
0803 
0804     scale0 = sin( ( Scalar(1) - t ) * theta) / sinTheta;
0805     scale1 = sin( ( t * theta) ) / sinTheta;
0806   }
0807   if(d<Scalar(0)) scale1 = -scale1;
0808 
0809   return Quaternion<Scalar>(scale0 * coeffs() + scale1 * other.coeffs());
0810 }
0811 
0812 namespace internal {
0813 
0814 // set from a rotation matrix
0815 template<typename Other>
0816 struct quaternionbase_assign_impl<Other,3,3>
0817 {
0818   typedef typename Other::Scalar Scalar;
0819   template<class Derived> EIGEN_DEVICE_FUNC static inline void run(QuaternionBase<Derived>& q, const Other& a_mat)
0820   {
0821     const typename internal::nested_eval<Other,2>::type mat(a_mat);
0822     EIGEN_USING_STD(sqrt)
0823     // This algorithm comes from  "Quaternion Calculus and Fast Animation",
0824     // Ken Shoemake, 1987 SIGGRAPH course notes
0825     Scalar t = mat.trace();
0826     if (t > Scalar(0))
0827     {
0828       t = sqrt(t + Scalar(1.0));
0829       q.w() = Scalar(0.5)*t;
0830       t = Scalar(0.5)/t;
0831       q.x() = (mat.coeff(2,1) - mat.coeff(1,2)) * t;
0832       q.y() = (mat.coeff(0,2) - mat.coeff(2,0)) * t;
0833       q.z() = (mat.coeff(1,0) - mat.coeff(0,1)) * t;
0834     }
0835     else
0836     {
0837       Index i = 0;
0838       if (mat.coeff(1,1) > mat.coeff(0,0))
0839         i = 1;
0840       if (mat.coeff(2,2) > mat.coeff(i,i))
0841         i = 2;
0842       Index j = (i+1)%3;
0843       Index k = (j+1)%3;
0844 
0845       t = sqrt(mat.coeff(i,i)-mat.coeff(j,j)-mat.coeff(k,k) + Scalar(1.0));
0846       q.coeffs().coeffRef(i) = Scalar(0.5) * t;
0847       t = Scalar(0.5)/t;
0848       q.w() = (mat.coeff(k,j)-mat.coeff(j,k))*t;
0849       q.coeffs().coeffRef(j) = (mat.coeff(j,i)+mat.coeff(i,j))*t;
0850       q.coeffs().coeffRef(k) = (mat.coeff(k,i)+mat.coeff(i,k))*t;
0851     }
0852   }
0853 };
0854 
0855 // set from a vector of coefficients assumed to be a quaternion
0856 template<typename Other>
0857 struct quaternionbase_assign_impl<Other,4,1>
0858 {
0859   typedef typename Other::Scalar Scalar;
0860   template<class Derived> EIGEN_DEVICE_FUNC static inline void run(QuaternionBase<Derived>& q, const Other& vec)
0861   {
0862     q.coeffs() = vec;
0863   }
0864 };
0865 
0866 } // end namespace internal
0867 
0868 } // end namespace Eigen
0869 
0870 #endif // EIGEN_QUATERNION_H