per_corner_normals.cpp 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 DerivedFN, typename DerivedCN>
  22. IGL_INLINE void igl::per_corner_normals(
  23. const Eigen::PlainObjectBase<DerivedV>& V,
  24. const Eigen::PlainObjectBase<DerivedF>& F,
  25. const Eigen::PlainObjectBase<DerivedFN>& FN,
  26. const double corner_threshold,
  27. Eigen::PlainObjectBase<DerivedCN> & CN)
  28. {
  29. using namespace igl;
  30. using namespace Eigen;
  31. using namespace std;
  32. vector<vector<int> > VF,VFi;
  33. vf(V,F,VF,VFi);
  34. return per_corner_normals(V,F,FN,VF,corner_threshold,CN);
  35. }
  36. template <typename DerivedV, typename DerivedF, typename IndexType>
  37. IGL_INLINE void igl::per_corner_normals(
  38. const Eigen::PlainObjectBase<DerivedV>& /*V*/,
  39. const Eigen::PlainObjectBase<DerivedF>& F,
  40. const Eigen::PlainObjectBase<DerivedV>& FN,
  41. const std::vector<std::vector<IndexType> >& VF,
  42. const double corner_threshold,
  43. Eigen::PlainObjectBase<DerivedV> & CN)
  44. {
  45. using namespace igl;
  46. using namespace Eigen;
  47. using namespace std;
  48. // number of faces
  49. const int m = F.rows();
  50. // valence of faces
  51. const int n = F.cols();
  52. // initialize output to ***zero***
  53. CN.setZero(m*n,3);
  54. // loop over faces
  55. for(size_t i = 0;int(i)<m;i++)
  56. {
  57. // Normal of this face
  58. Eigen::Matrix<typename DerivedV::Scalar,3,1> fn = FN.row(i);
  59. // loop over corners
  60. for(size_t j = 0;int(j)<n;j++)
  61. {
  62. const std::vector<IndexType> &incident_faces = VF[F(i,j)];
  63. // loop over faces sharing vertex of this corner
  64. for(int k = 0;k<(int)incident_faces.size();k++)
  65. {
  66. Eigen::Matrix<typename DerivedV::Scalar,3,1> ifn = FN.row(incident_faces[k]);
  67. // dot product between face's normal and other face's normal
  68. double dp = fn.dot(ifn);
  69. // if difference in normal is slight then add to average
  70. if(dp > cos(corner_threshold*PI/180))
  71. {
  72. // add to running sum
  73. CN.row(i*n+j) += ifn;
  74. // else ignore
  75. }else
  76. {
  77. }
  78. }
  79. // normalize to take average
  80. CN.row(i*n+j).normalize();
  81. }
  82. }
  83. }
  84. #ifndef IGL_HEADER_ONLY
  85. // Explicit template specialization
  86. // generated by autoexplicit.sh
  87. 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> >&);
  88. template void igl::per_corner_normals<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1>, unsigned int>(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, std::vector<std::vector<unsigned int, std::allocator<unsigned int> >, std::allocator<std::vector<unsigned int, std::allocator<unsigned int> > > > const&, double, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&);
  89. template void igl::per_corner_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<double, -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> > const&, double, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  90. #endif