line_field_missmatch.cpp 4.7 KB

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