Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/math/delaunay_2d.h
9896 views
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
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
// Store indices in a sorted manner to avoid having to check both orientations later.
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
// Get the values of the circumcircle and store them inside the triangle object.
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
// Get a bounding rectangle.
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
// Construct a bounding triangle around the rectangle.
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
// Save the edges of the triangles whose circumcircles contain the i-th vertex. Delete the triangles themselves.
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
// Create a triangle for every unique edge.
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
// Compare the edges.
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; // Since no more than two triangles can share an edge, no more than two edges can share vertices.
134
}
135
}
136
137
// Create triangles out of good edges.
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
// Filter out the triangles containing vertices of the bounding triangle.
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
};
158
159