unproject.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef IGL_NO_OPENGL
  10. #ifndef IGL_OPENGL_4
  11. #include <Eigen/Dense>
  12. #include <Eigen/LU>
  13. #include "OpenGL_convenience.h"
  14. IGL_INLINE void igl::unproject(
  15. const double winX,
  16. const double winY,
  17. const double winZ,
  18. double* objX,
  19. double* objY,
  20. double* objZ)
  21. {
  22. Eigen::Vector3d obj;
  23. igl::unproject(Eigen::Vector3d(winX,winY,winZ),obj);
  24. *objX = obj(0);
  25. *objY = obj(1);
  26. *objZ = obj(2);
  27. }
  28. template <typename Derivedwin, typename Derivedobj>
  29. IGL_INLINE void igl::unproject(
  30. const Eigen::PlainObjectBase<Derivedwin> & win,
  31. Eigen::PlainObjectBase<Derivedobj> & obj)
  32. {
  33. obj = igl::unproject(win).template cast<typename Derivedobj::Scalar>();
  34. }
  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 void igl::unproject<Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&);
  76. template void igl::unproject<Eigen::Matrix<float, 3, 1, 0, 3, 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, 3, 1, 0, 3, 1> >&);
  77. template Eigen::PlainObjectBase<Eigen::Matrix<float, 3, 1, 0, 3, 1> > igl::unproject<Eigen::Matrix<float, 3, 1, 0, 3, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, 3, 1, 0, 3, 1> > const&);
  78. template void igl::unproject<Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<float, 3, 1, 0, 3, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 3, 1, 0, 3, 1> >&);
  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. template void igl::unproject<Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> >&);
  81. template Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > igl::unproject<Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&);
  82. #endif
  83. 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&);
  84. 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&);
  85. #endif
  86. #endif