normalize_quat.h 928 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef IGL_NORMALIZE_QUAT_H
  2. #define IGL_NORMALIZE_QUAT_H
  3. namespace igl
  4. {
  5. // Normalize a quaternion
  6. // A Quaternion, q, is defined here as an arrays of four scalars (x,y,z,w),
  7. // such that q = x*i + y*j + z*k + w
  8. // Inputs:
  9. // q input quaternion
  10. // Outputs:
  11. // out result of normalization, allowed to be same as q
  12. // Returns true on success, false if len(q) < EPS
  13. template <typename Q_type>
  14. inline bool normalize_quat(
  15. const Q_type *q,
  16. Q_type *out);
  17. };
  18. // Implementation
  19. #include "EPS.h"
  20. template <typename Q_type>
  21. inline bool igl::normalize_quat(
  22. const Q_type *q,
  23. Q_type *out)
  24. {
  25. // Get length
  26. Q_type len = sqrt(
  27. q[0]*q[0]+
  28. q[1]*q[1]+
  29. q[2]*q[2]+
  30. q[3]*q[3]);
  31. // Noramlize each coordinate
  32. out[0] = q[0]/len;
  33. out[1] = q[1]/len;
  34. out[2] = q[2]/len;
  35. out[3] = q[3]/len;
  36. // Test whether length was below Epsilon
  37. return (len > igl::EPS<Q_type>());
  38. }
  39. #endif