per_face_normals.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 if (Frows>10000)
  22. for(int i = 0; i < Frows;i++)
  23. {
  24. const Eigen::Matrix<typename DerivedV::Scalar, 1, 3> v1 = V.row(F(i,1)) - V.row(F(i,0));
  25. const 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. template <typename DerivedV, typename DerivedF, typename DerivedN>
  48. IGL_INLINE void igl::per_face_normals_stable(
  49. const Eigen::PlainObjectBase<DerivedV>& V,
  50. const Eigen::PlainObjectBase<DerivedF>& F,
  51. Eigen::PlainObjectBase<DerivedN> & N)
  52. {
  53. using namespace Eigen;
  54. typedef Matrix<typename DerivedV::Scalar,1,3> RowVectorV3;
  55. typedef typename DerivedV::Scalar Scalar;
  56. const size_t m = F.rows();
  57. N.resize(F.rows(),3);
  58. // Grad all points
  59. for(size_t f = 0;f<m;f++)
  60. {
  61. const RowVectorV3 p0 = V.row(F(f,0));
  62. const RowVectorV3 p1 = V.row(F(f,1));
  63. const RowVectorV3 p2 = V.row(F(f,2));
  64. const RowVectorV3 n0 = (p1 - p0).cross(p2 - p0);
  65. const RowVectorV3 n1 = (p2 - p1).cross(p0 - p1);
  66. const RowVectorV3 n2 = (p0 - p2).cross(p1 - p2);
  67. // careful sum
  68. for(int d = 0;d<3;d++)
  69. {
  70. // This is a little _silly_ in terms of complexity, but its recursive
  71. // implementation is clean looking...
  72. const std::function<Scalar(Scalar,Scalar,Scalar)> sum3 =
  73. [&sum3](Scalar a, Scalar b, Scalar c)->Scalar
  74. {
  75. if(fabs(c)>fabs(a))
  76. {
  77. return sum3(c,b,a);
  78. }
  79. // c < a
  80. if(fabs(c)>fabs(b))
  81. {
  82. return sum3(a,c,b);
  83. }
  84. // c < a, c < b
  85. if(fabs(b)>fabs(a))
  86. {
  87. return sum3(b,a,c);
  88. }
  89. return (a+b)+c;
  90. };
  91. N(f,d) = sum3(n0(d),n1(d),n2(d));
  92. }
  93. // sum better not be sure, or else NaN
  94. N.row(f) /= N.row(f).norm();
  95. }
  96. }
  97. #ifdef IGL_STATIC_LIBRARY
  98. // Explicit template specialization
  99. // generated by autoexplicit.sh
  100. template void igl::per_face_normals<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3>, Eigen::Matrix<float, -1, 3, 0, -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, 0, -1, 3> >&);
  101. // generated by autoexplicit.sh
  102. template void igl::per_face_normals<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3>, Eigen::Matrix<float, -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. 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> >&);
  104. 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> >&);
  105. 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> >&);
  106. template void igl::per_face_normals<Eigen::Matrix<float, -1, -1, 1, -1, -1>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1>, Eigen::Matrix<float, -1, -1, 1, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 1, -1, -1> >&);
  107. 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, -1, 3, 0, -1, 3> >(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, 3, 0, -1, 3> >&);
  108. template void igl::per_face_normals_stable<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> >&);
  109. template void igl::per_face_normals_stable<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 3, 0, -1, 3> >(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, 3, 0, -1, 3> >&);
  110. #endif