cross_field_missmatch.cpp 5.2 KB

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