SkeletonBounds.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes License Agreement
  3. -- Last updated May 1, 2019. Replaces all prior versions.
  4. --
  5. -- Copyright (c) 2013-2019, 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. -- THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY EXPRESS
  19. -- OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. -- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  21. -- NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. -- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. -- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS
  24. -- INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY
  25. -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. -- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  27. -- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. -------------------------------------------------------------------------------
  29. local AttachmentType = require "spine-lua.attachments.AttachmentType"
  30. local utils = require "spine-lua.utils"
  31. local setmetatable = setmetatable
  32. local math_min = math.min
  33. local math_max = math.max
  34. local ipairs = ipairs
  35. local table_insert = table.insert
  36. local SkeletonBounds = {}
  37. SkeletonBounds.__index = SkeletonBounds
  38. function SkeletonBounds.new ()
  39. local self = {
  40. minX = 0, minY = 0, maxX = 0, maxY = 0,
  41. polygons = {},
  42. boundingBoxes = {},
  43. }
  44. setmetatable(self, SkeletonBounds)
  45. return self
  46. end
  47. function SkeletonBounds:update (skeleton, updateAabb)
  48. if skeleton == nil then error("skeleton cannot be null", 2) end
  49. local boundingBoxes = {}
  50. self.boundingBoxes = boundingBoxes
  51. local polygons = {}
  52. self.polygons = polygons
  53. local slots = skeleton.slots
  54. for _,slot in ipairs(skeleton.slots) do
  55. local attachment = slot.attachment
  56. if attachment and attachment.type == AttachmentType.boundingbox then
  57. local boundingBox = attachment
  58. table_insert(boundingBoxes, boundingBox)
  59. local polygon = {}
  60. table_insert(polygons, polygon)
  61. boundingBox:computeWorldVertices(slot, 0, boundingBox.worldVerticesLength, polygon, 0, 2)
  62. end
  63. end
  64. if updateAabb then
  65. self:aabbCompute()
  66. else
  67. self.minX = 9999999
  68. self.minY = 9999999
  69. self.maxX = -9999999
  70. self.maxY = -9999999
  71. end
  72. end
  73. function SkeletonBounds:aabbCompute ()
  74. local minX, minY, maxX, maxY = 9999999, 9999999, -9999999, -9999999
  75. local polygons = self.polygons
  76. for _,vertices in ipairs(polygons) do
  77. local count = #vertices
  78. for ii = 1, count, 2 do
  79. local x = vertices[ii]
  80. local y = vertices[ii + 1]
  81. minX = math_min(minX, x)
  82. minY = math_min(minY, y)
  83. maxX = math_max(maxX, x)
  84. maxY = math_max(maxY, y)
  85. end
  86. end
  87. self.minX = minX
  88. self.minY = minY
  89. self.maxX = maxX
  90. self.maxY = maxY
  91. end
  92. function SkeletonBounds:aabbContainsPoint (x, y)
  93. return x >= self.minX and x <= self.maxX and y >= self.minY and y <= self.maxY
  94. end
  95. function SkeletonBounds:aabbIntersectsSegment (x1, y1, x2, y2)
  96. local minX, minY, maxX, maxY = self.minX, self.minY, self.maxX, self.maxY
  97. 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
  98. return false
  99. end
  100. local m = (y2 - y1) / (x2 - x1)
  101. local y = m * (minX - x1) + y1
  102. if y > minY and y < maxY then return true end
  103. y = m * (maxX - x1) + y1
  104. if y > minY and y < maxY then return true end
  105. local x = (minY - y1) / m + x1
  106. if x > minX and x < maxX then return true end
  107. x = (maxY - y1) / m + x1
  108. if x > minX and x < maxX then return true end
  109. return false
  110. end
  111. function SkeletonBounds:aabbIntersectsSkeleton (bounds)
  112. return self.minX < bounds.maxX and self.maxX > bounds.minX and self.minY < bounds.maxY and self.maxY > bounds.minY
  113. end
  114. function SkeletonBounds:containsPoint (x, y)
  115. for i,polygon in ipairs(self.polygons) do
  116. if self:polygonContainsPoint(polygon, x, y) then return self.boundingBoxes[i] end
  117. end
  118. return nil
  119. end
  120. function SkeletonBounds:intersectsSegment (x1, y1, x2, y2)
  121. for i,polygon in ipairs(self.polygons) do
  122. if self:polygonIntersectsSegment(polygon, x1, y1, x2, y2) then return self.boundingBoxes[i] end
  123. end
  124. return nil
  125. end
  126. function SkeletonBounds:polygonContainsPoint (polygon, x, y)
  127. local nn = #polygon
  128. local prevIndex = nn - 1
  129. local inside = false
  130. for ii = 1, nn, 2 do
  131. local vertexY = polygon[ii + 1]
  132. local prevY = polygon[prevIndex + 1]
  133. if (vertexY < y and prevY >= y) or (prevY < y and vertexY >= y) then
  134. local vertexX = polygon[ii]
  135. if vertexX + (y - vertexY) / (prevY - vertexY) * (polygon[prevIndex] - vertexX) < x then inside = not inside end
  136. end
  137. prevIndex = ii
  138. end
  139. return inside
  140. end
  141. function SkeletonBounds:polygonIntersectsSegment (polygon, x1, y1, x2, y2)
  142. local nn = #polygon
  143. local width12, height12 = x1 - x2, y1 - y2
  144. local det1 = x1 * y2 - y1 * x2
  145. local x3, y3 = polygon[nn - 2], polygon[nn - 1]
  146. for ii = 1, nn, 2 do
  147. local x4, y4 = polygon[ii], polygon[ii + 1]
  148. local det2 = x3 * y4 - y3 * x4
  149. local width34, height34 = x3 - x4, y3 - y4
  150. local det3 = width12 * height34 - height12 * width34
  151. local x = (det1 * width34 - width12 * det2) / det3
  152. 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
  153. local y = (det1 * height34 - height12 * det2) / det3
  154. 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
  155. end
  156. x3 = x4
  157. y3 = y4
  158. end
  159. return false
  160. end
  161. function SkeletonBounds:getPolygon (attachment)
  162. local index = utils.indexOf(self.boundingBoxes, attachment)
  163. if index == -1 then
  164. return nil
  165. else
  166. return self.polygons[index]
  167. end
  168. end
  169. function SkeletonBounds:getWidth()
  170. return self.maxX - self.minX
  171. end
  172. function SkeletonBounds:getHeight()
  173. return self.maxY - self.minY
  174. end
  175. return SkeletonBounds