00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef IMPALGEBRA_ROTATION_2D_H
00010 #define IMPALGEBRA_ROTATION_2D_H
00011
00012 #include "algebra_config.h"
00013 #include "utility.h"
00014 #include "Vector2D.h"
00015 #include "Matrix2D.h"
00016 #include "IMP/constants.h"
00017 #include <cmath>
00018 #include <stdlib.h>
00019
00020 IMPALGEBRA_BEGIN_NAMESPACE
00021
00022
00023 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
00024 class Rotation2D;
00025 Rotation2D compose(const Rotation2D &a, const Rotation2D &b) ;
00026 #endif
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 class Rotation2D
00038 {
00039 public:
00040 Rotation2D(): angle_(std::numeric_limits<double>::quiet_NaN()) {};
00041
00042
00043 Rotation2D(double angle) {
00044 set_angle(angle);
00045 }
00046
00047
00048
00049
00050
00051 VectorD<2> get_rotated(const VectorD<2> &o) const {
00052 IMP_INTERNAL_CHECK(!is_nan(angle_),
00053 "Attempting to use uninitialized rotation");
00054 return get_rotated(o[0],o[1]);
00055 }
00056
00057
00058 VectorD<2> get_rotated(const double x,const double y) const {
00059 IMP_INTERNAL_CHECK(!is_nan(angle_),
00060 "Attempting to use uninitialized rotation");
00061 return VectorD<2>(c_*x-s_*y , s_*x+c_*y);
00062 }
00063
00064
00065 Rotation2D get_inverse() const {
00066 IMP_INTERNAL_CHECK(!is_nan(angle_),
00067 "Attempting to use uninitialized rotation");
00068 return Rotation2D(-angle_);
00069 }
00070
00071
00072
00073
00074
00075 void set_angle(double angle) {
00076 angle_ = angle;
00077 c_ = cos(angle);
00078 s_ = sin(angle);
00079 }
00080
00081
00082 double get_angle() const {
00083 return angle_;
00084 }
00085
00086
00087 void show(std::ostream& out = std::cout, std::string delim=" ") const {
00088 out << "Rotation2D (radians): " << angle_;
00089 }
00090
00091 private:
00092 double angle_;
00093 double c_;
00094 double s_;
00095 };
00096
00097
00098
00099 inline Rotation2D get_identity_rotation_2d() {
00100 return Rotation2D(0.0);
00101 };
00102
00103
00104 inline Rotation2D get_random_rotation_2d() {
00105 return Rotation2D(2*PI*((double)rand() /((double)RAND_MAX+1)));
00106 };
00107
00108
00109
00110
00111 inline Rotation2D get_rotation_to_x_axis(const VectorD<2> &v) {
00112 return Rotation2D(atan2(v[1],v[0]));
00113 };
00114
00115
00116
00117
00118
00119 inline Rotation2D compose(const Rotation2D &a,const Rotation2D &b) {
00120 double new_angle = a.get_angle()+b.get_angle();
00121 Rotation2D R(new_angle);
00122 return R;
00123 };
00124
00125
00126
00127 IMPALGEBRA_END_NAMESPACE
00128 #endif