delaunay_2d.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*************************************************************************/
  2. /* delaunay_2d.h */
  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. #ifndef DELAUNAY_2D_H
  31. #define DELAUNAY_2D_H
  32. #include "core/math/rect2.h"
  33. class Delaunay2D {
  34. public:
  35. struct Triangle {
  36. int points[3];
  37. bool bad = false;
  38. Triangle() {}
  39. Triangle(int p_a, int p_b, int p_c) {
  40. points[0] = p_a;
  41. points[1] = p_b;
  42. points[2] = p_c;
  43. }
  44. };
  45. struct Edge {
  46. int edge[2];
  47. bool bad = false;
  48. Edge() {}
  49. Edge(int p_a, int p_b) {
  50. edge[0] = p_a;
  51. edge[1] = p_b;
  52. }
  53. };
  54. static bool circum_circle_contains(const Vector<Vector2> &p_vertices, const Triangle &p_triangle, int p_vertex) {
  55. Vector2 p1 = p_vertices[p_triangle.points[0]];
  56. Vector2 p2 = p_vertices[p_triangle.points[1]];
  57. Vector2 p3 = p_vertices[p_triangle.points[2]];
  58. real_t ab = p1.x * p1.x + p1.y * p1.y;
  59. real_t cd = p2.x * p2.x + p2.y * p2.y;
  60. real_t ef = p3.x * p3.x + p3.y * p3.y;
  61. Vector2 circum(
  62. (ab * (p3.y - p2.y) + cd * (p1.y - p3.y) + ef * (p2.y - p1.y)) / (p1.x * (p3.y - p2.y) + p2.x * (p1.y - p3.y) + p3.x * (p2.y - p1.y)),
  63. (ab * (p3.x - p2.x) + cd * (p1.x - p3.x) + ef * (p2.x - p1.x)) / (p1.y * (p3.x - p2.x) + p2.y * (p1.x - p3.x) + p3.y * (p2.x - p1.x)));
  64. circum *= 0.5;
  65. float r = p1.distance_squared_to(circum);
  66. float d = p_vertices[p_vertex].distance_squared_to(circum);
  67. return d <= r;
  68. }
  69. static bool edge_compare(const Vector<Vector2> &p_vertices, const Edge &p_a, const Edge &p_b) {
  70. if (p_vertices[p_a.edge[0]].is_equal_approx(p_vertices[p_b.edge[0]]) && p_vertices[p_a.edge[1]].is_equal_approx(p_vertices[p_b.edge[1]])) {
  71. return true;
  72. }
  73. if (p_vertices[p_a.edge[0]].is_equal_approx(p_vertices[p_b.edge[1]]) && p_vertices[p_a.edge[1]].is_equal_approx(p_vertices[p_b.edge[0]])) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. static Vector<Triangle> triangulate(const Vector<Vector2> &p_points) {
  79. Vector<Vector2> points = p_points;
  80. Vector<Triangle> triangles;
  81. Rect2 rect;
  82. for (int i = 0; i < p_points.size(); i++) {
  83. if (i == 0) {
  84. rect.position = p_points[i];
  85. } else {
  86. rect.expand_to(p_points[i]);
  87. }
  88. }
  89. float delta_max = MAX(rect.size.width, rect.size.height);
  90. Vector2 center = rect.position + rect.size * 0.5;
  91. points.push_back(Vector2(center.x - 20 * delta_max, center.y - delta_max));
  92. points.push_back(Vector2(center.x, center.y + 20 * delta_max));
  93. points.push_back(Vector2(center.x + 20 * delta_max, center.y - delta_max));
  94. triangles.push_back(Triangle(p_points.size() + 0, p_points.size() + 1, p_points.size() + 2));
  95. for (int i = 0; i < p_points.size(); i++) {
  96. Vector<Edge> polygon;
  97. for (int j = 0; j < triangles.size(); j++) {
  98. if (circum_circle_contains(points, triangles[j], i)) {
  99. triangles.write[j].bad = true;
  100. polygon.push_back(Edge(triangles[j].points[0], triangles[j].points[1]));
  101. polygon.push_back(Edge(triangles[j].points[1], triangles[j].points[2]));
  102. polygon.push_back(Edge(triangles[j].points[2], triangles[j].points[0]));
  103. }
  104. }
  105. for (int j = 0; j < triangles.size(); j++) {
  106. if (triangles[j].bad) {
  107. triangles.remove(j);
  108. j--;
  109. }
  110. }
  111. for (int j = 0; j < polygon.size(); j++) {
  112. for (int k = j + 1; k < polygon.size(); k++) {
  113. if (edge_compare(points, polygon[j], polygon[k])) {
  114. polygon.write[j].bad = true;
  115. polygon.write[k].bad = true;
  116. }
  117. }
  118. }
  119. for (int j = 0; j < polygon.size(); j++) {
  120. if (polygon[j].bad) {
  121. continue;
  122. }
  123. triangles.push_back(Triangle(polygon[j].edge[0], polygon[j].edge[1], i));
  124. }
  125. }
  126. for (int i = 0; i < triangles.size(); i++) {
  127. bool invalid = false;
  128. for (int j = 0; j < 3; j++) {
  129. if (triangles[i].points[j] >= p_points.size()) {
  130. invalid = true;
  131. break;
  132. }
  133. }
  134. if (invalid) {
  135. triangles.remove(i);
  136. i--;
  137. }
  138. }
  139. return triangles;
  140. }
  141. };
  142. #endif // DELAUNAY_2D_H