Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/math/geometry_3d.h
9898 views
1
/**************************************************************************/
2
/* geometry_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/delaunay_3d.h"
34
#include "core/math/face3.h"
35
#include "core/templates/local_vector.h"
36
#include "core/templates/vector.h"
37
38
class Geometry3D {
39
public:
40
static void 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);
41
static real_t get_closest_distance_between_segments(const Vector3 &p_p0, const Vector3 &p_p1, const Vector3 &p_q0, const Vector3 &p_q1);
42
43
static inline bool ray_intersects_triangle(const Vector3 &p_from, const Vector3 &p_dir, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2, Vector3 *r_res = nullptr) {
44
Vector3 e1 = p_v1 - p_v0;
45
Vector3 e2 = p_v2 - p_v0;
46
Vector3 h = p_dir.cross(e2);
47
real_t a = e1.dot(h);
48
if (Math::is_zero_approx(a)) { // Parallel test.
49
return false;
50
}
51
52
real_t f = 1.0f / a;
53
54
Vector3 s = p_from - p_v0;
55
real_t u = f * s.dot(h);
56
57
if ((u < 0.0f) || (u > 1.0f)) {
58
return false;
59
}
60
61
Vector3 q = s.cross(e1);
62
63
real_t v = f * p_dir.dot(q);
64
65
if ((v < 0.0f) || (u + v > 1.0f)) {
66
return false;
67
}
68
69
// At this stage we can compute t to find out where
70
// the intersection point is on the line.
71
real_t t = f * e2.dot(q);
72
73
if (t > 0.00001f) { // ray intersection
74
if (r_res) {
75
*r_res = p_from + p_dir * t;
76
}
77
return true;
78
} else { // This means that there is a line intersection but not a ray intersection.
79
return false;
80
}
81
}
82
83
static inline bool segment_intersects_triangle(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2, Vector3 *r_res = nullptr) {
84
Vector3 rel = p_to - p_from;
85
Vector3 e1 = p_v1 - p_v0;
86
Vector3 e2 = p_v2 - p_v0;
87
Vector3 h = rel.cross(e2);
88
real_t a = e1.dot(h);
89
if (Math::is_zero_approx(a)) { // Parallel test.
90
return false;
91
}
92
93
real_t f = 1.0f / a;
94
95
Vector3 s = p_from - p_v0;
96
real_t u = f * s.dot(h);
97
98
if ((u < 0.0f) || (u > 1.0f)) {
99
return false;
100
}
101
102
Vector3 q = s.cross(e1);
103
104
real_t v = f * rel.dot(q);
105
106
if ((v < 0.0f) || (u + v > 1.0f)) {
107
return false;
108
}
109
110
// At this stage we can compute t to find out where
111
// the intersection point is on the line.
112
real_t t = f * e2.dot(q);
113
114
if (t > (real_t)CMP_EPSILON && t <= 1.0f) { // Ray intersection.
115
if (r_res) {
116
*r_res = p_from + rel * t;
117
}
118
return true;
119
} else { // This means that there is a line intersection but not a ray intersection.
120
return false;
121
}
122
}
123
124
static inline bool segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius, Vector3 *r_res = nullptr, Vector3 *r_norm = nullptr) {
125
Vector3 sphere_pos = p_sphere_pos - p_from;
126
Vector3 rel = (p_to - p_from);
127
real_t rel_l = rel.length();
128
if (rel_l < (real_t)CMP_EPSILON) {
129
return false; // Both points are the same.
130
}
131
Vector3 normal = rel / rel_l;
132
133
real_t sphere_d = normal.dot(sphere_pos);
134
135
real_t ray_distance = sphere_pos.distance_to(normal * sphere_d);
136
137
if (ray_distance >= p_sphere_radius) {
138
return false;
139
}
140
141
real_t inters_d2 = p_sphere_radius * p_sphere_radius - ray_distance * ray_distance;
142
real_t inters_d = sphere_d;
143
144
if (inters_d2 >= (real_t)CMP_EPSILON) {
145
inters_d -= Math::sqrt(inters_d2);
146
}
147
148
// Check in segment.
149
if (inters_d < 0 || inters_d > rel_l) {
150
return false;
151
}
152
153
Vector3 result = p_from + normal * inters_d;
154
155
if (r_res) {
156
*r_res = result;
157
}
158
if (r_norm) {
159
*r_norm = (result - p_sphere_pos).normalized();
160
}
161
162
return true;
163
}
164
165
static inline bool segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, real_t p_height, real_t p_radius, Vector3 *r_res = nullptr, Vector3 *r_norm = nullptr, int p_cylinder_axis = 2) {
166
Vector3 rel = (p_to - p_from);
167
real_t rel_l = rel.length();
168
if (rel_l < (real_t)CMP_EPSILON) {
169
return false; // Both points are the same.
170
}
171
172
ERR_FAIL_COND_V(p_cylinder_axis < 0, false);
173
ERR_FAIL_COND_V(p_cylinder_axis > 2, false);
174
Vector3 cylinder_axis;
175
cylinder_axis[p_cylinder_axis] = 1.0f;
176
177
// First check if they are parallel.
178
Vector3 normal = (rel / rel_l);
179
Vector3 crs = normal.cross(cylinder_axis);
180
real_t crs_l = crs.length();
181
182
Vector3 axis_dir;
183
184
if (crs_l < (real_t)CMP_EPSILON) {
185
Vector3 side_axis;
186
side_axis[(p_cylinder_axis + 1) % 3] = 1.0f; // Any side axis OK.
187
axis_dir = side_axis;
188
} else {
189
axis_dir = crs / crs_l;
190
}
191
192
real_t dist = axis_dir.dot(p_from);
193
194
if (dist >= p_radius) {
195
return false; // Too far away.
196
}
197
198
// Convert to 2D.
199
real_t w2 = p_radius * p_radius - dist * dist;
200
if (w2 < (real_t)CMP_EPSILON) {
201
return false; // Avoid numerical error.
202
}
203
Size2 size(Math::sqrt(w2), p_height * 0.5f);
204
205
Vector3 side_dir = axis_dir.cross(cylinder_axis).normalized();
206
207
Vector2 from2D(side_dir.dot(p_from), p_from[p_cylinder_axis]);
208
Vector2 to2D(side_dir.dot(p_to), p_to[p_cylinder_axis]);
209
210
real_t min = 0, max = 1;
211
212
int axis = -1;
213
214
for (int i = 0; i < 2; i++) {
215
real_t seg_from = from2D[i];
216
real_t seg_to = to2D[i];
217
real_t box_begin = -size[i];
218
real_t box_end = size[i];
219
real_t cmin, cmax;
220
221
if (seg_from < seg_to) {
222
if (seg_from > box_end || seg_to < box_begin) {
223
return false;
224
}
225
real_t length = seg_to - seg_from;
226
cmin = (seg_from < box_begin) ? ((box_begin - seg_from) / length) : 0;
227
cmax = (seg_to > box_end) ? ((box_end - seg_from) / length) : 1;
228
229
} else {
230
if (seg_to > box_end || seg_from < box_begin) {
231
return false;
232
}
233
real_t length = seg_to - seg_from;
234
cmin = (seg_from > box_end) ? (box_end - seg_from) / length : 0;
235
cmax = (seg_to < box_begin) ? (box_begin - seg_from) / length : 1;
236
}
237
238
if (cmin > min) {
239
min = cmin;
240
axis = i;
241
}
242
if (cmax < max) {
243
max = cmax;
244
}
245
if (max < min) {
246
return false;
247
}
248
}
249
250
// Convert to 3D again.
251
Vector3 result = p_from + (rel * min);
252
Vector3 res_normal = result;
253
254
if (axis == 0) {
255
res_normal[p_cylinder_axis] = 0;
256
} else {
257
int axis_side = (p_cylinder_axis + 1) % 3;
258
res_normal[axis_side] = 0;
259
axis_side = (axis_side + 1) % 3;
260
res_normal[axis_side] = 0;
261
}
262
263
res_normal.normalize();
264
265
if (r_res) {
266
*r_res = result;
267
}
268
if (r_norm) {
269
*r_norm = res_normal;
270
}
271
272
return true;
273
}
274
275
static bool segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Plane *p_planes, int p_plane_count, Vector3 *r_res, Vector3 *r_norm) {
276
real_t min = -1e20, max = 1e20;
277
278
Vector3 rel = p_to - p_from;
279
real_t rel_l = rel.length();
280
281
if (rel_l < (real_t)CMP_EPSILON) {
282
return false;
283
}
284
285
Vector3 dir = rel / rel_l;
286
287
int min_index = -1;
288
289
for (int i = 0; i < p_plane_count; i++) {
290
const Plane &p = p_planes[i];
291
292
real_t den = p.normal.dot(dir);
293
294
if (Math::abs(den) <= (real_t)CMP_EPSILON) {
295
continue; // Ignore parallel plane.
296
}
297
298
real_t dist = -p.distance_to(p_from) / den;
299
300
if (den > 0) {
301
// Backwards facing plane.
302
if (dist < max) {
303
max = dist;
304
}
305
} else {
306
// Front facing plane.
307
if (dist > min) {
308
min = dist;
309
min_index = i;
310
}
311
}
312
}
313
314
if (max <= min || min < 0 || min > rel_l || min_index == -1) { // Exit conditions.
315
return false; // No intersection.
316
}
317
318
if (r_res) {
319
*r_res = p_from + dir * min;
320
}
321
if (r_norm) {
322
*r_norm = p_planes[min_index].normal;
323
}
324
325
return true;
326
}
327
328
#ifndef DISABLE_DEPRECATED
329
static Vector3 get_closest_point_to_segment(const Vector3 &p_point, const Vector3 *p_segment) {
330
return get_closest_point_to_segment(p_point, p_segment[0], p_segment[1]);
331
}
332
#endif // DISABLE_DEPRECATED
333
334
static Vector3 get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_segment_a, const Vector3 &p_segment_b) {
335
Vector3 p = p_point - p_segment_a;
336
Vector3 n = p_segment_b - p_segment_a;
337
real_t l2 = n.length_squared();
338
if (l2 < 1e-20f) {
339
return p_segment_a; // Both points are the same, just give any.
340
}
341
342
real_t d = n.dot(p) / l2;
343
344
if (d <= 0.0f) {
345
return p_segment_a; // Before first point.
346
} else if (d >= 1.0f) {
347
return p_segment_b; // After first point.
348
} else {
349
return p_segment_a + n * d; // Inside.
350
}
351
}
352
353
#ifndef DISABLE_DEPRECATED
354
static Vector3 get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 *p_segment) {
355
return get_closest_point_to_segment_uncapped(p_point, p_segment[0], p_segment[1]);
356
}
357
#endif // DISABLE_DEPRECATED
358
359
static Vector3 get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_segment_a, const Vector3 &p_segment_b) {
360
Vector3 p = p_point - p_segment_a;
361
Vector3 n = p_segment_b - p_segment_a;
362
real_t l2 = n.length_squared();
363
if (l2 < 1e-20f) {
364
return p_segment_a; // Both points are the same, just give any.
365
}
366
367
real_t d = n.dot(p) / l2;
368
369
return p_segment_a + n * d; // Inside.
370
}
371
372
static inline bool point_in_projected_triangle(const Vector3 &p_point, const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) {
373
Vector3 face_n = (p_v1 - p_v3).cross(p_v1 - p_v2);
374
375
Vector3 n1 = (p_point - p_v3).cross(p_point - p_v2);
376
377
if (face_n.dot(n1) < 0) {
378
return false;
379
}
380
381
Vector3 n2 = (p_v1 - p_v3).cross(p_v1 - p_point);
382
383
if (face_n.dot(n2) < 0) {
384
return false;
385
}
386
387
Vector3 n3 = (p_v1 - p_point).cross(p_v1 - p_v2);
388
389
if (face_n.dot(n3) < 0) {
390
return false;
391
}
392
393
return true;
394
}
395
396
#ifndef DISABLE_DEPRECATED
397
static inline bool triangle_sphere_intersection_test(const Vector3 *p_triangle, const Vector3 &p_normal, const Vector3 &p_sphere_pos, real_t p_sphere_radius, Vector3 &r_triangle_contact, Vector3 &r_sphere_contact) {
398
return triangle_sphere_intersection_test(p_triangle[0], p_triangle[1], p_triangle[2], p_normal, p_sphere_pos, p_sphere_radius, r_triangle_contact, r_sphere_contact);
399
}
400
#endif // DISABLE_DEPRECATED
401
402
static inline bool triangle_sphere_intersection_test(const Vector3 &p_triangle_a, const Vector3 &p_triangle_b, const Vector3 &p_triangle_c, const Vector3 &p_normal, const Vector3 &p_sphere_pos, real_t p_sphere_radius, Vector3 &r_triangle_contact, Vector3 &r_sphere_contact) {
403
real_t d = p_normal.dot(p_sphere_pos) - p_normal.dot(p_triangle_a);
404
405
if (d > p_sphere_radius || d < -p_sphere_radius) {
406
// Not touching the plane of the face, return.
407
return false;
408
}
409
410
Vector3 contact = p_sphere_pos - (p_normal * d);
411
412
/** 2nd) TEST INSIDE TRIANGLE **/
413
414
if (Geometry3D::point_in_projected_triangle(contact, p_triangle_a, p_triangle_b, p_triangle_c)) {
415
r_triangle_contact = contact;
416
r_sphere_contact = p_sphere_pos - p_normal * p_sphere_radius;
417
//printf("solved inside triangle\n");
418
return true;
419
}
420
421
/** 3rd TEST INSIDE EDGE CYLINDERS **/
422
423
const Vector3 verts[4] = { p_triangle_a, p_triangle_b, p_triangle_c, p_triangle_a }; // for() friendly
424
425
for (int i = 0; i < 3; i++) {
426
// Check edge cylinder.
427
428
Vector3 n1 = verts[i] - verts[i + 1];
429
Vector3 n2 = p_sphere_pos - verts[i + 1];
430
431
///@TODO Maybe discard by range here to make the algorithm quicker.
432
433
// Check point within cylinder radius.
434
Vector3 axis = n1.cross(n2).cross(n1);
435
axis.normalize();
436
437
real_t ad = axis.dot(n2);
438
439
if (Math::abs(ad) > p_sphere_radius) {
440
// No chance with this edge, too far away.
441
continue;
442
}
443
444
// Check point within edge capsule cylinder.
445
/** 4th TEST INSIDE EDGE POINTS **/
446
447
real_t sphere_at = n1.dot(n2);
448
449
if (sphere_at >= 0 && sphere_at < n1.dot(n1)) {
450
r_triangle_contact = p_sphere_pos - axis * (axis.dot(n2));
451
r_sphere_contact = p_sphere_pos - axis * p_sphere_radius;
452
// Point inside here.
453
return true;
454
}
455
456
real_t r2 = p_sphere_radius * p_sphere_radius;
457
458
if (n2.length_squared() < r2) {
459
Vector3 n = (p_sphere_pos - verts[i + 1]).normalized();
460
461
r_triangle_contact = verts[i + 1];
462
r_sphere_contact = p_sphere_pos - n * p_sphere_radius;
463
return true;
464
}
465
466
if (n2.distance_squared_to(n1) < r2) {
467
Vector3 n = (p_sphere_pos - verts[i]).normalized();
468
469
r_triangle_contact = verts[i];
470
r_sphere_contact = p_sphere_pos - n * p_sphere_radius;
471
return true;
472
}
473
474
break; // It's pointless to continue at this point, so save some CPU cycles.
475
}
476
477
return false;
478
}
479
480
static inline Vector<Vector3> clip_polygon(const Vector<Vector3> &polygon, const Plane &p_plane) {
481
enum LocationCache {
482
LOC_INSIDE = 1,
483
LOC_BOUNDARY = 0,
484
LOC_OUTSIDE = -1
485
};
486
487
if (polygon.is_empty()) {
488
return polygon;
489
}
490
491
int *location_cache = (int *)alloca(sizeof(int) * polygon.size());
492
int inside_count = 0;
493
int outside_count = 0;
494
495
for (int a = 0; a < polygon.size(); a++) {
496
real_t dist = p_plane.distance_to(polygon[a]);
497
if (dist < (real_t)-CMP_POINT_IN_PLANE_EPSILON) {
498
location_cache[a] = LOC_INSIDE;
499
inside_count++;
500
} else {
501
if (dist > (real_t)CMP_POINT_IN_PLANE_EPSILON) {
502
location_cache[a] = LOC_OUTSIDE;
503
outside_count++;
504
} else {
505
location_cache[a] = LOC_BOUNDARY;
506
}
507
}
508
}
509
510
if (outside_count == 0) {
511
return polygon; // No changes.
512
} else if (inside_count == 0) {
513
return Vector<Vector3>(); // Empty.
514
}
515
516
long previous = polygon.size() - 1;
517
Vector<Vector3> clipped;
518
519
for (int index = 0; index < polygon.size(); index++) {
520
int loc = location_cache[index];
521
if (loc == LOC_OUTSIDE) {
522
if (location_cache[previous] == LOC_INSIDE) {
523
const Vector3 &v1 = polygon[previous];
524
const Vector3 &v2 = polygon[index];
525
526
Vector3 segment = v1 - v2;
527
real_t den = p_plane.normal.dot(segment);
528
real_t dist = p_plane.distance_to(v1) / den;
529
dist = -dist;
530
clipped.push_back(v1 + segment * dist);
531
}
532
} else {
533
const Vector3 &v1 = polygon[index];
534
if ((loc == LOC_INSIDE) && (location_cache[previous] == LOC_OUTSIDE)) {
535
const Vector3 &v2 = polygon[previous];
536
Vector3 segment = v1 - v2;
537
real_t den = p_plane.normal.dot(segment);
538
real_t dist = p_plane.distance_to(v1) / den;
539
dist = -dist;
540
clipped.push_back(v1 + segment * dist);
541
}
542
543
clipped.push_back(v1);
544
}
545
546
previous = index;
547
}
548
549
return clipped;
550
}
551
552
static Vector<int32_t> tetrahedralize_delaunay(const Vector<Vector3> &p_points) {
553
Vector<Delaunay3D::OutputSimplex> tetr = Delaunay3D::tetrahedralize(p_points);
554
Vector<int32_t> tetrahedrons;
555
556
tetrahedrons.resize(4 * tetr.size());
557
int32_t *ptr = tetrahedrons.ptrw();
558
for (int i = 0; i < tetr.size(); i++) {
559
*ptr++ = tetr[i].points[0];
560
*ptr++ = tetr[i].points[1];
561
*ptr++ = tetr[i].points[2];
562
*ptr++ = tetr[i].points[3];
563
}
564
return tetrahedrons;
565
}
566
567
// Create a "wrap" that encloses the given geometry.
568
static Vector<Face3> wrap_geometry(const Vector<Face3> &p_array, real_t *p_error = nullptr);
569
570
struct MeshData {
571
struct Face {
572
Plane plane;
573
LocalVector<int> indices;
574
};
575
576
LocalVector<Face> faces;
577
578
struct Edge {
579
int vertex_a, vertex_b;
580
int face_a, face_b;
581
};
582
583
LocalVector<Edge> edges;
584
585
LocalVector<Vector3> vertices;
586
587
void optimize_vertices();
588
};
589
590
static MeshData build_convex_mesh(const Vector<Plane> &p_planes);
591
static Vector<Plane> build_sphere_planes(real_t p_radius, int p_lats, int p_lons, Vector3::Axis p_axis = Vector3::AXIS_Z);
592
static Vector<Plane> build_box_planes(const Vector3 &p_extents);
593
static Vector<Plane> build_cylinder_planes(real_t p_radius, real_t p_height, int p_sides, Vector3::Axis p_axis = Vector3::AXIS_Z);
594
static Vector<Plane> build_capsule_planes(real_t p_radius, real_t p_height, int p_sides, int p_lats, Vector3::Axis p_axis = Vector3::AXIS_Z);
595
596
static Vector<Vector3> compute_convex_mesh_points(const Plane *p_planes, int p_plane_count);
597
598
#define FINDMINMAX(x0, x1, x2, min, max) \
599
min = max = x0; \
600
if (x1 < min) { \
601
min = x1; \
602
} \
603
if (x1 > max) { \
604
max = x1; \
605
} \
606
if (x2 < min) { \
607
min = x2; \
608
} \
609
if (x2 > max) { \
610
max = x2; \
611
}
612
613
_FORCE_INLINE_ static bool planeBoxOverlap(Vector3 normal, real_t d, Vector3 maxbox) {
614
int q;
615
Vector3 vmin, vmax;
616
for (q = 0; q <= 2; q++) {
617
if (normal[q] > 0.0f) {
618
vmin[q] = -maxbox[q];
619
vmax[q] = maxbox[q];
620
} else {
621
vmin[q] = maxbox[q];
622
vmax[q] = -maxbox[q];
623
}
624
}
625
if (normal.dot(vmin) + d > 0.0f) {
626
return false;
627
}
628
if (normal.dot(vmax) + d >= 0.0f) {
629
return true;
630
}
631
632
return false;
633
}
634
635
/*======================== X-tests ========================*/
636
#define AXISTEST_X01(a, b, fa, fb) \
637
p0 = a * v0.y - b * v0.z; \
638
p2 = a * v2.y - b * v2.z; \
639
if (p0 < p2) { \
640
min = p0; \
641
max = p2; \
642
} else { \
643
min = p2; \
644
max = p0; \
645
} \
646
rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
647
if (min > rad || max < -rad) { \
648
return false; \
649
}
650
651
#define AXISTEST_X2(a, b, fa, fb) \
652
p0 = a * v0.y - b * v0.z; \
653
p1 = a * v1.y - b * v1.z; \
654
if (p0 < p1) { \
655
min = p0; \
656
max = p1; \
657
} else { \
658
min = p1; \
659
max = p0; \
660
} \
661
rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
662
if (min > rad || max < -rad) { \
663
return false; \
664
}
665
666
/*======================== Y-tests ========================*/
667
#define AXISTEST_Y02(a, b, fa, fb) \
668
p0 = -a * v0.x + b * v0.z; \
669
p2 = -a * v2.x + b * v2.z; \
670
if (p0 < p2) { \
671
min = p0; \
672
max = p2; \
673
} else { \
674
min = p2; \
675
max = p0; \
676
} \
677
rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
678
if (min > rad || max < -rad) { \
679
return false; \
680
}
681
682
#define AXISTEST_Y1(a, b, fa, fb) \
683
p0 = -a * v0.x + b * v0.z; \
684
p1 = -a * v1.x + b * v1.z; \
685
if (p0 < p1) { \
686
min = p0; \
687
max = p1; \
688
} else { \
689
min = p1; \
690
max = p0; \
691
} \
692
rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
693
if (min > rad || max < -rad) { \
694
return false; \
695
}
696
697
/*======================== Z-tests ========================*/
698
#define AXISTEST_Z12(a, b, fa, fb) \
699
p1 = a * v1.x - b * v1.y; \
700
p2 = a * v2.x - b * v2.y; \
701
if (p2 < p1) { \
702
min = p2; \
703
max = p1; \
704
} else { \
705
min = p1; \
706
max = p2; \
707
} \
708
rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
709
if (min > rad || max < -rad) { \
710
return false; \
711
}
712
713
#define AXISTEST_Z0(a, b, fa, fb) \
714
p0 = a * v0.x - b * v0.y; \
715
p1 = a * v1.x - b * v1.y; \
716
if (p0 < p1) { \
717
min = p0; \
718
max = p1; \
719
} else { \
720
min = p1; \
721
max = p0; \
722
} \
723
rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
724
if (min > rad || max < -rad) { \
725
return false; \
726
}
727
728
_FORCE_INLINE_ static bool triangle_box_overlap(const Vector3 &boxcenter, const Vector3 boxhalfsize, const Vector3 *triverts) {
729
/* use separating axis theorem to test overlap between triangle and box */
730
/* need to test for overlap in these directions: */
731
/* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
732
/* we do not even need to test these) */
733
/* 2) normal of the triangle */
734
/* 3) crossproduct(edge from tri, {x,y,z}-directin) */
735
/* this gives 3x3=9 more tests */
736
real_t min, max, p0, p1, p2, rad, fex, fey, fez;
737
738
/* This is the fastest branch on Sun */
739
/* move everything so that the boxcenter is in (0,0,0) */
740
741
const Vector3 v0 = triverts[0] - boxcenter;
742
const Vector3 v1 = triverts[1] - boxcenter;
743
const Vector3 v2 = triverts[2] - boxcenter;
744
745
/* compute triangle edges */
746
const Vector3 e0 = v1 - v0; /* tri edge 0 */
747
const Vector3 e1 = v2 - v1; /* tri edge 1 */
748
const Vector3 e2 = v0 - v2; /* tri edge 2 */
749
750
/* Bullet 3: */
751
/* test the 9 tests first (this was faster) */
752
fex = Math::abs(e0.x);
753
fey = Math::abs(e0.y);
754
fez = Math::abs(e0.z);
755
AXISTEST_X01(e0.z, e0.y, fez, fey);
756
AXISTEST_Y02(e0.z, e0.x, fez, fex);
757
AXISTEST_Z12(e0.y, e0.x, fey, fex);
758
759
fex = Math::abs(e1.x);
760
fey = Math::abs(e1.y);
761
fez = Math::abs(e1.z);
762
AXISTEST_X01(e1.z, e1.y, fez, fey);
763
AXISTEST_Y02(e1.z, e1.x, fez, fex);
764
AXISTEST_Z0(e1.y, e1.x, fey, fex);
765
766
fex = Math::abs(e2.x);
767
fey = Math::abs(e2.y);
768
fez = Math::abs(e2.z);
769
AXISTEST_X2(e2.z, e2.y, fez, fey);
770
AXISTEST_Y1(e2.z, e2.x, fez, fex);
771
AXISTEST_Z12(e2.y, e2.x, fey, fex);
772
773
/* Bullet 1: */
774
/* first test overlap in the {x,y,z}-directions */
775
/* find min, max of the triangle each direction, and test for overlap in */
776
/* that direction -- this is equivalent to testing a minimal AABB around */
777
/* the triangle against the AABB */
778
779
/* test in X-direction */
780
FINDMINMAX(v0.x, v1.x, v2.x, min, max);
781
if (min > boxhalfsize.x || max < -boxhalfsize.x) {
782
return false;
783
}
784
785
/* test in Y-direction */
786
FINDMINMAX(v0.y, v1.y, v2.y, min, max);
787
if (min > boxhalfsize.y || max < -boxhalfsize.y) {
788
return false;
789
}
790
791
/* test in Z-direction */
792
FINDMINMAX(v0.z, v1.z, v2.z, min, max);
793
if (min > boxhalfsize.z || max < -boxhalfsize.z) {
794
return false;
795
}
796
797
/* Bullet 2: */
798
/* test if the box intersects the plane of the triangle */
799
/* compute plane equation of triangle: normal*x+d=0 */
800
const Vector3 normal = e0.cross(e1);
801
const real_t d = -normal.dot(v0); /* plane eq: normal.x+d=0 */
802
return planeBoxOverlap(normal, d, boxhalfsize); /* if true, box and triangle overlaps */
803
}
804
805
static Vector<uint32_t> generate_edf(const Vector<bool> &p_voxels, const Vector3i &p_size, bool p_negative);
806
static Vector<int8_t> generate_sdf8(const Vector<uint32_t> &p_positive, const Vector<uint32_t> &p_negative);
807
808
static Vector3 triangle_get_barycentric_coords(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c, const Vector3 &p_pos) {
809
const Vector3 v0 = p_b - p_a;
810
const Vector3 v1 = p_c - p_a;
811
const Vector3 v2 = p_pos - p_a;
812
813
const real_t d00 = v0.dot(v0);
814
const real_t d01 = v0.dot(v1);
815
const real_t d11 = v1.dot(v1);
816
const real_t d20 = v2.dot(v0);
817
const real_t d21 = v2.dot(v1);
818
const real_t denom = (d00 * d11 - d01 * d01);
819
if (denom == 0) {
820
return Vector3(); //invalid triangle, return empty
821
}
822
const real_t v = (d11 * d20 - d01 * d21) / denom;
823
const real_t w = (d00 * d21 - d01 * d20) / denom;
824
const real_t u = 1.0f - v - w;
825
return Vector3(u, v, w);
826
}
827
828
static Color tetrahedron_get_barycentric_coords(const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_c, const Vector3 &p_d, const Vector3 &p_pos) {
829
const Vector3 vap = p_pos - p_a;
830
const Vector3 vbp = p_pos - p_b;
831
832
const Vector3 vab = p_b - p_a;
833
const Vector3 vac = p_c - p_a;
834
const Vector3 vad = p_d - p_a;
835
836
const Vector3 vbc = p_c - p_b;
837
const Vector3 vbd = p_d - p_b;
838
// ScTP computes the scalar triple product
839
#define STP(m_a, m_b, m_c) ((m_a).dot((m_b).cross((m_c))))
840
const real_t va6 = STP(vbp, vbd, vbc);
841
const real_t vb6 = STP(vap, vac, vad);
842
const real_t vc6 = STP(vap, vad, vab);
843
const real_t vd6 = STP(vap, vab, vac);
844
const real_t v6 = 1 / STP(vab, vac, vad);
845
return Color(va6 * v6, vb6 * v6, vc6 * v6, vd6 * v6);
846
#undef STP
847
}
848
849
_FORCE_INLINE_ static Vector3 octahedron_map_decode(const Vector2 &p_uv) {
850
// https://twitter.com/Stubbesaurus/status/937994790553227264
851
const Vector2 f = p_uv * 2.0f - Vector2(1.0f, 1.0f);
852
Vector3 n = Vector3(f.x, f.y, 1.0f - Math::abs(f.x) - Math::abs(f.y));
853
const real_t t = CLAMP(-n.z, 0.0f, 1.0f);
854
n.x += n.x >= 0 ? -t : t;
855
n.y += n.y >= 0 ? -t : t;
856
return n.normalized();
857
}
858
};
859
860