orient_outward.cpp 3.3 KB

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