## [Vxl-users] rotation matrix and quaternion

 [Vxl-users] rotation matrix and quaternion From: Michael Kuhn - 2003-07-23 23:42:17 Hi, I'm trying to set up a vnl_quaternion (representing a rotation) in two different ways. On one hand, I'm trying to set it up directly, i.e. by using the constructor that takes an axis and an angle. On the other hand, I'm trying to create it by vnl_rotation_matrix, which I created using the same axis and angle as before. I noticed, that the quaternions produced by these two construction methods are not equal. The difference is in the orientation of the rotation axis. Can somebody explain me why? To make sure that my explanation is clear enough, I appended the code and output below. Thanks, Michael CODE: ===== (...) double x = 1; double y = 1; double z = 1; double mag = x * x + y * y + z * z; mag = sqrt(mag); x /= mag; y /= mag; z /= mag; double alpha = 40.0 * vnl_math::pi / 180.0; double* vec = new double[3]; vnl_vector axis(3); axis[0] = x; axis[1] = y; axis[2] = z; vnl_quaternion q(axis, alpha); double tmp3 = 2 * acos(q.r()); cout << "q: " << endl; cout << "alpha: " << 180 / vnl_math::pi * tmp3 << endl; cout << "x: " << q.x() / sin(tmp3 / 2) * mag << endl; cout << "y: " << q.y() / sin(tmp3 / 2) * mag << endl; cout << "z: " << q.z() / sin(tmp3 / 2) * mag << endl; cout << endl << endl; axis[0] *= alpha; axis[1] *= alpha; axis[2] *= alpha; vnl_matrix rMat3x3 = vnl_rotation_matrix(axis); vnl_quaternion q2(rMat3x3); double tmp4 = 2 * acos(q2.r()); cout << "q2: " << endl; cout << "alpha: " << 180 / vnl_math::pi * tmp4 << endl; cout << "x: " << q2.x() / sin(tmp4 / 2) * mag << endl; cout << "y: " << q2.y() / sin(tmp4 / 2) * mag << endl; cout << "z: " << q2.z() / sin(tmp4 / 2) * mag << endl; OUTPUT: ======= (...) q: alpha: 40 x: 1 y: 1 z: 1 q2: alpha: 40 x: -1 y: -1 z: -1 Press any key to continue