propagate_winding_numbers.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. assert(false && "Input mesh is not orientable");
  113. std::cerr << "Input mesh is not orientable!" << std::endl;
  114. valid = false;
  115. }
  116. const size_t num_faces = F.rows();
  117. typedef std::tuple<typename DerivedC::Scalar, bool, size_t> CellConnection;
  118. std::vector<std::set<CellConnection> > cell_adj;
  119. igl::copyleft::cgal::cell_adjacency(C, num_cells, cell_adj);
  120. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  121. log_time("cell_connectivity");
  122. #endif
  123. auto save_cell = [&](const std::string& filename, size_t cell_id) -> void{
  124. std::vector<size_t> faces;
  125. for (size_t i=0; i<num_patches; i++) {
  126. if ((C.row(i).array() == cell_id).any()) {
  127. for (size_t j=0; j<num_faces; j++) {
  128. if ((size_t)P[j] == i) {
  129. faces.push_back(j);
  130. }
  131. }
  132. }
  133. }
  134. Eigen::MatrixXi cell_faces(faces.size(), 3);
  135. for (size_t i=0; i<faces.size(); i++) {
  136. cell_faces.row(i) = F.row(faces[i]);
  137. }
  138. Eigen::MatrixXd vertices(V.rows(), 3);
  139. for (size_t i=0; i<(size_t)V.rows(); i++) {
  140. assign_scalar(V(i,0), vertices(i,0));
  141. assign_scalar(V(i,1), vertices(i,1));
  142. assign_scalar(V(i,2), vertices(i,2));
  143. }
  144. writePLY(filename, vertices, cell_faces);
  145. };
  146. #ifndef NDEBUG
  147. {
  148. // Check for odd cycle.
  149. Eigen::VectorXi cell_labels(num_cells);
  150. cell_labels.setZero();
  151. Eigen::VectorXi parents(num_cells);
  152. parents.setConstant(-1);
  153. auto trace_parents = [&](size_t idx) -> std::list<size_t> {
  154. std::list<size_t> path;
  155. path.push_back(idx);
  156. while ((size_t)parents[path.back()] != path.back()) {
  157. path.push_back(parents[path.back()]);
  158. }
  159. return path;
  160. };
  161. for (size_t i=0; i<num_cells; i++) {
  162. if (cell_labels[i] == 0) {
  163. cell_labels[i] = 1;
  164. std::queue<size_t> Q;
  165. Q.push(i);
  166. parents[i] = i;
  167. while (!Q.empty()) {
  168. size_t curr_idx = Q.front();
  169. Q.pop();
  170. int curr_label = cell_labels[curr_idx];
  171. for (const auto& neighbor : cell_adj[curr_idx]) {
  172. if (cell_labels[std::get<0>(neighbor)] == 0)
  173. {
  174. cell_labels[std::get<0>(neighbor)] = curr_label * -1;
  175. Q.push(std::get<0>(neighbor));
  176. parents[std::get<0>(neighbor)] = curr_idx;
  177. } else
  178. {
  179. if (cell_labels[std::get<0>(neighbor)] != curr_label * -1)
  180. {
  181. std::cerr << "Odd cell cycle detected!" << std::endl;
  182. auto path = trace_parents(curr_idx);
  183. path.reverse();
  184. auto path2 = trace_parents(std::get<0>(neighbor));
  185. path.insert(path.end(), path2.begin(), path2.end());
  186. for (auto cell_id : path)
  187. {
  188. std::cout << cell_id << " ";
  189. std::stringstream filename;
  190. filename << "cell_" << cell_id << ".ply";
  191. save_cell(filename.str(), cell_id);
  192. }
  193. std::cout << std::endl;
  194. valid = false;
  195. }
  196. // Do not fail when odd cycle is detected because the resulting
  197. // integer winding number field, although inconsistent, may still
  198. // be used if the problem region is local and embedded within a
  199. // valid volume.
  200. //assert(cell_labels[std::get<0>(neighbor)] == curr_label * -1);
  201. }
  202. }
  203. }
  204. }
  205. }
  206. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  207. log_time("odd_cycle_check");
  208. #endif
  209. }
  210. #endif
  211. size_t outer_facet;
  212. bool flipped;
  213. Eigen::VectorXi I;
  214. I.setLinSpaced(num_faces, 0, num_faces-1);
  215. igl::copyleft::cgal::outer_facet(V, F, I, outer_facet, flipped);
  216. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  217. log_time("outer_facet");
  218. #endif
  219. const size_t outer_patch = P[outer_facet];
  220. const size_t infinity_cell = C(outer_patch, flipped?1:0);
  221. Eigen::VectorXi patch_labels(num_patches);
  222. const int INVALID = std::numeric_limits<int>::max();
  223. patch_labels.setConstant(INVALID);
  224. for (size_t i=0; i<num_faces; i++) {
  225. if (patch_labels[P[i]] == INVALID) {
  226. patch_labels[P[i]] = labels[i];
  227. } else {
  228. assert(patch_labels[P[i]] == labels[i]);
  229. }
  230. }
  231. assert((patch_labels.array() != INVALID).all());
  232. const size_t num_labels = patch_labels.maxCoeff()+1;
  233. Eigen::MatrixXi per_cell_W(num_cells, num_labels);
  234. per_cell_W.setConstant(INVALID);
  235. per_cell_W.row(infinity_cell).setZero();
  236. std::queue<size_t> Q;
  237. Q.push(infinity_cell);
  238. while (!Q.empty()) {
  239. size_t curr_cell = Q.front();
  240. Q.pop();
  241. for (const auto& neighbor : cell_adj[curr_cell]) {
  242. size_t neighbor_cell, patch_idx;
  243. bool direction;
  244. std::tie(neighbor_cell, direction, patch_idx) = neighbor;
  245. if ((per_cell_W.row(neighbor_cell).array() == INVALID).any()) {
  246. per_cell_W.row(neighbor_cell) = per_cell_W.row(curr_cell);
  247. for (size_t i=0; i<num_labels; i++) {
  248. int inc = (patch_labels[patch_idx] == (int)i) ?
  249. (direction ? -1:1) :0;
  250. per_cell_W(neighbor_cell, i) =
  251. per_cell_W(curr_cell, i) + inc;
  252. }
  253. Q.push(neighbor_cell);
  254. } else {
  255. #ifndef NDEBUG
  256. // Checking for winding number consistency.
  257. // This check would inevitably fail for meshes that contain open
  258. // boundary or non-orientable. However, the inconsistent winding number
  259. // field would still be useful in some cases such as when problem region
  260. // is local and embedded within the volume. This, unfortunately, is the
  261. // best we can do because the problem of computing integer winding
  262. // number is ill-defined for open and non-orientable surfaces.
  263. for (size_t i=0; i<num_labels; i++) {
  264. if ((int)i == patch_labels[patch_idx]) {
  265. int inc = direction ? -1:1;
  266. //assert(per_cell_W(neighbor_cell, i) ==
  267. // per_cell_W(curr_cell, i) + inc);
  268. } else {
  269. //assert(per_cell_W(neighbor_cell, i) ==
  270. // per_cell_W(curr_cell, i));
  271. }
  272. }
  273. #endif
  274. }
  275. }
  276. }
  277. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  278. log_time("propagate_winding_number");
  279. #endif
  280. W.resize(num_faces, num_labels*2);
  281. for (size_t i=0; i<num_faces; i++)
  282. {
  283. const size_t patch = P[i];
  284. const size_t positive_cell = C(patch, 0);
  285. const size_t negative_cell = C(patch, 1);
  286. for (size_t j=0; j<num_labels; j++) {
  287. W(i,j*2 ) = per_cell_W(positive_cell, j);
  288. W(i,j*2+1) = per_cell_W(negative_cell, j);
  289. }
  290. }
  291. #ifdef PROPAGATE_WINDING_NUMBER_TIMING
  292. log_time("store_result");
  293. #endif
  294. return valid;
  295. }
  296. #ifdef IGL_STATIC_LIBRARY
  297. 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> >&);
  298. #endif