sla_print_tests.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/SLA/Concurrency.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. sla::IndexedMesh 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("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("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("ElevatedSupportsDoNotPierceModel", "[SLASupportGeneration]") {
  109. sla::SupportTreeConfig supportcfg;
  110. for (auto fname : SUPPORT_TEST_MODELS)
  111. test_support_model_collision(fname, supportcfg);
  112. }
  113. TEST_CASE("FloorSupportsDoNotPierceModel", "[SLASupportGeneration]") {
  114. sla::SupportTreeConfig supportcfg;
  115. supportcfg.object_elevation_mm = 0;
  116. for (auto fname : SUPPORT_TEST_MODELS)
  117. test_support_model_collision(fname, supportcfg);
  118. }
  119. TEST_CASE("InitializedRasterShouldBeNONEmpty", "[SLARasterOutput]") {
  120. // Default Prusa SL1 display parameters
  121. sla::RasterBase::Resolution res{2560, 1440};
  122. sla::RasterBase::PixelDim pixdim{120. / res.width_px, 68. / res.height_px};
  123. sla::RasterGrayscaleAAGammaPower raster(res, pixdim, {}, 1.);
  124. REQUIRE(raster.resolution().width_px == res.width_px);
  125. REQUIRE(raster.resolution().height_px == res.height_px);
  126. REQUIRE(raster.pixel_dimensions().w_mm == Approx(pixdim.w_mm));
  127. REQUIRE(raster.pixel_dimensions().h_mm == Approx(pixdim.h_mm));
  128. }
  129. TEST_CASE("MirroringShouldBeCorrect", "[SLARasterOutput]") {
  130. sla::RasterBase::TMirroring mirrorings[] = {sla::RasterBase::NoMirror,
  131. sla::RasterBase::MirrorX,
  132. sla::RasterBase::MirrorY,
  133. sla::RasterBase::MirrorXY};
  134. sla::RasterBase::Orientation orientations[] =
  135. {sla::RasterBase::roLandscape, sla::RasterBase::roPortrait};
  136. for (auto orientation : orientations)
  137. for (auto &mirror : mirrorings)
  138. check_raster_transformations(orientation, mirror);
  139. }
  140. TEST_CASE("RasterizedPolygonAreaShouldMatch", "[SLARasterOutput]") {
  141. double disp_w = 120., disp_h = 68.;
  142. sla::RasterBase::Resolution res{2560, 1440};
  143. sla::RasterBase::PixelDim pixdim{disp_w / res.width_px, disp_h / res.height_px};
  144. double gamma = 1.;
  145. sla::RasterGrayscaleAAGammaPower raster(res, pixdim, {}, gamma);
  146. auto bb = BoundingBox({0, 0}, {scaled(disp_w), scaled(disp_h)});
  147. ExPolygon poly = square_with_hole(10.);
  148. poly.translate(bb.center().x(), bb.center().y());
  149. raster.draw(poly);
  150. double a = poly.area() / (scaled<double>(1.) * scaled(1.));
  151. double ra = raster_white_area(raster);
  152. double diff = std::abs(a - ra);
  153. REQUIRE(diff <= predict_error(poly, pixdim));
  154. raster.clear();
  155. poly = square_with_hole(60.);
  156. poly.translate(bb.center().x(), bb.center().y());
  157. raster.draw(poly);
  158. a = poly.area() / (scaled<double>(1.) * scaled(1.));
  159. ra = raster_white_area(raster);
  160. diff = std::abs(a - ra);
  161. REQUIRE(diff <= predict_error(poly, pixdim));
  162. sla::RasterGrayscaleAA raster0(res, pixdim, {}, [](double) { return 0.; });
  163. REQUIRE(raster_pxsum(raster0) == 0);
  164. raster0.draw(poly);
  165. ra = raster_white_area(raster);
  166. REQUIRE(raster_pxsum(raster0) == 0);
  167. }
  168. TEST_CASE("halfcone test", "[halfcone]") {
  169. sla::DiffBridge br{Vec3d{1., 1., 1.}, Vec3d{10., 10., 10.}, 0.25, 0.5};
  170. indexed_triangle_set m = sla::get_mesh(br, 45);
  171. its_merge_vertices(m);
  172. its_write_obj(m, "Halfcone.obj");
  173. }
  174. TEST_CASE("Test concurrency")
  175. {
  176. std::vector<double> vals = grid(0., 100., 10.);
  177. double ref = std::accumulate(vals.begin(), vals.end(), 0.);
  178. double s = execution::accumulate(ex_tbb, vals.begin(), vals.end(), 0.);
  179. REQUIRE(s == Approx(ref));
  180. }