rotate_by_quat.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "rotate_by_quat.h"
  2. #include "quat_conjugate.h"
  3. #include "quat_mult.h"
  4. #include "normalize_quat.h"
  5. #include <cassert>
  6. template <typename Q_type>
  7. IGL_INLINE void igl::rotate_by_quat(
  8. const Q_type *v,
  9. const Q_type *q,
  10. Q_type *out)
  11. {
  12. // Quaternion form of v, copy data in v, (as a result out can be same pointer
  13. // as v)
  14. Q_type quat_v[4] = {v[0],v[1],v[2],0};
  15. // normalize input
  16. Q_type normalized_q[4];
  17. #ifndef NDEBUG
  18. bool normalized =
  19. #endif
  20. igl::normalize_quat<Q_type>(q,normalized_q);
  21. #ifndef NDEBUG
  22. assert(normalized);
  23. #endif
  24. // Conjugate of q
  25. Q_type q_conj[4];
  26. igl::quat_conjugate<Q_type>(normalized_q,q_conj);
  27. // Rotate of vector v by quaternion q is:
  28. // q*v*conj(q)
  29. // Compute q*v
  30. Q_type q_mult_quat_v[4];
  31. igl::quat_mult<Q_type>(normalized_q,quat_v,q_mult_quat_v);
  32. // Compute (q*v) * conj(q)
  33. igl::quat_mult<Q_type>(q_mult_quat_v,q_conj,out);
  34. }
  35. #ifndef IGL_HEADER_ONLY
  36. // Explicit template specialization
  37. // generated by autoexplicit.sh
  38. template void igl::rotate_by_quat<float>(float const*, float const*, float*);
  39. template void igl::rotate_by_quat<double>(double const*, double const*, double*);
  40. #endif