sla_supptgen_tests.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <catch2/catch.hpp>
  2. #include <test_utils.hpp>
  3. #include <libslic3r/ExPolygon.hpp>
  4. #include <libslic3r/BoundingBox.hpp>
  5. #include <libslic3r/SLA/SpatIndex.hpp>
  6. #include "sla_test_utils.hpp"
  7. namespace Slic3r { namespace sla {
  8. TEST_CASE("Overhanging point should be supported", "[SupGen]") {
  9. // Pyramid with 45 deg slope
  10. TriangleMesh mesh = make_pyramid(10.f, 10.f);
  11. mesh.rotate_y(float(PI));
  12. mesh.WriteOBJFile("Pyramid.obj");
  13. sla::SupportPoints pts = calc_support_pts(mesh);
  14. // The overhang, which is the upside-down pyramid's edge
  15. Vec3f overh{0., 0., -10.};
  16. REQUIRE(!pts.empty());
  17. float dist = (overh - pts.front().pos).norm();
  18. for (const auto &pt : pts)
  19. dist = std::min(dist, (overh - pt.pos).norm());
  20. // Should require exactly one support point at the overhang
  21. REQUIRE(pts.size() > 0);
  22. REQUIRE(dist < 1.f);
  23. }
  24. double min_point_distance(const sla::SupportPoints &pts)
  25. {
  26. sla::PointIndex index;
  27. for (size_t i = 0; i < pts.size(); ++i)
  28. index.insert(pts[i].pos.cast<double>(), i);
  29. auto d = std::numeric_limits<double>::max();
  30. index.foreach([&d, &index](const sla::PointIndexEl &el) {
  31. auto res = index.nearest(el.first, 2);
  32. for (const sla::PointIndexEl &r : res)
  33. if (r.second != el.second)
  34. d = std::min(d, (el.first - r.first).norm());
  35. });
  36. return d;
  37. }
  38. TEST_CASE("Overhanging horizontal surface should be supported", "[SupGen]") {
  39. double width = 10., depth = 10., height = 1.;
  40. TriangleMesh mesh = make_cube(width, depth, height);
  41. mesh.translate(0., 0., 5.); // lift up
  42. mesh.WriteOBJFile("Cuboid.obj");
  43. sla::SupportPointGenerator::Config cfg;
  44. sla::SupportPoints pts = calc_support_pts(mesh, cfg);
  45. double mm2 = width * depth;
  46. REQUIRE(!pts.empty());
  47. REQUIRE(pts.size() * cfg.support_force() > mm2 * cfg.tear_pressure());
  48. REQUIRE(min_point_distance(pts) >= cfg.minimal_distance);
  49. }
  50. template<class M> auto&& center_around_bb(M &&mesh)
  51. {
  52. auto bb = mesh.bounding_box();
  53. mesh.translate(-bb.center().template cast<float>());
  54. return std::forward<M>(mesh);
  55. }
  56. TEST_CASE("Overhanging edge should be supported", "[SupGen]") {
  57. float width = 10.f, depth = 10.f, height = 5.f;
  58. TriangleMesh mesh = make_prism(width, depth, height);
  59. mesh.rotate_y(float(PI)); // rotate on its back
  60. mesh.translate(0., 0., height);
  61. mesh.WriteOBJFile("Prism.obj");
  62. sla::SupportPointGenerator::Config cfg;
  63. sla::SupportPoints pts = calc_support_pts(mesh, cfg);
  64. Linef3 overh{ {0.f, -depth / 2.f, 0.f}, {0.f, depth / 2.f, 0.f}};
  65. // Get all the points closer that 1 mm to the overhanging edge:
  66. sla::SupportPoints overh_pts; overh_pts.reserve(pts.size());
  67. std::copy_if(pts.begin(), pts.end(), std::back_inserter(overh_pts),
  68. [&overh](const sla::SupportPoint &pt){
  69. return line_alg::distance_to(overh, Vec3d{pt.pos.cast<double>()}) < 1.;
  70. });
  71. REQUIRE(overh_pts.size() * cfg.support_force() > overh.length() * cfg.tear_pressure());
  72. double ddiff = min_point_distance(pts) - cfg.minimal_distance;
  73. REQUIRE(ddiff > - 0.1 * cfg.minimal_distance);
  74. }
  75. TEST_CASE("Hollowed cube should be supported from the inside", "[SupGen][Hollowed]") {
  76. TriangleMesh mesh = make_cube(20., 20., 20.);
  77. hollow_mesh(mesh, HollowingConfig{});
  78. mesh.WriteOBJFile("cube_hollowed.obj");
  79. auto bb = mesh.bounding_box();
  80. auto h = float(bb.max.z() - bb.min.z());
  81. Vec3f mv = bb.center().cast<float>() - Vec3f{0.f, 0.f, 0.5f * h};
  82. mesh.translate(-mv);
  83. sla::SupportPointGenerator::Config cfg;
  84. sla::SupportPoints pts = calc_support_pts(mesh, cfg);
  85. sla::remove_bottom_points(pts, mesh.bounding_box().min.z() + EPSILON);
  86. REQUIRE(!pts.empty());
  87. }
  88. TEST_CASE("Two parallel plates should be supported", "[SupGen][Hollowed]")
  89. {
  90. double width = 20., depth = 20., height = 1.;
  91. TriangleMesh mesh = center_around_bb(make_cube(width + 5., depth + 5., height));
  92. TriangleMesh mesh_high = center_around_bb(make_cube(width, depth, height));
  93. mesh_high.translate(0., 0., 10.); // lift up
  94. mesh.merge(mesh_high);
  95. mesh.WriteOBJFile("parallel_plates.obj");
  96. sla::SupportPointGenerator::Config cfg;
  97. sla::SupportPoints pts = calc_support_pts(mesh, cfg);
  98. sla::remove_bottom_points(pts, mesh.bounding_box().min.z() + EPSILON);
  99. REQUIRE(!pts.empty());
  100. }
  101. }} // namespace Slic3r::sla