Eigne Matrix Class Library

Dependents:   MPC_current_control HydraulicControlBoard_SW AHRS Test_ekf ... more

Committer:
jsoh91
Date:
Tue Sep 24 00:18:23 2019 +0000
Revision:
1:3b8049da21b8
Parent:
0:13a5d365ba16
ignore and revise some of error parts

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ykuroda 0:13a5d365ba16 1 // This file is part of Eigen, a lightweight C++ template library
ykuroda 0:13a5d365ba16 2 // for linear algebra.
ykuroda 0:13a5d365ba16 3 //
ykuroda 0:13a5d365ba16 4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
ykuroda 0:13a5d365ba16 5 //
ykuroda 0:13a5d365ba16 6 // This Source Code Form is subject to the terms of the Mozilla
ykuroda 0:13a5d365ba16 7 // Public License v. 2.0. If a copy of the MPL was not distributed
ykuroda 0:13a5d365ba16 8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
ykuroda 0:13a5d365ba16 9
ykuroda 0:13a5d365ba16 10 #ifndef EIGEN_TRANSLATION_H
ykuroda 0:13a5d365ba16 11 #define EIGEN_TRANSLATION_H
ykuroda 0:13a5d365ba16 12
ykuroda 0:13a5d365ba16 13 namespace Eigen {
ykuroda 0:13a5d365ba16 14
ykuroda 0:13a5d365ba16 15 /** \geometry_module \ingroup Geometry_Module
ykuroda 0:13a5d365ba16 16 *
ykuroda 0:13a5d365ba16 17 * \class Translation
ykuroda 0:13a5d365ba16 18 *
ykuroda 0:13a5d365ba16 19 * \brief Represents a translation transformation
ykuroda 0:13a5d365ba16 20 *
ykuroda 0:13a5d365ba16 21 * \param _Scalar the scalar type, i.e., the type of the coefficients.
ykuroda 0:13a5d365ba16 22 * \param _Dim the dimension of the space, can be a compile time value or Dynamic
ykuroda 0:13a5d365ba16 23 *
ykuroda 0:13a5d365ba16 24 * \note This class is not aimed to be used to store a translation transformation,
ykuroda 0:13a5d365ba16 25 * but rather to make easier the constructions and updates of Transform objects.
ykuroda 0:13a5d365ba16 26 *
ykuroda 0:13a5d365ba16 27 * \sa class Scaling, class Transform
ykuroda 0:13a5d365ba16 28 */
ykuroda 0:13a5d365ba16 29 template<typename _Scalar, int _Dim>
ykuroda 0:13a5d365ba16 30 class Translation
ykuroda 0:13a5d365ba16 31 {
ykuroda 0:13a5d365ba16 32 public:
ykuroda 0:13a5d365ba16 33 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_Dim)
ykuroda 0:13a5d365ba16 34 /** dimension of the space */
ykuroda 0:13a5d365ba16 35 enum { Dim = _Dim };
ykuroda 0:13a5d365ba16 36 /** the scalar type of the coefficients */
ykuroda 0:13a5d365ba16 37 typedef _Scalar Scalar;
ykuroda 0:13a5d365ba16 38 /** corresponding vector type */
ykuroda 0:13a5d365ba16 39 typedef Matrix<Scalar,Dim,1> VectorType;
ykuroda 0:13a5d365ba16 40 /** corresponding linear transformation matrix type */
ykuroda 0:13a5d365ba16 41 typedef Matrix<Scalar,Dim,Dim> LinearMatrixType;
ykuroda 0:13a5d365ba16 42 /** corresponding affine transformation type */
ykuroda 0:13a5d365ba16 43 typedef Transform<Scalar,Dim,Affine> AffineTransformType;
ykuroda 0:13a5d365ba16 44 /** corresponding isometric transformation type */
ykuroda 0:13a5d365ba16 45 typedef Transform<Scalar,Dim,Isometry> IsometryTransformType;
ykuroda 0:13a5d365ba16 46
ykuroda 0:13a5d365ba16 47 protected:
ykuroda 0:13a5d365ba16 48
ykuroda 0:13a5d365ba16 49 VectorType m_coeffs;
ykuroda 0:13a5d365ba16 50
ykuroda 0:13a5d365ba16 51 public:
ykuroda 0:13a5d365ba16 52
ykuroda 0:13a5d365ba16 53 /** Default constructor without initialization. */
ykuroda 0:13a5d365ba16 54 Translation() {}
ykuroda 0:13a5d365ba16 55 /** */
ykuroda 0:13a5d365ba16 56 inline Translation(const Scalar& sx, const Scalar& sy)
ykuroda 0:13a5d365ba16 57 {
ykuroda 0:13a5d365ba16 58 eigen_assert(Dim==2);
ykuroda 0:13a5d365ba16 59 m_coeffs.x() = sx;
ykuroda 0:13a5d365ba16 60 m_coeffs.y() = sy;
ykuroda 0:13a5d365ba16 61 }
ykuroda 0:13a5d365ba16 62 /** */
ykuroda 0:13a5d365ba16 63 inline Translation(const Scalar& sx, const Scalar& sy, const Scalar& sz)
ykuroda 0:13a5d365ba16 64 {
ykuroda 0:13a5d365ba16 65 eigen_assert(Dim==3);
ykuroda 0:13a5d365ba16 66 m_coeffs.x() = sx;
ykuroda 0:13a5d365ba16 67 m_coeffs.y() = sy;
ykuroda 0:13a5d365ba16 68 m_coeffs.z() = sz;
ykuroda 0:13a5d365ba16 69 }
ykuroda 0:13a5d365ba16 70 /** Constructs and initialize the translation transformation from a vector of translation coefficients */
ykuroda 0:13a5d365ba16 71 explicit inline Translation(const VectorType& vector) : m_coeffs(vector) {}
ykuroda 0:13a5d365ba16 72
ykuroda 0:13a5d365ba16 73 /** \brief Retruns the x-translation by value. **/
ykuroda 0:13a5d365ba16 74 inline Scalar x() const { return m_coeffs.x(); }
ykuroda 0:13a5d365ba16 75 /** \brief Retruns the y-translation by value. **/
ykuroda 0:13a5d365ba16 76 inline Scalar y() const { return m_coeffs.y(); }
ykuroda 0:13a5d365ba16 77 /** \brief Retruns the z-translation by value. **/
ykuroda 0:13a5d365ba16 78 inline Scalar z() const { return m_coeffs.z(); }
ykuroda 0:13a5d365ba16 79
ykuroda 0:13a5d365ba16 80 /** \brief Retruns the x-translation as a reference. **/
ykuroda 0:13a5d365ba16 81 inline Scalar& x() { return m_coeffs.x(); }
ykuroda 0:13a5d365ba16 82 /** \brief Retruns the y-translation as a reference. **/
ykuroda 0:13a5d365ba16 83 inline Scalar& y() { return m_coeffs.y(); }
ykuroda 0:13a5d365ba16 84 /** \brief Retruns the z-translation as a reference. **/
ykuroda 0:13a5d365ba16 85 inline Scalar& z() { return m_coeffs.z(); }
ykuroda 0:13a5d365ba16 86
ykuroda 0:13a5d365ba16 87 const VectorType& vector() const { return m_coeffs; }
ykuroda 0:13a5d365ba16 88 VectorType& vector() { return m_coeffs; }
ykuroda 0:13a5d365ba16 89
ykuroda 0:13a5d365ba16 90 const VectorType& translation() const { return m_coeffs; }
ykuroda 0:13a5d365ba16 91 VectorType& translation() { return m_coeffs; }
ykuroda 0:13a5d365ba16 92
ykuroda 0:13a5d365ba16 93 /** Concatenates two translation */
ykuroda 0:13a5d365ba16 94 inline Translation operator* (const Translation& other) const
ykuroda 0:13a5d365ba16 95 { return Translation(m_coeffs + other.m_coeffs); }
ykuroda 0:13a5d365ba16 96
ykuroda 0:13a5d365ba16 97 /** Concatenates a translation and a uniform scaling */
ykuroda 0:13a5d365ba16 98 inline AffineTransformType operator* (const UniformScaling<Scalar>& other) const;
ykuroda 0:13a5d365ba16 99
ykuroda 0:13a5d365ba16 100 /** Concatenates a translation and a linear transformation */
ykuroda 0:13a5d365ba16 101 template<typename OtherDerived>
ykuroda 0:13a5d365ba16 102 inline AffineTransformType operator* (const EigenBase<OtherDerived>& linear) const;
ykuroda 0:13a5d365ba16 103
ykuroda 0:13a5d365ba16 104 /** Concatenates a translation and a rotation */
ykuroda 0:13a5d365ba16 105 template<typename Derived>
ykuroda 0:13a5d365ba16 106 inline IsometryTransformType operator*(const RotationBase<Derived,Dim>& r) const
ykuroda 0:13a5d365ba16 107 { return *this * IsometryTransformType(r); }
ykuroda 0:13a5d365ba16 108
ykuroda 0:13a5d365ba16 109 /** \returns the concatenation of a linear transformation \a l with the translation \a t */
ykuroda 0:13a5d365ba16 110 // its a nightmare to define a templated friend function outside its declaration
ykuroda 0:13a5d365ba16 111 template<typename OtherDerived> friend
ykuroda 0:13a5d365ba16 112 inline AffineTransformType operator*(const EigenBase<OtherDerived>& linear, const Translation& t)
ykuroda 0:13a5d365ba16 113 {
ykuroda 0:13a5d365ba16 114 AffineTransformType res;
ykuroda 0:13a5d365ba16 115 res.matrix().setZero();
ykuroda 0:13a5d365ba16 116 res.linear() = linear.derived();
ykuroda 0:13a5d365ba16 117 res.translation() = linear.derived() * t.m_coeffs;
ykuroda 0:13a5d365ba16 118 res.matrix().row(Dim).setZero();
ykuroda 0:13a5d365ba16 119 res(Dim,Dim) = Scalar(1);
ykuroda 0:13a5d365ba16 120 return res;
ykuroda 0:13a5d365ba16 121 }
ykuroda 0:13a5d365ba16 122
ykuroda 0:13a5d365ba16 123 /** Concatenates a translation and a transformation */
ykuroda 0:13a5d365ba16 124 template<int Mode, int Options>
ykuroda 0:13a5d365ba16 125 inline Transform<Scalar,Dim,Mode> operator* (const Transform<Scalar,Dim,Mode,Options>& t) const
ykuroda 0:13a5d365ba16 126 {
ykuroda 0:13a5d365ba16 127 Transform<Scalar,Dim,Mode> res = t;
ykuroda 0:13a5d365ba16 128 res.pretranslate(m_coeffs);
ykuroda 0:13a5d365ba16 129 return res;
ykuroda 0:13a5d365ba16 130 }
ykuroda 0:13a5d365ba16 131
ykuroda 0:13a5d365ba16 132 /** Applies translation to vector */
ykuroda 0:13a5d365ba16 133 inline VectorType operator* (const VectorType& other) const
ykuroda 0:13a5d365ba16 134 { return m_coeffs + other; }
ykuroda 0:13a5d365ba16 135
ykuroda 0:13a5d365ba16 136 /** \returns the inverse translation (opposite) */
ykuroda 0:13a5d365ba16 137 Translation inverse() const { return Translation(-m_coeffs); }
ykuroda 0:13a5d365ba16 138
ykuroda 0:13a5d365ba16 139 Translation& operator=(const Translation& other)
ykuroda 0:13a5d365ba16 140 {
ykuroda 0:13a5d365ba16 141 m_coeffs = other.m_coeffs;
ykuroda 0:13a5d365ba16 142 return *this;
ykuroda 0:13a5d365ba16 143 }
ykuroda 0:13a5d365ba16 144
ykuroda 0:13a5d365ba16 145 static const Translation Identity() { return Translation(VectorType::Zero()); }
ykuroda 0:13a5d365ba16 146
ykuroda 0:13a5d365ba16 147 /** \returns \c *this with scalar type casted to \a NewScalarType
ykuroda 0:13a5d365ba16 148 *
ykuroda 0:13a5d365ba16 149 * Note that if \a NewScalarType is equal to the current scalar type of \c *this
ykuroda 0:13a5d365ba16 150 * then this function smartly returns a const reference to \c *this.
ykuroda 0:13a5d365ba16 151 */
ykuroda 0:13a5d365ba16 152 template<typename NewScalarType>
ykuroda 0:13a5d365ba16 153 inline typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type cast() const
ykuroda 0:13a5d365ba16 154 { return typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type(*this); }
ykuroda 0:13a5d365ba16 155
ykuroda 0:13a5d365ba16 156 /** Copy constructor with scalar type conversion */
ykuroda 0:13a5d365ba16 157 template<typename OtherScalarType>
ykuroda 0:13a5d365ba16 158 inline explicit Translation(const Translation<OtherScalarType,Dim>& other)
ykuroda 0:13a5d365ba16 159 { m_coeffs = other.vector().template cast<Scalar>(); }
ykuroda 0:13a5d365ba16 160
ykuroda 0:13a5d365ba16 161 /** \returns \c true if \c *this is approximately equal to \a other, within the precision
ykuroda 0:13a5d365ba16 162 * determined by \a prec.
ykuroda 0:13a5d365ba16 163 *
ykuroda 0:13a5d365ba16 164 * \sa MatrixBase::isApprox() */
ykuroda 0:13a5d365ba16 165 bool isApprox(const Translation& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const
ykuroda 0:13a5d365ba16 166 { return m_coeffs.isApprox(other.m_coeffs, prec); }
ykuroda 0:13a5d365ba16 167
ykuroda 0:13a5d365ba16 168 };
ykuroda 0:13a5d365ba16 169
ykuroda 0:13a5d365ba16 170 /** \addtogroup Geometry_Module */
ykuroda 0:13a5d365ba16 171 //@{
ykuroda 0:13a5d365ba16 172 typedef Translation<float, 2> Translation2f;
ykuroda 0:13a5d365ba16 173 typedef Translation<double,2> Translation2d;
ykuroda 0:13a5d365ba16 174 typedef Translation<float, 3> Translation3f;
ykuroda 0:13a5d365ba16 175 typedef Translation<double,3> Translation3d;
ykuroda 0:13a5d365ba16 176 //@}
ykuroda 0:13a5d365ba16 177
ykuroda 0:13a5d365ba16 178 template<typename Scalar, int Dim>
ykuroda 0:13a5d365ba16 179 inline typename Translation<Scalar,Dim>::AffineTransformType
ykuroda 0:13a5d365ba16 180 Translation<Scalar,Dim>::operator* (const UniformScaling<Scalar>& other) const
ykuroda 0:13a5d365ba16 181 {
ykuroda 0:13a5d365ba16 182 AffineTransformType res;
ykuroda 0:13a5d365ba16 183 res.matrix().setZero();
ykuroda 0:13a5d365ba16 184 res.linear().diagonal().fill(other.factor());
ykuroda 0:13a5d365ba16 185 res.translation() = m_coeffs;
ykuroda 0:13a5d365ba16 186 res(Dim,Dim) = Scalar(1);
ykuroda 0:13a5d365ba16 187 return res;
ykuroda 0:13a5d365ba16 188 }
ykuroda 0:13a5d365ba16 189
ykuroda 0:13a5d365ba16 190 template<typename Scalar, int Dim>
ykuroda 0:13a5d365ba16 191 template<typename OtherDerived>
ykuroda 0:13a5d365ba16 192 inline typename Translation<Scalar,Dim>::AffineTransformType
ykuroda 0:13a5d365ba16 193 Translation<Scalar,Dim>::operator* (const EigenBase<OtherDerived>& linear) const
ykuroda 0:13a5d365ba16 194 {
ykuroda 0:13a5d365ba16 195 AffineTransformType res;
ykuroda 0:13a5d365ba16 196 res.matrix().setZero();
ykuroda 0:13a5d365ba16 197 res.linear() = linear.derived();
ykuroda 0:13a5d365ba16 198 res.translation() = m_coeffs;
ykuroda 0:13a5d365ba16 199 res.matrix().row(Dim).setZero();
ykuroda 0:13a5d365ba16 200 res(Dim,Dim) = Scalar(1);
ykuroda 0:13a5d365ba16 201 return res;
ykuroda 0:13a5d365ba16 202 }
ykuroda 0:13a5d365ba16 203
ykuroda 0:13a5d365ba16 204 } // end namespace Eigen
ykuroda 0:13a5d365ba16 205
ykuroda 0:13a5d365ba16 206 #endif // EIGEN_TRANSLATION_H