| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/math/rect2.h" |
| 34 |
#include "core/templates/vector.h" |
| 35 |
|
| 36 |
class Delaunay2D { |
| 37 |
public: |
| 38 |
struct Triangle { |
| 39 |
int points[3]; |
| 40 |
Vector2 circum_center; |
| 41 |
real_t circum_radius_squared; |
| 42 |
Triangle() {} |
| 43 |
Triangle(int p_a, int p_b, int p_c) { |
| 44 |
points[0] = p_a; |
| 45 |
points[1] = p_b; |
| 46 |
points[2] = p_c; |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
struct Edge { |
| 51 |
int points[2]; |
| 52 |
bool bad = false; |
| 53 |
Edge() {} |
| 54 |
Edge(int p_a, int p_b) { |
| 55 |
|
| 56 |
if (p_a > p_b) { |
| 57 |
points[0] = p_b; |
| 58 |
points[1] = p_a; |
| 59 |
} else { |
| 60 |
points[0] = p_a; |
| 61 |
points[1] = p_b; |
| 62 |
} |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
static Triangle create_triangle(const Vector<Vector2> &p_vertices, int p_a, int p_b, int p_c) { |
| 67 |
Triangle triangle = Triangle(p_a, p_b, p_c); |
| 68 |
|
| 69 |
|
| 70 |
Vector2 a = p_vertices[p_b] - p_vertices[p_a]; |
| 71 |
Vector2 b = p_vertices[p_c] - p_vertices[p_a]; |
| 72 |
|
| 73 |
Vector2 O = (b * a.length_squared() - a * b.length_squared()).orthogonal() / (a.cross(b) * 2.0f); |
| 74 |
|
| 75 |
triangle.circum_radius_squared = O.length_squared(); |
| 76 |
triangle.circum_center = O + p_vertices[p_a]; |
| 77 |
|
| 78 |
return triangle; |
| 79 |
} |
| 80 |
|
| 81 |
static Vector<Triangle> triangulate(const Vector<Vector2> &p_points) { |
| 82 |
Vector<Vector2> points = p_points; |
| 83 |
Vector<Triangle> triangles; |
| 84 |
|
| 85 |
int point_count = p_points.size(); |
| 86 |
if (point_count <= 2) { |
| 87 |
return triangles; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
Rect2 rect = Rect2(p_points[0], Size2()); |
| 92 |
for (int i = 1; i < point_count; i++) { |
| 93 |
rect.expand_to(p_points[i]); |
| 94 |
} |
| 95 |
|
| 96 |
real_t delta_max = MAX(rect.size.width, rect.size.height); |
| 97 |
Vector2 center = rect.get_center(); |
| 98 |
|
| 99 |
|
| 100 |
points.push_back(Vector2(center.x - delta_max * 16, center.y - delta_max)); |
| 101 |
points.push_back(Vector2(center.x, center.y + delta_max * 16)); |
| 102 |
points.push_back(Vector2(center.x + delta_max * 16, center.y - delta_max)); |
| 103 |
|
| 104 |
Triangle bounding_triangle = create_triangle(points, point_count + 0, point_count + 1, point_count + 2); |
| 105 |
triangles.push_back(bounding_triangle); |
| 106 |
|
| 107 |
for (int i = 0; i < point_count; i++) { |
| 108 |
Vector<Edge> polygon; |
| 109 |
|
| 110 |
|
| 111 |
for (int j = triangles.size() - 1; j >= 0; j--) { |
| 112 |
if (points[i].distance_squared_to(triangles[j].circum_center) < triangles[j].circum_radius_squared) { |
| 113 |
polygon.push_back(Edge(triangles[j].points[0], triangles[j].points[1])); |
| 114 |
polygon.push_back(Edge(triangles[j].points[1], triangles[j].points[2])); |
| 115 |
polygon.push_back(Edge(triangles[j].points[2], triangles[j].points[0])); |
| 116 |
|
| 117 |
triangles.remove_at(j); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
for (int j = 0; j < polygon.size(); j++) { |
| 123 |
if (polygon[j].bad) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
for (int k = j + 1; k < polygon.size(); k++) { |
| 128 |
|
| 129 |
if (polygon[k].points[0] == polygon[j].points[0] && polygon[k].points[1] == polygon[j].points[1]) { |
| 130 |
polygon.write[j].bad = true; |
| 131 |
polygon.write[k].bad = true; |
| 132 |
|
| 133 |
break; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
if (!polygon[j].bad) { |
| 139 |
triangles.push_back(create_triangle(points, polygon[j].points[0], polygon[j].points[1], i)); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
int preserved_count = 0; |
| 146 |
Triangle *triangles_ptrw = triangles.ptrw(); |
| 147 |
for (int i = 0; i < triangles.size(); i++) { |
| 148 |
if (!(triangles[i].points[0] >= point_count || triangles[i].points[1] >= point_count || triangles[i].points[2] >= point_count)) { |
| 149 |
triangles_ptrw[preserved_count] = triangles[i]; |
| 150 |
preserved_count++; |
| 151 |
} |
| 152 |
} |
| 153 |
triangles.resize(preserved_count); |
| 154 |
|
| 155 |
return triangles; |
| 156 |
} |
| 157 |
}; |