cross_field_missmatch.cpp 5.4 KB

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