From 6680fa42ee830d315db8879d7b0746c68bdcba4e Mon Sep 17 00:00:00 2001 From: Mathieu Gautier Date: Fri, 13 Nov 2009 16:41:51 +0100 Subject: * add Map test based on Map from test/map.cpp * replace implicit constructor AngleAxis(QuaternionBase&) by an explicit one, it seems ambiguous for the compiler * remove explicit constructor with conversion type quaternion(Quaternion&): conflict between constructor. * modify EIGEN_INHERIT_ASSIGNEMENT_OPERATORS to suit Quaternion class --- Eigen/src/Core/util/Macros.h | 28 ++++++------ Eigen/src/Geometry/AngleAxis.h | 8 ++-- Eigen/src/Geometry/Quaternion.h | 95 +++++++++++++++++++++++++-------------- Eigen/src/Geometry/RotationBase.h | 4 +- test/geo_quaternion.cpp | 28 +++++++++++- test/geo_transformations.cpp | 2 +- 6 files changed, 111 insertions(+), 54 deletions(-) diff --git a/Eigen/src/Core/util/Macros.h b/Eigen/src/Core/util/Macros.h index e9539e3cf..a94300de3 100644 --- a/Eigen/src/Core/util/Macros.h +++ b/Eigen/src/Core/util/Macros.h @@ -168,7 +168,7 @@ using Eigen::ei_cos; #endif // EIGEN_FORCE_INLINE means "inline as much as possible" -#if (defined _MSC_VER) +#if (defined _MSC_VER) || (defined __intel_compiler) #define EIGEN_STRONG_INLINE __forceinline #else #define EIGEN_STRONG_INLINE inline @@ -261,26 +261,26 @@ using Eigen::ei_cos; #define EIGEN_REF_TO_TEMPORARY const & #endif -#ifdef _MSC_VER -#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived) \ -using Base::operator =; \ -using Base::operator +=; \ -using Base::operator -=; \ -using Base::operator *=; \ -using Base::operator /=; +#if defined(_MSC_VER) && (!defined(__INTEL_COMPILER)) +#define EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Derived) \ +using Base::operator =; #else -#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived) \ +#define EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Derived) \ using Base::operator =; \ -using Base::operator +=; \ -using Base::operator -=; \ -using Base::operator *=; \ -using Base::operator /=; \ EIGEN_STRONG_INLINE Derived& operator=(const Derived& other) \ { \ - return Base::operator=(other); \ + Base::operator=(other); \ + return *this; \ } #endif +#define EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Derived) \ +using Base::operator +=; \ +using Base::operator -=; \ +using Base::operator *=; \ +using Base::operator /=; \ +EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Derived) + #define _EIGEN_GENERIC_PUBLIC_INTERFACE(Derived, BaseClass) \ typedef BaseClass Base; \ typedef typename Eigen::ei_traits::Scalar Scalar; \ diff --git a/Eigen/src/Geometry/AngleAxis.h b/Eigen/src/Geometry/AngleAxis.h index b9dfa6972..ef86eb20d 100644 --- a/Eigen/src/Geometry/AngleAxis.h +++ b/Eigen/src/Geometry/AngleAxis.h @@ -89,7 +89,7 @@ public: template inline AngleAxis(Scalar angle, const MatrixBase& axis) : m_axis(axis), m_angle(angle) {} /** Constructs and initialize the angle-axis rotation from a quaternion \a q. */ - inline AngleAxis(const QuaternionType& q) { *this = q; } + template inline explicit AngleAxis(const QuaternionBase& q) { *this = q; } /** Constructs and initialize the angle-axis rotation from a 3x3 rotation matrix. */ template inline explicit AngleAxis(const MatrixBase& m) { *this = m; } @@ -116,7 +116,8 @@ public: AngleAxis inverse() const { return AngleAxis(-m_angle, m_axis); } - AngleAxis& operator=(const QuaternionType& q); + template + AngleAxis& operator=(const QuaternionBase& q); template AngleAxis& operator=(const MatrixBase& m); @@ -160,7 +161,8 @@ typedef AngleAxis AngleAxisd; * The axis is normalized. */ template -AngleAxis& AngleAxis::operator=(const QuaternionType& q) +template +AngleAxis& AngleAxis::operator=(const QuaternionBase& q) { Scalar n2 = q.vec().squaredNorm(); if (n2 < precision()*precision()) diff --git a/Eigen/src/Geometry/Quaternion.h b/Eigen/src/Geometry/Quaternion.h index b08a027c9..660e10d1e 100644 --- a/Eigen/src/Geometry/Quaternion.h +++ b/Eigen/src/Geometry/Quaternion.h @@ -88,7 +88,8 @@ public: /** \returns a vector expression of the coefficients (x,y,z,w) */ inline typename ei_traits::Coefficients& coeffs() { return derived().coeffs(); } - template Derived& operator=(const QuaternionBase& other); + EIGEN_STRONG_INLINE QuaternionBase& operator=(const QuaternionBase& other); + template EIGEN_STRONG_INLINE Derived& operator=(const QuaternionBase& other); // disabled this copy operator as it is giving very strange compilation errors when compiling // test_stdvector with GCC 4.4.2. This looks like a GCC bug though, so feel free to re-enable it if it's @@ -133,19 +134,28 @@ public: */ template inline Scalar dot(const QuaternionBase& other) const { return coeffs().dot(other.coeffs()); } - template inline Scalar angularDistance(const QuaternionBase& other) const; + template Scalar angularDistance(const QuaternionBase& other) const; + /** \returns an equivalent 3x3 rotation matrix */ Matrix3 toRotationMatrix() const; + /** \returns the quaternion which transform \a a into \a b through a rotation */ template Derived& setFromTwoVectors(const MatrixBase& a, const MatrixBase& b); - template inline Quaternion operator* (const QuaternionBase& q) const; - template inline Derived& operator*= (const QuaternionBase& q); + template EIGEN_STRONG_INLINE Quaternion operator* (const QuaternionBase& q) const; + template EIGEN_STRONG_INLINE Derived& operator*= (const QuaternionBase& q); + /** \returns the quaternion describing the inverse rotation */ Quaternion inverse() const; + + /** \returns the conjugated quaternion */ Quaternion conjugate() const; + /** \returns an interpolation for a constant motion between \a other and \c *this + * \a t in [0;1] + * see http://en.wikipedia.org/wiki/Slerp + */ template Quaternion slerp(Scalar t, const QuaternionBase& other) const; /** \returns \c true if \c *this is approximately equal to \a other, within the precision @@ -156,7 +166,8 @@ public: bool isApprox(const QuaternionBase& other, RealScalar prec = precision()) const { return coeffs().isApprox(other.coeffs(), prec); } - Vector3 _transformVector(Vector3 v) const; + /** return the result vector of \a v through the rotation*/ + EIGEN_STRONG_INLINE Vector3 _transformVector(Vector3 v) const; /** \returns \c *this with scalar type casted to \a NewScalarType * @@ -210,11 +221,12 @@ struct ei_traits > template class Quaternion : public QuaternionBase >{ typedef QuaternionBase > Base; -public: - using Base::operator=; - +public: typedef _Scalar Scalar; + EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(Quaternion) + using Base::operator*=; + typedef typename ei_traits >::Coefficients Coefficients; typedef typename Base::AngleAxisType AngleAxisType; @@ -228,15 +240,13 @@ public: * while internally the coefficients are stored in the following order: * [\c x, \c y, \c z, \c w] */ - inline Quaternion(Scalar w, Scalar x, Scalar y, Scalar z) - { coeffs() << x, y, z, w; } + inline Quaternion(Scalar w, Scalar x, Scalar y, Scalar z) : m_coeffs(x, y, z, w){} - /** Constructs and initialize a quaternion from the array data - * This constructor is also used to map an array */ + /** Constructs and initialize a quaternion from the array data */ inline Quaternion(const Scalar* data) : m_coeffs(data) {} /** Copy constructor */ -// template inline Quaternion(const QuaternionBase& other) { m_coeffs = other.coeffs(); } [XXX] redundant with 703 + template EIGEN_STRONG_INLINE Quaternion(const QuaternionBase& other) { this->Base::operator=(other); } /** Constructs and initializes a quaternion from the angle-axis \a aa */ explicit inline Quaternion(const AngleAxisType& aa) { *this = aa; } @@ -248,11 +258,6 @@ public: template explicit inline Quaternion(const MatrixBase& other) { *this = other; } - /** Copy constructor with scalar type conversion */ - template - inline explicit Quaternion(const QuaternionBase& other) - { m_coeffs = other.coeffs().template cast(); } - inline Coefficients& coeffs() { return m_coeffs;} inline const Coefficients& coeffs() const { return m_coeffs;} @@ -289,7 +294,7 @@ struct ei_traits, _PacketAccess> >: ei_traits > { typedef _Scalar Scalar; - typedef Map > Coefficients; + typedef Map, _PacketAccess> Coefficients; enum { PacketAccess = _PacketAccess }; @@ -297,13 +302,22 @@ ei_traits > template class Map, PacketAccess > - : public QuaternionBase, PacketAccess> >, - ei_no_assignment_operator + : public QuaternionBase, PacketAccess> > { + public: + typedef _Scalar Scalar; + typedef Map, PacketAccess > MapQuat; + + private: + Map, PacketAccess >(); + Map, PacketAccess >(const Map, PacketAccess>&); + + typedef QuaternionBase, PacketAccess> > Base; public: + EIGEN_INHERIT_ASSIGNMENT_EQUAL_OPERATOR(MapQuat) + using Base::operator*=; - typedef _Scalar Scalar; - typedef typename ei_traits::Coefficients Coefficients; + typedef typename ei_traits, PacketAccess> >::Coefficients Coefficients; /** Constructs a Mapped Quaternion object from the pointer \a coeffs * @@ -311,7 +325,7 @@ class Map, PacketAccess > * \code *coeffs == {x, y, z, w} \endcode * * If the template paramter PacketAccess is set to Aligned, then the pointer coeffs must be aligned. */ - inline Map(const Scalar* coeffs) : m_coeffs(coeffs) {} + EIGEN_STRONG_INLINE Map(const Scalar* coeffs) : m_coeffs(coeffs) {} inline Coefficients& coeffs() { return m_coeffs;} inline const Coefficients& coeffs() const { return m_coeffs;} @@ -320,10 +334,18 @@ class Map, PacketAccess > Coefficients m_coeffs; }; -typedef Map > QuaternionMapd; -typedef Map > QuaternionMapf; -typedef Map, Aligned> QuaternionMapAlignedd; +/** \ingroup Geometry_Module + * Map an unaligned array of single precision scalar as a quaternion */ +typedef Map, 0> QuaternionMapf; +/** \ingroup Geometry_Module + * Map an unaligned array of double precision scalar as a quaternion */ +typedef Map, 0> QuaternionMapd; +/** \ingroup Geometry_Module + * Map a 16-bits aligned array of double precision scalars as a quaternion */ typedef Map, Aligned> QuaternionMapAlignedf; +/** \ingroup Geometry_Module + * Map a 16-bits aligned array of double precision scalars as a quaternion */ +typedef Map, Aligned> QuaternionMapAlignedd; /*************************************************************************** * Implementation of QuaternionBase methods @@ -333,7 +355,7 @@ typedef Map, Aligned> QuaternionMapAlignedf; // This product can be specialized for a given architecture via the Arch template argument. template struct ei_quat_product { - inline static Quaternion run(const QuaternionBase& a, const QuaternionBase& b){ + EIGEN_STRONG_INLINE static Quaternion run(const QuaternionBase& a, const QuaternionBase& b){ return Quaternion ( a.w() * b.w() - a.x() * b.x() - a.y() * b.y() - a.z() * b.z(), @@ -347,7 +369,7 @@ template template -inline Quaternion::Scalar> +EIGEN_STRONG_INLINE Quaternion::Scalar> QuaternionBase::operator* (const QuaternionBase& other) const { EIGEN_STATIC_ASSERT((ei_is_same_type::ret), @@ -360,7 +382,7 @@ QuaternionBase::operator* (const QuaternionBase& other) c /** \sa operator*(Quaternion) */ template template -inline Derived& QuaternionBase::operator*= (const QuaternionBase& other) +EIGEN_STRONG_INLINE Derived& QuaternionBase::operator*= (const QuaternionBase& other) { return (derived() = derived() * other.derived()); } @@ -373,7 +395,7 @@ inline Derived& QuaternionBase::operator*= (const QuaternionBase -inline typename QuaternionBase::Vector3 +EIGEN_STRONG_INLINE typename QuaternionBase::Vector3 QuaternionBase::_transformVector(Vector3 v) const { // Note that this algorithm comes from the optimization by hand @@ -385,9 +407,16 @@ QuaternionBase::_transformVector(Vector3 v) const return v + this->w() * uv + this->vec().cross(uv); } +template +EIGEN_STRONG_INLINE QuaternionBase& QuaternionBase::operator=(const QuaternionBase& other) +{ + coeffs() = other.coeffs(); + return derived(); +} + template template -inline Derived& QuaternionBase::operator=(const QuaternionBase& other) +EIGEN_STRONG_INLINE Derived& QuaternionBase::operator=(const QuaternionBase& other) { coeffs() = other.coeffs(); return derived(); @@ -396,7 +425,7 @@ inline Derived& QuaternionBase::operator=(const QuaternionBase -inline Derived& QuaternionBase::operator=(const AngleAxisType& aa) +EIGEN_STRONG_INLINE Derived& QuaternionBase::operator=(const AngleAxisType& aa) { Scalar ha = Scalar(0.5)*aa.angle(); // Scalar(0.5) to suppress precision loss warnings this->w() = ei_cos(ha); diff --git a/Eigen/src/Geometry/RotationBase.h b/Eigen/src/Geometry/RotationBase.h index baffd8e24..50dc17311 100644 --- a/Eigen/src/Geometry/RotationBase.h +++ b/Eigen/src/Geometry/RotationBase.h @@ -73,7 +73,7 @@ class RotationBase * - a vector of size Dim */ template - inline typename ei_rotation_base_generic_product_selector::ReturnType + EIGEN_STRONG_INLINE typename ei_rotation_base_generic_product_selector::ReturnType operator*(const AnyMatrixBase& e) const { return ei_rotation_base_generic_product_selector::run(derived(), e.derived()); } @@ -107,7 +107,7 @@ struct ei_rotation_base_generic_product_selector ReturnType; - inline static ReturnType run(const RotationDerived& r, const OtherVectorType& v) + EIGEN_STRONG_INLINE static ReturnType run(const RotationDerived& r, const OtherVectorType& v) { return r._transformVector(v); } diff --git a/test/geo_quaternion.cpp b/test/geo_quaternion.cpp index 7dbf890f4..2e97fe295 100644 --- a/test/geo_quaternion.cpp +++ b/test/geo_quaternion.cpp @@ -2,6 +2,7 @@ // for linear algebra. // // Copyright (C) 2008-2009 Gael Guennebaud +// Copyright (C) 2009 Mathieu Gautier // // Eigen is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -84,7 +85,7 @@ template void quaternion(void) // angle-axis conversion - AngleAxisx aa = q1; + AngleAxisx aa = AngleAxisx(q1); VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1); VERIFY_IS_NOT_APPROX(q1 * v1, Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1); @@ -110,10 +111,35 @@ template void quaternion(void) VERIFY_IS_APPROX(q1d.template cast(),q1); } +template void mapQuaternion(void){ + typedef Map, Aligned> MQuaternionA; + typedef Map > MQuaternionUA; + typedef Quaternion Quaternionx; + + EIGEN_ALIGN16 Scalar array1[4]; + EIGEN_ALIGN16 Scalar array2[4]; + EIGEN_ALIGN16 Scalar array3[4+1]; + Scalar* array3unaligned = array3+1; + + MQuaternionA(array1).coeffs().setRandom(); + (MQuaternionA(array2)) = MQuaternionA(array1); + (MQuaternionUA(array3unaligned)) = MQuaternionA(array1); + + Quaternionx q1 = MQuaternionA(array1); + Quaternionx q2 = MQuaternionA(array2); + Quaternionx q3 = MQuaternionUA(array3unaligned); + + VERIFY_IS_APPROX(q1.coeffs(), q2.coeffs()); + VERIFY_IS_APPROX(q1.coeffs(), q3.coeffs()); + VERIFY_RAISES_ASSERT((MQuaternionA(array3unaligned))); +} + void test_geo_quaternion() { for(int i = 0; i < g_repeat; i++) { CALL_SUBTEST_1( quaternion() ); CALL_SUBTEST_2( quaternion() ); + CALL_SUBTEST( mapQuaternion() ); + CALL_SUBTEST( mapQuaternion() ); } } diff --git a/test/geo_transformations.cpp b/test/geo_transformations.cpp index f1d068b83..84b3c5355 100644 --- a/test/geo_transformations.cpp +++ b/test/geo_transformations.cpp @@ -81,7 +81,7 @@ template void transformations(void) * (AngleAxisx(Scalar(0.3), Vector3(0,0,1)).toRotationMatrix() * v1))); // angle-axis conversion - AngleAxisx aa = q1; + AngleAxisx aa = AngleAxisx(q1); VERIFY_IS_APPROX(q1 * v1, Quaternionx(aa) * v1); VERIFY_IS_NOT_APPROX(q1 * v1, Quaternionx(AngleAxisx(aa.angle()*2,aa.axis())) * v1); -- cgit v1.2.3