Directly use segment points in Geometry2D/3D function parameters

This commit is contained in:
Aaron Franke 2025-03-11 10:55:53 -07:00
parent e585e6a3eb
commit c1acc839a8
No known key found for this signature in database
GPG key ID: 40A1750B977E56BF
30 changed files with 205 additions and 224 deletions

View file

@ -302,12 +302,7 @@ Vector3 AStar3D::get_closest_position_in_segment(const Vector3 &p_point) const {
continue;
}
Vector3 segment[2] = {
from_point->pos,
to_point->pos,
};
Vector3 p = Geometry3D::get_closest_point_to_segment(p_point, segment);
Vector3 p = Geometry3D::get_closest_point_to_segment(p_point, from_point->pos, to_point->pos);
real_t d = p_point.distance_squared_to(p);
if (d < closest_dist) {
closest_point = p;

View file

@ -99,27 +99,39 @@ public:
return Math::sqrt((c1 - c2).dot(c1 - c2));
}
#ifndef DISABLE_DEPRECATED
static Vector2 get_closest_point_to_segment(const Vector2 &p_point, const Vector2 *p_segment) {
Vector2 p = p_point - p_segment[0];
Vector2 n = p_segment[1] - p_segment[0];
return get_closest_point_to_segment(p_point, p_segment[0], p_segment[1]);
}
#endif // DISABLE_DEPRECATED
static Vector2 get_closest_point_to_segment(const Vector2 &p_point, const Vector2 &p_segment_a, const Vector2 &p_segment_b) {
Vector2 p = p_point - p_segment_a;
Vector2 n = p_segment_b - p_segment_a;
real_t l2 = n.length_squared();
if (l2 < 1e-20f) {
return p_segment[0]; // Both points are the same, just give any.
return p_segment_a; // Both points are the same, just give any.
}
real_t d = n.dot(p) / l2;
if (d <= 0.0f) {
return p_segment[0]; // Before first point.
return p_segment_a; // Before first point.
} else if (d >= 1.0f) {
return p_segment[1]; // After first point.
return p_segment_b; // After first point.
} else {
return p_segment[0] + n * d; // Inside.
return p_segment_a + n * d; // Inside.
}
}
#ifndef DISABLE_DEPRECATED
static real_t get_distance_to_segment(const Vector2 &p_point, const Vector2 *p_segment) {
return p_point.distance_to(get_closest_point_to_segment(p_point, p_segment));
return get_distance_to_segment(p_point, p_segment[0], p_segment[1]);
}
#endif // DISABLE_DEPRECATED
static real_t get_distance_to_segment(const Vector2 &p_point, const Vector2 &p_segment_a, const Vector2 &p_segment_b) {
return p_point.distance_to(get_closest_point_to_segment(p_point, p_segment_a, p_segment_b));
}
static bool is_point_in_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) {
@ -136,17 +148,23 @@ public:
return (cn.cross(an) > 0) == orientation;
}
#ifndef DISABLE_DEPRECATED
static Vector2 get_closest_point_to_segment_uncapped(const Vector2 &p_point, const Vector2 *p_segment) {
Vector2 p = p_point - p_segment[0];
Vector2 n = p_segment[1] - p_segment[0];
return get_closest_point_to_segment_uncapped(p_point, p_segment[0], p_segment[1]);
}
#endif // DISABLE_DEPRECATED
static Vector2 get_closest_point_to_segment_uncapped(const Vector2 &p_point, const Vector2 &p_segment_a, const Vector2 &p_segment_b) {
Vector2 p = p_point - p_segment_a;
Vector2 n = p_segment_b - p_segment_a;
real_t l2 = n.length_squared();
if (l2 < 1e-20f) {
return p_segment[0]; // Both points are the same, just give any.
return p_segment_a; // Both points are the same, just give any.
}
real_t d = n.dot(p) / l2;
return p_segment[0] + n * d; // Inside.
return p_segment_a + n * d; // Inside.
}
// Disable False Positives in MSVC compiler; we correctly check for 0 here to prevent a division by 0.

View file

@ -272,7 +272,7 @@ public:
return true;
}
static bool segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Plane *p_planes, int p_plane_count, Vector3 *p_res, Vector3 *p_norm) {
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) {
real_t min = -1e20, max = 1e20;
Vector3 rel = p_to - p_from;
@ -315,46 +315,58 @@ public:
return false; // No intersection.
}
if (p_res) {
*p_res = p_from + dir * min;
if (r_res) {
*r_res = p_from + dir * min;
}
if (p_norm) {
*p_norm = p_planes[min_index].normal;
if (r_norm) {
*r_norm = p_planes[min_index].normal;
}
return true;
}
#ifndef DISABLE_DEPRECATED
static Vector3 get_closest_point_to_segment(const Vector3 &p_point, const Vector3 *p_segment) {
Vector3 p = p_point - p_segment[0];
Vector3 n = p_segment[1] - p_segment[0];
return get_closest_point_to_segment(p_point, p_segment[0], p_segment[1]);
}
#endif // DISABLE_DEPRECATED
static Vector3 get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_segment_a, const Vector3 &p_segment_b) {
Vector3 p = p_point - p_segment_a;
Vector3 n = p_segment_b - p_segment_a;
real_t l2 = n.length_squared();
if (l2 < 1e-20f) {
return p_segment[0]; // Both points are the same, just give any.
return p_segment_a; // Both points are the same, just give any.
}
real_t d = n.dot(p) / l2;
if (d <= 0.0f) {
return p_segment[0]; // Before first point.
return p_segment_a; // Before first point.
} else if (d >= 1.0f) {
return p_segment[1]; // After first point.
return p_segment_b; // After first point.
} else {
return p_segment[0] + n * d; // Inside.
return p_segment_a + n * d; // Inside.
}
}
#ifndef DISABLE_DEPRECATED
static Vector3 get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 *p_segment) {
Vector3 p = p_point - p_segment[0];
Vector3 n = p_segment[1] - p_segment[0];
return get_closest_point_to_segment_uncapped(p_point, p_segment[0], p_segment[1]);
}
#endif // DISABLE_DEPRECATED
static Vector3 get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_segment_a, const Vector3 &p_segment_b) {
Vector3 p = p_point - p_segment_a;
Vector3 n = p_segment_b - p_segment_a;
real_t l2 = n.length_squared();
if (l2 < 1e-20f) {
return p_segment[0]; // Both points are the same, just give any.
return p_segment_a; // Both points are the same, just give any.
}
real_t d = n.dot(p) / l2;
return p_segment[0] + n * d; // Inside.
return p_segment_a + n * d; // Inside.
}
static inline bool point_in_projected_triangle(const Vector3 &p_point, const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) {
@ -381,8 +393,14 @@ public:
return true;
}
#ifndef DISABLE_DEPRECATED
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) {
real_t d = p_normal.dot(p_sphere_pos) - p_normal.dot(p_triangle[0]);
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);
}
#endif // DISABLE_DEPRECATED
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) {
real_t d = p_normal.dot(p_sphere_pos) - p_normal.dot(p_triangle_a);
if (d > p_sphere_radius || d < -p_sphere_radius) {
// Not touching the plane of the face, return.
@ -393,7 +411,7 @@ public:
/** 2nd) TEST INSIDE TRIANGLE **/
if (Geometry3D::point_in_projected_triangle(contact, p_triangle[0], p_triangle[1], p_triangle[2])) {
if (Geometry3D::point_in_projected_triangle(contact, p_triangle_a, p_triangle_b, p_triangle_c)) {
r_triangle_contact = contact;
r_sphere_contact = p_sphere_pos - p_normal * p_sphere_radius;
//printf("solved inside triangle\n");
@ -402,7 +420,7 @@ public:
/** 3rd TEST INSIDE EDGE CYLINDERS **/
const Vector3 verts[4] = { p_triangle[0], p_triangle[1], p_triangle[2], p_triangle[0] }; // for() friendly
const Vector3 verts[4] = { p_triangle_a, p_triangle_b, p_triangle_c, p_triangle_a }; // for() friendly
for (int i = 0; i < 3; i++) {
// Check edge cylinder.