cross_field_missmatch.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "cross_field_missmatch.h"
  2. #include "comb_cross_field.h"
  3. #include <vector>
  4. #include <deque>
  5. #include "per_face_normals.h"
  6. #include "is_border_vertex.h"
  7. #include "vf.h"
  8. #include "tt.h"
  9. namespace igl {
  10. template <typename DerivedV, typename DerivedF, typename DerivedO>
  11. class MissMatchCalculator
  12. {
  13. public:
  14. const Eigen::PlainObjectBase<DerivedV> &V;
  15. const Eigen::PlainObjectBase<DerivedF> &F;
  16. const Eigen::PlainObjectBase<DerivedV> &PD1;
  17. const Eigen::PlainObjectBase<DerivedV> &PD2;
  18. Eigen::PlainObjectBase<DerivedV> N;
  19. private:
  20. // internal
  21. std::vector<bool> V_border; // bool
  22. std::vector<std::vector<int> > VF;
  23. std::vector<std::vector<int> > VFi;
  24. Eigen::PlainObjectBase<DerivedF> TT;
  25. Eigen::PlainObjectBase<DerivedF> TTi;
  26. private:
  27. ///return true if a vertex is singluar by looking at initialized missmatches
  28. // possible bugs, verify deleted flag vs IsD()
  29. // not sorted vf, but should not make a difference
  30. // olga: TODO: this returns the index modulo 4.
  31. int oneRingMissMatch(const int vid)
  32. {
  33. ///check that is on border..
  34. if (V_border[vid])
  35. return 0;
  36. int missmatch=0;
  37. for (unsigned int i=0;i<VF[vid].size();i++)
  38. {
  39. // look for the vertex
  40. int j=-1;
  41. for (unsigned z=0; z<3; ++z)
  42. if (F(VF[vid][i],z) == vid)
  43. j=z;
  44. assert(j!=-1);
  45. missmatch+=Handle_MMatch(VF[vid][i],j);
  46. }
  47. missmatch=missmatch%4;
  48. return missmatch;
  49. }
  50. ///compute the mismatch between 2 faces
  51. int MissMatchByCross(const int f0,
  52. const int f1)
  53. {
  54. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir0 = PD1.row(f0);
  55. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir1 = PD1.row(f1);
  56. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n0 = N.row(f0);
  57. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n1 = N.row(f1);
  58. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir1Rot = Comb<DerivedV, DerivedF>::Rotate(n1,n0,dir1);
  59. dir1Rot.normalize();
  60. // TODO: this should be equivalent to the other code below, to check!
  61. // Compute the angle between the two vectors
  62. // double a0 = atan2(dir0.dot(B2.row(f0)),dir0.dot(B1.row(f0)));
  63. // double a1 = atan2(dir1Rot.dot(B2.row(f0)),dir1Rot.dot(B1.row(f0)));
  64. //
  65. // double angle_diff = a1-a0; //VectToAngle(f0,dir1Rot);
  66. double angle_diff = atan2(dir1Rot.dot(PD2.row(f0)),dir1Rot.dot(PD1.row(f0)));
  67. // std::cerr << "Dani: " << dir0(0) << " " << dir0(1) << " " << dir0(2) << " " << dir1Rot(0) << " " << dir1Rot(1) << " " << dir1Rot(2) << " " << angle_diff << std::endl;
  68. double step=M_PI/2.0;
  69. int i=(int)floor((angle_diff/step)+0.5);
  70. int k=0;
  71. if (i>=0)
  72. k=i%4;
  73. else
  74. k=(-(3*i))%4;
  75. return k;
  76. }
  77. public:
  78. MissMatchCalculator(const Eigen::PlainObjectBase<DerivedV> &_V,
  79. const Eigen::PlainObjectBase<DerivedF> &_F,
  80. const Eigen::PlainObjectBase<DerivedV> &_PD1,
  81. const Eigen::PlainObjectBase<DerivedV> &_PD2
  82. ):
  83. V(_V),
  84. F(_F),
  85. PD1(_PD1),
  86. PD2(_PD2)
  87. {
  88. igl::per_face_normals(V,F,N);
  89. V_border = igl::is_border_vertex(V,F);
  90. igl::vf(V,F,VF,VFi);
  91. igl::tt(V,F,TT,TTi);
  92. }
  93. void calculateMissmatch(Eigen::PlainObjectBase<DerivedO> &Handle_MMatch)
  94. {
  95. Handle_MMatch.setConstant(F.rows(),3,-1);
  96. for (unsigned int i=0;i<F.rows();i++)
  97. {
  98. for (int j=0;j<3;j++)
  99. {
  100. if (i==TT(i,j) || TT(i,j) == -1)
  101. Handle_MMatch(i,j)=0;
  102. else
  103. Handle_MMatch(i,j) = MissMatchByCross(i,TT(i,j));
  104. }
  105. }
  106. }
  107. };
  108. }
  109. template <typename DerivedV, typename DerivedF, typename DerivedO>
  110. IGL_INLINE void igl::cross_field_missmatch(const Eigen::PlainObjectBase<DerivedV> &V,
  111. const Eigen::PlainObjectBase<DerivedF> &F,
  112. const Eigen::PlainObjectBase<DerivedV> &PD1,
  113. const Eigen::PlainObjectBase<DerivedV> &PD2,
  114. const bool isCombed,
  115. Eigen::PlainObjectBase<DerivedO> &missmatch)
  116. {
  117. Eigen::PlainObjectBase<DerivedV> PD1_combed;
  118. Eigen::PlainObjectBase<DerivedV> PD2_combed;
  119. if (!isCombed)
  120. igl::comb_cross_field(V,F,PD1,PD2,PD1_combed,PD2_combed);
  121. else
  122. {
  123. PD1_combed = PD1;
  124. PD2_combed = PD2;
  125. }
  126. igl::MissMatchCalculator<DerivedV, DerivedF, DerivedO> sf(V, F, PD1_combed, PD2_combed);
  127. sf.calculateMissmatch(missmatch);
  128. }