Debian Codegraph @

core/math/geometry_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* geometry_3d.cpp */
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 #include "geometry_3d.h"
32
33 #include "core/templates/hash_map.h"
34
35 void Geometry3D::get_closest_points_between_segments(const Vector3 &p_p0, const Vector3 &p_p1, const Vector3 &p_q0, const Vector3 &p_q1, Vector3 &r_ps, Vector3 &r_qt) {
36 // Based on David Eberly's Computation of Distance Between Line Segments algorithm.
37
38 Vector3 p = p_p1 - p_p0;
39 Vector3 q = p_q1 - p_q0;
40 Vector3 r = p_p0 - p_q0;
41
42 real_t a = p.dot(p);
43 real_t b = p.dot(q);
44 real_t c = q.dot(q);
45 real_t d = p.dot(r);
46 real_t e = q.dot(r);
47
48 real_t s = 0.0f;
49 real_t t = 0.0f;
50
51 real_t det = a * c - b * b;
52 if (det > CMP_EPSILON) {
53 // Non-parallel segments
54 real_t bte = b * e;
55 real_t ctd = c * d;
56
57 if (bte <= ctd) {
58 // s <= 0.0f
59 if (e <= 0.0f) {
60 // t <= 0.0f
61 s = (-d >= a ? 1 : (-d > 0.0f ? -d / a : 0.0f));
62 t = 0.0f;
63 } else if (e < c) {
64 // 0.0f < t < 1
65 s = 0.0f;
66 t = e / c;
67 } else {
68 // t >= 1
69 s = (b - d >= a ? 1 : (b - d > 0.0f ? (b - d) / a : 0.0f));
70 t = 1;
71 }
72 } else {
73 // s > 0.0f
74 s = bte - ctd;
75 if (s >= det) {
76 // s >= 1
77 if (b + e <= 0.0f) {
78 // t <= 0.0f
79 s = (-d <= 0.0f ? 0.0f : (-d < a ? -d / a : 1));
80 t = 0.0f;
81 } else if (b + e < c) {
82 // 0.0f < t < 1
83 s = 1;
84 t = (b + e) / c;
85 } else {
86 // t >= 1
87 s = (b - d <= 0.0f ? 0.0f : (b - d < a ? (b - d) / a : 1));
88 t = 1;
89 }
90 } else {
91 // 0.0f < s < 1
92 real_t ate = a * e;
93 real_t btd = b * d;
94
95 if (ate <= btd) {
96 // t <= 0.0f
97 s = (-d <= 0.0f ? 0.0f : (-d >= a ? 1 : -d / a));
98 t = 0.0f;
99 } else {
100 // t > 0.0f
101 t = ate - btd;
102 if (t >= det) {
103 // t >= 1
104 s = (b - d <= 0.0f ? 0.0f : (b - d >= a ? 1 : (b - d) / a));
105 t = 1;
106 } else {
107 // 0.0f < t < 1
108 s /= det;
109 t /= det;
110 }
111 }
112 }
113 }
114 } else {
115 // Parallel segments
116 if (e <= 0.0f) {
117 s = (-d <= 0.0f ? 0.0f : (-d >= a ? 1 : -d / a));
118 t = 0.0f;
119 } else if (e >= c) {
120 s = (b - d <= 0.0f ? 0.0f : (b - d >= a ? 1 : (b - d) / a));
121 t = 1;
122 } else {
123 s = 0.0f;
124 t = e / c;
125 }
126 }
127
128 r_ps = (1 - s) * p_p0 + s * p_p1;
129 r_qt = (1 - t) * p_q0 + t * p_q1;
130 }
131
132 real_t Geometry3D::get_closest_distance_between_segments(const Vector3 &p_p0, const Vector3 &p_p1, const Vector3 &p_q0, const Vector3 &p_q1) {
133 Vector3 ps;
134 Vector3 qt;
135 get_closest_points_between_segments(p_p0, p_p1, p_q0, p_q1, ps, qt);
136 Vector3 st = qt - ps;
137 return st.length();
138 }
139
140 void Geometry3D::MeshData::optimize_vertices() {
141 HashMap<int, int> vtx_remap;
142
143 for (MeshData::Face &face : faces) {
144 for (int &index : face.indices) {
145 if (!vtx_remap.has(index)) {
146 int ni = vtx_remap.size();
147 vtx_remap[index] = ni;
148 }
149 index = vtx_remap[index];
150 }
151 }
152
153 for (MeshData::Edge &edge : edges) {
154 int a = edge.vertex_a;
155 int b = edge.vertex_b;
156
157 if (!vtx_remap.has(a)) {
158 int ni = vtx_remap.size();
159 vtx_remap[a] = ni;
160 }
161 if (!vtx_remap.has(b)) {
162 int ni = vtx_remap.size();
163 vtx_remap[b] = ni;
164 }
165
166 edge.vertex_a = vtx_remap[a];
167 edge.vertex_b = vtx_remap[b];
168 }
169
170 LocalVector<Vector3> new_vertices;
171 new_vertices.resize(vtx_remap.size());
172
173 for (uint32_t i = 0; i < vertices.size(); i++) {
174 if (vtx_remap.has(i)) {
175 new_vertices[vtx_remap[i]] = vertices[i];
176 }
177 }
178 vertices = new_vertices;
179 }
180
181 struct _FaceClassify {
182 struct _Link {
183 int face = -1;
184 int edge = -1;
185 void clear() {
186 face = -1;
187 edge = -1;
188 }
189 };
190 bool valid = false;
191 int group = -1;
192 _Link links[3];
193 Face3 face;
194 };
195
196 /*** GEOMETRY WRAPPER ***/
197
198 enum _CellFlags {
199 _CELL_SOLID = 1,
200 _CELL_EXTERIOR = 2,
201 _CELL_STEP_MASK = 0x1C,
202 _CELL_STEP_NONE = 0 << 2,
203 _CELL_STEP_Y_POS = 1 << 2,
204 _CELL_STEP_Y_NEG = 2 << 2,
205 _CELL_STEP_X_POS = 3 << 2,
206 _CELL_STEP_X_NEG = 4 << 2,
207 _CELL_STEP_Z_POS = 5 << 2,
208 _CELL_STEP_Z_NEG = 6 << 2,
209 _CELL_STEP_DONE = 7 << 2,
210 _CELL_PREV_MASK = 0xE0,
211 _CELL_PREV_NONE = 0 << 5,
212 _CELL_PREV_Y_POS = 1 << 5,
213 _CELL_PREV_Y_NEG = 2 << 5,
214 _CELL_PREV_X_POS = 3 << 5,
215 _CELL_PREV_X_NEG = 4 << 5,
216 _CELL_PREV_Z_POS = 5 << 5,
217 _CELL_PREV_Z_NEG = 6 << 5,
218 _CELL_PREV_FIRST = 7 << 5,
219 };
220
221 static inline void _plot_face(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z, const Vector3 &voxelsize, const Face3 &p_face) {
222 AABB aabb(Vector3(x, y, z), Vector3(len_x, len_y, len_z));
223 aabb.position = aabb.position * voxelsize;
224 aabb.size = aabb.size * voxelsize;
225
226 if (!p_face.intersects_aabb(aabb)) {
227 return;
228 }
229
230 if (len_x == 1 && len_y == 1 && len_z == 1) {
231 p_cell_status[x][y][z] = _CELL_SOLID;
232 return;
233 }
234
235 int div_x = len_x > 1 ? 2 : 1;
236 int div_y = len_y > 1 ? 2 : 1;
237 int div_z = len_z > 1 ? 2 : 1;
238
239 #define SPLIT_DIV(m_i, m_div, m_v, m_len_v, m_new_v, m_new_len_v) \
240 if (m_div == 1) { \
241 m_new_v = m_v; \
242 m_new_len_v = 1; \
243 } else if (m_i == 0) { \
244 m_new_v = m_v; \
245 m_new_len_v = m_len_v / 2; \
246 } else { \
247 m_new_v = m_v + m_len_v / 2; \
248 m_new_len_v = m_len_v - m_len_v / 2; \
249 }
250
251 int new_x;
252 int new_len_x;
253 int new_y;
254 int new_len_y;
255 int new_z;
256 int new_len_z;
257
258 for (int i = 0; i < div_x; i++) {
259 SPLIT_DIV(i, div_x, x, len_x, new_x, new_len_x);
260
261 for (int j = 0; j < div_y; j++) {
262 SPLIT_DIV(j, div_y, y, len_y, new_y, new_len_y);
263
264 for (int k = 0; k < div_z; k++) {
265 SPLIT_DIV(k, div_z, z, len_z, new_z, new_len_z);
266
267 _plot_face(p_cell_status, new_x, new_y, new_z, new_len_x, new_len_y, new_len_z, voxelsize, p_face);
268 }
269 }
270 }
271
272 #undef SPLIT_DIV
273 }
274
275 static inline void _mark_outside(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z) {
276 if (p_cell_status[x][y][z] & 3) {
277 return; // Nothing to do, already used and/or visited.
278 }
279
280 p_cell_status[x][y][z] = _CELL_PREV_FIRST;
281
282 while (true) {
283 uint8_t &c = p_cell_status[x][y][z];
284
285 if ((c & _CELL_STEP_MASK) == _CELL_STEP_NONE) {
286 // Haven't been in here, mark as outside.
287 p_cell_status[x][y][z] |= _CELL_EXTERIOR;
288 }
289
290 if ((c & _CELL_STEP_MASK) != _CELL_STEP_DONE) {
291 // If not done, increase step.
292 c += 1 << 2;
293 }
294
295 if ((c & _CELL_STEP_MASK) == _CELL_STEP_DONE) {
296 // Go back.
297 switch (c & _CELL_PREV_MASK) {
298 case _CELL_PREV_FIRST: {
299 return;
300 } break;
301 case _CELL_PREV_Y_POS: {
302 y++;
303 ERR_FAIL_COND(y >= len_y);
304 } break;
305 case _CELL_PREV_Y_NEG: {
306 y--;
307 ERR_FAIL_COND(y < 0);
308 } break;
309 case _CELL_PREV_X_POS: {
310 x++;
311 ERR_FAIL_COND(x >= len_x);
312 } break;
313 case _CELL_PREV_X_NEG: {
314 x--;
315 ERR_FAIL_COND(x < 0);
316 } break;
317 case _CELL_PREV_Z_POS: {
318 z++;
319 ERR_FAIL_COND(z >= len_z);
320 } break;
321 case _CELL_PREV_Z_NEG: {
322 z--;
323 ERR_FAIL_COND(z < 0);
324 } break;
325 default: {
326 ERR_FAIL();
327 }
328 }
329 continue;
330 }
331
332 int next_x = x, next_y = y, next_z = z;
333 uint8_t prev = 0;
334
335 switch (c & _CELL_STEP_MASK) {
336 case _CELL_STEP_Y_POS: {
337 next_y++;
338 prev = _CELL_PREV_Y_NEG;
339 } break;
340 case _CELL_STEP_Y_NEG: {
341 next_y--;
342 prev = _CELL_PREV_Y_POS;
343 } break;
344 case _CELL_STEP_X_POS: {
345 next_x++;
346 prev = _CELL_PREV_X_NEG;
347 } break;
348 case _CELL_STEP_X_NEG: {
349 next_x--;
350 prev = _CELL_PREV_X_POS;
351 } break;
352 case _CELL_STEP_Z_POS: {
353 next_z++;
354 prev = _CELL_PREV_Z_NEG;
355 } break;
356 case _CELL_STEP_Z_NEG: {
357 next_z--;
358 prev = _CELL_PREV_Z_POS;
359 } break;
360 default:
361 ERR_FAIL();
362 }
363
364 if (next_x < 0 || next_x >= len_x) {
365 continue;
366 }
367 if (next_y < 0 || next_y >= len_y) {
368 continue;
369 }
370 if (next_z < 0 || next_z >= len_z) {
371 continue;
372 }
373
374 if (p_cell_status[next_x][next_y][next_z] & 3) {
375 continue;
376 }
377
378 x = next_x;
379 y = next_y;
380 z = next_z;
381 p_cell_status[x][y][z] |= prev;
382 }
383 }
384
385 static inline void _build_faces(uint8_t ***p_cell_status, int x, int y, int z, int len_x, int len_y, int len_z, Vector<Face3> &p_faces) {
386 ERR_FAIL_INDEX(x, len_x);
387 ERR_FAIL_INDEX(y, len_y);
388 ERR_FAIL_INDEX(z, len_z);
389
390 if (p_cell_status[x][y][z] & _CELL_EXTERIOR) {
391 return;
392 }
393
394 #define vert(m_idx) Vector3(((m_idx) & 4) >> 2, ((m_idx) & 2) >> 1, (m_idx) & 1)
395
396 static const uint8_t indices[6][4] = {
397 { 7, 6, 4, 5 },
398 { 7, 3, 2, 6 },
399 { 7, 5, 1, 3 },
400 { 0, 2, 3, 1 },
401 { 0, 1, 5, 4 },
402 { 0, 4, 6, 2 },
403
404 };
405
406 for (int i = 0; i < 6; i++) {
407 Vector3 face_points[4];
408 int disp_x = x + ((i % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
409 int disp_y = y + (((i - 1) % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
410 int disp_z = z + (((i - 2) % 3) == 0 ? ((i < 3) ? 1 : -1) : 0);
411
412 bool plot = false;
413
414 if (disp_x < 0 || disp_x >= len_x) {
415 plot = true;
416 }
417 if (disp_y < 0 || disp_y >= len_y) {
418 plot = true;
419 }
420 if (disp_z < 0 || disp_z >= len_z) {
421 plot = true;
422 }
423
424 if (!plot && (p_cell_status[disp_x][disp_y][disp_z] & _CELL_EXTERIOR)) {
425 plot = true;
426 }
427
428 if (!plot) {
429 continue;
430 }
431
432 for (int j = 0; j < 4; j++) {
433 face_points[j] = vert(indices[i][j]) + Vector3(x, y, z);
434 }
435
436 p_faces.push_back(
437 Face3(
438 face_points[0],
439 face_points[1],
440 face_points[2]));
441
442 p_faces.push_back(
443 Face3(
444 face_points[2],
445 face_points[3],
446 face_points[0]));
447 }
448 }
449
450 Vector<Face3> Geometry3D::wrap_geometry(const Vector<Face3> &p_array, real_t *p_error) {
451 int face_count = p_array.size();
452 const Face3 *faces = p_array.ptr();
453 constexpr double min_size = 1.0;
454 constexpr int max_length = 20;
455
456 AABB global_aabb;
457
458 for (int i = 0; i < face_count; i++) {
459 if (i == 0) {
460 global_aabb = faces[i].get_aabb();
461 } else {
462 global_aabb.merge_with(faces[i].get_aabb());
463 }
464 }
465
466 global_aabb.grow_by(0.01f); // Avoid numerical error.
467
468 // Determine amount of cells in grid axis.
469 int div_x, div_y, div_z;
470
471 if (global_aabb.size.x / min_size < max_length) {
472 div_x = (int)(global_aabb.size.x / min_size) + 1;
473 } else {
474 div_x = max_length;
475 }
476
477 if (global_aabb.size.y / min_size < max_length) {
478 div_y = (int)(global_aabb.size.y / min_size) + 1;
479 } else {
480 div_y = max_length;
481 }
482
483 if (global_aabb.size.z / min_size < max_length) {
484 div_z = (int)(global_aabb.size.z / min_size) + 1;
485 } else {
486 div_z = max_length;
487 }
488
489 Vector3 voxelsize = global_aabb.size;
490 voxelsize.x /= div_x;
491 voxelsize.y /= div_y;
492 voxelsize.z /= div_z;
493
494 // Create and initialize cells to zero.
495
496 uint8_t ***cell_status = memnew_arr(uint8_t **, div_x);
497 for (int i = 0; i < div_x; i++) {
498 cell_status[i] = memnew_arr(uint8_t *, div_y);
499
500 for (int j = 0; j < div_y; j++) {
501 cell_status[i][j] = memnew_arr(uint8_t, div_z);
502
503 for (int k = 0; k < div_z; k++) {
504 cell_status[i][j][k] = 0;
505 }
506 }
507 }
508
509 // Plot faces into cells.
510
511 for (int i = 0; i < face_count; i++) {
512 Face3 f = faces[i];
513 for (int j = 0; j < 3; j++) {
514 f.vertex[j] -= global_aabb.position;
515 }
516 _plot_face(cell_status, 0, 0, 0, div_x, div_y, div_z, voxelsize, f);
517 }
518
519 // Determine which cells connect to the outside by traversing the outside and recursively flood-fill marking.
520
521 for (int i = 0; i < div_x; i++) {
522 for (int j = 0; j < div_y; j++) {
523 _mark_outside(cell_status, i, j, 0, div_x, div_y, div_z);
524 _mark_outside(cell_status, i, j, div_z - 1, div_x, div_y, div_z);
525 }
526 }
527
528 for (int i = 0; i < div_z; i++) {
529 for (int j = 0; j < div_y; j++) {
530 _mark_outside(cell_status, 0, j, i, div_x, div_y, div_z);
531 _mark_outside(cell_status, div_x - 1, j, i, div_x, div_y, div_z);
532 }
533 }
534
535 for (int i = 0; i < div_x; i++) {
536 for (int j = 0; j < div_z; j++) {
537 _mark_outside(cell_status, i, 0, j, div_x, div_y, div_z);
538 _mark_outside(cell_status, i, div_y - 1, j, div_x, div_y, div_z);
539 }
540 }
541
542 // Build faces for the inside-outside cell divisors.
543
544 Vector<Face3> wrapped_faces;
545
546 for (int i = 0; i < div_x; i++) {
547 for (int j = 0; j < div_y; j++) {
548 for (int k = 0; k < div_z; k++) {
549 _build_faces(cell_status, i, j, k, div_x, div_y, div_z, wrapped_faces);
550 }
551 }
552 }
553
554 // Transform face vertices to global coords.
555
556 int wrapped_faces_count = wrapped_faces.size();
557 Face3 *wrapped_faces_ptr = wrapped_faces.ptrw();
558
559 for (int i = 0; i < wrapped_faces_count; i++) {
560 for (int j = 0; j < 3; j++) {
561 Vector3 &v = wrapped_faces_ptr[i].vertex[j];
562 v = v * voxelsize;
563 v += global_aabb.position;
564 }
565 }
566
567 // clean up grid
568
569 for (int i = 0; i < div_x; i++) {
570 for (int j = 0; j < div_y; j++) {
571 memdelete_arr(cell_status[i][j]);
572 }
573
574 memdelete_arr(cell_status[i]);
575 }
576
577 memdelete_arr(cell_status);
578 if (p_error) {
579 *p_error = voxelsize.length();
580 }
581
582 return wrapped_faces;
583 }
584
585 Geometry3D::MeshData Geometry3D::build_convex_mesh(const Vector<Plane> &p_planes) {
586 MeshData mesh;
587
588 #define SUBPLANE_SIZE 1024.0
589
590 real_t subplane_size = 1024.0; // Should compute this from the actual plane.
591 for (int i = 0; i < p_planes.size(); i++) {
592 Plane p = p_planes[i];
593
594 Vector3 ref = Vector3(0.0, 1.0, 0.0);
595
596 if (Math::abs(p.normal.dot(ref)) > 0.95f) {
597 ref = Vector3(0.0, 0.0, 1.0); // Change axis.
598 }
599
600 Vector3 right = p.normal.cross(ref).normalized();
601 Vector3 up = p.normal.cross(right).normalized();
602
603 Vector3 center = p.get_center();
604
605 // make a quad clockwise
606 LocalVector<Vector3> vertices = {
607 center - up * subplane_size + right * subplane_size,
608 center - up * subplane_size - right * subplane_size,
609 center + up * subplane_size - right * subplane_size,
610 center + up * subplane_size + right * subplane_size
611 };
612
613 for (int j = 0; j < p_planes.size(); j++) {
614 if (j == i) {
615 continue;
616 }
617
618 LocalVector<Vector3> new_vertices;
619 Plane clip = p_planes[j];
620
621 if (clip.normal.dot(p.normal) > 0.95f) {
622 continue;
623 }
624
625 if (vertices.size() < 3) {
626 break;
627 }
628
629 for (uint32_t k = 0; k < vertices.size(); k++) {
630 int k_n = (k + 1) % vertices.size();
631
632 Vector3 edge0_A = vertices[k];
633 Vector3 edge1_A = vertices[k_n];
634
635 real_t dist0 = clip.distance_to(edge0_A);
636 real_t dist1 = clip.distance_to(edge1_A);
637
638 if (dist0 <= 0) { // Behind plane.
639
640 new_vertices.push_back(vertices[k]);
641 }
642
643 // Check for different sides and non coplanar.
644 if ((dist0 * dist1) < 0) {
645 // Calculate intersection.
646 Vector3 rel = edge1_A - edge0_A;
647
648 real_t den = clip.normal.dot(rel);
649 if (Math::is_zero_approx(den)) {
650 continue; // Point too short.
651 }
652
653 real_t dist = -(clip.normal.dot(edge0_A) - clip.d) / den;
654 Vector3 inters = edge0_A + rel * dist;
655 new_vertices.push_back(inters);
656 }
657 }
658
659 vertices = new_vertices;
660 }
661
662 if (vertices.size() < 3) {
663 continue;
664 }
665
666 // Result is a clockwise face.
667
668 MeshData::Face face;
669
670 // Add face indices.
671 for (const Vector3 &vertex : vertices) {
672 int idx = -1;
673 for (uint32_t k = 0; k < mesh.vertices.size(); k++) {
674 if (mesh.vertices[k].distance_to(vertex) < 0.001f) {
675 idx = k;
676 break;
677 }
678 }
679
680 if (idx == -1) {
681 idx = mesh.vertices.size();
682 mesh.vertices.push_back(vertex);
683 }
684
685 face.indices.push_back(idx);
686 }
687 face.plane = p;
688 mesh.faces.push_back(face);
689
690 // Add edge.
691
692 for (uint32_t j = 0; j < face.indices.size(); j++) {
693 int a = face.indices[j];
694 int b = face.indices[(j + 1) % face.indices.size()];
695
696 bool found = false;
697 int found_idx = -1;
698 for (uint32_t k = 0; k < mesh.edges.size(); k++) {
699 if (mesh.edges[k].vertex_a == a && mesh.edges[k].vertex_b == b) {
700 found = true;
701 found_idx = k;
702 break;
703 }
704 if (mesh.edges[k].vertex_b == a && mesh.edges[k].vertex_a == b) {
705 found = true;
706 found_idx = k;
707 break;
708 }
709 }
710
711 if (found) {
712 mesh.edges[found_idx].face_b = j;
713 continue;
714 }
715 MeshData::Edge edge;
716 edge.vertex_a = a;
717 edge.vertex_b = b;
718 edge.face_a = j;
719 edge.face_b = -1;
720 mesh.edges.push_back(edge);
721 }
722 }
723
724 return mesh;
725 }
726
727 Vector<Plane> Geometry3D::build_box_planes(const Vector3 &p_extents) {
728 Vector<Plane> planes = {
729 Plane(Vector3(1, 0, 0), p_extents.x),
730 Plane(Vector3(-1, 0, 0), p_extents.x),
731 Plane(Vector3(0, 1, 0), p_extents.y),
732 Plane(Vector3(0, -1, 0), p_extents.y),
733 Plane(Vector3(0, 0, 1), p_extents.z),
734 Plane(Vector3(0, 0, -1), p_extents.z)
735 };
736
737 return planes;
738 }
739
740 Vector<Plane> Geometry3D::build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis) {
741 ERR_FAIL_INDEX_V(p_axis, 3, Vector<Plane>());
742
743 Vector<Plane> planes;
744
745 const double sides_step = Math::TAU / p_sides;
746 for (int i = 0; i < p_sides; i++) {
747 Vector3 normal;
748 normal[(p_axis + 1) % 3] = Math::cos(i * sides_step);
749 normal[(p_axis + 2) % 3] = Math::sin(i * sides_step);
750
751 planes.push_back(Plane(normal, p_radius));
752 }
753
754 Vector3 axis;
755 axis[p_axis] = 1.0;
756
757 planes.push_back(Plane(axis, p_height * 0.5f));
758 planes.push_back(Plane(-axis, p_height * 0.5f));
759
760 return planes;
761 }
762
763 Vector<Plane> Geometry3D::build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis) {
764 ERR_FAIL_INDEX_V(p_axis, 3, Vector<Plane>());
765
766 Vector<Plane> planes;
767
768 Vector3 axis;
769 axis[p_axis] = 1.0;
770
771 Vector3 axis_neg;
772 axis_neg[(p_axis + 1) % 3] = 1.0;
773 axis_neg[(p_axis + 2) % 3] = 1.0;
774 axis_neg[p_axis] = -1.0;
775
776 const double lon_step = Math::TAU / p_lons;
777 for (int i = 0; i < p_lons; i++) {
778 Vector3 normal;
779 normal[(p_axis + 1) % 3] = Math::cos(i * lon_step);
780 normal[(p_axis + 2) % 3] = Math::sin(i * lon_step);
781
782 planes.push_back(Plane(normal, p_radius));
783
784 for (int j = 1; j <= p_lats; j++) {
785 Vector3 plane_normal = normal.lerp(axis, j / (real_t)p_lats).normalized();
786 planes.push_back(Plane(plane_normal, p_radius));
787 planes.push_back(Plane(plane_normal * axis_neg, p_radius));
788 }
789 }
790
791 return planes;
792 }
793
794 Vector<Plane> Geometry3D::build_capsule_planes(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
795 ERR_FAIL_INDEX_V(p_axis, 3, Vector<Plane>());
796
797 Vector<Plane> planes;
798
799 Vector3 axis;
800 axis[p_axis] = 1.0;
801
802 Vector3 axis_neg;
803 axis_neg[(p_axis + 1) % 3] = 1.0;
804 axis_neg[(p_axis + 2) % 3] = 1.0;
805 axis_neg[p_axis] = -1.0;
806
807 const double sides_step = Math::TAU / p_sides;
808 for (int i = 0; i < p_sides; i++) {
809 Vector3 normal;
810 normal[(p_axis + 1) % 3] = Math::cos(i * sides_step);
811 normal[(p_axis + 2) % 3] = Math::sin(i * sides_step);
812
813 planes.push_back(Plane(normal, p_radius));
814
815 for (int j = 1; j <= p_lats; j++) {
816 Vector3 plane_normal = normal.lerp(axis, j / (real_t)p_lats).normalized();
817 Vector3 position = axis * p_height * 0.5f + plane_normal * p_radius;
818 planes.push_back(Plane(plane_normal, position));
819 planes.push_back(Plane(plane_normal * axis_neg, position * axis_neg));
820 }
821 }
822
823 return planes;
824 }
825
826 Vector<Vector3> Geometry3D::compute_convex_mesh_points(const Plane *p_planes, int p_plane_count) {
827 Vector<Vector3> points;
828
829 // Iterate through every unique combination of any three planes.
830 for (int i = p_plane_count - 1; i >= 0; i--) {
831 for (int j = i - 1; j >= 0; j--) {
832 for (int k = j - 1; k >= 0; k--) {
833 // Find the point where these planes all cross over (if they
834 // do at all).
835 Vector3 convex_shape_point;
836 if (p_planes[i].intersect_3(p_planes[j], p_planes[k], &convex_shape_point)) {
837 // See if any *other* plane excludes this point because it's
838 // on the wrong side.
839 bool excluded = false;
840 for (int n = 0; n < p_plane_count; n++) {
841 if (n != i && n != j && n != k) {
842 real_t dp = p_planes[n].normal.dot(convex_shape_point);
843 if (dp - p_planes[n].d > (real_t)CMP_EPSILON) {
844 excluded = true;
845 break;
846 }
847 }
848 }
849
850 // Only add the point if it passed all tests.
851 if (!excluded) {
852 points.push_back(convex_shape_point);
853 }
854 }
855 }
856 }
857 }
858
859 return points;
860 }
861
862 #define square(m_s) ((m_s) * (m_s))
863 #define BIG_VAL 1e20
864
865 /* dt of 1d function using squared distance */
866 static void edt(float *f, int stride, int n) {
867 float *d = (float *)alloca(sizeof(float) * n + sizeof(int) * n + sizeof(float) * (n + 1));
868 int *v = reinterpret_cast<int *>(&(d[n]));
869 float *z = reinterpret_cast<float *>(&v[n]);
870
871 int k = 0;
872 v[0] = 0;
873 z[0] = -BIG_VAL;
874 z[1] = +BIG_VAL;
875 for (int q = 1; q <= n - 1; q++) {
876 float s = ((f[q * stride] + square(q)) - (f[v[k] * stride] + square(v[k]))) / (2 * q - 2 * v[k]);
877 while (s <= z[k]) {
878 k--;
879 s = ((f[q * stride] + square(q)) - (f[v[k] * stride] + square(v[k]))) / (2 * q - 2 * v[k]);
880 }
881 k++;
882 v[k] = q;
883
884 z[k] = s;
885 z[k + 1] = +BIG_VAL;
886 }
887
888 k = 0;
889 for (int q = 0; q <= n - 1; q++) {
890 while (z[k + 1] < q) {
891 k++;
892 }
893 d[q] = square(q - v[k]) + f[v[k] * stride];
894 }
895
896 for (int i = 0; i < n; i++) {
897 f[i * stride] = d[i];
898 }
899 }
900
901 #undef square
902
903 Vector<uint32_t> Geometry3D::generate_edf(const Vector<bool> &p_voxels, const Vector3i &p_size, bool p_negative) {
904 uint32_t float_count = p_size.x * p_size.y * p_size.z;
905
906 ERR_FAIL_COND_V((uint32_t)p_voxels.size() != float_count, Vector<uint32_t>());
907
908 float *work_memory = memnew_arr(float, float_count);
909 for (uint32_t i = 0; i < float_count; i++) {
910 work_memory[i] = BIG_VAL;
911 }
912
913 uint32_t y_mult = p_size.x;
914 uint32_t z_mult = y_mult * p_size.y;
915
916 //plot solid cells
917 {
918 const bool *voxr = p_voxels.ptr();
919 for (uint32_t i = 0; i < float_count; i++) {
920 bool plot = voxr[i];
921 if (p_negative) {
922 plot = !plot;
923 }
924 if (plot) {
925 work_memory[i] = 0;
926 }
927 }
928 }
929
930 //process in each direction
931
932 //xy->z
933
934 for (int i = 0; i < p_size.x; i++) {
935 for (int j = 0; j < p_size.y; j++) {
936 edt(&work_memory[i + j * y_mult], z_mult, p_size.z);
937 }
938 }
939
940 //xz->y
941
942 for (int i = 0; i < p_size.x; i++) {
943 for (int j = 0; j < p_size.z; j++) {
944 edt(&work_memory[i + j * z_mult], y_mult, p_size.y);
945 }
946 }
947
948 //yz->x
949 for (int i = 0; i < p_size.y; i++) {
950 for (int j = 0; j < p_size.z; j++) {
951 edt(&work_memory[i * y_mult + j * z_mult], 1, p_size.x);
952 }
953 }
954
955 Vector<uint32_t> ret;
956 ret.resize(float_count);
957 {
958 uint32_t *w = ret.ptrw();
959 for (uint32_t i = 0; i < float_count; i++) {
960 w[i] = uint32_t(Math::sqrt(work_memory[i]));
961 }
962 }
963
964 memdelete_arr(work_memory);
965
966 return ret;
967 }
968
969 #undef BIG_VAL
970
971 Vector<int8_t> Geometry3D::generate_sdf8(const Vector<uint32_t> &p_positive, const Vector<uint32_t> &p_negative) {
972 ERR_FAIL_COND_V(p_positive.size() != p_negative.size(), Vector<int8_t>());
973 Vector<int8_t> sdf8;
974 int s = p_positive.size();
975 sdf8.resize(s);
976
977 const uint32_t *rpos = p_positive.ptr();
978 const uint32_t *rneg = p_negative.ptr();
979 int8_t *wsdf = sdf8.ptrw();
980 for (int i = 0; i < s; i++) {
981 int32_t diff = int32_t(rpos[i]) - int32_t(rneg[i]);
982 wsdf[i] = CLAMP(diff, -128, 127);
983 }
984 return sdf8;
985 }