IMP logo
IMP Reference Guide  2.18.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 <boost/serialization/access.hpp>
19 #include <boost/serialization/split_member.hpp>
20 #include <cmath>
21 //#include <stdlib.h>
22 
23 IMPALGEBRA_BEGIN_NAMESPACE
24 
25 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
26 class Rotation2D;
27 Rotation2D compose(const Rotation2D &a, const Rotation2D &b);
28 #endif
29 
30 //! Represent a rotation in 2D space.
31 /**
32  \note This class requires the angles to be given in radians, and the
33  convention used is that the rotations are performed rotating counterclockwise
34  (right hand side convention).
35 
36  \geometry
37 **/
38 class Rotation2D : public GeometricPrimitiveD<2> {
39  public:
40  Rotation2D() : angle_(std::numeric_limits<double>::quiet_NaN()) {};
41 
42  //! Build the matrix for the given angle
43  Rotation2D(double angle) { set_angle(angle); }
44 
45  //! Rotate a 2D point
46  /**
47  * \param[in] o a 2D vector to be rotated
48  */
49  Vector2D get_rotated(const Vector2D &o) const {
51  "Attempting to use uninitialized rotation");
52  return get_rotated(o[0], o[1]);
53  }
54 
55  //! Rotate a 2D point
56  Vector2D get_rotated(const double x, const double y) const {
58  "Attempting to use uninitialized rotation");
59  return Vector2D(c_ * x - s_ * y, s_ * x + c_ * y);
60  }
61 
62  //! Return the matrix for the inverse rotation
65  "Attempting to use uninitialized rotation");
66  return Rotation2D(-angle_);
67  }
68 
69  //! Set the angle for the rotation
70  /**
71  * \param[in] angle the angle
72  */
73  void set_angle(double angle) {
74  angle_ = angle;
75  c_ = cos(angle);
76  s_ = sin(angle);
77  }
78 
79  //! Get the angle
80  double get_angle() const { return angle_; }
81 
82  //! Print the angle
83  IMP_SHOWABLE_INLINE(Rotation2D, out << "Rotation2D (radians): " << angle_;);
84 
85 private:
86  double angle_; // angle
87  double c_; // cosine of the angle
88  double s_; // sine of the angle
89 
90 #ifndef SWIG
91  friend class boost::serialization::access;
92 
93  template<class Archive> void save(Archive &ar, const unsigned int) const {
94  ar << angle_;
95  }
96 
97  template<class Archive> void load(Archive &ar, const unsigned int) {
98  double a;
99  ar >> a;
100  set_angle(a);
101  }
102 
103  BOOST_SERIALIZATION_SPLIT_MEMBER()
104 #endif
105 };
106 
107 //! Build an identity rotation in 2D
109 
110 //! Build an identity rotation in 2D
112  return Rotation2D(2 * PI *
113  boost::uniform_01<>()(random_number_generator));
114 }
115 
116 //! Build the rotation that transforms the vector X of the origin
117 //! of coordinates into the given vector
119  return Rotation2D(atan2(v[1], v[0]));
120 }
121 
122 //! Compose two rotations a and b.
123 /**
124  For any vector v (a*b)*v = a*(b*v).
125 */
126 inline Rotation2D compose(const Rotation2D &a, const Rotation2D &b) {
127  double new_angle = a.get_angle() + b.get_angle();
128  Rotation2D R(new_angle);
129  return R;
130 }
131 
133 
134 IMPALGEBRA_END_NAMESPACE
135 
136 #endif /* IMPALGEBRA_ROTATION_2D_H */
Rotation2D get_inverse() const
Return the matrix for the inverse rotation.
Definition: Rotation2D.h:63
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:417
Represent a rotation in 2D space.
Definition: Rotation2D.h:38
Rotation2D compose(const Rotation2D &a, const Rotation2D &b)
Compose two rotations a and b.
Definition: Rotation2D.h:126
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:108
Vector2D get_rotated(const Vector2D &o) const
Rotate a 2D point.
Definition: Rotation2D.h:49
Rotation2D get_rotation_to_x_axis(const Vector2D &v)
Definition: Rotation2D.h:118
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:80
void set_angle(double angle)
Set the angle for the rotation.
Definition: Rotation2D.h:73
Random number generators used by IMP.
Rotation2D(double angle)
Build the matrix for the given angle.
Definition: Rotation2D.h:43
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:56
Rotation2D get_random_rotation_2d()
Build an identity rotation in 2D.
Definition: Rotation2D.h:111