propagate_winding_numbers.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "propagate_winding_numbers.h"
  10. #include "../../extract_manifold_patches.h"
  11. #include "../../extract_non_manifold_edge_curves.h"
  12. #include "../../facet_components.h"
  13. #include "../../unique_edge_map.h"
  14. #include "../../piecewise_constant_winding_number.h"
  15. #include "../../writeOBJ.h"
  16. #include "../../writePLY.h"
  17. #include "../../get_seconds.h"
  18. #include "order_facets_around_edge.h"
  19. #include "outer_facet.h"
  20. #include "closest_facet.h"
  21. #include "assign_scalar.h"
  22. #include "extract_cells.h"
  23. #include "cell_adjacency.h"
  24. #include <stdexcept>
  25. #include <limits>
  26. #include <vector>
  27. #include <tuple>
  28. #include <queue>
  29. //#define PROPAGATE_WINDING_NUMBER_TIMING
  30. template<
  31. typename DerivedV,
  32. typename DerivedF,
  33. typename DerivedL,
  34. typename DerivedW>
  35. IGL_INLINE bool igl::copyleft::cgal::propagate_winding_numbers(
  36. const Eigen::PlainObjectBase<DerivedV>& V,
  37. const Eigen::PlainObjectBase<DerivedF>& F,
  38. const Eigen::PlainObjectBase<DerivedL>& labels,
  39. Eigen::PlainObjectBase<DerivedW>& W)
  40. {
  41. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  42. const auto & tictoc = []() -> double
  43. {
  44. static double t_start = igl::get_seconds();
  45. double diff = igl::get_seconds()-t_start;
  46. t_start += diff;
  47. return diff;
  48. };
  49. const auto log_time = [&](const std::string& label) -> void {
  50. std::cout << "propagate_winding_num." << label << ": "
  51. << tictoc() << std::endl;
  52. };
  53. tictoc();
  54. #endif
  55. Eigen::MatrixXi E, uE;
  56. Eigen::VectorXi EMAP;
  57. std::vector<std::vector<size_t> > uE2E;
  58. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  59. Eigen::VectorXi P;
  60. const size_t num_patches = igl::extract_manifold_patches(F, EMAP, uE2E, P);
  61. DerivedW per_patch_cells;
  62. const size_t num_cells =
  63. igl::copyleft::cgal::extract_cells(
  64. V, F, P, E, uE, uE2E, EMAP, per_patch_cells);
  65. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  66. log_time("cell_extraction");
  67. #endif
  68. return propagate_winding_numbers(V, F,
  69. uE, uE2E,
  70. num_patches, P,
  71. num_cells, per_patch_cells,
  72. labels, W);
  73. }
  74. template<
  75. typename DerivedV,
  76. typename DerivedF,
  77. typename DeriveduE,
  78. typename uE2EType,
  79. typename DerivedP,
  80. typename DerivedC,
  81. typename DerivedL,
  82. typename DerivedW>
  83. IGL_INLINE bool igl::copyleft::cgal::propagate_winding_numbers(
  84. const Eigen::PlainObjectBase<DerivedV>& V,
  85. const Eigen::PlainObjectBase<DerivedF>& F,
  86. const Eigen::PlainObjectBase<DeriveduE>& uE,
  87. const std::vector<std::vector<uE2EType> >& uE2E,
  88. const size_t num_patches,
  89. const Eigen::PlainObjectBase<DerivedP>& P,
  90. const size_t num_cells,
  91. const Eigen::PlainObjectBase<DerivedC>& C,
  92. const Eigen::PlainObjectBase<DerivedL>& labels,
  93. Eigen::PlainObjectBase<DerivedW>& W)
  94. {
  95. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  96. const auto & tictoc = []() -> double
  97. {
  98. static double t_start = igl::get_seconds();
  99. double diff = igl::get_seconds()-t_start;
  100. t_start += diff;
  101. return diff;
  102. };
  103. const auto log_time = [&](const std::string& label) -> void {
  104. std::cout << "propagate_winding_num." << label << ": "
  105. << tictoc() << std::endl;
  106. };
  107. tictoc();
  108. #endif
  109. bool valid = true;
  110. if (!piecewise_constant_winding_number(F, uE, uE2E))
  111. {
  112. std::cerr << "Input mesh is not orientable!" << std::endl;
  113. valid = false;
  114. }
  115. const size_t num_faces = F.rows();
  116. typedef std::tuple<typename DerivedC::Scalar, bool, size_t> CellConnection;
  117. std::vector<std::set<CellConnection> > cell_adj;
  118. igl::copyleft::cgal::cell_adjacency(C, num_cells, cell_adj);
  119. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  120. log_time("cell_connectivity");
  121. #endif
  122. auto save_cell = [&](const std::string& filename, size_t cell_id) -> void{
  123. std::vector<size_t> faces;
  124. for (size_t i=0; i<num_patches; i++) {
  125. if ((C.row(i).array() == cell_id).any()) {
  126. for (size_t j=0; j<num_faces; j++) {
  127. if ((size_t)P[j] == i) {
  128. faces.push_back(j);
  129. }
  130. }
  131. }
  132. }
  133. Eigen::MatrixXi cell_faces(faces.size(), 3);
  134. for (size_t i=0; i<faces.size(); i++) {
  135. cell_faces.row(i) = F.row(faces[i]);
  136. }
  137. Eigen::MatrixXd vertices(V.rows(), 3);
  138. for (size_t i=0; i<(size_t)V.rows(); i++) {
  139. assign_scalar(V(i,0), vertices(i,0));
  140. assign_scalar(V(i,1), vertices(i,1));
  141. assign_scalar(V(i,2), vertices(i,2));
  142. }
  143. writePLY(filename, vertices, cell_faces);
  144. };
  145. #ifndef NDEBUG
  146. {
  147. // Check for odd cycle.
  148. Eigen::VectorXi cell_labels(num_cells);
  149. cell_labels.setZero();
  150. Eigen::VectorXi parents(num_cells);
  151. parents.setConstant(-1);
  152. auto trace_parents = [&](size_t idx) -> std::list<size_t> {
  153. std::list<size_t> path;
  154. path.push_back(idx);
  155. while ((size_t)parents[path.back()] != path.back()) {
  156. path.push_back(parents[path.back()]);
  157. }
  158. return path;
  159. };
  160. for (size_t i=0; i<num_cells; i++) {
  161. if (cell_labels[i] == 0) {
  162. cell_labels[i] = 1;
  163. std::queue<size_t> Q;
  164. Q.push(i);
  165. parents[i] = i;
  166. while (!Q.empty()) {
  167. size_t curr_idx = Q.front();
  168. Q.pop();
  169. int curr_label = cell_labels[curr_idx];
  170. for (const auto& neighbor : cell_adj[curr_idx]) {
  171. if (cell_labels[std::get<0>(neighbor)] == 0)
  172. {
  173. cell_labels[std::get<0>(neighbor)] = curr_label * -1;
  174. Q.push(std::get<0>(neighbor));
  175. parents[std::get<0>(neighbor)] = curr_idx;
  176. } else
  177. {
  178. if (cell_labels[std::get<0>(neighbor)] != curr_label * -1)
  179. {
  180. std::cerr << "Odd cell cycle detected!" << std::endl;
  181. auto path = trace_parents(curr_idx);
  182. path.reverse();
  183. auto path2 = trace_parents(std::get<0>(neighbor));
  184. path.insert(path.end(), path2.begin(), path2.end());
  185. for (auto cell_id : path)
  186. {
  187. std::cout << cell_id << " ";
  188. std::stringstream filename;
  189. filename << "cell_" << cell_id << ".ply";
  190. save_cell(filename.str(), cell_id);
  191. }
  192. std::cout << std::endl;
  193. valid = false;
  194. }
  195. // Do not fail when odd cycle is detected because the resulting
  196. // integer winding number field, although inconsistent, may still
  197. // be used if the problem region is local and embedded within a
  198. // valid volume.
  199. //assert(cell_labels[std::get<0>(neighbor)] == curr_label * -1);
  200. }
  201. }
  202. }
  203. }
  204. }
  205. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  206. log_time("odd_cycle_check");
  207. #endif
  208. }
  209. #endif
  210. size_t outer_facet;
  211. bool flipped;
  212. Eigen::VectorXi I;
  213. I.setLinSpaced(num_faces, 0, num_faces-1);
  214. igl::copyleft::cgal::outer_facet(V, F, I, outer_facet, flipped);
  215. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  216. log_time("outer_facet");
  217. #endif
  218. const size_t outer_patch = P[outer_facet];
  219. const size_t infinity_cell = C(outer_patch, flipped?1:0);
  220. Eigen::VectorXi patch_labels(num_patches);
  221. const int INVALID = std::numeric_limits<int>::max();
  222. patch_labels.setConstant(INVALID);
  223. for (size_t i=0; i<num_faces; i++) {
  224. if (patch_labels[P[i]] == INVALID) {
  225. patch_labels[P[i]] = labels[i];
  226. } else {
  227. assert(patch_labels[P[i]] == labels[i]);
  228. }
  229. }
  230. assert((patch_labels.array() != INVALID).all());
  231. const size_t num_labels = patch_labels.maxCoeff()+1;
  232. Eigen::MatrixXi per_cell_W(num_cells, num_labels);
  233. per_cell_W.setConstant(INVALID);
  234. per_cell_W.row(infinity_cell).setZero();
  235. std::queue<size_t> Q;
  236. Q.push(infinity_cell);
  237. while (!Q.empty()) {
  238. size_t curr_cell = Q.front();
  239. Q.pop();
  240. for (const auto& neighbor : cell_adj[curr_cell]) {
  241. size_t neighbor_cell, patch_idx;
  242. bool direction;
  243. std::tie(neighbor_cell, direction, patch_idx) = neighbor;
  244. if ((per_cell_W.row(neighbor_cell).array() == INVALID).any()) {
  245. per_cell_W.row(neighbor_cell) = per_cell_W.row(curr_cell);
  246. for (size_t i=0; i<num_labels; i++) {
  247. int inc = (patch_labels[patch_idx] == (int)i) ?
  248. (direction ? -1:1) :0;
  249. per_cell_W(neighbor_cell, i) =
  250. per_cell_W(curr_cell, i) + inc;
  251. }
  252. Q.push(neighbor_cell);
  253. } else {
  254. #ifndef NDEBUG
  255. // Checking for winding number consistency.
  256. // This check would inevitably fail for meshes that contain open
  257. // boundary or non-orientable. However, the inconsistent winding number
  258. // field would still be useful in some cases such as when problem region
  259. // is local and embedded within the volume. This, unfortunately, is the
  260. // best we can do because the problem of computing integer winding
  261. // number is ill-defined for open and non-orientable surfaces.
  262. for (size_t i=0; i<num_labels; i++) {
  263. if ((int)i == patch_labels[patch_idx]) {
  264. int inc = direction ? -1:1;
  265. //assert(per_cell_W(neighbor_cell, i) ==
  266. // per_cell_W(curr_cell, i) + inc);
  267. } else {
  268. //assert(per_cell_W(neighbor_cell, i) ==
  269. // per_cell_W(curr_cell, i));
  270. }
  271. }
  272. #endif
  273. }
  274. }
  275. }
  276. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  277. log_time("propagate_winding_number");
  278. #endif
  279. W.resize(num_faces, num_labels*2);
  280. for (size_t i=0; i<num_faces; i++)
  281. {
  282. const size_t patch = P[i];
  283. const size_t positive_cell = C(patch, 0);
  284. const size_t negative_cell = C(patch, 1);
  285. for (size_t j=0; j<num_labels; j++) {
  286. W(i,j*2 ) = per_cell_W(positive_cell, j);
  287. W(i,j*2+1) = per_cell_W(negative_cell, j);
  288. }
  289. }
  290. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  291. log_time("store_result");
  292. #endif
  293. return valid;
  294. }
  295. #ifdef IGL_STATIC_LIBRARY
  296. template bool igl::copyleft::cgal::propagate_winding_numbers<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::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> >&);
  297. #endif