sla_raycast_tests.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. cube.require_shared_vertices();
  56. sla::IndexedMesh emesh{cube};
  57. emesh.load_holes(holes);
  58. Vec3d s = center.cast<double>();
  59. // Fire from center, should hit the interior wall
  60. auto hit = emesh.query_ray_hit(s, {0, 1., 0.});
  61. REQUIRE(hit.distance() == Approx(boxbb.size().x() / 2 - hcfg.min_thickness));
  62. // Fire upward from hole center, hit distance equals the radius (hits the
  63. // side of the hole cut.
  64. s.y() = hcfg.min_thickness / 2;
  65. hit = emesh.query_ray_hit(s, {0, 0., 1.});
  66. REQUIRE(hit.distance() == Approx(radius));
  67. // Fire from outside, hit the back side of the cube interior
  68. s.y() = -1.;
  69. hit = emesh.query_ray_hit(s, {0, 1., 0.});
  70. REQUIRE(hit.distance() == Approx(boxbb.max.y() - hcfg.min_thickness - s.y()));
  71. // Fire downwards from above the hole cylinder. Has to go through the cyl.
  72. // as it was not there.
  73. s = center.cast<double>();
  74. s.z() = boxbb.max.z() - hcfg.min_thickness - 1.;
  75. hit = emesh.query_ray_hit(s, {0, 0., -1.});
  76. REQUIRE(hit.distance() == Approx(s.z() - boxbb.min.z() - hcfg.min_thickness));
  77. // Check for support tree correctness
  78. test_support_model_collision("20mm_cube.obj", {}, hcfg, holes);
  79. }
  80. #endif