test_trianglemesh.cpp 14 KB

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