cross_field_missmatch.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. ///compute the mismatch between 2 faces
  36. inline int MissMatchByCross(const int f0,
  37. const int f1)
  38. {
  39. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir0 = PD1.row(f0);
  40. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir1 = PD1.row(f1);
  41. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n0 = N.row(f0);
  42. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n1 = N.row(f1);
  43. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir1Rot = igl::rotation_matrix_from_directions(n1,n0)*dir1;
  44. dir1Rot.normalize();
  45. // TODO: this should be equivalent to the other code below, to check!
  46. // Compute the angle between the two vectors
  47. // double a0 = atan2(dir0.dot(B2.row(f0)),dir0.dot(B1.row(f0)));
  48. // double a1 = atan2(dir1Rot.dot(B2.row(f0)),dir1Rot.dot(B1.row(f0)));
  49. //
  50. // double angle_diff = a1-a0; //VectToAngle(f0,dir1Rot);
  51. double angle_diff = atan2(dir1Rot.dot(PD2.row(f0)),dir1Rot.dot(PD1.row(f0)));
  52. // std::cerr << "Dani: " << dir0(0) << " " << dir0(1) << " " << dir0(2) << " " << dir1Rot(0) << " " << dir1Rot(1) << " " << dir1Rot(2) << " " << angle_diff << std::endl;
  53. double step=M_PI/2.0;
  54. int i=(int)floor((angle_diff/step)+0.5);
  55. int k=0;
  56. if (i>=0)
  57. k=i%4;
  58. else
  59. k=(-(3*i))%4;
  60. return k;
  61. }
  62. public:
  63. inline MissMatchCalculator(const Eigen::PlainObjectBase<DerivedV> &_V,
  64. const Eigen::PlainObjectBase<DerivedF> &_F,
  65. const Eigen::PlainObjectBase<DerivedV> &_PD1,
  66. const Eigen::PlainObjectBase<DerivedV> &_PD2
  67. ):
  68. V(_V),
  69. F(_F),
  70. PD1(_PD1),
  71. PD2(_PD2)
  72. {
  73. igl::per_face_normals(V,F,N);
  74. V_border = igl::is_border_vertex(V,F);
  75. igl::vf(V,F,VF,VFi);
  76. igl::tt(V,F,TT,TTi);
  77. }
  78. inline void calculateMissmatch(Eigen::PlainObjectBase<DerivedO> &Handle_MMatch)
  79. {
  80. Handle_MMatch.setConstant(F.rows(),3,-1);
  81. for (unsigned int i=0;i<F.rows();i++)
  82. {
  83. for (int j=0;j<3;j++)
  84. {
  85. if (i==TT(i,j) || TT(i,j) == -1)
  86. Handle_MMatch(i,j)=0;
  87. else
  88. Handle_MMatch(i,j) = MissMatchByCross(i,TT(i,j));
  89. }
  90. }
  91. }
  92. };
  93. }
  94. template <typename DerivedV, typename DerivedF, typename DerivedO>
  95. IGL_INLINE void igl::cross_field_missmatch(const Eigen::PlainObjectBase<DerivedV> &V,
  96. const Eigen::PlainObjectBase<DerivedF> &F,
  97. const Eigen::PlainObjectBase<DerivedV> &PD1,
  98. const Eigen::PlainObjectBase<DerivedV> &PD2,
  99. const bool isCombed,
  100. Eigen::PlainObjectBase<DerivedO> &missmatch)
  101. {
  102. Eigen::PlainObjectBase<DerivedV> PD1_combed;
  103. Eigen::PlainObjectBase<DerivedV> PD2_combed;
  104. if (!isCombed)
  105. igl::comb_cross_field(V,F,PD1,PD2,PD1_combed,PD2_combed);
  106. else
  107. {
  108. PD1_combed = PD1;
  109. PD2_combed = PD2;
  110. }
  111. igl::MissMatchCalculator<DerivedV, DerivedF, DerivedO> sf(V, F, PD1_combed, PD2_combed);
  112. sf.calculateMissmatch(missmatch);
  113. }
  114. #ifndef IGL_HEADER_ONLY
  115. // Explicit template specialization
  116. #endif