IMP  2.1.0
The Integrative Modeling Platform
Rotation2D.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/Rotation2D.h
3  * \brief Classes and operations related with rotations
4  *
5  * Copyright 2007-2013 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 <cmath>
17 //#include <stdlib.h>
18 
19 IMPALGEBRA_BEGIN_NAMESPACE
20 
21 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
22 class Rotation2D;
23 Rotation2D compose(const Rotation2D &a, const Rotation2D &b);
24 #endif
25 
26 //! Stores a 2D rotation matrix
27 /**
28  \note This class requires the angles to be given in radians, and the
29  convention used is that the rotations are performed rotating counterclockwise
30  (right hand side convention).
31 
32  \geometry
33 **/
34 class Rotation2D : public GeometricPrimitiveD<2> {
35  public:
36  Rotation2D() : angle_(std::numeric_limits<double>::quiet_NaN()) {};
37 
38  //! Builds the matrix for the given angle
39  Rotation2D(double angle) { set_angle(angle); }
40 
41  //! rotates a 2D point
42  /**
43  * \param[in] o a 2D vector to be rotated
44  */
45  Vector2D get_rotated(const Vector2D &o) const {
47  "Attempting to use uninitialized rotation");
48  return get_rotated(o[0], o[1]);
49  }
50 
51  //! rotates a 2D point
52  Vector2D get_rotated(const double x, const double y) const {
54  "Attempting to use uninitialized rotation");
55  return Vector2D(c_ * x - s_ * y, s_ * x + c_ * y);
56  }
57 
58  //! Returns the matrix for the inverse rotation
61  "Attempting to use uninitialized rotation");
62  return Rotation2D(-angle_);
63  }
64 
65  //! sets the angle for the rotation
66  /**
67  * \param[in] angle the angle
68  */
69  void set_angle(double angle) {
70  angle_ = angle;
71  c_ = cos(angle);
72  s_ = sin(angle);
73  }
74 
75  //! gets the angle
76  double get_angle() const { return angle_; }
77 
78  //! Prints the angle
79  IMP_SHOWABLE_INLINE(Rotation2D, out << "Rotation2D (radians): " << angle_;);
80 
81  private:
82  double angle_; // angle
83  double c_; // cosine of the angle
84  double s_; // sine of the angle
85 };
86 
87 //! Builds an identity rotation in 2D
89 
90 //! Builds an identity rotation in 2D
92  return Rotation2D(2 * PI * ((double)rand() / ((double)RAND_MAX + 1)));
93 }
94 
95 //! Builds the rotation that transforms the vector X of the origin
96 //! of coordinates into the given vector
98  return Rotation2D(atan2(v[1], v[0]));
99 }
100 
101 //! compose two rotations a and b
102 /**
103  For any vector v (a*b)*v = a*(b*v).
104 */
105 inline Rotation2D compose(const Rotation2D &a, const Rotation2D &b) {
106  double new_angle = a.get_angle() + b.get_angle();
107  Rotation2D R(new_angle);
108  return R;
109 }
110 
112 
113 IMPALGEBRA_END_NAMESPACE
114 #endif /* IMPALGEBRA_ROTATION_2D_H */
Rotation2D get_inverse() const
Returns the matrix for the inverse rotation.
Definition: Rotation2D.h:59
Basic types used by IMP.
VectorD< 2 > Vector2D
Definition: VectorD.h:583
Stores a 2D rotation matrix.
Definition: Rotation2D.h:34
Rotation2D compose(const Rotation2D &a, const Rotation2D &b)
compose two rotations a and b
Definition: Rotation2D.h:105
static const double PI
the constant pi
Simple 2D vector class.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Functions to deal with very common math operations.
Rotation2D get_identity_rotation_2d()
Builds an identity rotation in 2D.
Definition: Rotation2D.h:88
#define IMP_INTERNAL_CHECK(expr, message)
An assertion to check for internal errors in IMP. An IMP::ErrorException will be thrown.
Vector2D get_rotated(const Vector2D &o) const
rotates a 2D point
Definition: Rotation2D.h:45
Rotation2D get_rotation_to_x_axis(const Vector2D &v)
Definition: Rotation2D.h:97
Various useful constants.
double get_angle() const
gets the angle
Definition: Rotation2D.h:76
void set_angle(double angle)
sets the angle for the rotation
Definition: Rotation2D.h:69
bool isnan(const T &a)
Return true if a number is NaN.
Definition: base/math.h:24
Rotation2D(double angle)
Builds the matrix for the given angle.
Definition: Rotation2D.h:39
Vector2D get_rotated(const double x, const double y) const
rotates a 2D point
Definition: Rotation2D.h:52
Rotation2D get_random_rotation_2d()
Builds an identity rotation in 2D.
Definition: Rotation2D.h:91