rotate_by_quat.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "rotate_by_quat.h"
  9. #include "quat_conjugate.h"
  10. #include "quat_mult.h"
  11. #include "normalize_quat.h"
  12. #include <cassert>
  13. template <typename Q_type>
  14. IGL_INLINE void igl::rotate_by_quat(
  15. const Q_type *v,
  16. const Q_type *q,
  17. Q_type *out)
  18. {
  19. // Quaternion form of v, copy data in v, (as a result out can be same pointer
  20. // as v)
  21. Q_type quat_v[4] = {v[0],v[1],v[2],0};
  22. // normalize input
  23. Q_type normalized_q[4];
  24. #ifndef NDEBUG
  25. bool normalized =
  26. #endif
  27. igl::normalize_quat<Q_type>(q,normalized_q);
  28. #ifndef NDEBUG
  29. assert(normalized);
  30. #endif
  31. // Conjugate of q
  32. Q_type q_conj[4];
  33. igl::quat_conjugate<Q_type>(normalized_q,q_conj);
  34. // Rotate of vector v by quaternion q is:
  35. // q*v*conj(q)
  36. // Compute q*v
  37. Q_type q_mult_quat_v[4];
  38. igl::quat_mult<Q_type>(normalized_q,quat_v,q_mult_quat_v);
  39. // Compute (q*v) * conj(q)
  40. igl::quat_mult<Q_type>(q_mult_quat_v,q_conj,out);
  41. }
  42. #ifdef IGL_STATIC_LIBRARY
  43. // Explicit template specialization
  44. // generated by autoexplicit.sh
  45. template void igl::rotate_by_quat<double>(double const*, double const*, double*);
  46. // generated by autoexplicit.sh
  47. template void igl::rotate_by_quat<float>(float const*, float const*, float*);
  48. #endif