project.cpp 5.8 KB

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