BoundedVoronoi.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // -----------------------------------------------------------------------
  2. // <copyright file="BoundedVoronoi.cs">
  3. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace UnityEngine.U2D.Animation.TriangleNet
  7. .Voronoi
  8. {
  9. using System.Collections.Generic;
  10. using Animation.TriangleNet.Geometry;
  11. using Animation.TriangleNet.Tools;
  12. using Animation.TriangleNet.Topology.DCEL;
  13. using HVertex = Animation.TriangleNet.Topology.DCEL.Vertex;
  14. using TVertex = Animation.TriangleNet.Geometry.Vertex;
  15. internal class BoundedVoronoi : VoronoiBase
  16. {
  17. int offset;
  18. public BoundedVoronoi(Mesh mesh)
  19. : this(mesh, new DefaultVoronoiFactory(), RobustPredicates.Default)
  20. {
  21. }
  22. public BoundedVoronoi(Mesh mesh, IVoronoiFactory factory, IPredicates predicates)
  23. : base(mesh, factory, predicates, true)
  24. {
  25. // We explicitly told the base constructor to call the Generate method, so
  26. // at this point the basic Voronoi diagram is already created.
  27. offset = base.vertices.Count;
  28. // Each vertex of the hull will be part of a Voronoi cell.
  29. base.vertices.Capacity = offset + mesh.hullsize;
  30. // Create bounded Voronoi diagram.
  31. PostProcess();
  32. ResolveBoundaryEdges();
  33. }
  34. /// <summary>
  35. /// Computes edge intersections with mesh boundary edges.
  36. /// </summary>
  37. private void PostProcess()
  38. {
  39. foreach (var edge in rays)
  40. {
  41. var twin = edge.twin;
  42. var v1 = (TVertex)edge.face.generator;
  43. var v2 = (TVertex)twin.face.generator;
  44. double dir = predicates.CounterClockwise(v1, v2, edge.origin);
  45. if (dir <= 0)
  46. {
  47. HandleCase1(edge, v1, v2);
  48. }
  49. else
  50. {
  51. HandleCase2(edge, v1, v2);
  52. }
  53. }
  54. }
  55. /// <summary>
  56. /// Case 1: edge origin lies inside the domain.
  57. /// </summary>
  58. private void HandleCase1(HalfEdge edge, TVertex v1, TVertex v2)
  59. {
  60. //int mark = GetBoundaryMark(v1);
  61. // The infinite vertex.
  62. var v = (Point)edge.twin.origin;
  63. // The half-edge is the bisector of v1 and v2, so the projection onto the
  64. // boundary segment is actually its midpoint.
  65. v.x = (v1.x + v2.x) / 2.0;
  66. v.y = (v1.y + v2.y) / 2.0;
  67. // Close the cell connected to edge.
  68. var gen = factory.CreateVertex(v1.x, v1.y);
  69. var h1 = factory.CreateHalfEdge(edge.twin.origin, edge.face);
  70. var h2 = factory.CreateHalfEdge(gen, edge.face);
  71. edge.next = h1;
  72. h1.next = h2;
  73. h2.next = edge.face.edge;
  74. gen.leaving = h2;
  75. // Let the face edge point to the edge leaving at generator.
  76. edge.face.edge = h2;
  77. base.edges.Add(h1);
  78. base.edges.Add(h2);
  79. int count = base.edges.Count;
  80. h1.id = count;
  81. h2.id = count + 1;
  82. gen.id = offset++;
  83. base.vertices.Add(gen);
  84. }
  85. /// <summary>
  86. /// Case 2: edge origin lies outside the domain.
  87. /// </summary>
  88. private void HandleCase2(HalfEdge edge, TVertex v1, TVertex v2)
  89. {
  90. // The vertices of the infinite edge.
  91. var p1 = (Point)edge.origin;
  92. var p2 = (Point)edge.twin.origin;
  93. // The two edges leaving p1, pointing into the mesh.
  94. var e1 = edge.twin.next;
  95. var e2 = e1.twin.next;
  96. // Find the two intersections with boundary edge.
  97. IntersectionHelper.IntersectSegments(v1, v2, e1.origin, e1.twin.origin, ref p2);
  98. IntersectionHelper.IntersectSegments(v1, v2, e2.origin, e2.twin.origin, ref p1);
  99. // The infinite edge will now lie on the boundary. Update pointers:
  100. e1.twin.next = edge.twin;
  101. edge.twin.next = e2;
  102. edge.twin.face = e2.face;
  103. e1.origin = edge.twin.origin;
  104. edge.twin.twin = null;
  105. edge.twin = null;
  106. // Close the cell.
  107. var gen = factory.CreateVertex(v1.x, v1.y);
  108. var he = factory.CreateHalfEdge(gen, edge.face);
  109. edge.next = he;
  110. he.next = edge.face.edge;
  111. // Let the face edge point to the edge leaving at generator.
  112. edge.face.edge = he;
  113. base.edges.Add(he);
  114. he.id = base.edges.Count;
  115. gen.id = offset++;
  116. base.vertices.Add(gen);
  117. }
  118. /*
  119. private int GetBoundaryMark(Vertex v)
  120. {
  121. Otri tri = default(Otri);
  122. Otri next = default(Otri);
  123. Osub seg = default(Osub);
  124. // Get triangle connected to generator.
  125. v.tri.Copy(ref tri);
  126. v.tri.Copy(ref next);
  127. // Find boundary triangle.
  128. while (next.triangle.id != -1)
  129. {
  130. next.Copy(ref tri);
  131. next.OnextSelf();
  132. }
  133. // Find edge dual to current half-edge.
  134. tri.LnextSelf();
  135. tri.LnextSelf();
  136. tri.SegPivot(ref seg);
  137. return seg.seg.boundary;
  138. }
  139. //*/
  140. }
  141. }