axis_angle_to_quat.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 "axis_angle_to_quat.h"
  9. #include "EPS.h"
  10. #include <cmath>
  11. // http://www.antisphere.com/Wiki/tools:anttweakbar
  12. template <typename Q_type>
  13. IGL_INLINE void igl::axis_angle_to_quat(
  14. const Q_type *axis,
  15. const Q_type angle,
  16. Q_type *out)
  17. {
  18. Q_type n = axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2];
  19. if( fabs(n)>igl::EPS<Q_type>())
  20. {
  21. Q_type f = 0.5*angle;
  22. out[3] = cos(f);
  23. f = sin(f)/sqrt(n);
  24. out[0] = axis[0]*f;
  25. out[1] = axis[1]*f;
  26. out[2] = axis[2]*f;
  27. }
  28. else
  29. {
  30. out[3] = 1.0;
  31. out[0] = out[1] = out[2] = 0.0;
  32. }
  33. }
  34. #ifdef IGL_STATIC_LIBRARY
  35. // Explicit template specialization
  36. // generated by autoexplicit.sh
  37. template void igl::axis_angle_to_quat<double>(double const*, double, double*);
  38. // generated by autoexplicit.sh
  39. template void igl::axis_angle_to_quat<float>(float const*, float, float*);
  40. #endif