sla_supptreeutils_tests.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #include <catch2/catch.hpp>
  2. #include <test_utils.hpp>
  3. #include <unordered_set>
  4. #include "libslic3r/Execution/ExecutionSeq.hpp"
  5. #include "libslic3r/SLA/SupportTreeUtils.hpp"
  6. #include "libslic3r/SLA/SupportTreeUtilsLegacy.hpp"
  7. // Test pair hash for 'nums' random number pairs.
  8. template <class I, class II> void test_pairhash()
  9. {
  10. const constexpr size_t nums = 1000;
  11. I A[nums] = {0}, B[nums] = {0};
  12. std::unordered_set<I> CH;
  13. std::unordered_map<II, std::pair<I, I>> ints;
  14. std::random_device rd;
  15. std::mt19937 gen(rd());
  16. const I Ibits = int(sizeof(I) * CHAR_BIT);
  17. const II IIbits = int(sizeof(II) * CHAR_BIT);
  18. int bits = IIbits / 2 < Ibits ? Ibits / 2 : Ibits;
  19. if (std::is_signed<I>::value) bits -= 1;
  20. const I Imin = 0;
  21. const I Imax = I(std::pow(2., bits) - 1);
  22. std::uniform_int_distribution<I> dis(Imin, Imax);
  23. for (size_t i = 0; i < nums;) {
  24. I a = dis(gen);
  25. if (CH.find(a) == CH.end()) { CH.insert(a); A[i] = a; ++i; }
  26. }
  27. for (size_t i = 0; i < nums;) {
  28. I b = dis(gen);
  29. if (CH.find(b) == CH.end()) { CH.insert(b); B[i] = b; ++i; }
  30. }
  31. for (size_t i = 0; i < nums; ++i) {
  32. I a = A[i], b = B[i];
  33. REQUIRE(a != b);
  34. II hash_ab = Slic3r::sla::pairhash<I, II>(a, b);
  35. II hash_ba = Slic3r::sla::pairhash<I, II>(b, a);
  36. REQUIRE(hash_ab == hash_ba);
  37. auto it = ints.find(hash_ab);
  38. if (it != ints.end()) {
  39. REQUIRE((
  40. (it->second.first == a && it->second.second == b) ||
  41. (it->second.first == b && it->second.second == a)
  42. ));
  43. } else
  44. ints[hash_ab] = std::make_pair(a, b);
  45. }
  46. }
  47. TEST_CASE("Pillar pairhash should be unique", "[suptreeutils]") {
  48. test_pairhash<int, int>();
  49. test_pairhash<int, long>();
  50. test_pairhash<unsigned, unsigned>();
  51. test_pairhash<unsigned, unsigned long>();
  52. }
  53. static void eval_ground_conn(const Slic3r::sla::GroundConnection &conn,
  54. const Slic3r::sla::SupportableMesh &sm,
  55. const Slic3r::sla::Junction &j,
  56. double end_r,
  57. const std::string &stl_fname = "output.stl")
  58. {
  59. using namespace Slic3r;
  60. //#ifndef NDEBUG
  61. sla::SupportTreeBuilder builder;
  62. if (!conn)
  63. builder.add_junction(j);
  64. sla::build_ground_connection(builder, sm, conn);
  65. indexed_triangle_set mesh = *sm.emesh.get_triangle_mesh();
  66. its_merge(mesh, builder.merged_mesh());
  67. its_write_stl_ascii(stl_fname.c_str(), "stl_fname", mesh);
  68. //#endif
  69. REQUIRE(bool(conn));
  70. // The route should include the source and one avoidance junction.
  71. REQUIRE(conn.path.size() == 2);
  72. // Check if the radius increases with each node
  73. REQUIRE(conn.path.front().r < conn.path.back().r);
  74. REQUIRE(conn.path.back().r < conn.pillar_base->r_top);
  75. // The end radius and the pillar base's upper radius should match
  76. REQUIRE(conn.pillar_base->r_top == Approx(end_r));
  77. }
  78. TEST_CASE("Pillar search dumb case", "[suptreeutils]") {
  79. using namespace Slic3r;
  80. constexpr double FromR = 0.5;
  81. auto j = sla::Junction{Vec3d::Zero(), FromR};
  82. SECTION("with empty mesh") {
  83. sla::SupportableMesh sm{indexed_triangle_set{},
  84. sla::SupportPoints{},
  85. sla::SupportTreeConfig{}};
  86. constexpr double EndR = 1.;
  87. sla::GroundConnection conn =
  88. sla::deepsearch_ground_connection(ex_seq, sm, j, EndR, sla::DOWN);
  89. REQUIRE(conn);
  90. // REQUIRE(conn.path.size() == 1);
  91. REQUIRE(conn.pillar_base->pos.z() == Approx(ground_level(sm)));
  92. }
  93. SECTION("with zero R source and destination") {
  94. sla::SupportableMesh sm{indexed_triangle_set{},
  95. sla::SupportPoints{},
  96. sla::SupportTreeConfig{}};
  97. j.r = 0.;
  98. constexpr double EndR = 0.;
  99. sla::GroundConnection conn =
  100. sla::deepsearch_ground_connection(ex_seq, sm, j, EndR, sla::DOWN);
  101. REQUIRE(conn);
  102. // REQUIRE(conn.path.size() == 1);
  103. REQUIRE(conn.pillar_base->pos.z() == Approx(ground_level(sm)));
  104. REQUIRE(conn.pillar_base->r_top == Approx(0.));
  105. }
  106. SECTION("with zero init direction") {
  107. sla::SupportableMesh sm{indexed_triangle_set{},
  108. sla::SupportPoints{},
  109. sla::SupportTreeConfig{}};
  110. constexpr double EndR = 1.;
  111. Vec3d init_dir = Vec3d::Zero();
  112. sla::GroundConnection conn =
  113. sla::deepsearch_ground_connection(ex_seq, sm, j, EndR, init_dir);
  114. REQUIRE(conn);
  115. // REQUIRE(conn.path.size() == 1);
  116. REQUIRE(conn.pillar_base->pos.z() == Approx(ground_level(sm)));
  117. }
  118. }
  119. TEST_CASE("Avoid disk below junction", "[suptreeutils]")
  120. {
  121. // In this test there will be a disk mesh with some radius, centered at
  122. // (0, 0, 0) and above the disk, a junction from which the support pillar
  123. // should be routed. The algorithm needs to find an avoidance route.
  124. using namespace Slic3r;
  125. constexpr double FromRadius = .5;
  126. constexpr double EndRadius = 1.;
  127. constexpr double CylRadius = 4.;
  128. constexpr double CylHeight = 1.;
  129. sla::SupportTreeConfig cfg;
  130. indexed_triangle_set disk = its_make_cylinder(CylRadius, CylHeight);
  131. // 2.5 * CyRadius height should be enough to be able to insert a bridge
  132. // with 45 degree tilt above the disk.
  133. sla::Junction j{Vec3d{0., 0., 2.5 * CylRadius}, FromRadius};
  134. sla::SupportableMesh sm{disk, sla::SupportPoints{}, cfg};
  135. SECTION("with elevation") {
  136. sla::GroundConnection conn =
  137. sla::deepsearch_ground_connection(ex_tbb, sm, j, EndRadius, sla::DOWN);
  138. eval_ground_conn(conn, sm, j, EndRadius, "disk.stl");
  139. // Check if the avoidance junction is indeed outside of the disk barrier's
  140. // edge.
  141. auto p = conn.path.back().pos;
  142. double pR = std::sqrt(p.x() * p.x()) + std::sqrt(p.y() * p.y());
  143. REQUIRE(pR + FromRadius > CylRadius);
  144. }
  145. SECTION("without elevation") {
  146. sm.cfg.object_elevation_mm = 0.;
  147. sla::GroundConnection conn =
  148. sla::deepsearch_ground_connection(ex_tbb, sm, j, EndRadius, sla::DOWN);
  149. eval_ground_conn(conn, sm, j, EndRadius, "disk_ze.stl");
  150. // Check if the avoidance junction is indeed outside of the disk barrier's
  151. // edge.
  152. auto p = conn.path.back().pos;
  153. double pR = std::sqrt(p.x() * p.x()) + std::sqrt(p.y() * p.y());
  154. REQUIRE(pR + FromRadius > CylRadius);
  155. }
  156. }
  157. TEST_CASE("Avoid disk below junction with barrier on the side", "[suptreeutils]")
  158. {
  159. // In this test there will be a disk mesh with some radius, centered at
  160. // (0, 0, 0) and above the disk, a junction from which the support pillar
  161. // should be routed. The algorithm needs to find an avoidance route.
  162. using namespace Slic3r;
  163. constexpr double FromRadius = .5;
  164. constexpr double EndRadius = 1.;
  165. constexpr double CylRadius = 4.;
  166. constexpr double CylHeight = 1.;
  167. constexpr double JElevX = 2.5;
  168. sla::SupportTreeConfig cfg;
  169. indexed_triangle_set disk = its_make_cylinder(CylRadius, CylHeight);
  170. indexed_triangle_set wall = its_make_cube(1., 2 * CylRadius, JElevX * CylRadius);
  171. its_translate(wall, Vec3f{float(FromRadius), -float(CylRadius), 0.f});
  172. its_merge(disk, wall);
  173. // 2.5 * CyRadius height should be enough to be able to insert a bridge
  174. // with 45 degree tilt above the disk.
  175. sla::Junction j{Vec3d{0., 0., JElevX * CylRadius}, FromRadius};
  176. sla::SupportableMesh sm{disk, sla::SupportPoints{}, cfg};
  177. SECTION("with elevation") {
  178. sla::GroundConnection conn =
  179. sla::deepsearch_ground_connection(ex_seq, sm, j, EndRadius, sla::DOWN);
  180. eval_ground_conn(conn, sm, j, EndRadius, "disk_with_barrier.stl");
  181. // Check if the avoidance junction is indeed outside of the disk barrier's
  182. // edge.
  183. auto p = conn.path.back().pos;
  184. double pR = std::sqrt(p.x() * p.x()) + std::sqrt(p.y() * p.y());
  185. REQUIRE(pR + FromRadius > CylRadius);
  186. }
  187. SECTION("without elevation") {
  188. sm.cfg.object_elevation_mm = 0.;
  189. sla::GroundConnection conn =
  190. sla::deepsearch_ground_connection(ex_seq, sm, j, EndRadius, sla::DOWN);
  191. eval_ground_conn(conn, sm, j, EndRadius, "disk_with_barrier_ze.stl");
  192. // Check if the avoidance junction is indeed outside of the disk barrier's
  193. // edge.
  194. auto p = conn.path.back().pos;
  195. double pR = std::sqrt(p.x() * p.x()) + std::sqrt(p.y() * p.y());
  196. REQUIRE(pR + FromRadius > CylRadius);
  197. }
  198. }
  199. TEST_CASE("Find ground route just above ground", "[suptreeutils]") {
  200. using namespace Slic3r;
  201. sla::SupportTreeConfig cfg;
  202. cfg.object_elevation_mm = 0.;
  203. sla::Junction j{Vec3d{0., 0., 2. * cfg.head_back_radius_mm}, cfg.head_back_radius_mm};
  204. sla::SupportableMesh sm{{}, sla::SupportPoints{}, cfg};
  205. sla::GroundConnection conn =
  206. sla::deepsearch_ground_connection(ex_seq, sm, j, Geometry::spheric_to_dir(3 * PI/ 4, PI));
  207. REQUIRE(conn);
  208. REQUIRE(conn.pillar_base->pos.z() >= Approx(ground_level(sm)));
  209. }
  210. TEST_CASE("BranchingSupports::MergePointFinder", "[suptreeutils]") {
  211. using namespace Slic3r;
  212. SECTION("Identical points have the same merge point") {
  213. Vec3f a{0.f, 0.f, 0.f}, b = a;
  214. auto slope = float(PI / 4.);
  215. auto mergept = sla::find_merge_pt(a, b, slope);
  216. REQUIRE(bool(mergept));
  217. REQUIRE((*mergept - b).norm() < EPSILON);
  218. REQUIRE((*mergept - a).norm() < EPSILON);
  219. }
  220. // ^ Z
  221. // | a *
  222. // |
  223. // | b * <= mergept
  224. SECTION("Points at different heights have the lower point as mergepoint") {
  225. Vec3f a{0.f, 0.f, 0.f}, b = {0.f, 0.f, -1.f};
  226. auto slope = float(PI / 4.);
  227. auto mergept = sla::find_merge_pt(a, b, slope);
  228. REQUIRE(bool(mergept));
  229. REQUIRE((*mergept - b).squaredNorm() < 2 * EPSILON);
  230. }
  231. // -|---------> X
  232. // a b
  233. // * *
  234. // * <= mergept
  235. SECTION("Points at different X have mergept in the middle at lower Z") {
  236. Vec3f a{0.f, 0.f, 0.f}, b = {1.f, 0.f, 0.f};
  237. auto slope = float(PI / 4.);
  238. auto mergept = sla::find_merge_pt(a, b, slope);
  239. REQUIRE(bool(mergept));
  240. // Distance of mergept should be equal from both input points
  241. float D = std::abs((*mergept - b).squaredNorm() - (*mergept - a).squaredNorm());
  242. REQUIRE(D < EPSILON);
  243. REQUIRE(!sla::is_outside_support_cone(a, *mergept, slope));
  244. REQUIRE(!sla::is_outside_support_cone(b, *mergept, slope));
  245. }
  246. // -|---------> Y
  247. // a b
  248. // * *
  249. // * <= mergept
  250. SECTION("Points at different Y have mergept in the middle at lower Z") {
  251. Vec3f a{0.f, 0.f, 0.f}, b = {0.f, 1.f, 0.f};
  252. auto slope = float(PI / 4.);
  253. auto mergept = sla::find_merge_pt(a, b, slope);
  254. REQUIRE(bool(mergept));
  255. // Distance of mergept should be equal from both input points
  256. float D = std::abs((*mergept - b).squaredNorm() - (*mergept - a).squaredNorm());
  257. REQUIRE(D < EPSILON);
  258. REQUIRE(!sla::is_outside_support_cone(a, *mergept, slope));
  259. REQUIRE(!sla::is_outside_support_cone(b, *mergept, slope));
  260. }
  261. SECTION("Points separated by less than critical angle have the lower point as mergept") {
  262. Vec3f a{-1.f, -1.f, -1.f}, b = {-1.5f, -1.5f, -2.f};
  263. auto slope = float(PI / 4.);
  264. auto mergept = sla::find_merge_pt(a, b, slope);
  265. REQUIRE(bool(mergept));
  266. REQUIRE((*mergept - b).norm() < 2 * EPSILON);
  267. }
  268. // -|----------------------------> Y
  269. // a b
  270. // * * <= mergept *
  271. //
  272. SECTION("Points at same height have mergepoint in the middle if critical angle is zero ") {
  273. Vec3f a{-1.f, -1.f, -1.f}, b = {-1.5f, -1.5f, -1.f};
  274. auto slope = EPSILON;
  275. auto mergept = sla::find_merge_pt(a, b, slope);
  276. REQUIRE(bool(mergept));
  277. Vec3f middle = (b + a) / 2.;
  278. REQUIRE((*mergept - middle).norm() < 4 * EPSILON);
  279. }
  280. }