Debian Codegraph @

core/math/delaunay_3d.h

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