project.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef IGL_NO_OPENGL
  10. #include <iostream>
  11. #include "report_gl_error.h"
  12. #include "OpenGL_convenience.h"
  13. IGL_INLINE int igl::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::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::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::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::project(
  77. const Eigen::PlainObjectBase<Derivedobj> & obj,
  78. Eigen::PlainObjectBase<Derivedwin> & win)
  79. {
  80. Eigen::Vector3d dobj(obj(0),obj(1),obj(2));
  81. Eigen::Vector3d dwin;
  82. int ret = project(dobj(0),dobj(1),dobj(2),
  83. &dwin.data()[0],
  84. &dwin.data()[1],
  85. &dwin.data()[2]);
  86. win(0) = dwin(0);
  87. win(1) = dwin(1);
  88. win(2) = dwin(2);
  89. return ret;
  90. }
  91. template <typename Derivedobj>
  92. IGL_INLINE Eigen::PlainObjectBase<Derivedobj> igl::project(
  93. const Eigen::PlainObjectBase<Derivedobj> & obj)
  94. {
  95. Eigen::PlainObjectBase<Derivedobj> win;
  96. project(obj,win);
  97. return win;
  98. }
  99. #ifndef IGL_HEADER_ONLY
  100. // Explicit template instanciations
  101. 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> >&);
  102. 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&);
  103. 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> >&);
  104. 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&);
  105. template Eigen::PlainObjectBase<Eigen::Matrix<double, 1, -1, 1, 1, -1> > igl::project<Eigen::Matrix<double, 1, -1, 1, 1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 1, -1, 1, 1, -1> > const&);
  106. template int igl::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> >&);
  107. #endif
  108. #endif