sla_print_tests.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include <unordered_set>
  2. #include <unordered_map>
  3. #include <random>
  4. #include <numeric>
  5. #include <cstdint>
  6. #include "sla_test_utils.hpp"
  7. #include <libslic3r/TriangleMeshSlicer.hpp>
  8. #include <libslic3r/SLA/SupportTreeMesher.hpp>
  9. #include <libslic3r/BranchingTree/PointCloud.hpp>
  10. namespace {
  11. const char *const BELOW_PAD_TEST_OBJECTS[] = {
  12. "20mm_cube.obj",
  13. "V.obj",
  14. };
  15. const char *const AROUND_PAD_TEST_OBJECTS[] = {
  16. "20mm_cube.obj",
  17. "V.obj",
  18. "frog_legs.obj",
  19. "cube_with_concave_hole_enlarged.obj",
  20. };
  21. const char *const SUPPORT_TEST_MODELS[] = {
  22. "cube_with_concave_hole_enlarged_standing.obj",
  23. "A_upsidedown.obj",
  24. "extruder_idler.obj"
  25. };
  26. } // namespace
  27. TEST_CASE("Pillar pairhash should be unique", "[SLASupportGeneration]") {
  28. test_pairhash<int, int>();
  29. test_pairhash<int, long>();
  30. test_pairhash<unsigned, unsigned>();
  31. test_pairhash<unsigned, unsigned long>();
  32. }
  33. TEST_CASE("Support point generator should be deterministic if seeded",
  34. "[SLASupportGeneration], [SLAPointGen]") {
  35. TriangleMesh mesh = load_model("A_upsidedown.obj");
  36. AABBMesh emesh{mesh};
  37. sla::SupportTreeConfig supportcfg;
  38. sla::SupportPointGenerator::Config autogencfg;
  39. autogencfg.head_diameter = float(2 * supportcfg.head_front_radius_mm);
  40. sla::SupportPointGenerator point_gen{emesh, autogencfg, [] {}, [](int) {}};
  41. auto bb = mesh.bounding_box();
  42. double zmin = bb.min.z();
  43. double zmax = bb.max.z();
  44. double gnd = zmin - supportcfg.object_elevation_mm;
  45. auto layer_h = 0.05f;
  46. auto slicegrid = grid(float(gnd), float(zmax), layer_h);
  47. std::vector<ExPolygons> slices = slice_mesh_ex(mesh.its, slicegrid, CLOSING_RADIUS);
  48. point_gen.seed(0);
  49. point_gen.execute(slices, slicegrid);
  50. auto get_chksum = [](const std::vector<sla::SupportPoint> &pts){
  51. int64_t chksum = 0;
  52. for (auto &pt : pts) {
  53. auto p = scaled(pt.pos);
  54. chksum += p.x() + p.y() + p.z();
  55. }
  56. return chksum;
  57. };
  58. int64_t checksum = get_chksum(point_gen.output());
  59. size_t ptnum = point_gen.output().size();
  60. REQUIRE(point_gen.output().size() > 0);
  61. for (int i = 0; i < 20; ++i) {
  62. point_gen.output().clear();
  63. point_gen.seed(0);
  64. point_gen.execute(slices, slicegrid);
  65. REQUIRE(point_gen.output().size() == ptnum);
  66. REQUIRE(checksum == get_chksum(point_gen.output()));
  67. }
  68. }
  69. TEST_CASE("Flat pad geometry is valid", "[SLASupportGeneration]") {
  70. sla::PadConfig padcfg;
  71. // Disable wings
  72. padcfg.wall_height_mm = .0;
  73. for (auto &fname : BELOW_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
  74. }
  75. TEST_CASE("WingedPadGeometryIsValid", "[SLASupportGeneration]") {
  76. sla::PadConfig padcfg;
  77. // Add some wings to the pad to test the cavity
  78. padcfg.wall_height_mm = 1.;
  79. for (auto &fname : BELOW_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
  80. }
  81. TEST_CASE("FlatPadAroundObjectIsValid", "[SLASupportGeneration]") {
  82. sla::PadConfig padcfg;
  83. // Add some wings to the pad to test the cavity
  84. padcfg.wall_height_mm = 0.;
  85. // padcfg.embed_object.stick_stride_mm = 0.;
  86. padcfg.embed_object.enabled = true;
  87. padcfg.embed_object.everywhere = true;
  88. for (auto &fname : AROUND_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
  89. }
  90. TEST_CASE("WingedPadAroundObjectIsValid", "[SLASupportGeneration]") {
  91. sla::PadConfig padcfg;
  92. // Add some wings to the pad to test the cavity
  93. padcfg.wall_height_mm = 1.;
  94. padcfg.embed_object.enabled = true;
  95. padcfg.embed_object.everywhere = true;
  96. for (auto &fname : AROUND_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
  97. }
  98. TEST_CASE("DefaultSupports::ElevatedSupportGeometryIsValid", "[SLASupportGeneration]") {
  99. sla::SupportTreeConfig supportcfg;
  100. supportcfg.object_elevation_mm = 10.;
  101. for (auto fname : SUPPORT_TEST_MODELS) test_supports(fname, supportcfg);
  102. }
  103. TEST_CASE("DefaultSupports::FloorSupportGeometryIsValid", "[SLASupportGeneration]") {
  104. sla::SupportTreeConfig supportcfg;
  105. supportcfg.object_elevation_mm = 0;
  106. for (auto &fname: SUPPORT_TEST_MODELS) test_supports(fname, supportcfg);
  107. }
  108. TEST_CASE("DefaultSupports::ElevatedSupportsDoNotPierceModel", "[SLASupportGeneration]") {
  109. sla::SupportTreeConfig supportcfg;
  110. supportcfg.object_elevation_mm = 10.;
  111. for (auto fname : SUPPORT_TEST_MODELS)
  112. test_support_model_collision(fname, supportcfg);
  113. }
  114. TEST_CASE("DefaultSupports::FloorSupportsDoNotPierceModel", "[SLASupportGeneration]") {
  115. sla::SupportTreeConfig supportcfg;
  116. supportcfg.object_elevation_mm = 0;
  117. for (auto fname : SUPPORT_TEST_MODELS)
  118. test_support_model_collision(fname, supportcfg);
  119. }
  120. //TEST_CASE("BranchingSupports::ElevatedSupportGeometryIsValid", "[SLASupportGeneration][Branching]") {
  121. // sla::SupportTreeConfig supportcfg;
  122. // supportcfg.object_elevation_mm = 10.;
  123. // supportcfg.tree_type = sla::SupportTreeType::Branching;
  124. // for (auto fname : SUPPORT_TEST_MODELS) test_supports(fname, supportcfg);
  125. //}
  126. //TEST_CASE("BranchingSupports::FloorSupportGeometryIsValid", "[SLASupportGeneration][Branching]") {
  127. // sla::SupportTreeConfig supportcfg;
  128. // supportcfg.object_elevation_mm = 0;
  129. // supportcfg.tree_type = sla::SupportTreeType::Branching;
  130. // for (auto &fname: SUPPORT_TEST_MODELS) test_supports(fname, supportcfg);
  131. //}
  132. bool is_outside_support_cone(const Vec3f &supp, const Vec3f &pt, float angle)
  133. {
  134. Vec3d D = (pt - supp).cast<double>();
  135. double dot_sq = -D.z() * std::abs(-D.z());
  136. return dot_sq <
  137. D.squaredNorm() * std::cos(angle) * std::abs(std::cos(angle));
  138. }
  139. TEST_CASE("BranchingSupports::MergePointFinder", "[SLASupportGeneration][Branching]") {
  140. SECTION("Identical points have the same merge point") {
  141. Vec3f a{0.f, 0.f, 0.f}, b = a;
  142. auto slope = float(PI / 4.);
  143. auto mergept = branchingtree::find_merge_pt(a, b, slope);
  144. REQUIRE(bool(mergept));
  145. REQUIRE((*mergept - b).norm() < EPSILON);
  146. REQUIRE((*mergept - a).norm() < EPSILON);
  147. }
  148. // ^ Z
  149. // | a *
  150. // |
  151. // | b * <= mergept
  152. SECTION("Points at different heights have the lower point as mergepoint") {
  153. Vec3f a{0.f, 0.f, 0.f}, b = {0.f, 0.f, -1.f};
  154. auto slope = float(PI / 4.);
  155. auto mergept = branchingtree::find_merge_pt(a, b, slope);
  156. REQUIRE(bool(mergept));
  157. REQUIRE((*mergept - b).squaredNorm() < 2 * EPSILON);
  158. }
  159. // -|---------> X
  160. // a b
  161. // * *
  162. // * <= mergept
  163. SECTION("Points at different X have mergept in the middle at lower Z") {
  164. Vec3f a{0.f, 0.f, 0.f}, b = {1.f, 0.f, 0.f};
  165. auto slope = float(PI / 4.);
  166. auto mergept = branchingtree::find_merge_pt(a, b, slope);
  167. REQUIRE(bool(mergept));
  168. // Distance of mergept should be equal from both input points
  169. float D = std::abs((*mergept - b).squaredNorm() - (*mergept - a).squaredNorm());
  170. REQUIRE(D < EPSILON);
  171. REQUIRE(!is_outside_support_cone(a, *mergept, slope));
  172. REQUIRE(!is_outside_support_cone(b, *mergept, slope));
  173. }
  174. // -|---------> Y
  175. // a b
  176. // * *
  177. // * <= mergept
  178. SECTION("Points at different Y have mergept in the middle at lower Z") {
  179. Vec3f a{0.f, 0.f, 0.f}, b = {0.f, 1.f, 0.f};
  180. auto slope = float(PI / 4.);
  181. auto mergept = branchingtree::find_merge_pt(a, b, slope);
  182. REQUIRE(bool(mergept));
  183. // Distance of mergept should be equal from both input points
  184. float D = std::abs((*mergept - b).squaredNorm() - (*mergept - a).squaredNorm());
  185. REQUIRE(D < EPSILON);
  186. REQUIRE(!is_outside_support_cone(a, *mergept, slope));
  187. REQUIRE(!is_outside_support_cone(b, *mergept, slope));
  188. }
  189. SECTION("Points separated by less than critical angle have the lower point as mergept") {
  190. Vec3f a{-1.f, -1.f, -1.f}, b = {-1.5f, -1.5f, -2.f};
  191. auto slope = float(PI / 4.);
  192. auto mergept = branchingtree::find_merge_pt(a, b, slope);
  193. REQUIRE(bool(mergept));
  194. REQUIRE((*mergept - b).norm() < 2 * EPSILON);
  195. }
  196. }
  197. TEST_CASE("BranchingSupports::ElevatedSupportsDoNotPierceModel", "[SLASupportGeneration][Branching]") {
  198. sla::SupportTreeConfig supportcfg;
  199. supportcfg.object_elevation_mm = 10.;
  200. supportcfg.tree_type = sla::SupportTreeType::Branching;
  201. for (auto fname : SUPPORT_TEST_MODELS)
  202. test_support_model_collision(fname, supportcfg);
  203. }
  204. TEST_CASE("BranchingSupports::FloorSupportsDoNotPierceModel", "[SLASupportGeneration][Branching]") {
  205. sla::SupportTreeConfig supportcfg;
  206. supportcfg.object_elevation_mm = 0;
  207. supportcfg.tree_type = sla::SupportTreeType::Branching;
  208. for (auto fname : SUPPORT_TEST_MODELS)
  209. test_support_model_collision(fname, supportcfg);
  210. }
  211. TEST_CASE("InitializedRasterShouldBeNONEmpty", "[SLARasterOutput]") {
  212. // Default Prusa SL1 display parameters
  213. sla::Resolution res{2560, 1440};
  214. sla::PixelDim pixdim{120. / res.width_px, 68. / res.height_px};
  215. sla::RasterGrayscaleAAGammaPower raster(res, pixdim, {}, 1.);
  216. REQUIRE(raster.resolution().width_px == res.width_px);
  217. REQUIRE(raster.resolution().height_px == res.height_px);
  218. REQUIRE(raster.pixel_dimensions().w_mm == Approx(pixdim.w_mm));
  219. REQUIRE(raster.pixel_dimensions().h_mm == Approx(pixdim.h_mm));
  220. }
  221. TEST_CASE("MirroringShouldBeCorrect", "[SLARasterOutput]") {
  222. sla::RasterBase::TMirroring mirrorings[] = {sla::RasterBase::NoMirror,
  223. sla::RasterBase::MirrorX,
  224. sla::RasterBase::MirrorY,
  225. sla::RasterBase::MirrorXY};
  226. sla::RasterBase::Orientation orientations[] =
  227. {sla::RasterBase::roLandscape, sla::RasterBase::roPortrait};
  228. for (auto orientation : orientations)
  229. for (auto &mirror : mirrorings)
  230. check_raster_transformations(orientation, mirror);
  231. }
  232. TEST_CASE("RasterizedPolygonAreaShouldMatch", "[SLARasterOutput]") {
  233. double disp_w = 120., disp_h = 68.;
  234. sla::Resolution res{2560, 1440};
  235. sla::PixelDim pixdim{disp_w / res.width_px, disp_h / res.height_px};
  236. double gamma = 1.;
  237. sla::RasterGrayscaleAAGammaPower raster(res, pixdim, {}, gamma);
  238. auto bb = BoundingBox({0, 0}, {scaled(disp_w), scaled(disp_h)});
  239. ExPolygon poly = square_with_hole(10.);
  240. poly.translate(bb.center().x(), bb.center().y());
  241. raster.draw(poly);
  242. double a = poly.area() / (scaled<double>(1.) * scaled(1.));
  243. double ra = raster_white_area(raster);
  244. double diff = std::abs(a - ra);
  245. REQUIRE(diff <= predict_error(poly, pixdim));
  246. raster.clear();
  247. poly = square_with_hole(60.);
  248. poly.translate(bb.center().x(), bb.center().y());
  249. raster.draw(poly);
  250. a = poly.area() / (scaled<double>(1.) * scaled(1.));
  251. ra = raster_white_area(raster);
  252. diff = std::abs(a - ra);
  253. REQUIRE(diff <= predict_error(poly, pixdim));
  254. sla::RasterGrayscaleAA raster0(res, pixdim, {}, [](double) { return 0.; });
  255. REQUIRE(raster_pxsum(raster0) == 0);
  256. raster0.draw(poly);
  257. ra = raster_white_area(raster);
  258. REQUIRE(raster_pxsum(raster0) == 0);
  259. }
  260. TEST_CASE("halfcone test", "[halfcone]") {
  261. sla::DiffBridge br{Vec3d{1., 1., 1.}, Vec3d{10., 10., 10.}, 0.25, 0.5};
  262. indexed_triangle_set m = sla::get_mesh(br, 45);
  263. its_merge_vertices(m);
  264. its_write_obj(m, "Halfcone.obj");
  265. }
  266. TEST_CASE("Test concurrency")
  267. {
  268. std::vector<double> vals = grid(0., 100., 10.);
  269. double ref = std::accumulate(vals.begin(), vals.end(), 0.);
  270. double s = execution::accumulate(ex_tbb, vals.begin(), vals.end(), 0.);
  271. REQUIRE(s == Approx(ref));
  272. }