polyvector_field_matchings.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. {
  17. // make sure the matching preserve ccw order of the vectors across the edge
  18. // 1) order vectors in a, ccw e.g. (0,1,2,3)_a not ccw --> (0,3,2,1)_a ccw
  19. // 2) order vectors in b, ccw e.g. (0,1,2,3)_b not ccw --> (0,2,1,3)_b ccw
  20. // 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
  21. int hN = _ua.cols()/3;
  22. int N = 2*hN;
  23. Eigen::Matrix<typename DerivedS::Scalar,1,Eigen::Dynamic> ua (1,N*3); ua <<_ua, -_ua;
  24. Eigen::Matrix<typename DerivedS::Scalar,1,Eigen::Dynamic> ub (1,N*3); ub <<_ub, -_ub;
  25. Eigen::Matrix<typename DerivedM::Scalar,Eigen::Dynamic,1> order_a, order_b;
  26. igl::sort_vectors_ccw(ua, na, order_a);
  27. igl::sort_vectors_ccw(ub, nb, order_b);
  28. //checking all possible circshifts of order_b as matches for order_a
  29. Eigen::Matrix<typename DerivedM::Scalar,Eigen::Dynamic,Eigen::Dynamic> all_matches(N,N);
  30. Eigen::Matrix<typename DerivedS::Scalar,1,Eigen::Dynamic> all_scores(1,N);
  31. for (int s =0; s< N; ++s)
  32. {
  33. all_matches.row(s) = order_b;
  34. typename DerivedS::Scalar current_score=0;
  35. for (int i=0; i< N; ++i)
  36. {
  37. if (match_with_curl)
  38. current_score += fabs(ua.segment(order_a[i]*3, 3).dot(e) - ub.segment(order_b[i]*3, 3).dot(e));
  39. else
  40. {
  41. Eigen::Matrix<typename DerivedS::Scalar,3,1> na_ = na.transpose();
  42. Eigen::Matrix<typename DerivedS::Scalar,3,1> nb_ = nb.transpose();
  43. Eigen::Matrix<typename DerivedS::Scalar,1,3> uaRot = igl::rotation_matrix_from_directions(na_, nb_)*ua.segment(order_a[i]*3, 3).transpose();
  44. current_score += (uaRot-ub.segment(order_b[i]*3, 3)).norm();
  45. }
  46. }
  47. all_scores[s] = current_score;
  48. // do a circshift on order_b to check the next preserving matching
  49. int temp = order_b[0];
  50. for (int i =0; i< N-1; ++i)
  51. order_b[i] = order_b[i+1];
  52. order_b(N-1) = temp;
  53. }
  54. Eigen::Matrix<typename DerivedM::Scalar,1,Eigen::Dynamic> best_matching_for_sorted_a;
  55. int best_i;
  56. all_scores.minCoeff(&best_i);
  57. best_matching_for_sorted_a = all_matches.row(best_i);
  58. // best_matching_for_sorted_a is the matching for the sorted vectors in a
  59. // get the matching for the initial (unsorted) vectors
  60. mab.resize(1,N);
  61. for (int i=0; i< N; ++i)
  62. mab[order_a[i]] = best_matching_for_sorted_a[i];
  63. //mab contains the best matching a->b, get the opposite too
  64. mba.resize(1, N);
  65. for (int i=0; i< N; ++i)
  66. mba[mab[i]] = i;
  67. mab = mab.head(hN);
  68. mba = mba.head(hN);
  69. }
  70. template <typename DerivedS, typename DerivedV, typename DerivedF, typename DerivedE, typename DerivedM, typename DerivedC>
  71. IGL_INLINE typename DerivedC::Scalar igl::polyvector_field_matchings(
  72. const Eigen::PlainObjectBase<DerivedS>& sol3D,
  73. const Eigen::PlainObjectBase<DerivedV>&V,
  74. const Eigen::PlainObjectBase<DerivedF>&F,
  75. const Eigen::PlainObjectBase<DerivedE>&E,
  76. const Eigen::PlainObjectBase<DerivedV>& FN,
  77. const Eigen::MatrixXi &E2F,
  78. bool match_with_curl,
  79. Eigen::PlainObjectBase<DerivedM>& match_ab,
  80. Eigen::PlainObjectBase<DerivedM>& match_ba,
  81. Eigen::PlainObjectBase<DerivedC>& curl)
  82. {
  83. int numEdges = E.rows();
  84. int half_degree = sol3D.cols()/3;
  85. Eigen::VectorXi isBorderEdge;
  86. isBorderEdge.setZero(numEdges,1);
  87. for(unsigned i=0; i<numEdges; ++i)
  88. {
  89. if ((E2F(i,0) == -1) || ((E2F(i,1) == -1)))
  90. isBorderEdge[i] = 1;
  91. }
  92. curl.setZero(numEdges,1);
  93. match_ab.setZero(numEdges, half_degree);
  94. match_ba.setZero(numEdges, half_degree);
  95. typename DerivedC::Scalar meanCurl = 0;
  96. for (int ei=0; ei<numEdges; ++ei)
  97. {
  98. if (isBorderEdge[ei])
  99. continue;
  100. // the two faces of the flap
  101. int a = E2F(ei,0);
  102. int b = E2F(ei,1);
  103. 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>();
  104. Eigen::Matrix<typename DerivedM::Scalar, 1, Eigen::Dynamic> mab, mba;
  105. igl::polyvector_field_matching(sol3D.row(a).eval(),
  106. sol3D.row(b).eval(),
  107. FN.row(a).eval(),
  108. FN.row(b).eval(),
  109. ce,
  110. match_with_curl,
  111. mab,
  112. mba);
  113. match_ab.row(ei) = mab;
  114. match_ba.row(ei) = mba;
  115. Eigen::Matrix<typename DerivedS::Scalar, 1, Eigen::Dynamic> matched;
  116. matched.resize(1, 3*half_degree);
  117. for (int i = 0; i<half_degree; ++i)
  118. {
  119. int sign = (mab[i]<half_degree)?1:-1;
  120. matched.segment(i*3, 3) = sign*sol3D.row(b).segment((mab[i]%half_degree)*3, 3);
  121. }
  122. typename DerivedC::Scalar avgCurl = 0;
  123. for (int i = 0; i<half_degree; ++i)
  124. avgCurl += fabs(sol3D.row(a).segment(i*3, 3).dot(ce) - matched.segment(i*3, 3).dot(ce));
  125. avgCurl = avgCurl/half_degree;
  126. curl[ ei ] = avgCurl;
  127. meanCurl+= avgCurl;
  128. }
  129. meanCurl /= 1.*(numEdges - isBorderEdge.sum());
  130. return meanCurl;
  131. }
  132. template <typename DerivedS, typename DerivedV, typename DerivedF, typename DerivedM, typename DerivedC>
  133. IGL_INLINE typename DerivedC::Scalar igl::polyvector_field_matchings(
  134. const Eigen::PlainObjectBase<DerivedS>& sol3D,
  135. const Eigen::PlainObjectBase<DerivedV>&V,
  136. const Eigen::PlainObjectBase<DerivedF>&F,
  137. bool match_with_curl,
  138. Eigen::PlainObjectBase<DerivedM>& match_ab,
  139. Eigen::PlainObjectBase<DerivedM>& match_ba,
  140. Eigen::PlainObjectBase<DerivedC>& curl)
  141. {
  142. Eigen::MatrixXi E, E2F, F2E;
  143. igl::edge_topology(V,F,E,F2E,E2F);
  144. Eigen::PlainObjectBase<DerivedV> FN;
  145. igl::per_face_normals(V,F,FN);
  146. return igl::polyvector_field_matchings(sol3D, V, F, E, FN, E2F, match_with_curl, match_ab, match_ba, curl);
  147. }
  148. #ifdef IGL_STATIC_LIBRARY
  149. // Explicit template specialization
  150. 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> >&);
  151. 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> >&);
  152. #endif