Crate polydf

Crate polydf 

Source
Expand description

polydf: Distance queries for parametric curves (2D and 3D).

  • 2D curves: nearest point, unsigned and signed distance (using outward normal).
  • 3D curves: nearest point and unsigned distance (no signed distance for space curves).

Example (3D helix):

use nalgebra::{Vector3, vector};
use polydf::{NearestOptions, nearest_t_3d, distance_to_curve_3d};

fn helix(t: f32) -> Vector3<f32> {
    vector![t.cos(), t.sin(), 0.5 * t]
}

let p = vector![2.0, 0.0, 0.0];
let t_range = -10.0f32..=10.0f32;
let opts = NearestOptions::default();
let res = nearest_t_3d(p, helix, t_range.clone(), opts);
println!("t* = {} distance = {}", res.t, res.distance);
let d = distance_to_curve_3d(p, helix, t_range, opts);

This crate provides routines to find the nearest parameter t and compute the (optionally signed) distance from an arbitrary 2D point p to a differentiable parametric curve C(t): f32 -> (f32, f32).

Key features:

  • Works with any differentiable 2D parametric function (closures supported).
  • Returns the nearest parameter t, closest point, and (signed) distance.
  • Optional thresholded query with a conservative early-out if the point is provably farther than the threshold (requires a speed bound).

Notes on signed distance:

  • The sign is defined using the outward normal estimated from the curve tangent at the closest point. We take the outward normal as the tangent rotated by -90 degrees, and define signed_distance = (p - C(t*)) · n. For CCW-oriented closed curves, points outside are positive and inside are negative. For other orientations, the sign will be consistent but may flip depending on your curve’s direction.

Structs§

NearestOptions
Options controlling the search behavior.
NearestResult
Result of a nearest-point query on a parametric curve.
NearestResult3D
Result of a nearest-point query on a 3D parametric curve.

Functions§

distance_to_curve
Return the unsigned distance from point p to the curve f(t) over t_range.
distance_to_curve_3d
3D: Return the unsigned distance from point p to the curve f(t).
distance_to_curve_tuple
Tuple-friendly unsigned distance from (x, y) to curve f(t) in t_range.
is_definitely_far
Attempt to quickly reject queries that are provably farther than threshold.
is_definitely_far_3d
3D variant: attempt to quickly reject queries that are provably farther than threshold, given an upper bound on the curve speed.
nearest_t
Find nearest parameter t to point p on curve f(t) over t_range.
nearest_t_3d
Find nearest parameter t to point p on a 3D curve f(t) over t_range. Returns the nearest parameter and unsigned distance.
nearest_t_tuple
Convenience wrapper for curves defined as Fn(f32) -> (f32, f32) and points as tuples.
nearest_t_tuple_3d
Convenience wrapper for 3D curves defined as Fn(f32) -> (f32, f32, f32) and points as tuples.
nearest_t_with_derivative
Find nearest parameter t using both the curve f and its derivative df.
nearest_t_with_derivative_tuple
Convenience wrapper for tuple-based curve and derivative.
nearest_t_within
Thresholded query: if speed_upper_bound is provided and the point is provably farther than threshold, returns None quickly. Otherwise falls back to a full nearest search and returns the result.
nearest_t_within_3d
3D variant of thresholded query. If speed_upper_bound is provided and the point is provably farther than threshold, returns None. Otherwise runs a full nearest search and returns the result.
nearest_t_within_tuple
Convenience wrapper for thresholded query using tuple-based curve and point.
within_threshold
Return true if the point is within threshold distance of the curve.
within_threshold_3d
3D: Return true if the point is within threshold distance of the curve.
within_threshold_tuple
Tuple-friendly predicate: is (x, y) within threshold distance of curve f?