delaunay_2d.h 5.9 KB

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