is_delaunay.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "is_delaunay.h"
  2. #include "unique_edge_map.h"
  3. #include <cassert>
  4. template <
  5. typename DerivedV,
  6. typename DerivedF,
  7. typename DerivedD>
  8. IGL_INLINE void igl::is_delaunay(
  9. const Eigen::MatrixBase<DerivedV> & V,
  10. const Eigen::MatrixBase<DerivedF> & F,
  11. Eigen::PlainObjectBase<DerivedD> & D)
  12. {
  13. typedef Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,2> MatrixX2I;
  14. typedef Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,1> VectorXI;
  15. MatrixX2I E,uE;
  16. VectorXI EMAP;
  17. std::vector<std::vector<typename DerivedF::Scalar> > uE2E;
  18. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  19. const int num_faces = F.rows();
  20. D.setConstant(F.rows(),F.cols(),false);
  21. const auto D_at = [&D,&num_faces](const int he)->typename DerivedD::Scalar&
  22. {
  23. const int f = he%num_faces;
  24. const int c = he/num_faces;
  25. return D(f,c);
  26. };
  27. typedef typename DerivedV::Scalar Scalar;
  28. // Should use Shewchuk's predicates instead.
  29. const auto float_incircle = [](
  30. const Scalar pa[2],
  31. const Scalar pb[2],
  32. const Scalar pc[2],
  33. const Scalar pd[2])->short
  34. {
  35. const Eigen::Matrix3d A = (Eigen::Matrix3d(3,3)<<
  36. pa[0]-pd[0], pa[1]-pd[1],(pa[0]-pd[0])*(pa[0]-pd[0])+(pa[1]-pd[1])*(pa[1]-pd[1]),
  37. pb[0]-pd[0], pb[1]-pd[1],(pb[0]-pd[0])*(pb[0]-pd[0])+(pb[1]-pd[1])*(pb[1]-pd[1]),
  38. pc[0]-pd[0], pc[1]-pd[1],(pc[0]-pd[0])*(pc[0]-pd[0])+(pc[1]-pd[1])*(pc[1]-pd[1])
  39. ).finished();
  40. const Scalar detA = A.determinant();
  41. return (Scalar(0) < detA) - (detA < Scalar(0));
  42. };
  43. // loop over all unique edges
  44. for(int ue = 0;ue < uE2E.size(); ue++)
  45. {
  46. bool ue_is_d = false;
  47. // Is boundary?
  48. switch(uE2E[ue].size())
  49. {
  50. case 1:
  51. ue_is_d = true;
  52. break;
  53. case 2:
  54. {
  55. ue_is_d = is_delaunay(V,F,uE2E,float_incircle,ue);
  56. break;
  57. }
  58. default:
  59. ue_is_d = false;
  60. break;
  61. }
  62. for(int e = 0;e<uE2E[ue].size();e++)
  63. {
  64. D_at(uE2E[ue][e]) = ue_is_d;
  65. }
  66. }
  67. }
  68. template <
  69. typename DerivedV,
  70. typename DerivedF,
  71. typename uE2EType,
  72. typename InCircle,
  73. typename ueiType>
  74. IGL_INLINE bool igl::is_delaunay(
  75. const Eigen::MatrixBase<DerivedV> & V,
  76. const Eigen::MatrixBase<DerivedF> & F,
  77. const std::vector<std::vector<uE2EType> > & uE2E,
  78. const InCircle incircle,
  79. const ueiType uei)
  80. {
  81. const int num_faces = F.rows();
  82. typedef typename DerivedV::Scalar Scalar;
  83. const auto& half_edges = uE2E[uei];
  84. assert((half_edges.size() == 2) && "uE2E[uei].size() should be 2");
  85. const size_t f1 = half_edges[0] % num_faces;
  86. const size_t f2 = half_edges[1] % num_faces;
  87. const size_t c1 = half_edges[0] / num_faces;
  88. const size_t c2 = half_edges[1] / num_faces;
  89. assert(c1 < 3);
  90. assert(c2 < 3);
  91. assert(f1 != f2);
  92. const size_t v1 = F(f1, (c1+1)%3);
  93. const size_t v2 = F(f1, (c1+2)%3);
  94. const size_t v4 = F(f1, c1);
  95. const size_t v3 = F(f2, c2);
  96. const Scalar p1[] = {V(v1, 0), V(v1, 1)};
  97. const Scalar p2[] = {V(v2, 0), V(v2, 1)};
  98. const Scalar p3[] = {V(v3, 0), V(v3, 1)};
  99. const Scalar p4[] = {V(v4, 0), V(v4, 1)};
  100. auto orientation = incircle(p1, p2, p4, p3);
  101. return orientation <= 0;
  102. }
  103. #ifdef IGL_STATIC_LIBRARY
  104. // Explicit template instantiation
  105. // generated by autoexplicit.sh
  106. template void igl::is_delaunay<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<bool, -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::PlainObjectBase<Eigen::Matrix<bool, -1, -1, 0, -1, -1> >&);
  107. // generated by autoexplicit.sh
  108. template bool igl::is_delaunay<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, int, short (*)(double const*, double const*, double const*, double const*), unsigned long>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, short (*)(double const*, double const*, double const*, double const*), unsigned long);
  109. #endif