sharp_edges.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "sharp_edges.h"
  2. #include <igl/unique_edge_map.h>
  3. #include <igl/per_face_normals.h>
  4. #include <igl/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<Index,Eigen::Dynamic,2> MatrixX2I;
  29. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,3> MatrixX3S;
  30. typedef Eigen::Matrix<Scalar,1,3> RowVector3S;
  31. typedef Eigen::Matrix<Index,Eigen::Dynamic,1> VectorXI;
  32. unique_edge_map(F,E,uE,EMAP,uE2E);
  33. MatrixX3S N;
  34. per_face_normals(V,F,N);
  35. // number of faces
  36. const Index m = F.rows();
  37. // Dihedral angles
  38. //std::vector<Eigen::Triplet<Scalar,int> > DIJV;
  39. sharp.clear();
  40. // Loop over each unique edge
  41. for(int u = 0;u<uE2E.size();u++)
  42. {
  43. bool u_is_sharp = false;
  44. // Consider every pair of incident faces
  45. for(int i = 0;i<uE2E[u].size();i++)
  46. for(int j = i+1;j<uE2E[u].size();j++)
  47. {
  48. const int ei = uE2E[u][i];
  49. const int fi = ei%m;
  50. const int ej = uE2E[u][j];
  51. const int fj = ej%m;
  52. const RowVector3S ni = N.row(fi);
  53. const RowVector3S nj = N.row(fj);
  54. // Edge vector
  55. // normalization might not be necessary
  56. const RowVector3S ev = (V.row(E(ei,1)) - V.row(E(ei,0))).normalized();
  57. const Scalar dij =
  58. igl::PI - atan2((ni.cross(nj)).dot(ev),ni.dot(nj));
  59. //DIJV.emplace_back(fi,fj,dij);
  60. if(std::abs(dij-igl::PI) > angle)
  61. {
  62. u_is_sharp = true;
  63. }
  64. }
  65. if(u_is_sharp)
  66. {
  67. sharp.push_back(u);
  68. }
  69. }
  70. SE.resize(sharp.size(),2);
  71. for(int i = 0;i<SE.rows();i++)
  72. {
  73. SE(i,0) = uE(sharp[i],0);
  74. SE(i,1) = uE(sharp[i],1);
  75. }
  76. }
  77. template <
  78. typename DerivedV,
  79. typename DerivedF,
  80. typename DerivedSE>
  81. IGL_INLINE void igl::sharp_edges(
  82. const Eigen::MatrixBase<DerivedV> & V,
  83. const Eigen::MatrixBase<DerivedF> & F,
  84. const typename DerivedV::Scalar angle,
  85. Eigen::PlainObjectBase<DerivedSE> & SE
  86. )
  87. {
  88. typedef typename DerivedSE::Scalar Index;
  89. typedef typename DerivedV::Scalar Scalar;
  90. typedef Eigen::Matrix<Index,Eigen::Dynamic,2> MatrixX2I;
  91. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,3> MatrixX3S;
  92. typedef Eigen::Matrix<Scalar,1,3> RowVector3S;
  93. typedef Eigen::Matrix<Index,Eigen::Dynamic,1> VectorXI;
  94. MatrixX2I E,uE;
  95. VectorXI EMAP;
  96. std::vector<std::vector<Index> > uE2E;
  97. std::vector<int> sharp;
  98. return sharp_edges(V,F,angle,SE,E,uE,EMAP,uE2E,sharp);
  99. }
  100. #ifdef IGL_STATIC_LIBRARY
  101. // Explicit template instantiation
  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::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> >&);
  103. 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> >&);
  104. #endif