IMP logo
IMP Reference Guide  2.19.0
The Integrative Modeling Platform
Rotation2D.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/Rotation2D.h
3  * \brief Represent a rotation in 2D space.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6 */
7 
8 #ifndef IMPALGEBRA_ROTATION_2D_H
9 #define IMPALGEBRA_ROTATION_2D_H
10 
11 #include <IMP/algebra/algebra_config.h>
12 #include "utility.h"
13 #include "Vector2D.h"
14 #include "GeometricPrimitiveD.h"
15 #include "constants.h"
16 #include <IMP/random.h>
17 #include <boost/random/uniform_01.hpp>
18 #include <cereal/access.hpp>
19 #include <cmath>
20 //#include <stdlib.h>
21 
22 IMPALGEBRA_BEGIN_NAMESPACE
23 
24 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
25 class Rotation2D;
26 Rotation2D compose(const Rotation2D &a, const Rotation2D &b);
27 #endif
28 
29 //! Represent a rotation in 2D space.
30 /**
31  \note This class requires the angles to be given in radians, and the
32  convention used is that the rotations are performed rotating counterclockwise
33  (right hand side convention).
34 
35  \geometry
36 **/
37 class Rotation2D : public GeometricPrimitiveD<2> {
38  public:
39  Rotation2D() : angle_(std::numeric_limits<double>::quiet_NaN()) {};
40 
41  //! Build the matrix for the given angle
42  Rotation2D(double angle) { set_angle(angle); }
43 
44  //! Rotate a 2D point
45  /**
46  * \param[in] o a 2D vector to be rotated
47  */
48  Vector2D get_rotated(const Vector2D &o) const {
50  "Attempting to use uninitialized rotation");
51  return get_rotated(o[0], o[1]);
52  }
53 
54  //! Rotate a 2D point
55  Vector2D get_rotated(const double x, const double y) const {
57  "Attempting to use uninitialized rotation");
58  return Vector2D(c_ * x - s_ * y, s_ * x + c_ * y);
59  }
60 
61  //! Return the matrix for the inverse rotation
64  "Attempting to use uninitialized rotation");
65  return Rotation2D(-angle_);
66  }
67 
68  //! Set the angle for the rotation
69  /**
70  * \param[in] angle the angle
71  */
72  void set_angle(double angle) {
73  angle_ = angle;
74  c_ = cos(angle);
75  s_ = sin(angle);
76  }
77 
78  //! Get the angle
79  double get_angle() const { return angle_; }
80 
81  //! Print the angle
82  IMP_SHOWABLE_INLINE(Rotation2D, out << "Rotation2D (radians): " << angle_;);
83 
84 private:
85  double angle_; // angle
86  double c_; // cosine of the angle
87  double s_; // sine of the angle
88 
89  friend class cereal::access;
90 
91  template<class Archive> void save(Archive &ar) const {
92  ar(angle_);
93  }
94 
95  template<class Archive> void load(Archive &ar) {
96  double a;
97  ar(a);
98  set_angle(a);
99  }
100 };
101 
102 //! Build an identity rotation in 2D
104 
105 //! Build an identity rotation in 2D
107  return Rotation2D(2 * PI *
108  boost::uniform_01<>()(random_number_generator));
109 }
110 
111 //! Build the rotation that transforms the vector X of the origin
112 //! of coordinates into the given vector
114  return Rotation2D(atan2(v[1], v[0]));
115 }
116 
117 //! Compose two rotations a and b.
118 /**
119  For any vector v (a*b)*v = a*(b*v).
120 */
121 inline Rotation2D compose(const Rotation2D &a, const Rotation2D &b) {
122  double new_angle = a.get_angle() + b.get_angle();
123  Rotation2D R(new_angle);
124  return R;
125 }
126 
128 
129 IMPALGEBRA_END_NAMESPACE
130 
131 #endif /* IMPALGEBRA_ROTATION_2D_H */
Rotation2D get_inverse() const
Return the matrix for the inverse rotation.
Definition: Rotation2D.h:62
Base class for geometric types.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
VectorD< 2 > Vector2D
Definition: VectorD.h:421
Represent a rotation in 2D space.
Definition: Rotation2D.h:37
Rotation2D compose(const Rotation2D &a, const Rotation2D &b)
Compose two rotations a and b.
Definition: Rotation2D.h:121
static const double PI
the constant pi
Simple 2D vector class.
A more IMP-like version of the std::vector.
Definition: Vector.h:42
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Definition: check_macros.h:139
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
Base class for geometric types.
Functions to deal with very common math operations.
Rotation2D get_identity_rotation_2d()
Build an identity rotation in 2D.
Definition: Rotation2D.h:103
Vector2D get_rotated(const Vector2D &o) const
Rotate a 2D point.
Definition: Rotation2D.h:48
Rotation2D get_rotation_to_x_axis(const Vector2D &v)
Definition: Rotation2D.h:113
Various useful constants.
bool isnan(const T &a)
Return true if a number is NaN.
Definition: math.h:24
double get_angle() const
Get the angle.
Definition: Rotation2D.h:79
void set_angle(double angle)
Set the angle for the rotation.
Definition: Rotation2D.h:72
Random number generators used by IMP.
Rotation2D(double angle)
Build the matrix for the given angle.
Definition: Rotation2D.h:42
RandomNumberGenerator random_number_generator
A shared non-GPU random number generator.
Vector2D get_rotated(const double x, const double y) const
Rotate a 2D point.
Definition: Rotation2D.h:55
Rotation2D get_random_rotation_2d()
Build an identity rotation in 2D.
Definition: Rotation2D.h:106