DcelMesh.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // -----------------------------------------------------------------------
  2. // <copyright file="DcelMesh.cs">
  3. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace UnityEngine.U2D.Animation.TriangleNet
  7. .Topology.DCEL
  8. {
  9. using System.Collections.Generic;
  10. using Animation.TriangleNet.Geometry;
  11. internal class DcelMesh
  12. {
  13. protected List<Vertex> vertices;
  14. protected List<HalfEdge> edges;
  15. protected List<Face> faces;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="DcelMesh" /> class.
  18. /// </summary>
  19. public DcelMesh()
  20. : this(true)
  21. {
  22. }
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="" /> class.
  25. /// </summary>
  26. /// <param name="initialize">If false, lists will not be initialized.</param>
  27. protected DcelMesh(bool initialize)
  28. {
  29. if (initialize)
  30. {
  31. vertices = new List<Vertex>();
  32. edges = new List<HalfEdge>();
  33. faces = new List<Face>();
  34. }
  35. }
  36. /// <summary>
  37. /// Gets the vertices of the Voronoi diagram.
  38. /// </summary>
  39. public List<Vertex> Vertices
  40. {
  41. get { return vertices; }
  42. }
  43. /// <summary>
  44. /// Gets the list of half-edges specify the Voronoi diagram topology.
  45. /// </summary>
  46. public List<HalfEdge> HalfEdges
  47. {
  48. get { return edges; }
  49. }
  50. /// <summary>
  51. /// Gets the faces of the Voronoi diagram.
  52. /// </summary>
  53. public List<Face> Faces
  54. {
  55. get { return faces; }
  56. }
  57. /// <summary>
  58. /// Gets the collection of edges of the Voronoi diagram.
  59. /// </summary>
  60. public IEnumerable<IEdge> Edges
  61. {
  62. get { return EnumerateEdges(); }
  63. }
  64. /// <summary>
  65. /// Check if the DCEL is consistend.
  66. /// </summary>
  67. /// <param name="closed">If true, faces are assumed to be closed (i.e. all edges must have
  68. /// a valid next pointer).</param>
  69. /// <param name="depth">Maximum edge count of faces (default = 0 means skip check).</param>
  70. /// <returns></returns>
  71. public virtual bool IsConsistent(bool closed = true, int depth = 0)
  72. {
  73. // Check vertices for null pointers.
  74. foreach (var vertex in vertices)
  75. {
  76. if (vertex.id < 0)
  77. {
  78. continue;
  79. }
  80. if (vertex.leaving == null)
  81. {
  82. return false;
  83. }
  84. if (vertex.Leaving.Origin.id != vertex.id)
  85. {
  86. return false;
  87. }
  88. }
  89. // Check faces for null pointers.
  90. foreach (var face in faces)
  91. {
  92. if (face.ID < 0)
  93. {
  94. continue;
  95. }
  96. if (face.edge == null)
  97. {
  98. return false;
  99. }
  100. if (face.id != face.edge.face.id)
  101. {
  102. return false;
  103. }
  104. }
  105. // Check half-edges for null pointers.
  106. foreach (var edge in edges)
  107. {
  108. if (edge.id < 0)
  109. {
  110. continue;
  111. }
  112. if (edge.twin == null)
  113. {
  114. return false;
  115. }
  116. if (edge.origin == null)
  117. {
  118. return false;
  119. }
  120. if (edge.face == null)
  121. {
  122. return false;
  123. }
  124. if (closed && edge.next == null)
  125. {
  126. return false;
  127. }
  128. }
  129. // Check half-edges (topology).
  130. foreach (var edge in edges)
  131. {
  132. if (edge.id < 0)
  133. {
  134. continue;
  135. }
  136. var twin = edge.twin;
  137. var next = edge.next;
  138. if (edge.id != twin.twin.id)
  139. {
  140. return false;
  141. }
  142. if (closed)
  143. {
  144. if (next.origin.id != twin.origin.id)
  145. {
  146. return false;
  147. }
  148. if (next.twin.next.origin.id != edge.twin.origin.id)
  149. {
  150. return false;
  151. }
  152. }
  153. }
  154. if (closed && depth > 0)
  155. {
  156. // Check if faces are closed.
  157. foreach (var face in faces)
  158. {
  159. if (face.id < 0)
  160. {
  161. continue;
  162. }
  163. var edge = face.edge;
  164. var next = edge.next;
  165. int id = edge.id;
  166. int k = 0;
  167. while (next.id != id && k < depth)
  168. {
  169. next = next.next;
  170. k++;
  171. }
  172. if (next.id != id)
  173. {
  174. return false;
  175. }
  176. }
  177. }
  178. return true;
  179. }
  180. /// <summary>
  181. /// Search for half-edge without twin and add a twin. Connect twins to form connected
  182. /// boundary contours.
  183. /// </summary>
  184. /// <remarks>
  185. /// This method assumes that all faces are closed (i.e. no edge.next pointers are null).
  186. /// </remarks>
  187. public void ResolveBoundaryEdges()
  188. {
  189. // Maps vertices to leaving boundary edge.
  190. var map = new Dictionary<int, HalfEdge>();
  191. // TODO: parallel?
  192. foreach (var edge in this.edges)
  193. {
  194. if (edge.twin == null)
  195. {
  196. var twin = edge.twin = new HalfEdge(edge.next.origin, Face.Empty);
  197. twin.twin = edge;
  198. map.Add(twin.origin.id, twin);
  199. }
  200. }
  201. int j = edges.Count;
  202. foreach (var edge in map.Values)
  203. {
  204. edge.id = j++;
  205. edge.next = map[edge.twin.origin.id];
  206. this.edges.Add(edge);
  207. }
  208. }
  209. /// <summary>
  210. /// Enumerates all edges of the DCEL.
  211. /// </summary>
  212. /// <remarks>
  213. /// This method assumes that each half-edge has a twin (i.e. NOT null).
  214. /// </remarks>
  215. protected virtual IEnumerable<IEdge> EnumerateEdges()
  216. {
  217. var edges = new List<IEdge>(this.edges.Count / 2);
  218. foreach (var edge in this.edges)
  219. {
  220. var twin = edge.twin;
  221. // Report edge only once.
  222. if (edge.id < twin.id)
  223. {
  224. edges.Add(new Edge(edge.origin.id, twin.origin.id));
  225. }
  226. }
  227. return edges;
  228. }
  229. }
  230. }