IMP logo
IMP Reference Guide  2.12.0
The Integrative Modeling Platform
Transformation2D.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/Transformation2D.h
3  * \brief 2D transformations.
4  * Copyright 2007-2019 IMP Inventors. All rights reserved.
5  *
6  */
7 
8 #ifndef IMPALGEBRA_TRANSFORMATION_2D_H
9 #define IMPALGEBRA_TRANSFORMATION_2D_H
10 
11 #include <IMP/algebra/algebra_config.h>
12 
13 #include "Vector2D.h"
14 #include "Rotation2D.h"
15 #include "GeometricPrimitiveD.h"
16 
17 IMPALGEBRA_BEGIN_NAMESPACE
18 
19 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
20 // #ifndef IMP_DOXYGEN
21 class Transformation2D;
22 Transformation2D compose(const Transformation2D &a, const Transformation2D &b);
23 #endif
24 
25 //! Simple 2D transformation class
26 /** \geometry
27 */
28 class IMPALGEBRAEXPORT Transformation2D : public GeometricPrimitiveD<2> {
29  public:
30  IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(Transformation2D);
31 
32  //! Default constructor. An invalid transformation is built.
34 
35  //! Constructor from a Rotation2D and translation vector
36  Transformation2D(const Rotation2D &r, const Vector2D &t = Vector2D(0.0, 0.0))
37  : trans_(t), rot_(r) {}
38 
39  //! Constructor for a transformation with an identity rotation.
41  : trans_(t), rot_(get_identity_rotation_2d()) {}
42 
44 
45  //! Perform the transformation on a 2D vector
46  /**
47  \param[in] o vector where the transformation is applied
48  \note: The transformation is done first applying the rotation and then
49  the translation
50  **/
51  Vector2D get_transformed(const Vector2D &o) const {
52  return rot_.get_rotated(o) + trans_;
53  }
54 
55  //! Perform the transformation on a 2D vector
56  /**
57  \note: The transformation is done first applying the rotation and then
58  the translation
59  **/
60  Vector2D operator*(const Vector2D &v) const { return get_transformed(v); }
61 
62  //! Compose two transformations
63  /**
64  \note The transformations are composed such that for any vector v
65  (rt1*rt2)*v = rt1*(rt2*v)
66  **/
68  return compose(*this, tr);
69  }
70 
71  //! See help for operator*
73  *this = compose(*this, o);
74  return *this;
75  }
76 
77  //! Compute the transformation d which, when composed with b, gives this one.
78  /** That is a(x)== d(b(x)) for all x.
79  */
81  Transformation2D ret = compose(*this, b.get_inverse());
82  return ret;
83  }
84 
85  //! See help for operator/
87  *this = *this / o;
88  return *this;
89  }
90 
91  //! Return the rotation
92  const Rotation2D get_rotation() const { return rot_; }
93 
94  void set_rotation(double angle) { rot_.set_angle(angle); }
95 
96  //! Return the translation
97  const Vector2D get_translation() const { return trans_; }
98 
99  //! Set the translation
100  void set_translation(const Vector2D &v) {
101  trans_[0] = v[0];
102  trans_[1] = v[1];
103  }
104 
106  rot_.show(out);
107  out << " || " << trans_;
108  });
109 
110  //! Return the inverse transformation
111  Transformation2D get_inverse() const;
112 
113  private:
114  Vector2D trans_; // translation
115  Rotation2D rot_; // rotation
116 };
117 
119 
120 //! Return a transformation that does not do anything
121 /** \see Transformation2D
122 **/
125 }
126 
127 //! Generate a Transformation2D object from a rotation around a point
128 /**
129  Generate a Transformation2D to rotate about a point rather than the origin.
130  \param[in] point Center to rotate about
131  \param[in] rotation The rotation to perform (defined taking the origin as
132  reference, not the new point).
133  \see Transformation2D
134 */
136  const Rotation2D &rotation) {
137  return Transformation2D(rotation, (rotation.get_rotated(-point) + point));
138 }
139 
140 //! Compose two transformations
141 /** For any vector v (a*b)*v = a*(b*v).
142  \see Transformation2D
143  */
145  const Transformation2D &b) {
148 }
149 
150 IMPALGEBRA_END_NAMESPACE
151 #endif /* IMPALGEBRA_TRANSFORMATION_2D_H */
Base class for geometric types.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Represent a rotation in 2D space.
VectorD< 2 > Vector2D
Definition: VectorD.h:417
Represent a rotation in 2D space.
Definition: Rotation2D.h:36
const Rotation2D get_rotation() const
Return the rotation.
Rotation2D compose(const Rotation2D &a, const Rotation2D &b)
Compose two rotations a and b.
Definition: Rotation2D.h:108
Simple 2D vector class.
Transformation2D get_inverse() const
Return the inverse transformation.
const Vector2D get_translation() const
Return the translation.
A more IMP-like version of the std::vector.
Definition: Vector.h:39
Transformation2D()
Default constructor. An invalid transformation is built.
Transformation2D get_rotation_about_point(const Vector2D &point, const Rotation2D &rotation)
Generate a Transformation2D object from a rotation around a point.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
Base class for geometric types.
Vector2D operator*(const Vector2D &v) const
Perform the transformation on a 2D vector.
Rotation2D get_identity_rotation_2d()
Build an identity rotation in 2D.
Definition: Rotation2D.h:90
Transformation2D(const Vector2D &t)
Constructor for a transformation with an identity rotation.
void set_translation(const Vector2D &v)
Set the translation.
const Transformation2D & operator*=(const Transformation2D &o)
See help for operator*.
Simple 2D transformation class.
Transformation2D operator*(const Transformation2D &tr) const
Compose two transformations.
Vector2D get_rotated(const Vector2D &o) const
Rotate a 2D point.
Definition: Rotation2D.h:47
Transformation2D compose(const Transformation2D &a, const Transformation2D &b)
Compose two transformations.
Transformation2D operator/(const Transformation2D &b) const
Compute the transformation d which, when composed with b, gives this one.
void set_angle(double angle)
Set the angle for the rotation.
Definition: Rotation2D.h:71
Transformation2D(const Rotation2D &r, const Vector2D &t=Vector2D(0.0, 0.0))
Constructor from a Rotation2D and translation vector.
const Transformation2D & operator/=(const Transformation2D &o)
See help for operator/.
Vector2D get_transformed(const Vector2D &o) const
Perform the transformation on a 2D vector.
Transformation2D get_identity_transformation_2d()
Return a transformation that does not do anything.