unproject.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 int 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. // Put model, projection, and viewport matrices into double arrays
  23. double MV[16];
  24. double P[16];
  25. int VP[4];
  26. glGetDoublev(GL_MODELVIEW_MATRIX, MV);
  27. glGetDoublev(GL_PROJECTION_MATRIX, P);
  28. glGetIntegerv(GL_VIEWPORT, VP);
  29. return gluUnProject(winX,winY,winZ,MV,P,VP,objX,objY,objZ);
  30. }
  31. template <typename Derivedwin, typename Derivedobj>
  32. IGL_INLINE int igl::unproject(
  33. const Eigen::PlainObjectBase<Derivedwin> & win,
  34. Eigen::PlainObjectBase<Derivedobj> & obj)
  35. {
  36. Eigen::Vector3d dwin(win(0),win(1),win(2));
  37. Eigen::Vector3d dobj;
  38. int ret = unproject(dwin(0),dwin(1),dwin(2),
  39. &dobj.data()[0],
  40. &dobj.data()[1],
  41. &dobj.data()[2]);
  42. obj(0) = dobj(0);
  43. obj(1) = dobj(1);
  44. obj(2) = dobj(2);
  45. return ret;
  46. }
  47. template <typename Derivedwin>
  48. IGL_INLINE Eigen::PlainObjectBase<Derivedwin> igl::unproject(
  49. const Eigen::PlainObjectBase<Derivedwin> & win)
  50. {
  51. Eigen::PlainObjectBase<Derivedwin> obj;
  52. unproject(win,obj);
  53. return obj;
  54. }
  55. #endif
  56. #endif
  57. Eigen::Vector3f igl::unproject(const Eigen::Vector3f& win,
  58. const Eigen::Matrix4f& model,
  59. const Eigen::Matrix4f& proj,
  60. const Eigen::Vector4f& viewport)
  61. {
  62. Eigen::Matrix4f Inverse = (proj * model).inverse();
  63. Eigen::Vector4f tmp;
  64. tmp << win, 1;
  65. tmp(0) = (tmp(0) - viewport(0)) / viewport(2);
  66. tmp(1) = (tmp(1) - viewport(1)) / viewport(3);
  67. tmp = tmp.array() * 2.0f - 1.0f;
  68. Eigen::Vector4f obj = Inverse * tmp;
  69. obj /= obj(3);
  70. return obj.head(3);
  71. }
  72. #ifndef IGL_NO_OPENGL
  73. #ifndef IGL_OPENGL_4
  74. #ifdef IGL_STATIC_LIBRARY
  75. // Explicit template instanciation
  76. template int 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> >&);
  77. template int 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> >&);
  78. 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&);
  79. template int 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> >&);
  80. 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&);
  81. template int 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> >&);
  82. #endif
  83. #endif
  84. #endif