EmbreeIntersector.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <test_common.h>
  2. #include <igl/embree/EmbreeIntersector.h>
  3. TEST_CASE("EmbreeIntersector: cube", "[igl/embree]")
  4. {
  5. //The allowed error for this test
  6. const double epsilon = 1e-6;
  7. Eigen::MatrixXd V;
  8. Eigen::MatrixXi F;
  9. // This is a cube of dimensions 1.0x1.0x1.0
  10. igl::read_triangle_mesh(test_common::data_path("cube.obj"), V, F);
  11. // Initialize embree
  12. igl::embree::EmbreeIntersector embree;
  13. embree.init(V.cast<float>(),F.cast<int>());
  14. // These are not expected to be exact if the hit is on a vertex of edge.
  15. const int expected_id[] = {4,8,5,2,7,0};
  16. const float expected_u[] = {0.5,0.5,0.5,0.5,0.5,0.5};
  17. const float expected_v[] = {0.5,0.0,0.0,0.0,0.5,0.0};
  18. Eigen::MatrixXd hit_P(6,3);
  19. for (int dim=0; dim<6; ++dim)
  20. {
  21. hit_P.row(dim) =
  22. V.row(F(expected_id[dim],0))*(1.f - expected_u[dim] - expected_v[dim])+
  23. V.row(F(expected_id[dim],1))*expected_u[dim] +
  24. V.row(F(expected_id[dim],2))*expected_v[dim];
  25. }
  26. const auto test_hit = [&](const bool hitP, const igl::Hit& hit, const int dim)
  27. {
  28. CHECK(hitP);
  29. if(hitP)
  30. {
  31. const Eigen::RowVectorXd hit_p =
  32. V.row(F(hit.id,0))*(1.f - hit.u - hit.v) +
  33. V.row(F(hit.id,1))*hit.u +
  34. V.row(F(hit.id,2))*hit.v;
  35. // hits will be along diagonal edge so expected_id may be different
  36. test_common::assert_near(hit_P.row(dim),hit_p,epsilon);
  37. }
  38. };
  39. // Shoot ray from inside out
  40. for (int dim=0; dim<6; ++dim)
  41. {
  42. Eigen::Vector3f pos(0,0,0);
  43. Eigen::Vector3f dir(0,0,0);
  44. // test each dimension, pos and neg
  45. dir[dim/2] = dim%2 ? -1 : 1;
  46. igl::Hit hit;
  47. bool hitP = embree.intersectRay(pos, dir, hit);
  48. test_hit(hitP,hit,dim);
  49. }
  50. // Shoot ray from outside in
  51. for (int dim=0; dim<6; ++dim)
  52. {
  53. Eigen::Vector3f dir(0,0,0);
  54. // test each dimension, pos and neg
  55. dir[dim/2] = dim%2 ? 1 : -1;
  56. Eigen::Vector3f pos = -dir;
  57. igl::Hit hit;
  58. bool hitP = embree.intersectRay(pos, dir, hit);
  59. test_hit(hitP,hit,dim);
  60. }
  61. // Rays that miss
  62. for (int dim=0; dim<6; ++dim)
  63. {
  64. Eigen::Vector3f pos(0,0,0);
  65. Eigen::Vector3f dir(0,0,0);
  66. // test each dimension, pos and neg
  67. dir[dim/2] = dim%2 ? -1 : 1;
  68. pos[(dim/2+1)%3] = dir[dim/2];
  69. igl::Hit hit;
  70. bool hitP = embree.intersectRay(pos, dir, hit);
  71. CHECK_FALSE(hitP);
  72. }
  73. // intersect beam
  74. {
  75. Eigen::Vector3f pos(1.75,0.25,0);
  76. Eigen::Vector3f dir(-1,0,0);
  77. igl::Hit hit;
  78. bool hitP = embree.intersectBeam(pos, dir, hit);
  79. CHECK(hitP);
  80. REQUIRE(hit.t == Approx(1.25).margin(epsilon));
  81. REQUIRE(hit.id == 4);
  82. REQUIRE(hit.u == Approx(0.5).margin(epsilon));
  83. REQUIRE(hit.v == Approx(0.25).margin(epsilon));
  84. }
  85. }