unproject.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef IGL_OPENGL_4
  12. IGL_INLINE void igl::unproject(
  13. const double winX,
  14. const double winY,
  15. const double winZ,
  16. double* objX,
  17. double* objY,
  18. double* objZ)
  19. {
  20. Eigen::Vector3d obj;
  21. igl::unproject(Eigen::Vector3d(winX,winY,winZ),obj);
  22. *objX = obj(0);
  23. *objY = obj(1);
  24. *objZ = obj(2);
  25. }
  26. template <typename Derivedwin, typename Derivedobj>
  27. IGL_INLINE void igl::unproject(
  28. const Eigen::PlainObjectBase<Derivedwin> & win,
  29. Eigen::PlainObjectBase<Derivedobj> & obj)
  30. {
  31. obj = igl::unproject(win).template cast<typename Derivedobj::Scalar>();
  32. }
  33. // #ifndef IGL_NO_OPENGL
  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. 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&);
  76. #endif
  77. //#endif
  78. 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&);
  79. 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&);
  80. #endif