Triangulator.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #ifdef SPINE_UE4
  30. #include "SpinePluginPrivatePCH.h"
  31. #endif
  32. #include <spine/Triangulator.h>
  33. #include <spine/MathUtil.h>
  34. using namespace spine;
  35. Triangulator::~Triangulator() {
  36. ContainerUtil::cleanUpVectorOfPointers(_convexPolygons);
  37. ContainerUtil::cleanUpVectorOfPointers(_convexPolygonsIndices);
  38. }
  39. Vector<int> &Triangulator::triangulate(Vector<float> &vertices) {
  40. size_t vertexCount = vertices.size() >> 1;
  41. Vector<int> &indices = _indices;
  42. indices.clear();
  43. indices.ensureCapacity(vertexCount);
  44. indices.setSize(vertexCount, 0);
  45. for (size_t i = 0; i < vertexCount; ++i) {
  46. indices[i] = i;
  47. }
  48. Vector<bool> &isConcaveArray = _isConcaveArray;
  49. isConcaveArray.ensureCapacity(vertexCount);
  50. isConcaveArray.setSize(vertexCount, 0);
  51. for (size_t i = 0, n = vertexCount; i < n; ++i) {
  52. isConcaveArray[i] = isConcave(i, vertexCount, vertices, indices);
  53. }
  54. Vector<int> &triangles = _triangles;
  55. triangles.clear();
  56. triangles.ensureCapacity(MathUtil::max((int)0, (int)vertexCount - 2) << 2);
  57. while (vertexCount > 3) {
  58. // Find ear tip.
  59. size_t previous = vertexCount - 1, i = 0, next = 1;
  60. // outer:
  61. while (true) {
  62. if (!isConcaveArray[i]) {
  63. int p1 = indices[previous] << 1, p2 = indices[i] << 1, p3 = indices[next] << 1;
  64. float p1x = vertices[p1], p1y = vertices[p1 + 1];
  65. float p2x = vertices[p2], p2y = vertices[p2 + 1];
  66. float p3x = vertices[p3], p3y = vertices[p3 + 1];
  67. for (size_t ii = (next + 1) % vertexCount; ii != previous; ii = (ii + 1) % vertexCount) {
  68. if (!isConcaveArray[ii]) continue;
  69. int v = indices[ii] << 1;
  70. float &vx = vertices[v], vy = vertices[v + 1];
  71. if (positiveArea(p3x, p3y, p1x, p1y, vx, vy)) {
  72. if (positiveArea(p1x, p1y, p2x, p2y, vx, vy)) {
  73. if (positiveArea(p2x, p2y, p3x, p3y, vx, vy)) {
  74. goto break_outer; // break outer;
  75. }
  76. }
  77. }
  78. }
  79. break;
  80. }
  81. break_outer:
  82. if (next == 0) {
  83. do {
  84. if (!isConcaveArray[i]) break;
  85. i--;
  86. } while (i > 0);
  87. break;
  88. }
  89. previous = i;
  90. i = next;
  91. next = (next + 1) % vertexCount;
  92. }
  93. // Cut ear tip.
  94. triangles.add(indices[(vertexCount + i - 1) % vertexCount]);
  95. triangles.add(indices[i]);
  96. triangles.add(indices[(i + 1) % vertexCount]);
  97. indices.removeAt(i);
  98. isConcaveArray.removeAt(i);
  99. vertexCount--;
  100. int previousIndex = (vertexCount + i - 1) % vertexCount;
  101. int nextIndex = i == vertexCount ? 0 : i;
  102. isConcaveArray[previousIndex] = isConcave(previousIndex, vertexCount, vertices, indices);
  103. isConcaveArray[nextIndex] = isConcave(nextIndex, vertexCount, vertices, indices);
  104. }
  105. if (vertexCount == 3) {
  106. triangles.add(indices[2]);
  107. triangles.add(indices[0]);
  108. triangles.add(indices[1]);
  109. }
  110. return triangles;
  111. }
  112. Vector<Vector<float> *> &Triangulator::decompose(Vector<float> &vertices, Vector<int> &triangles) {
  113. Vector<Vector<float> *> &convexPolygons = _convexPolygons;
  114. for (size_t i = 0, n = convexPolygons.size(); i < n; ++i)
  115. _polygonPool.free(convexPolygons[i]);
  116. convexPolygons.clear();
  117. Vector<Vector<int> *> &convexPolygonsIndices = _convexPolygonsIndices;
  118. for (size_t i = 0, n = convexPolygonsIndices.size(); i < n; ++i)
  119. _polygonIndicesPool.free(convexPolygonsIndices[i]);
  120. convexPolygonsIndices.clear();
  121. Vector<int> *polygonIndices = _polygonIndicesPool.obtain();
  122. polygonIndices->clear();
  123. Vector<float> *polygon = _polygonPool.obtain();
  124. polygon->clear();
  125. // Merge subsequent triangles if they form a triangle fan.
  126. int fanBaseIndex = -1, lastwinding = 0;
  127. for (size_t i = 0, n = triangles.size(); i < n; i += 3) {
  128. int t1 = triangles[i] << 1, t2 = triangles[i + 1] << 1, t3 = triangles[i + 2] << 1;
  129. float x1 = vertices[t1], y1 = vertices[t1 + 1];
  130. float x2 = vertices[t2], y2 = vertices[t2 + 1];
  131. float x3 = vertices[t3], y3 = vertices[t3 + 1];
  132. // If the base of the last triangle is the same as this triangle, check if they form a convex polygon (triangle fan).
  133. bool merged = false;
  134. if (fanBaseIndex == t1) {
  135. size_t o = polygon->size() - 4;
  136. Vector<float> &p = *polygon;
  137. int winding1 = winding(p[o], p[o + 1], p[o + 2], p[o + 3], x3, y3);
  138. int winding2 = winding(x3, y3, p[0], p[1], p[2], p[3]);
  139. if (winding1 == lastwinding && winding2 == lastwinding) {
  140. polygon->add(x3);
  141. polygon->add(y3);
  142. polygonIndices->add(t3);
  143. merged = true;
  144. }
  145. }
  146. // Otherwise make this triangle the new base.
  147. if (!merged) {
  148. if (polygon->size() > 0) {
  149. convexPolygons.add(polygon);
  150. convexPolygonsIndices.add(polygonIndices);
  151. } else {
  152. _polygonPool.free(polygon);
  153. _polygonIndicesPool.free(polygonIndices);
  154. }
  155. polygon = _polygonPool.obtain();
  156. polygon->clear();
  157. polygon->add(x1);
  158. polygon->add(y1);
  159. polygon->add(x2);
  160. polygon->add(y2);
  161. polygon->add(x3);
  162. polygon->add(y3);
  163. polygonIndices = _polygonIndicesPool.obtain();
  164. polygonIndices->clear();
  165. polygonIndices->add(t1);
  166. polygonIndices->add(t2);
  167. polygonIndices->add(t3);
  168. lastwinding = winding(x1, y1, x2, y2, x3, y3);
  169. fanBaseIndex = t1;
  170. }
  171. }
  172. if (polygon->size() > 0) {
  173. convexPolygons.add(polygon);
  174. convexPolygonsIndices.add(polygonIndices);
  175. }
  176. // Go through the list of polygons and try to merge the remaining triangles with the found triangle fans.
  177. for (size_t i = 0, n = convexPolygons.size(); i < n; ++i) {
  178. polygonIndices = convexPolygonsIndices[i];
  179. if (polygonIndices->size() == 0) continue;
  180. int firstIndex = (*polygonIndices)[0];
  181. int lastIndex = (*polygonIndices)[polygonIndices->size() - 1];
  182. polygon = convexPolygons[i];
  183. size_t o = polygon->size() - 4;
  184. Vector<float> &p = *polygon;
  185. float prevPrevX = p[o], prevPrevY = p[o + 1];
  186. float prevX = p[o + 2], prevY = p[o + 3];
  187. float firstX = p[0], firstY = p[1];
  188. float secondX = p[2], secondY = p[3];
  189. int winding0 = winding(prevPrevX, prevPrevY, prevX, prevY, firstX, firstY);
  190. for (size_t ii = 0; ii < n; ++ii) {
  191. if (ii == i) continue;
  192. Vector<int> *otherIndicesP = convexPolygonsIndices[ii];
  193. Vector<int> &otherIndices = *otherIndicesP;
  194. if (otherIndices.size() != 3) continue;
  195. int otherFirstIndex = otherIndices[0];
  196. int otherSecondIndex = otherIndices[1];
  197. int otherLastIndex = otherIndices[2];
  198. Vector<float> *otherPolyP = convexPolygons[ii];
  199. Vector<float> &otherPoly = *otherPolyP;
  200. float x3 = otherPoly[otherPoly.size() - 2], y3 = otherPoly[otherPoly.size() - 1];
  201. if (otherFirstIndex != firstIndex || otherSecondIndex != lastIndex) continue;
  202. int winding1 = winding(prevPrevX, prevPrevY, prevX, prevY, x3, y3);
  203. int winding2 = winding(x3, y3, firstX, firstY, secondX, secondY);
  204. if (winding1 == winding0 && winding2 == winding0) {
  205. otherPoly.clear();
  206. otherIndices.clear();
  207. polygon->add(x3);
  208. polygon->add(y3);
  209. polygonIndices->add(otherLastIndex);
  210. prevPrevX = prevX;
  211. prevPrevY = prevY;
  212. prevX = x3;
  213. prevY = y3;
  214. ii = 0;
  215. }
  216. }
  217. }
  218. // Remove empty polygons that resulted from the merge step above.
  219. for (int i = (int)convexPolygons.size() - 1; i >= 0; --i) {
  220. polygon = convexPolygons[i];
  221. if (polygon->size() == 0) {
  222. convexPolygons.removeAt(i);
  223. _polygonPool.free(polygon);
  224. polygonIndices = convexPolygonsIndices[i];
  225. convexPolygonsIndices.removeAt(i);
  226. _polygonIndicesPool.free(polygonIndices);
  227. }
  228. }
  229. return convexPolygons;
  230. }
  231. bool Triangulator::isConcave(int index, int vertexCount, Vector<float> &vertices, Vector<int> &indices) {
  232. int previous = indices[(vertexCount + index - 1) % vertexCount] << 1;
  233. int current = indices[index] << 1;
  234. int next = indices[(index + 1) % vertexCount] << 1;
  235. return !positiveArea(vertices[previous], vertices[previous + 1],
  236. vertices[current], vertices[current + 1],
  237. vertices[next], vertices[next + 1]);
  238. }
  239. bool Triangulator::positiveArea(float p1x, float p1y, float p2x, float p2y, float p3x, float p3y) {
  240. return p1x * (p3y - p2y) + p2x * (p1y - p3y) + p3x * (p2y - p1y) >= 0;
  241. }
  242. int Triangulator::winding(float p1x, float p1y, float p2x, float p2y, float p3x, float p3y) {
  243. float px = p2x - p1x, py = p2y - p1y;
  244. return p3x * py - p3y * px + px * p1y - p1x * py >= 0 ? 1 : -1;
  245. }