orient_outward.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "orient_outward.h"
  2. #include "per_face_normals.h"
  3. #include "barycenter.h"
  4. #include "doublearea.h"
  5. #include "matlab_format.h"
  6. #include <iostream>
  7. template <
  8. typename DerivedV,
  9. typename DerivedF,
  10. typename DerivedC,
  11. typename DerivedFF,
  12. typename DerivedI>
  13. IGL_INLINE void igl::orient_outward(
  14. const Eigen::PlainObjectBase<DerivedV> & V,
  15. const Eigen::PlainObjectBase<DerivedF> & F,
  16. const Eigen::PlainObjectBase<DerivedC> & C,
  17. Eigen::PlainObjectBase<DerivedFF> & FF,
  18. Eigen::PlainObjectBase<DerivedI> & I)
  19. {
  20. using namespace Eigen;
  21. using namespace std;
  22. assert(C.rows() == F.rows());
  23. assert(F.cols() == 3);
  24. assert(V.cols() == 3);
  25. // number of faces
  26. const int m = F.rows();
  27. // number of patches
  28. const int num_cc = C.maxCoeff()+1;
  29. I.resize(num_cc);
  30. if(&FF != &F)
  31. {
  32. FF = F;
  33. }
  34. PlainObjectBase<DerivedV> N,BC,BCmean;
  35. Matrix<typename DerivedV::Scalar,Dynamic,1> A;
  36. VectorXd totA(num_cc), dot(num_cc);
  37. Matrix<typename DerivedV::Scalar,3,1> Z(1,1,1);
  38. per_face_normals(V,F,Z.normalized(),N);
  39. barycenter(V,F,BC);
  40. doublearea(V,F,A);
  41. BCmean.setConstant(num_cc,3,0);
  42. dot.setConstant(num_cc,1,0);
  43. totA.setConstant(num_cc,1,0);
  44. // loop over faces
  45. for(int f = 0;f<m;f++)
  46. {
  47. BCmean.row(C(f)) += A(f)*BC.row(f);
  48. totA(C(f))+=A(f);
  49. }
  50. // take area weighted average
  51. for(int c = 0;c<num_cc;c++)
  52. {
  53. BCmean.row(c) /= (typename DerivedV::Scalar) totA(c);
  54. }
  55. // subtract bcmean
  56. for(int f = 0;f<m;f++)
  57. {
  58. BC.row(f) -= BCmean.row(C(f));
  59. dot(C(f)) += A(f)*N.row(f).dot(BC.row(f));
  60. }
  61. // take area weighted average
  62. for(int c = 0;c<num_cc;c++)
  63. {
  64. dot(c) /= (typename DerivedV::Scalar) totA(c);
  65. if(dot(c) < 0)
  66. {
  67. I(c) = true;
  68. }else
  69. {
  70. I(c) = false;
  71. }
  72. }
  73. // flip according to I
  74. for(int f = 0;f<m;f++)
  75. {
  76. if(I(C(f)))
  77. {
  78. FF.row(f) = FF.row(f).reverse().eval();
  79. }
  80. }
  81. }
  82. #ifndef IGL_HEADER_ONLY
  83. // Explicit template specialization
  84. template void igl::orient_outward<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -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<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  85. #endif