per_vertex_normals.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "get_seconds.h"
  10. #include "per_face_normals.h"
  11. #include "doublearea.h"
  12. #include "internal_angles.h"
  13. template <typename DerivedV, typename DerivedF>
  14. IGL_INLINE void igl::per_vertex_normals(
  15. const Eigen::PlainObjectBase<DerivedV>& V,
  16. const Eigen::PlainObjectBase<DerivedF>& F,
  17. const igl::PerVertexNormalsWeightingType weighting,
  18. Eigen::PlainObjectBase<DerivedV> & N)
  19. {
  20. Eigen::PlainObjectBase<DerivedV> PFN;
  21. igl::per_face_normals(V,F,PFN);
  22. return per_vertex_normals(V,F,weighting,PFN,N);
  23. }
  24. template <typename DerivedV, typename DerivedF>
  25. IGL_INLINE void igl::per_vertex_normals(
  26. const Eigen::PlainObjectBase<DerivedV>& V,
  27. const Eigen::PlainObjectBase<DerivedF>& F,
  28. Eigen::PlainObjectBase<DerivedV> & N)
  29. {
  30. return per_vertex_normals(V,F,PER_VERTEX_NORMALS_WEIGHTING_TYPE_DEFAULT,N);
  31. }
  32. template <typename DerivedV, typename DerivedF, typename DerivedFN, typename DerivedN>
  33. IGL_INLINE void igl::per_vertex_normals(
  34. const Eigen::PlainObjectBase<DerivedV>& V,
  35. const Eigen::PlainObjectBase<DerivedF>& F,
  36. const igl::PerVertexNormalsWeightingType weighting,
  37. const Eigen::PlainObjectBase<DerivedFN>& FN,
  38. Eigen::PlainObjectBase<DerivedN> & N)
  39. {
  40. using namespace std;
  41. // Resize for output
  42. N.setZero(V.rows(),3);
  43. Eigen::MatrixXd W(F.rows(),3);
  44. switch(weighting)
  45. {
  46. case PER_VERTEX_NORMALS_WEIGHTING_TYPE_UNIFORM:
  47. W.setConstant(1.);
  48. break;
  49. default:
  50. assert(false && "Unknown weighting type");
  51. case PER_VERTEX_NORMALS_WEIGHTING_TYPE_DEFAULT:
  52. case PER_VERTEX_NORMALS_WEIGHTING_TYPE_AREA:
  53. {
  54. Eigen::VectorXd A;
  55. doublearea(V,F,A);
  56. W = A.replicate(1,3);
  57. break;
  58. }
  59. case PER_VERTEX_NORMALS_WEIGHTING_TYPE_ANGLE:
  60. internal_angles(V,F,W);
  61. break;
  62. }
  63. // loop over faces
  64. const int Frows = F.rows();
  65. //// Minimum number of iterms per openmp thread
  66. //#ifndef IGL_OMP_MIN_VALUE
  67. //# define IGL_OMP_MIN_VALUE 1000
  68. //#endif
  69. //#pragma omp parallel for if (Frows>IGL_OMP_MIN_VALUE)
  70. for(int i = 0; i < Frows;i++)
  71. {
  72. // throw normal at each corner
  73. for(int j = 0; j < 3;j++)
  74. {
  75. // Q: Does this need to be critical?
  76. // A: Yes. Different (i,j)'s could produce the same F(i,j)
  77. //#pragma omp critical
  78. N.row(F(i,j)) += W(i,j)*FN.row(i);
  79. }
  80. }
  81. // take average via normalization
  82. N.rowwise().normalize();
  83. }
  84. template <typename DerivedV, typename DerivedF>
  85. IGL_INLINE void igl::per_vertex_normals(
  86. const Eigen::PlainObjectBase<DerivedV>& V,
  87. const Eigen::PlainObjectBase<DerivedF>& F,
  88. const Eigen::PlainObjectBase<DerivedV>& FN,
  89. Eigen::PlainObjectBase<DerivedV> & N)
  90. {
  91. return
  92. per_vertex_normals(V,F,PER_VERTEX_NORMALS_WEIGHTING_TYPE_DEFAULT,FN,N);
  93. }
  94. #ifdef IGL_STATIC_LIBRARY
  95. // Explicit template specialization
  96. 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> >&);
  97. 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> >&);
  98. template void igl::per_vertex_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::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&, igl::PerVertexNormalsWeightingType, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&);
  99. template void igl::per_vertex_normals<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -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> >&);
  100. template void igl::per_vertex_normals<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&);
  101. #endif