ray_mesh_intersect.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <test_common.h>
  2. #include <igl/ray_mesh_intersect.h>
  3. #include <igl/ray_box_intersect.h>
  4. #include <igl/AABB.h>
  5. TEST_CASE("ray_mesh_intersect: one_triangle", "[igl]")
  6. {
  7. IGL_PUSH_FPE;
  8. Eigen::MatrixXd V(3,3);
  9. V.row(0) << 0.0, 0.0, 0.0;
  10. V.row(1) << 1.0, 0.0, 0.0;
  11. V.row(2) << 0.5, 1.0, 0.0;
  12. Eigen::MatrixXi F(1,3);
  13. F.row(0) << 0,1,2;
  14. Eigen::Vector3f source{0.5, 0.5, -1.0};
  15. Eigen::Vector3f direction{0.0, 0.0, 1.0};
  16. igl::Hit hit;
  17. REQUIRE(igl::ray_mesh_intersect(source, direction, V, F, hit) == true);
  18. REQUIRE(hit.t == Approx(1.0));
  19. std::vector<igl::Hit> hits;
  20. REQUIRE(igl::ray_mesh_intersect(source, direction, V, F, hits) == true);
  21. REQUIRE(hits.size() == 1);
  22. REQUIRE(hits.front().t == Approx(1.0));
  23. IGL_POP_FPE;
  24. }
  25. // https://github.com/libigl/libigl/issues/2363
  26. #ifdef _WIN32
  27. #define IGL_MAYFAIL_WIN "[!mayfail]"
  28. #else
  29. #define IGL_MAYFAIL_WIN
  30. #endif
  31. TEST_CASE("ray_mesh_intersect: corner-case", "[igl]" IGL_MAYFAIL_WIN)
  32. {
  33. IGL_PUSH_FPE;
  34. // . //
  35. // /|\ //
  36. // / | \ //
  37. // / | \ //
  38. // / | \ //
  39. // .----x----. //
  40. // \ | / //
  41. // \ | / //
  42. // \ | / //
  43. // \|/ //
  44. // . //
  45. Eigen::MatrixXf vertices(5, 3);
  46. vertices <<
  47. -1., 0., 0.,
  48. 0., 1., 0.,
  49. 1., 0., 0.,
  50. 0., -1., 0.,
  51. 0., 0., 0.;
  52. Eigen::MatrixXi faces(4, 3);
  53. faces <<
  54. 0, 1, 4,
  55. 1, 2, 4,
  56. 2, 3, 4,
  57. 3, 0, 4;
  58. igl::AABB<Eigen::MatrixXf, 3> mesh_bvh;
  59. mesh_bvh.init(vertices, faces);
  60. for (float eps: {1e-5f, 0.f})
  61. {
  62. Eigen::Vector3f origin(eps, eps, 1.f + eps);
  63. Eigen::Vector3f direction(0.f, 0.f, -1.f);
  64. std::vector<igl::Hit> hits, hits_bvh;
  65. bool is_hit = igl::ray_mesh_intersect(origin, direction, vertices, faces, hits);
  66. bool is_hit_bvh = mesh_bvh.intersect_ray(vertices, faces, origin, direction, hits_bvh);
  67. REQUIRE (is_hit);
  68. REQUIRE (is_hit == is_hit_bvh);
  69. REQUIRE (hits.size() == hits_bvh.size());
  70. }
  71. IGL_POP_FPE;
  72. }
  73. TEST_CASE("ray_mesh_intersect: corner-case2", "[igl]")
  74. {
  75. IGL_PUSH_FPE;
  76. Eigen::MatrixXf vertices(3, 3);
  77. vertices <<
  78. -2.891303300857544, 0.7025225162506104, 1.157850384712219,
  79. -2.870383024215698, 0.7444183230400085, 1.18663215637207,
  80. -2.890183448791504, 0.7462523579597473, 1.157822966575623;
  81. Eigen::MatrixXi faces(1, 3);
  82. faces <<
  83. 0, 2, 1;
  84. Eigen::Vector3f origin;
  85. Eigen::Vector3f direction;
  86. origin << -5.411622047424316, -0.02165498770773411, 0.7916983366012573;
  87. direction << 0.9475222229957581, 0.2885690927505493, 0.1375846415758133;
  88. std::vector<igl::Hit> hits, hits_bvh;
  89. bool is_hit = igl::ray_mesh_intersect(origin, direction, vertices, faces, hits);
  90. Eigen::AlignedBox3f box;
  91. box.extend(vertices.row(0).transpose());
  92. box.extend(vertices.row(1).transpose());
  93. box.extend(vertices.row(2).transpose());
  94. float tmin, tmax;
  95. bool is_hit_box = igl::ray_box_intersect(origin, direction, box, 0.0f, std::numeric_limits<float>::max(), tmin, tmax);
  96. REQUIRE (is_hit == is_hit_box);
  97. }