polyvector_field_matchings.cpp 13 KB

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