unproject.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 <
  12. typename Derivedwin,
  13. typename Derivedmodel,
  14. typename Derivedproj,
  15. typename Derivedviewport,
  16. typename Derivedscene>
  17. IGL_INLINE void igl::unproject(
  18. const Eigen::PlainObjectBase<Derivedwin>& win,
  19. const Eigen::PlainObjectBase<Derivedmodel>& model,
  20. const Eigen::PlainObjectBase<Derivedproj>& proj,
  21. const Eigen::PlainObjectBase<Derivedviewport>& viewport,
  22. Eigen::PlainObjectBase<Derivedscene> & scene)
  23. {
  24. if(win.cols() != 3)
  25. {
  26. assert(win.rows() == 3);
  27. // needless transposes
  28. Eigen::Matrix<typename Derivedscene::Scalar,1,3> sceneT;
  29. unproject(win.transpose().eval(),model,proj,viewport,sceneT);
  30. scene = sceneT.head(3);
  31. return;
  32. }
  33. assert(win.cols() == 3);
  34. const int n = win.rows();
  35. scene.resize(n,3);
  36. for(int i = 0;i<n;i++)
  37. {
  38. typedef typename Derivedscene::Scalar Scalar;
  39. Eigen::Matrix<Scalar,4,4> Inverse =
  40. (proj.template cast<Scalar>() * model.template cast<Scalar>()).inverse();
  41. Eigen::Matrix<Scalar,4,1> tmp;
  42. tmp << win.row(i).head(3).transpose(), 1;
  43. tmp(0) = (tmp(0) - viewport(0)) / viewport(2);
  44. tmp(1) = (tmp(1) - viewport(1)) / viewport(3);
  45. tmp = tmp.array() * 2.0f - 1.0f;
  46. Eigen::Matrix<Scalar,4,1> obj = Inverse * tmp;
  47. obj /= obj(3);
  48. scene.row(i).head(3) = obj.head(3);
  49. }
  50. }
  51. template <typename Scalar>
  52. IGL_INLINE Eigen::Matrix<Scalar,3,1> igl::unproject(
  53. const Eigen::Matrix<Scalar,3,1>& win,
  54. const Eigen::Matrix<Scalar,4,4>& model,
  55. const Eigen::Matrix<Scalar,4,4>& proj,
  56. const Eigen::Matrix<Scalar,4,1>& viewport)
  57. {
  58. Eigen::Matrix<Scalar,3,1> scene;
  59. unproject(win,model,proj,viewport,scene);
  60. return scene;
  61. }
  62. #ifdef IGL_STATIC_LIBRARY
  63. 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&);
  64. 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&);
  65. template void igl::unproject<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<float, 4, 4, 0, 4, 4>, Eigen::Matrix<float, 4, 4, 0, 4, 4>, Eigen::Matrix<float, 4, 1, 0, 4, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 4, 1, 0, 4, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  66. template void igl::unproject<Eigen::Matrix<float, 3, 1, 0, 3, 1>, Eigen::Matrix<float, 4, 4, 0, 4, 4>, Eigen::Matrix<float, 4, 4, 0, 4, 4>, Eigen::Matrix<float, 4, 1, 0, 4, 1>, Eigen::Matrix<float, 3, 1, 0, 3, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 4, 4, 0, 4, 4> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 4, 1, 0, 4, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 3, 1, 0, 3, 1> >&);
  67. #endif