polyvector_field_singularities_from_matchings.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <alecjacobson@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. #include <iostream>
  9. #include <igl/polyvector_field_singularities_from_matchings.h>
  10. #include <igl/is_border_vertex.h>
  11. #include <igl/vertex_triangle_adjacency.h>
  12. #include <igl/list_to_matrix.h>
  13. #include <igl/triangle_triangle_adjacency.h>
  14. #include <igl/edge_topology.h>
  15. template <typename DerivedV, typename DerivedF, typename DerivedM, typename VFType, typename DerivedTT>
  16. void igl::polyvector_field_one_ring_matchings(const Eigen::PlainObjectBase<DerivedV> &V,
  17. const Eigen::PlainObjectBase<DerivedF> &F,
  18. const std::vector<std::vector<VFType> >& VF,
  19. const Eigen::MatrixXi& E2F,
  20. const Eigen::MatrixXi& F2E,
  21. const Eigen::PlainObjectBase<DerivedTT>& TT,
  22. const Eigen::PlainObjectBase<DerivedM> &match_ab,
  23. const Eigen::PlainObjectBase<DerivedM> &match_ba,
  24. const int vi,
  25. Eigen::MatrixXi &mvi,
  26. Eigen::VectorXi &fi)
  27. {
  28. int half_degree = match_ab.cols();
  29. mvi.resize(VF[vi].size()+1,half_degree);
  30. fi.resize(VF[vi].size()+1,1);
  31. //start from one face
  32. //first, check if the vertex is on a boundary
  33. //then there must be two faces that are on the boundary
  34. //(other cases not supported)
  35. int fstart = -1;
  36. int ind = 0;
  37. for (int i =0; i<VF[vi].size(); ++i)
  38. {
  39. int fi = VF[vi][i];
  40. for (int j=0; j<3; ++j)
  41. if (F(fi,j)==vi && TT(fi,j) == -1)
  42. {
  43. ind ++;
  44. fstart = fi;
  45. // break;
  46. }
  47. }
  48. if (ind >1 )
  49. {
  50. std::cerr<<"igl::polyvector_field_one_ring_matchings -- vertex "<<vi<< " is on an unusual boundary"<<std::endl;
  51. exit(1);
  52. }
  53. if (fstart == -1)
  54. fstart = VF[vi][0];
  55. int current_face = fstart;
  56. int i =0;
  57. fi[i] = current_face;
  58. for (int j=0; j<half_degree; ++j)
  59. mvi(i,j) = j;
  60. int next_face = -1;
  61. while (next_face != fstart && current_face!=-1)
  62. {
  63. // look for the vertex
  64. int j=-1;
  65. for (unsigned z=0; z<3; ++z)
  66. if (F(current_face,(z+1)%3) == vi)
  67. {
  68. j=z;
  69. break;
  70. }
  71. assert(j!=-1);
  72. next_face = TT(current_face, j);
  73. ++i;
  74. if (next_face == -1)
  75. mvi.row(i).setConstant(-1);
  76. else
  77. {
  78. // look at the edge between the two faces
  79. const int &current_edge = F2E(current_face,j);
  80. for (int k=0; k<half_degree; ++k)
  81. {
  82. // check its orientation to determine whether match_ab or match_ba should be used
  83. if ((E2F(current_edge,0) == current_face) &&
  84. (E2F(current_edge,1) == next_face) )
  85. {
  86. //look at match_ab
  87. mvi(i,k) = match_ab(current_edge,(mvi(i-1,k))%half_degree);
  88. }
  89. else
  90. {
  91. assert((E2F(current_edge,1) == current_face) &&
  92. (E2F(current_edge,0) == next_face));
  93. //look at match_ba
  94. mvi(i,k) = match_ba(current_edge,(mvi(i-1,k))%half_degree);
  95. }
  96. if (mvi(i-1,k)>=half_degree)
  97. mvi(i,k) = (mvi(i,k)+half_degree)%(2*half_degree);
  98. }
  99. }
  100. current_face = next_face;
  101. fi[i] = current_face;
  102. }
  103. }
  104. template <typename DerivedV, typename DerivedF, typename DerivedM, typename DerivedS>
  105. IGL_INLINE void igl::polyvector_field_singularities_from_matchings(
  106. const Eigen::PlainObjectBase<DerivedV> &V,
  107. const Eigen::PlainObjectBase<DerivedF> &F,
  108. const Eigen::PlainObjectBase<DerivedM> &match_ab,
  109. const Eigen::PlainObjectBase<DerivedM> &match_ba,
  110. Eigen::PlainObjectBase<DerivedS> &singularities)
  111. {
  112. std::vector<bool> V_border = igl::is_border_vertex(V,F);
  113. std::vector<std::vector<int> > VF, VFi;
  114. igl::vertex_triangle_adjacency(V,F,VF,VFi);
  115. Eigen::MatrixXi TT, TTi;
  116. igl::triangle_triangle_adjacency(F,TT,TTi);
  117. Eigen::MatrixXi E, E2F, F2E;
  118. igl::edge_topology(V,F,E,F2E,E2F);
  119. igl::polyvector_field_singularities_from_matchings(V, F, V_border, VF, TT, E2F, F2E, match_ab, match_ba, singularities);
  120. }
  121. template <typename DerivedV, typename DerivedF, typename DerivedM, typename VFType, typename DerivedS>
  122. IGL_INLINE void igl::polyvector_field_singularities_from_matchings(
  123. const Eigen::PlainObjectBase<DerivedV> &V,
  124. const Eigen::PlainObjectBase<DerivedF> &F,
  125. const std::vector<bool> &V_border,
  126. const std::vector<std::vector<VFType> > &VF,
  127. const Eigen::MatrixXi &TT,
  128. const Eigen::MatrixXi &E2F,
  129. const Eigen::MatrixXi &F2E,
  130. const Eigen::PlainObjectBase<DerivedM> &match_ab,
  131. const Eigen::PlainObjectBase<DerivedM> &match_ba,
  132. Eigen::PlainObjectBase<DerivedS> &singularities)
  133. {
  134. int numV = V.rows();
  135. std::vector<int> singularities_v;
  136. int half_degree = match_ab.cols();
  137. for (int vi =0; vi<numV; ++vi)
  138. {
  139. ///check that is on border..
  140. if (V_border[vi])
  141. continue;
  142. Eigen::VectorXi fi;
  143. Eigen::MatrixXi mvi;
  144. igl::polyvector_field_one_ring_matchings(V, F, VF, E2F, F2E, TT, match_ab, match_ba, vi, mvi, fi);
  145. int num = fi.size();
  146. //pick one of the vectors to check for singularities
  147. for (int vector_to_match = 0; vector_to_match < half_degree; ++vector_to_match)
  148. {
  149. if(mvi(num-1,vector_to_match) != mvi(0,vector_to_match))
  150. {
  151. singularities_v.push_back(vi);
  152. break;
  153. }
  154. }
  155. }
  156. std::sort(singularities_v.begin(), singularities_v.end());
  157. auto last = std::unique(singularities_v.begin(), singularities_v.end());
  158. singularities_v.erase(last, singularities_v.end());
  159. igl::list_to_matrix(singularities_v, singularities);
  160. }
  161. template <typename DerivedV, typename DerivedF, typename DerivedM, typename DerivedS>
  162. IGL_INLINE void igl::polyvector_field_singularities_from_matchings(
  163. const Eigen::PlainObjectBase<DerivedV> &V,
  164. const Eigen::PlainObjectBase<DerivedF> &F,
  165. const Eigen::PlainObjectBase<DerivedM> &match_ab,
  166. const Eigen::PlainObjectBase<DerivedM> &match_ba,
  167. Eigen::PlainObjectBase<DerivedS> &singularities,
  168. Eigen::PlainObjectBase<DerivedS> &singularity_indices)
  169. {
  170. std::vector<bool> V_border = igl::is_border_vertex(V,F);
  171. std::vector<std::vector<int> > VF, VFi;
  172. igl::vertex_triangle_adjacency(V,F,VF,VFi);
  173. Eigen::MatrixXi TT, TTi;
  174. igl::triangle_triangle_adjacency(V,F,TT,TTi);
  175. Eigen::MatrixXi E, E2F, F2E;
  176. igl::edge_topology(V,F,E,F2E,E2F);
  177. igl::polyvector_field_singularities_from_matchings(V, F, V_border, VF, TT, E2F, F2E, match_ab, match_ba, singularities, singularity_indices);
  178. }
  179. template <typename DerivedV, typename DerivedF, typename DerivedM, typename VFType, typename DerivedS>
  180. IGL_INLINE void igl::polyvector_field_singularities_from_matchings(
  181. const Eigen::PlainObjectBase<DerivedV> &V,
  182. const Eigen::PlainObjectBase<DerivedF> &F,
  183. const std::vector<bool> &V_border,
  184. const std::vector<std::vector<VFType> > &VF,
  185. const Eigen::MatrixXi &TT,
  186. const Eigen::MatrixXi &E2F,
  187. const Eigen::MatrixXi &F2E,
  188. const Eigen::PlainObjectBase<DerivedM> &match_ab,
  189. const Eigen::PlainObjectBase<DerivedM> &match_ba,
  190. Eigen::PlainObjectBase<DerivedS> &singularities,
  191. Eigen::PlainObjectBase<DerivedS> &singularity_indices)
  192. {
  193. igl::polyvector_field_singularities_from_matchings(V, F, V_border, VF, TT, E2F, F2E, match_ab, match_ba, singularities);
  194. singularity_indices.setZero(singularities.size(), 1);
  195. //get index from first vector only
  196. int vector_to_match = 0;
  197. for (int i =0; i<singularities.size(); ++i)
  198. {
  199. int vi = singularities[i];
  200. // Eigen::VectorXi mvi,fi;
  201. // igl::polyvector_field_one_ring_matchings(V, F, VF, E2F, F2E, TT, match_ab, match_ba, vi, vector_to_match, mvi, fi);
  202. Eigen::VectorXi fi;
  203. Eigen::MatrixXi mvi;
  204. igl::polyvector_field_one_ring_matchings(V, F, VF, E2F, F2E, TT, match_ab, match_ba, vi, mvi, fi);
  205. singularity_indices[i] = (mvi(mvi.rows()-1,vector_to_match) - vector_to_match);
  206. }
  207. }
  208. #ifdef IGL_STATIC_LIBRARY
  209. // Explicit template specialization
  210. template void igl::polyvector_field_singularities_from_matchings<Eigen::Matrix<double, -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<double, -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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  211. #endif