unproject_in_mesh.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. bool 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. using namespace igl;
  17. using namespace std;
  18. using namespace Eigen;
  19. // Source and direction on screen
  20. Vector3d win_s = Vector3d(x,y,0);
  21. Vector3d win_d(x,y,1);
  22. // Source, destination and direction in world
  23. Vector3d s,d,dir;
  24. unproject(win_s,s);
  25. unproject(win_d,d);
  26. dir = d-s;
  27. // Shoot ray, collect all hits (could just collect first two)
  28. int num_rays_shot;
  29. vector<embree::Hit > hits;
  30. ei.intersectRay(s,dir,hits,num_rays_shot);
  31. switch(hits.size())
  32. {
  33. case 0:
  34. return false;
  35. case 1:
  36. {
  37. obj = s + dir*hits[0].t;
  38. break;
  39. }
  40. case 2:
  41. default:
  42. {
  43. obj = 0.5*((s + dir*hits[0].t) + (s + dir*hits[hits.size()-1].t));
  44. break;
  45. }
  46. }
  47. return true;
  48. }
  49. #ifndef IGL_HEADER_ONLY
  50. template bool 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> >&);
  51. #endif