unproject_in_mesh.cpp 1.4 KB

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