order_facets_around_edges.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <test_common.h>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <vector>
  5. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  6. #include <igl/cgal/order_facets_around_edges.h>
  7. #include <igl/unique_edge_map.h>
  8. #include <igl/readDMAT.h>
  9. #include <igl/per_face_normals.h>
  10. namespace order_facets_around_edges_test {
  11. typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
  12. template<typename T>
  13. size_t index_of(const std::vector<T>& array, T val) {
  14. auto loc = std::find(array.begin(), array.end(), val);
  15. assert(loc != array.end());
  16. return loc - array.begin();
  17. }
  18. void assert_consistently_oriented(size_t num_faces,
  19. const std::vector<int>& expected_face_order,
  20. const std::vector<int>& e_order) {
  21. const size_t num_items = expected_face_order.size();
  22. ASSERT_EQ(num_items, e_order.size());
  23. std::vector<int> order(num_items);
  24. std::transform(e_order.begin(), e_order.end(), order.begin(),
  25. [=](int val) { return val % num_faces; });
  26. size_t ref_start = index_of(order, expected_face_order[0]);
  27. for (size_t i=0; i<num_items; i++) {
  28. ASSERT_EQ(expected_face_order[i], order[(ref_start + i) % num_items]);
  29. }
  30. }
  31. template<typename DerivedV, typename DerivedF>
  32. void assert_order(
  33. const Eigen::PlainObjectBase<DerivedV>& V,
  34. const Eigen::PlainObjectBase<DerivedF>& F,
  35. size_t v0, size_t v1,
  36. std::vector<int> expected_order, const std::string& normal="") {
  37. Eigen::MatrixXi E, uE, EMAP;
  38. std::vector<std::vector<int> > uE2E;
  39. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  40. std::vector<std::vector<int> > uE2oE;
  41. std::vector<std::vector<bool> > uE2C;
  42. if (normal != "") {
  43. Eigen::MatrixXd N;
  44. //igl::per_face_normals_stable(V, F, N);
  45. //igl::per_face_normals(V, F, N);
  46. test_common::load_matrix(normal, N);
  47. igl::cgal::order_facets_around_edges(V, F, N, E, uE, EMAP, uE2E, uE2oE, uE2C);
  48. } else {
  49. igl::cgal::order_facets_around_edges(V, F, E, uE, EMAP, uE2E, uE2oE, uE2C);
  50. }
  51. const size_t num_uE = uE.rows();
  52. for (size_t i=0; i<num_uE; i++) {
  53. const auto& order = uE2oE[i];
  54. const auto& cons = uE2C[i];
  55. Eigen::VectorXi e = uE.row(i);
  56. if (order.size() <= 1) continue;
  57. if (e[0] != v0 && e[0] != v1) continue;
  58. if (e[1] != v0 && e[1] != v1) continue;
  59. if (e[0] == v1 && e[1] == v0) {
  60. std::reverse(expected_order.begin(), expected_order.end());
  61. }
  62. assert_consistently_oriented(F.rows(), expected_order, order);
  63. }
  64. }
  65. TEST(OrderFacetsAroundEdges, Simple) {
  66. Eigen::MatrixXd V(4, 3);
  67. V << 0.0, 0.0, 0.0,
  68. 1.0, 0.0, 0.0,
  69. 0.0, 1.0, 0.0,
  70. 1.0, 1.0, 0.0;
  71. Eigen::MatrixXi F(2, 3);
  72. F << 0, 1, 2,
  73. 2, 1, 3;
  74. assert_order(V, F, 1, 2, {0, 1});
  75. }
  76. TEST(OrderFacetsAroundEdges, TripletFaces) {
  77. Eigen::MatrixXd V(5, 3);
  78. V << 0.0, 0.0, 0.0,
  79. 1.0, 0.0, 0.0,
  80. 0.0, 1.0, 0.0,
  81. 1.0, 1.0, 0.0,
  82. 0.0, 0.0, 1.0;
  83. Eigen::MatrixXi F(3, 3);
  84. F << 0, 1, 2,
  85. 2, 1, 3,
  86. 1, 2, 4;
  87. assert_order(V, F, 1, 2, {0, 1, 2});
  88. }
  89. TEST(OrderFacetsAroundEdges, DuplicatedFaces) {
  90. Eigen::MatrixXd V(5, 3);
  91. V << 0.0, 0.0, 0.0,
  92. 1.0, 0.0, 0.0,
  93. 0.0, 1.0, 0.0,
  94. 1.0, 1.0, 0.0,
  95. 0.0, 0.0, 1.0;
  96. Eigen::MatrixXi F(4, 3);
  97. F << 0, 1, 2,
  98. 2, 1, 3,
  99. 1, 2, 4,
  100. 4, 1, 2;
  101. assert_order(V, F, 1, 2, {0, 1, 3, 2});
  102. }
  103. TEST(OrderFacetsAroundEdges, MultipleDuplicatedFaces) {
  104. Eigen::MatrixXd V(5, 3);
  105. V << 0.0, 0.0, 0.0,
  106. 1.0, 0.0, 0.0,
  107. 0.0, 1.0, 0.0,
  108. 1.0, 1.0, 0.0,
  109. 0.0, 0.0, 1.0;
  110. Eigen::MatrixXi F(6, 3);
  111. F << 0, 1, 2,
  112. 1, 2, 0,
  113. 2, 1, 3,
  114. 1, 3, 2,
  115. 1, 2, 4,
  116. 4, 1, 2;
  117. assert_order(V, F, 1, 2, {1, 0, 2, 3, 5, 4});
  118. }
  119. TEST(OrderFacetsAroundEdges, Debug) {
  120. Eigen::MatrixXd V(5, 3);
  121. V <<
  122. -44.3205080756887781, 4.22994972382184579e-15, 75,
  123. -27.933756729740665, -48.382685902179837, 75,
  124. -55.8675134594812945, -2.81996648254789745e-15, 75,
  125. -27.933756729740665, -48.382685902179837, 70,
  126. -31.4903810567666049, -42.2224318643354408, 85;
  127. Eigen::MatrixXi F(3, 3);
  128. F << 1, 0, 2,
  129. 2, 3, 1,
  130. 4, 1, 2;
  131. assert_order(V, F, 1, 2, {0, 2, 1});
  132. }
  133. TEST(OrderFacetsAroundEdges, Debug2) {
  134. Eigen::MatrixXd V(5, 3);
  135. V <<
  136. -22.160254037844382, 38.3826859021798441, 75,
  137. -27.9337567297406331, 48.3826859021798654, 75,
  138. 27.9337567297406544, 48.3826859021798512, 75,
  139. 27.9337567297406544, 48.3826859021798512, 70,
  140. 20.8205080756887924, 48.3826859021798512, 85;
  141. Eigen::MatrixXi F(3, 3);
  142. F << 1, 0, 2,
  143. 3, 1, 2,
  144. 2, 4, 1;
  145. assert_order(V, F, 1, 2, {1, 0, 2});
  146. }
  147. TEST(OrderFacetsAroundEdges, NormalSensitivity) {
  148. // This example shows that epsilon difference in normal vectors could
  149. // results in very different ordering of facets.
  150. Eigen::MatrixXd V;
  151. test_common::load_matrix("duplicated_faces_V.dmat", V);
  152. Eigen::MatrixXi F;
  153. test_common::load_matrix("duplicated_faces_F.dmat", F);
  154. assert_order(V, F, 223, 224, {2, 0, 3, 1}, "duplicated_faces_N1.dmat");
  155. assert_order(V, F, 223, 224, {0, 3, 2, 1}, "duplicated_faces_N2.dmat");
  156. }
  157. }