per_corner_normals.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "per_corner_normals.h"
  2. #include "vf.h"
  3. #include "per_face_normals.h"
  4. #include "PI.h"
  5. template <typename DerivedV, typename DerivedF>
  6. IGL_INLINE void igl::per_corner_normals(
  7. const Eigen::PlainObjectBase<DerivedV>& V,
  8. const Eigen::PlainObjectBase<DerivedF>& F,
  9. const double corner_threshold,
  10. Eigen::PlainObjectBase<DerivedV> & CN)
  11. {
  12. using namespace igl;
  13. using namespace Eigen;
  14. using namespace std;
  15. Eigen::PlainObjectBase<DerivedV> FN;
  16. per_face_normals(V,F,FN);
  17. vector<vector<int> > VF,VFi;
  18. vf(V,F,VF,VFi);
  19. return per_corner_normals(V,F,FN,VF,corner_threshold,CN);
  20. }
  21. template <typename DerivedV, typename DerivedF, typename IndexType>
  22. IGL_INLINE void igl::per_corner_normals(
  23. const Eigen::PlainObjectBase<DerivedV>& V,
  24. const Eigen::PlainObjectBase<DerivedF>& F,
  25. const Eigen::PlainObjectBase<DerivedV>& FN,
  26. const std::vector<std::vector<IndexType> >& VF,
  27. const double corner_threshold,
  28. Eigen::PlainObjectBase<DerivedV> & CN)
  29. {
  30. using namespace igl;
  31. using namespace Eigen;
  32. using namespace std;
  33. // number of faces
  34. const int m = F.rows();
  35. // valence of faces
  36. const int n = F.cols();
  37. // initialize output to zero
  38. CN.resize(m*n,3);
  39. // loop over faces
  40. for(size_t i = 0;int(i)<m;i++)
  41. {
  42. // Normal of this face
  43. Eigen::Matrix<typename DerivedV::Scalar,3,1> fn = FN.row(i);
  44. // loop over corners
  45. for(size_t j = 0;int(j)<n;j++)
  46. {
  47. const std::vector<IndexType> &incident_faces = VF[F(i,j)];
  48. // loop over faces sharing vertex of this corner
  49. for(int k = 0;k<(int)incident_faces.size();k++)
  50. {
  51. Eigen::Matrix<typename DerivedV::Scalar,3,1> ifn = FN.row(incident_faces[k]);
  52. // dot product between face's normal and other face's normal
  53. double dp = fn.dot(ifn);
  54. // if difference in normal is slight then add to average
  55. if(dp > cos(corner_threshold*PI/180))
  56. {
  57. // add to running sum
  58. CN.row(i*n+j) += ifn;
  59. // else ignore
  60. }else
  61. {
  62. }
  63. }
  64. // normalize to take average
  65. CN.row(i*n+j).normalize();
  66. }
  67. }
  68. }
  69. #ifndef IGL_HEADER_ONLY
  70. // Explicit template specialization
  71. // generated by autoexplicit.sh
  72. template void igl::per_corner_normals<Eigen::Matrix<double, -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&, double, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  73. #endif