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