delaunay_2d.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. Vector2 circum_center;
  39. real_t circum_radius_squared;
  40. Triangle() {}
  41. Triangle(int p_a, int p_b, int p_c) {
  42. points[0] = p_a;
  43. points[1] = p_b;
  44. points[2] = p_c;
  45. }
  46. };
  47. struct Edge {
  48. int points[2];
  49. bool bad = false;
  50. Edge() {}
  51. Edge(int p_a, int p_b) {
  52. // Store indices in a sorted manner to avoid having to check both orientations later.
  53. if (p_a > p_b) {
  54. points[0] = p_b;
  55. points[1] = p_a;
  56. } else {
  57. points[0] = p_a;
  58. points[1] = p_b;
  59. }
  60. }
  61. };
  62. static Triangle create_triangle(const Vector<Vector2> &p_vertices, const int &p_a, const int &p_b, const int &p_c) {
  63. Triangle triangle = Triangle(p_a, p_b, p_c);
  64. // Get the values of the circumcircle and store them inside the triangle object.
  65. Vector2 a = p_vertices[p_b] - p_vertices[p_a];
  66. Vector2 b = p_vertices[p_c] - p_vertices[p_a];
  67. Vector2 O = (b * a.length_squared() - a * b.length_squared()).orthogonal() / (a.cross(b) * 2.0f);
  68. triangle.circum_radius_squared = O.length_squared();
  69. triangle.circum_center = O + p_vertices[p_a];
  70. return triangle;
  71. }
  72. static Vector<Triangle> triangulate(const Vector<Vector2> &p_points) {
  73. Vector<Vector2> points = p_points;
  74. Vector<Triangle> triangles;
  75. int point_count = p_points.size();
  76. if (point_count <= 2) {
  77. return triangles;
  78. }
  79. // Get a bounding rectangle.
  80. Rect2 rect = Rect2(p_points[0], Size2());
  81. for (int i = 1; i < point_count; i++) {
  82. rect.expand_to(p_points[i]);
  83. }
  84. real_t delta_max = MAX(rect.size.width, rect.size.height);
  85. Vector2 center = rect.get_center();
  86. // Construct a bounding triangle around the rectangle.
  87. points.push_back(Vector2(center.x - delta_max * 16, center.y - delta_max));
  88. points.push_back(Vector2(center.x, center.y + delta_max * 16));
  89. points.push_back(Vector2(center.x + delta_max * 16, center.y - delta_max));
  90. Triangle bounding_triangle = create_triangle(points, point_count + 0, point_count + 1, point_count + 2);
  91. triangles.push_back(bounding_triangle);
  92. for (int i = 0; i < point_count; i++) {
  93. Vector<Edge> polygon;
  94. // Save the edges of the triangles whose circumcircles contain the i-th vertex. Delete the triangles themselves.
  95. for (int j = triangles.size() - 1; j >= 0; j--) {
  96. if (points[i].distance_squared_to(triangles[j].circum_center) < triangles[j].circum_radius_squared) {
  97. polygon.push_back(Edge(triangles[j].points[0], triangles[j].points[1]));
  98. polygon.push_back(Edge(triangles[j].points[1], triangles[j].points[2]));
  99. polygon.push_back(Edge(triangles[j].points[2], triangles[j].points[0]));
  100. triangles.remove_at(j);
  101. }
  102. }
  103. // Create a triangle for every unique edge.
  104. for (int j = 0; j < polygon.size(); j++) {
  105. if (polygon[j].bad) {
  106. continue;
  107. }
  108. for (int k = j + 1; k < polygon.size(); k++) {
  109. // Compare the edges.
  110. if (polygon[k].points[0] == polygon[j].points[0] && polygon[k].points[1] == polygon[j].points[1]) {
  111. polygon.write[j].bad = true;
  112. polygon.write[k].bad = true;
  113. break; // Since no more than two triangles can share an edge, no more than two edges can share vertices.
  114. }
  115. }
  116. // Create triangles out of good edges.
  117. if (!polygon[j].bad) {
  118. triangles.push_back(create_triangle(points, polygon[j].points[0], polygon[j].points[1], i));
  119. }
  120. }
  121. }
  122. // Filter out the triangles containing vertices of the bounding triangle.
  123. int preserved_count = 0;
  124. Triangle *triangles_ptrw = triangles.ptrw();
  125. for (int i = 0; i < triangles.size(); i++) {
  126. if (!(triangles[i].points[0] >= point_count || triangles[i].points[1] >= point_count || triangles[i].points[2] >= point_count)) {
  127. triangles_ptrw[preserved_count] = triangles[i];
  128. preserved_count++;
  129. }
  130. }
  131. triangles.resize(preserved_count);
  132. return triangles;
  133. }
  134. };
  135. #endif // DELAUNAY_2D_H