line_field_mismatch.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Nico Pietroni <[email protected]>
  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 "line_field_mismatch.h"
  9. #include <vector>
  10. #include <deque>
  11. #include "comb_line_field.h"
  12. #include "rotate_vectors.h"
  13. #include "comb_cross_field.h"
  14. #include "comb_line_field.h"
  15. #include "per_face_normals.h"
  16. #include "is_border_vertex.h"
  17. #include "vertex_triangle_adjacency.h"
  18. #include "triangle_triangle_adjacency.h"
  19. #include "rotation_matrix_from_directions.h"
  20. #include "local_basis.h"
  21. #include "PI.h"
  22. #include "PlainMatrix.h"
  23. namespace igl {
  24. template <typename DerivedV, typename DerivedF, typename DerivedO>
  25. class MismatchCalculatorLine
  26. {
  27. public:
  28. const Eigen::MatrixBase<DerivedV> &V;
  29. const Eigen::MatrixBase<DerivedF> &F;
  30. const Eigen::MatrixBase<DerivedV> &PD1;
  31. const Eigen::MatrixBase<DerivedV> &PD2;
  32. PlainMatrix<DerivedV> N;
  33. private:
  34. // internal
  35. std::vector<bool> V_border; // bool
  36. std::vector<std::vector<int> > VF;
  37. std::vector<std::vector<int> > VFi;
  38. PlainMatrix<DerivedF> TT;
  39. PlainMatrix<DerivedF> TTi;
  40. private:
  41. //compute the mismatch between 2 faces
  42. inline int mismatchByLine(const int f0,
  43. const int f1)
  44. {
  45. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir0 = PD1.row(f0);
  46. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir1 = PD1.row(f1);
  47. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n0 = N.row(f0);
  48. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> n1 = N.row(f1);
  49. Eigen::Matrix<typename DerivedV::Scalar, 3, 1> dir1Rot = igl::rotation_matrix_from_directions(n1,n0)*dir1;
  50. dir1Rot.normalize();
  51. // TODO: this should be equivalent to the other code below, to check!
  52. // Compute the angle between the two vectors
  53. // double a0 = atan2(dir0.dot(B2.row(f0)),dir0.dot(B1.row(f0)));
  54. // double a1 = atan2(dir1Rot.dot(B2.row(f0)),dir1Rot.dot(B1.row(f0)));
  55. //
  56. // double angle_diff = a1-a0; //VectToAngle(f0,dir1Rot);
  57. double angle_diff = atan2(dir1Rot.dot(PD2.row(f0)),dir1Rot.dot(PD1.row(f0)));
  58. double step=igl::PI;
  59. int i=(int)std::floor((angle_diff/step)+0.5);
  60. assert((i>=-2)&&(i<=2));
  61. int k=0;
  62. if (i>=0)
  63. k=i%2;
  64. else
  65. k=(2+i)%2;
  66. assert((k==0)||(k==1));
  67. return (k*2);
  68. }
  69. public:
  70. inline MismatchCalculatorLine(const Eigen::MatrixBase<DerivedV> &_V,
  71. const Eigen::MatrixBase<DerivedF> &_F,
  72. const Eigen::MatrixBase<DerivedV> &_PD1,
  73. const Eigen::MatrixBase<DerivedV> &_PD2
  74. ):
  75. V(_V),
  76. F(_F),
  77. PD1(_PD1),
  78. PD2(_PD2)
  79. {
  80. igl::per_face_normals(V,F,N);
  81. V_border = igl::is_border_vertex(F);
  82. igl::vertex_triangle_adjacency(V,F,VF,VFi);
  83. igl::triangle_triangle_adjacency(F,TT,TTi);
  84. }
  85. inline void calculateMismatchLine(Eigen::PlainObjectBase<DerivedO> &Handle_MMatch)
  86. {
  87. Handle_MMatch.setConstant(F.rows(),3,-1);
  88. for (unsigned int i=0;i<F.rows();i++)
  89. {
  90. for (int j=0;j<3;j++)
  91. {
  92. if (i==TT(i,j) || TT(i,j) == -1)
  93. Handle_MMatch(i,j)=0;
  94. else
  95. Handle_MMatch(i,j) = mismatchByLine(i, TT(i, j));
  96. }
  97. }
  98. }
  99. };
  100. }
  101. template <typename DerivedV, typename DerivedF, typename DerivedO>
  102. IGL_INLINE void igl::line_field_mismatch(const Eigen::MatrixBase<DerivedV> &V,
  103. const Eigen::MatrixBase<DerivedF> &F,
  104. const Eigen::MatrixBase<DerivedV> &PD1,
  105. const bool isCombed,
  106. Eigen::PlainObjectBase<DerivedO> &mismatch)
  107. {
  108. DerivedV PD1_combed;
  109. DerivedV PD2_combed;
  110. if (!isCombed)
  111. igl::comb_line_field(V,F,PD1,PD1_combed);
  112. else
  113. {
  114. PD1_combed = PD1;
  115. }
  116. Eigen::MatrixXd B1,B2,B3;
  117. igl::local_basis(V,F,B1,B2,B3);
  118. PD2_combed = igl::rotate_vectors(PD1_combed, Eigen::VectorXd::Constant(1,igl::PI/2), B1, B2);
  119. igl::MismatchCalculatorLine<DerivedV, DerivedF, DerivedO> sf(V, F, PD1_combed, PD2_combed);
  120. sf.calculateMismatchLine(mismatch);
  121. }
  122. #ifdef IGL_STATIC_LIBRARY
  123. // Explicit template instantiation
  124. #endif