sharp_edges.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "sharp_edges.h"
  2. #include "unique_edge_map.h"
  3. #include "per_face_normals.h"
  4. #include "PI.h"
  5. #include <Eigen/Geometry>
  6. template <
  7. typename DerivedV,
  8. typename DerivedF,
  9. typename DerivedSE,
  10. typename DerivedE,
  11. typename DeriveduE,
  12. typename DerivedEMAP,
  13. typename uE2Etype,
  14. typename sharptype>
  15. IGL_INLINE void igl::sharp_edges(
  16. const Eigen::MatrixBase<DerivedV> & V,
  17. const Eigen::MatrixBase<DerivedF> & F,
  18. const typename DerivedV::Scalar angle,
  19. Eigen::PlainObjectBase<DerivedSE> & SE,
  20. Eigen::PlainObjectBase<DerivedE> & E,
  21. Eigen::PlainObjectBase<DeriveduE> & uE,
  22. Eigen::PlainObjectBase<DerivedEMAP> & EMAP,
  23. std::vector<std::vector<uE2Etype> > & uE2E,
  24. std::vector< sharptype > & sharp)
  25. {
  26. typedef typename DerivedSE::Scalar Index;
  27. typedef typename DerivedV::Scalar Scalar;
  28. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,3> MatrixX3S;
  29. typedef Eigen::Matrix<Scalar,1,3> RowVector3S;
  30. unique_edge_map(F,E,uE,EMAP,uE2E);
  31. MatrixX3S N;
  32. per_face_normals(V,F,N);
  33. // number of faces
  34. const Index m = F.rows();
  35. // Dihedral angles
  36. //std::vector<Eigen::Triplet<Scalar,int> > DIJV;
  37. sharp.clear();
  38. // Loop over each unique edge
  39. for(int u = 0;u<uE2E.size();u++)
  40. {
  41. bool u_is_sharp = false;
  42. // Consider every pair of incident faces
  43. //
  44. // if there are 3 faces (non-manifold) it appears to follow that the edge
  45. // must be sharp if angle<60. Could skip those (they're likely small number
  46. // anyway).
  47. for(int i = 0;i<uE2E[u].size();i++)
  48. for(int j = i+1;j<uE2E[u].size();j++)
  49. {
  50. const int ei = uE2E[u][i];
  51. const int fi = ei%m;
  52. const int ej = uE2E[u][j];
  53. const int fj = ej%m;
  54. const RowVector3S ni = N.row(fi);
  55. const RowVector3S nj = N.row(fj);
  56. // Edge vector
  57. // normalization might not be necessary
  58. const RowVector3S ev = (V.row(E(ei,1)) - V.row(E(ei,0))).normalized();
  59. const Scalar dij =
  60. igl::PI - atan2((ni.cross(nj)).dot(ev),ni.dot(nj));
  61. //DIJV.emplace_back(fi,fj,dij);
  62. if(std::abs(dij-igl::PI) > angle)
  63. {
  64. u_is_sharp = true;
  65. }
  66. }
  67. if(u_is_sharp)
  68. {
  69. sharp.push_back(u);
  70. }
  71. }
  72. SE.resize(sharp.size(),2);
  73. for(int i = 0;i<SE.rows();i++)
  74. {
  75. SE(i,0) = uE(sharp[i],0);
  76. SE(i,1) = uE(sharp[i],1);
  77. }
  78. }
  79. template <
  80. typename DerivedV,
  81. typename DerivedF,
  82. typename DerivedSE>
  83. IGL_INLINE void igl::sharp_edges(
  84. const Eigen::MatrixBase<DerivedV> & V,
  85. const Eigen::MatrixBase<DerivedF> & F,
  86. const typename DerivedV::Scalar angle,
  87. Eigen::PlainObjectBase<DerivedSE> & SE
  88. )
  89. {
  90. typedef typename DerivedSE::Scalar Index;
  91. typedef Eigen::Matrix<Index,Eigen::Dynamic,2> MatrixX2I;
  92. typedef Eigen::Matrix<Index,Eigen::Dynamic,1> VectorXI;
  93. MatrixX2I E,uE;
  94. VectorXI EMAP;
  95. std::vector<std::vector<Index> > uE2E;
  96. std::vector<int> sharp;
  97. return sharp_edges(V,F,angle,SE,E,uE,EMAP,uE2E,sharp);
  98. }
  99. #ifdef IGL_STATIC_LIBRARY
  100. // Explicit template instantiation
  101. template void igl::sharp_edges<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  102. template void igl::sharp_edges<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, int, int>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&, std::vector<int, std::allocator<int> >&);
  103. #endif