is_delaunay.cpp 5.2 KB

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