sla_print_tests.cpp 7.0 KB

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