extract_cells.cpp 17 KB

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