is_delaunay.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 typename DerivedV::Scalar Scalar;
  14. // Should use Shewchuk's predicates instead.
  15. const auto float_incircle = [](
  16. const Scalar pa[2],
  17. const Scalar pb[2],
  18. const Scalar pc[2],
  19. const Scalar pd[2])->short
  20. {
  21. // I acknowledge that I am cating to double
  22. const Eigen::Matrix3d A = (Eigen::Matrix3d(3,3)<<
  23. pa[0]-pd[0], pa[1]-pd[1],(pa[0]-pd[0])*(pa[0]-pd[0])+(pa[1]-pd[1])*(pa[1]-pd[1]),
  24. pb[0]-pd[0], pb[1]-pd[1],(pb[0]-pd[0])*(pb[0]-pd[0])+(pb[1]-pd[1])*(pb[1]-pd[1]),
  25. pc[0]-pd[0], pc[1]-pd[1],(pc[0]-pd[0])*(pc[0]-pd[0])+(pc[1]-pd[1])*(pc[1]-pd[1])
  26. ).finished();
  27. const Scalar detA = A.determinant();
  28. return (Scalar(0) < detA) - (detA < Scalar(0));
  29. };
  30. typedef Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,2> MatrixX2I;
  31. typedef Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,1> VectorXI;
  32. MatrixX2I E,uE;
  33. VectorXI EMAP;
  34. std::vector<std::vector<typename DerivedF::Scalar> > uE2E;
  35. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  36. const int num_faces = F.rows();
  37. D.setConstant(F.rows(),F.cols(),false);
  38. // loop over all unique edges
  39. for(int ue = 0;ue < uE2E.size(); ue++)
  40. {
  41. const bool ue_is_d = is_delaunay(V,F,uE2E,float_incircle,ue);
  42. // Set for all instances
  43. for(int e = 0;e<uE2E[ue].size();e++)
  44. {
  45. D( uE2E[ue][e]%num_faces, uE2E[ue][e]/num_faces) = ue_is_d;
  46. }
  47. }
  48. }
  49. template <
  50. typename DerivedV,
  51. typename DerivedF,
  52. typename uE2EType,
  53. typename InCircle,
  54. typename ueiType>
  55. IGL_INLINE bool igl::is_delaunay(
  56. const Eigen::MatrixBase<DerivedV> & V,
  57. const Eigen::MatrixBase<DerivedF> & F,
  58. const std::vector<std::vector<uE2EType> > & uE2E,
  59. const InCircle incircle,
  60. const ueiType uei)
  61. {
  62. if(uE2E[uei].size() == 1) return true;
  63. if(uE2E[uei].size() > 2) return false;
  64. const int num_faces = F.rows();
  65. typedef typename DerivedV::Scalar Scalar;
  66. const auto& half_edges = uE2E[uei];
  67. assert((half_edges.size() == 2) && "uE2E[uei].size() should be 2");
  68. const size_t f1 = half_edges[0] % num_faces;
  69. const size_t f2 = half_edges[1] % num_faces;
  70. const size_t c1 = half_edges[0] / num_faces;
  71. const size_t c2 = half_edges[1] / num_faces;
  72. assert(c1 < 3);
  73. assert(c2 < 3);
  74. assert(f1 != f2);
  75. const size_t v1 = F(f1, (c1+1)%3);
  76. const size_t v2 = F(f1, (c1+2)%3);
  77. const size_t v4 = F(f1, c1);
  78. const size_t v3 = F(f2, c2);
  79. const Scalar p1[] = {V(v1, 0), V(v1, 1)};
  80. const Scalar p2[] = {V(v2, 0), V(v2, 1)};
  81. const Scalar p3[] = {V(v3, 0), V(v3, 1)};
  82. const Scalar p4[] = {V(v4, 0), V(v4, 1)};
  83. auto orientation = incircle(p1, p2, p4, p3);
  84. return orientation <= 0;
  85. }
  86. #ifdef IGL_STATIC_LIBRARY
  87. // Explicit template instantiation
  88. // generated by autoexplicit.sh
  89. 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> >&);
  90. // generated by autoexplicit.sh
  91. 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);
  92. #endif