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