propagate_winding_numbers.cpp 9.5 KB

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