Expand description
Checked closest witnesses for finite 2D and 3D segments.
Inputs reuse Vec2 and Vec3. Coordinates are promoted before
subtraction. Parameters, witness coordinates and distance are f64: even the
separation of opposite f32::MAX points is representable. Witness arrays
retain the computed precision rather than rounding to the engine’s f32
vectors; Vec3::from_array(hit.first.map(|x| x as f32)) is an explicit
presentation conversion. Distance is the Euclidean distance between the
returned arrays, not a separate calculation from rounded f32 witnesses.
Parameters are clamped to 0..=1. Exactly coincident endpoints define a
point segment with parameter zero; short nonzero segments are not discarded.
Pair queries examine the four endpoint projections and, for nonparallel
lines, their interior closest pair. Equal computed squared separations
choose the lexicographically lowest (first_parameter, second_parameter).
Comparisons subtract squared separations componentwise before summing, so a
large identical component does not hide improvements on another axis.
Thus parallel overlap selects its earliest point on the first segment;
reversing or swapping segments can change the selected witness in a tie.
Parallel means an exactly zero f64 cross product; no angle epsilon changes
near-parallel crossings into parallel lines. Cross products avoid the
cancellation of dot(u,u) * dot(v,v) - dot(u,v).powi(2) near parallelism.
These are ordinary floating-point queries, not exact geometric predicates. Ill-conditioned inputs and coordinates spanning widely different scales still lose precision in f64. No cross-platform bitwise result is promised. All queries use bounded stack storage, constant work, no heap allocation, and no retained state. A rejected query cannot affect a later one. There is no capacity, clock, reset, collision sweep, or route-progress state.
§Runnable aim, tether and gate recipe
A game can use distance to choose aim proximity, report a stretched tether, or measure proximity between a movement segment and a track gate. These geometric observations do not establish swept shape contact or a valid lap.
use ferravex_math::{Vec2, Vec3, vec2, vec3};
use ferravex_math::segments::{
ClosestPoints, SegmentError, closest_point_segment2, closest_point_segment3,
closest_segments2, closest_segments3,
};
fn main() -> Result<(), SegmentError> {
let aim: ClosestPoints<2> = closest_point_segment2(
vec2(4., 1.), Vec2::ZERO, vec2(10., 0.),
)?;
assert!(aim.distance <= 1.5); // target is within authored aim radius
assert_eq!(aim.second, [4., 0.]);
let tether = closest_point_segment3(
vec3(3., 4., 2.), Vec3::ZERO, vec3(6., 0., 0.),
)?;
assert!(tether.distance > 4.); // compare in the same world units
let attachment = Vec3::from_array(tether.second.map(|x| x as f32));
assert_eq!(attachment, vec3(3., 0., 0.));
let gate = closest_segments2(
vec2(-2., 0.), vec2(2., 0.), vec2(0., -1.), vec2(0., 1.),
)?;
assert_eq!(gate.distance, 0.);
assert_eq!(gate.first_parameter, 0.5);
// The game still owns gate ordering, direction, and accepted-event feedback.
let elevated_gate = closest_segments3(
vec3(-2., 0., 2.), vec3(2., 0., 2.),
vec3(0., -1., 0.), vec3(0., 1., 0.),
)?;
assert_eq!(elevated_gate.distance, 2.);
Ok(())
}Structs§
- Closest
Points - Two closest witnesses, with finite f64 coordinates and distance.
Enums§
- Segment
Error - A query rejected NaN or an infinite input coordinate.
Functions§
- closest_
point_ segment2 - Projects a finite point onto an ordered 2D segment, including its endpoints. A zero-length segment returns its start at parameter zero.
- closest_
point_ segment3 - Projects a finite point onto an ordered 3D segment. See the module’s f64 witness policy; nonfinite inputs are errors, not fallback points.
- closest_
segments2 - Finds closest points on two finite ordered 2D segments, including crossings, parallel overlap and point segments. Equal computed squared separations prefer the lowest first parameter, then the lowest second parameter.
- closest_
segments3 - Finds closest points on two finite ordered 3D segments, including skew and point segments. No normalization or minimum-length threshold is required.