polyvector_field_matchings.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <igl/polyvector_field_matchings.h>
  2. #include <igl/edge_topology.h>
  3. #include <igl/per_face_normals.h>
  4. #include <igl/sort_vectors_ccw.h>
  5. #include <igl/rotation_matrix_from_directions.h>
  6. template <typename DerivedS, typename DerivedV, typename DerivedM>
  7. IGL_INLINE void igl::polyvector_field_matching(
  8. const Eigen::PlainObjectBase<DerivedS>& _ua,
  9. const Eigen::PlainObjectBase<DerivedS>& _ub,
  10. const Eigen::PlainObjectBase<DerivedV>& na,
  11. const Eigen::PlainObjectBase<DerivedV>& nb,
  12. const Eigen::PlainObjectBase<DerivedV>& e,
  13. bool match_with_curl,
  14. Eigen::PlainObjectBase<DerivedM>& mab,
  15. Eigen::PlainObjectBase<DerivedM>& mba,
  16. bool is_symmetric)
  17. {
  18. // make sure the matching preserve ccw order of the vectors across the edge
  19. // 1) order vectors in a, ccw e.g. (0,1,2,3)_a not ccw --> (0,3,2,1)_a ccw
  20. // 2) order vectors in b, ccw e.g. (0,1,2,3)_b not ccw --> (0,2,1,3)_b ccw
  21. // 3) the vectors in b that match the ordered vectors in a (in this case (0,3,2,1)_a ) must be a circular shift of the ccw ordered vectors in b - so we need to explicitely check only these matchings to find the best ccw one, there are N of them
  22. int hN = _ua.cols()/3;
  23. int N;
  24. if (is_symmetric)
  25. N = 2*hN;
  26. else
  27. N = hN;
  28. Eigen::Matrix<typename DerivedS::Scalar,1,Eigen::Dynamic> ua (1,N*3);
  29. Eigen::Matrix<typename DerivedS::Scalar,1,Eigen::Dynamic> ub (1,N*3);
  30. if (is_symmetric)
  31. {
  32. ua <<_ua, -_ua;
  33. ub <<_ub, -_ub;
  34. }
  35. else
  36. {
  37. ua =_ua;
  38. ub =_ub;
  39. }
  40. Eigen::Matrix<typename DerivedM::Scalar,Eigen::Dynamic,1> order_a, order_b;
  41. igl::sort_vectors_ccw(ua, na, order_a);
  42. igl::sort_vectors_ccw(ub, nb, order_b);
  43. //checking all possible circshifts of order_b as matches for order_a
  44. Eigen::Matrix<typename DerivedM::Scalar,Eigen::Dynamic,Eigen::Dynamic> all_matches(N,N);
  45. Eigen::Matrix<typename DerivedS::Scalar,1,Eigen::Dynamic> all_scores(1,N);
  46. for (int s =0; s< N; ++s)
  47. {
  48. all_matches.row(s) = order_b;
  49. typename DerivedS::Scalar current_score=0;
  50. for (int i=0; i< N; ++i)
  51. {
  52. if (match_with_curl)
  53. current_score += fabs(ua.segment(order_a[i]*3, 3).dot(e) - ub.segment(order_b[i]*3, 3).dot(e));
  54. else
  55. {
  56. Eigen::Matrix<typename DerivedS::Scalar,3,1> na_ = na.transpose();
  57. Eigen::Matrix<typename DerivedS::Scalar,3,1> nb_ = nb.transpose();
  58. Eigen::Matrix<typename DerivedS::Scalar,1,3> uaRot = igl::rotation_matrix_from_directions(na_, nb_)*ua.segment(order_a[i]*3, 3).transpose();
  59. current_score += (uaRot-ub.segment(order_b[i]*3, 3)).norm();
  60. }
  61. }
  62. all_scores[s] = current_score;
  63. // do a circshift on order_b to check the next preserving matching
  64. int temp = order_b[0];
  65. for (int i =0; i< N-1; ++i)
  66. order_b[i] = order_b[i+1];
  67. order_b(N-1) = temp;
  68. }
  69. Eigen::Matrix<typename DerivedM::Scalar,1,Eigen::Dynamic> best_matching_for_sorted_a;
  70. int best_i;
  71. all_scores.minCoeff(&best_i);
  72. best_matching_for_sorted_a = all_matches.row(best_i);
  73. // best_matching_for_sorted_a is the matching for the sorted vectors in a
  74. // get the matching for the initial (unsorted) vectors
  75. mab.resize(1,N);
  76. for (int i=0; i< N; ++i)
  77. mab[order_a[i]] = best_matching_for_sorted_a[i];
  78. //mab contains the best matching a->b, get the opposite too
  79. mba.resize(1, N);
  80. for (int i=0; i< N; ++i)
  81. mba[mab[i]] = i;
  82. if (is_symmetric)
  83. {
  84. mab = mab.head(hN);
  85. mba = mba.head(hN);
  86. }
  87. }
  88. template <typename DerivedS, typename DerivedV, typename DerivedF, typename DerivedE, typename DerivedM>
  89. IGL_INLINE void igl::polyvector_field_matchings(
  90. const Eigen::PlainObjectBase<DerivedS>& sol3D,
  91. const Eigen::PlainObjectBase<DerivedV>&V,
  92. const Eigen::PlainObjectBase<DerivedF>&F,
  93. const Eigen::PlainObjectBase<DerivedE>&E,
  94. const Eigen::PlainObjectBase<DerivedV>& FN,
  95. const Eigen::MatrixXi &E2F,
  96. bool match_with_curl,
  97. bool is_symmetric,
  98. Eigen::PlainObjectBase<DerivedM>& match_ab,
  99. Eigen::PlainObjectBase<DerivedM>& match_ba)
  100. {
  101. int numEdges = E.rows();
  102. int half_degree = sol3D.cols()/3;
  103. Eigen::VectorXi isBorderEdge;
  104. isBorderEdge.setZero(numEdges,1);
  105. for(unsigned i=0; i<numEdges; ++i)
  106. {
  107. if ((E2F(i,0) == -1) || ((E2F(i,1) == -1)))
  108. isBorderEdge[i] = 1;
  109. }
  110. match_ab.setZero(numEdges, half_degree);
  111. match_ba.setZero(numEdges, half_degree);
  112. for (int ei=0; ei<numEdges; ++ei)
  113. {
  114. if (isBorderEdge[ei])
  115. continue;
  116. // the two faces of the flap
  117. int a = E2F(ei,0);
  118. int b = E2F(ei,1);
  119. Eigen::Matrix<typename DerivedV::Scalar, 1, Eigen::Dynamic> ce = (V.row(E(ei,1))-V.row(E(ei,0))).normalized().template cast<typename DerivedV::Scalar>();
  120. Eigen::Matrix<typename DerivedM::Scalar, 1, Eigen::Dynamic> mab, mba;
  121. igl::polyvector_field_matching(sol3D.row(a).eval(),
  122. sol3D.row(b).eval(),
  123. FN.row(a).eval(),
  124. FN.row(b).eval(),
  125. ce,
  126. match_with_curl,
  127. mab,
  128. mba,
  129. is_symmetric);
  130. match_ab.row(ei) = mab;
  131. match_ba.row(ei) = mba;
  132. }
  133. }
  134. template <typename DerivedS, typename DerivedV, typename DerivedF, typename DerivedE, typename DerivedM, typename DerivedC>
  135. IGL_INLINE typename DerivedC::Scalar igl::polyvector_field_matchings(
  136. const Eigen::PlainObjectBase<DerivedS>& sol3D,
  137. const Eigen::PlainObjectBase<DerivedV>&V,
  138. const Eigen::PlainObjectBase<DerivedF>&F,
  139. const Eigen::PlainObjectBase<DerivedE>&E,
  140. const Eigen::PlainObjectBase<DerivedV>& FN,
  141. const Eigen::MatrixXi &E2F,
  142. bool match_with_curl,
  143. bool is_symmetric,
  144. Eigen::PlainObjectBase<DerivedM>& match_ab,
  145. Eigen::PlainObjectBase<DerivedM>& match_ba,
  146. Eigen::PlainObjectBase<DerivedC>& curl)
  147. {
  148. int numEdges = E.rows();
  149. int half_degree = sol3D.cols()/3;
  150. Eigen::VectorXi isBorderEdge;
  151. isBorderEdge.setZero(numEdges,1);
  152. for(unsigned i=0; i<numEdges; ++i)
  153. {
  154. if ((E2F(i,0) == -1) || ((E2F(i,1) == -1)))
  155. isBorderEdge[i] = 1;
  156. }
  157. igl::polyvector_field_matchings(sol3D, V, F, E, FN, E2F, match_with_curl, is_symmetric, match_ab, match_ba);
  158. curl.setZero(numEdges,1);
  159. typename DerivedC::Scalar meanCurl = 0;
  160. for (int ei=0; ei<numEdges; ++ei)
  161. {
  162. if (isBorderEdge[ei])
  163. continue;
  164. // the two faces of the flap
  165. int a = E2F(ei,0);
  166. int b = E2F(ei,1);
  167. const Eigen::Matrix<typename DerivedM::Scalar, 1, Eigen::Dynamic> &mab = match_ab.row(ei);
  168. const Eigen::Matrix<typename DerivedM::Scalar, 1, Eigen::Dynamic> &mba = match_ba.row(ei);
  169. Eigen::Matrix<typename DerivedS::Scalar, 1, Eigen::Dynamic> matched;
  170. matched.resize(1, 3*half_degree);
  171. for (int i = 0; i<half_degree; ++i)
  172. {
  173. int sign = 1;
  174. int m = mab[i];
  175. if (is_symmetric)
  176. {
  177. sign = (mab[i]<half_degree)?1:-1;
  178. m = m%half_degree;
  179. }
  180. matched.segment(i*3, 3) = sign*sol3D.row(b).segment(m*3, 3);
  181. }
  182. Eigen::Matrix<typename DerivedV::Scalar, 1, Eigen::Dynamic> ce = (V.row(E(ei,1))-V.row(E(ei,0))).normalized().template cast<typename DerivedV::Scalar>();
  183. typename DerivedC::Scalar avgCurl = 0;
  184. for (int i = 0; i<half_degree; ++i)
  185. avgCurl += fabs(sol3D.row(a).segment(i*3, 3).dot(ce) - matched.segment(i*3, 3).dot(ce));
  186. avgCurl = avgCurl/half_degree;
  187. curl[ ei ] = avgCurl;
  188. meanCurl+= avgCurl;
  189. }
  190. meanCurl /= 1.*(numEdges - isBorderEdge.sum());
  191. return meanCurl;
  192. }
  193. template <typename DerivedS, typename DerivedV, typename DerivedF, typename DerivedM, typename DerivedC>
  194. IGL_INLINE typename DerivedC::Scalar igl::polyvector_field_matchings(
  195. const Eigen::PlainObjectBase<DerivedS>& sol3D,
  196. const Eigen::PlainObjectBase<DerivedV>&V,
  197. const Eigen::PlainObjectBase<DerivedF>&F,
  198. bool match_with_curl,
  199. bool is_symmetric,
  200. Eigen::PlainObjectBase<DerivedM>& match_ab,
  201. Eigen::PlainObjectBase<DerivedM>& match_ba,
  202. Eigen::PlainObjectBase<DerivedC>& curl)
  203. {
  204. Eigen::MatrixXi E, E2F, F2E;
  205. igl::edge_topology(V,F,E,F2E,E2F);
  206. //#warning "Poor templating of igl::polyvector_field_matchings forces FN to be DerivedV (this could cause issues if DerivedV has fixed number of rows)"
  207. DerivedV FN;
  208. igl::per_face_normals(V,F,FN);
  209. return igl::polyvector_field_matchings(sol3D, V, F, E, FN, E2F, match_with_curl, is_symmetric, match_ab, match_ba, curl);
  210. }
  211. template <typename DerivedS, typename DerivedV, typename DerivedF, typename DerivedM>
  212. IGL_INLINE void igl::polyvector_field_matchings(
  213. const Eigen::PlainObjectBase<DerivedS>& sol3D,
  214. const Eigen::PlainObjectBase<DerivedV>&V,
  215. const Eigen::PlainObjectBase<DerivedF>&F,
  216. bool match_with_curl,
  217. bool is_symmetric,
  218. Eigen::PlainObjectBase<DerivedM>& match_ab,
  219. Eigen::PlainObjectBase<DerivedM>& match_ba)
  220. {
  221. Eigen::MatrixXi E, E2F, F2E;
  222. igl::edge_topology(V,F,E,F2E,E2F);
  223. DerivedV FN;
  224. igl::per_face_normals(V,F,FN);
  225. igl::polyvector_field_matchings(sol3D, V, F, E, FN, E2F, match_with_curl, is_symmetric, match_ab, match_ba);
  226. }
  227. #ifdef IGL_STATIC_LIBRARY
  228. // Explicit template specialization
  229. template void igl::polyvector_field_matchings<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, bool, bool, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  230. template Eigen::Matrix<double, -1, 1, 0, -1, 1>::Scalar igl::polyvector_field_matchings<Eigen::Matrix<double, -1, -1, 0, -1, -1>, 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<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, bool, bool, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  231. #endif