project.cpp 4.8 KB

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