per_face_normals.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef IGL_PER_FACE_NORMALS_H
  2. #define IGL_PER_FACE_NORMALS_H
  3. #include <Eigen/Core>
  4. namespace igl
  5. {
  6. // Compute face normals via vertex position list, face list
  7. // Inputs:
  8. // V #V by 3 eigen Matrix of mesh vertex 3D positions
  9. // F #F by 3 eigne Matrix of face (triangle) indices
  10. // Output:
  11. // N #F by 3 eigen Matrix of mesh face (triangle) 3D normals
  12. inline void per_face_normals(
  13. const Eigen::MatrixXd & V,
  14. const Eigen::MatrixXi & F,
  15. Eigen::MatrixXd & N);
  16. }
  17. // Implementation
  18. inline void igl::per_face_normals(
  19. const Eigen::MatrixXd & V,
  20. const Eigen::MatrixXi & F,
  21. Eigen::MatrixXd & N)
  22. {
  23. N.resize(F.rows(),3);
  24. // loop over faces
  25. for(int i = 0; i < F.rows();i++)
  26. {
  27. float v1[3];
  28. v1[0] = V(F(i,1),0) - V(F(i,0),0);
  29. v1[1] = V(F(i,1),1) - V(F(i,0),1);
  30. v1[2] = V(F(i,1),2) - V(F(i,0),2);
  31. float v2[3];
  32. v2[0] = V(F(i,2),0) - V(F(i,0),0);
  33. v2[1] = V(F(i,2),1) - V(F(i,0),1);
  34. v2[2] = V(F(i,2),2) - V(F(i,0),2);
  35. N(i,0) = v1[1]*v2[2] - v1[2]*v2[1];
  36. N(i,1) = -(v1[0]*v2[2] - v1[2]*v2[0]);
  37. N(i,2) = v1[0]*v2[1] - v1[1]*v2[0];
  38. float length = sqrt(
  39. N(i,0)*N(i,0) +
  40. N(i,1)*N(i,1) +
  41. N(i,2)*N(i,2));
  42. N(i,0) /= length;
  43. N(i,1) /= length;
  44. N(i,2) /= length;
  45. }
  46. }
  47. #endif