snap_to_fixed_up.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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 "snap_to_fixed_up.h"
  9. template <typename Qtype>
  10. IGL_INLINE void igl::snap_to_fixed_up(
  11. const Eigen::Quaternion<Qtype> & q,
  12. Eigen::Quaternion<Qtype> & s)
  13. {
  14. using namespace Eigen;
  15. typedef Eigen::Matrix<Qtype,3,1> Vector3Q;
  16. const Vector3Q up = q.matrix() * Vector3Q(0,1,0);
  17. Vector3Q proj_up(0,up(1),up(2));
  18. if(proj_up.norm() == 0)
  19. {
  20. proj_up = Vector3Q(0,1,0);
  21. }
  22. proj_up.normalize();
  23. Quaternion<Qtype> dq;
  24. dq = Quaternion<Qtype>::FromTwoVectors(up,proj_up);
  25. s = dq * q;
  26. }
  27. #ifdef IGL_STATIC_LIBRARY
  28. // Explicit template instanciations
  29. template void igl::snap_to_fixed_up<float>(Eigen::Quaternion<float, 0> const&, Eigen::Quaternion<float, 0>&);
  30. template void igl::snap_to_fixed_up<double>(Eigen::Quaternion<double, 0> const&, Eigen::Quaternion<double, 0>&);
  31. #endif