polyvector_field_matchings.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, typename DerivedC>
  89. IGL_INLINE typename DerivedC::Scalar 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. Eigen::PlainObjectBase<DerivedM>& match_ab,
  98. Eigen::PlainObjectBase<DerivedM>& match_ba,
  99. Eigen::PlainObjectBase<DerivedC>& curl)
  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. curl.setZero(numEdges,1);
  111. match_ab.setZero(numEdges, half_degree);
  112. match_ba.setZero(numEdges, half_degree);
  113. typename DerivedC::Scalar meanCurl = 0;
  114. for (int ei=0; ei<numEdges; ++ei)
  115. {
  116. if (isBorderEdge[ei])
  117. continue;
  118. // the two faces of the flap
  119. int a = E2F(ei,0);
  120. int b = E2F(ei,1);
  121. 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>();
  122. Eigen::Matrix<typename DerivedM::Scalar, 1, Eigen::Dynamic> mab, mba;
  123. igl::polyvector_field_matching(sol3D.row(a).eval(),
  124. sol3D.row(b).eval(),
  125. FN.row(a).eval(),
  126. FN.row(b).eval(),
  127. ce,
  128. match_with_curl,
  129. mab,
  130. mba,
  131. true);
  132. match_ab.row(ei) = mab;
  133. match_ba.row(ei) = mba;
  134. Eigen::Matrix<typename DerivedS::Scalar, 1, Eigen::Dynamic> matched;
  135. matched.resize(1, 3*half_degree);
  136. for (int i = 0; i<half_degree; ++i)
  137. {
  138. int sign = (mab[i]<half_degree)?1:-1;
  139. matched.segment(i*3, 3) = sign*sol3D.row(b).segment((mab[i]%half_degree)*3, 3);
  140. }
  141. typename DerivedC::Scalar avgCurl = 0;
  142. for (int i = 0; i<half_degree; ++i)
  143. avgCurl += fabs(sol3D.row(a).segment(i*3, 3).dot(ce) - matched.segment(i*3, 3).dot(ce));
  144. avgCurl = avgCurl/half_degree;
  145. curl[ ei ] = avgCurl;
  146. meanCurl+= avgCurl;
  147. }
  148. meanCurl /= 1.*(numEdges - isBorderEdge.sum());
  149. return meanCurl;
  150. }
  151. template <typename DerivedS, typename DerivedV, typename DerivedF, typename DerivedM, typename DerivedC>
  152. IGL_INLINE typename DerivedC::Scalar igl::polyvector_field_matchings(
  153. const Eigen::PlainObjectBase<DerivedS>& sol3D,
  154. const Eigen::PlainObjectBase<DerivedV>&V,
  155. const Eigen::PlainObjectBase<DerivedF>&F,
  156. bool match_with_curl,
  157. Eigen::PlainObjectBase<DerivedM>& match_ab,
  158. Eigen::PlainObjectBase<DerivedM>& match_ba,
  159. Eigen::PlainObjectBase<DerivedC>& curl)
  160. {
  161. Eigen::MatrixXi E, E2F, F2E;
  162. igl::edge_topology(V,F,E,F2E,E2F);
  163. Eigen::PlainObjectBase<DerivedV> FN;
  164. igl::per_face_normals(V,F,FN);
  165. return igl::polyvector_field_matchings(sol3D, V, F, E, FN, E2F, match_with_curl, match_ab, match_ba, curl);
  166. }
  167. #ifdef IGL_STATIC_LIBRARY
  168. // Explicit template specialization
  169. template Eigen::Matrix<float, -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<float, -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, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> >&);
  170. 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, 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> >&);
  171. template void igl::polyvector_field_matching<Eigen::Matrix<double, 1, -1, 1, 1, -1>, Eigen::Matrix<double, 1, -1, 1, 1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 1, -1, 1, 1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, -1, 1, 1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, -1, 1, 1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, -1, 1, 1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, -1, 1, 1, -1> > const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, bool);
  172. #endif