SkeletonBounds.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes Software License v2.5
  3. --
  4. -- Copyright (c) 2013-2016, Esoteric Software
  5. -- All rights reserved.
  6. --
  7. -- You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. -- non-transferable license to use, install, execute, and perform the Spine
  9. -- Runtimes software and derivative works solely for personal or internal
  10. -- use. Without the written permission of Esoteric Software (see Section 2 of
  11. -- the Spine Software License Agreement), you may not (a) modify, translate,
  12. -- adapt, or develop new applications using the Spine Runtimes or otherwise
  13. -- create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. -- delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. -- or other intellectual property or proprietary rights notices on or in the
  16. -- Software, including any copy thereof. Redistributions in binary or source
  17. -- form must include this license and terms.
  18. --
  19. -- THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. -- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. -- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. -- EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. -- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. -- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. -- USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. -- IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. -- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. -- POSSIBILITY OF SUCH DAMAGE.
  29. -------------------------------------------------------------------------------
  30. local AttachmentType = require "spine-lua.attachments.AttachmentType"
  31. local utils = require "spine-lua.utils"
  32. local setmetatable = setmetatable
  33. local math_min = math.min
  34. local math_max = math.max
  35. local ipairs = ipairs
  36. local table_insert = table.insert
  37. local SkeletonBounds = {}
  38. SkeletonBounds.__index = SkeletonBounds
  39. function SkeletonBounds.new ()
  40. local self = {
  41. minX = 0, minY = 0, maxX = 0, maxY = 0,
  42. polygons = {},
  43. boundingBoxes = {},
  44. }
  45. setmetatable(self, SkeletonBounds)
  46. return self
  47. end
  48. function SkeletonBounds:update (skeleton, updateAabb)
  49. if skeleton == nil then error("skeleton cannot be null", 2) end
  50. local boundingBoxes = {}
  51. self.boundingBoxes = boundingBoxes
  52. local polygons = {}
  53. self.polygons = polygons
  54. local slots = skeleton.slots
  55. for i,slot in ipairs(skeleton.slots) do
  56. local attachment = slot.attachment
  57. if attachment and attachment.type == AttachmentType.boundingbox then
  58. local boundingBox = attachment
  59. table_insert(boundingBoxes, boundingBox)
  60. local polygon = {}
  61. table_insert(polygons, polygon)
  62. boundingBox:computeWorldVertices(slot, 0, boundingBox.worldVerticesLength, polygon, 0, 2)
  63. end
  64. end
  65. if updateAabb then
  66. self:aabbCompute()
  67. else
  68. self.minX = 9999999
  69. self.minY = 9999999
  70. self.maxX = -9999999
  71. self.maxY = -9999999
  72. end
  73. end
  74. function SkeletonBounds:aabbCompute ()
  75. local minX, minY, maxX, maxY = 9999999, 9999999, -9999999, -9999999
  76. local polygons = self.polygons
  77. for i,vertices in ipairs(polygons) do
  78. local count = #vertices
  79. for ii = 1, count, 2 do
  80. local x = vertices[ii]
  81. local y = vertices[ii + 1]
  82. minX = math_min(minX, x)
  83. minY = math_min(minY, y)
  84. maxX = math_max(maxX, x)
  85. maxY = math_max(maxY, y)
  86. end
  87. end
  88. self.minX = minX
  89. self.minY = minY
  90. self.maxX = maxX
  91. self.maxY = maxY
  92. end
  93. function SkeletonBounds:aabbContainsPoint (x, y)
  94. return x >= self.minX and x <= self.maxX and y >= self.minY and y <= self.maxY
  95. end
  96. function SkeletonBounds:aabbIntersectsSegment (x1, y1, x2, y2)
  97. local minX, minY, maxX, maxY = self.minX, self.minY, self.maxX, self.maxY
  98. if (x1 <= minX and x2 <= minX) or (y1 <= minY and y2 <= minY) or (x1 >= maxX and x2 >= maxX) or (y1 >= maxY and y2 >= maxY) then
  99. return false
  100. end
  101. local m = (y2 - y1) / (x2 - x1)
  102. local y = m * (minX - x1) + y1
  103. if y > minY and y < maxY then return true end
  104. y = m * (maxX - x1) + y1
  105. if y > minY and y < maxY then return true end
  106. local x = (minY - y1) / m + x1
  107. if x > minX and x < maxX then return true end
  108. x = (maxY - y1) / m + x1
  109. if x > minX and x < maxX then return true end
  110. return false
  111. end
  112. function SkeletonBounds:aabbIntersectsSkeleton (bounds)
  113. return self.minX < bounds.maxX and self.maxX > bounds.minX and self.minY < bounds.maxY and self.maxY > bounds.minY
  114. end
  115. function SkeletonBounds:containsPoint (x, y)
  116. for i,polygon in ipairs(self.polygons) do
  117. if self:polygonContainsPoint(polygon, x, y) then return self.boundingBoxes[i] end
  118. end
  119. return nil
  120. end
  121. function SkeletonBounds:intersectsSegment (x1, y1, x2, y2)
  122. for i,polygon in ipairs(self.polygons) do
  123. if self:polygonIntersectsSegment(polygon, x1, y1, x2, y2) then return self.boundingBoxes[i] end
  124. end
  125. return nil
  126. end
  127. function SkeletonBounds:polygonContainsPoint (polygon, x, y)
  128. local nn = #polygon
  129. local prevIndex = nn - 1
  130. local inside = false
  131. for ii = 1, nn, 2 do
  132. local vertexY = polygon[ii + 1]
  133. local prevY = polygon[prevIndex + 1]
  134. if (vertexY < y and prevY >= y) or (prevY < y and vertexY >= y) then
  135. local vertexX = polygon[ii]
  136. if vertexX + (y - vertexY) / (prevY - vertexY) * (polygon[prevIndex] - vertexX) < x then inside = not inside end
  137. end
  138. prevIndex = ii
  139. end
  140. return inside
  141. end
  142. function SkeletonBounds:polygonIntersectsSegment (polygon, x1, y1, x2, y2)
  143. local nn = #polygon
  144. local width12, height12 = x1 - x2, y1 - y2
  145. local det1 = x1 * y2 - y1 * x2
  146. local x3, y3 = polygon[nn - 2], polygon[nn - 1]
  147. for ii = 1, nn, 2 do
  148. local x4, y4 = polygon[ii], polygon[ii + 1]
  149. local det2 = x3 * y4 - y3 * x4
  150. local width34, height34 = x3 - x4, y3 - y4
  151. local det3 = width12 * height34 - height12 * width34
  152. local x = (det1 * width34 - width12 * det2) / det3
  153. if ((x >= x3 and x <= x4) or (x >= x4 and x <= x3)) and ((x >= x1 and x <= x2) or (x >= x2 and x <= x1)) then
  154. local y = (det1 * height34 - height12 * det2) / det3
  155. if ((y >= y3 and y <= y4) or (y >= y4 and y <= y3)) and ((y >= y1 and y <= y2) or (y >= y2 and y <= y1)) then return true end
  156. end
  157. x3 = x4
  158. y3 = y4
  159. end
  160. return false
  161. end
  162. function SkeletonBounds:getPolygon (attachment)
  163. local index = spine.utils.indexOf(self.boundingBoxes, attachment)
  164. if index == -1 then
  165. return nil
  166. else
  167. return self.polygons[index]
  168. end
  169. end
  170. function SkeletonBounds:getWidth()
  171. return self.maxX - self.minX
  172. end
  173. function SkeletonBounds:getHeight()
  174. return self.maxY - self.minY
  175. end
  176. return SkeletonBounds