extract_cells.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Qingnan Zhou <qnzhou@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. //
  9. #include "extract_cells.h"
  10. #include "../../extract_manifold_patches.h"
  11. #include "../../facet_components.h"
  12. #include "../../triangle_triangle_adjacency.h"
  13. #include "../../unique_edge_map.h"
  14. #include "../../get_seconds.h"
  15. #include "closest_facet.h"
  16. #include "order_facets_around_edge.h"
  17. #include "outer_facet.h"
  18. #include <vector>
  19. #include <queue>
  20. //#define EXTRACT_CELLS_DEBUG
  21. template<
  22. typename DerivedV,
  23. typename DerivedF,
  24. typename DerivedP,
  25. typename DeriveduE,
  26. typename uE2EType,
  27. typename DerivedEMAP,
  28. typename DerivedC >
  29. IGL_INLINE size_t igl::copyleft::cgal::extract_cells_single_component(
  30. const Eigen::PlainObjectBase<DerivedV>& V,
  31. const Eigen::PlainObjectBase<DerivedF>& F,
  32. const Eigen::PlainObjectBase<DerivedP>& P,
  33. const Eigen::PlainObjectBase<DeriveduE>& uE,
  34. const std::vector<std::vector<uE2EType> >& uE2E,
  35. const Eigen::PlainObjectBase<DerivedEMAP>& EMAP,
  36. Eigen::PlainObjectBase<DerivedC>& cells) {
  37. //typedef typename DerivedF::Scalar Index;
  38. const size_t num_faces = F.rows();
  39. auto edge_index_to_face_index = [&](size_t index) {
  40. return index % num_faces;
  41. };
  42. auto is_consistent = [&](const size_t fid, const size_t s, const size_t d) {
  43. if ((size_t)F(fid, 0) == s && (size_t)F(fid, 1) == d) return false;
  44. if ((size_t)F(fid, 1) == s && (size_t)F(fid, 2) == d) return false;
  45. if ((size_t)F(fid, 2) == s && (size_t)F(fid, 0) == d) return false;
  46. if ((size_t)F(fid, 0) == d && (size_t)F(fid, 1) == s) return true;
  47. if ((size_t)F(fid, 1) == d && (size_t)F(fid, 2) == s) return true;
  48. if ((size_t)F(fid, 2) == d && (size_t)F(fid, 0) == s) return true;
  49. throw "Invalid face!";
  50. return false;
  51. };
  52. const size_t num_unique_edges = uE.rows();
  53. const size_t num_patches = P.maxCoeff() + 1;
  54. std::vector<std::vector<size_t> > patch_edge_adj(num_patches);
  55. std::vector<Eigen::VectorXi> orders(num_unique_edges);
  56. std::vector<std::vector<bool> > orientations(num_unique_edges);
  57. for (size_t i=0; i<num_unique_edges; i++) {
  58. const size_t s = uE(i,0);
  59. const size_t d = uE(i,1);
  60. const auto adj_faces = uE2E[i];
  61. if (adj_faces.size() > 2) {
  62. std::vector<int> signed_adj_faces;
  63. for (auto ei : adj_faces) {
  64. const size_t fid = edge_index_to_face_index(ei);
  65. bool cons = is_consistent(fid, s, d);
  66. signed_adj_faces.push_back((fid+1)*(cons ? 1:-1));
  67. }
  68. auto& order = orders[i];
  69. igl::copyleft::cgal::order_facets_around_edge(
  70. V, F, s, d, signed_adj_faces, order);
  71. auto& orientation = orientations[i];
  72. orientation.resize(order.size());
  73. std::transform(order.data(), order.data() + order.size(),
  74. orientation.begin(), [&](int index) { return
  75. signed_adj_faces[index] > 0; });
  76. std::transform(order.data(), order.data() + order.size(),
  77. order.data(), [&](int index) { return adj_faces[index]; });
  78. for (auto ei : adj_faces) {
  79. const size_t fid = edge_index_to_face_index(ei);
  80. patch_edge_adj[P[fid]].push_back(ei);
  81. }
  82. }
  83. }
  84. const int INVALID = std::numeric_limits<int>::max();
  85. cells.resize(num_patches, 2);
  86. cells.setConstant(INVALID);
  87. auto peel_cell_bd = [&](
  88. size_t seed_patch_id,
  89. short seed_patch_side,
  90. size_t cell_idx) {
  91. typedef std::pair<size_t, short> PatchSide;
  92. std::queue<PatchSide> Q;
  93. Q.emplace(seed_patch_id, seed_patch_side);
  94. cells(seed_patch_id, seed_patch_side) = cell_idx;
  95. while (!Q.empty()) {
  96. auto entry = Q.front();
  97. Q.pop();
  98. const size_t patch_id = entry.first;
  99. const short side = entry.second;
  100. const auto& adj_edges = patch_edge_adj[patch_id];
  101. for (const auto& ei : adj_edges) {
  102. const size_t uei = EMAP[ei];
  103. const auto& order = orders[uei];
  104. const auto& orientation = orientations[uei];
  105. const size_t edge_valance = order.size();
  106. size_t curr_i = 0;
  107. for (curr_i=0; curr_i < edge_valance; curr_i++) {
  108. if ((size_t)order[curr_i] == ei) break;
  109. }
  110. assert(curr_i < edge_valance);
  111. const bool cons = orientation[curr_i];
  112. size_t next;
  113. if (side == 0) {
  114. next = (cons ? (curr_i + 1) :
  115. (curr_i + edge_valance - 1)) % edge_valance;
  116. } else {
  117. next = (cons ? (curr_i + edge_valance - 1) :
  118. (curr_i + 1)) % edge_valance;
  119. }
  120. const size_t next_ei = order[next];
  121. const bool next_cons = orientation[next];
  122. const size_t next_patch_id = P[next_ei % num_faces];
  123. const short next_patch_side = (next_cons != cons) ?
  124. side:abs(side-1);
  125. if (cells(next_patch_id, next_patch_side) == INVALID) {
  126. Q.emplace(next_patch_id, next_patch_side);
  127. cells(next_patch_id, next_patch_side) = cell_idx;
  128. } else {
  129. assert(
  130. (size_t)cells(next_patch_id, next_patch_side) ==
  131. cell_idx);
  132. }
  133. }
  134. }
  135. };
  136. size_t count=0;
  137. for (size_t i=0; i<num_patches; i++) {
  138. if (cells(i, 0) == INVALID) {
  139. peel_cell_bd(i, 0, count);
  140. count++;
  141. }
  142. if (cells(i, 1) == INVALID) {
  143. peel_cell_bd(i, 1, count);
  144. count++;
  145. }
  146. }
  147. return count;
  148. }
  149. template<
  150. typename DerivedV,
  151. typename DerivedF,
  152. typename DerivedP,
  153. typename DerivedE,
  154. typename DeriveduE,
  155. typename uE2EType,
  156. typename DerivedEMAP,
  157. typename DerivedC >
  158. IGL_INLINE size_t igl::copyleft::cgal::extract_cells(
  159. const Eigen::PlainObjectBase<DerivedV>& V,
  160. const Eigen::PlainObjectBase<DerivedF>& F,
  161. const Eigen::PlainObjectBase<DerivedP>& P,
  162. const Eigen::PlainObjectBase<DerivedE>& E,
  163. const Eigen::PlainObjectBase<DeriveduE>& uE,
  164. const std::vector<std::vector<uE2EType> >& uE2E,
  165. const Eigen::PlainObjectBase<DerivedEMAP>& EMAP,
  166. Eigen::PlainObjectBase<DerivedC>& cells) {
  167. #ifdef EXTRACT_CELLS_DEBUG
  168. const auto & tictoc = []()
  169. {
  170. static double t_start = igl::get_seconds();
  171. double diff = igl::get_seconds()-t_start;
  172. t_start += diff;
  173. return diff;
  174. };
  175. tictoc();
  176. #endif
  177. const size_t num_faces = F.rows();
  178. typedef typename DerivedF::Scalar Index;
  179. const size_t num_patches = P.maxCoeff()+1;
  180. DerivedC raw_cells;
  181. const size_t num_raw_cells =
  182. igl::copyleft::cgal::extract_cells_single_component(
  183. V, F, P, uE, uE2E, EMAP, raw_cells);
  184. #ifdef EXTRACT_CELLS_DEBUG
  185. std::cout << "Extract single component cells: " << tictoc() << std::endl;
  186. #endif
  187. std::vector<std::vector<std::vector<Index > > > TT,_1;
  188. igl::triangle_triangle_adjacency(E, EMAP, uE2E, false, TT, _1);
  189. #ifdef EXTRACT_CELLS_DEBUG
  190. std::cout << "face adj: " << tictoc() << std::endl;
  191. #endif
  192. Eigen::VectorXi C, counts;
  193. igl::facet_components(TT, C, counts);
  194. #ifdef EXTRACT_CELLS_DEBUG
  195. std::cout << "face comp: " << tictoc() << std::endl;
  196. #endif
  197. const size_t num_components = counts.size();
  198. std::vector<std::vector<size_t> > components(num_components);
  199. for (size_t i=0; i<num_faces; i++) {
  200. components[C[i]].push_back(i);
  201. }
  202. std::vector<Eigen::VectorXi> Is(num_components);
  203. for (size_t i=0; i<num_components; i++) {
  204. Is[i].resize(components[i].size());
  205. std::copy(components[i].begin(), components[i].end(),
  206. Is[i].data());
  207. }
  208. Eigen::VectorXi outer_facets(num_components);
  209. Eigen::VectorXi outer_facet_orientation(num_components);
  210. Eigen::VectorXi outer_cells(num_components);
  211. for (size_t i=0; i<num_components; i++) {
  212. bool flipped;
  213. igl::copyleft::cgal::outer_facet(V, F, Is[i], outer_facets[i], flipped);
  214. outer_facet_orientation[i] = flipped?1:0;
  215. outer_cells[i] = raw_cells(P[outer_facets[i]], outer_facet_orientation[i]);
  216. }
  217. #ifdef EXTRACT_CELLS_DEBUG
  218. std::cout << "Per comp outer facet: " << tictoc() << std::endl;
  219. #endif
  220. auto get_triangle_center = [&](const size_t fid) {
  221. return ((V.row(F(fid, 0)) + V.row(F(fid, 1)) + V.row(F(fid, 2)))
  222. /3.0).eval();
  223. };
  224. std::vector<std::vector<size_t> > nested_cells(num_raw_cells);
  225. std::vector<std::vector<size_t> > ambient_cells(num_raw_cells);
  226. std::vector<std::vector<size_t> > ambient_comps(num_components);
  227. if (num_components > 1) {
  228. DerivedV bbox_min(num_components, 3);
  229. DerivedV bbox_max(num_components, 3);
  230. bbox_min.rowwise() = V.colwise().maxCoeff().eval();
  231. bbox_max.rowwise() = V.colwise().minCoeff().eval();
  232. for (size_t i=0; i<num_faces; i++) {
  233. const auto comp_id = C[i];
  234. const auto& f = F.row(i);
  235. for (size_t j=0; j<3; j++) {
  236. bbox_min(comp_id, 0) = std::min(bbox_min(comp_id, 0), V(f[j], 0));
  237. bbox_min(comp_id, 1) = std::min(bbox_min(comp_id, 1), V(f[j], 1));
  238. bbox_min(comp_id, 2) = std::min(bbox_min(comp_id, 2), V(f[j], 2));
  239. bbox_max(comp_id, 0) = std::max(bbox_max(comp_id, 0), V(f[j], 0));
  240. bbox_max(comp_id, 1) = std::max(bbox_max(comp_id, 1), V(f[j], 1));
  241. bbox_max(comp_id, 2) = std::max(bbox_max(comp_id, 2), V(f[j], 2));
  242. }
  243. }
  244. auto bbox_intersects = [&](size_t comp_i, size_t comp_j) {
  245. return !(
  246. bbox_max(comp_i,0) < bbox_min(comp_j,0) ||
  247. bbox_max(comp_i,1) < bbox_min(comp_j,1) ||
  248. bbox_max(comp_i,2) < bbox_min(comp_j,2) ||
  249. bbox_max(comp_j,0) < bbox_min(comp_i,0) ||
  250. bbox_max(comp_j,1) < bbox_min(comp_i,1) ||
  251. bbox_max(comp_j,2) < bbox_min(comp_i,2));
  252. };
  253. for (size_t i=0; i<num_components; i++) {
  254. std::vector<size_t> candidate_comps;
  255. candidate_comps.reserve(num_components);
  256. for (size_t j=0; j<num_components; j++) {
  257. if (i == j) continue;
  258. if (bbox_intersects(i,j)) candidate_comps.push_back(j);
  259. }
  260. const size_t num_candidate_comps = candidate_comps.size();
  261. if (num_candidate_comps == 0) continue;
  262. DerivedV queries(num_candidate_comps, 3);
  263. for (size_t j=0; j<num_candidate_comps; j++) {
  264. const size_t index = candidate_comps[j];
  265. queries.row(j) = get_triangle_center(outer_facets[index]);
  266. }
  267. const auto& I = Is[i];
  268. Eigen::VectorXi closest_facets, closest_facet_orientations;
  269. igl::copyleft::cgal::closest_facet(V, F, I, queries,
  270. uE2E, EMAP, closest_facets, closest_facet_orientations);
  271. for (size_t j=0; j<num_candidate_comps; j++) {
  272. const size_t index = candidate_comps[j];
  273. const size_t closest_patch = P[closest_facets[j]];
  274. const size_t closest_patch_side = closest_facet_orientations[j]
  275. ? 0:1;
  276. const size_t ambient_cell = raw_cells(closest_patch,
  277. closest_patch_side);
  278. if (ambient_cell != (size_t)outer_cells[i]) {
  279. nested_cells[ambient_cell].push_back(outer_cells[index]);
  280. ambient_cells[outer_cells[index]].push_back(ambient_cell);
  281. ambient_comps[index].push_back(i);
  282. }
  283. }
  284. }
  285. }
  286. #ifdef EXTRACT_CELLS_DEBUG
  287. std::cout << "Determine nested relaitonship: " << tictoc() << std::endl;
  288. #endif
  289. const size_t INVALID = std::numeric_limits<size_t>::max();
  290. const size_t INFINITE_CELL = num_raw_cells;
  291. std::vector<size_t> embedded_cells(num_raw_cells, INVALID);
  292. for (size_t i=0; i<num_components; i++) {
  293. const size_t outer_cell = outer_cells[i];
  294. const auto& ambient_comps_i = ambient_comps[i];
  295. const auto& ambient_cells_i = ambient_cells[outer_cell];
  296. const size_t num_ambient_comps = ambient_comps_i.size();
  297. assert(num_ambient_comps == ambient_cells_i.size());
  298. if (num_ambient_comps > 0) {
  299. size_t embedded_comp = INVALID;
  300. size_t embedded_cell = INVALID;
  301. for (size_t j=0; j<num_ambient_comps; j++) {
  302. if (ambient_comps[ambient_comps_i[j]].size() ==
  303. num_ambient_comps-1) {
  304. embedded_comp = ambient_comps_i[j];
  305. embedded_cell = ambient_cells_i[j];
  306. break;
  307. }
  308. }
  309. assert(embedded_comp != INVALID);
  310. assert(embedded_cell != INVALID);
  311. embedded_cells[outer_cell] = embedded_cell;
  312. } else {
  313. embedded_cells[outer_cell] = INFINITE_CELL;
  314. }
  315. }
  316. for (size_t i=0; i<num_patches; i++) {
  317. if (embedded_cells[raw_cells(i,0)] != INVALID) {
  318. raw_cells(i,0) = embedded_cells[raw_cells(i, 0)];
  319. }
  320. if (embedded_cells[raw_cells(i,1)] != INVALID) {
  321. raw_cells(i,1) = embedded_cells[raw_cells(i, 1)];
  322. }
  323. }
  324. size_t count = 0;
  325. std::vector<size_t> mapped_indices(num_raw_cells+1, INVALID);
  326. for (size_t i=0; i<num_patches; i++) {
  327. const size_t old_positive_cell_id = raw_cells(i, 0);
  328. const size_t old_negative_cell_id = raw_cells(i, 1);
  329. size_t positive_cell_id, negative_cell_id;
  330. if (mapped_indices[old_positive_cell_id] == INVALID) {
  331. mapped_indices[old_positive_cell_id] = count;
  332. positive_cell_id = count;
  333. count++;
  334. } else {
  335. positive_cell_id = mapped_indices[old_positive_cell_id];
  336. }
  337. if (mapped_indices[old_negative_cell_id] == INVALID) {
  338. mapped_indices[old_negative_cell_id] = count;
  339. negative_cell_id = count;
  340. count++;
  341. } else {
  342. negative_cell_id = mapped_indices[old_negative_cell_id];
  343. }
  344. raw_cells(i, 0) = positive_cell_id;
  345. raw_cells(i, 1) = negative_cell_id;
  346. }
  347. cells = raw_cells;
  348. #ifdef EXTRACT_CELLS_DEBUG
  349. std::cout << "Finalize and output: " << tictoc() << std::endl;
  350. #endif
  351. return count;
  352. }
  353. template<
  354. typename DerivedV,
  355. typename DerivedF,
  356. typename DerivedC >
  357. IGL_INLINE size_t igl::copyleft::cgal::extract_cells(
  358. const Eigen::PlainObjectBase<DerivedV>& V,
  359. const Eigen::PlainObjectBase<DerivedF>& F,
  360. Eigen::PlainObjectBase<DerivedC>& cells) {
  361. const size_t num_faces = F.rows();
  362. //typedef typename DerivedF::Scalar Index;
  363. Eigen::MatrixXi E, uE;
  364. Eigen::VectorXi EMAP;
  365. std::vector<std::vector<size_t> > uE2E;
  366. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  367. Eigen::VectorXi P;
  368. //const size_t num_patches =
  369. igl::extract_manifold_patches(F, EMAP, uE2E, P);
  370. DerivedC per_patch_cells;
  371. const size_t num_cells =
  372. igl::copyleft::cgal::extract_cells(V, F, P, E, uE, uE2E, EMAP, per_patch_cells);
  373. cells.resize(num_faces, 2);
  374. for (size_t i=0; i<num_faces; i++) {
  375. cells.row(i) = per_patch_cells.row(P[i]);
  376. }
  377. return num_cells;
  378. }
  379. #ifdef IGL_STATIC_LIBRARY
  380. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  381. template unsigned long igl::copyleft::cgal::extract_cells<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, unsigned long, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, std::vector<std::vector<unsigned long, std::allocator<unsigned long> >, std::allocator<std::vector<unsigned long, std::allocator<unsigned long> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  382. #endif