axis_angle_to_quat.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef IGL_AXIS_ANGLE_TO_QUAT_H
  2. #define IGL_AXIS_ANGLE_TO_QUAT_H
  3. #include <EPS.h>
  4. #include <cmath>
  5. namespace igl
  6. {
  7. // Convert axis angle representation of a rotation to a quaternion
  8. // A Quaternion, q, is defined here as an arrays of four scalars (x,y,z,w),
  9. // such that q = x*i + y*j + z*k + w
  10. // Inputs:
  11. // axis 3d vector
  12. // angle scalar
  13. // Outputs:
  14. // quaternion
  15. inline void axis_angle_to_quat(
  16. const double *axis,
  17. const double angle,
  18. double *out);
  19. // Same but with floats
  20. inline void axis_angle_to_quat(
  21. const float *axis,
  22. const float angle,
  23. float *out);
  24. }
  25. // Implementation
  26. // http://www.antisphere.com/Wiki/tools:anttweakbar
  27. inline void igl::axis_angle_to_quat(
  28. const double *axis,
  29. const double angle,
  30. double *out)
  31. {
  32. double n = axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2];
  33. if( fabs(n)>igl::DOUBLE_EPS )
  34. {
  35. double f = 0.5*angle;
  36. out[3] = cos(f);
  37. f = sin(f)/sqrt(n);
  38. out[0] = axis[0]*f;
  39. out[1] = axis[1]*f;
  40. out[2] = axis[2]*f;
  41. }
  42. else
  43. {
  44. out[3] = 1.0;
  45. out[0] = out[1] = out[2] = 0.0;
  46. }
  47. }
  48. // http://www.antisphere.com/Wiki/tools:anttweakbar
  49. inline void igl::axis_angle_to_quat(
  50. const float *axis,
  51. const float angle,
  52. float *out)
  53. {
  54. float n = axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2];
  55. if( fabs(n)>igl::FLOAT_EPS )
  56. {
  57. float f = 0.5*angle;
  58. out[3] = cos(f);
  59. f = sin(f)/sqrt(n);
  60. out[0] = axis[0]*f;
  61. out[1] = axis[1]*f;
  62. out[2] = axis[2]*f;
  63. }
  64. else
  65. {
  66. out[3] = 1.0;
  67. out[0] = out[1] = out[2] = 0.0;
  68. }
  69. }
  70. #endif