parallel_transport_angles.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <igl/parallel_transport_angles.h>
  2. #include <Eigen/Geometry>
  3. template <typename DerivedV, typename DerivedF, typename DerivedK>
  4. IGL_INLINE void igl::parallel_transport_angles(
  5. const Eigen::PlainObjectBase<DerivedV>& V,
  6. const Eigen::PlainObjectBase<DerivedF>& F,
  7. const Eigen::PlainObjectBase<DerivedV>& FN,
  8. const Eigen::MatrixXi &E2F,
  9. const Eigen::MatrixXi &F2E,
  10. Eigen::PlainObjectBase<DerivedK> &K)
  11. {
  12. int numE = E2F.rows();
  13. Eigen::VectorXi isBorderEdge;
  14. isBorderEdge.setZero(numE,1);
  15. for(unsigned i=0; i<numE; ++i)
  16. {
  17. if ((E2F(i,0) == -1) || ((E2F(i,1) == -1)))
  18. isBorderEdge[i] = 1;
  19. }
  20. K.setZero(numE);
  21. // For every non-border edge
  22. for (unsigned eid=0; eid<numE; ++eid)
  23. {
  24. if (!isBorderEdge[eid])
  25. {
  26. int fid0 = E2F(eid,0);
  27. int fid1 = E2F(eid,1);
  28. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> N0 = FN.row(fid0);
  29. // Eigen::Matrix<typename DerivedV::Scalar, 1, 3> N1 = FN.row(fid1);
  30. // find common edge on triangle 0 and 1
  31. int fid0_vc = -1;
  32. int fid1_vc = -1;
  33. for (unsigned i=0;i<3;++i)
  34. {
  35. if (F2E(fid0,i) == eid)
  36. fid0_vc = i;
  37. if (F2E(fid1,i) == eid)
  38. fid1_vc = i;
  39. }
  40. assert(fid0_vc != -1);
  41. assert(fid1_vc != -1);
  42. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> common_edge = V.row(F(fid0,(fid0_vc+1)%3)) - V.row(F(fid0,fid0_vc));
  43. common_edge.normalize();
  44. // Map the two triangles in a new space where the common edge is the x axis and the N0 the z axis
  45. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> P;
  46. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> o = V.row(F(fid0,fid0_vc));
  47. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> tmp = -N0.cross(common_edge);
  48. P << common_edge, tmp, N0;
  49. // P.transposeInPlace();
  50. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> V0;
  51. V0.row(0) = V.row(F(fid0,0)) -o;
  52. V0.row(1) = V.row(F(fid0,1)) -o;
  53. V0.row(2) = V.row(F(fid0,2)) -o;
  54. V0 = (P*V0.transpose()).transpose();
  55. // assert(V0(0,2) < 1e-10);
  56. // assert(V0(1,2) < 1e-10);
  57. // assert(V0(2,2) < 1e-10);
  58. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> V1;
  59. V1.row(0) = V.row(F(fid1,0)) -o;
  60. V1.row(1) = V.row(F(fid1,1)) -o;
  61. V1.row(2) = V.row(F(fid1,2)) -o;
  62. V1 = (P*V1.transpose()).transpose();
  63. // assert(V1(fid1_vc,2) < 10e-10);
  64. // assert(V1((fid1_vc+1)%3,2) < 10e-10);
  65. // compute rotation R such that R * N1 = N0
  66. // i.e. map both triangles to the same plane
  67. double alpha = -atan2(V1((fid1_vc+2)%3,2),V1((fid1_vc+2)%3,1));
  68. Eigen::Matrix<typename DerivedV::Scalar, 3, 3> R;
  69. R << 1, 0, 0,
  70. 0, cos(alpha), -sin(alpha) ,
  71. 0, sin(alpha), cos(alpha);
  72. V1 = (R*V1.transpose()).transpose();
  73. // assert(V1(0,2) < 1e-10);
  74. // assert(V1(1,2) < 1e-10);
  75. // assert(V1(2,2) < 1e-10);
  76. // measure the angle between the reference frames
  77. // k_ij is the angle between the triangle on the left and the one on the right
  78. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> ref0 = V0.row(1) - V0.row(0);
  79. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> ref1 = V1.row(1) - V1.row(0);
  80. ref0.normalize();
  81. ref1.normalize();
  82. double ktemp = atan2(ref1(1),ref1(0)) - atan2(ref0(1),ref0(0));
  83. // just to be sure, rotate ref0 using angle ktemp...
  84. Eigen::Matrix<typename DerivedV::Scalar,2,2> R2;
  85. R2 << cos(ktemp), -sin(ktemp), sin(ktemp), cos(ktemp);
  86. // Eigen::Matrix<typename DerivedV::Scalar, 1, 2> tmp1 = R2*(ref0.head(2)).transpose();
  87. // assert(tmp1(0) - ref1(0) < 1e-10);
  88. // assert(tmp1(1) - ref1(1) < 1e-10);
  89. K[eid] = ktemp;
  90. }
  91. }
  92. }
  93. #ifdef IGL_STATIC_LIBRARY
  94. // Explicit template specialization
  95. template void igl::parallel_transport_angles<Eigen::Matrix<double, -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<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  96. #endif