per_vertex_normals.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef IGL_PER_VERTEX_NORMALS_H
  2. #define IGL_PER_VERTEX_NORMALS_H
  3. #include <Eigen/Core>
  4. // Note: So for this only computes normals per vertex as uniformly weighted
  5. // averages of incident triangle normals. It would be nice to support more or
  6. // all of the methods here:
  7. // "A comparison of algorithms for vertex normal computation"
  8. namespace igl
  9. {
  10. // Compute vertex normals via vertex position list, face list
  11. // Inputs:
  12. // V #V by 3 eigen Matrix of mesh vertex 3D positions
  13. // F #F by 3 eigne Matrix of face (triangle) indices
  14. // Output:
  15. // N #V by 3 eigen Matrix of mesh vertex 3D normals
  16. inline void per_vertex_normals(
  17. const Eigen::MatrixXd & V,
  18. const Eigen::MatrixXi & F,
  19. Eigen::MatrixXd & N);
  20. }
  21. // Implementation
  22. #include "per_face_normals.h"
  23. #include "normalize_rows.h"
  24. inline void igl::per_vertex_normals(
  25. const Eigen::MatrixXd & V,
  26. const Eigen::MatrixXi & F,
  27. Eigen::MatrixXd & N)
  28. {
  29. Eigen::MatrixXd PFN;
  30. igl::per_face_normals(V,F,PFN);
  31. // Resize for output
  32. N.resize(V.rows(),3);
  33. // loop over vertices, setting normalize to 0
  34. for(int i = 0; i < N.rows();i++)
  35. {
  36. N(i,0) = 0;
  37. N(i,1) = 0;
  38. N(i,2) = 0;
  39. }
  40. // loop over faces
  41. for(int i = 0; i < F.rows();i++)
  42. {
  43. // throw normal at each corner
  44. for(int j = 0; j < 3;j++)
  45. {
  46. N(F(i,j),0) += PFN(i,0);
  47. N(F(i,j),1) += PFN(i,1);
  48. N(F(i,j),2) += PFN(i,2);
  49. }
  50. }
  51. // normalize each row
  52. igl::normalize_rows(N,N);
  53. }
  54. #endif