project.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef IGL_OPENGL_4
  11. #include <iostream>
  12. #include "report_gl_error.h"
  13. #include "OpenGL_convenience.h"
  14. IGL_INLINE int igl::project(
  15. const double objX,
  16. const double objY,
  17. const double objZ,
  18. double* winX,
  19. double* winY,
  20. double* winZ)
  21. {
  22. using namespace std;
  23. #ifdef EXTREME_VERBOSE
  24. cout<<"project();"<<endl;
  25. #endif
  26. // Put model, projection, and viewport matrices into double arrays
  27. double MV[16];
  28. double P[16];
  29. int VP[4];
  30. glGetDoublev(GL_MODELVIEW_MATRIX, MV);
  31. #ifdef EXTREME_VERBOSE
  32. cout<<"MV=["<<endl<<
  33. MV[0]<<" "<< MV[1]<<" "<< MV[2]<<" "<< MV[3]<<" "<<endl<<
  34. MV[4]<<" "<< MV[5]<<" "<< MV[6]<<" "<< MV[7]<<" "<<endl<<
  35. MV[8]<<" "<< MV[9]<<" "<< MV[10]<<" "<< MV[11]<<" "<<endl<<
  36. MV[12]<<" "<< MV[13]<<" "<< MV[14]<<" "<< MV[15]<<" "<<endl<<
  37. "];"<<endl;
  38. #endif
  39. #ifndef NDEBUG
  40. igl::report_gl_error();
  41. #endif
  42. glGetDoublev(GL_PROJECTION_MATRIX, P);
  43. #ifdef EXTREME_VERBOSE
  44. cout<<"P=["<<endl<<
  45. P[0]<<" "<< P[1]<<" "<< P[2]<<" "<< P[3]<<" "<<endl<<
  46. P[4]<<" "<< P[5]<<" "<< P[6]<<" "<< P[7]<<" "<<endl<<
  47. P[8]<<" "<< P[9]<<" "<< P[10]<<" "<< P[11]<<" "<<endl<<
  48. P[12]<<" "<< P[13]<<" "<< P[14]<<" "<< P[15]<<" "<<endl<<
  49. "];"<<endl;
  50. #endif
  51. #ifndef NDEBUG
  52. igl::report_gl_error();
  53. #endif
  54. glGetIntegerv(GL_VIEWPORT, VP);
  55. #ifdef EXTREME_VERBOSE
  56. cout<<"VP=["<<endl<<
  57. VP[0]<<" "<< VP[1]<<" "<< VP[2]<<" "<< VP[3]<<" "<<endl<<
  58. "];"<<endl;
  59. #endif
  60. #ifndef NDEBUG
  61. igl::report_gl_error();
  62. #endif
  63. #ifdef EXTREME_VERBOSE
  64. cout<<"obj=["<<endl<<
  65. objX<<" "<< objY<<" "<< objZ<<endl<<
  66. "];"<<endl;
  67. #endif
  68. int ret = gluProject(objX,objY,objZ,MV,P,VP,winX,winY,winZ);
  69. #ifdef EXTREME_VERBOSE
  70. cout<<"win=["<<endl<<
  71. *winX<<" "<< *winY<<" "<< *winZ<<endl<<
  72. "];"<<endl;
  73. #endif
  74. return ret;
  75. }
  76. template <typename Derivedobj, typename Derivedwin>
  77. IGL_INLINE int igl::project(
  78. const Eigen::PlainObjectBase<Derivedobj> & obj,
  79. Eigen::PlainObjectBase<Derivedwin> & win)
  80. {
  81. Eigen::Vector3d dobj(obj(0),obj(1),obj(2));
  82. Eigen::Vector3d dwin;
  83. int ret = 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::project(
  94. const Eigen::PlainObjectBase<Derivedobj> & obj)
  95. {
  96. Eigen::PlainObjectBase<Derivedobj> win;
  97. project(obj,win);
  98. return win;
  99. }
  100. #endif
  101. #endif
  102. Eigen::Vector3f igl::project(const Eigen::Vector3f& obj,
  103. const Eigen::Matrix4f& model,
  104. const Eigen::Matrix4f& proj,
  105. const Eigen::Vector4f& viewport)
  106. {
  107. Eigen::Vector4f tmp;
  108. tmp << obj,1;
  109. tmp = model * tmp;
  110. tmp = proj * tmp;
  111. tmp = tmp.array() / tmp(3);
  112. tmp = tmp.array() * 0.5f + 0.5f;
  113. tmp(0) = tmp(0) * viewport(2) + viewport(0);
  114. tmp(1) = tmp(1) * viewport(3) + viewport(1);
  115. return tmp.head(3);
  116. }
  117. #ifdef IGL_STATIC_LIBRARY
  118. #ifndef IGL_NO_OPENGL
  119. #ifndef IGL_OPENGL_4
  120. // Explicit template instanciations
  121. 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> >&);
  122. 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&);
  123. 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> >&);
  124. 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&);
  125. 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&);
  126. 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> >&);
  127. template Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > igl::project<Eigen::Matrix<double, 1, 3, 1, 1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&);
  128. #endif
  129. #endif
  130. #endif