unproject.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. IGL_INLINE void igl::unproject(
  12. const double winX,
  13. const double winY,
  14. const double winZ,
  15. double* objX,
  16. double* objY,
  17. double* objZ)
  18. {
  19. Eigen::Vector3d obj;
  20. igl::unproject(Eigen::Vector3d(winX,winY,winZ),obj);
  21. *objX = obj(0);
  22. *objY = obj(1);
  23. *objZ = obj(2);
  24. }
  25. template <typename Derivedwin, typename Derivedobj>
  26. IGL_INLINE void igl::unproject(
  27. const Eigen::PlainObjectBase<Derivedwin> & win,
  28. Eigen::PlainObjectBase<Derivedobj> & obj)
  29. {
  30. obj = igl::unproject(win).template cast<typename Derivedobj::Scalar>();
  31. }
  32. // #ifndef IGL_NO_OPENGL
  33. // #ifndef IGL_OPENGL_4
  34. #include "OpenGL_convenience.h"
  35. template <typename Derivedwin>
  36. IGL_INLINE Eigen::PlainObjectBase<Derivedwin> igl::unproject(
  37. const Eigen::PlainObjectBase<Derivedwin> & win)
  38. {
  39. using namespace Eigen;
  40. typedef typename Derivedwin::Scalar Scalar;
  41. Matrix4d MV,P;
  42. Vector4i VPi;
  43. Vector4d VPd;
  44. glGetDoublev(GL_MODELVIEW_MATRIX,MV.data());
  45. glGetDoublev(GL_PROJECTION_MATRIX,P.data());
  46. glGetIntegerv(GL_VIEWPORT,VPi.data());
  47. VPd = VPi.cast<double>();
  48. Vector3d wind = win.template cast<double>();
  49. Vector3d objd = igl::unproject(wind,MV,P,VPd);
  50. return objd.template cast<Scalar>();
  51. }
  52. // #endif
  53. // #endif
  54. template <typename Scalar>
  55. IGL_INLINE Eigen::Matrix<Scalar,3,1> igl::unproject(
  56. const Eigen::Matrix<Scalar,3,1>& win,
  57. const Eigen::Matrix<Scalar,4,4>& model,
  58. const Eigen::Matrix<Scalar,4,4>& proj,
  59. const Eigen::Matrix<Scalar,4,1>& viewport)
  60. {
  61. Eigen::Matrix<Scalar,4,4> Inverse = (proj * model).inverse();
  62. Eigen::Matrix<Scalar,4,1> tmp;
  63. tmp << win, 1;
  64. tmp(0) = (tmp(0) - viewport(0)) / viewport(2);
  65. tmp(1) = (tmp(1) - viewport(1)) / viewport(3);
  66. tmp = tmp.array() * 2.0f - 1.0f;
  67. Eigen::Matrix<Scalar,4,1> obj = Inverse * tmp;
  68. obj /= obj(3);
  69. return obj.head(3);
  70. }
  71. #ifdef IGL_STATIC_LIBRARY
  72. #ifndef IGL_NO_OPENGL
  73. #ifndef IGL_OPENGL_4
  74. // Explicit template instanciation
  75. #endif
  76. #endif
  77. 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&);
  78. 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&);
  79. template Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > igl::unproject<Eigen::Matrix<double, 3, 1, 0, 3, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&);
  80. #endif