per_vertex_normals.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. double t_start;
  42. t_start = get_seconds();
  43. // Resize for output
  44. N.setZero(V.rows(),3);
  45. Eigen::MatrixXd W(F.rows(),3);
  46. switch(weighting)
  47. {
  48. case PER_VERTEX_NORMALS_WEIGHTING_TYPE_UNIFORM:
  49. W.setConstant(1.);
  50. break;
  51. default:
  52. assert(false && "Unknown weighting type");
  53. case PER_VERTEX_NORMALS_WEIGHTING_TYPE_DEFAULT:
  54. case PER_VERTEX_NORMALS_WEIGHTING_TYPE_AREA:
  55. {
  56. Eigen::VectorXd A;
  57. doublearea(V,F,A);
  58. W = A.replicate(1,3);
  59. break;
  60. }
  61. case PER_VERTEX_NORMALS_WEIGHTING_TYPE_ANGLE:
  62. internal_angles(V,F,W);
  63. break;
  64. }
  65. // loop over faces
  66. const int Frows = F.rows();
  67. //// Minimum number of iterms per openmp thread
  68. //#ifndef IGL_OMP_MIN_VALUE
  69. //# define IGL_OMP_MIN_VALUE 1000
  70. //#endif
  71. //#pragma omp parallel for if (Frows>IGL_OMP_MIN_VALUE)
  72. for(int i = 0; i < Frows;i++)
  73. {
  74. // throw normal at each corner
  75. for(int j = 0; j < 3;j++)
  76. {
  77. // Q: Does this need to be critical?
  78. // A: Yes. Different (i,j)'s could produce the same F(i,j)
  79. //#pragma omp critical
  80. N.row(F(i,j)) += W(i,j)*FN.row(i);
  81. }
  82. }
  83. // take average via normalization
  84. N.rowwise().normalize();
  85. }
  86. template <typename DerivedV, typename DerivedF>
  87. IGL_INLINE void igl::per_vertex_normals(
  88. const Eigen::PlainObjectBase<DerivedV>& V,
  89. const Eigen::PlainObjectBase<DerivedF>& F,
  90. const Eigen::PlainObjectBase<DerivedV>& FN,
  91. Eigen::PlainObjectBase<DerivedV> & N)
  92. {
  93. return
  94. per_vertex_normals(V,F,PER_VERTEX_NORMALS_WEIGHTING_TYPE_DEFAULT,FN,N);
  95. }
  96. #ifdef IGL_STATIC_LIBRARY
  97. // Explicit template specialization
  98. 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> >&);
  99. 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> >&);
  100. #endif