outer_facet.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "outer_facet.h"
  2. #include "sort.h"
  3. #include "vertex_triangle_adjacency.h"
  4. #include <iostream>
  5. #define IGL_OUTER_FACET_DEBUG
  6. template <
  7. typename DerivedV,
  8. typename DerivedF,
  9. typename DerivedN,
  10. typename DerivedI,
  11. typename f_type>
  12. IGL_INLINE void igl::outer_facet(
  13. const Eigen::PlainObjectBase<DerivedV> & V,
  14. const Eigen::PlainObjectBase<DerivedF> & F,
  15. const Eigen::PlainObjectBase<DerivedN> & N,
  16. const Eigen::PlainObjectBase<DerivedI> & I,
  17. f_type & max_f,
  18. bool & flip)
  19. {
  20. using namespace std;
  21. typedef typename DerivedV::Scalar Scalar;
  22. typedef typename DerivedV::Index Index;
  23. typedef
  24. typename Eigen::Matrix<Index, DerivedV::RowsAtCompileTime,1> VectorXI;
  25. typedef
  26. typename Eigen::Matrix<Scalar, DerivedV::RowsAtCompileTime,1> VectorXS;
  27. // "Direct repair of self-intersecting meshes" [Attene 14]
  28. const Index mi = I.size();
  29. assert(V.cols() == 3);
  30. assert(N.cols() == 3);
  31. Index max_v = -1;
  32. for(size_t d = 0;d<(size_t)V.cols();d++)
  33. {
  34. Scalar max_d = -1e26;
  35. Scalar max_nd = -1e26;
  36. for(Index i = 0;i<mi;i++)
  37. {
  38. const Index f = I(i);
  39. const Scalar nd = N(f,d);
  40. if(fabs(nd)>0)
  41. {
  42. for(Index c = 0;c<3;c++)
  43. {
  44. const Index v = F(f,c);
  45. if(v == max_v)
  46. {
  47. if(fabs(nd) > max_nd)
  48. {
  49. // Just update max face and normal
  50. max_f = f;
  51. max_nd = fabs(nd);
  52. flip = nd<0;
  53. }
  54. }else
  55. {
  56. const Scalar vd = V(v,d);
  57. if(vd>max_d)
  58. {
  59. // update max vertex, face and normal
  60. max_v = v;
  61. max_d = vd;
  62. max_f = f;
  63. max_nd = fabs(nd);
  64. flip = nd<0;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. if(max_v >= 0)
  71. {
  72. break;
  73. }
  74. // if we get here and max_v is still -1 then there were no faces with
  75. // |N(d)| > 0
  76. }
  77. #ifdef IGL_OUTER_FACET_DEBUG
  78. if(max_v <0)
  79. {
  80. cerr<<"Very degenerate case, no suitable face found."<<endl;
  81. }
  82. #endif
  83. assert(max_v >=0 && "Very degenerate case, no suitable face found.");
  84. }
  85. #ifdef IGL_STATIC_LIBRARY
  86. // Explicit template specialization
  87. template void igl::outer_facet<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<long, -1, 1, 0, -1, 1>, int>(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<long, -1, 1, 0, -1, 1> > const&, int&, bool&);
  88. template void igl::outer_facet<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 1, -1, -1>, Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 1, -1, -1>, unsigned long>(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 1, -1, -1> > const&, unsigned long&, bool&);
  89. template void igl::outer_facet<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::Matrix<int, -1, 1, 0, -1, 1>, int>(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> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int&, bool&);
  90. template void igl::outer_facet<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, int>(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<int, -1, 1, 0, -1, 1> > const&, int&, bool&);
  91. #endif