propagate_winding_numbers.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "../../writeOBJ.h"
  15. #include "../../writePLY.h"
  16. #include "order_facets_around_edge.h"
  17. #include "outer_facet.h"
  18. #include "closest_facet.h"
  19. #include "assign_scalar.h"
  20. #include "extract_cells.h"
  21. #include <stdexcept>
  22. #include <limits>
  23. #include <vector>
  24. #include <tuple>
  25. #include <queue>
  26. namespace propagate_winding_numbers_helper {
  27. template<
  28. typename DerivedF,
  29. typename DeriveduE,
  30. typename uE2EType >
  31. bool is_orientable(
  32. const Eigen::PlainObjectBase<DerivedF>& F,
  33. const Eigen::PlainObjectBase<DeriveduE>& uE,
  34. const std::vector<std::vector<uE2EType> >& uE2E) {
  35. const size_t num_faces = F.rows();
  36. const size_t num_edges = uE.rows();
  37. auto edge_index_to_face_index = [&](size_t ei) {
  38. return ei % num_faces;
  39. };
  40. auto is_consistent = [&](size_t fid, size_t s, size_t d) {
  41. if ((size_t)F(fid, 0) == s && (size_t)F(fid, 1) == d) return true;
  42. if ((size_t)F(fid, 1) == s && (size_t)F(fid, 2) == d) return true;
  43. if ((size_t)F(fid, 2) == s && (size_t)F(fid, 0) == d) return true;
  44. if ((size_t)F(fid, 0) == d && (size_t)F(fid, 1) == s) return false;
  45. if ((size_t)F(fid, 1) == d && (size_t)F(fid, 2) == s) return false;
  46. if ((size_t)F(fid, 2) == d && (size_t)F(fid, 0) == s) return false;
  47. throw "Invalid face!!";
  48. };
  49. for (size_t i=0; i<num_edges; i++) {
  50. const size_t s = uE(i,0);
  51. const size_t d = uE(i,1);
  52. int count=0;
  53. for (const auto& ei : uE2E[i]) {
  54. const size_t fid = edge_index_to_face_index(ei);
  55. if (is_consistent(fid, s, d)) count++;
  56. else count--;
  57. }
  58. if (count != 0) {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. }
  65. template<
  66. typename DerivedV,
  67. typename DerivedF,
  68. typename DerivedL,
  69. typename DerivedW>
  70. IGL_INLINE void igl::copyleft::cgal::propagate_winding_numbers(
  71. const Eigen::PlainObjectBase<DerivedV>& V,
  72. const Eigen::PlainObjectBase<DerivedF>& F,
  73. const Eigen::PlainObjectBase<DerivedL>& labels,
  74. Eigen::PlainObjectBase<DerivedW>& W) {
  75. const size_t num_faces = F.rows();
  76. //typedef typename DerivedF::Scalar Index;
  77. Eigen::MatrixXi E, uE;
  78. Eigen::VectorXi EMAP;
  79. std::vector<std::vector<size_t> > uE2E;
  80. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  81. if (!propagate_winding_numbers_helper::is_orientable(F, uE, uE2E)) {
  82. std::cerr << "Input mesh is not orientable!" << std::endl;
  83. }
  84. Eigen::VectorXi P;
  85. const size_t num_patches = igl::extract_manifold_patches(F, EMAP, uE2E, P);
  86. DerivedW per_patch_cells;
  87. const size_t num_cells =
  88. igl::copyleft::cgal::extract_cells(
  89. V, F, P, E, uE, uE2E, EMAP, per_patch_cells);
  90. typedef std::tuple<size_t, bool, size_t> CellConnection;
  91. std::vector<std::set<CellConnection> > cell_adjacency(num_cells);
  92. for (size_t i=0; i<num_patches; i++) {
  93. const int positive_cell = per_patch_cells(i,0);
  94. const int negative_cell = per_patch_cells(i,1);
  95. cell_adjacency[positive_cell].emplace(negative_cell, false, i);
  96. cell_adjacency[negative_cell].emplace(positive_cell, true, i);
  97. }
  98. auto save_cell = [&](const std::string& filename, size_t cell_id) {
  99. std::vector<size_t> faces;
  100. for (size_t i=0; i<num_patches; i++) {
  101. if ((per_patch_cells.row(i).array() == cell_id).any()) {
  102. for (size_t j=0; j<num_faces; j++) {
  103. if ((size_t)P[j] == i) {
  104. faces.push_back(j);
  105. }
  106. }
  107. }
  108. }
  109. Eigen::MatrixXi cell_faces(faces.size(), 3);
  110. for (size_t i=0; i<faces.size(); i++) {
  111. cell_faces.row(i) = F.row(faces[i]);
  112. }
  113. Eigen::MatrixXd vertices(V.rows(), 3);
  114. for (size_t i=0; i<(size_t)V.rows(); i++) {
  115. assign_scalar(V(i,0), vertices(i,0));
  116. assign_scalar(V(i,1), vertices(i,1));
  117. assign_scalar(V(i,2), vertices(i,2));
  118. }
  119. writePLY(filename, vertices, cell_faces);
  120. };
  121. #ifndef NDEBUG
  122. {
  123. // Check for odd cycle.
  124. Eigen::VectorXi cell_labels(num_cells);
  125. cell_labels.setZero();
  126. Eigen::VectorXi parents(num_cells);
  127. parents.setConstant(-1);
  128. auto trace_parents = [&](size_t idx) {
  129. std::list<size_t> path;
  130. path.push_back(idx);
  131. while ((size_t)parents[path.back()] != path.back()) {
  132. path.push_back(parents[path.back()]);
  133. }
  134. return path;
  135. };
  136. for (size_t i=0; i<num_cells; i++) {
  137. if (cell_labels[i] == 0) {
  138. cell_labels[i] = 1;
  139. std::queue<size_t> Q;
  140. Q.push(i);
  141. parents[i] = i;
  142. while (!Q.empty()) {
  143. size_t curr_idx = Q.front();
  144. Q.pop();
  145. int curr_label = cell_labels[curr_idx];
  146. for (const auto& neighbor : cell_adjacency[curr_idx]) {
  147. if (cell_labels[std::get<0>(neighbor)] == 0) {
  148. cell_labels[std::get<0>(neighbor)] = curr_label * -1;
  149. Q.push(std::get<0>(neighbor));
  150. parents[std::get<0>(neighbor)] = curr_idx;
  151. } else {
  152. if (cell_labels[std::get<0>(neighbor)] !=
  153. curr_label * -1) {
  154. std::cerr << "Odd cell cycle detected!" << std::endl;
  155. auto path = trace_parents(curr_idx);
  156. path.reverse();
  157. auto path2 = trace_parents(std::get<0>(neighbor));
  158. path.insert(path.end(),
  159. path2.begin(), path2.end());
  160. for (auto cell_id : path) {
  161. std::cout << cell_id << " ";
  162. std::stringstream filename;
  163. filename << "cell_" << cell_id << ".ply";
  164. save_cell(filename.str(), cell_id);
  165. }
  166. std::cout << std::endl;
  167. }
  168. assert(cell_labels[std::get<0>(neighbor)] == curr_label * -1);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. #endif
  176. size_t outer_facet;
  177. bool flipped;
  178. Eigen::VectorXi I;
  179. I.setLinSpaced(num_faces, 0, num_faces-1);
  180. igl::copyleft::cgal::outer_facet(V, F, I, outer_facet, flipped);
  181. const size_t outer_patch = P[outer_facet];
  182. const size_t infinity_cell = per_patch_cells(outer_patch, flipped?1:0);
  183. Eigen::VectorXi patch_labels(num_patches);
  184. const int INVALID = std::numeric_limits<int>::max();
  185. patch_labels.setConstant(INVALID);
  186. for (size_t i=0; i<num_faces; i++) {
  187. if (patch_labels[P[i]] == INVALID) {
  188. patch_labels[P[i]] = labels[i];
  189. } else {
  190. assert(patch_labels[P[i]] == labels[i]);
  191. }
  192. }
  193. assert((patch_labels.array() != INVALID).all());
  194. const size_t num_labels = patch_labels.maxCoeff()+1;
  195. Eigen::MatrixXi per_cell_W(num_cells, num_labels);
  196. per_cell_W.setConstant(INVALID);
  197. per_cell_W.row(infinity_cell).setZero();
  198. std::queue<size_t> Q;
  199. Q.push(infinity_cell);
  200. while (!Q.empty()) {
  201. size_t curr_cell = Q.front();
  202. Q.pop();
  203. for (const auto& neighbor : cell_adjacency[curr_cell]) {
  204. size_t neighbor_cell, patch_idx;
  205. bool direction;
  206. std::tie(neighbor_cell, direction, patch_idx) = neighbor;
  207. if ((per_cell_W.row(neighbor_cell).array() == INVALID).any()) {
  208. per_cell_W.row(neighbor_cell) = per_cell_W.row(curr_cell);
  209. for (size_t i=0; i<num_labels; i++) {
  210. int inc = (patch_labels[patch_idx] == (int)i) ?
  211. (direction ? -1:1) :0;
  212. per_cell_W(neighbor_cell, i) =
  213. per_cell_W(curr_cell, i) + inc;
  214. }
  215. Q.push(neighbor_cell);
  216. } else {
  217. #ifndef NDEBUG
  218. for (size_t i=0; i<num_labels; i++) {
  219. if ((int)i == patch_labels[patch_idx]) {
  220. int inc = direction ? -1:1;
  221. assert(per_cell_W(neighbor_cell, i) ==
  222. per_cell_W(curr_cell, i) + inc);
  223. } else {
  224. assert(per_cell_W(neighbor_cell, i) ==
  225. per_cell_W(curr_cell, i));
  226. }
  227. }
  228. #endif
  229. }
  230. }
  231. }
  232. W.resize(num_faces, num_labels*2);
  233. for (size_t i=0; i<num_faces; i++) {
  234. const size_t patch = P[i];
  235. const size_t positive_cell = per_patch_cells(patch, 0);
  236. const size_t negative_cell = per_patch_cells(patch, 1);
  237. for (size_t j=0; j<num_labels; j++) {
  238. W(i,j*2 ) = per_cell_W(positive_cell, j);
  239. W(i,j*2+1) = per_cell_W(negative_cell, j);
  240. }
  241. }
  242. }
  243. #ifdef IGL_STATIC_LIBRARY
  244. 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> >&);
  245. #endif