test_aabbindirect.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <catch2/catch.hpp>
  2. #include <test_utils.hpp>
  3. #include <libslic3r/TriangleMesh.hpp>
  4. #include <libslic3r/AABBTreeIndirect.hpp>
  5. using namespace Slic3r;
  6. TEST_CASE("Building a tree over a box, ray caster and closest query", "[AABBIndirect]")
  7. {
  8. TriangleMesh tmesh = make_cube(1., 1., 1.);
  9. tmesh.repair();
  10. auto tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set(tmesh.its.vertices, tmesh.its.indices);
  11. REQUIRE(! tree.empty());
  12. igl::Hit hit;
  13. bool intersected = AABBTreeIndirect::intersect_ray_first_hit(
  14. tmesh.its.vertices, tmesh.its.indices,
  15. tree,
  16. Vec3d(0.5, 0.5, -5.),
  17. Vec3d(0., 0., 1.),
  18. hit);
  19. REQUIRE(intersected);
  20. REQUIRE(hit.t == Approx(5.));
  21. std::vector<igl::Hit> hits;
  22. bool intersected2 = AABBTreeIndirect::intersect_ray_all_hits(
  23. tmesh.its.vertices, tmesh.its.indices,
  24. tree,
  25. Vec3d(0.3, 0.5, -5.),
  26. Vec3d(0., 0., 1.),
  27. hits);
  28. REQUIRE(intersected2);
  29. REQUIRE(hits.size() == 2);
  30. REQUIRE(hits.front().t == Approx(5.));
  31. REQUIRE(hits.back().t == Approx(6.));
  32. size_t hit_idx;
  33. Vec3d closest_point;
  34. double squared_distance = AABBTreeIndirect::squared_distance_to_indexed_triangle_set(
  35. tmesh.its.vertices, tmesh.its.indices,
  36. tree,
  37. Vec3d(0.3, 0.5, -5.),
  38. hit_idx, closest_point);
  39. REQUIRE(squared_distance == Approx(5. * 5.));
  40. REQUIRE(closest_point.x() == Approx(0.3));
  41. REQUIRE(closest_point.y() == Approx(0.5));
  42. REQUIRE(closest_point.z() == Approx(0.));
  43. squared_distance = AABBTreeIndirect::squared_distance_to_indexed_triangle_set(
  44. tmesh.its.vertices, tmesh.its.indices,
  45. tree,
  46. Vec3d(0.3, 0.5, 5.),
  47. hit_idx, closest_point);
  48. REQUIRE(squared_distance == Approx(4. * 4.));
  49. REQUIRE(closest_point.x() == Approx(0.3));
  50. REQUIRE(closest_point.y() == Approx(0.5));
  51. REQUIRE(closest_point.z() == Approx(1.));
  52. }