Triangulator.lua 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. local utils = require "spine-lua.utils"
  30. local setmetatable = setmetatable
  31. local math_min = math.min
  32. local math_max = math.max
  33. local ipairs = ipairs
  34. local table_insert = table.insert
  35. local table_remove = table.remove
  36. local Triangulator = {}
  37. Triangulator.__index = Triangulator
  38. function Triangulator.new ()
  39. local self = {
  40. convexPolygons = {},
  41. convexPolygonsIndices = {},
  42. indicesArray = {},
  43. isConcaveArray = {},
  44. triangles = {}
  45. }
  46. setmetatable(self, Triangulator)
  47. return self
  48. end
  49. function Triangulator:triangulate (verticesArray)
  50. local vertices = verticesArray
  51. local vertexCount = #verticesArray / 2
  52. self.indicesArray = {}
  53. local indicesArray = self.indicesArray
  54. local indices = utils.setArraySize(indicesArray, vertexCount)
  55. local i = 0
  56. while i < vertexCount do
  57. indices[i] = i
  58. i = i + 1
  59. end
  60. local isConcaveArray = self.isConcaveArray
  61. local isConcave = isConcaveArray
  62. i = 0
  63. while i < vertexCount do
  64. isConcave[i] = self:isConcave(i, vertexCount, vertices, indices)
  65. i = i + 1
  66. end
  67. self.triangles = {}
  68. local triangles = self.triangles;
  69. while vertexCount > 3 do
  70. -- Find ear tip.
  71. local previous = vertexCount - 1
  72. local i = 0
  73. local _next = 1
  74. while true do
  75. local goToHead = false
  76. local breakLoop = false
  77. if not isConcave[i] then
  78. local p1 = indices[previous] * 2 + 1
  79. local p2 = indices[i] * 2 + 1
  80. local p3 = indices[_next] * 2 + 1
  81. local p1x = vertices[p1]
  82. local p1y = vertices[p1 + 1]
  83. local p2x = vertices[p2]
  84. local p2y = vertices[p2 + 1]
  85. local p3x = vertices[p3]
  86. local p3y = vertices[p3 + 1]
  87. local ii = ((_next + 1) % vertexCount)
  88. while ii ~= previous do
  89. if isConcave[ii] then
  90. local v = indices[ii] * 2 + 1
  91. local vx = vertices[v]
  92. local vy = vertices[v + 1]
  93. if self:positiveArea(p3x, p3y, p1x, p1y, vx, vy) then
  94. if self:positiveArea(p1x, p1y, p2x, p2y, vx, vy) then
  95. if self:positiveArea(p2x, p2y, p3x, p3y, vx, vy) then
  96. goToHead = true
  97. break
  98. end
  99. end
  100. end
  101. end
  102. ii = (ii + 1) % vertexCount
  103. end
  104. if (not goToHead) then
  105. breakLoop = true
  106. break
  107. end
  108. end
  109. if breakLoop then break end
  110. if _next == 0 then
  111. repeat
  112. if not isConcave[i] then
  113. break;
  114. end
  115. i = i - 1
  116. until i == 0
  117. break
  118. end
  119. previous = i
  120. i = _next
  121. _next = (_next + 1) % vertexCount
  122. end
  123. -- Cut ear tip.
  124. table_insert(triangles, indices[(vertexCount + i - 1) % vertexCount] + 1)
  125. table_insert(triangles, indices[i] + 1)
  126. table_insert(triangles, indices[(i + 1) % vertexCount] + 1)
  127. if i == 0 then
  128. local ii = 1
  129. while ii <= #indicesArray do
  130. indicesArray[ii-1] = indicesArray[ii]
  131. isConcaveArray[ii-1] = isConcaveArray[ii]
  132. ii = ii + 1
  133. end
  134. else
  135. table_remove(indicesArray, i)
  136. table_remove(isConcaveArray, i)
  137. end
  138. vertexCount = vertexCount - 1
  139. local previousIndex = (vertexCount + i - 1) % vertexCount
  140. local nextIndex = i
  141. if i == vertexCount then nextIndex = 0 end
  142. isConcave[previousIndex] = self:isConcave(previousIndex, vertexCount, vertices, indices)
  143. isConcave[nextIndex] = self:isConcave(nextIndex, vertexCount, vertices, indices)
  144. end
  145. if vertexCount == 3 then
  146. table_insert(triangles, indices[2] + 1)
  147. table_insert(triangles, indices[0] + 1)
  148. table_insert(triangles, indices[1] + 1)
  149. end
  150. return triangles
  151. end
  152. function Triangulator:decompose(verticesArray, triangles)
  153. local vertices = verticesArray
  154. self.convexPolygons = {}
  155. local convexPolygons = self.convexPolygons;
  156. self.convexPolygonsIndices = {}
  157. local convexPolygonsIndices = self.convexPolygonsIndices;
  158. local polygonIndices = {}
  159. local polygon = {}
  160. -- Merge subsequent triangles if they form a triangle fan.
  161. local fanBaseIndex = -1
  162. local lastWinding = 0
  163. local trianglesItems = triangles
  164. local i = 1
  165. local n = #triangles
  166. while i <= n do
  167. local t1 = (trianglesItems[i] - 1) * 2 + 1
  168. local t2 = (trianglesItems[i + 1] - 1) * 2 + 1
  169. local t3 = (trianglesItems[i + 2] - 1) * 2 + 1
  170. local x1 = vertices[t1]
  171. local y1 = vertices[t1 + 1]
  172. local x2 = vertices[t2]
  173. local y2 = vertices[t2 + 1]
  174. local x3 = vertices[t3]
  175. local y3 = vertices[t3 + 1]
  176. -- If the base of the last triangle is the same as this triangle, check if they form a convex polygon (triangle fan).
  177. local merged = false;
  178. if fanBaseIndex == t1 then
  179. local o = #polygon - 4 + 1;
  180. local p = polygon;
  181. local winding1 = self:winding(p[o], p[o + 1], p[o + 2], p[o + 3], x3, y3);
  182. local winding2 = self:winding(x3, y3, p[1], p[2], p[3], p[4]);
  183. if winding1 == lastWinding and winding2 == lastWinding then
  184. table_insert(polygon, x3)
  185. table_insert(polygon, y3)
  186. table_insert(polygonIndices, t3)
  187. merged = true
  188. end
  189. end
  190. -- Otherwise make this triangle the new base.
  191. if not merged then
  192. if #polygon > 0 then
  193. table_insert(convexPolygons, polygon)
  194. table_insert(convexPolygonsIndices, polygonIndices)
  195. end
  196. polygon = {}
  197. table_insert(polygon, x1)
  198. table_insert(polygon, y1)
  199. table_insert(polygon, x2)
  200. table_insert(polygon, y2)
  201. table_insert(polygon, x3)
  202. table_insert(polygon, y3)
  203. polygonIndices = {}
  204. table_insert(polygonIndices, t1)
  205. table_insert(polygonIndices, t2);
  206. table_insert(polygonIndices, t3);
  207. lastWinding = self:winding(x1, y1, x2, y2, x3, y3)
  208. fanBaseIndex = t1
  209. end
  210. i = i + 3
  211. end
  212. if #polygon > 0 then
  213. table_insert(convexPolygons, polygon)
  214. table_insert(convexPolygonsIndices, polygonIndices)
  215. end
  216. -- Go through the list of polygons and try to merge the remaining triangles with the found triangle fans.
  217. i = 1
  218. n = #convexPolygons
  219. while i <= n do
  220. polygonIndices = convexPolygonsIndices[i]
  221. if (#polygonIndices > 0) then
  222. local firstIndex = polygonIndices[1]
  223. local lastIndex = polygonIndices[#polygonIndices]
  224. polygon = convexPolygons[i]
  225. local o = #polygon - 4 + 1
  226. local p = polygon
  227. local prevPrevX = p[o]
  228. local prevPrevY = p[o + 1]
  229. local prevX = p[o + 2]
  230. local prevY = p[o + 3]
  231. local firstX = p[1]
  232. local firstY = p[2]
  233. local secondX = p[3]
  234. local secondY = p[4]
  235. local winding = self:winding(prevPrevX, prevPrevY, prevX, prevY, firstX, firstY)
  236. local ii = 1
  237. while ii <= n do
  238. if ii ~= i then
  239. local otherIndices = convexPolygonsIndices[ii]
  240. if (#otherIndices == 3) then
  241. local otherFirstIndex = otherIndices[1];
  242. local otherSecondIndex = otherIndices[2];
  243. local otherLastIndex = otherIndices[3];
  244. local otherPoly = convexPolygons[ii];
  245. local x3 = otherPoly[#otherPoly - 2 + 1]
  246. local y3 = otherPoly[#otherPoly - 1 + 1]
  247. if not (otherFirstIndex ~= firstIndex or otherSecondIndex ~= lastIndex) then
  248. local winding1 = self:winding(prevPrevX, prevPrevY, prevX, prevY, x3, y3)
  249. local winding2 = self:winding(x3, y3, firstX, firstY, secondX, secondY)
  250. if winding1 == winding and winding2 == winding then
  251. convexPolygons[ii] = {}
  252. convexPolygonsIndices[ii] = {}
  253. table_insert(polygon, x3)
  254. table_insert(polygon, y3)
  255. table_insert(polygonIndices, otherLastIndex)
  256. prevPrevX = prevX
  257. prevPrevY = prevY
  258. prevX = x3
  259. prevY = y3
  260. ii = 1
  261. end
  262. end
  263. end
  264. end
  265. ii = ii + 1
  266. end
  267. end
  268. i = i + 1
  269. end
  270. -- Remove empty polygons that resulted from the merge step above.
  271. i = #convexPolygons
  272. while i >= 1 do
  273. polygon = convexPolygons[i]
  274. if #polygon == 0 then
  275. table_remove(convexPolygons, i)
  276. end
  277. i = i - 1
  278. end
  279. return convexPolygons;
  280. end
  281. function Triangulator:isConcave(index, vertexCount, vertices, indices)
  282. local previous = indices[(vertexCount + index - 1) % vertexCount] * 2 + 1;
  283. local current = indices[index] * 2 + 1;
  284. local _next = indices[(index + 1) % vertexCount] * 2 + 1;
  285. return not self:positiveArea(vertices[previous], vertices[previous + 1], vertices[current], vertices[current + 1], vertices[_next],vertices[_next + 1]);
  286. end
  287. function Triangulator:positiveArea(p1x, p1y, p2x, p2y, p3x, p3y)
  288. return p1x * (p3y - p2y) + p2x * (p1y - p3y) + p3x * (p2y - p1y) >= 0
  289. end
  290. function Triangulator:winding(p1x, p1y, p2x, p2y, p3x, p3y)
  291. local px = p2x - p1x
  292. local py = p2y - p1y
  293. if p3x * py - p3y * px + px * p1y - p1x * py >= 0 then
  294. return 1
  295. else
  296. return -1;
  297. end
  298. end
  299. return Triangulator