VoronoiFactory.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. namespace UnityEngine.U2D.Animation.TriangleNet
  2. .Smoothing
  3. {
  4. using System;
  5. using Animation.TriangleNet.Topology.DCEL;
  6. using Animation.TriangleNet.Voronoi;
  7. /// <summary>
  8. /// Factory which re-uses objects in the smoothing loop to enhance performance.
  9. /// </summary>
  10. /// <remarks>
  11. /// See <see cref="SimpleSmoother"/>.
  12. /// </remarks>
  13. class VoronoiFactory : IVoronoiFactory
  14. {
  15. ObjectPool<Vertex> vertices;
  16. ObjectPool<HalfEdge> edges;
  17. ObjectPool<Face> faces;
  18. public VoronoiFactory()
  19. {
  20. vertices = new ObjectPool<Vertex>();
  21. edges = new ObjectPool<HalfEdge>();
  22. faces = new ObjectPool<Face>();
  23. }
  24. public void Initialize(int vertexCount, int edgeCount, int faceCount)
  25. {
  26. vertices.Capacity = vertexCount;
  27. edges.Capacity = edgeCount;
  28. faces.Capacity = faceCount;
  29. for (int i = vertices.Count; i < vertexCount; i++)
  30. {
  31. vertices.Put(new Vertex(0, 0));
  32. }
  33. for (int i = edges.Count; i < edgeCount; i++)
  34. {
  35. edges.Put(new HalfEdge(null));
  36. }
  37. for (int i = faces.Count; i < faceCount; i++)
  38. {
  39. faces.Put(new Face(null));
  40. }
  41. Reset();
  42. }
  43. public void Reset()
  44. {
  45. vertices.Release();
  46. edges.Release();
  47. faces.Release();
  48. }
  49. public Vertex CreateVertex(double x, double y)
  50. {
  51. Vertex vertex;
  52. if (vertices.TryGet(out vertex))
  53. {
  54. vertex.x = x;
  55. vertex.y = y;
  56. vertex.leaving = null;
  57. return vertex;
  58. }
  59. vertex = new Vertex(x, y);
  60. vertices.Put(vertex);
  61. return vertex;
  62. }
  63. public HalfEdge CreateHalfEdge(Vertex origin, Face face)
  64. {
  65. HalfEdge edge;
  66. if (edges.TryGet(out edge))
  67. {
  68. edge.origin = origin;
  69. edge.face = face;
  70. edge.next = null;
  71. edge.twin = null;
  72. if (face != null && face.edge == null)
  73. {
  74. face.edge = edge;
  75. }
  76. return edge;
  77. }
  78. edge = new HalfEdge(origin, face);
  79. edges.Put(edge);
  80. return edge;
  81. }
  82. public Face CreateFace(Geometry.Vertex vertex)
  83. {
  84. Face face;
  85. if (faces.TryGet(out face))
  86. {
  87. face.id = vertex.id;
  88. face.generator = vertex;
  89. face.edge = null;
  90. return face;
  91. }
  92. face = new Face(vertex);
  93. faces.Put(face);
  94. return face;
  95. }
  96. class ObjectPool<T> where T : class
  97. {
  98. int index, count;
  99. T[] pool;
  100. public int Count
  101. {
  102. get { return count; }
  103. }
  104. public int Capacity
  105. {
  106. get { return this.pool.Length; }
  107. set { Resize(value); }
  108. }
  109. public ObjectPool(int capacity = 3)
  110. {
  111. this.index = 0;
  112. this.count = 0;
  113. this.pool = new T[capacity];
  114. }
  115. public ObjectPool(T[] pool)
  116. {
  117. this.index = 0;
  118. this.count = 0;
  119. this.pool = pool;
  120. }
  121. public bool TryGet(out T obj)
  122. {
  123. if (this.index < this.count)
  124. {
  125. obj = this.pool[this.index++];
  126. return true;
  127. }
  128. obj = null;
  129. return false;
  130. }
  131. public void Put(T obj)
  132. {
  133. var capacity = this.pool.Length;
  134. if (capacity <= this.count)
  135. {
  136. Resize(2 * capacity);
  137. }
  138. this.pool[this.count++] = obj;
  139. this.index++;
  140. }
  141. public void Release()
  142. {
  143. this.index = 0;
  144. }
  145. private void Resize(int size)
  146. {
  147. if (size > this.count)
  148. {
  149. Array.Resize(ref this.pool, size);
  150. }
  151. }
  152. }
  153. }
  154. }