orient_outward.cpp 2.8 KB

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