2 #ifndef vnl_quaternion_hxx_ 3 #define vnl_quaternion_hxx_ 39 # include <vcl_msvc_warnings.h> 57 this->operator[](0) = tx;
58 this->operator[](1) = ty;
59 this->operator[](2) = tz;
60 this->operator[](3) = rea;
68 double a = Angle * 0.5;
70 for (
int i = 0; i < 3; i++)
71 this->
operator[](i) = T(s * Axis(i));
72 this->operator[](3) = T(std::cos(a));
82 for (
unsigned int i = 0; i < 3; ++i)
83 this->
operator[](i) = vec(i);
84 this->operator[](3) = T(0);
94 for (
unsigned int i = 0; i < 4; ++i)
95 this->
operator[](i) = vec[i];
107 double d0 = rot(0,0), d1 = rot(1,1), d2 = rot(2,2);
108 double xx = 1.0 + d0 - d1 - d2;
109 double yy = 1.0 - d0 + d1 - d2;
110 double zz = 1.0 - d0 - d1 + d2;
111 double rr = 1.0 + d0 + d1 + d2;
119 T r4 = T(std::sqrt(rr)*2);
122 this->x() = (rot(1,2) - rot(2,1)) * r4;
123 this->y() = (rot(2,0) - rot(0,2)) * r4;
124 this->z() = (rot(0,1) - rot(1,0)) * r4;
126 else if (xx ==
max) {
127 T x4 = T(std::sqrt(xx)*2);
130 this->y() = (rot(0,1) + rot(1,0)) * x4;
131 this->z() = (rot(0,2) + rot(2,0)) * x4;
132 this->r() = (rot(1,2) - rot(2,1)) * x4;
134 else if (yy ==
max) {
135 T y4 = T(std::sqrt(yy)*2);
138 this->x() = (rot(0,1) + rot(1,0)) * y4;
139 this->z() = (rot(1,2) + rot(2,1)) * y4;
140 this->r() = (rot(2,0) - rot(0,2)) * y4;
143 T z4 = T(std::sqrt(zz)*2);
146 this->x() = (rot(0,2) + rot(2,0)) * z4;
147 this->y() = (rot(1,2) + rot(2,1)) * z4;
148 this->r() = (rot(0,1) - rot(1,0)) * z4;
159 vnl_quaternion<T> Rx(T(std::sin(
double(theta_X)*0.5)), 0, 0, T(std::cos(
double(theta_X)*0.5)));
160 vnl_quaternion<T> Ry(0, T(std::sin(
double(theta_Y)*0.5)), 0, T(std::cos(
double(theta_Y)*0.5)));
161 vnl_quaternion<T> Rz(0, 0, T(std::sin(
double(theta_Z)*0.5)), T(std::cos(
double(theta_Z)*0.5)));
162 *
this = Rz * Ry * Rx;
177 if (xy > std::numeric_limits<T>::epsilon() * T(8))
179 angles(0) = T(std::atan2(
double(rotM(1,2)),
double(rotM(2,2))));
180 angles(1) = T(std::atan2(
double(-rotM(0,2)),
double(xy)));
181 angles(2) = T(std::atan2(
double(rotM(0,1)),
double(rotM(0,0))));
185 angles(0) = T(std::atan2(
double(-rotM(2,1)),
double(rotM(1,1))));
186 angles(1) = T(std::atan2(
double(-rotM(0,2)),
double(xy)));
198 return 2 * std::atan2(
double(this->imaginary().magnitude()),
199 double(this->real()));
210 std::cout <<
"Axis not well defined for zero Quaternion. Using (0,0,1) instead.\n";
226 T x2 = x() * x(), xy = x() * y(), rx = r() * x(),
227 y2 = y() * y(), yz = y() * z(), ry = r() * y(),
228 z2 = z() * z(), zx = z() * x(), rz = r() * z(),
231 rot(0,0) = r2 + x2 - y2 - z2;
232 rot(1,1) = r2 - x2 + y2 - z2;
233 rot(2,2) = r2 - x2 - y2 + z2;
234 rot(0,1) = 2 * (xy + rz);
235 rot(0,2) = 2 * (zx - ry);
236 rot(1,2) = 2 * (yz + rx);
237 rot(1,0) = 2 * (xy - rz);
238 rot(2,0) = 2 * (zx + ry);
239 rot(2,1) = 2 * (yz - rx);
290 img += (i2 * r1) + (i1 * r2);
301 T rea = this->real();
307 #undef VNL_QUATERNION_INSTANTIATE 308 #define VNL_QUATERNION_INSTANTIATE(T) \ 309 template class VNL_EXPORT vnl_quaternion<T >;\ 312 #endif // vnl_quaternion_hxx_ abs_t magnitude() const
Return magnitude (length) of vector.
vnl_matrix_fixed< T, 4, 4 > rotation_matrix_transpose_4() const
4x4 rotation matrix.
vnl_vector_fixed< T, 3 > imaginary() const
Copies and returns the imaginary part.
T real() const
Copies and returns the real part.
4-element vector that represents rotation in 3D.
vnl_matrix_fixed & update(vnl_matrix< T > const &, unsigned top=0, unsigned left=0)
Set values of this matrix to those of M, starting at [top,left].
Namespace with standard math functions.
Implements cross product for vectors.
vnl_vector_fixed< T, 3 > axis() const
Axis of rotation.
vnl_quaternion< T > conjugate() const
Same real, opposite img part.
vnl_matrix_fixed & set_identity()
Sets this matrix to an identity matrix, then returns "*this".
double angle() const
Angle of rotation.
T dot_product(const vnl_vector_fixed< T, n > &a, const vnl_vector_fixed< T, n > &b)
vnl_quaternion< T > operator *(vnl_quaternion< T > const &) const
Returns the product of two quaternions.
vnl_decnum max(vnl_decnum const &x, vnl_decnum const &y)
vnl_vector_fixed< T, 3 > rotate(vnl_vector_fixed< T, 3 > const &) const
Rotate 3D v.
vnl_vector_fixed< T, 3 > rotation_euler_angles() const
Rotation representation in Euler angles.
vnl_quaternion< T > inverse() const
Inverse for nonzero quat.
vnl_matrix_fixed< T, 3, 3 > rotation_matrix_transpose() const
3x3 rotation matrix.
vnl_quaternion()=default
Constructor for null quaternion.
static T dot_product(T const *, T const *, unsigned)
vnl_bignum sqr(vnl_bignum const &x)
Unit quaternion represents rotation in 3D.
vnl_vector< T > vnl_cross_3d(const vnl_vector< T > &v1, const vnl_vector< T > &v2)
Compute the 3-D cross product.