line_field_missmatch.cpp 4.7 KB

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