per_face_normals.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include "per_face_normals.h"
  2. #include <Eigen/Geometry>
  3. #define SQRT_ONE_OVER_THREE 0.57735026918962573
  4. template <typename DerivedV, typename DerivedF>
  5. IGL_INLINE void igl::per_face_normals(
  6. const Eigen::PlainObjectBase<DerivedV>& V,
  7. const Eigen::PlainObjectBase<DerivedF>& F,
  8. Eigen::PlainObjectBase<DerivedV> & N)
  9. {
  10. N.resize(F.rows(),3);
  11. // loop over faces
  12. int Frows = F.rows();
  13. #pragma omp parallel for
  14. for(int i = 0; i < Frows;i++)
  15. {
  16. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> v1 = V.row(F(i,1)) - V.row(F(i,0));
  17. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> v2 = V.row(F(i,2)) - V.row(F(i,0));
  18. N.row(i) = v1.cross(v2);//.normalized();
  19. if(N.row(i).sum() == 0)
  20. {
  21. N(i,0) = SQRT_ONE_OVER_THREE;
  22. N(i,1) = SQRT_ONE_OVER_THREE;
  23. N(i,2) = SQRT_ONE_OVER_THREE;
  24. }else
  25. {
  26. N.row(i) = N.row(i).normalized().eval();
  27. }
  28. }
  29. }
  30. #ifndef IGL_HEADER_ONLY
  31. // Explicit template specialization
  32. // generated by autoexplicit.sh
  33. template void igl::per_face_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&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  34. template void igl::per_face_normals<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >(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> >&);
  35. #endif