SkeletonBounds.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes Software License
  3. -- Version 2.1
  4. --
  5. -- Copyright (c) 2013, Esoteric Software
  6. -- All rights reserved.
  7. --
  8. -- You are granted a perpetual, non-exclusive, non-sublicensable and
  9. -- non-transferable license to install, execute and perform the Spine Runtimes
  10. -- Software (the "Software") solely for internal use. Without the written
  11. -- permission of Esoteric Software (typically granted by licensing Spine), you
  12. -- may not (a) modify, translate, adapt or otherwise create derivative works,
  13. -- improvements of the Software or develop new applications using the Software
  14. -- or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. -- trademark, patent or other intellectual property or proprietary rights
  16. -- notices on or in the Software, including any copy thereof. Redistributions
  17. -- in binary or source 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 SOFTARE 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; LOSS OF USE, DATA, OR PROFITS;
  25. -- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. -- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. -- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. -- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. -------------------------------------------------------------------------------
  30. local AttachmentType = require "spine-lua.AttachmentType"
  31. local utils = require "spine-lua.utils"
  32. local SkeletonBounds = {}
  33. function SkeletonBounds.new ()
  34. local self = {
  35. polygons = {},
  36. boundingBoxes = {},
  37. minX = 0, minY = 0, maxX = 0, maxY = 0
  38. }
  39. function aabbCompute ()
  40. local polygons = self.polygons
  41. local minX, minY, maxX, maxY = 9999999, 9999999, -9999999, -9999999
  42. for i,vertices in ipairs(polygons) do
  43. local count = #vertices
  44. for ii = 1, count, 2 do
  45. local x = vertices[ii]
  46. local y = vertices[ii + 1]
  47. minX = math.min(minX, x)
  48. minY = math.min(minY, y)
  49. maxX = math.max(maxX, x)
  50. maxY = math.max(maxY, y)
  51. end
  52. end
  53. self.minX = minX
  54. self.minY = minY
  55. self.maxX = maxX
  56. self.maxY = maxY
  57. end
  58. function self:update (skeleton, updateAabb)
  59. local x = skeleton.x
  60. local y = skeleton.y
  61. local polygons = {}
  62. self.polygons = polygons
  63. local boundingBoxes = {}
  64. self.boundingBoxes = boundingBoxes
  65. for i,slot in ipairs(skeleton.slots) do
  66. local boundingBox = slot.attachment
  67. if boundingBox and boundingBox.type == AttachmentType.boundingbox then
  68. table.insert(boundingBoxes, boundingBox)
  69. local polygon = {}
  70. table.insert(polygons, polygon)
  71. boundingBox:computeWorldVertices(x, y, slot.bone, polygon)
  72. end
  73. end
  74. if updateAabb then aabbCompute() end
  75. end
  76. function self:aabbContainsPoint (x, y)
  77. return x >= self.minX and x <= self.maxX and y >= self.minY and y <= self.maxY
  78. end
  79. function self:aabbIntersectsSegment (x1, y1, x2, y2)
  80. local minX, minY, maxX, maxY = self.minX, self.minY, self.maxX, self.maxY
  81. 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
  82. return false
  83. end
  84. local m = (y2 - y1) / (x2 - x1)
  85. local y = m * (minX - x1) + y1
  86. if y > minY and y < maxY then return true end
  87. y = m * (maxX - x1) + y1
  88. if y > minY and y < maxY then return true end
  89. local x = (minY - y1) / m + x1
  90. if x > minX and x < maxX then return true end
  91. x = (maxY - y1) / m + x1
  92. if x > minX and x < maxX then return true end
  93. return false
  94. end
  95. function self:aabbIntersectsSkeleton (bounds)
  96. return self.minX < bounds.maxX and self.maxX > bounds.minX and self.minY < bounds.maxY and self.maxY > bounds.minY
  97. end
  98. function self:containsPoint (x, y)
  99. for i,polygon in ipairs(self.polygons) do
  100. if self:polygonContainsPoint(polygon, x, y) then return self.boundingBoxes[i] end
  101. end
  102. return nil
  103. end
  104. function self:intersectsSegment (x1, y1, x2, y2)
  105. for i,polygon in ipairs(self.polygons) do
  106. if self:polygonIntersectsSegment(polygon, x1, y1, x2, y2) then return self.boundingBoxes[i] end
  107. end
  108. return nil
  109. end
  110. function self:polygonContainsPoint (polygon, x, y)
  111. local nn = #polygon
  112. local prevIndex = nn - 1
  113. local inside = false
  114. for ii = 1, nn, 2 do
  115. local vertexY = polygon[ii + 1]
  116. local prevY = polygon[prevIndex + 1]
  117. if (vertexY < y and prevY >= y) or (prevY < y and vertexY >= y) then
  118. local vertexX = polygon[ii]
  119. if vertexX + (y - vertexY) / (prevY - vertexY) * (polygon[prevIndex] - vertexX) < x then inside = not inside end
  120. end
  121. prevIndex = ii
  122. end
  123. return inside
  124. end
  125. function self:polygonIntersectsSegment (polygon, x1, y1, x2, y2)
  126. local nn = #polygon
  127. local width12, height12 = x1 - x2, y1 - y2
  128. local det1 = x1 * y2 - y1 * x2
  129. local x3, y3 = polygon[nn - 2], polygon[nn - 1]
  130. for ii = 1, nn, 2 do
  131. local x4, y4 = polygon[ii], polygon[ii + 1]
  132. local det2 = x3 * y4 - y3 * x4
  133. local width34, height34 = x3 - x4, y3 - y4
  134. local det3 = width12 * height34 - height12 * width34
  135. local x = (det1 * width34 - width12 * det2) / det3
  136. 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
  137. local y = (det1 * height34 - height12 * det2) / det3
  138. 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
  139. end
  140. x3 = x4
  141. y3 = y4
  142. end
  143. return false
  144. end
  145. function self:getPolygon (attachment)
  146. local index = spine.utils.indexOf(self.boundingBoxes, attachment)
  147. if index == -1 then
  148. return nil
  149. else
  150. return self.polygons[index]
  151. end
  152. end
  153. function self:getWidth ()
  154. return self.maxX - self.minX
  155. end
  156. function self:getHeight ()
  157. return self.maxY - self.minY
  158. end
  159. return self
  160. end
  161. return SkeletonBounds