ray_mesh_intersect.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <test_common.h>
  2. #include <igl/ray_mesh_intersect.h>
  3. #include <igl/AABB.h>
  4. TEST_CASE("ray_mesh_intersect: one_triangle", "[igl]")
  5. {
  6. Eigen::MatrixXd V(3,3);
  7. V.row(0) << 0.0, 0.0, 0.0;
  8. V.row(1) << 1.0, 0.0, 0.0;
  9. V.row(2) << 0.5, 1.0, 0.0;
  10. Eigen::MatrixXi F(1,3);
  11. F.row(0) << 0,1,2;
  12. Eigen::Vector3f source{0.5, 0.5, -1.0};
  13. Eigen::Vector3f direction{0.0, 0.0, 1.0};
  14. igl::Hit hit;
  15. REQUIRE(igl::ray_mesh_intersect(source, direction, V, F, hit) == true);
  16. REQUIRE(hit.t == Approx(1.0));
  17. std::vector<igl::Hit> hits;
  18. REQUIRE(igl::ray_mesh_intersect(source, direction, V, F, hits) == true);
  19. REQUIRE(hits.size() == 1);
  20. REQUIRE(hits.front().t == Approx(1.0));
  21. }
  22. TEST_CASE("ray_mesh_intersect: corner-case", "[igl]")
  23. {
  24. // . //
  25. // /|\ //
  26. // / | \ //
  27. // / | \ //
  28. // / | \ //
  29. // .----x----. //
  30. // \ | / //
  31. // \ | / //
  32. // \ | / //
  33. // \|/ //
  34. // . //
  35. Eigen::MatrixXf vertices(5, 3);
  36. vertices <<
  37. -1., 0., 0.,
  38. 0., 1., 0.,
  39. 1., 0., 0.,
  40. 0., -1., 0.,
  41. 0., 0., 0.;
  42. Eigen::MatrixXi faces(4, 3);
  43. faces <<
  44. 0, 1, 4,
  45. 1, 2, 4,
  46. 2, 3, 4,
  47. 3, 0, 4;
  48. igl::AABB<Eigen::MatrixXf, 3> mesh_bvh;
  49. mesh_bvh.init(vertices, faces);
  50. for (float eps: {1e-5f, 0.f})
  51. {
  52. Eigen::Vector3f origin(eps, eps, 1.f + eps);
  53. Eigen::Vector3f direction(0.f, 0.f, -1.f);
  54. std::vector<igl::Hit> hits, hits_bvh;
  55. bool is_hit = igl::ray_mesh_intersect(origin, direction, vertices, faces, hits);
  56. bool is_hit_bvh = mesh_bvh.intersect_ray(vertices, faces, origin, direction, hits_bvh);
  57. REQUIRE (is_hit);
  58. REQUIRE (is_hit == is_hit_bvh);
  59. REQUIRE (hits.size() == hits_bvh.size());
  60. }
  61. }