test_aabbindirect.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. auto tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set(tmesh.its.vertices, tmesh.its.indices);
  10. REQUIRE(! tree.empty());
  11. igl::Hit hit;
  12. bool intersected = AABBTreeIndirect::intersect_ray_first_hit(
  13. tmesh.its.vertices, tmesh.its.indices,
  14. tree,
  15. Vec3d(0.5, 0.5, -5.),
  16. Vec3d(0., 0., 1.),
  17. hit);
  18. REQUIRE(intersected);
  19. REQUIRE(hit.t == Approx(5.));
  20. std::vector<igl::Hit> hits;
  21. bool intersected2 = AABBTreeIndirect::intersect_ray_all_hits(
  22. tmesh.its.vertices, tmesh.its.indices,
  23. tree,
  24. Vec3d(0.3, 0.5, -5.),
  25. Vec3d(0., 0., 1.),
  26. hits);
  27. REQUIRE(intersected2);
  28. REQUIRE(hits.size() == 2);
  29. REQUIRE(hits.front().t == Approx(5.));
  30. REQUIRE(hits.back().t == Approx(6.));
  31. size_t hit_idx;
  32. Vec3d closest_point;
  33. double squared_distance = AABBTreeIndirect::squared_distance_to_indexed_triangle_set(
  34. tmesh.its.vertices, tmesh.its.indices,
  35. tree,
  36. Vec3d(0.3, 0.5, -5.),
  37. hit_idx, closest_point);
  38. REQUIRE(squared_distance == Approx(5. * 5.));
  39. REQUIRE(closest_point.x() == Approx(0.3));
  40. REQUIRE(closest_point.y() == Approx(0.5));
  41. REQUIRE(closest_point.z() == Approx(0.));
  42. squared_distance = AABBTreeIndirect::squared_distance_to_indexed_triangle_set(
  43. tmesh.its.vertices, tmesh.its.indices,
  44. tree,
  45. Vec3d(0.3, 0.5, 5.),
  46. hit_idx, closest_point);
  47. REQUIRE(squared_distance == Approx(4. * 4.));
  48. REQUIRE(closest_point.x() == Approx(0.3));
  49. REQUIRE(closest_point.y() == Approx(0.5));
  50. REQUIRE(closest_point.z() == Approx(1.));
  51. }