unproject_in_mesh.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "unproject_in_mesh.h"
  2. #include "EmbreeIntersector.h"
  3. #include <igl/unproject.h>
  4. #include <vector>
  5. template <
  6. typename PointMatrixType,
  7. typename FaceMatrixType,
  8. typename RowVector3,
  9. typename Derivedobj>
  10. int igl::unproject_in_mesh(
  11. const int x,
  12. const int y,
  13. const igl::EmbreeIntersector<PointMatrixType,FaceMatrixType,RowVector3> & ei,
  14. Eigen::PlainObjectBase<Derivedobj> & obj)
  15. {
  16. std::vector<embree::Hit> hits;
  17. return igl::unproject_in_mesh(x,y,ei,obj,hits);
  18. }
  19. template <
  20. typename PointMatrixType,
  21. typename FaceMatrixType,
  22. typename RowVector3,
  23. typename Derivedobj>
  24. int igl::unproject_in_mesh(
  25. const int x,
  26. const int y,
  27. const igl::EmbreeIntersector<PointMatrixType,FaceMatrixType,RowVector3> & ei,
  28. Eigen::PlainObjectBase<Derivedobj> & obj,
  29. std::vector<embree::Hit > & hits)
  30. {
  31. using namespace igl;
  32. using namespace std;
  33. using namespace Eigen;
  34. // Source and direction on screen
  35. Vector3d win_s = Vector3d(x,y,0);
  36. Vector3d win_d(x,y,1);
  37. // Source, destination and direction in world
  38. Vector3d s,d,dir;
  39. unproject(win_s,s);
  40. unproject(win_d,d);
  41. dir = d-s;
  42. // Shoot ray, collect all hits (could just collect first two)
  43. int num_rays_shot;
  44. hits.clear();
  45. ei.intersectRay(s,dir,hits,num_rays_shot);
  46. switch(hits.size())
  47. {
  48. case 0:
  49. break;
  50. case 1:
  51. {
  52. obj = s + dir*hits[0].t;
  53. break;
  54. }
  55. case 2:
  56. default:
  57. {
  58. obj = 0.5*((s + dir*hits[0].t) + (s + dir*hits[1].t));
  59. break;
  60. }
  61. }
  62. return hits.size();
  63. }
  64. #ifndef IGL_HEADER_ONLY
  65. template int igl::unproject_in_mesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> >(int, int, igl::EmbreeIntersector<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> >&);
  66. #endif