per_face_normals.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "per_face_normals.h"
  2. #include <Eigen/Geometry>
  3. #define SQRT_ONE_OVER_THREE 0.57735026918962573
  4. template <typename DerivedV, typename DerivedF, typename DerivedZ, typename DerivedN>
  5. IGL_INLINE void igl::per_face_normals(
  6. const Eigen::PlainObjectBase<DerivedV>& V,
  7. const Eigen::PlainObjectBase<DerivedF>& F,
  8. const Eigen::PlainObjectBase<DerivedZ> & Z,
  9. Eigen::PlainObjectBase<DerivedN> & N)
  10. {
  11. N.resize(F.rows(),3);
  12. // loop over faces
  13. int Frows = F.rows();
  14. #pragma omp parallel for
  15. for(int i = 0; i < Frows;i++)
  16. {
  17. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> v1 = V.row(F(i,1)) - V.row(F(i,0));
  18. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> v2 = V.row(F(i,2)) - V.row(F(i,0));
  19. N.row(i) = v1.cross(v2);//.normalized();
  20. typename DerivedV::Scalar r = N.row(i).norm();
  21. if(r == 0)
  22. {
  23. N.row(i) = Z;
  24. }else
  25. {
  26. N.row(i) /= r;
  27. }
  28. }
  29. }
  30. template <typename DerivedV, typename DerivedF, typename DerivedN>
  31. IGL_INLINE void igl::per_face_normals(
  32. const Eigen::PlainObjectBase<DerivedV>& V,
  33. const Eigen::PlainObjectBase<DerivedF>& F,
  34. Eigen::PlainObjectBase<DerivedN> & N)
  35. {
  36. using namespace Eigen;
  37. Matrix<typename DerivedN::Scalar,3,1> Z(0,0,0);
  38. return per_face_normals(V,F,Z,N);
  39. }
  40. #ifndef IGL_HEADER_ONLY
  41. // Explicit template specialization
  42. 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> >&);
  43. template void igl::per_face_normals<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 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, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  44. #endif