PageRenderTime 103ms CodeModel.GetById 82ms app.highlight 15ms RepoModel.GetById 3ms app.codeStats 0ms

/src/Geometry_Eigen/Eigen/src/Eigen2Support/Geometry/Translation.h

http://github.com/Akranar/daguerreo
C Header | 196 lines | 101 code | 26 blank | 69 comment | 0 complexity | 7918364c7edde9001d10e831262cbf4a MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, LGPL-3.0, GPL-2.0
  1// This file is part of Eigen, a lightweight C++ template library
  2// for linear algebra. Eigen itself is part of the KDE project.
  3//
  4// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
  5//
  6// Eigen is free software; you can redistribute it and/or
  7// modify it under the terms of the GNU Lesser General Public
  8// License as published by the Free Software Foundation; either
  9// version 3 of the License, or (at your option) any later version.
 10//
 11// Alternatively, you can redistribute it and/or
 12// modify it under the terms of the GNU General Public License as
 13// published by the Free Software Foundation; either version 2 of
 14// the License, or (at your option) any later version.
 15//
 16// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
 17// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 18// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
 19// GNU General Public License for more details.
 20//
 21// You should have received a copy of the GNU Lesser General Public
 22// License and a copy of the GNU General Public License along with
 23// Eigen. If not, see <http://www.gnu.org/licenses/>.
 24
 25// no include guard, we'll include this twice from All.h from Eigen2Support, and it's internal anyway
 26
 27
 28/** \geometry_module \ingroup Geometry_Module
 29  *
 30  * \class Translation
 31  *
 32  * \brief Represents a translation transformation
 33  *
 34  * \param _Scalar the scalar type, i.e., the type of the coefficients.
 35  * \param _Dim the  dimension of the space, can be a compile time value or Dynamic
 36  *
 37  * \note This class is not aimed to be used to store a translation transformation,
 38  * but rather to make easier the constructions and updates of Transform objects.
 39  *
 40  * \sa class Scaling, class Transform
 41  */
 42template<typename _Scalar, int _Dim>
 43class Translation
 44{
 45public:
 46  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(_Scalar,_Dim)
 47  /** dimension of the space */
 48  enum { Dim = _Dim };
 49  /** the scalar type of the coefficients */
 50  typedef _Scalar Scalar;
 51  /** corresponding vector type */
 52  typedef Matrix<Scalar,Dim,1> VectorType;
 53  /** corresponding linear transformation matrix type */
 54  typedef Matrix<Scalar,Dim,Dim> LinearMatrixType;
 55  /** corresponding scaling transformation type */
 56  typedef Scaling<Scalar,Dim> ScalingType;
 57  /** corresponding affine transformation type */
 58  typedef Transform<Scalar,Dim> TransformType;
 59
 60protected:
 61
 62  VectorType m_coeffs;
 63
 64public:
 65
 66  /** Default constructor without initialization. */
 67  Translation() {}
 68  /**  */
 69  inline Translation(const Scalar& sx, const Scalar& sy)
 70  {
 71    ei_assert(Dim==2);
 72    m_coeffs.x() = sx;
 73    m_coeffs.y() = sy;
 74  }
 75  /**  */
 76  inline Translation(const Scalar& sx, const Scalar& sy, const Scalar& sz)
 77  {
 78    ei_assert(Dim==3);
 79    m_coeffs.x() = sx;
 80    m_coeffs.y() = sy;
 81    m_coeffs.z() = sz;
 82  }
 83  /** Constructs and initialize the scaling transformation from a vector of scaling coefficients */
 84  explicit inline Translation(const VectorType& vector) : m_coeffs(vector) {}
 85
 86  const VectorType& vector() const { return m_coeffs; }
 87  VectorType& vector() { return m_coeffs; }
 88
 89  /** Concatenates two translation */
 90  inline Translation operator* (const Translation& other) const
 91  { return Translation(m_coeffs + other.m_coeffs); }
 92
 93  /** Concatenates a translation and a scaling */
 94  inline TransformType operator* (const ScalingType& other) const;
 95
 96  /** Concatenates a translation and a linear transformation */
 97  inline TransformType operator* (const LinearMatrixType& linear) const;
 98
 99  template<typename Derived>
100  inline TransformType operator*(const RotationBase<Derived,Dim>& r) const
101  { return *this * r.toRotationMatrix(); }
102
103  /** Concatenates a linear transformation and a translation */
104  // its a nightmare to define a templated friend function outside its declaration
105  friend inline TransformType operator* (const LinearMatrixType& linear, const Translation& t)
106  {
107    TransformType res;
108    res.matrix().setZero();
109    res.linear() = linear;
110    res.translation() = linear * t.m_coeffs;
111    res.matrix().row(Dim).setZero();
112    res(Dim,Dim) = Scalar(1);
113    return res;
114  }
115
116  /** Concatenates a translation and an affine transformation */
117  inline TransformType operator* (const TransformType& t) const;
118
119  /** Applies translation to vector */
120  inline VectorType operator* (const VectorType& other) const
121  { return m_coeffs + other; }
122
123  /** \returns the inverse translation (opposite) */
124  Translation inverse() const { return Translation(-m_coeffs); }
125
126  Translation& operator=(const Translation& other)
127  {
128    m_coeffs = other.m_coeffs;
129    return *this;
130  }
131
132  /** \returns \c *this with scalar type casted to \a NewScalarType
133    *
134    * Note that if \a NewScalarType is equal to the current scalar type of \c *this
135    * then this function smartly returns a const reference to \c *this.
136    */
137  template<typename NewScalarType>
138  inline typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type cast() const
139  { return typename internal::cast_return_type<Translation,Translation<NewScalarType,Dim> >::type(*this); }
140
141  /** Copy constructor with scalar type conversion */
142  template<typename OtherScalarType>
143  inline explicit Translation(const Translation<OtherScalarType,Dim>& other)
144  { m_coeffs = other.vector().template cast<Scalar>(); }
145
146  /** \returns \c true if \c *this is approximately equal to \a other, within the precision
147    * determined by \a prec.
148    *
149    * \sa MatrixBase::isApprox() */
150  bool isApprox(const Translation& other, typename NumTraits<Scalar>::Real prec = precision<Scalar>()) const
151  { return m_coeffs.isApprox(other.m_coeffs, prec); }
152
153};
154
155/** \addtogroup Geometry_Module */
156//@{
157typedef Translation<float, 2> Translation2f;
158typedef Translation<double,2> Translation2d;
159typedef Translation<float, 3> Translation3f;
160typedef Translation<double,3> Translation3d;
161//@}
162
163
164template<typename Scalar, int Dim>
165inline typename Translation<Scalar,Dim>::TransformType
166Translation<Scalar,Dim>::operator* (const ScalingType& other) const
167{
168  TransformType res;
169  res.matrix().setZero();
170  res.linear().diagonal() = other.coeffs();
171  res.translation() = m_coeffs;
172  res(Dim,Dim) = Scalar(1);
173  return res;
174}
175
176template<typename Scalar, int Dim>
177inline typename Translation<Scalar,Dim>::TransformType
178Translation<Scalar,Dim>::operator* (const LinearMatrixType& linear) const
179{
180  TransformType res;
181  res.matrix().setZero();
182  res.linear() = linear;
183  res.translation() = m_coeffs;
184  res.matrix().row(Dim).setZero();
185  res(Dim,Dim) = Scalar(1);
186  return res;
187}
188
189template<typename Scalar, int Dim>
190inline typename Translation<Scalar,Dim>::TransformType
191Translation<Scalar,Dim>::operator* (const TransformType& t) const
192{
193  TransformType res = t;
194  res.pretranslate(m_coeffs);
195  return res;
196}