8 #ifndef IMPALGEBRA_ROTATION_3D_H
9 #define IMPALGEBRA_ROTATION_3D_H
11 #include <IMP/algebra/algebra_config.h>
22 IMPALGEBRA_BEGIN_NAMESPACE
24 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
26 Rotation3D
compose(
const Rotation3D &a,
const Rotation3D &b);
47 mutable bool has_cache_;
51 void fill_cache()
const {
53 "Attempting to apply uninitialized rotation");
55 double v0s = get_squared(v_[0]);
56 double v1s = get_squared(v_[1]);
57 double v2s = get_squared(v_[2]);
58 double v3s = get_squared(v_[3]);
59 double v12 = v_[1] * v_[2];
60 double v01 = v_[0] * v_[1];
61 double v02 = v_[0] * v_[2];
62 double v23 = v_[2] * v_[3];
63 double v03 = v_[0] * v_[3];
64 double v13 = v_[1] * v_[3];
66 Vector3D(v0s + v1s - v2s - v3s, 2 * (v12 - v03), 2 * (v13 + v02));
68 Vector3D(2 * (v12 + v03), v0s - v1s + v2s - v3s, 2 * (v23 - v01));
70 Vector3D(2 * (v13 - v02), 2 * (v23 + v01), v0s - v1s - v2s + v3s);
74 IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(
Rotation3D);
77 : v_(v.get_unit_vector()), has_cache_(false) {}
85 : v_(a, b, c, d), has_cache_(false) {
87 v_.get_squared_magnitude(), 1.0,
88 "Attempting to construct a rotation from a "
89 <<
" non-quaternion value. The coefficient vector"
90 <<
" must have a length of 1. Got: " << a <<
" " << b <<
" " << c
91 <<
" " << d <<
" gives " << v_.get_squared_magnitude());
102 "Attempting to access uninitialized rotation");
104 (v_[0] * v_[0] + v_[1] * v_[1] - v_[2] * v_[2] - v_[3] * v_[3]) * o[0] +
105 2 * (v_[1] * v_[2] - v_[0] * v_[3]) * o[1] +
106 2 * (v_[1] * v_[3] + v_[0] * v_[2]) * o[2],
107 2 * (v_[1] * v_[2] + v_[0] * v_[3]) * o[0] +
108 (v_[0] * v_[0] - v_[1] * v_[1] + v_[2] * v_[2] - v_[3] * v_[3]) *
110 2 * (v_[2] * v_[3] - v_[0] * v_[1]) * o[2],
111 2 * (v_[1] * v_[3] - v_[0] * v_[2]) * o[0] +
112 2 * (v_[2] * v_[3] + v_[0] * v_[1]) * o[1] +
113 (v_[0] * v_[0] - v_[1] * v_[1] - v_[2] * v_[2] + v_[3] * v_[3]) *
118 double get_rotated_one_coordinate_no_cache(
const Vector3D &o,
119 unsigned int coord)
const {
121 "Attempting to apply uninitialized rotation");
124 return (v_[0] * v_[0] + v_[1] * v_[1] - v_[2] * v_[2] - v_[3] * v_[3]) *
126 2 * (v_[1] * v_[2] - v_[0] * v_[3]) * o[1] +
127 2 * (v_[1] * v_[3] + v_[0] * v_[2]) * o[2];
130 return 2 * (v_[1] * v_[2] + v_[0] * v_[3]) * o[0] +
131 (v_[0] * v_[0] - v_[1] * v_[1] + v_[2] * v_[2] - v_[3] * v_[3]) *
133 2 * (v_[2] * v_[3] - v_[0] * v_[1]) * o[2];
137 return 2 * (v_[1] * v_[3] - v_[0] * v_[2]) * o[0] +
138 2 * (v_[2] * v_[3] + v_[0] * v_[1]) * o[1] +
139 (v_[0] * v_[0] - v_[1] * v_[1] - v_[2] * v_[2] + v_[3] * v_[3]) *
150 "Attempting to apply uninitialized rotation");
151 if (!has_cache_) fill_cache();
152 return Vector3D(o * matrix_[0], o * matrix_[1], o * matrix_[2]);
157 unsigned int coord)
const {
159 "Attempting to apply uninitialized rotation");
160 if (!has_cache_) fill_cache();
161 return o * matrix_[coord];
166 Vector3D get_rotation_matrix_row(
int i)
const {
168 if (!has_cache_) fill_cache();
172 out << v_[0] <<
" " << v_[1] <<
" " << v_[2] <<
" " << v_[3];
178 "Attempting to invert uninitialized rotation");
179 Rotation3D ret(v_[0], -v_[1], -v_[2], -v_[3]);
189 "Attempting to access uninitialized rotation");
195 "Attempting to compose uninitialized rotation");
202 "Attempting to compose uninitialized rotation");
239 double ans = std::min(dot, odot);
240 const double s2 = std::sqrt(2.0);
241 double ret = ans / s2;
242 return std::max(std::min(ret, 1.0), 0.0);
256 IMPALGEBRAEXPORT Rotation3D
269 IMPALGEBRAEXPORT Rotation3D
275 IMPALGEBRAEXPORT Rotation3D
282 IMPALGEBRAEXPORT Rotation3D
284 double m11,
double m12,
double m20,
double m21,
303 IMPALGEBRAEXPORT Rotation3D
328 return Rotation3D(uv[0], uv[1], uv[2], uv[3]);
334 return Rotation3D(a.v_[0] * b.v_[0] - a.v_[1] * b.v_[1] - a.v_[2] * b.v_[2] -
336 a.v_[0] * b.v_[1] + a.v_[1] * b.v_[0] + a.v_[2] * b.v_[3] -
338 a.v_[0] * b.v_[2] - a.v_[1] * b.v_[3] + a.v_[2] * b.v_[0] +
340 a.v_[0] * b.v_[3] + a.v_[1] * b.v_[2] - a.v_[2] * b.v_[1] +
368 IMPALGEBRAEXPORT Rotation3D
382 IMPALGEBRAEXPORT Rotation3D
394 IMPALGEBRAEXPORT Rotation3D
403 FixedXYZ(
double x,
double y,
double z) {
408 double get_x()
const {
return v_[0]; }
409 double get_y()
const {
return v_[1]; }
410 double get_z()
const {
return v_[2]; }
412 { out << v_[0] <<
" " << v_[1] <<
" " << v_[2]; });
433 if (bq * aq < 0) bq = -bq;
434 return f * aq + (1 - f) * bq;
441 IMPALGEBRAEXPORT Rotation3D
454 const Rotation3D &rot);
456 typedef std::pair<Vector3D, double> AxisAnglePair;
461 IMPALGEBRA_END_NAMESPACE
Vector3D get_rotated(const Vector3D &o) const
Rotate a vector around the origin.
An exception for a request for an invalid member of a container.
Rotation3D get_rotation_about_normalized_axis(const Vector3D &axis_norm, double angle)
Rotation3D get_rotation_from_x_y_axes(const Vector3D &x, const Vector3D &y)
Rotation3D get_rotation_from_fixed_xyz(double xr, double yr, double zr)
Initialize a rotation in x-y-z order from three angles.
Rotation3D()
Create an invalid rotation.
Rotation2D compose(const Rotation2D &a, const Rotation2D &b)
compose two rotations a and b
algebra::Rotation3Ds get_uniformly_sampled_rotations(double delta)
Generates a nondegenerate set of Euler angles with a delta resolution.
Rotation3D operator/(const Rotation3D &r) const
Compute the rotation which when composed with r gives this.
A simple class for returning XYZ Euler angles.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Vector3D operator*(const Vector3D &v) const
Rotate a vector around the origin.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
double get_rotated_one_coordinate(const Vector3D &o, unsigned int coord) const
Gets only the requested rotation coordinate of the vector.
Rotation3D(const VectorD< 4 > &v)
Create a rotation from an unnormalized vector 4.
#define IMP_USAGE_CHECK_FLOAT_EQUAL(expra, exprb, message)
const Vector4D & get_quaternion() const
Return the quaternion so that it can be stored.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Rotation3D get_rotation_about_axis(const Vector3D &axis, double angle)
Generate a Rotation3D object from a rotation around an axis.
IMP::base::Vector< Rotation3D > Rotation3Ds
Rotation3D get_random_rotation_3d()
Pick a rotation at random from all possible rotations.
double get_distance(const Plane3D &pln, const Vector3D &p)
Return the distance between a plane and a point in 3D.
Functions to deal with very common math operations.
FixedXYZ get_fixed_xyz_from_rotation(const Rotation3D &r)
The inverse of rotation_from_fixed_xyz()
Rotation3D get_inverse() const
Return the rotation which undoes this rotation.
Rotation3D get_rotation_from_fixed_zyz(double Rot, double Tilt, double Psi)
Generate a rotation object from Euler Angles.
#define IMP_NO_SWIG(x)
Hide the line when SWIG is compiled or parses it.
std::pair< Vector3D, double > get_axis_and_angle(const Rotation3D &rot)
Decompose a Rotation3D object into a rotation around an axis.
Rotation3D get_interpolated(const Rotation3D &a, const Rotation3D &b, double f)
Interpolate between two rotations.
Rotation3D get_rotation_from_vector4d(const VectorD< 4 > &v)
Compute a rotatation from an unnormalized quaternion.
Rotation3D get_rotation_taking_first_to_second(const Vector3D &v1, const Vector3D &v2)
Create a rotation from the first vector to the second one.
Rotation3Ds get_uniform_cover_rotations_3d(unsigned int num_points)
Cover the space of rotations evenly.
Various useful constants.
Rotation3D operator*(const Rotation3D &q) const
multiply two rotations
Rotation3D get_rotation_from_matrix(double m00, double m01, double m02, double m10, double m11, double m12, double m20, double m21, double m22)
Generate a Rotation3D object from a rotation matrix.
Rotation3D(double a, double b, double c, double d)
Create a rotation from a quaternion.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Logging and error reporting support.
Rotation3D get_identity_rotation_3d()
Return a rotation that does not do anything.
VectorD get_unit_vector() const
Rotation3D get_rotation_from_fixed_zxz(double phi, double theta, double psi)
Initialize a rotation from euler angles.