unproject.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include "OpenGL_convenience.h"
  11. IGL_INLINE int 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. // Put model, projection, and viewport matrices into double arrays
  20. double MV[16];
  21. double P[16];
  22. int VP[4];
  23. glGetDoublev(GL_MODELVIEW_MATRIX, MV);
  24. glGetDoublev(GL_PROJECTION_MATRIX, P);
  25. glGetIntegerv(GL_VIEWPORT, VP);
  26. return gluUnProject(winX,winY,winZ,MV,P,VP,objX,objY,objZ);
  27. }
  28. template <typename Derivedwin, typename Derivedobj>
  29. IGL_INLINE int igl::unproject(
  30. const Eigen::PlainObjectBase<Derivedwin> & win,
  31. Eigen::PlainObjectBase<Derivedobj> & obj)
  32. {
  33. Eigen::Vector3d dwin(win(0),win(1),win(2));
  34. Eigen::Vector3d dobj;
  35. int ret = unproject(dwin(0),dwin(1),dwin(2),
  36. &dobj.data()[0],
  37. &dobj.data()[1],
  38. &dobj.data()[2]);
  39. obj(0) = dobj(0);
  40. obj(1) = dobj(1);
  41. obj(2) = dobj(2);
  42. return ret;
  43. }
  44. template <typename Derivedwin>
  45. IGL_INLINE Eigen::PlainObjectBase<Derivedwin> igl::unproject(
  46. const Eigen::PlainObjectBase<Derivedwin> & win)
  47. {
  48. Eigen::PlainObjectBase<Derivedwin> obj;
  49. unproject(win,obj);
  50. return obj;
  51. }
  52. #ifndef IGL_HEADER_ONLY
  53. // Explicit template instanciation
  54. 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> >&);
  55. 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> >&);
  56. 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&);
  57. 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> >&);
  58. 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&);
  59. 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> >&);
  60. #endif
  61. #endif