sla_raycast_tests.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <catch2/catch.hpp>
  2. #include <test_utils.hpp>
  3. #include <libslic3r/SLA/IndexedMesh.hpp>
  4. #include <libslic3r/SLA/Hollowing.hpp>
  5. #include "sla_test_utils.hpp"
  6. using namespace Slic3r;
  7. // First do a simple test of the hole raycaster.
  8. TEST_CASE("Raycaster - find intersections of a line and cylinder")
  9. {
  10. sla::DrainHole hole{Vec3f(0,0,0), Vec3f(0,0,1), 5, 10};
  11. std::array<std::pair<float, Vec3d>, 2> out;
  12. Vec3f s;
  13. Vec3f dir;
  14. // Start inside the hole and cast perpendicular to its axis.
  15. s = {-1.f, 0, 5.f};
  16. dir = {1.f, 0, 0};
  17. hole.get_intersections(s, dir, out);
  18. REQUIRE(out[0].first == Approx(-4.f));
  19. REQUIRE(out[1].first == Approx(6.f));
  20. // Start outside and cast parallel to axis.
  21. s = {0, 0, -1.f};
  22. dir = {0, 0, 1.f};
  23. hole.get_intersections(s, dir, out);
  24. REQUIRE(std::abs(out[0].first - 1.f) < 0.001f);
  25. REQUIRE(std::abs(out[1].first - 11.f) < 0.001f);
  26. // Start outside and cast so that entry is in base and exit on the cylinder
  27. s = {0, -1.f, -1.f};
  28. dir = {0, 1.f, 1.f};
  29. dir.normalize();
  30. hole.get_intersections(s, dir, out);
  31. REQUIRE(std::abs(out[0].first - std::sqrt(2.f)) < 0.001f);
  32. REQUIRE(std::abs(out[1].first - std::sqrt(72.f)) < 0.001f);
  33. }
  34. #ifdef SLIC3R_HOLE_RAYCASTER
  35. // Create a simple scene with a 20mm cube and a big hole in the front wall
  36. // with 5mm radius. Then shoot rays from interesting positions and see where
  37. // they land.
  38. TEST_CASE("Raycaster with loaded drillholes", "[sla_raycast]")
  39. {
  40. // Load the cube and make it hollow.
  41. TriangleMesh cube = load_model("20mm_cube.obj");
  42. sla::HollowingConfig hcfg;
  43. std::unique_ptr<TriangleMesh> cube_inside = sla::generate_interior(cube, hcfg);
  44. REQUIRE(cube_inside);
  45. // Helper bb
  46. auto boxbb = cube.bounding_box();
  47. // Create the big 10mm long drainhole in the front wall.
  48. Vec3f center = boxbb.center().cast<float>();
  49. Vec3f p = {center.x(), 0., center.z()};
  50. Vec3f normal = {0.f, 1.f, 0.f};
  51. float radius = 5.f;
  52. float hole_length = 10.;
  53. sla::DrainHoles holes = { sla::DrainHole{p, normal, radius, hole_length} };
  54. cube.merge(*cube_inside);
  55. sla::IndexedMesh emesh{cube};
  56. emesh.load_holes(holes);
  57. Vec3d s = center.cast<double>();
  58. // Fire from center, should hit the interior wall
  59. auto hit = emesh.query_ray_hit(s, {0, 1., 0.});
  60. REQUIRE(hit.distance() == Approx(boxbb.size().x() / 2 - hcfg.min_thickness));
  61. // Fire upward from hole center, hit distance equals the radius (hits the
  62. // side of the hole cut.
  63. s.y() = hcfg.min_thickness / 2;
  64. hit = emesh.query_ray_hit(s, {0, 0., 1.});
  65. REQUIRE(hit.distance() == Approx(radius));
  66. // Fire from outside, hit the back side of the cube interior
  67. s.y() = -1.;
  68. hit = emesh.query_ray_hit(s, {0, 1., 0.});
  69. REQUIRE(hit.distance() == Approx(boxbb.max.y() - hcfg.min_thickness - s.y()));
  70. // Fire downwards from above the hole cylinder. Has to go through the cyl.
  71. // as it was not there.
  72. s = center.cast<double>();
  73. s.z() = boxbb.max.z() - hcfg.min_thickness - 1.;
  74. hit = emesh.query_ray_hit(s, {0, 0., -1.});
  75. REQUIRE(hit.distance() == Approx(s.z() - boxbb.min.z() - hcfg.min_thickness));
  76. // Check for support tree correctness
  77. test_support_model_collision("20mm_cube.obj", {}, hcfg, holes);
  78. }
  79. #endif