test_indexed_triangle_set.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include <iostream>
  2. #include <fstream>
  3. #include <catch2/catch.hpp>
  4. #include "libslic3r/TriangleMesh.hpp"
  5. using namespace Slic3r;
  6. TEST_CASE("Split empty mesh", "[its_split][its]") {
  7. using namespace Slic3r;
  8. indexed_triangle_set its;
  9. std::vector<indexed_triangle_set> res = its_split(its);
  10. REQUIRE(res.empty());
  11. }
  12. TEST_CASE("Split simple mesh consisting of one part", "[its_split][its]") {
  13. using namespace Slic3r;
  14. auto cube = its_make_cube(10., 10., 10.);
  15. std::vector<indexed_triangle_set> res = its_split(cube);
  16. REQUIRE(res.size() == 1);
  17. REQUIRE(res.front().indices.size() == cube.indices.size());
  18. REQUIRE(res.front().vertices.size() == cube.vertices.size());
  19. }
  20. void debug_write_obj(const std::vector<indexed_triangle_set> &res, const std::string &name)
  21. {
  22. #ifndef NDEBUG
  23. size_t part_idx = 0;
  24. for (auto &part_its : res) {
  25. its_write_obj(part_its, (name + std::to_string(part_idx++) + ".obj").c_str());
  26. }
  27. #endif
  28. }
  29. TEST_CASE("Split two non-watertight mesh", "[its_split][its]") {
  30. using namespace Slic3r;
  31. auto cube1 = its_make_cube(10., 10., 10.);
  32. cube1.indices.pop_back();
  33. auto cube2 = cube1;
  34. its_transform(cube1, identity3f().translate(Vec3f{-5.f, 0.f, 0.f}));
  35. its_transform(cube2, identity3f().translate(Vec3f{5.f, 0.f, 0.f}));
  36. its_merge(cube1, cube2);
  37. std::vector<indexed_triangle_set> res = its_split(cube1);
  38. REQUIRE(res.size() == 2);
  39. REQUIRE(res[0].indices.size() == res[1].indices.size());
  40. REQUIRE(res[0].indices.size() == cube2.indices.size());
  41. REQUIRE(res[0].vertices.size() == res[1].vertices.size());
  42. REQUIRE(res[0].vertices.size() == cube2.vertices.size());
  43. debug_write_obj(res, "parts_non_watertight");
  44. }
  45. TEST_CASE("Split non-manifold mesh", "[its_split][its]") {
  46. using namespace Slic3r;
  47. auto cube = its_make_cube(10., 10., 10.), cube_low = cube;
  48. its_transform(cube_low, identity3f().translate(Vec3f{10.f, 10.f, 10.f}));
  49. its_merge(cube, cube_low);
  50. its_merge_vertices(cube);
  51. std::vector<indexed_triangle_set> res = its_split(cube);
  52. REQUIRE(res.size() == 2);
  53. REQUIRE(res[0].indices.size() == res[1].indices.size());
  54. REQUIRE(res[0].indices.size() == cube_low.indices.size());
  55. REQUIRE(res[0].vertices.size() == res[1].vertices.size());
  56. REQUIRE(res[0].vertices.size() == cube_low.vertices.size());
  57. debug_write_obj(res, "cubes_non_manifold");
  58. }
  59. TEST_CASE("Split two watertight meshes", "[its_split][its]") {
  60. using namespace Slic3r;
  61. auto sphere1 = its_make_sphere(10., 2 * PI / 200.), sphere2 = sphere1;
  62. its_transform(sphere1, identity3f().translate(Vec3f{-5.f, 0.f, 0.f}));
  63. its_transform(sphere2, identity3f().translate(Vec3f{5.f, 0.f, 0.f}));
  64. its_merge(sphere1, sphere2);
  65. std::vector<indexed_triangle_set> res = its_split(sphere1);
  66. REQUIRE(res.size() == 2);
  67. REQUIRE(res[0].indices.size() == res[1].indices.size());
  68. REQUIRE(res[0].indices.size() == sphere2.indices.size());
  69. REQUIRE(res[0].vertices.size() == res[1].vertices.size());
  70. REQUIRE(res[0].vertices.size() == sphere2.vertices.size());
  71. debug_write_obj(res, "parts_watertight");
  72. }
  73. #include <libslic3r/QuadricEdgeCollapse.hpp>
  74. static float triangle_area(const Vec3f &v0, const Vec3f &v1, const Vec3f &v2)
  75. {
  76. Vec3f ab = v1 - v0;
  77. Vec3f ac = v2 - v0;
  78. return ab.cross(ac).norm() / 2.f;
  79. }
  80. static float triangle_area(const Vec3crd &triangle_inices, const std::vector<Vec3f> &vertices)
  81. {
  82. return triangle_area(vertices[triangle_inices[0]],
  83. vertices[triangle_inices[1]],
  84. vertices[triangle_inices[2]]);
  85. }
  86. #if 0
  87. // clang complains about unused functions
  88. static std::mt19937 create_random_generator() {
  89. std::random_device rd;
  90. std::mt19937 gen(rd());
  91. return gen;
  92. }
  93. #endif
  94. std::vector<Vec3f> its_sample_surface(const indexed_triangle_set &its,
  95. double sample_per_mm2,
  96. std::mt19937 random_generator) // = create_random_generator())
  97. {
  98. std::vector<Vec3f> samples;
  99. std::uniform_real_distribution<float> rand01(0.f, 1.f);
  100. for (const auto &triangle_indices : its.indices) {
  101. float area = triangle_area(triangle_indices, its.vertices);
  102. float countf;
  103. float fractional = std::modf(area * sample_per_mm2, &countf);
  104. int count = static_cast<int>(countf);
  105. float generate = rand01(random_generator);
  106. if (generate < fractional) ++count;
  107. if (count == 0) continue;
  108. const Vec3f &v0 = its.vertices[triangle_indices[0]];
  109. const Vec3f &v1 = its.vertices[triangle_indices[1]];
  110. const Vec3f &v2 = its.vertices[triangle_indices[2]];
  111. for (int c = 0; c < count; c++) {
  112. // barycentric coordinate
  113. Vec3f b;
  114. b[0] = rand01(random_generator);
  115. b[1] = rand01(random_generator);
  116. if ((b[0] + b[1]) > 1.f) {
  117. b[0] = 1.f - b[0];
  118. b[1] = 1.f - b[1];
  119. }
  120. b[2] = 1.f - b[0] - b[1];
  121. Vec3f pos;
  122. for (int i = 0; i < 3; i++) {
  123. pos[i] = b[0] * v0[i] + b[1] * v1[i] + b[2] * v2[i];
  124. }
  125. samples.push_back(pos);
  126. }
  127. }
  128. return samples;
  129. }
  130. #include "libslic3r/AABBTreeIndirect.hpp"
  131. struct CompareConfig
  132. {
  133. float max_distance = 3.f;
  134. float max_average_distance = 2.f;
  135. };
  136. bool is_similar(const indexed_triangle_set &from,
  137. const indexed_triangle_set &to,
  138. const CompareConfig &cfg)
  139. {
  140. // create ABBTree
  141. auto tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set(
  142. from.vertices, from.indices);
  143. float sum_distance = 0.f;
  144. float max_distance = 0.f;
  145. auto collect_distances = [&](const Vec3f &surface_point) {
  146. size_t hit_idx;
  147. Vec3f hit_point;
  148. float distance2 =
  149. AABBTreeIndirect::squared_distance_to_indexed_triangle_set(
  150. from.vertices, from.indices, tree, surface_point, hit_idx, hit_point);
  151. float distance = sqrt(distance2);
  152. if (max_distance < distance) max_distance = distance;
  153. sum_distance += distance;
  154. };
  155. for (const Vec3f &vertex : to.vertices) {
  156. collect_distances(vertex);
  157. }
  158. for (const Vec3i &t : to.indices) {
  159. Vec3f center(0,0,0);
  160. for (size_t i = 0; i < 3; ++i) {
  161. center += to.vertices[t[i]] / 3;
  162. }
  163. collect_distances(center);
  164. }
  165. size_t count = to.vertices.size() + to.indices.size();
  166. float avg_distance = sum_distance / count;
  167. if (avg_distance > cfg.max_average_distance ||
  168. max_distance > cfg.max_distance)
  169. return false;
  170. return true;
  171. }
  172. TEST_CASE("Reduce one edge by Quadric Edge Collapse", "[its]")
  173. {
  174. indexed_triangle_set its;
  175. its.vertices = {Vec3f(-1.f, 0.f, 0.f), Vec3f(0.f, 1.f, 0.f),
  176. Vec3f(1.f, 0.f, 0.f), Vec3f(0.f, 0.f, 1.f),
  177. // vertex to be removed
  178. Vec3f(0.9f, .1f, -.1f)};
  179. its.indices = {Vec3i(1, 0, 3), Vec3i(2, 1, 3), Vec3i(0, 2, 3),
  180. Vec3i(0, 1, 4), Vec3i(1, 2, 4), Vec3i(2, 0, 4)};
  181. // edge to remove is between vertices 2 and 4 on trinagles 4 and 5
  182. indexed_triangle_set its_ = its; // copy
  183. // its_write_obj(its, "tetrhedron_in.obj");
  184. uint32_t wanted_count = its.indices.size() - 1;
  185. its_quadric_edge_collapse(its, wanted_count);
  186. // its_write_obj(its, "tetrhedron_out.obj");
  187. CHECK(its.indices.size() == 4);
  188. CHECK(its.vertices.size() == 4);
  189. for (size_t i = 0; i < 3; i++) {
  190. CHECK(its.indices[i] == its_.indices[i]);
  191. }
  192. for (size_t i = 0; i < 4; i++) {
  193. if (i == 2) continue;
  194. CHECK(its.vertices[i] == its_.vertices[i]);
  195. }
  196. const Vec3f &v = its.vertices[2]; // new vertex
  197. const Vec3f &v2 = its_.vertices[2]; // moved vertex
  198. const Vec3f &v4 = its_.vertices[4]; // removed vertex
  199. for (size_t i = 0; i < 3; i++) {
  200. bool is_between = (v[i] < v4[i] && v[i] > v2[i]) ||
  201. (v[i] > v4[i] && v[i] < v2[i]);
  202. CHECK(is_between);
  203. }
  204. CompareConfig cfg;
  205. cfg.max_average_distance = 0.014f;
  206. cfg.max_distance = 0.75f;
  207. CHECK(is_similar(its, its_, cfg));
  208. CHECK(is_similar(its_, its, cfg));
  209. }
  210. #include "test_utils.hpp"
  211. TEST_CASE("Simplify mesh by Quadric edge collapse to 5%", "[its]")
  212. {
  213. TriangleMesh mesh = load_model("frog_legs.obj");
  214. double original_volume = its_volume(mesh.its);
  215. uint32_t wanted_count = mesh.its.indices.size() * 0.05;
  216. REQUIRE_FALSE(mesh.empty());
  217. indexed_triangle_set its = mesh.its; // copy
  218. float max_error = std::numeric_limits<float>::max();
  219. its_quadric_edge_collapse(its, wanted_count, &max_error);
  220. //its_write_obj(its, "frog_legs_qec.obj");
  221. CHECK(its.indices.size() <= wanted_count);
  222. double volume = its_volume(its);
  223. CHECK(fabs(original_volume - volume) < 33.);
  224. CompareConfig cfg;
  225. cfg.max_average_distance = 0.043f;
  226. cfg.max_distance = 0.32f;
  227. CHECK(is_similar(mesh.its, its, cfg));
  228. CHECK(is_similar(its, mesh.its, cfg));
  229. }
  230. bool exist_triangle_with_twice_vertices(const std::vector<stl_triangle_vertex_indices>& indices)
  231. {
  232. for (const auto &face : indices)
  233. if (face[0] == face[1] ||
  234. face[0] == face[2] ||
  235. face[1] == face[2]) return true;
  236. return false;
  237. }
  238. TEST_CASE("Simplify trouble case", "[its]")
  239. {
  240. TriangleMesh tm = load_model("simplification.obj");
  241. REQUIRE_FALSE(tm.empty());
  242. float max_error = std::numeric_limits<float>::max();
  243. uint32_t wanted_count = 0;
  244. its_quadric_edge_collapse(tm.its, wanted_count, &max_error);
  245. CHECK(!exist_triangle_with_twice_vertices(tm.its.indices));
  246. }
  247. TEST_CASE("Simplified cube should not be empty.", "[its]")
  248. {
  249. auto its = its_make_cube(1, 2, 3);
  250. float max_error = std::numeric_limits<float>::max();
  251. uint32_t wanted_count = 0;
  252. its_quadric_edge_collapse(its, wanted_count, &max_error);
  253. CHECK(!its.indices.empty());
  254. }