per_vertex_normals.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_vertex_normals.h"
  9. #include "per_face_normals.h"
  10. #include "normalize_row_lengths.h"
  11. template <typename DerivedV, typename DerivedF>
  12. IGL_INLINE void igl::per_vertex_normals(
  13. const Eigen::PlainObjectBase<DerivedV>& V,
  14. const Eigen::PlainObjectBase<DerivedF>& F,
  15. Eigen::PlainObjectBase<DerivedV> & N)
  16. {
  17. Eigen::PlainObjectBase<DerivedV> PFN;
  18. igl::per_face_normals(V,F,PFN);
  19. return igl::per_vertex_normals(V,F,PFN,N);
  20. }
  21. template <typename DerivedV, typename DerivedF>
  22. IGL_INLINE void igl::per_vertex_normals(
  23. const Eigen::PlainObjectBase<DerivedV>& V,
  24. const Eigen::PlainObjectBase<DerivedF>& F,
  25. const Eigen::PlainObjectBase<DerivedV>& FN,
  26. Eigen::PlainObjectBase<DerivedV> & N)
  27. {
  28. // Resize for output
  29. N.setZero(V.rows(),3);
  30. // loop over faces
  31. int Frows = F.rows();
  32. #pragma omp parallel for
  33. for(int i = 0; i < Frows;i++)
  34. {
  35. // throw normal at each corner
  36. for(int j = 0; j < 3;j++)
  37. {
  38. N.row(F(i,j)) += FN.row(i);
  39. }
  40. }
  41. // normalize each row
  42. igl::normalize_row_lengths(N,N);
  43. }
  44. #ifndef IGL_HEADER_ONLY
  45. // Explicit template specialization
  46. // generated by autoexplicit.sh
  47. template void igl::per_vertex_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> >&);
  48. template void igl::per_vertex_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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&);
  49. template void igl::per_vertex_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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  50. #endif