is_intrinsic_delaunay.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_intrinsic_delaunay.h"
  9. #include "unique_edge_map.h"
  10. #include "tan_half_angle.h"
  11. #include "EPS.h"
  12. #include "matlab_format.h"
  13. #include "cotmatrix_entries.h"
  14. #include <iostream>
  15. #include <cassert>
  16. template <
  17. typename Derivedl,
  18. typename DerivedF,
  19. typename DerivedD>
  20. IGL_INLINE void igl::is_intrinsic_delaunay(
  21. const Eigen::MatrixBase<Derivedl> & l,
  22. const Eigen::MatrixBase<DerivedF> & F,
  23. Eigen::PlainObjectBase<DerivedD> & D)
  24. {
  25. typedef Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,2> MatrixX2I;
  26. typedef Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,1> VectorXI;
  27. MatrixX2I E,uE;
  28. VectorXI EMAP;
  29. std::vector<std::vector<typename DerivedF::Scalar> > uE2E;
  30. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  31. return is_intrinsic_delaunay(l,F,uE2E,D);
  32. }
  33. template <
  34. typename Derivedl,
  35. typename DerivedF,
  36. typename uE2EType,
  37. typename DerivedD>
  38. IGL_INLINE void igl::is_intrinsic_delaunay(
  39. const Eigen::MatrixBase<Derivedl> & l,
  40. const Eigen::MatrixBase<DerivedF> & F,
  41. const std::vector<std::vector<uE2EType> > & uE2E,
  42. Eigen::PlainObjectBase<DerivedD> & D)
  43. {
  44. const int num_faces = F.rows();
  45. D.setConstant(F.rows(),F.cols(),false);
  46. // loop over all unique edges
  47. for(int ue = 0;ue < uE2E.size(); ue++)
  48. {
  49. const bool ue_is_d = is_intrinsic_delaunay(l,uE2E,num_faces,ue);
  50. // Set for all instances
  51. for(int e = 0;e<uE2E[ue].size();e++)
  52. {
  53. D( uE2E[ue][e]%num_faces, uE2E[ue][e]/num_faces) = ue_is_d;
  54. }
  55. }
  56. };
  57. template <
  58. typename Derivedl,
  59. typename uE2EType,
  60. typename Index>
  61. IGL_INLINE bool igl::is_intrinsic_delaunay(
  62. const Eigen::MatrixBase<Derivedl> & l,
  63. const std::vector<std::vector<uE2EType> > & uE2E,
  64. const Index num_faces,
  65. const Index uei)
  66. {
  67. if(uE2E[uei].size() == 1) return true;
  68. if(uE2E[uei].size() > 2) return false;
  69. typedef typename Derivedl::Scalar Scalar;
  70. const auto cot_alpha = [](
  71. const Scalar & a,
  72. const Scalar & b,
  73. const Scalar & c)->Scalar
  74. {
  75. // Fisher 2007
  76. const Scalar t = tan_half_angle(a,b,c);
  77. return (1.0-t*t)/(2*t);
  78. };
  79. // 2 //
  80. // /|\ //
  81. // a/ | \d //
  82. // / e \ //
  83. // / | \ //
  84. //0.α---|-f-β.3 //
  85. // \ | / //
  86. // \ | / //
  87. // b\ | /c //
  88. // \|/ //
  89. // . //
  90. // 1
  91. // Fisher 2007
  92. assert(uE2E[uei].size() == 2 && "edge should have 2 incident faces");
  93. const Index he1 = uE2E[uei][0];
  94. const Index he2 = uE2E[uei][1];
  95. const Index f1 = he1%num_faces;
  96. const Index c1 = he1/num_faces;
  97. const Index f2 = he2%num_faces;
  98. const Index c2 = he2/num_faces;
  99. assert( std::abs(l(f1,c1)-l(f2,c2)) < igl::EPS<Scalar>());
  100. const Scalar e = l(f1,c1);
  101. const Scalar a = l(f1,(c1+1)%3);
  102. const Scalar b = l(f1,(c1+2)%3);
  103. const Scalar c = l(f2,(c2+1)%3);
  104. const Scalar d = l(f2,(c2+2)%3);
  105. const Scalar w = cot_alpha(e,a,b) + cot_alpha(e,c,d);
  106. //// Test
  107. //{
  108. // Eigen::MatrixXd l(2,3);
  109. // l<<e,a,b,
  110. // d,e,c;
  111. // //Eigen::MatrixXi F(2,3);
  112. // //F<<0,1,2,
  113. // // 1,3,2;
  114. // Eigen::MatrixXd C;
  115. // cotmatrix_entries(l,C);
  116. // if(std::abs(w-(2.0*(C(0,0)+C(1,1))))>1e-10)
  117. // {
  118. // std::cout<<matlab_format(l,"l")<<std::endl;
  119. // std::cout<<w<<std::endl;
  120. // std::cout<<(2.0*(C(0,0)+C(1,1)))<<std::endl;
  121. // std::cout<<w-(2.0*(C(0,0)+C(1,1)))<<std::endl;
  122. // std::cout<<std::endl;
  123. // }
  124. //}
  125. return w >= 0;
  126. }
  127. #ifdef IGL_STATIC_LIBRARY
  128. // Explicit template instantiation
  129. // generated by autoexplicit.sh
  130. template bool igl::is_intrinsic_delaunay<Eigen::Matrix<double, -1, 3, 0, -1, 3>, int, int>(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, int, int);
  131. // generated by autoexplicit.sh
  132. template bool igl::is_intrinsic_delaunay<Eigen::Matrix<double, -1, -1, 0, -1, -1>, int, int>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, int, int);
  133. // generated by autoexplicit.sh
  134. template void igl::is_intrinsic_delaunay<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, int, 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&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 3, 0, -1, 3> >&);
  135. // generated by autoexplicit.sh
  136. template void igl::is_intrinsic_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> >&);
  137. // generated by autoexplicit.sh
  138. template void igl::is_intrinsic_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> >&);
  139. #endif