cross_field_missmatch.cpp 5.3 KB

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