delaunay.h 6.0 KB

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