triangulate.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*************************************************************************/
  2. /* triangulate.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "triangulate.h"
  31. float Triangulate::get_area(const Vector<Vector2> &contour) {
  32. int n = contour.size();
  33. const Vector2 *c = &contour[0];
  34. float A = 0.0f;
  35. for (int p = n - 1, q = 0; q < n; p = q++) {
  36. A += c[p].cross(c[q]);
  37. }
  38. return A * 0.5f;
  39. }
  40. /*
  41. is_inside_triangle decides if a point P is Inside of the triangle
  42. defined by A, B, C.
  43. */
  44. bool Triangulate::is_inside_triangle(float Ax, float Ay,
  45. float Bx, float By,
  46. float Cx, float Cy,
  47. float Px, float Py)
  48. {
  49. float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
  50. float cCROSSap, bCROSScp, aCROSSbp;
  51. ax = Cx - Bx;
  52. ay = Cy - By;
  53. bx = Ax - Cx;
  54. by = Ay - Cy;
  55. cx = Bx - Ax;
  56. cy = By - Ay;
  57. apx = Px - Ax;
  58. apy = Py - Ay;
  59. bpx = Px - Bx;
  60. bpy = Py - By;
  61. cpx = Px - Cx;
  62. cpy = Py - Cy;
  63. aCROSSbp = ax * bpy - ay * bpx;
  64. cCROSSap = cx * apy - cy * apx;
  65. bCROSScp = bx * cpy - by * cpx;
  66. return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
  67. };
  68. bool Triangulate::snip(const Vector<Vector2> &p_contour, int u, int v, int w, int n, const Vector<int> &V) {
  69. int p;
  70. float Ax, Ay, Bx, By, Cx, Cy, Px, Py;
  71. const Vector2 *contour = &p_contour[0];
  72. Ax = contour[V[u]].x;
  73. Ay = contour[V[u]].y;
  74. Bx = contour[V[v]].x;
  75. By = contour[V[v]].y;
  76. Cx = contour[V[w]].x;
  77. Cy = contour[V[w]].y;
  78. if (CMP_EPSILON > (((Bx - Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax)))) return false;
  79. for (p = 0; p < n; p++) {
  80. if ((p == u) || (p == v) || (p == w)) continue;
  81. Px = contour[V[p]].x;
  82. Py = contour[V[p]].y;
  83. if (is_inside_triangle(Ax, Ay, Bx, By, Cx, Cy, Px, Py)) return false;
  84. }
  85. return true;
  86. }
  87. bool Triangulate::triangulate(const Vector<Vector2> &contour, Vector<int> &result) {
  88. /* allocate and initialize list of Vertices in polygon */
  89. int n = contour.size();
  90. if (n < 3) return false;
  91. Vector<int> V;
  92. V.resize(n);
  93. /* we want a counter-clockwise polygon in V */
  94. if (0.0f < get_area(contour))
  95. for (int v = 0; v < n; v++)
  96. V[v] = v;
  97. else
  98. for (int v = 0; v < n; v++)
  99. V[v] = (n - 1) - v;
  100. int nv = n;
  101. /* remove nv-2 Vertices, creating 1 triangle every time */
  102. int count = 2 * nv; /* error detection */
  103. for (int v = nv - 1; nv > 2;) {
  104. /* if we loop, it is probably a non-simple polygon */
  105. if (0 >= (count--)) {
  106. //** Triangulate: ERROR - probable bad polygon!
  107. return false;
  108. }
  109. /* three consecutive vertices in current polygon, <u,v,w> */
  110. int u = v;
  111. if (nv <= u) u = 0; /* previous */
  112. v = u + 1;
  113. if (nv <= v) v = 0; /* new v */
  114. int w = v + 1;
  115. if (nv <= w) w = 0; /* next */
  116. if (snip(contour, u, v, w, nv, V)) {
  117. int a, b, c, s, t;
  118. /* true names of the vertices */
  119. a = V[u];
  120. b = V[v];
  121. c = V[w];
  122. /* output Triangle */
  123. result.push_back(a);
  124. result.push_back(b);
  125. result.push_back(c);
  126. /* remove v from remaining polygon */
  127. for (s = v, t = v + 1; t < nv; s++, t++)
  128. V[s] = V[t];
  129. nv--;
  130. /* resest error detection counter */
  131. count = 2 * nv;
  132. }
  133. }
  134. return true;
  135. }