test_quadric_edge_collapse.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include <catch2/catch.hpp>
  2. #include <igl/qslim.h>
  3. #include <test_utils.hpp>
  4. #include <libslic3r/QuadricEdgeCollapse.hpp>
  5. #include <libslic3r/TriangleMesh.hpp> // its - indexed_triangle_set
  6. #include "libslic3r/AABBTreeIndirect.hpp" // is similar
  7. using namespace Slic3r;
  8. namespace Private {
  9. struct Similarity
  10. {
  11. float max_distance = 0.f;
  12. float average_distance = 0.f;
  13. Similarity() = default;
  14. Similarity(float max_distance, float average_distance)
  15. : max_distance(max_distance), average_distance(average_distance)
  16. {}
  17. };
  18. // border for our algorithm with frog_leg model and decimation to 5%
  19. Similarity frog_leg_5(0.32f, 0.043f);
  20. Similarity get_similarity(const indexed_triangle_set &from,
  21. const indexed_triangle_set &to)
  22. {
  23. // create ABBTree
  24. auto tree = AABBTreeIndirect::build_aabb_tree_over_indexed_triangle_set(
  25. from.vertices, from.indices);
  26. float sum_distance = 0.f;
  27. float max_distance = 0.f;
  28. auto collect_distances = [&](const Vec3f &surface_point) {
  29. size_t hit_idx;
  30. Vec3f hit_point;
  31. float distance2 =
  32. AABBTreeIndirect::squared_distance_to_indexed_triangle_set(
  33. from.vertices, from.indices, tree, surface_point, hit_idx,
  34. hit_point);
  35. float distance = sqrt(distance2);
  36. if (max_distance < distance) max_distance = distance;
  37. sum_distance += distance;
  38. };
  39. for (const Vec3f &vertex : to.vertices) { collect_distances(vertex); }
  40. for (const Vec3i &t : to.indices) {
  41. Vec3f center(0, 0, 0);
  42. for (size_t i = 0; i < 3; ++i) { center += to.vertices[t[i]] / 3; }
  43. collect_distances(center);
  44. }
  45. size_t count = to.vertices.size() + to.indices.size();
  46. float average_distance = sum_distance / count;
  47. std::cout << "max_distance = " << max_distance << ", average_distance = " << average_distance << std::endl;
  48. return Similarity(max_distance, average_distance);
  49. }
  50. void is_better_similarity(const indexed_triangle_set &its_first,
  51. const indexed_triangle_set &its_second,
  52. const Similarity & compare)
  53. {
  54. Similarity s1 = get_similarity(its_first, its_second);
  55. Similarity s2 = get_similarity(its_second, its_first);
  56. CHECK(s1.average_distance < compare.average_distance);
  57. CHECK(s1.max_distance < compare.max_distance);
  58. CHECK(s2.average_distance < compare.average_distance);
  59. CHECK(s2.max_distance < compare.max_distance);
  60. }
  61. void is_worse_similarity(const indexed_triangle_set &its_first,
  62. const indexed_triangle_set &its_second,
  63. const Similarity & compare)
  64. {
  65. Similarity s1 = get_similarity(its_first, its_second);
  66. Similarity s2 = get_similarity(its_second, its_first);
  67. if (s1.max_distance < compare.max_distance &&
  68. s2.max_distance < compare.max_distance)
  69. CHECK(false);
  70. }
  71. bool exist_triangle_with_twice_vertices(const std::vector<stl_triangle_vertex_indices> &indices)
  72. {
  73. for (const auto &face : indices)
  74. if (face[0] == face[1] || face[0] == face[2] || face[1] == face[2])
  75. return true;
  76. return false;
  77. }
  78. } // namespace Private
  79. TEST_CASE("Reduce one edge by Quadric Edge Collapse", "[its]")
  80. {
  81. indexed_triangle_set its;
  82. its.vertices = {Vec3f(-1.f, 0.f, 0.f), Vec3f(0.f, 1.f, 0.f),
  83. Vec3f(1.f, 0.f, 0.f), Vec3f(0.f, 0.f, 1.f),
  84. // vertex to be removed
  85. Vec3f(0.9f, .1f, -.1f)};
  86. its.indices = {Vec3i(1, 0, 3), Vec3i(2, 1, 3), Vec3i(0, 2, 3),
  87. Vec3i(0, 1, 4), Vec3i(1, 2, 4), Vec3i(2, 0, 4)};
  88. // edge to remove is between vertices 2 and 4 on trinagles 4 and 5
  89. indexed_triangle_set its_ = its; // copy
  90. // its_write_obj(its, "tetrhedron_in.obj");
  91. uint32_t wanted_count = its.indices.size() - 1;
  92. its_quadric_edge_collapse(its, wanted_count);
  93. // its_write_obj(its, "tetrhedron_out.obj");
  94. CHECK(its.indices.size() == 4);
  95. CHECK(its.vertices.size() == 4);
  96. for (size_t i = 0; i < 3; i++) {
  97. CHECK(its.indices[i] == its_.indices[i]);
  98. }
  99. for (size_t i = 0; i < 4; i++) {
  100. if (i == 2) continue;
  101. CHECK(its.vertices[i] == its_.vertices[i]);
  102. }
  103. const Vec3f &v = its.vertices[2]; // new vertex
  104. const Vec3f &v2 = its_.vertices[2]; // moved vertex
  105. const Vec3f &v4 = its_.vertices[4]; // removed vertex
  106. for (size_t i = 0; i < 3; i++) {
  107. bool is_between = (v[i] < v4[i] && v[i] > v2[i]) ||
  108. (v[i] > v4[i] && v[i] < v2[i]);
  109. CHECK(is_between);
  110. }
  111. Private::Similarity max_similarity(0.75f, 0.014f);
  112. Private::is_better_similarity(its, its_, max_similarity);
  113. }
  114. static bool is_equal(const std::vector<stl_vertex> &v1,
  115. const std::vector<stl_vertex> &v2,
  116. float epsilon = std::numeric_limits<float>::epsilon())
  117. {
  118. // is same count?
  119. if (v1.size() != v2.size()) return false;
  120. // check all v1 vertices
  121. for (const auto &v1_ : v1) {
  122. auto is_equal = [&v1_, epsilon](const auto &v2_) {
  123. for (size_t i = 0; i < 3; i++)
  124. if (fabs(v1_[i] - v2_[i]) > epsilon)
  125. return false;
  126. return true;
  127. };
  128. // is v1 vertex in v2 vertices?
  129. if(std::find_if(v2.begin(), v2.end(), is_equal) == v2.end()) return false;
  130. }
  131. return true;
  132. }
  133. TEST_CASE("Reduce to one triangle by Quadric Edge Collapse", "[its]")
  134. {
  135. // !!! Not work (no manifold - open edges{0-1, 1-2, 2-4, 4-5, 5-3, 3-0}):
  136. /////////////image////
  137. // * 5 //
  138. // |\ //
  139. // | \ //
  140. // 3 *--* 4 //
  141. // | /|\ //
  142. // |/ | \ //
  143. // 0 *--*--* 2 //
  144. // 1 //
  145. //////////////////////
  146. // all triangles are on a plane therefore quadric is zero and
  147. // when reduce edge between vertices 3 and 4 new vertex lay on vertex 3 not 4 !!!
  148. indexed_triangle_set its;
  149. its.vertices = {Vec3f(0.f, 0.f, 0.f), Vec3f(1.f, 0.f, 0.f),
  150. Vec3f(2.f, 0.f, 0.f), Vec3f(0.f, 1.f, 0.f),
  151. Vec3f(1.f, 1.f, 0.f), Vec3f(0.f, 2.f, 0.f)};
  152. its.indices = {Vec3i(0, 1, 4), Vec3i(1, 2, 4), Vec3i(0, 4, 3),
  153. Vec3i(3, 4, 5)};
  154. std::vector<stl_vertex> triangle_vertices = {its.vertices[0],
  155. its.vertices[2],
  156. its.vertices[5]};
  157. uint32_t wanted_count = 1;
  158. its_quadric_edge_collapse(its, wanted_count);
  159. // result should be one triangle made of vertices 0, 2, 5
  160. // NOT WORK
  161. //CHECK(its.indices.size() == wanted_count);
  162. //// check all triangle vertices
  163. //CHECK(is_equal(its.vertices, triangle_vertices));
  164. }
  165. TEST_CASE("Reduce to one tetrahedron by Quadric Edge Collapse", "[its]")
  166. {
  167. // Extend previous test to tetrahedron to make it manifold
  168. indexed_triangle_set its;
  169. its.vertices = {
  170. Vec3f(0.f, 0.f, 0.f), Vec3f(1.f, 0.f, 0.f), Vec3f(2.f, 0.f, 0.f),
  171. Vec3f(0.f, 1.f, 0.f), Vec3f(1.f, 1.f, 0.f),
  172. Vec3f(0.f, 2.f, 0.f)
  173. // tetrahedron extetion
  174. , Vec3f(0.f, 0.f, -2.f)
  175. };
  176. std::vector<stl_vertex> tetrahedron_vertices = {its.vertices[0],
  177. its.vertices[2],
  178. its.vertices[5],
  179. // tetrahedron extetion
  180. its.vertices[6]};
  181. its.indices = {Vec3i(0, 1, 4), Vec3i(1, 2, 4), Vec3i(0, 4, 3), Vec3i(3, 4, 5),
  182. // tetrahedron extetion
  183. Vec3i(4, 2, 6), Vec3i(5, 4, 6), Vec3i(3, 5, 6), Vec3i(0, 3, 6), Vec3i(1, 0, 6), Vec3i(2, 1, 6)
  184. };
  185. uint32_t wanted_count = 4;
  186. //its_write_obj(its, "tetrhedron_in.obj");
  187. its_quadric_edge_collapse(its, wanted_count);
  188. //its_write_obj(its, "tetrhedron_out.obj");
  189. // result should be tetrahedron
  190. CHECK(its.indices.size() == wanted_count);
  191. // check all tetrahedron vertices
  192. CHECK(is_equal(its.vertices, tetrahedron_vertices));
  193. }
  194. TEST_CASE("Simplify frog_legs.obj to 5% by Quadric edge collapse", "[its][quadric_edge_collapse]")
  195. {
  196. TriangleMesh mesh = load_model("frog_legs.obj");
  197. double original_volume = its_volume(mesh.its);
  198. uint32_t wanted_count = mesh.its.indices.size() * 0.05;
  199. REQUIRE_FALSE(mesh.empty());
  200. indexed_triangle_set its = mesh.its; // copy
  201. float max_error = std::numeric_limits<float>::max();
  202. its_quadric_edge_collapse(its, wanted_count, &max_error);
  203. // its_write_obj(its, "frog_legs_qec.obj");
  204. CHECK(its.indices.size() <= wanted_count);
  205. double volume = its_volume(its);
  206. CHECK(fabs(original_volume - volume) < 33.);
  207. Private::is_better_similarity(mesh.its, its, Private::frog_leg_5);
  208. }
  209. TEST_CASE("Simplify frog_legs.obj to 5% by IGL/qslim", "[]")
  210. {
  211. std::string obj_filename = "frog_legs.obj";
  212. TriangleMesh mesh = load_model(obj_filename);
  213. REQUIRE_FALSE(mesh.empty());
  214. indexed_triangle_set &its = mesh.its;
  215. //double original_volume = its_volume(its);
  216. uint32_t wanted_count = its.indices.size() * 0.05;
  217. Eigen::MatrixXd V(its.vertices.size(), 3);
  218. Eigen::MatrixXi F(its.indices.size(), 3);
  219. for (size_t j = 0; j < its.vertices.size(); ++j) {
  220. Vec3d vd = its.vertices[j].cast<double>();
  221. for (int i = 0; i < 3; ++i) V(j, i) = vd(i);
  222. }
  223. for (size_t j = 0; j < its.indices.size(); ++j) {
  224. const auto &f = its.indices[j];
  225. for (int i = 0; i < 3; ++i) F(j, i) = f(i);
  226. }
  227. size_t max_m = wanted_count;
  228. Eigen::MatrixXd U;
  229. Eigen::MatrixXi G;
  230. Eigen::VectorXi J, I;
  231. CHECK(igl::qslim(V, F, max_m, U, G, J, I));
  232. // convert to its
  233. indexed_triangle_set its_out;
  234. its_out.vertices.reserve(U.size()/3);
  235. its_out.indices.reserve(G.size()/3);
  236. size_t U_size = U.size() / 3;
  237. for (size_t i = 0; i < U_size; i++)
  238. its_out.vertices.emplace_back(U(i, 0), U(i, 1), U(i, 2));
  239. size_t G_size = G.size() / 3;
  240. for (size_t i = 0; i < G_size; i++)
  241. its_out.indices.emplace_back(G(i, 0), G(i, 1), G(i, 2));
  242. // check if algorithm is still worse than our
  243. Private::is_worse_similarity(its_out, its, Private::frog_leg_5);
  244. // its_out, its --> avg_distance: 0.0351217, max_distance 0.364316
  245. // its, its_out --> avg_distance: 0.0412358, max_distance 0.238913
  246. }
  247. TEST_CASE("Simplify trouble case", "[its]")
  248. {
  249. TriangleMesh tm = load_model("simplification.obj");
  250. REQUIRE_FALSE(tm.empty());
  251. float max_error = std::numeric_limits<float>::max();
  252. uint32_t wanted_count = 0;
  253. its_quadric_edge_collapse(tm.its, wanted_count, &max_error);
  254. CHECK(!Private::exist_triangle_with_twice_vertices(tm.its.indices));
  255. }
  256. TEST_CASE("Simplified cube should not be empty.", "[its]")
  257. {
  258. auto its = its_make_cube(1, 2, 3);
  259. float max_error = std::numeric_limits<float>::max();
  260. uint32_t wanted_count = 0;
  261. its_quadric_edge_collapse(its, wanted_count, &max_error);
  262. CHECK(!its.indices.empty());
  263. }