SkeletonBounds.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System;
  30. namespace Spine {
  31. /// <summary>
  32. /// Collects each BoundingBoxAttachment that is visible and computes the world vertices for its polygon.
  33. /// The polygon vertices are provided along with convenience methods for doing hit detection.
  34. /// </summary>
  35. public class SkeletonBounds {
  36. private ExposedList<Polygon> polygonPool = new ExposedList<Polygon>();
  37. private float minX, minY, maxX, maxY;
  38. public ExposedList<BoundingBoxAttachment> BoundingBoxes { get; private set; }
  39. public ExposedList<Polygon> Polygons { get; private set; }
  40. public float MinX { get { return minX; } set { minX = value; } }
  41. public float MinY { get { return minY; } set { minY = value; } }
  42. public float MaxX { get { return maxX; } set { maxX = value; } }
  43. public float MaxY { get { return maxY; } set { maxY = value; } }
  44. public float Width { get { return maxX - minX; } }
  45. public float Height { get { return maxY - minY; } }
  46. public SkeletonBounds () {
  47. BoundingBoxes = new ExposedList<BoundingBoxAttachment>();
  48. Polygons = new ExposedList<Polygon>();
  49. }
  50. /// <summary>
  51. /// Clears any previous polygons, finds all visible bounding box attachments,
  52. /// and computes the world vertices for each bounding box's polygon.</summary>
  53. /// <param name="skeleton">The skeleton.</param>
  54. /// <param name="updateAabb">
  55. /// If true, the axis aligned bounding box containing all the polygons is computed.
  56. /// If false, the SkeletonBounds AABB methods will always return true.
  57. /// </param>
  58. public void Update (Skeleton skeleton, bool updateAabb) {
  59. ExposedList<BoundingBoxAttachment> boundingBoxes = BoundingBoxes;
  60. ExposedList<Polygon> polygons = Polygons;
  61. ExposedList<Slot> slots = skeleton.slots;
  62. int slotCount = slots.Count;
  63. boundingBoxes.Clear();
  64. for (int i = 0, n = polygons.Count; i < n; i++)
  65. polygonPool.Add(polygons.Items[i]);
  66. polygons.Clear();
  67. for (int i = 0; i < slotCount; i++) {
  68. Slot slot = slots.Items[i];
  69. if (!slot.bone.active) continue;
  70. BoundingBoxAttachment boundingBox = slot.attachment as BoundingBoxAttachment;
  71. if (boundingBox == null) continue;
  72. boundingBoxes.Add(boundingBox);
  73. Polygon polygon = null;
  74. int poolCount = polygonPool.Count;
  75. if (poolCount > 0) {
  76. polygon = polygonPool.Items[poolCount - 1];
  77. polygonPool.RemoveAt(poolCount - 1);
  78. } else
  79. polygon = new Polygon();
  80. polygons.Add(polygon);
  81. int count = boundingBox.worldVerticesLength;
  82. polygon.Count = count;
  83. if (polygon.Vertices.Length < count) polygon.Vertices = new float[count];
  84. boundingBox.ComputeWorldVertices(slot, polygon.Vertices);
  85. }
  86. if (updateAabb) {
  87. AabbCompute();
  88. } else {
  89. minX = int.MinValue;
  90. minY = int.MinValue;
  91. maxX = int.MaxValue;
  92. maxY = int.MaxValue;
  93. }
  94. }
  95. private void AabbCompute () {
  96. float minX = int.MaxValue, minY = int.MaxValue, maxX = int.MinValue, maxY = int.MinValue;
  97. ExposedList<Polygon> polygons = Polygons;
  98. for (int i = 0, n = polygons.Count; i < n; i++) {
  99. Polygon polygon = polygons.Items[i];
  100. float[] vertices = polygon.Vertices;
  101. for (int ii = 0, nn = polygon.Count; ii < nn; ii += 2) {
  102. float x = vertices[ii];
  103. float y = vertices[ii + 1];
  104. minX = Math.Min(minX, x);
  105. minY = Math.Min(minY, y);
  106. maxX = Math.Max(maxX, x);
  107. maxY = Math.Max(maxY, y);
  108. }
  109. }
  110. this.minX = minX;
  111. this.minY = minY;
  112. this.maxX = maxX;
  113. this.maxY = maxY;
  114. }
  115. /// <summary>Returns true if the axis aligned bounding box contains the point.</summary>
  116. public bool AabbContainsPoint (float x, float y) {
  117. return x >= minX && x <= maxX && y >= minY && y <= maxY;
  118. }
  119. /// <summary>Returns true if the axis aligned bounding box intersects the line segment.</summary>
  120. public bool AabbIntersectsSegment (float x1, float y1, float x2, float y2) {
  121. float minX = this.minX;
  122. float minY = this.minY;
  123. float maxX = this.maxX;
  124. float maxY = this.maxY;
  125. if ((x1 <= minX && x2 <= minX) || (y1 <= minY && y2 <= minY) || (x1 >= maxX && x2 >= maxX) || (y1 >= maxY && y2 >= maxY))
  126. return false;
  127. float m = (y2 - y1) / (x2 - x1);
  128. float y = m * (minX - x1) + y1;
  129. if (y > minY && y < maxY) return true;
  130. y = m * (maxX - x1) + y1;
  131. if (y > minY && y < maxY) return true;
  132. float x = (minY - y1) / m + x1;
  133. if (x > minX && x < maxX) return true;
  134. x = (maxY - y1) / m + x1;
  135. if (x > minX && x < maxX) return true;
  136. return false;
  137. }
  138. /// <summary>Returns true if the axis aligned bounding box intersects the axis aligned bounding box of the specified bounds.</summary>
  139. public bool AabbIntersectsSkeleton (SkeletonBounds bounds) {
  140. return minX < bounds.maxX && maxX > bounds.minX && minY < bounds.maxY && maxY > bounds.minY;
  141. }
  142. /// <summary>Returns true if the polygon contains the point.</summary>
  143. public bool ContainsPoint (Polygon polygon, float x, float y) {
  144. float[] vertices = polygon.Vertices;
  145. int nn = polygon.Count;
  146. int prevIndex = nn - 2;
  147. bool inside = false;
  148. for (int ii = 0; ii < nn; ii += 2) {
  149. float vertexY = vertices[ii + 1];
  150. float prevY = vertices[prevIndex + 1];
  151. if ((vertexY < y && prevY >= y) || (prevY < y && vertexY >= y)) {
  152. float vertexX = vertices[ii];
  153. if (vertexX + (y - vertexY) / (prevY - vertexY) * (vertices[prevIndex] - vertexX) < x) inside = !inside;
  154. }
  155. prevIndex = ii;
  156. }
  157. return inside;
  158. }
  159. /// <summary>Returns the first bounding box attachment that contains the point, or null. When doing many checks, it is usually more
  160. /// efficient to only call this method if {@link #aabbContainsPoint(float, float)} returns true.</summary>
  161. public BoundingBoxAttachment ContainsPoint (float x, float y) {
  162. ExposedList<Polygon> polygons = Polygons;
  163. for (int i = 0, n = polygons.Count; i < n; i++)
  164. if (ContainsPoint(polygons.Items[i], x, y)) return BoundingBoxes.Items[i];
  165. return null;
  166. }
  167. /// <summary>Returns the first bounding box attachment that contains the line segment, or null. When doing many checks, it is usually
  168. /// more efficient to only call this method if {@link #aabbIntersectsSegment(float, float, float, float)} returns true.</summary>
  169. public BoundingBoxAttachment IntersectsSegment (float x1, float y1, float x2, float y2) {
  170. ExposedList<Polygon> polygons = Polygons;
  171. for (int i = 0, n = polygons.Count; i < n; i++)
  172. if (IntersectsSegment(polygons.Items[i], x1, y1, x2, y2)) return BoundingBoxes.Items[i];
  173. return null;
  174. }
  175. /// <summary>Returns true if the polygon contains the line segment.</summary>
  176. public bool IntersectsSegment (Polygon polygon, float x1, float y1, float x2, float y2) {
  177. float[] vertices = polygon.Vertices;
  178. int nn = polygon.Count;
  179. float width12 = x1 - x2, height12 = y1 - y2;
  180. float det1 = x1 * y2 - y1 * x2;
  181. float x3 = vertices[nn - 2], y3 = vertices[nn - 1];
  182. for (int ii = 0; ii < nn; ii += 2) {
  183. float x4 = vertices[ii], y4 = vertices[ii + 1];
  184. float det2 = x3 * y4 - y3 * x4;
  185. float width34 = x3 - x4, height34 = y3 - y4;
  186. float det3 = width12 * height34 - height12 * width34;
  187. float x = (det1 * width34 - width12 * det2) / det3;
  188. if (((x >= x3 && x <= x4) || (x >= x4 && x <= x3)) && ((x >= x1 && x <= x2) || (x >= x2 && x <= x1))) {
  189. float y = (det1 * height34 - height12 * det2) / det3;
  190. if (((y >= y3 && y <= y4) || (y >= y4 && y <= y3)) && ((y >= y1 && y <= y2) || (y >= y2 && y <= y1))) return true;
  191. }
  192. x3 = x4;
  193. y3 = y4;
  194. }
  195. return false;
  196. }
  197. public Polygon GetPolygon (BoundingBoxAttachment attachment) {
  198. int index = BoundingBoxes.IndexOf(attachment);
  199. return index == -1 ? null : Polygons.Items[index];
  200. }
  201. }
  202. public class Polygon {
  203. public float[] Vertices { get; set; }
  204. public int Count { get; set; }
  205. public Polygon () {
  206. Vertices = new float[16];
  207. }
  208. }
  209. }