rotate_by_quat.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #ifndef IGL_ROTATE_BY_QUAT_H
  2. #define IGL_ROTATE_BY_QUAT_H
  3. namespace igl
  4. {
  5. // Compute rotation of a given vector/point by 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. // v input 3d point/vector
  10. // q input quaternion
  11. // Outputs:
  12. // out result of rotation, allowed to be same as v
  13. template <typename Q_type>
  14. inline void rotate_by_quat(
  15. const Q_type *v,
  16. const Q_type *q,
  17. Q_type *out);
  18. };
  19. // Implementation
  20. #include "quat_conjugate.h"
  21. #include "quat_mult.h"
  22. #include "normalize_quat.h"
  23. #include <cassert>
  24. template <typename Q_type>
  25. inline void igl::rotate_by_quat(
  26. const Q_type *v,
  27. const Q_type *q,
  28. Q_type *out)
  29. {
  30. // Quaternion form of v, copy data in v, (as a result out can be same pointer
  31. // as v)
  32. Q_type quat_v[4] = {v[0],v[1],v[2],0};
  33. // normalize input
  34. Q_type normalized_q[4];
  35. bool normalized = igl::normalize_quat<Q_type>(q,normalized_q);
  36. assert(normalized);
  37. // Conjugate of q
  38. Q_type q_conj[4];
  39. igl::quat_conjugate<Q_type>(normalized_q,q_conj);
  40. // Rotate of vector v by quaternion q is:
  41. // q*v*conj(q)
  42. // Compute q*v
  43. Q_type q_mult_quat_v[4];
  44. igl::quat_mult<Q_type>(normalized_q,quat_v,q_mult_quat_v);
  45. // Compute (q*v) * conj(q)
  46. igl::quat_mult<Q_type>(q_mult_quat_v,q_conj,out);
  47. }
  48. #endif