sla_print_tests.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include <unordered_map>
  2. #include <random>
  3. #include <numeric>
  4. #include <cstdint>
  5. #include "sla_test_utils.hpp"
  6. #include <libslic3r/TriangleMeshSlicer.hpp>
  7. #include <libslic3r/SLA/SupportTreeMesher.hpp>
  8. #include <libslic3r/BranchingTree/PointCloud.hpp>
  9. namespace {
  10. const char *const BELOW_PAD_TEST_OBJECTS[] = {
  11. "20mm_cube.obj",
  12. "V.obj",
  13. };
  14. const char *const AROUND_PAD_TEST_OBJECTS[] = {
  15. "20mm_cube.obj",
  16. "V.obj",
  17. "frog_legs.obj",
  18. "cube_with_concave_hole_enlarged.obj",
  19. };
  20. const char *const SUPPORT_TEST_MODELS[] = {
  21. "cube_with_concave_hole_enlarged_standing.obj",
  22. "A_upsidedown.obj",
  23. "extruder_idler.obj"
  24. };
  25. } // namespace
  26. TEST_CASE("Support point generator should be deterministic if seeded",
  27. "[SLASupportGeneration], [SLAPointGen]") {
  28. TriangleMesh mesh = load_model("A_upsidedown.obj");
  29. AABBMesh emesh{mesh};
  30. sla::SupportTreeConfig supportcfg;
  31. sla::SupportPointGenerator::Config autogencfg;
  32. autogencfg.head_diameter = float(2 * supportcfg.head_front_radius_mm);
  33. sla::SupportPointGenerator point_gen{emesh, autogencfg, [] {}, [](int) {}};
  34. auto bb = mesh.bounding_box();
  35. double zmin = bb.min.z();
  36. double zmax = bb.max.z();
  37. double gnd = zmin - supportcfg.object_elevation_mm;
  38. auto layer_h = 0.05f;
  39. auto slicegrid = grid(float(gnd), float(zmax), layer_h);
  40. std::vector<ExPolygons> slices = slice_mesh_ex(mesh.its, slicegrid, CLOSING_RADIUS);
  41. point_gen.seed(0);
  42. point_gen.execute(slices, slicegrid);
  43. auto get_chksum = [](const std::vector<sla::SupportPoint> &pts){
  44. int64_t chksum = 0;
  45. for (auto &pt : pts) {
  46. auto p = scaled(pt.pos);
  47. chksum += p.x() + p.y() + p.z();
  48. }
  49. return chksum;
  50. };
  51. int64_t checksum = get_chksum(point_gen.output());
  52. size_t ptnum = point_gen.output().size();
  53. REQUIRE(point_gen.output().size() > 0);
  54. for (int i = 0; i < 20; ++i) {
  55. point_gen.output().clear();
  56. point_gen.seed(0);
  57. point_gen.execute(slices, slicegrid);
  58. REQUIRE(point_gen.output().size() == ptnum);
  59. REQUIRE(checksum == get_chksum(point_gen.output()));
  60. }
  61. }
  62. TEST_CASE("Flat pad geometry is valid", "[SLASupportGeneration]") {
  63. sla::PadConfig padcfg;
  64. // Disable wings
  65. padcfg.wall_height_mm = .0;
  66. for (auto &fname : BELOW_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
  67. }
  68. TEST_CASE("WingedPadGeometryIsValid", "[SLASupportGeneration]") {
  69. sla::PadConfig padcfg;
  70. // Add some wings to the pad to test the cavity
  71. padcfg.wall_height_mm = 1.;
  72. for (auto &fname : BELOW_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
  73. }
  74. TEST_CASE("FlatPadAroundObjectIsValid", "[SLASupportGeneration]") {
  75. sla::PadConfig padcfg;
  76. // Add some wings to the pad to test the cavity
  77. padcfg.wall_height_mm = 0.;
  78. // padcfg.embed_object.stick_stride_mm = 0.;
  79. padcfg.embed_object.enabled = true;
  80. padcfg.embed_object.everywhere = true;
  81. for (auto &fname : AROUND_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
  82. }
  83. TEST_CASE("WingedPadAroundObjectIsValid", "[SLASupportGeneration]") {
  84. sla::PadConfig padcfg;
  85. // Add some wings to the pad to test the cavity
  86. padcfg.wall_height_mm = 1.;
  87. padcfg.embed_object.enabled = true;
  88. padcfg.embed_object.everywhere = true;
  89. for (auto &fname : AROUND_PAD_TEST_OBJECTS) test_pad(fname, padcfg);
  90. }
  91. TEST_CASE("DefaultSupports::ElevatedSupportGeometryIsValid", "[SLASupportGeneration]") {
  92. sla::SupportTreeConfig supportcfg;
  93. supportcfg.object_elevation_mm = 10.;
  94. for (auto fname : SUPPORT_TEST_MODELS) test_supports(fname, supportcfg);
  95. }
  96. TEST_CASE("DefaultSupports::FloorSupportGeometryIsValid", "[SLASupportGeneration]") {
  97. sla::SupportTreeConfig supportcfg;
  98. supportcfg.object_elevation_mm = 0;
  99. for (auto &fname: SUPPORT_TEST_MODELS) test_supports(fname, supportcfg);
  100. }
  101. TEST_CASE("DefaultSupports::ElevatedSupportsDoNotPierceModel", "[SLASupportGeneration]") {
  102. sla::SupportTreeConfig supportcfg;
  103. supportcfg.object_elevation_mm = 10.;
  104. for (auto fname : SUPPORT_TEST_MODELS)
  105. test_support_model_collision(fname, supportcfg);
  106. }
  107. TEST_CASE("DefaultSupports::FloorSupportsDoNotPierceModel", "[SLASupportGeneration]") {
  108. sla::SupportTreeConfig supportcfg;
  109. supportcfg.object_elevation_mm = 0;
  110. for (auto fname : SUPPORT_TEST_MODELS)
  111. test_support_model_collision(fname, supportcfg);
  112. }
  113. //TEST_CASE("BranchingSupports::ElevatedSupportGeometryIsValid", "[SLASupportGeneration][Branching]") {
  114. // sla::SupportTreeConfig supportcfg;
  115. // supportcfg.object_elevation_mm = 10.;
  116. // supportcfg.tree_type = sla::SupportTreeType::Branching;
  117. // for (auto fname : SUPPORT_TEST_MODELS) test_supports(fname, supportcfg);
  118. //}
  119. //TEST_CASE("BranchingSupports::FloorSupportGeometryIsValid", "[SLASupportGeneration][Branching]") {
  120. // sla::SupportTreeConfig supportcfg;
  121. // supportcfg.object_elevation_mm = 0;
  122. // supportcfg.tree_type = sla::SupportTreeType::Branching;
  123. // for (auto &fname: SUPPORT_TEST_MODELS) test_supports(fname, supportcfg);
  124. //}
  125. TEST_CASE("BranchingSupports::ElevatedSupportsDoNotPierceModel", "[SLASupportGeneration][Branching]") {
  126. sla::SupportTreeConfig supportcfg;
  127. supportcfg.object_elevation_mm = 10.;
  128. supportcfg.tree_type = sla::SupportTreeType::Branching;
  129. for (auto fname : SUPPORT_TEST_MODELS)
  130. test_support_model_collision(fname, supportcfg);
  131. }
  132. TEST_CASE("BranchingSupports::FloorSupportsDoNotPierceModel", "[SLASupportGeneration][Branching]") {
  133. sla::SupportTreeConfig supportcfg;
  134. supportcfg.object_elevation_mm = 0;
  135. supportcfg.tree_type = sla::SupportTreeType::Branching;
  136. for (auto fname : SUPPORT_TEST_MODELS)
  137. test_support_model_collision(fname, supportcfg);
  138. }
  139. TEST_CASE("InitializedRasterShouldBeNONEmpty", "[SLARasterOutput]") {
  140. // Default Prusa SL1 display parameters
  141. sla::Resolution res{2560, 1440};
  142. sla::PixelDim pixdim{120. / res.width_px, 68. / res.height_px};
  143. sla::RasterGrayscaleAAGammaPower raster(res, pixdim, {}, 1.);
  144. REQUIRE(raster.resolution().width_px == res.width_px);
  145. REQUIRE(raster.resolution().height_px == res.height_px);
  146. REQUIRE(raster.pixel_dimensions().w_mm == Approx(pixdim.w_mm));
  147. REQUIRE(raster.pixel_dimensions().h_mm == Approx(pixdim.h_mm));
  148. }
  149. TEST_CASE("MirroringShouldBeCorrect", "[SLARasterOutput]") {
  150. sla::RasterBase::TMirroring mirrorings[] = {sla::RasterBase::NoMirror,
  151. sla::RasterBase::MirrorX,
  152. sla::RasterBase::MirrorY,
  153. sla::RasterBase::MirrorXY};
  154. sla::RasterBase::Orientation orientations[] =
  155. {sla::RasterBase::roLandscape, sla::RasterBase::roPortrait};
  156. for (auto orientation : orientations)
  157. for (auto &mirror : mirrorings)
  158. check_raster_transformations(orientation, mirror);
  159. }
  160. TEST_CASE("RasterizedPolygonAreaShouldMatch", "[SLARasterOutput]") {
  161. double disp_w = 120., disp_h = 68.;
  162. sla::Resolution res{2560, 1440};
  163. sla::PixelDim pixdim{disp_w / res.width_px, disp_h / res.height_px};
  164. double gamma = 1.;
  165. sla::RasterGrayscaleAAGammaPower raster(res, pixdim, {}, gamma);
  166. auto bb = BoundingBox({0, 0}, {scaled(disp_w), scaled(disp_h)});
  167. ExPolygon poly = square_with_hole(10.);
  168. poly.translate(bb.center().x(), bb.center().y());
  169. raster.draw(poly);
  170. double a = poly.area() / (scaled<double>(1.) * scaled(1.));
  171. double ra = raster_white_area(raster);
  172. double diff = std::abs(a - ra);
  173. REQUIRE(diff <= predict_error(poly, pixdim));
  174. raster.clear();
  175. poly = square_with_hole(60.);
  176. poly.translate(bb.center().x(), bb.center().y());
  177. raster.draw(poly);
  178. a = poly.area() / (scaled<double>(1.) * scaled(1.));
  179. ra = raster_white_area(raster);
  180. diff = std::abs(a - ra);
  181. REQUIRE(diff <= predict_error(poly, pixdim));
  182. sla::RasterGrayscaleAA raster0(res, pixdim, {}, [](double) { return 0.; });
  183. REQUIRE(raster_pxsum(raster0) == 0);
  184. raster0.draw(poly);
  185. ra = raster_white_area(raster);
  186. REQUIRE(raster_pxsum(raster0) == 0);
  187. }
  188. TEST_CASE("halfcone test", "[halfcone]") {
  189. sla::DiffBridge br{Vec3d{1., 1., 1.}, Vec3d{10., 10., 10.}, 0.25, 0.5};
  190. indexed_triangle_set m = sla::get_mesh(br, 45);
  191. its_merge_vertices(m);
  192. its_write_obj(m, "Halfcone.obj");
  193. }
  194. TEST_CASE("Test concurrency")
  195. {
  196. std::vector<double> vals = grid(0., 100., 10.);
  197. double ref = std::accumulate(vals.begin(), vals.end(), 0.);
  198. double s = execution::accumulate(ex_tbb, vals.begin(), vals.end(), 0.);
  199. REQUIRE(s == Approx(ref));
  200. }