Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/math/delaunay_3d.h
9896 views
1
/**************************************************************************/
2
/* delaunay_3d.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/aabb.h"
34
#include "core/math/projection.h"
35
#include "core/math/vector3.h"
36
#include "core/templates/a_hash_map.h"
37
#include "core/templates/list.h"
38
#include "core/templates/local_vector.h"
39
#include "core/templates/vector.h"
40
41
#include "thirdparty/misc/r128.h"
42
43
class Delaunay3D {
44
struct Simplex;
45
46
enum {
47
ACCEL_GRID_SIZE = 16,
48
QUANTIZATION_MAX = 1 << 16 // A power of two smaller than the 23 bit significand of a float.
49
};
50
struct GridPos {
51
Vector3i pos;
52
List<Simplex *>::Element *E = nullptr;
53
};
54
55
struct Simplex {
56
uint32_t points[4];
57
R128 circum_center_x;
58
R128 circum_center_y;
59
R128 circum_center_z;
60
R128 circum_r2;
61
LocalVector<GridPos> grid_positions;
62
List<Simplex *>::Element *SE = nullptr;
63
64
_FORCE_INLINE_ Simplex() {}
65
_FORCE_INLINE_ Simplex(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d) {
66
points[0] = p_a;
67
points[1] = p_b;
68
points[2] = p_c;
69
points[3] = p_d;
70
}
71
};
72
73
struct Triangle {
74
uint32_t triangle[3];
75
bool bad = false;
76
_FORCE_INLINE_ bool operator==(const Triangle &p_triangle) const {
77
return triangle[0] == p_triangle.triangle[0] && triangle[1] == p_triangle.triangle[1] && triangle[2] == p_triangle.triangle[2];
78
}
79
80
_FORCE_INLINE_ Triangle() {}
81
_FORCE_INLINE_ Triangle(uint32_t p_a, uint32_t p_b, uint32_t p_c) {
82
if (p_a > p_b) {
83
SWAP(p_a, p_b);
84
}
85
if (p_b > p_c) {
86
SWAP(p_b, p_c);
87
}
88
if (p_a > p_b) {
89
SWAP(p_a, p_b);
90
}
91
92
triangle[0] = p_a;
93
triangle[1] = p_b;
94
triangle[2] = p_c;
95
}
96
};
97
98
struct TriangleHasher {
99
_FORCE_INLINE_ static uint32_t hash(const Triangle &p_triangle) {
100
uint32_t h = hash_djb2_one_32(p_triangle.triangle[0]);
101
h = hash_djb2_one_32(p_triangle.triangle[1], h);
102
return hash_fmix32(hash_djb2_one_32(p_triangle.triangle[2], h));
103
}
104
};
105
106
_FORCE_INLINE_ static void circum_sphere_compute(const Vector3 *p_points, Simplex *p_simplex) {
107
// The only part in the algorithm where there may be precision errors is this one,
108
// so ensure that we do it with the maximum precision possible.
109
110
R128 v0_x = p_points[p_simplex->points[0]].x;
111
R128 v0_y = p_points[p_simplex->points[0]].y;
112
R128 v0_z = p_points[p_simplex->points[0]].z;
113
R128 v1_x = p_points[p_simplex->points[1]].x;
114
R128 v1_y = p_points[p_simplex->points[1]].y;
115
R128 v1_z = p_points[p_simplex->points[1]].z;
116
R128 v2_x = p_points[p_simplex->points[2]].x;
117
R128 v2_y = p_points[p_simplex->points[2]].y;
118
R128 v2_z = p_points[p_simplex->points[2]].z;
119
R128 v3_x = p_points[p_simplex->points[3]].x;
120
R128 v3_y = p_points[p_simplex->points[3]].y;
121
R128 v3_z = p_points[p_simplex->points[3]].z;
122
123
// Create the rows of our "unrolled" 3x3 matrix.
124
R128 row1_x = v1_x - v0_x;
125
R128 row1_y = v1_y - v0_y;
126
R128 row1_z = v1_z - v0_z;
127
128
R128 row2_x = v2_x - v0_x;
129
R128 row2_y = v2_y - v0_y;
130
R128 row2_z = v2_z - v0_z;
131
132
R128 row3_x = v3_x - v0_x;
133
R128 row3_y = v3_y - v0_y;
134
R128 row3_z = v3_z - v0_z;
135
136
R128 sq_length1 = row1_x * row1_x + row1_y * row1_y + row1_z * row1_z;
137
R128 sq_length2 = row2_x * row2_x + row2_y * row2_y + row2_z * row2_z;
138
R128 sq_length3 = row3_x * row3_x + row3_y * row3_y + row3_z * row3_z;
139
140
// Compute the determinant of said matrix.
141
R128 determinant = row1_x * (row2_y * row3_z - row3_y * row2_z) - row2_x * (row1_y * row3_z - row3_y * row1_z) + row3_x * (row1_y * row2_z - row2_y * row1_z);
142
143
// Compute the volume of the tetrahedron, and precompute a scalar quantity for reuse in the formula.
144
R128 volume = determinant / R128(6.f);
145
R128 i12volume = R128(1.f) / (volume * R128(12.f));
146
147
R128 center_x = v0_x + i12volume * ((row2_y * row3_z - row3_y * row2_z) * sq_length1 - (row1_y * row3_z - row3_y * row1_z) * sq_length2 + (row1_y * row2_z - row2_y * row1_z) * sq_length3);
148
R128 center_y = v0_y + i12volume * (-(row2_x * row3_z - row3_x * row2_z) * sq_length1 + (row1_x * row3_z - row3_x * row1_z) * sq_length2 - (row1_x * row2_z - row2_x * row1_z) * sq_length3);
149
R128 center_z = v0_z + i12volume * ((row2_x * row3_y - row3_x * row2_y) * sq_length1 - (row1_x * row3_y - row3_x * row1_y) * sq_length2 + (row1_x * row2_y - row2_x * row1_y) * sq_length3);
150
151
// Once we know the center, the radius is clearly the distance to any vertex.
152
R128 rel1_x = center_x - v0_x;
153
R128 rel1_y = center_y - v0_y;
154
R128 rel1_z = center_z - v0_z;
155
156
R128 radius1 = rel1_x * rel1_x + rel1_y * rel1_y + rel1_z * rel1_z;
157
158
p_simplex->circum_center_x = center_x;
159
p_simplex->circum_center_y = center_y;
160
p_simplex->circum_center_z = center_z;
161
p_simplex->circum_r2 = radius1;
162
}
163
164
_FORCE_INLINE_ static bool simplex_contains(const Vector3 *p_points, const Simplex &p_simplex, uint32_t p_vertex) {
165
R128 v_x = p_points[p_vertex].x;
166
R128 v_y = p_points[p_vertex].y;
167
R128 v_z = p_points[p_vertex].z;
168
169
R128 rel2_x = p_simplex.circum_center_x - v_x;
170
R128 rel2_y = p_simplex.circum_center_y - v_y;
171
R128 rel2_z = p_simplex.circum_center_z - v_z;
172
173
R128 radius2 = rel2_x * rel2_x + rel2_y * rel2_y + rel2_z * rel2_z;
174
175
return radius2 < (p_simplex.circum_r2 - R128(0.0000000001));
176
// When this tolerance is too big, it can result in overlapping simplices.
177
// When it's too small, large amounts of planar simplices are created.
178
}
179
180
static bool simplex_is_coplanar(const Vector3 *p_points, const Simplex &p_simplex) {
181
// Checking every possible distance like this is overkill, but only checking
182
// one is not enough. If the simplex is almost planar then the vectors p1-p2
183
// and p1-p3 can be practically collinear, which makes Plane unreliable.
184
for (uint32_t i = 0; i < 4; i++) {
185
Plane p(p_points[p_simplex.points[i]], p_points[p_simplex.points[(i + 1) % 4]], p_points[p_simplex.points[(i + 2) % 4]]);
186
// This tolerance should not be smaller than the one used with
187
// Plane::has_point() when creating the LightmapGI probe BSP tree.
188
if (Math::abs(p.distance_to(p_points[p_simplex.points[(i + 3) % 4]])) < 0.001) {
189
return true;
190
}
191
}
192
193
return false;
194
}
195
196
public:
197
struct OutputSimplex {
198
uint32_t points[4];
199
};
200
201
static Vector<OutputSimplex> tetrahedralize(const Vector<Vector3> &p_points) {
202
uint32_t point_count = p_points.size();
203
Vector3 *points = (Vector3 *)memalloc(sizeof(Vector3) * (point_count + 4));
204
const Vector3 *src_points = p_points.ptr();
205
Vector3 proportions;
206
207
{
208
AABB rect;
209
for (uint32_t i = 0; i < point_count; i++) {
210
Vector3 point = src_points[i];
211
if (i == 0) {
212
rect.position = point;
213
} else {
214
rect.expand_to(point);
215
}
216
}
217
218
real_t longest_axis = rect.size[rect.get_longest_axis_index()];
219
proportions = Vector3(longest_axis, longest_axis, longest_axis) / rect.size;
220
221
for (uint32_t i = 0; i < point_count; i++) {
222
// Scale points to the unit cube to better utilize R128 precision
223
// and quantize to stabilize triangulation over a wide range of
224
// distances.
225
points[i] = Vector3(Vector3i((src_points[i] - rect.position) / longest_axis * QUANTIZATION_MAX)) / QUANTIZATION_MAX;
226
}
227
228
const real_t delta_max = Math::sqrt(2.0) * 100.0;
229
Vector3 center = Vector3(0.5, 0.5, 0.5);
230
231
// The larger the root simplex is, the more likely it is that the
232
// triangulation is convex. If it's not absolutely huge, there can
233
// be missing simplices that are not created for the outermost faces
234
// of the point cloud if the point density is very low there.
235
points[point_count + 0] = center + Vector3(0, 1, 0) * delta_max;
236
points[point_count + 1] = center + Vector3(0, -1, 1) * delta_max;
237
points[point_count + 2] = center + Vector3(1, -1, -1) * delta_max;
238
points[point_count + 3] = center + Vector3(-1, -1, -1) * delta_max;
239
}
240
241
List<Simplex *> acceleration_grid[ACCEL_GRID_SIZE][ACCEL_GRID_SIZE][ACCEL_GRID_SIZE];
242
243
List<Simplex *> simplex_list;
244
{
245
//create root simplex
246
Simplex *root = memnew(Simplex(point_count + 0, point_count + 1, point_count + 2, point_count + 3));
247
root->SE = simplex_list.push_back(root);
248
249
for (uint32_t i = 0; i < ACCEL_GRID_SIZE; i++) {
250
for (uint32_t j = 0; j < ACCEL_GRID_SIZE; j++) {
251
for (uint32_t k = 0; k < ACCEL_GRID_SIZE; k++) {
252
GridPos gp;
253
gp.E = acceleration_grid[i][j][k].push_back(root);
254
gp.pos = Vector3i(i, j, k);
255
root->grid_positions.push_back(gp);
256
}
257
}
258
}
259
260
circum_sphere_compute(points, root);
261
}
262
263
AHashMap<Triangle, uint32_t, TriangleHasher> triangles_inserted;
264
LocalVector<Triangle> triangles;
265
266
for (uint32_t i = 0; i < point_count; i++) {
267
bool unique = true;
268
for (uint32_t j = i + 1; j < point_count; j++) {
269
if (points[i] == points[j]) {
270
unique = false;
271
break;
272
}
273
}
274
if (!unique) {
275
continue;
276
}
277
278
Vector3i grid_pos = Vector3i(points[i] * proportions * ACCEL_GRID_SIZE);
279
grid_pos = grid_pos.clampi(0, ACCEL_GRID_SIZE - 1);
280
281
for (List<Simplex *>::Element *E = acceleration_grid[grid_pos.x][grid_pos.y][grid_pos.z].front(); E;) {
282
List<Simplex *>::Element *N = E->next(); //may be deleted
283
284
Simplex *simplex = E->get();
285
286
if (simplex_contains(points, *simplex, i)) {
287
static const uint32_t triangle_order[4][3] = {
288
{ 0, 1, 2 },
289
{ 0, 1, 3 },
290
{ 0, 2, 3 },
291
{ 1, 2, 3 },
292
};
293
294
for (uint32_t k = 0; k < 4; k++) {
295
Triangle t = Triangle(simplex->points[triangle_order[k][0]], simplex->points[triangle_order[k][1]], simplex->points[triangle_order[k][2]]);
296
uint32_t *p = triangles_inserted.getptr(t);
297
if (p) {
298
// This Delaunay implementation uses the Bowyer-Watson algorithm.
299
// The rule is that you don't reuse any triangles that were
300
// shared by any of the retriangulated simplices.
301
triangles[*p].bad = true;
302
} else {
303
triangles_inserted.insert(t, triangles.size());
304
triangles.push_back(t);
305
}
306
}
307
308
simplex_list.erase(simplex->SE);
309
310
for (const GridPos &gp : simplex->grid_positions) {
311
Vector3i p = gp.pos;
312
acceleration_grid[p.x][p.y][p.z].erase(gp.E);
313
}
314
memdelete(simplex);
315
}
316
E = N;
317
}
318
319
for (const Triangle &triangle : triangles) {
320
if (triangle.bad) {
321
continue;
322
}
323
Simplex *new_simplex = memnew(Simplex(triangle.triangle[0], triangle.triangle[1], triangle.triangle[2], i));
324
circum_sphere_compute(points, new_simplex);
325
new_simplex->SE = simplex_list.push_back(new_simplex);
326
{
327
Vector3 center;
328
center.x = double(new_simplex->circum_center_x);
329
center.y = double(new_simplex->circum_center_y);
330
center.z = double(new_simplex->circum_center_z);
331
332
const real_t radius2 = Math::sqrt(double(new_simplex->circum_r2)) + 0.0001;
333
Vector3 extents = Vector3(radius2, radius2, radius2);
334
Vector3i from = Vector3i((center - extents) * proportions * ACCEL_GRID_SIZE);
335
Vector3i to = Vector3i((center + extents) * proportions * ACCEL_GRID_SIZE);
336
from = from.clampi(0, ACCEL_GRID_SIZE - 1);
337
to = to.clampi(0, ACCEL_GRID_SIZE - 1);
338
339
for (int32_t x = from.x; x <= to.x; x++) {
340
for (int32_t y = from.y; y <= to.y; y++) {
341
for (int32_t z = from.z; z <= to.z; z++) {
342
GridPos gp;
343
gp.pos = Vector3(x, y, z);
344
gp.E = acceleration_grid[x][y][z].push_back(new_simplex);
345
new_simplex->grid_positions.push_back(gp);
346
}
347
}
348
}
349
}
350
}
351
352
triangles.clear();
353
triangles_inserted.clear();
354
}
355
356
//print_line("end with simplices: " + itos(simplex_list.size()));
357
Vector<OutputSimplex> ret_simplices;
358
ret_simplices.resize(simplex_list.size());
359
OutputSimplex *ret_simplicesw = ret_simplices.ptrw();
360
uint32_t simplices_written = 0;
361
362
for (Simplex *simplex : simplex_list) {
363
bool invalid = false;
364
for (int j = 0; j < 4; j++) {
365
if (simplex->points[j] >= point_count) {
366
invalid = true;
367
break;
368
}
369
}
370
if (invalid || simplex_is_coplanar(src_points, *simplex)) {
371
memdelete(simplex);
372
continue;
373
}
374
375
ret_simplicesw[simplices_written].points[0] = simplex->points[0];
376
ret_simplicesw[simplices_written].points[1] = simplex->points[1];
377
ret_simplicesw[simplices_written].points[2] = simplex->points[2];
378
ret_simplicesw[simplices_written].points[3] = simplex->points[3];
379
simplices_written++;
380
memdelete(simplex);
381
}
382
383
ret_simplices.resize(simplices_written);
384
385
memfree(points);
386
387
return ret_simplices;
388
}
389
};
390
391