quat_mult.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #ifndef IGL_QUAT_MULT_H
  2. #define IGL_QUAT_MULT_H
  3. namespace igl
  4. {
  5. // Computes out = q1 * q2 with quaternion multiplication
  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. // q1 left quaternion
  10. // q2 right quaternion
  11. // Outputs:
  12. // out result of multiplication
  13. template <typename Q_type>
  14. inline void quat_mult(
  15. const Q_type *q1,
  16. const Q_type *q2,
  17. Q_type *out);
  18. };
  19. // Implementation
  20. #include <cassert>
  21. // http://www.antisphere.com/Wiki/tools:anttweakbar
  22. template <typename Q_type>
  23. inline void igl::quat_mult(
  24. const Q_type *q1,
  25. const Q_type *q2,
  26. Q_type *out)
  27. {
  28. // output can't be either of the inputs
  29. assert(q1 != out);
  30. assert(q2 != out);
  31. out[0] = q1[3]*q2[0] + q1[0]*q2[3] + q1[1]*q2[2] - q1[2]*q2[1];
  32. out[1] = q1[3]*q2[1] + q1[1]*q2[3] + q1[2]*q2[0] - q1[0]*q2[2];
  33. out[2] = q1[3]*q2[2] + q1[2]*q2[3] + q1[0]*q2[1] - q1[1]*q2[0];
  34. out[3] = q1[3]*q2[3] - (q1[0]*q2[0] + q1[1]*q2[1] + q1[2]*q2[2]);
  35. }
  36. #endif