triangulate.cpp 6.1 KB

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