unproject.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 "unproject.h"
  9. #include <Eigen/Dense>
  10. #include <Eigen/LU>
  11. template <typename Scalar>
  12. IGL_INLINE Eigen::Matrix<Scalar,3,1> igl::unproject(
  13. const Eigen::Matrix<Scalar,3,1>& win,
  14. const Eigen::Matrix<Scalar,4,4>& model,
  15. const Eigen::Matrix<Scalar,4,4>& proj,
  16. const Eigen::Matrix<Scalar,4,1>& viewport)
  17. {
  18. Eigen::Matrix<Scalar,4,4> Inverse = (proj * model).inverse();
  19. Eigen::Matrix<Scalar,4,1> tmp;
  20. tmp << win, 1;
  21. tmp(0) = (tmp(0) - viewport(0)) / viewport(2);
  22. tmp(1) = (tmp(1) - viewport(1)) / viewport(3);
  23. tmp = tmp.array() * 2.0f - 1.0f;
  24. Eigen::Matrix<Scalar,4,1> obj = Inverse * tmp;
  25. obj /= obj(3);
  26. return obj.head(3);
  27. }
  28. #ifdef IGL_STATIC_LIBRARY
  29. template Eigen::Matrix<float, 3, 1, 0, 3, 1> igl::unproject<float>(Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, Eigen::Matrix<float, 4, 1, 0, 4, 1> const&);
  30. template Eigen::Matrix<double, 3, 1, 0, 3, 1> igl::unproject<double>(Eigen::Matrix<double, 3, 1, 0, 3, 1> const&, Eigen::Matrix<double, 4, 4, 0, 4, 4> const&, Eigen::Matrix<double, 4, 4, 0, 4, 4> const&, Eigen::Matrix<double, 4, 1, 0, 4, 1> const&);
  31. #endif