EmbreeIntersector.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. test_common::load_mesh("cube.obj", V, F);
  11. // Initialize embree
  12. igl::embree::EmbreeIntersector embree;
  13. embree.init(V.cast<float>(),F.cast<int>());
  14. const int expected_id[] = {4,8,5,2,7,0};
  15. const float expected_u[] = {0.5,0.5,0.5,0.5,0.5,0.5};
  16. const float expected_v[] = {0.5,0.0,0.0,0.0,0.5,0.0};
  17. // Shoot ray from inside out
  18. for (int dim=0; dim<6; ++dim)
  19. {
  20. Eigen::Vector3f pos(0,0,0);
  21. Eigen::Vector3f dir(0,0,0);
  22. // test each dimension, pos and neg
  23. dir[dim/2] = dim%2 ? -1 : 1;
  24. igl::Hit hit;
  25. bool hitP = embree.intersectRay(pos, dir, hit);
  26. CHECK(hitP);
  27. REQUIRE(hit.t == Approx(0.5).margin(epsilon));
  28. REQUIRE(hit.id == expected_id[dim]);
  29. REQUIRE(hit.u == Approx(expected_u[dim]).margin(epsilon));
  30. REQUIRE(hit.v == Approx(expected_v[dim]).margin(epsilon));
  31. }
  32. // Shoot ray from outside in
  33. for (int dim=0; dim<6; ++dim)
  34. {
  35. Eigen::Vector3f dir(0,0,0);
  36. // test each dimension, pos and neg
  37. dir[dim/2] = dim%2 ? 1 : -1;
  38. Eigen::Vector3f pos = -dir;
  39. igl::Hit hit;
  40. bool hitP = embree.intersectRay(pos, dir, hit);
  41. CHECK(hitP);
  42. REQUIRE(hit.t == Approx(0.5).margin(epsilon));
  43. REQUIRE(hit.id == expected_id[dim]);
  44. REQUIRE(hit.u == Approx(expected_u[dim]).margin(epsilon));
  45. REQUIRE(hit.v == Approx(expected_v[dim]).margin(epsilon));
  46. }
  47. // Rays that miss
  48. for (int dim=0; dim<6; ++dim)
  49. {
  50. Eigen::Vector3f pos(0,0,0);
  51. Eigen::Vector3f dir(0,0,0);
  52. // test each dimension, pos and neg
  53. dir[dim/2] = dim%2 ? -1 : 1;
  54. pos[(dim/2+1)%3] = dir[dim/2];
  55. igl::Hit hit;
  56. bool hitP = embree.intersectRay(pos, dir, hit);
  57. CHECK_FALSE(hitP);
  58. }
  59. // intersect beam
  60. {
  61. Eigen::Vector3f pos(-0.5,-0.5,1);
  62. Eigen::Vector3f dir(0,0,-1);
  63. igl::Hit hit;
  64. bool hitP = embree.intersectBeam(pos, dir, hit);
  65. CHECK(hitP);
  66. REQUIRE(hit.t == Approx(0.5).margin(epsilon));
  67. REQUIRE(hit.id == 7);
  68. REQUIRE(hit.u == Approx(0).margin(epsilon));
  69. REQUIRE(hit.v == Approx(1).margin(epsilon));
  70. }
  71. {
  72. Eigen::Vector3f pos(0.5,-1,0.5);
  73. Eigen::Vector3f dir(0,1,0);
  74. igl::Hit hit;
  75. bool hitP = embree.intersectBeam(pos, dir, hit);
  76. CHECK(hitP);
  77. REQUIRE(hit.t == Approx(0.5).margin(epsilon));
  78. REQUIRE(hit.id == 2);
  79. REQUIRE(hit.u == Approx(0).margin(epsilon));
  80. REQUIRE(hit.v == Approx(0).margin(epsilon));
  81. }
  82. }