per_edge_normals.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "all_edges.h"
  2. #include "doublearea.h"
  3. #include "per_edge_normals.h"
  4. #include "per_face_normals.h"
  5. #include "unique_simplices.h"
  6. #include <vector>
  7. template <
  8. typename DerivedV,
  9. typename DerivedF,
  10. typename DerivedFN,
  11. typename DerivedN,
  12. typename DerivedE,
  13. typename DerivedEMAP>
  14. IGL_INLINE void igl::per_edge_normals(
  15. const Eigen::PlainObjectBase<DerivedV>& V,
  16. const Eigen::PlainObjectBase<DerivedF>& F,
  17. const PerEdgeNormalsWeightingType weighting,
  18. const Eigen::PlainObjectBase<DerivedFN>& FN,
  19. Eigen::PlainObjectBase<DerivedN> & N,
  20. Eigen::PlainObjectBase<DerivedE> & E,
  21. Eigen::PlainObjectBase<DerivedEMAP> & EMAP)
  22. {
  23. using namespace Eigen;
  24. using namespace std;
  25. assert(F.cols() == 3 && "Faces must be triangles");
  26. // number of faces
  27. const int m = F.rows();
  28. // All occurances of directed edges
  29. MatrixXi allE;
  30. all_edges(F,allE);
  31. // Find unique undirected edges and mapping
  32. VectorXi _;
  33. unique_simplices(allE,E,_,EMAP);
  34. // now sort(allE,2) == E(EMAP,:), that is, if EMAP(i) = j, then E.row(j) is
  35. // the undirected edge corresponding to the directed edge allE.row(i).
  36. Eigen::VectorXd W(F.rows());
  37. switch(weighting)
  38. {
  39. case PER_EDGE_NORMALS_WEIGHTING_TYPE_UNIFORM:
  40. W.setConstant(1.);
  41. break;
  42. default:
  43. assert(false && "Unknown weighting type");
  44. case PER_EDGE_NORMALS_WEIGHTING_TYPE_DEFAULT:
  45. case PER_EDGE_NORMALS_WEIGHTING_TYPE_AREA:
  46. {
  47. doublearea(V,F,W);
  48. break;
  49. }
  50. }
  51. N.setConstant(E.rows(),3,0);
  52. for(int f = 0;f<m;f++)
  53. {
  54. for(int c = 0;c<3;c++)
  55. {
  56. N.row(EMAP(f+c*m)) += W(f) * FN.row(f);
  57. }
  58. }
  59. N.rowwise().normalize();
  60. }
  61. template <
  62. typename DerivedV,
  63. typename DerivedF,
  64. typename DerivedN,
  65. typename DerivedE,
  66. typename DerivedEMAP>
  67. IGL_INLINE void igl::per_edge_normals(
  68. const Eigen::PlainObjectBase<DerivedV>& V,
  69. const Eigen::PlainObjectBase<DerivedF>& F,
  70. const PerEdgeNormalsWeightingType weighting,
  71. Eigen::PlainObjectBase<DerivedN> & N,
  72. Eigen::PlainObjectBase<DerivedE> & E,
  73. Eigen::PlainObjectBase<DerivedEMAP> & EMAP)
  74. {
  75. Eigen::PlainObjectBase<DerivedN> FN;
  76. per_face_normals(V,F,FN);
  77. return per_edge_normals(V,F,weighting,FN,N,E,EMAP);
  78. }
  79. template <
  80. typename DerivedV,
  81. typename DerivedF,
  82. typename DerivedN,
  83. typename DerivedE,
  84. typename DerivedEMAP>
  85. IGL_INLINE void igl::per_edge_normals(
  86. const Eigen::PlainObjectBase<DerivedV>& V,
  87. const Eigen::PlainObjectBase<DerivedF>& F,
  88. Eigen::PlainObjectBase<DerivedN> & N,
  89. Eigen::PlainObjectBase<DerivedE> & E,
  90. Eigen::PlainObjectBase<DerivedEMAP> & EMAP)
  91. {
  92. return
  93. per_edge_normals(V,F,PER_EDGE_NORMALS_WEIGHTING_TYPE_DEFAULT,N,E,EMAP);
  94. }
  95. #ifdef IGL_STATIC_LIBRARY
  96. // Explicit template instanciation
  97. template void igl::per_edge_normals<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, 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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -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> >&);
  98. #endif