![]() |
Eigen
3.3.3
|
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> 00005 // 00006 // This Source Code Form is subject to the terms of the Mozilla 00007 // Public License v. 2.0. If a copy of the MPL was not distributed 00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00009 00010 #ifndef EIGEN_TRANSLATION_H 00011 #define EIGEN_TRANSLATION_H 00012 00013 namespace Eigen { 00014 00029 template<typename _Scalar, int _Dim> 00030 class Translation 00031 { 00032 public: 00033 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_Dim) 00035 enum { Dim = _Dim }; 00037 typedef _Scalar Scalar; 00039 typedef Matrix<Scalar,Dim,1> VectorType; 00041 typedef Matrix<Scalar,Dim,Dim> LinearMatrixType; 00043 typedef Transform<Scalar,Dim,Affine> AffineTransformType; 00045 typedef Transform<Scalar,Dim,Isometry> IsometryTransformType; 00046 00047 protected: 00048 00049 VectorType m_coeffs; 00050 00051 public: 00052 00054 EIGEN_DEVICE_FUNC Translation() {} 00056 EIGEN_DEVICE_FUNC inline Translation(const Scalar& sx, const Scalar& sy) 00057 { 00058 eigen_assert(Dim==2); 00059 m_coeffs.x() = sx; 00060 m_coeffs.y() = sy; 00061 } 00063 EIGEN_DEVICE_FUNC inline Translation(const Scalar& sx, const Scalar& sy, const Scalar& sz) 00064 { 00065 eigen_assert(Dim==3); 00066 m_coeffs.x() = sx; 00067 m_coeffs.y() = sy; 00068 m_coeffs.z() = sz; 00069 } 00071 EIGEN_DEVICE_FUNC explicit inline Translation(const VectorType& vector) : m_coeffs(vector) {} 00072 00074 EIGEN_DEVICE_FUNC inline Scalar x() const { return m_coeffs.x(); } 00076 EIGEN_DEVICE_FUNC inline Scalar y() const { return m_coeffs.y(); } 00078 EIGEN_DEVICE_FUNC inline Scalar z() const { return m_coeffs.z(); } 00079 00081 EIGEN_DEVICE_FUNC inline Scalar& x() { return m_coeffs.x(); } 00083 EIGEN_DEVICE_FUNC inline Scalar& y() { return m_coeffs.y(); } 00085 EIGEN_DEVICE_FUNC inline Scalar& z() { return m_coeffs.z(); } 00086 00087 EIGEN_DEVICE_FUNC const VectorType& vector() const { return m_coeffs; } 00088 EIGEN_DEVICE_FUNC VectorType& vector() { return m_coeffs; } 00089 00090 EIGEN_DEVICE_FUNC const VectorType& translation() const { return m_coeffs; } 00091 EIGEN_DEVICE_FUNC VectorType& translation() { return m_coeffs; } 00092 00094 EIGEN_DEVICE_FUNC inline Translation operator* (const Translation& other) const 00095 { return Translation(m_coeffs + other.m_coeffs); } 00096 00098 EIGEN_DEVICE_FUNC inline AffineTransformType operator* (const UniformScaling<Scalar>& other) const; 00099 00101 template<typename OtherDerived> 00102 EIGEN_DEVICE_FUNC inline AffineTransformType operator* (const EigenBase<OtherDerived>& linear) const; 00103 00105 template<typename Derived> 00106 EIGEN_DEVICE_FUNC inline IsometryTransformType operator*(const RotationBase<Derived,Dim>& r) const 00107 { return *this * IsometryTransformType(r); } 00108 00110 // its a nightmare to define a templated friend function outside its declaration 00111 template<typename OtherDerived> friend 00112 EIGEN_DEVICE_FUNC inline AffineTransformType operator*(const EigenBase<OtherDerived>& linear, const Translation& t) 00113 { 00114 AffineTransformType res; 00115 res.matrix().setZero(); 00116 res.linear() = linear.derived(); 00117 res.translation() = linear.derived() * t.m_coeffs; 00118 res.matrix().row(Dim).setZero(); 00119 res(Dim,Dim) = Scalar(1); 00120 return res; 00121 } 00122 00124 template<int Mode, int Options> 00125 EIGEN_DEVICE_FUNC inline Transform<Scalar,Dim,Mode> operator* (const Transform<Scalar,Dim,Mode,Options>& t) const 00126 { 00127 Transform<Scalar,Dim,Mode> res = t; 00128 res.pretranslate(m_coeffs); 00129 return res; 00130 } 00131 00133 template<typename Derived> 00134 inline typename internal::enable_if<Derived::IsVectorAtCompileTime,VectorType>::type 00135 operator* (const MatrixBase<Derived>& vec) const 00136 { return m_coeffs + vec.derived(); } 00137 00139 Translation inverse() const { return Translation(-m_coeffs); } 00140 00141 Translation& operator=(const Translation& other) 00142 { 00143 m_coeffs = other.m_coeffs; 00144 return *this; 00145 } 00146 00147 static const Translation Identity() { return Translation(VectorType::Zero()); } 00148 00154 template<typename NewScalarType> 00155 EIGEN_DEVICE_FUNC inline typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type cast() const 00156 { return typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type(*this); } 00157 00159 template<typename OtherScalarType> 00160 EIGEN_DEVICE_FUNC inline explicit Translation(const Translation<OtherScalarType,Dim>& other) 00161 { m_coeffs = other.vector().template cast<Scalar>(); } 00162 00167 EIGEN_DEVICE_FUNC bool isApprox(const Translation& other, const typename NumTraits<Scalar>::Real& prec = NumTraits<Scalar>::dummy_precision()) const 00168 { return m_coeffs.isApprox(other.m_coeffs, prec); } 00169 00170 }; 00171 00174 typedef Translation<float, 2> Translation2f; 00175 typedef Translation<double,2> Translation2d; 00176 typedef Translation<float, 3> Translation3f; 00177 typedef Translation<double,3> Translation3d; 00179 00180 template<typename Scalar, int Dim> 00181 EIGEN_DEVICE_FUNC inline typename Translation<Scalar,Dim>::AffineTransformType 00182 Translation<Scalar,Dim>::operator* (const UniformScaling<Scalar>& other) const 00183 { 00184 AffineTransformType res; 00185 res.matrix().setZero(); 00186 res.linear().diagonal().fill(other.factor()); 00187 res.translation() = m_coeffs; 00188 res(Dim,Dim) = Scalar(1); 00189 return res; 00190 } 00191 00192 template<typename Scalar, int Dim> 00193 template<typename OtherDerived> 00194 EIGEN_DEVICE_FUNC inline typename Translation<Scalar,Dim>::AffineTransformType 00195 Translation<Scalar,Dim>::operator* (const EigenBase<OtherDerived>& linear) const 00196 { 00197 AffineTransformType res; 00198 res.matrix().setZero(); 00199 res.linear() = linear.derived(); 00200 res.translation() = m_coeffs; 00201 res.matrix().row(Dim).setZero(); 00202 res(Dim,Dim) = Scalar(1); 00203 return res; 00204 } 00205 00206 } // end namespace Eigen 00207 00208 #endif // EIGEN_TRANSLATION_H