axis_angle_to_quat.h 948 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <EPS.h>
  2. namespace igl
  3. {
  4. // Convert axis angle representation of a rotation to a quaternion
  5. // A Quaternion, q, is defined here as an arrays of four scalars (x,y,z,w),
  6. // such that q = x*i + y*j + z*k + w
  7. // Inputs:
  8. // axis 3d vector
  9. // angle scalar
  10. // Outputs:
  11. // quaternion
  12. inline void axis_angle_to_quat(
  13. const double *axis,
  14. const double angle,
  15. double *out);
  16. }
  17. // Implementation
  18. // http://www.antisphere.com/Wiki/tools:anttweakbar
  19. inline void igl::axis_angle_to_quat(
  20. const double *axis,
  21. const double angle,
  22. double *out)
  23. {
  24. double n = axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2];
  25. if( fabs(n)>igl::DOUBLE_EPS )
  26. {
  27. double f = 0.5*angle;
  28. out[3] = cos(f);
  29. f = sin(f)/sqrt(n);
  30. out[0] = axis[0]*f;
  31. out[1] = axis[1]*f;
  32. out[2] = axis[2]*f;
  33. }
  34. else
  35. {
  36. out[3] = 1.0;
  37. out[0] = out[1] = out[2] = 0.0;
  38. }
  39. }