sla_supptgen_tests.cpp 4.3 KB

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