parallel_transport_angles.cpp 4.6 KB

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