intrinsic_delaunay_triangulation.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "intrinsic_delaunay_triangulation.h"
  9. #include "is_intrinsic_delaunay.h"
  10. #include "tan_half_angle.h"
  11. #include "unique_edge_map.h"
  12. #include "flip_edge.h"
  13. #include "EPS.h"
  14. #include "matlab_format.h"
  15. #include <iostream>
  16. #include <queue>
  17. #include <map>
  18. template <
  19. typename Derivedl_in,
  20. typename DerivedF_in,
  21. typename Derivedl,
  22. typename DerivedF>
  23. IGL_INLINE void igl::intrinsic_delaunay_triangulation(
  24. const Eigen::MatrixBase<Derivedl_in> & l_in,
  25. const Eigen::MatrixBase<DerivedF_in> & F_in,
  26. Eigen::PlainObjectBase<Derivedl> & l,
  27. Eigen::PlainObjectBase<DerivedF> & F)
  28. {
  29. // We're going to work in place
  30. l = l_in;
  31. F = F_in;
  32. typedef Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,2> MatrixX2I;
  33. typedef Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,1> VectorXI;
  34. MatrixX2I E,uE;
  35. VectorXI EMAP;
  36. std::vector<std::vector<typename DerivedF::Scalar> > uE2E;
  37. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  38. typedef typename DerivedF::Scalar Index;
  39. typedef typename Derivedl::Scalar Scalar;
  40. const Index num_faces = F.rows();
  41. // Copied from delaunay_triangulation
  42. bool all_delaunay = false;
  43. // Dumb O(#E * #flips). Use queue and gather only edges that could have
  44. // changed to make this O(#E + #flips).
  45. while(!all_delaunay)
  46. {
  47. all_delaunay = true;
  48. for (size_t uei=0; uei<uE2E.size(); uei++)
  49. {
  50. if (uE2E[uei].size() == 2)
  51. {
  52. if(!is_intrinsic_delaunay(l,F,uE2E,uei))
  53. {
  54. // update l just before flipping edge
  55. // . //
  56. // /|\ //
  57. // a/ | \d //
  58. // / e \ //
  59. // / | \ //
  60. // .----|-f--. //
  61. // \ | / //
  62. // \ | / //
  63. // b\α|δ/c //
  64. // \|/ //
  65. // . //
  66. // Annotated from flip_edge:
  67. // Edge to flip [v1,v2] --> [v3,v4]
  68. // Before:
  69. // F(f1,:) = [v1,v2,v4] // in some cyclic order
  70. // F(f2,:) = [v1,v3,v2] // in some cyclic order
  71. // After:
  72. // F(f1,:) = [v1,v3,v4] // in *this* order
  73. // F(f2,:) = [v2,v4,v3] // in *this* order
  74. //
  75. // v1 v1
  76. // /|\ / \
  77. // c/ | \b c/f1 \b
  78. // v3 /f2|f1\ v4 => v3 /__f__\ v4
  79. // \ e / \ f2 /
  80. // d\ | /a d\ /a
  81. // \|/ \ /
  82. // v2 v2
  83. //
  84. // Compute intrinsic length of oppposite edge
  85. assert(uE2E[uei].size() == 2 && "edge should have 2 incident faces");
  86. const Index f1 = uE2E[uei][0]%num_faces;
  87. const Index f2 = uE2E[uei][1]%num_faces;
  88. const Index c1 = uE2E[uei][0]/num_faces;
  89. const Index c2 = uE2E[uei][1]/num_faces;
  90. assert(c1 < 3);
  91. assert(c2 < 3);
  92. assert(f1 != f2);
  93. const Index v1 = F(f1, (c1+1)%3);
  94. const Index v2 = F(f1, (c1+2)%3);
  95. const Index v4 = F(f1, c1);
  96. const Index v3 = F(f2, c2);
  97. assert(F(f2, (c2+2)%3) == v1);
  98. assert(F(f2, (c2+1)%3) == v2);
  99. // From gptoolbox/mesh/flip_edge.m
  100. // "If edge-after-flip already exists then this will create a non-manifold
  101. // edge"
  102. // Yes, this can happen: e.g., an edge of a tetrahedron."
  103. // "If two edges will be the same edge after flip then this will create a
  104. // non-manifold edge."
  105. // I dont' think this can happen if we flip one at a time. gptoolbox
  106. // flips in parallel.
  107. //
  108. // Does edge (a,b) exist in the edges of all faces incident on
  109. // existing unique edge uei.
  110. //
  111. // Inputs:
  112. // a 1st end-point of query edge
  113. // b 2nd end-point of query edge
  114. // uei index into uE/uE2E of unique edge
  115. // uE2E map from unique edges to half-edges (see unique_edge_map)
  116. // E #F*3 by 2 list of half-edges
  117. //
  118. const auto edge_exists_near =
  119. [&](const Index & a,const Index & b,const Index & uei)->bool
  120. {
  121. assert(a!=b);
  122. // Not handling case where (a,b) is edge of face incident on uei
  123. // since this can't happen for edge-flipping.
  124. assert(a!=uE(uei,0));
  125. assert(a!=uE(uei,1));
  126. assert(b!=uE(uei,0));
  127. assert(b!=uE(uei,1));
  128. // starting with the (2) faces incident on e, consider all faces
  129. // incident on edges containing either a or b.
  130. //
  131. // face_queue Queue containing faces incident on exactly one of a/b
  132. std::queue<Index> face_queue;
  133. const Index f1 = uE2E[uei][0]%num_faces;
  134. const Index f2 = uE2E[uei][1]%num_faces;
  135. std::map<Index,bool> pushed;
  136. face_queue.push(f1);
  137. pushed[f1] = true;
  138. face_queue.push(f2);
  139. pushed[f2] = true;
  140. while(!face_queue.empty())
  141. {
  142. const Index f = face_queue.front();
  143. face_queue.pop();
  144. pushed[f] = true;
  145. // consider each edge of this face
  146. for(int c = 0;c<3;c++)
  147. {
  148. // Unique edge id
  149. const Index uec = EMAP(c*num_faces+f);
  150. const Index s = uE(uec,0);
  151. const Index d = uE(uec,1);
  152. const bool ona = s == a || d == a;
  153. const bool onb = s == b || d == b;
  154. // Is this the edge we're looking for?
  155. if(ona && onb)
  156. {
  157. return true;
  158. }
  159. // not incident on either?
  160. if(!ona && !onb)
  161. {
  162. continue;
  163. }
  164. // loop over all incident half-edges
  165. for(const auto & he : uE2E[uec])
  166. {
  167. // face of this he
  168. const Index fhe = he%num_faces;
  169. if(!pushed[fhe])
  170. {
  171. pushed[fhe] = true;
  172. face_queue.push(fhe);
  173. }
  174. }
  175. }
  176. }
  177. return false;
  178. };
  179. bool flippable = !edge_exists_near(v3,v4,uei);
  180. if(flippable)
  181. {
  182. all_delaunay = false;
  183. assert( std::abs(l(f1,c1)-l(f2,c2)) < igl::EPS<Scalar>() );
  184. const Scalar e = l(f1,c1);
  185. const Scalar a = l(f1,(c1+1)%3);
  186. const Scalar b = l(f1,(c1+2)%3);
  187. const Scalar c = l(f2,(c2+1)%3);
  188. const Scalar d = l(f2,(c2+2)%3);
  189. // tan(α/2)
  190. const Scalar tan_a_2= tan_half_angle(a,b,e);
  191. // tan(δ/2)
  192. const Scalar tan_d_2 = tan_half_angle(d,e,c);
  193. // tan((α+δ)/2)
  194. const Scalar tan_a_d_2 = (tan_a_2 + tan_d_2)/(1.0-tan_a_2*tan_d_2);
  195. // cos(α+δ)
  196. const Scalar cos_a_d =
  197. (1.0 - tan_a_d_2*tan_a_d_2)/(1.0+tan_a_d_2*tan_a_d_2);
  198. const Scalar f = sqrt(b*b + c*c - 2.0*b*c*cos_a_d);
  199. l(f1,0) = f;
  200. l(f1,1) = b;
  201. l(f1,2) = c;
  202. l(f2,0) = f;
  203. l(f2,1) = d;
  204. l(f2,2) = a;
  205. flip_edge(F, E, uE, EMAP, uE2E, uei);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. #ifdef IGL_STATIC_LIBRARY
  213. // Explicit template instantiation
  214. // generated by autoexplicit.sh
  215. template void igl::intrinsic_delaunay_triangulation<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> >(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<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  216. #endif