cross_field_missmatch.cpp 5.9 KB

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