Triangulator.lua 9.9 KB

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