order_facets_around_edge.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "order_facets_around_edge.h"
  2. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  3. namespace igl
  4. {
  5. namespace cgal
  6. {
  7. namespace order_facets_around_edges_helper
  8. {
  9. template<typename T>
  10. std::vector<size_t> index_sort(const std::vector<T>& data)
  11. {
  12. const size_t len = data.size();
  13. std::vector<size_t> order(len);
  14. for (size_t i=0; i<len; i++)
  15. {
  16. order[i] = i;
  17. }
  18. auto comp = [&](size_t i, size_t j)
  19. {
  20. return data[i] < data[j];
  21. };
  22. std::sort(order.begin(), order.end(), comp);
  23. return order;
  24. }
  25. }
  26. }
  27. }
  28. // adj_faces contains signed index starting from +- 1.
  29. template<
  30. typename DerivedV,
  31. typename DerivedF,
  32. typename DerivedI >
  33. void igl::cgal::order_facets_around_edge(
  34. const Eigen::PlainObjectBase<DerivedV>& V,
  35. const Eigen::PlainObjectBase<DerivedF>& F,
  36. size_t s,
  37. size_t d,
  38. const std::vector<int>& adj_faces,
  39. Eigen::PlainObjectBase<DerivedI>& order)
  40. {
  41. using namespace igl::cgal::order_facets_around_edges_helper;
  42. // Although we only need exact predicates in the algorithm,
  43. // exact constructions are needed to avoid degeneracies due to
  44. // casting to double.
  45. typedef CGAL::Exact_predicates_exact_constructions_kernel K;
  46. typedef K::Point_3 Point_3;
  47. typedef K::Plane_3 Plane_3;
  48. auto get_face_index = [&](int adj_f)->size_t
  49. {
  50. return abs(adj_f) - 1;
  51. };
  52. auto get_opposite_vertex = [&](size_t fid)->size_t
  53. {
  54. typedef typename DerivedF::Scalar Index;
  55. if (F(fid, 0) != (Index)s && F(fid, 0) != (Index)d) return F(fid, 0);
  56. if (F(fid, 1) != (Index)s && F(fid, 1) != (Index)d) return F(fid, 1);
  57. if (F(fid, 2) != (Index)s && F(fid, 2) != (Index)d) return F(fid, 2);
  58. assert(false);
  59. return -1;
  60. };
  61. // Handle base cases
  62. if (adj_faces.size() == 0)
  63. {
  64. order.resize(0, 1);
  65. return;
  66. } else if (adj_faces.size() == 1)
  67. {
  68. order.resize(1, 1);
  69. order(0, 0) = 0;
  70. return;
  71. } else if (adj_faces.size() == 2)
  72. {
  73. const size_t o1 = get_opposite_vertex(get_face_index(adj_faces[0]));
  74. const size_t o2 = get_opposite_vertex(get_face_index(adj_faces[1]));
  75. const Point_3 ps(V(s, 0), V(s, 1), V(s, 2));
  76. const Point_3 pd(V(d, 0), V(d, 1), V(d, 2));
  77. const Point_3 p1(V(o1, 0), V(o1, 1), V(o1, 2));
  78. const Point_3 p2(V(o2, 0), V(o2, 1), V(o2, 2));
  79. order.resize(2, 1);
  80. switch (CGAL::orientation(ps, pd, p1, p2))
  81. {
  82. case CGAL::POSITIVE:
  83. order(0, 0) = 1;
  84. order(1, 0) = 0;
  85. break;
  86. case CGAL::NEGATIVE:
  87. order(0, 0) = 0;
  88. order(1, 0) = 1;
  89. break;
  90. case CGAL::COPLANAR:
  91. {
  92. Plane_3 P1(ps, pd, p1);
  93. Plane_3 P2(ps, pd, p2);
  94. if (P1.orthogonal_direction() == P2.orthogonal_direction()){
  95. // Duplicated face, use index to break tie.
  96. order(0, 0) = adj_faces[0] < adj_faces[1] ? 0:1;
  97. order(1, 0) = adj_faces[0] < adj_faces[1] ? 1:0;
  98. } else {
  99. // Coplanar faces, one on each side of the edge.
  100. // It is equally valid to order them (0, 1) or (1, 0).
  101. // I cannot think of any reason to prefer one to the
  102. // other. So just use (0, 1) ordering by default.
  103. order(0, 0) = 0;
  104. order(1, 0) = 1;
  105. }
  106. }
  107. break;
  108. default:
  109. assert(false);
  110. }
  111. return;
  112. }
  113. const size_t num_adj_faces = adj_faces.size();
  114. const size_t o = get_opposite_vertex( get_face_index(adj_faces[0]));
  115. const Point_3 p_s(V(s, 0), V(s, 1), V(s, 2));
  116. const Point_3 p_d(V(d, 0), V(d, 1), V(d, 2));
  117. const Point_3 p_o(V(o, 0), V(o, 1), V(o, 2));
  118. const Plane_3 separator(p_s, p_d, p_o);
  119. assert(!separator.is_degenerate());
  120. std::vector<Point_3> opposite_vertices;
  121. for (size_t i=0; i<num_adj_faces; i++)
  122. {
  123. const size_t o = get_opposite_vertex( get_face_index(adj_faces[i]));
  124. opposite_vertices.emplace_back(
  125. V(o, 0), V(o, 1), V(o, 2));
  126. }
  127. std::vector<int> positive_side;
  128. std::vector<int> negative_side;
  129. std::vector<int> tie_positive_oriented;
  130. std::vector<int> tie_negative_oriented;
  131. std::vector<size_t> positive_side_index;
  132. std::vector<size_t> negative_side_index;
  133. std::vector<size_t> tie_positive_oriented_index;
  134. std::vector<size_t> tie_negative_oriented_index;
  135. for (size_t i=0; i<num_adj_faces; i++)
  136. {
  137. const int f = adj_faces[i];
  138. const Point_3& p_a = opposite_vertices[i];
  139. auto orientation = separator.oriented_side(p_a);
  140. switch (orientation) {
  141. case CGAL::ON_POSITIVE_SIDE:
  142. positive_side.push_back(f);
  143. positive_side_index.push_back(i);
  144. break;
  145. case CGAL::ON_NEGATIVE_SIDE:
  146. negative_side.push_back(f);
  147. negative_side_index.push_back(i);
  148. break;
  149. case CGAL::ON_ORIENTED_BOUNDARY:
  150. {
  151. const Plane_3 other(p_s, p_d, p_a);
  152. const auto target_dir = separator.orthogonal_direction();
  153. const auto query_dir = other.orthogonal_direction();
  154. if (target_dir == query_dir) {
  155. tie_positive_oriented.push_back(f);
  156. tie_positive_oriented_index.push_back(i);
  157. } else if (target_dir == -query_dir) {
  158. tie_negative_oriented.push_back(f);
  159. tie_negative_oriented_index.push_back(i);
  160. } else {
  161. assert(false);
  162. }
  163. }
  164. break;
  165. default:
  166. // Should not be here.
  167. assert(false);
  168. }
  169. }
  170. Eigen::PlainObjectBase<DerivedI> positive_order, negative_order;
  171. order_facets_around_edge(V, F, s, d, positive_side, positive_order);
  172. order_facets_around_edge(V, F, s, d, negative_side, negative_order);
  173. std::vector<size_t> tie_positive_order = index_sort(tie_positive_oriented);
  174. std::vector<size_t> tie_negative_order = index_sort(tie_negative_oriented);
  175. // Copy results into order vector.
  176. const size_t tie_positive_size = tie_positive_oriented.size();
  177. const size_t tie_negative_size = tie_negative_oriented.size();
  178. const size_t positive_size = positive_order.size();
  179. const size_t negative_size = negative_order.size();
  180. order.resize(
  181. tie_positive_size + positive_size + tie_negative_size + negative_size,1);
  182. size_t count=0;
  183. for (size_t i=0; i<tie_positive_size; i++)
  184. {
  185. order(count+i, 0) = tie_positive_oriented_index[tie_positive_order[i]];
  186. }
  187. count += tie_positive_size;
  188. for (size_t i=0; i<negative_size; i++)
  189. {
  190. order(count+i, 0) = negative_side_index[negative_order(i, 0)];
  191. }
  192. count += negative_size;
  193. for (size_t i=0; i<tie_negative_size; i++)
  194. {
  195. order(count+i, 0) = tie_negative_oriented_index[tie_negative_order[i]];
  196. }
  197. count += tie_negative_size;
  198. for (size_t i=0; i<positive_size; i++)
  199. {
  200. order(count+i, 0) = positive_side_index[positive_order(i, 0)];
  201. }
  202. count += positive_size;
  203. assert(count == num_adj_faces);
  204. // Find the correct start point.
  205. size_t start_idx = 0;
  206. for (size_t i=0; i<num_adj_faces; i++)
  207. {
  208. const Point_3& p_a = opposite_vertices[order(i, 0)];
  209. const Point_3& p_b =
  210. opposite_vertices[order((i+1)%num_adj_faces, 0)];
  211. auto orientation = CGAL::orientation(p_s, p_d, p_a, p_b);
  212. if (orientation == CGAL::POSITIVE)
  213. {
  214. // Angle between triangle (p_s, p_d, p_a) and (p_s, p_d, p_b) is
  215. // more than 180 degrees.
  216. start_idx = (i+1)%num_adj_faces;
  217. break;
  218. } else if (orientation == CGAL::COPLANAR &&
  219. Plane_3(p_s, p_d, p_a).orthogonal_direction() !=
  220. Plane_3(p_s, p_d, p_b).orthogonal_direction())
  221. {
  222. // All 4 points are coplanar, but p_a and p_b are on each side of
  223. // the edge (p_s, p_d). This means the angle between triangle
  224. // (p_s, p_d, p_a) and (p_s, p_d, p_b) is exactly 180 degrees.
  225. start_idx = (i+1)%num_adj_faces;
  226. break;
  227. }
  228. }
  229. DerivedI circular_order = order;
  230. for (size_t i=0; i<num_adj_faces; i++)
  231. {
  232. order(i, 0) = circular_order((start_idx + i)%num_adj_faces, 0);
  233. }
  234. }
  235. template<
  236. typename DerivedV,
  237. typename DerivedF,
  238. typename DerivedI>
  239. IGL_INLINE
  240. void igl::cgal::order_facets_around_edge(
  241. const Eigen::PlainObjectBase<DerivedV>& V,
  242. const Eigen::PlainObjectBase<DerivedF>& F,
  243. size_t s,
  244. size_t d,
  245. const std::vector<int>& adj_faces,
  246. const Eigen::PlainObjectBase<DerivedV>& pivot_point,
  247. Eigen::PlainObjectBase<DerivedI>& order)
  248. {
  249. assert(V.cols() == 3);
  250. assert(F.cols() == 3);
  251. auto signed_index_to_index = [&](int signed_idx)
  252. {
  253. return abs(signed_idx) -1;
  254. };
  255. auto get_opposite_vertex_index = [&](size_t fid)
  256. {
  257. typedef typename DerivedF::Scalar Index;
  258. if (F(fid, 0) != (Index)s && F(fid, 0) != (Index)d) return F(fid, 0);
  259. if (F(fid, 1) != (Index)s && F(fid, 1) != (Index)d) return F(fid, 1);
  260. if (F(fid, 2) != (Index)s && F(fid, 2) != (Index)d) return F(fid, 2);
  261. assert(false);
  262. // avoid warning
  263. return -1;
  264. };
  265. const typename DerivedF::Scalar N = adj_faces.size();
  266. const size_t num_faces = N + 1; // N adj faces + 1 pivot face
  267. DerivedV vertices(num_faces + 2, 3);
  268. for (size_t i=0; i<(size_t)N; i++)
  269. {
  270. const size_t fid = signed_index_to_index(adj_faces[i]);
  271. vertices.row(i) = V.row(get_opposite_vertex_index(fid));
  272. }
  273. vertices.row(N ) = pivot_point;
  274. vertices.row(N+1) = V.row(s);
  275. vertices.row(N+2) = V.row(d);
  276. DerivedF faces(num_faces, 3);
  277. for (size_t i=0; i<(size_t)N; i++)
  278. {
  279. if (adj_faces[i] < 0)
  280. {
  281. faces(i,0) = N+1; // s
  282. faces(i,1) = N+2; // d
  283. faces(i,2) = i ;
  284. } else {
  285. faces(i,0) = N+2; // d
  286. faces(i,1) = N+1; // s
  287. faces(i,2) = i ;
  288. }
  289. }
  290. // Last face is the pivot face.
  291. faces(N, 0) = N+1;
  292. faces(N, 1) = N+2;
  293. faces(N, 2) = N;
  294. std::vector<int> adj_faces_with_pivot(num_faces);
  295. for (size_t i=0; i<num_faces; i++)
  296. {
  297. if (faces(i,0) == N+1 && faces(i,1) == N+2)
  298. {
  299. adj_faces_with_pivot[i] = int(i+1) * -1;
  300. } else
  301. {
  302. adj_faces_with_pivot[i] = int(i+1);
  303. }
  304. }
  305. DerivedI order_with_pivot;
  306. igl::cgal::order_facets_around_edge(
  307. vertices, faces, N+1, N+2,
  308. adj_faces_with_pivot, order_with_pivot);
  309. assert(order_with_pivot.size() == num_faces);
  310. order.resize(N);
  311. size_t pivot_index = num_faces + 1;
  312. for (size_t i=0; i<num_faces; i++)
  313. {
  314. if (order_with_pivot[i] == N)
  315. {
  316. pivot_index = i;
  317. break;
  318. }
  319. }
  320. assert(pivot_index < num_faces);
  321. for (size_t i=0; i<(size_t)N; i++)
  322. {
  323. order[i] = order_with_pivot[(pivot_index+i+1)%num_faces];
  324. }
  325. }
  326. #ifdef IGL_STATIC_LIBRARY
  327. // Explicit template specialization
  328. template void igl::cgal::order_facets_around_edge<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, 1, 0, -1, 1> >(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&, unsigned long, unsigned long, std::__1::vector<int, std::__1::allocator<int> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  329. template void igl::cgal::order_facets_around_edge<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, unsigned long, unsigned long, std::__1::vector<int, std::__1::allocator<int> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  330. template void igl::cgal::order_facets_around_edge<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, unsigned long, unsigned long, std::__1::vector<int, std::__1::allocator<int> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  331. template void igl::cgal::order_facets_around_edge<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, 1, 0, -1, 1> >(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&, unsigned long, unsigned long, std::__1::vector<int, std::__1::allocator<int> > const&, Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  332. template void igl::cgal::order_facets_around_edge<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, unsigned long, unsigned long, std::__1::vector<int, std::__1::allocator<int> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  333. template void igl::cgal::order_facets_around_edge<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, unsigned long, unsigned long, std::__1::vector<int, std::__1::allocator<int> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  334. #endif