axis_angle_to_quat.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. template <typename Q_type>
  16. inline void axis_angle_to_quat(
  17. const Q_type *axis,
  18. const Q_type angle,
  19. Q_type *out);
  20. }
  21. // Implementation
  22. // http://www.antisphere.com/Wiki/tools:anttweakbar
  23. template <typename Q_type>
  24. inline void igl::axis_angle_to_quat(
  25. const Q_type *axis,
  26. const Q_type angle,
  27. Q_type *out)
  28. {
  29. Q_type n = axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2];
  30. if( fabs(n)>igl::EPS<Q_type>())
  31. {
  32. Q_type f = 0.5*angle;
  33. out[3] = cos(f);
  34. f = sin(f)/sqrt(n);
  35. out[0] = axis[0]*f;
  36. out[1] = axis[1]*f;
  37. out[2] = axis[2]*f;
  38. }
  39. else
  40. {
  41. out[3] = 1.0;
  42. out[0] = out[1] = out[2] = 0.0;
  43. }
  44. }
  45. #endif