propagate_winding_numbers.cpp 9.4 KB

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