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