per_face_normals.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "per_face_normals.h"
  9. #include <Eigen/Geometry>
  10. #define SQRT_ONE_OVER_THREE 0.57735026918962573
  11. template <typename DerivedV, typename DerivedF, typename DerivedZ, typename DerivedN>
  12. IGL_INLINE void igl::per_face_normals(
  13. const Eigen::PlainObjectBase<DerivedV>& V,
  14. const Eigen::PlainObjectBase<DerivedF>& F,
  15. const Eigen::PlainObjectBase<DerivedZ> & Z,
  16. Eigen::PlainObjectBase<DerivedN> & N)
  17. {
  18. N.resize(F.rows(),3);
  19. // loop over faces
  20. int Frows = F.rows();
  21. #pragma omp parallel for
  22. for(int i = 0; i < Frows;i++)
  23. {
  24. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> v1 = V.row(F(i,1)) - V.row(F(i,0));
  25. Eigen::Matrix<typename DerivedV::Scalar, 1, 3> v2 = V.row(F(i,2)) - V.row(F(i,0));
  26. N.row(i) = v1.cross(v2);//.normalized();
  27. typename DerivedV::Scalar r = N.row(i).norm();
  28. if(r == 0)
  29. {
  30. N.row(i) = Z;
  31. }else
  32. {
  33. N.row(i) /= r;
  34. }
  35. }
  36. }
  37. template <typename DerivedV, typename DerivedF, typename DerivedN>
  38. IGL_INLINE void igl::per_face_normals(
  39. const Eigen::PlainObjectBase<DerivedV>& V,
  40. const Eigen::PlainObjectBase<DerivedF>& F,
  41. Eigen::PlainObjectBase<DerivedN> & N)
  42. {
  43. using namespace Eigen;
  44. Matrix<typename DerivedN::Scalar,3,1> Z(0,0,0);
  45. return per_face_normals(V,F,Z,N);
  46. }
  47. #ifndef IGL_HEADER_ONLY
  48. // Explicit template specialization
  49. 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> >&);
  50. 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> >&);
  51. template void igl::per_face_normals<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&);
  52. #endif