Skip to main content

trx_rs/fit/
mod.rs

1//! Polyline → Catmull-Rom fitting.
2//!
3//! Reduces a dense streamline to a sparse set of control points whose
4//! Catmull-Rom interpolation reproduces the input within a user-specified
5//! Hausdorff tolerance. Useful for editor tools (which want few sculptable
6//! control points) and for compressing dense atlas tractograms.
7//!
8//! The algorithm:
9//!
10//! 1. Start with the two endpoints as kept control points.
11//! 2. For each segment between consecutive kept points, tessellate the
12//!    Catmull-Rom curve, then walk the *interior* input vertices in that
13//!    segment and find the one whose perpendicular distance to the
14//!    tessellated polyline is largest.
15//! 3. If any segment's worst vertex is farther than `epsilon_mm`, add the
16//!    worst vertex from each violating segment as a new kept point and
17//!    iterate. Adding many points per iteration converges in O(log n)
18//!    passes for smooth curves while staying stable on degenerate inputs.
19//! 4. Stop when no segment's worst error exceeds the tolerance.
20//!
21//! Distances are computed as squared distances throughout (no `sqrt` in the
22//! hot loop) and the per-segment tessellation buffer is reused across
23//! iterations so the working set stays compact.
24//!
25//! # Endpoint convention
26//!
27//! Both endpoints of every input streamline are always retained, matching
28//! how `trxviz-draw` and most editor UIs treat streamline terminations
29//! (cortical/sub-cortical anchors are anatomically meaningful).
30//!
31//! # Coordinate units
32//!
33//! `epsilon_mm` matches the units of `points`, which in TRX is RAS+ mm.
34
35mod catmull_rom;
36mod tractogram;
37
38pub use catmull_rom::{
39    fit_catmull_rom_indices, sample_catmull_rom, sample_catmull_rom_into, simplify_streamline,
40    MIN_KEPT_POINTS,
41};
42pub use tractogram::{
43    simplify_tractogram, FittedMarker, SimplifyOptions, SimplifyStats, FITTED_MARKER_KEY,
44};