propagate_winding_numbers.cpp 10 KB

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