unproject.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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(
  58. const Eigen::Vector3f& win,
  59. const Eigen::Matrix4f& model,
  60. const Eigen::Matrix4f& proj,
  61. const Eigen::Vector4f& viewport)
  62. {
  63. Eigen::Matrix4f Inverse = (proj * model).inverse();
  64. Eigen::Vector4f tmp;
  65. tmp << win, 1;
  66. tmp(0) = (tmp(0) - viewport(0)) / viewport(2);
  67. tmp(1) = (tmp(1) - viewport(1)) / viewport(3);
  68. tmp = tmp.array() * 2.0f - 1.0f;
  69. Eigen::Vector4f obj = Inverse * tmp;
  70. obj /= obj(3);
  71. return obj.head(3);
  72. }
  73. #ifdef IGL_STATIC_LIBRARY
  74. #ifndef IGL_NO_OPENGL
  75. #ifndef IGL_OPENGL_4
  76. // Explicit template instanciation
  77. 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> >&);
  78. 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> >&);
  79. 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&);
  80. 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> >&);
  81. 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&);
  82. 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> >&);
  83. #endif
  84. #endif
  85. #endif