per_vertex_normals.cpp 5.5 KB

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