triangulate.cpp 6.1 KB

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