IMP
2.0.0
The Integrative Modeling Platform
Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
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
22
#if !defined(IMP_DOXYGEN) && !defined(SWIG)
23
class
Rotation2D;
24
Rotation2D
compose
(
const
Rotation2D &a,
const
Rotation2D &b) ;
25
#endif
26
27
28
//! Stores a 2D rotation matrix
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
{
38
public
:
39
Rotation2D
(): angle_(std::numeric_limits<double>::quiet_NaN()) {};
40
41
//! Builds the matrix for the given angle
42
Rotation2D
(
double
angle) {
43
set_angle(angle);
44
}
45
46
//! rotates a 2D point
47
/**
48
* \param[in] o a 2D vector to be rotated
49
*/
50
Vector2D
get_rotated
(
const
Vector2D
&o)
const
{
51
IMP_INTERNAL_CHECK
(!
base::isnan
(angle_),
52
"Attempting to use uninitialized rotation"
);
53
return
get_rotated(o[0],o[1]);
54
}
55
56
//! rotates a 2D point
57
Vector2D
get_rotated
(
const
double
x,
const
double
y)
const
{
58
IMP_INTERNAL_CHECK
(!
base::isnan
(angle_),
59
"Attempting to use uninitialized rotation"
);
60
return
Vector2D
(c_*x-s_*y , s_*x+c_*y);
61
}
62
63
//! Returns the matrix for the inverse rotation
64
Rotation2D
get_inverse
()
const
{
65
IMP_INTERNAL_CHECK
(!
base::isnan
(angle_),
66
"Attempting to use uninitialized rotation"
);
67
return
Rotation2D
(-angle_);
68
}
69
70
//! sets the angle for the rotation
71
/**
72
* \param[in] angle the angle
73
*/
74
void
set_angle
(
double
angle) {
75
angle_ = angle;
76
c_ = cos(angle);
77
s_ = sin(angle);
78
}
79
80
//! gets the angle
81
double
get_angle
()
const
{
82
return
angle_;
83
}
84
85
//! Prints the angle
86
IMP_SHOWABLE_INLINE
(
Rotation2D
,
87
out <<
"Rotation2D (radians): "
<< angle_;);
88
89
private
:
90
double
angle_;
// angle
91
double
c_;
// cosine of the angle
92
double
s_;
// sine of the angle
93
};
94
95
96
//! Builds an identity rotation in 2D
97
inline
Rotation2D
get_identity_rotation_2d
() {
98
return
Rotation2D
(0.0);
99
}
100
101
//! Builds an identity rotation in 2D
102
inline
Rotation2D
get_random_rotation_2d
() {
103
return
Rotation2D
(2*
PI
*((
double
)rand() /((
double
)RAND_MAX+1)));
104
}
105
106
107
//! Builds the rotation that transforms the vector X of the origin
108
//! of coordinates into the given vector
109
inline
Rotation2D
get_rotation_to_x_axis
(
const
Vector2D
&v) {
110
return
Rotation2D
(atan2(v[1],v[0]));
111
}
112
113
//! compose two rotations a and b
114
/**
115
For any vector v (a*b)*v = a*(b*v).
116
*/
117
inline
Rotation2D
compose
(
const
Rotation2D
&a,
const
Rotation2D
&b) {
118
double
new_angle = a.
get_angle
()+b.
get_angle
();
119
Rotation2D
R(new_angle);
120
return
R;
121
}
122
123
IMP_VALUES
(
Rotation2D
,
Rotation2Ds
);
124
125
IMPALGEBRA_END_NAMESPACE
126
#endif
/* IMPALGEBRA_ROTATION_2D_H */