per_corner_normals.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "per_corner_normals.h"
  2. #include "vf.h"
  3. #include "per_face_normals.h"
  4. #include "PI.h"
  5. IGL_INLINE void igl::per_corner_normals(
  6. const Eigen::MatrixXd & V,
  7. const Eigen::MatrixXi & F,
  8. const double corner_threshold,
  9. Eigen::MatrixXd & CN)
  10. {
  11. using namespace igl;
  12. using namespace Eigen;
  13. using namespace std;
  14. MatrixXd FN;
  15. per_face_normals(V,F,FN);
  16. vector<vector<int> > VF,VFi;
  17. vf(V,F,VF,VFi);
  18. return per_corner_normals(V,F,FN,VF,corner_threshold,CN);
  19. }
  20. IGL_INLINE void igl::per_corner_normals(
  21. const Eigen::MatrixXd & V,
  22. const Eigen::MatrixXi & F,
  23. const Eigen::MatrixXd & FN,
  24. const std::vector<std::vector<int> >& VF,
  25. const double corner_threshold,
  26. Eigen::MatrixXd & CN)
  27. {
  28. using namespace igl;
  29. using namespace Eigen;
  30. using namespace std;
  31. // number of faces
  32. const int m = F.rows();
  33. // initialize output to zero
  34. CN = MatrixXd::Constant(m*3,3,0);
  35. // loop over faces
  36. for(size_t i = 0;i<m;i++)
  37. {
  38. // Normal of this face
  39. Vector3d fn = FN.row(i);
  40. // loop over corners
  41. for(size_t j = 0;j<3;j++)
  42. {
  43. std::vector<int> incident_faces = VF[F(i,j)];
  44. // loop over faces sharing vertex of this corner
  45. for(int k = 0;k<(int)incident_faces.size();k++)
  46. {
  47. Vector3d ifn = FN.row(incident_faces[k]);
  48. // dot product between face's normal and other face's normal
  49. double dp = fn.dot(ifn);
  50. // if difference in normal is slight then add to average
  51. if(dp > cos(corner_threshold*PI/180))
  52. {
  53. // add to running sum
  54. CN.row(i*3+j) += ifn;
  55. // else ignore
  56. }else
  57. {
  58. }
  59. }
  60. // normalize to take average
  61. double length = CN.row(i*3+j).norm();
  62. CN.row(i*3+j) /= length;
  63. }
  64. }
  65. }