triangulate.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**************************************************************************/
  2. /* triangulate.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. real_t Triangulate::get_area(const Vector<Vector2> &contour) {
  32. int n = contour.size();
  33. const Vector2 *c = &contour[0];
  34. real_t A = 0.0;
  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. /* `is_inside_triangle` decides if a point P is inside the triangle
  41. * defined by A, B, C. */
  42. bool Triangulate::is_inside_triangle(real_t Ax, real_t Ay,
  43. real_t Bx, real_t By,
  44. real_t Cx, real_t Cy,
  45. real_t Px, real_t Py,
  46. bool include_edges) {
  47. real_t ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
  48. real_t cCROSSap, bCROSScp, aCROSSbp;
  49. ax = Cx - Bx;
  50. ay = Cy - By;
  51. bx = Ax - Cx;
  52. by = Ay - Cy;
  53. cx = Bx - Ax;
  54. cy = By - Ay;
  55. apx = Px - Ax;
  56. apy = Py - Ay;
  57. bpx = Px - Bx;
  58. bpy = Py - By;
  59. cpx = Px - Cx;
  60. cpy = Py - Cy;
  61. aCROSSbp = ax * bpy - ay * bpx;
  62. cCROSSap = cx * apy - cy * apx;
  63. bCROSScp = bx * cpy - by * cpx;
  64. if (include_edges) {
  65. return ((aCROSSbp > 0.0f) && (bCROSScp > 0.0f) && (cCROSSap > 0.0f));
  66. } else {
  67. return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
  68. }
  69. }
  70. bool Triangulate::snip(const Vector<Vector2> &p_contour, int u, int v, int w, int n, const Vector<int> &V, bool relaxed) {
  71. int p;
  72. real_t Ax, Ay, Bx, By, Cx, Cy, Px, Py;
  73. const Vector2 *contour = &p_contour[0];
  74. Ax = contour[V[u]].x;
  75. Ay = contour[V[u]].y;
  76. Bx = contour[V[v]].x;
  77. By = contour[V[v]].y;
  78. Cx = contour[V[w]].x;
  79. Cy = contour[V[w]].y;
  80. // It can happen that the triangulation ends up with three aligned vertices to deal with.
  81. // In this scenario, making the check below strict may reject the possibility of
  82. // forming a last triangle with these aligned vertices, preventing the triangulation
  83. // from completing.
  84. // To avoid that we allow zero-area triangles if all else failed.
  85. float threshold = relaxed ? -CMP_EPSILON : CMP_EPSILON;
  86. if (threshold > (((Bx - Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax)))) {
  87. return false;
  88. }
  89. for (p = 0; p < n; p++) {
  90. if ((p == u) || (p == v) || (p == w)) {
  91. continue;
  92. }
  93. Px = contour[V[p]].x;
  94. Py = contour[V[p]].y;
  95. if (is_inside_triangle(Ax, Ay, Bx, By, Cx, Cy, Px, Py, relaxed)) {
  96. return false;
  97. }
  98. }
  99. return true;
  100. }
  101. bool Triangulate::triangulate(const Vector<Vector2> &contour, Vector<int> &result) {
  102. /* allocate and initialize list of Vertices in polygon */
  103. int n = contour.size();
  104. if (n < 3) {
  105. return false;
  106. }
  107. Vector<int> V;
  108. V.resize(n);
  109. /* we want a counter-clockwise polygon in V */
  110. if (0.0f < get_area(contour)) {
  111. for (int v = 0; v < n; v++) {
  112. V.write[v] = v;
  113. }
  114. } else {
  115. for (int v = 0; v < n; v++) {
  116. V.write[v] = (n - 1) - v;
  117. }
  118. }
  119. bool relaxed = false;
  120. int nv = n;
  121. /* remove nv-2 Vertices, creating 1 triangle every time */
  122. int count = 2 * nv; /* error detection */
  123. for (int v = nv - 1; nv > 2;) {
  124. /* if we loop, it is probably a non-simple polygon */
  125. if (0 >= (count--)) {
  126. if (relaxed) {
  127. //** Triangulate: ERROR - probable bad polygon!
  128. return false;
  129. } else {
  130. // There may be aligned vertices that the strict
  131. // checks prevent from triangulating. In this situation
  132. // we are better off adding flat triangles than
  133. // failing, so we relax the checks and try one last
  134. // round.
  135. // Only relaxing the constraints as a last resort avoids
  136. // degenerate triangles when they aren't necessary.
  137. count = 2 * nv;
  138. relaxed = true;
  139. }
  140. }
  141. /* three consecutive vertices in current polygon, <u,v,w> */
  142. int u = v;
  143. if (nv <= u) {
  144. u = 0; /* previous */
  145. }
  146. v = u + 1;
  147. if (nv <= v) {
  148. v = 0; /* new v */
  149. }
  150. int w = v + 1;
  151. if (nv <= w) {
  152. w = 0; /* next */
  153. }
  154. if (snip(contour, u, v, w, nv, V, relaxed)) {
  155. int a, b, c, s, t;
  156. /* true names of the vertices */
  157. a = V[u];
  158. b = V[v];
  159. c = V[w];
  160. /* output Triangle */
  161. result.push_back(a);
  162. result.push_back(b);
  163. result.push_back(c);
  164. /* remove v from remaining polygon */
  165. for (s = v, t = v + 1; t < nv; s++, t++) {
  166. V.write[s] = V[t];
  167. }
  168. nv--;
  169. /* reset error detection counter */
  170. count = 2 * nv;
  171. }
  172. }
  173. return true;
  174. }