test_trianglemesh.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/TriangleMesh.hpp"
  3. #include "libslic3r/TriangleMeshSlicer.hpp"
  4. #include "libslic3r/Point.hpp"
  5. #include "libslic3r/Config.hpp"
  6. #include "libslic3r/Model.hpp"
  7. #include "libslic3r/libslic3r.h"
  8. #include <algorithm>
  9. #include <future>
  10. #include <chrono>
  11. //#include "test_options.hpp"
  12. #include "test_data.hpp"
  13. using namespace Slic3r;
  14. using namespace std;
  15. static inline TriangleMesh make_cube() { return make_cube(20., 20, 20); }
  16. SCENARIO( "TriangleMesh: Basic mesh statistics") {
  17. GIVEN( "A 20mm cube, built from constexpr std::array" ) {
  18. std::vector<Vec3f> vertices { {20,20,0}, {20,0,0}, {0,0,0}, {0,20,0}, {20,20,20}, {0,20,20}, {0,0,20}, {20,0,20} };
  19. std::vector<Vec3i32> facets { {0,1,2}, {0,2,3}, {4,5,6}, {4,6,7}, {0,4,7}, {0,7,1}, {1,7,6}, {1,6,2}, {2,6,5}, {2,5,3}, {4,0,3}, {4,3,5} };
  20. TriangleMesh cube(vertices, facets);
  21. THEN( "Volume is appropriate for 20mm square cube.") {
  22. REQUIRE(abs(cube.volume() - 20.0*20.0*20.0) < 1e-2);
  23. }
  24. THEN( "Vertices array matches input.") {
  25. for (size_t i = 0U; i < cube.its.vertices.size(); i++) {
  26. REQUIRE(cube.its.vertices.at(i) == vertices.at(i).cast<float>());
  27. }
  28. for (size_t i = 0U; i < vertices.size(); i++) {
  29. REQUIRE(vertices.at(i).cast<float>() == cube.its.vertices.at(i));
  30. }
  31. }
  32. THEN( "Vertex count matches vertex array size.") {
  33. REQUIRE(cube.facets_count() == facets.size());
  34. }
  35. THEN( "Facet array matches input.") {
  36. for (size_t i = 0U; i < cube.its.indices.size(); i++) {
  37. REQUIRE(cube.its.indices.at(i) == facets.at(i));
  38. }
  39. for (size_t i = 0U; i < facets.size(); i++) {
  40. REQUIRE(facets.at(i) == cube.its.indices.at(i));
  41. }
  42. }
  43. THEN( "Facet count matches facet array size.") {
  44. REQUIRE(cube.facets_count() == facets.size());
  45. }
  46. #if 0
  47. THEN( "Number of normals is equal to the number of facets.") {
  48. REQUIRE(cube.normals().size() == facets.size());
  49. }
  50. #endif
  51. THEN( "center() returns the center of the object.") {
  52. REQUIRE(cube.center() == Vec3d(10.0,10.0,10.0));
  53. }
  54. THEN( "Size of cube is (20,20,20)") {
  55. REQUIRE(cube.size() == Vec3d(20,20,20));
  56. }
  57. }
  58. GIVEN( "A 20mm cube with one corner on the origin") {
  59. const std::vector<Vec3d> vertices { {20,20,0}, {20,0,0}, {0,0,0}, {0,20,0}, {20,20,20}, {0,20,20}, {0,0,20}, {20,0,20} };
  60. const std::vector<Vec3i32> facets { {0,1,2}, {0,2,3}, {4,5,6}, {4,6,7}, {0,4,7}, {0,7,1}, {1,7,6}, {1,6,2}, {2,6,5}, {2,5,3}, {4,0,3}, {4,3,5} };
  61. TriangleMesh cube(vertices, facets);
  62. cube.repair();
  63. THEN( "Volume is appropriate for 20mm square cube.") {
  64. REQUIRE(abs(cube.volume() - 20.0*20.0*20.0) < 1e-2);
  65. }
  66. THEN( "Vertices array matches input.") {
  67. for (size_t i = 0U; i < cube.its.vertices.size(); i++) {
  68. REQUIRE(cube.its.vertices.at(i) == vertices.at(i).cast<float>());
  69. }
  70. for (size_t i = 0U; i < vertices.size(); i++) {
  71. REQUIRE(vertices.at(i).cast<float>() == cube.its.vertices.at(i));
  72. }
  73. }
  74. THEN( "Vertex count matches vertex array size.") {
  75. REQUIRE(cube.facets_count() == facets.size());
  76. }
  77. THEN( "Facet array matches input.") {
  78. for (size_t i = 0U; i < cube.its.indices.size(); i++) {
  79. REQUIRE(cube.its.indices.at(i) == facets.at(i));
  80. }
  81. for (size_t i = 0U; i < facets.size(); i++) {
  82. REQUIRE(facets.at(i) == cube.its.indices.at(i));
  83. }
  84. }
  85. THEN( "Facet count matches facet array size.") {
  86. REQUIRE(cube.facets_count() == facets.size());
  87. }
  88. #if 0
  89. THEN( "Number of normals is equal to the number of facets.") {
  90. REQUIRE(cube.normals().size() == facets.size());
  91. }
  92. #endif
  93. THEN( "center() returns the center of the object.") {
  94. REQUIRE(cube.center() == Vec3d(10.0,10.0,10.0));
  95. }
  96. THEN( "Size of cube is (20,20,20)") {
  97. REQUIRE(cube.size() == Vec3d(20,20,20));
  98. }
  99. }
  100. }
  101. SCENARIO( "TriangleMesh: Transformation functions affect mesh as expected.") {
  102. GIVEN( "A 20mm cube with one corner on the origin") {
  103. auto cube = make_cube();
  104. WHEN( "The cube is scaled 200% uniformly") {
  105. cube.scale(2.0);
  106. THEN( "The volume is equivalent to 40x40x40 (all dimensions increased by 200%") {
  107. REQUIRE(abs(cube.volume() - 40.0*40.0*40.0) < 1e-2);
  108. }
  109. }
  110. WHEN( "The resulting cube is scaled 200% in the X direction") {
  111. cube.scale(Vec3f(2.0, 1, 1));
  112. THEN( "The volume is doubled.") {
  113. REQUIRE(abs(cube.volume() - 2*20.0*20.0*20.0) < 1e-2);
  114. }
  115. THEN( "The X coordinate size is 200%.") {
  116. REQUIRE(cube.its.vertices.at(0).x() == 40.0);
  117. }
  118. }
  119. WHEN( "The cube is scaled 25% in the X direction") {
  120. cube.scale(Vec3f(0.25, 1, 1));
  121. THEN( "The volume is 25% of the previous volume.") {
  122. REQUIRE(abs(cube.volume() - 0.25*20.0*20.0*20.0) < 1e-2);
  123. }
  124. THEN( "The X coordinate size is 25% from previous.") {
  125. REQUIRE(cube.its.vertices.at(0).x() == 5.0);
  126. }
  127. }
  128. WHEN( "The cube is rotated 45 degrees.") {
  129. cube.rotate_z(float(M_PI / 4.));
  130. THEN( "The X component of the size is sqrt(2)*20") {
  131. REQUIRE(abs(cube.size().x() - sqrt(2.0)*20) < 1e-2);
  132. }
  133. }
  134. WHEN( "The cube is translated (5, 10, 0) units with a Vec3f ") {
  135. cube.translate(Vec3f(5.0, 10.0, 0.0));
  136. THEN( "The first vertex is located at 25, 30, 0") {
  137. REQUIRE(cube.its.vertices.at(0) == Vec3f(25.0, 30.0, 0.0));
  138. }
  139. }
  140. WHEN( "The cube is translated (5, 10, 0) units with 3 doubles") {
  141. cube.translate(5.0, 10.0, 0.0);
  142. THEN( "The first vertex is located at 25, 30, 0") {
  143. REQUIRE(cube.its.vertices.at(0) == Vec3f(25.0, 30.0, 0.0));
  144. }
  145. }
  146. WHEN( "The cube is translated (5, 10, 0) units and then aligned to origin") {
  147. cube.translate(5.0, 10.0, 0.0);
  148. cube.align_to_origin();
  149. THEN( "The third vertex is located at 0,0,0") {
  150. REQUIRE(cube.its.vertices.at(2) == Vec3f::Zero());
  151. }
  152. THEN( "Size is OK") {
  153. REQUIRE(cube.stats().size == Vec3f(20.f, 20.f, 20.f));
  154. }
  155. }
  156. }
  157. }
  158. SCENARIO( "TriangleMesh: slice behavior.") {
  159. GIVEN( "A 20mm cube with one corner on the origin") {
  160. auto cube = make_cube();
  161. WHEN("Cube is sliced with z = [0+EPSILON,2,4,8,6,8,10,12,14,16,18,20]") {
  162. std::vector<double> z { 0+EPSILON,2,4,8,6,8,10,12,14,16,18,20 };
  163. std::vector<ExPolygons> result = cube.slice(z);
  164. THEN( "The correct number of polygons are returned per layer.") {
  165. for (size_t i = 0U; i < z.size(); i++) {
  166. REQUIRE(result.at(i).size() == 1);
  167. }
  168. }
  169. THEN( "The area of the returned polygons is correct.") {
  170. for (size_t i = 0U; i < z.size(); i++) {
  171. REQUIRE(result.at(i).at(0).area() == 20.0*20/(std::pow(SCALING_FACTOR,2)));
  172. }
  173. }
  174. }
  175. }
  176. GIVEN( "A STL with an irregular shape.") {
  177. const std::vector<Vec3f> vertices {{0,0,0},{0,0,20},{0,5,0},{0,5,20},{50,0,0},{50,0,20},{15,5,0},{35,5,0},{15,20,0},{50,5,0},{35,20,0},{15,5,10},{50,5,20},{35,5,10},{35,20,10},{15,20,10}};
  178. const std::vector<Vec3i32> facets {{0,1,2},{2,1,3},{1,0,4},{5,1,4},{0,2,4},{4,2,6},{7,6,8},{4,6,7},{9,4,7},{7,8,10},{2,3,6},{11,3,12},{7,12,9},{13,12,7},{6,3,11},{11,12,13},{3,1,5},{12,3,5},{5,4,9},{12,5,9},{13,7,10},{14,13,10},{8,15,10},{10,15,14},{6,11,8},{8,11,15},{15,11,13},{14,15,13}};
  179. auto cube = make_cube();
  180. WHEN(" a top tangent plane is sliced") {
  181. // At Z = 10 we have a top horizontal surface.
  182. std::vector<ExPolygons> slices = cube.slice({5.0, 10.0});
  183. THEN( "its area is included") {
  184. REQUIRE(slices.at(0).at(0).area() > 0);
  185. REQUIRE(slices.at(1).at(0).area() > 0);
  186. }
  187. }
  188. WHEN(" a model that has been transformed is sliced") {
  189. cube.mirror_z();
  190. std::vector<ExPolygons> slices = cube.slice({-5.0, -10.0});
  191. THEN( "it is sliced properly (mirrored bottom plane area is included)") {
  192. REQUIRE(slices.at(0).at(0).area() > 0);
  193. REQUIRE(slices.at(1).at(0).area() > 0);
  194. }
  195. }
  196. }
  197. }
  198. SCENARIO( "make_xxx functions produce meshes.") {
  199. GIVEN("make_cube() function") {
  200. WHEN("make_cube() is called with arguments 20,20,20") {
  201. TriangleMesh cube = make_cube(20,20,20);
  202. THEN("The resulting mesh has one and only one vertex at 0,0,0") {
  203. const std::vector<Vec3f> &verts = cube.its.vertices;
  204. REQUIRE(std::count_if(verts.begin(), verts.end(), [](const Vec3f& t) { return t.x() == 0 && t.y() == 0 && t.z() == 0; } ) == 1);
  205. }
  206. THEN("The mesh volume is 20*20*20") {
  207. REQUIRE(abs(cube.volume() - 20.0*20.0*20.0) < 1e-2);
  208. }
  209. THEN("There are 12 facets.") {
  210. REQUIRE(cube.its.indices.size() == 12);
  211. }
  212. }
  213. }
  214. GIVEN("make_cylinder() function") {
  215. WHEN("make_cylinder() is called with arguments 10,10, PI / 3") {
  216. TriangleMesh cyl = make_cylinder(10, 10, PI / 243.0);
  217. double angle = (2*PI / floor(2*PI / (PI / 243.0)));
  218. THEN("The resulting mesh has one and only one vertex at 0,0,0") {
  219. const std::vector<Vec3f> &verts = cyl.its.vertices;
  220. REQUIRE(std::count_if(verts.begin(), verts.end(), [](const Vec3f& t) { return t.x() == 0 && t.y() == 0 && t.z() == 0; } ) == 1);
  221. }
  222. THEN("The resulting mesh has one and only one vertex at 0,0,10") {
  223. const std::vector<Vec3f> &verts = cyl.its.vertices;
  224. REQUIRE(std::count_if(verts.begin(), verts.end(), [](const Vec3f& t) { return t.x() == 0 && t.y() == 0 && t.z() == 10; } ) == 1);
  225. }
  226. THEN("Resulting mesh has 2 + (2*PI/angle * 2) vertices.") {
  227. REQUIRE(cyl.its.vertices.size() == (2 + ((2*PI/angle)*2)));
  228. }
  229. THEN("Resulting mesh has 2*PI/angle * 4 facets") {
  230. REQUIRE(cyl.its.indices.size() == (2*PI/angle)*4);
  231. }
  232. THEN( "The mesh volume is approximately 10pi * 10^2") {
  233. REQUIRE(abs(cyl.volume() - (10.0 * M_PI * std::pow(10,2))) < 1);
  234. }
  235. }
  236. }
  237. GIVEN("make_sphere() function") {
  238. WHEN("make_sphere() is called with arguments 10, PI / 3") {
  239. TriangleMesh sph = make_sphere(10, PI / 243.0);
  240. THEN("Resulting mesh has one point at 0,0,-10 and one at 0,0,10") {
  241. const std::vector<stl_vertex> &verts = sph.its.vertices;
  242. REQUIRE(std::count_if(verts.begin(), verts.end(), [](const Vec3f& t) { return is_approx(t, Vec3f(0.f, 0.f, 10.f)); } ) == 1);
  243. REQUIRE(std::count_if(verts.begin(), verts.end(), [](const Vec3f& t) { return is_approx(t, Vec3f(0.f, 0.f, -10.f)); } ) == 1);
  244. }
  245. THEN( "The mesh volume is approximately 4/3 * pi * 10^3") {
  246. REQUIRE(abs(sph.volume() - (4.0/3.0 * M_PI * std::pow(10,3))) < 1); // 1% tolerance?
  247. }
  248. }
  249. }
  250. }
  251. SCENARIO( "TriangleMesh: split functionality.") {
  252. GIVEN( "A 20mm cube with one corner on the origin") {
  253. auto cube = make_cube();
  254. WHEN( "The mesh is split into its component parts.") {
  255. std::vector<TriangleMesh> meshes = cube.split();
  256. THEN(" The bounding box statistics are propagated to the split copies") {
  257. REQUIRE(meshes.size() == 1);
  258. REQUIRE((meshes.front().bounding_box() == cube.bounding_box()));
  259. }
  260. }
  261. }
  262. GIVEN( "Two 20mm cubes, each with one corner on the origin, merged into a single TriangleMesh") {
  263. auto cube = make_cube();
  264. TriangleMesh cube2(cube);
  265. cube.merge(cube2);
  266. WHEN( "The combined mesh is split") {
  267. THEN( "Number of faces is 2x the source.") {
  268. REQUIRE(cube.facets_count() == 2 * cube2.facets_count());
  269. }
  270. std::vector<TriangleMesh> meshes = cube.split();
  271. THEN( "Two meshes are in the output vector.") {
  272. REQUIRE(meshes.size() == 2);
  273. }
  274. }
  275. }
  276. }
  277. SCENARIO( "TriangleMesh: Mesh merge functions") {
  278. GIVEN( "Two 20mm cubes, each with one corner on the origin") {
  279. auto cube = make_cube();
  280. TriangleMesh cube2(cube);
  281. WHEN( "The two meshes are merged") {
  282. cube.merge(cube2);
  283. THEN( "There are twice as many facets in the merged mesh as the original.") {
  284. REQUIRE(cube.facets_count() == 2 * cube2.facets_count());
  285. }
  286. }
  287. }
  288. }
  289. SCENARIO( "TriangleMeshSlicer: Cut behavior.") {
  290. GIVEN( "A 20mm cube with one corner on the origin") {
  291. auto cube = make_cube();
  292. WHEN( "Object is cut at the bottom") {
  293. indexed_triangle_set upper {};
  294. indexed_triangle_set lower {};
  295. cut_mesh(cube.its, 0, &upper, &lower);
  296. THEN("Upper mesh has all facets except those belonging to the slicing plane.") {
  297. REQUIRE(upper.indices.size() == 12);
  298. }
  299. THEN("Lower mesh has no facets.") {
  300. REQUIRE(lower.indices.size() == 0);
  301. }
  302. }
  303. WHEN( "Object is cut at the center") {
  304. indexed_triangle_set upper {};
  305. indexed_triangle_set lower {};
  306. cut_mesh(cube.its, 10, &upper, &lower);
  307. THEN("Upper mesh has 2 external horizontal facets, 3 facets on each side, and 6 facets on the triangulated side (2 + 12 + 6).") {
  308. REQUIRE(upper.indices.size() == 2+12+6);
  309. }
  310. THEN("Lower mesh has 2 external horizontal facets, 3 facets on each side, and 6 facets on the triangulated side (2 + 12 + 6).") {
  311. REQUIRE(lower.indices.size() == 2+12+6);
  312. }
  313. }
  314. }
  315. }
  316. #ifdef TEST_PERFORMANCE
  317. TEST_CASE("Regression test for issue #4486 - files take forever to slice") {
  318. TriangleMesh mesh;
  319. DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  320. mesh.ReadSTLFile(std::string(testfile_dir) + "test_trianglemesh/4486/100_000.stl");
  321. config.set("layer_height", 500);
  322. config.set("first_layer_height", 250);
  323. config.set("nozzle_diameter", 500);
  324. Slic3r::Print print;
  325. Slic3r::Model model;
  326. Slic3r::Test::init_print({mesh}, print, model, config);
  327. print.status_cb = [] (int ln, const std::string& msg) { Slic3r::Log::info("Print") << ln << " " << msg << "\n";};
  328. std::future<void> fut = std::async([&print] () { print.process(); });
  329. std::chrono::milliseconds span {120000};
  330. bool timedout {false};
  331. if(fut.wait_for(span) == std::future_status::timeout) {
  332. timedout = true;
  333. }
  334. REQUIRE(timedout == false);
  335. }
  336. #endif // TEST_PERFORMANCE
  337. #ifdef BUILD_PROFILE
  338. TEST_CASE("Profile test for issue #4486 - files take forever to slice") {
  339. TriangleMesh mesh;
  340. DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  341. mesh.ReadSTLFile(std::string(testfile_dir) + "test_trianglemesh/4486/10_000.stl");
  342. config.set("layer_height", 500);
  343. config.set("first_layer_height", 250);
  344. config.set("nozzle_diameter", 500);
  345. config.set("fill_density", "5%");
  346. Slic3r::Print print;
  347. Slic3r::Model model;
  348. Slic3r::Test::init_print({mesh}, print, model, config);
  349. print.status_cb = [] (int ln, const std::string& msg) { Slic3r::Log::info("Print") << ln << " " << msg << "\n";};
  350. print.process();
  351. REQUIRE(true);
  352. }
  353. #endif //BUILD_PROFILE