unproject_in_mesh.cpp 1.6 KB

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