project.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "project.h"
  2. #ifndef IGL_NO_OPENGL
  3. #include <iostream>
  4. #include "report_gl_error.h"
  5. #include "OpenGL_convenience.h"
  6. IGL_INLINE int igl::project(
  7. const double objX,
  8. const double objY,
  9. const double objZ,
  10. double* winX,
  11. double* winY,
  12. double* winZ)
  13. {
  14. using namespace std;
  15. #ifdef EXTREME_VERBOSE
  16. cout<<"project();"<<endl;
  17. #endif
  18. // Put model, projection, and viewport matrices into double arrays
  19. double MV[16];
  20. double P[16];
  21. int VP[4];
  22. glGetDoublev(GL_MODELVIEW_MATRIX, MV);
  23. #ifdef EXTREME_VERBOSE
  24. cout<<"MV=["<<endl<<
  25. MV[0]<<" "<< MV[1]<<" "<< MV[2]<<" "<< MV[3]<<" "<<endl<<
  26. MV[4]<<" "<< MV[5]<<" "<< MV[6]<<" "<< MV[7]<<" "<<endl<<
  27. MV[8]<<" "<< MV[9]<<" "<< MV[10]<<" "<< MV[11]<<" "<<endl<<
  28. MV[12]<<" "<< MV[13]<<" "<< MV[14]<<" "<< MV[15]<<" "<<endl<<
  29. "];"<<endl;
  30. #endif
  31. #ifndef NDEBUG
  32. igl::report_gl_error();
  33. #endif
  34. glGetDoublev(GL_PROJECTION_MATRIX, P);
  35. #ifdef EXTREME_VERBOSE
  36. cout<<"P=["<<endl<<
  37. P[0]<<" "<< P[1]<<" "<< P[2]<<" "<< P[3]<<" "<<endl<<
  38. P[4]<<" "<< P[5]<<" "<< P[6]<<" "<< P[7]<<" "<<endl<<
  39. P[8]<<" "<< P[9]<<" "<< P[10]<<" "<< P[11]<<" "<<endl<<
  40. P[12]<<" "<< P[13]<<" "<< P[14]<<" "<< P[15]<<" "<<endl<<
  41. "];"<<endl;
  42. #endif
  43. #ifndef NDEBUG
  44. igl::report_gl_error();
  45. #endif
  46. glGetIntegerv(GL_VIEWPORT, VP);
  47. #ifdef EXTREME_VERBOSE
  48. cout<<"VP=["<<endl<<
  49. VP[0]<<" "<< VP[1]<<" "<< VP[2]<<" "<< VP[3]<<" "<<endl<<
  50. "];"<<endl;
  51. #endif
  52. #ifndef NDEBUG
  53. igl::report_gl_error();
  54. #endif
  55. #ifdef EXTREME_VERBOSE
  56. cout<<"obj=["<<endl<<
  57. objX<<" "<< objY<<" "<< objZ<<endl<<
  58. "];"<<endl;
  59. #endif
  60. int ret = gluProject(objX,objY,objZ,MV,P,VP,winX,winY,winZ);
  61. #ifdef EXTREME_VERBOSE
  62. cout<<"win=["<<endl<<
  63. *winX<<" "<< *winY<<" "<< *winZ<<endl<<
  64. "];"<<endl;
  65. #endif
  66. return ret;
  67. }
  68. template <typename Derivedobj, typename Derivedwin>
  69. IGL_INLINE int igl::project(
  70. const Eigen::PlainObjectBase<Derivedobj> & obj,
  71. Eigen::PlainObjectBase<Derivedwin> & win)
  72. {
  73. Eigen::Vector3d dobj(obj(0),obj(1),obj(2));
  74. Eigen::Vector3d dwin;
  75. int ret = project(dobj(0),dobj(1),dobj(2),
  76. &dwin.data()[0],
  77. &dwin.data()[1],
  78. &dwin.data()[2]);
  79. win(0) = dwin(0);
  80. win(1) = dwin(1);
  81. win(2) = dwin(2);
  82. return ret;
  83. }
  84. template <typename Derivedobj>
  85. IGL_INLINE Eigen::PlainObjectBase<Derivedobj> igl::project(
  86. const Eigen::PlainObjectBase<Derivedobj> & obj)
  87. {
  88. Eigen::PlainObjectBase<Derivedobj> win;
  89. project(obj,win);
  90. return win;
  91. }
  92. #ifndef IGL_HEADER_ONLY
  93. // Explicit template instanciations
  94. template int igl::project<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> >&);
  95. template Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > igl::project<Eigen::Matrix<double, 3, 1, 0, 3, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&);
  96. template int igl::project<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> >&);
  97. template Eigen::PlainObjectBase<Eigen::Matrix<float, 3, 1, 0, 3, 1> > igl::project<Eigen::Matrix<float, 3, 1, 0, 3, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, 3, 1, 0, 3, 1> > const&);
  98. #endif
  99. #endif