per_edge_normals.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 DerivedN,
  11. typename DerivedE,
  12. typename DerivedEMAP>
  13. IGL_INLINE void igl::per_edge_normals(
  14. const Eigen::PlainObjectBase<DerivedV>& V,
  15. const Eigen::PlainObjectBase<DerivedF>& F,
  16. const PerEdgeNormalsWeightingType weighting,
  17. Eigen::PlainObjectBase<DerivedN> & N,
  18. Eigen::PlainObjectBase<DerivedE> & E,
  19. Eigen::PlainObjectBase<DerivedEMAP> & EMAP)
  20. {
  21. using namespace Eigen;
  22. using namespace std;
  23. assert(F.cols() == 3 && "Faces must be triangles");
  24. // number of faces
  25. const int m = F.rows();
  26. // All occurances of directed edges
  27. MatrixXi allE;
  28. all_edges(F,allE);
  29. // Find unique undirected edges and mapping
  30. VectorXi _;
  31. unique_simplices(allE,E,_,EMAP);
  32. // now sort(allE,2) == E(EMAP,:), that is, if EMAP(i) = j, then E.row(j) is
  33. // the undirected edge corresponding to the directed edge allE.row(i).
  34. MatrixXd FN;
  35. per_face_normals(V,F,FN);
  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. Eigen::PlainObjectBase<DerivedN> & N,
  71. Eigen::PlainObjectBase<DerivedE> & E,
  72. Eigen::PlainObjectBase<DerivedEMAP> & EMAP)
  73. {
  74. return
  75. per_edge_normals(V,F,PER_EDGE_NORMALS_WEIGHTING_TYPE_DEFAULT,N,E,EMAP);
  76. }
  77. #ifdef IGL_STATIC_LIBRARY
  78. // Explicit template instanciation
  79. 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> >&);
  80. #endif