sla_print_tests.cpp 7.8 KB

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