order_facets_around_edges.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "order_facets_around_edges.h"
  9. #include "order_facets_around_edge.h"
  10. #include "../sort_angles.h"
  11. #include <type_traits>
  12. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  13. template<
  14. typename DerivedV,
  15. typename DerivedF,
  16. typename DerivedN,
  17. typename DerivedE,
  18. typename DeriveduE,
  19. typename DerivedEMAP,
  20. typename uE2EType,
  21. typename uE2oEType,
  22. typename uE2CType >
  23. IGL_INLINE
  24. typename std::enable_if<!std::is_same<typename DerivedV::Scalar,
  25. typename CGAL::Exact_predicates_exact_constructions_kernel::FT>::value, void>::type
  26. igl::cgal::order_facets_around_edges(
  27. const Eigen::PlainObjectBase<DerivedV>& V,
  28. const Eigen::PlainObjectBase<DerivedF>& F,
  29. const Eigen::PlainObjectBase<DerivedN>& N,
  30. const Eigen::PlainObjectBase<DerivedE>& E,
  31. const Eigen::PlainObjectBase<DeriveduE>& uE,
  32. const Eigen::PlainObjectBase<DerivedEMAP>& EMAP,
  33. const std::vector<std::vector<uE2EType> >& uE2E,
  34. std::vector<std::vector<uE2oEType> >& uE2oE,
  35. std::vector<std::vector<uE2CType > >& uE2C ) {
  36. typedef Eigen::Matrix<typename DerivedN::Scalar, 3, 1> Vector3F;
  37. const typename DerivedV::Scalar EPS = 1e-12;
  38. const size_t num_faces = F.rows();
  39. const size_t num_undirected_edges = uE.rows();
  40. auto edge_index_to_face_index = [&](size_t ei) { return ei % num_faces; };
  41. auto edge_index_to_corner_index = [&](size_t ei) { return ei / num_faces; };
  42. uE2oE.resize(num_undirected_edges);
  43. uE2C.resize(num_undirected_edges);
  44. for(size_t ui = 0;ui<num_undirected_edges;ui++)
  45. {
  46. const auto& adj_edges = uE2E[ui];
  47. const size_t edge_valance = adj_edges.size();
  48. assert(edge_valance > 0);
  49. const auto ref_edge = adj_edges[0];
  50. const auto ref_face = edge_index_to_face_index(ref_edge);
  51. Vector3F ref_normal = N.row(ref_face);
  52. const auto ref_corner_o = edge_index_to_corner_index(ref_edge);
  53. const auto ref_corner_s = (ref_corner_o+1)%3;
  54. const auto ref_corner_d = (ref_corner_o+2)%3;
  55. const typename DerivedF::Scalar o = F(ref_face, ref_corner_o);
  56. const typename DerivedF::Scalar s = F(ref_face, ref_corner_s);
  57. const typename DerivedF::Scalar d = F(ref_face, ref_corner_d);
  58. Vector3F edge = V.row(d) - V.row(s);
  59. auto edge_len = edge.norm();
  60. bool degenerated = edge_len < EPS;
  61. if (degenerated) {
  62. if (edge_valance <= 2) {
  63. // There is only one way to order 2 or less faces.
  64. edge.setZero();
  65. } else {
  66. edge.setZero();
  67. Eigen::Matrix<typename DerivedN::Scalar, Eigen::Dynamic, 3>
  68. normals(edge_valance, 3);
  69. for (size_t fei=0; fei<edge_valance; fei++) {
  70. const auto fe = adj_edges[fei];
  71. const auto f = edge_index_to_face_index(fe);
  72. normals.row(fei) = N.row(f);
  73. }
  74. for (size_t i=0; i<edge_valance; i++) {
  75. size_t j = (i+1) % edge_valance;
  76. Vector3F ni = normals.row(i);
  77. Vector3F nj = normals.row(j);
  78. edge = ni.cross(nj);
  79. edge_len = edge.norm();
  80. if (edge_len >= EPS) {
  81. edge.normalize();
  82. break;
  83. }
  84. }
  85. // Ensure edge direction are consistent with reference face.
  86. Vector3F in_face_vec = V.row(o) - V.row(s);
  87. if (edge.cross(in_face_vec).dot(ref_normal) < 0) {
  88. edge *= -1;
  89. }
  90. if (edge.norm() < EPS) {
  91. std::cerr << "=====================================" << std::endl;
  92. std::cerr << " ui: " << ui << std::endl;
  93. std::cerr << "edge: " << ref_edge << std::endl;
  94. std::cerr << "face: " << ref_face << std::endl;
  95. std::cerr << " vs: " << V.row(s) << std::endl;
  96. std::cerr << " vd: " << V.row(d) << std::endl;
  97. std::cerr << "adj face normals: " << std::endl;
  98. std::cerr << normals << std::endl;
  99. std::cerr << "Very degenerated case detected:" << std::endl;
  100. std::cerr << "Near zero edge surrounded by "
  101. << edge_valance << " neearly colinear faces" <<
  102. std::endl;
  103. std::cerr << "=====================================" << std::endl;
  104. }
  105. }
  106. } else {
  107. edge.normalize();
  108. }
  109. Eigen::MatrixXd angle_data(edge_valance, 3);
  110. std::vector<bool> cons(edge_valance);
  111. for (size_t fei=0; fei<edge_valance; fei++) {
  112. const auto fe = adj_edges[fei];
  113. const auto f = edge_index_to_face_index(fe);
  114. const auto c = edge_index_to_corner_index(fe);
  115. cons[fei] = (d == F(f, (c+1)%3));
  116. assert( cons[fei] || (d == F(f,(c+2)%3)));
  117. assert(!cons[fei] || (s == F(f,(c+2)%3)));
  118. assert(!cons[fei] || (d == F(f,(c+1)%3)));
  119. Vector3F n = N.row(f);
  120. angle_data(fei, 0) = ref_normal.cross(n).dot(edge);
  121. angle_data(fei, 1) = ref_normal.dot(n);
  122. if (cons[fei]) {
  123. angle_data(fei, 0) *= -1;
  124. angle_data(fei, 1) *= -1;
  125. }
  126. angle_data(fei, 0) *= -1; // Sort clockwise.
  127. angle_data(fei, 2) = (cons[fei]?1.:-1.)*(f+1);
  128. }
  129. Eigen::VectorXi order;
  130. igl::sort_angles(angle_data, order);
  131. auto& ordered_edges = uE2oE[ui];
  132. auto& consistency = uE2C[ui];
  133. ordered_edges.resize(edge_valance);
  134. consistency.resize(edge_valance);
  135. for (size_t fei=0; fei<edge_valance; fei++) {
  136. ordered_edges[fei] = adj_edges[order[fei]];
  137. consistency[fei] = cons[order[fei]];
  138. }
  139. }
  140. }
  141. template<
  142. typename DerivedV,
  143. typename DerivedF,
  144. typename DerivedN,
  145. typename DerivedE,
  146. typename DeriveduE,
  147. typename DerivedEMAP,
  148. typename uE2EType,
  149. typename uE2oEType,
  150. typename uE2CType >
  151. IGL_INLINE
  152. typename std::enable_if<std::is_same<typename DerivedV::Scalar,
  153. typename CGAL::Exact_predicates_exact_constructions_kernel::FT>::value, void>::type
  154. igl::cgal::order_facets_around_edges(
  155. const Eigen::PlainObjectBase<DerivedV>& V,
  156. const Eigen::PlainObjectBase<DerivedF>& F,
  157. const Eigen::PlainObjectBase<DerivedN>& N,
  158. const Eigen::PlainObjectBase<DerivedE>& E,
  159. const Eigen::PlainObjectBase<DeriveduE>& uE,
  160. const Eigen::PlainObjectBase<DerivedEMAP>& EMAP,
  161. const std::vector<std::vector<uE2EType> >& uE2E,
  162. std::vector<std::vector<uE2oEType> >& uE2oE,
  163. std::vector<std::vector<uE2CType > >& uE2C ) {
  164. typedef Eigen::Matrix<typename DerivedN::Scalar, 3, 1> Vector3F;
  165. typedef Eigen::Matrix<typename DerivedV::Scalar, 3, 1> Vector3E;
  166. const typename DerivedV::Scalar EPS = 1e-12;
  167. const size_t num_faces = F.rows();
  168. const size_t num_undirected_edges = uE.rows();
  169. auto edge_index_to_face_index = [&](size_t ei) { return ei % num_faces; };
  170. auto edge_index_to_corner_index = [&](size_t ei) { return ei / num_faces; };
  171. uE2oE.resize(num_undirected_edges);
  172. uE2C.resize(num_undirected_edges);
  173. for(size_t ui = 0;ui<num_undirected_edges;ui++)
  174. {
  175. const auto& adj_edges = uE2E[ui];
  176. const size_t edge_valance = adj_edges.size();
  177. assert(edge_valance > 0);
  178. const auto ref_edge = adj_edges[0];
  179. const auto ref_face = edge_index_to_face_index(ref_edge);
  180. Vector3F ref_normal = N.row(ref_face);
  181. const auto ref_corner_o = edge_index_to_corner_index(ref_edge);
  182. const auto ref_corner_s = (ref_corner_o+1)%3;
  183. const auto ref_corner_d = (ref_corner_o+2)%3;
  184. const typename DerivedF::Scalar o = F(ref_face, ref_corner_o);
  185. const typename DerivedF::Scalar s = F(ref_face, ref_corner_s);
  186. const typename DerivedF::Scalar d = F(ref_face, ref_corner_d);
  187. Vector3E exact_edge = V.row(d) - V.row(s);
  188. exact_edge.array() /= exact_edge.squaredNorm();
  189. Vector3F edge(
  190. CGAL::to_double(exact_edge[0]),
  191. CGAL::to_double(exact_edge[1]),
  192. CGAL::to_double(exact_edge[2]));
  193. edge.normalize();
  194. Eigen::MatrixXd angle_data(edge_valance, 3);
  195. std::vector<bool> cons(edge_valance);
  196. for (size_t fei=0; fei<edge_valance; fei++) {
  197. const auto fe = adj_edges[fei];
  198. const auto f = edge_index_to_face_index(fe);
  199. const auto c = edge_index_to_corner_index(fe);
  200. cons[fei] = (d == F(f, (c+1)%3));
  201. assert( cons[fei] || (d == F(f,(c+2)%3)));
  202. assert(!cons[fei] || (s == F(f,(c+2)%3)));
  203. assert(!cons[fei] || (d == F(f,(c+1)%3)));
  204. Vector3F n = N.row(f);
  205. angle_data(fei, 0) = ref_normal.cross(n).dot(edge);
  206. angle_data(fei, 1) = ref_normal.dot(n);
  207. if (cons[fei]) {
  208. angle_data(fei, 0) *= -1;
  209. angle_data(fei, 1) *= -1;
  210. }
  211. angle_data(fei, 0) *= -1; // Sort clockwise.
  212. angle_data(fei, 2) = (cons[fei]?1.:-1.)*(f+1);
  213. }
  214. Eigen::VectorXi order;
  215. igl::sort_angles(angle_data, order);
  216. auto& ordered_edges = uE2oE[ui];
  217. auto& consistency = uE2C[ui];
  218. ordered_edges.resize(edge_valance);
  219. consistency.resize(edge_valance);
  220. for (size_t fei=0; fei<edge_valance; fei++) {
  221. ordered_edges[fei] = adj_edges[order[fei]];
  222. consistency[fei] = cons[order[fei]];
  223. }
  224. }
  225. }
  226. template<
  227. typename DerivedV,
  228. typename DerivedF,
  229. typename DerivedE,
  230. typename DeriveduE,
  231. typename DerivedEMAP,
  232. typename uE2EType,
  233. typename uE2oEType,
  234. typename uE2CType >
  235. IGL_INLINE void igl::cgal::order_facets_around_edges(
  236. const Eigen::PlainObjectBase<DerivedV>& V,
  237. const Eigen::PlainObjectBase<DerivedF>& F,
  238. const Eigen::PlainObjectBase<DerivedE>& E,
  239. const Eigen::PlainObjectBase<DeriveduE>& uE,
  240. const Eigen::PlainObjectBase<DerivedEMAP>& EMAP,
  241. const std::vector<std::vector<uE2EType> >& uE2E,
  242. std::vector<std::vector<uE2oEType> >& uE2oE,
  243. std::vector<std::vector<uE2CType > >& uE2C ) {
  244. typedef Eigen::Matrix<typename DerivedV::Scalar, 3, 1> Vector3E;
  245. const size_t num_faces = F.rows();
  246. const size_t num_undirected_edges = uE.rows();
  247. auto edge_index_to_face_index = [&](size_t ei) { return ei % num_faces; };
  248. auto edge_index_to_corner_index = [&](size_t ei) { return ei / num_faces; };
  249. uE2oE.resize(num_undirected_edges);
  250. uE2C.resize(num_undirected_edges);
  251. for(size_t ui = 0;ui<num_undirected_edges;ui++)
  252. {
  253. const auto& adj_edges = uE2E[ui];
  254. const size_t edge_valance = adj_edges.size();
  255. assert(edge_valance > 0);
  256. const auto ref_edge = adj_edges[0];
  257. const auto ref_face = edge_index_to_face_index(ref_edge);
  258. const auto ref_corner_o = edge_index_to_corner_index(ref_edge);
  259. const auto ref_corner_s = (ref_corner_o+1)%3;
  260. const auto ref_corner_d = (ref_corner_o+2)%3;
  261. const typename DerivedF::Scalar o = F(ref_face, ref_corner_o);
  262. const typename DerivedF::Scalar s = F(ref_face, ref_corner_s);
  263. const typename DerivedF::Scalar d = F(ref_face, ref_corner_d);
  264. std::vector<bool> cons(edge_valance);
  265. std::vector<int> adj_faces(edge_valance);
  266. for (size_t fei=0; fei<edge_valance; fei++) {
  267. const auto fe = adj_edges[fei];
  268. const auto f = edge_index_to_face_index(fe);
  269. const auto c = edge_index_to_corner_index(fe);
  270. cons[fei] = (d == F(f, (c+1)%3));
  271. adj_faces[fei] = (f+1) * (cons[fei] ? 1:-1);
  272. assert( cons[fei] || (d == F(f,(c+2)%3)));
  273. assert(!cons[fei] || (s == F(f,(c+2)%3)));
  274. assert(!cons[fei] || (d == F(f,(c+1)%3)));
  275. }
  276. Eigen::VectorXi order;
  277. order_facets_around_edge(V, F, s, d, adj_faces, order);
  278. assert(order.size() == edge_valance);
  279. auto& ordered_edges = uE2oE[ui];
  280. auto& consistency = uE2C[ui];
  281. ordered_edges.resize(edge_valance);
  282. consistency.resize(edge_valance);
  283. for (size_t fei=0; fei<edge_valance; fei++) {
  284. ordered_edges[fei] = adj_edges[order[fei]];
  285. consistency[fei] = cons[order[fei]];
  286. }
  287. }
  288. }
  289. #ifdef IGL_STATIC_LIBRARY
  290. // Explicit template specialization
  291. template void igl::cgal::order_facets_around_edges<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, long, long, bool>(Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> > const&, std::__1::vector<std::__1::vector<long, std::__1::allocator<long> >, std::__1::allocator<std::__1::vector<long, std::__1::allocator<long> > > > const&, std::__1::vector<std::__1::vector<long, std::__1::allocator<long> >, std::__1::allocator<std::__1::vector<long, std::__1::allocator<long> > > >&, std::__1::vector<std::__1::vector<bool, std::__1::allocator<bool> >, std::__1::allocator<std::__1::vector<bool, std::__1::allocator<bool> > > >&);
  292. template void igl::cgal::order_facets_around_edges<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, long, long, bool>(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> > const&, std::__1::vector<std::__1::vector<long, std::__1::allocator<long> >, std::__1::allocator<std::__1::vector<long, std::__1::allocator<long> > > > const&, std::__1::vector<std::__1::vector<long, std::__1::allocator<long> >, std::__1::allocator<std::__1::vector<long, std::__1::allocator<long> > > >&, std::__1::vector<std::__1::vector<bool, std::__1::allocator<bool> >, std::__1::allocator<std::__1::vector<bool, std::__1::allocator<bool> > > >&);
  293. template void igl::cgal::order_facets_around_edges<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, long, long, bool>(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> > const&, std::__1::vector<std::__1::vector<long, std::__1::allocator<long> >, std::__1::allocator<std::__1::vector<long, std::__1::allocator<long> > > > const&, std::__1::vector<std::__1::vector<long, std::__1::allocator<long> >, std::__1::allocator<std::__1::vector<long, std::__1::allocator<long> > > >&, std::__1::vector<std::__1::vector<bool, std::__1::allocator<bool> >, std::__1::allocator<std::__1::vector<bool, std::__1::allocator<bool> > > >&);
  294. #endif