sla_print_tests.cpp 7.8 KB

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