rotate_by_quat.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. bool normalized = igl::normalize_quat<Q_type>(q,normalized_q);
  18. assert(normalized);
  19. // Conjugate of q
  20. Q_type q_conj[4];
  21. igl::quat_conjugate<Q_type>(normalized_q,q_conj);
  22. // Rotate of vector v by quaternion q is:
  23. // q*v*conj(q)
  24. // Compute q*v
  25. Q_type q_mult_quat_v[4];
  26. igl::quat_mult<Q_type>(normalized_q,quat_v,q_mult_quat_v);
  27. // Compute (q*v) * conj(q)
  28. igl::quat_mult<Q_type>(q_mult_quat_v,q_conj,out);
  29. }
  30. #ifndef IGL_HEADER_ONLY
  31. // Explicit template specialization
  32. // generated by autoexplicit.sh
  33. template void igl::rotate_by_quat<float>(float const*, float const*, float*);
  34. #endif