IMP logo
IMP Reference Guide  develop.e004443c3b,2024/04/25
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-2022 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 #include <cereal/access.hpp>
17 
18 IMPALGEBRA_BEGIN_NAMESPACE
19 
20 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
21 // #ifndef IMP_DOXYGEN
22 class Transformation2D;
23 Transformation2D compose(const Transformation2D &a, const Transformation2D &b);
24 #endif
25 
26 //! Simple 2D transformation class
27 /** \geometry
28 */
29 class IMPALGEBRAEXPORT Transformation2D : public GeometricPrimitiveD<2> {
30  public:
31  IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(Transformation2D);
32 
33  //! Default constructor. An invalid transformation is built.
35 
36  //! Constructor from a Rotation2D and translation vector
37  Transformation2D(const Rotation2D &r, const Vector2D &t = Vector2D(0.0, 0.0))
38  : trans_(t), rot_(r) {}
39 
40  //! Constructor for a transformation with an identity rotation.
42  : trans_(t), rot_(get_identity_rotation_2d()) {}
43 
45 
46  //! Perform the transformation on a 2D vector
47  /**
48  \param[in] o vector where the transformation is applied
49  \note: The transformation is done first applying the rotation and then
50  the translation
51  **/
52  Vector2D get_transformed(const Vector2D &o) const {
53  return rot_.get_rotated(o) + trans_;
54  }
55 
56  //! Perform the transformation on a 2D vector
57  /**
58  \note: The transformation is done first applying the rotation and then
59  the translation
60  **/
61  Vector2D operator*(const Vector2D &v) const { return get_transformed(v); }
62 
63  //! Compose two transformations
64  /**
65  \note The transformations are composed such that for any vector v
66  (rt1*rt2)*v = rt1*(rt2*v)
67  **/
69  return compose(*this, tr);
70  }
71 
72  //! See help for operator*
74  *this = compose(*this, o);
75  return *this;
76  }
77 
78  //! Compute the transformation d which, when composed with b, gives this one.
79  /** That is a(x)== d(b(x)) for all x.
80  */
82  Transformation2D ret = compose(*this, b.get_inverse());
83  return ret;
84  }
85 
86  //! See help for operator/
88  *this = *this / o;
89  return *this;
90  }
91 
92  //! Return the rotation
93  const Rotation2D get_rotation() const { return rot_; }
94 
95  void set_rotation(double angle) { rot_.set_angle(angle); }
96 
97  //! Return the translation
98  const Vector2D get_translation() const { return trans_; }
99 
100  //! Set the translation
101  void set_translation(const Vector2D &v) {
102  trans_[0] = v[0];
103  trans_[1] = v[1];
104  }
105 
107  rot_.show(out);
108  out << " || " << trans_;
109  });
110 
111  //! Return the inverse transformation
112  Transformation2D get_inverse() const;
113 
114 private:
115  Vector2D trans_; // translation
116  Rotation2D rot_; // rotation
117 
118  friend class cereal::access;
119 
120  template<class Archive> void serialize(Archive &ar) {
121  ar(trans_, rot_);
122  }
123 
124 };
125 
127 
128 //! Return a transformation that does not do anything
129 /** \see Transformation2D
130 **/
133 }
134 
135 //! Generate a Transformation2D object from a rotation around a point
136 /**
137  Generate a Transformation2D to rotate about a point rather than the origin.
138  \param[in] point Center to rotate about
139  \param[in] rotation The rotation to perform (defined taking the origin as
140  reference, not the new point).
141  \see Transformation2D
142 */
144  const Rotation2D &rotation) {
145  return Transformation2D(rotation, (rotation.get_rotated(-point) + point));
146 }
147 
148 //! Compose two transformations
149 /** For any vector v (a*b)*v = a*(b*v).
150  \see Transformation2D
151  */
153  const Transformation2D &b) {
156 }
157 
158 IMPALGEBRA_END_NAMESPACE
159 #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:404
Represent a rotation in 2D space.
Definition: Rotation2D.h:37
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:121
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:50
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:103
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:48
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:72
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.