use cranpose_ui_graphics::{ArcGeometry, CornerRadii, Point, Rect, StrokeCap, StrokeJoin};
const INV_SQRT2: f32 = std::f32::consts::FRAC_1_SQRT_2;
pub fn sdf_rounded_rect(p: Point, half_size: (f32, f32), radii: [f32; 4]) -> f32 {
let radius = match (p.x > 0.0, p.y > 0.0) {
(false, false) => radii[0],
(true, false) => radii[1],
(false, true) => radii[2],
(true, true) => radii[3],
};
let qx = p.x.abs() - half_size.0 + radius;
let qy = p.y.abs() - half_size.1 + radius;
let inside = qx.max(qy).min(0.0);
let outside = (qx.max(0.0).powi(2) + qy.max(0.0).powi(2)).sqrt();
inside + outside - radius
}
pub fn sdf_stroked_rounded_rect(
p: Point,
half_size: (f32, f32),
radii: [f32; 4],
half_width: f32,
join: StrokeJoin,
) -> f32 {
let hw = half_width.max(0.0);
let geom = ((half_size.0 - hw).max(0.0), (half_size.1 - hw).max(0.0));
let mut outer_radii = [radii[0] + hw, radii[1] + hw, radii[2] + hw, radii[3] + hw];
if join != StrokeJoin::Round {
for (out, r) in outer_radii.iter_mut().zip(radii.iter()) {
if *r < 0.0001 {
*out = 0.0;
}
}
}
let inner_radii = [
(radii[0] - hw).max(0.0),
(radii[1] - hw).max(0.0),
(radii[2] - hw).max(0.0),
(radii[3] - hw).max(0.0),
];
let outer = sdf_rounded_rect(p, (geom.0 + hw, geom.1 + hw), outer_radii);
let inner = sdf_rounded_rect(
p,
((geom.0 - hw).max(0.0), (geom.1 - hw).max(0.0)),
inner_radii,
);
let mut dist = outer.max(-inner);
if join == StrokeJoin::Bevel {
let chamfer = (p.x.abs() + p.y.abs() - (geom.0 + geom.1 + hw)) * INV_SQRT2;
dist = dist.max(chamfer);
}
dist
}
pub fn sdf_arc_band(p: Point, arc: &ArcGeometry) -> f32 {
let ra = arc.mid_radius();
let rb = arc.half_thickness().max(0.0);
let sweep = arc.sweep_angle.clamp(0.0, cranpose_ui_graphics::TAU);
let half_sweep = sweep * 0.5;
let mid = arc.start_angle + half_sweep;
let (sm, cm) = mid.sin_cos();
let dx = p.x - arc.center.x;
let dy = p.y - arc.center.y;
let qx = (-sm * dx + cm * dy).abs();
let qy = cm * dx + sm * dy;
let sc = (half_sweep.sin().max(0.0), half_sweep.cos());
let mut dist = if sc.1 * qx > sc.0 * qy {
((qx - sc.0 * ra).powi(2) + (qy - sc.1 * ra).powi(2)).sqrt() - rb
} else {
((qx * qx + qy * qy).sqrt() - ra).abs() - rb
};
let plane = sc.1 * qx - sc.0 * qy;
match arc.cap {
StrokeCap::Butt => dist = dist.max(plane),
StrokeCap::Square => dist = dist.max(plane - rb),
StrokeCap::Round => {}
}
dist
}
pub fn coverage_for_distance(distance: f32) -> f32 {
if !distance.is_finite() {
return 0.0;
}
let t = ((distance + 0.5).clamp(0.0, 1.0)) as f64;
let smooth = t * t * (3.0 - 2.0 * t);
(1.0 - smooth) as f32
}
pub fn stroked_rect_coverage(
point: Point,
rect: Rect,
radii: Option<CornerRadii>,
half_width: f32,
join: StrokeJoin,
) -> f32 {
let half_size = (rect.width * 0.5, rect.height * 0.5);
let local = Point::new(
point.x - (rect.x + half_size.0),
point.y - (rect.y + half_size.1),
);
let radii = radii.unwrap_or_default();
let distance = sdf_stroked_rounded_rect(
local,
half_size,
[
radii.top_left,
radii.top_right,
radii.bottom_left,
radii.bottom_right,
],
half_width,
join,
);
coverage_for_distance(distance)
}
pub fn arc_coverage(point: Point, arc: &ArcGeometry) -> f32 {
coverage_for_distance(sdf_arc_band(point, arc))
}
#[cfg(test)]
mod tests {
use super::*;
use cranpose_ui_graphics::TAU;
use std::f32::consts::{FRAC_PI_2, PI};
fn arc(inner: f32, outer: f32, start: f32, sweep: f32, cap: StrokeCap) -> ArcGeometry {
ArcGeometry::new(Point::ZERO, inner, outer, start, sweep, cap)
}
#[test]
fn rounded_rect_sdf_matches_known_distances() {
let d_center = sdf_rounded_rect(Point::ZERO, (10.0, 10.0), [0.0; 4]);
assert!((d_center + 10.0).abs() < 1e-4, "{d_center}");
let d_outside = sdf_rounded_rect(Point::new(15.0, 0.0), (10.0, 10.0), [0.0; 4]);
assert!((d_outside - 5.0).abs() < 1e-4, "{d_outside}");
}
#[test]
fn stroked_rect_covers_only_the_band_around_the_edge() {
let half = (12.0, 12.0);
let on_edge = sdf_stroked_rounded_rect(
Point::new(10.0, 0.0),
half,
[0.0; 4],
2.0,
StrokeJoin::Miter,
);
assert!(on_edge < 0.0, "the edge itself must be inside the stroke");
let inside =
sdf_stroked_rounded_rect(Point::new(4.0, 0.0), half, [0.0; 4], 2.0, StrokeJoin::Miter);
assert!(inside > 0.0, "the interior must be empty for a stroke");
let outside = sdf_stroked_rounded_rect(
Point::new(16.0, 0.0),
half,
[0.0; 4],
2.0,
StrokeJoin::Miter,
);
assert!(outside > 0.0, "well outside must be empty");
}
#[test]
fn miter_join_keeps_a_square_corner_round_join_does_not() {
let corner = Point::new(11.9, 11.9);
let miter =
sdf_stroked_rounded_rect(corner, (12.0, 12.0), [0.0; 4], 2.0, StrokeJoin::Miter);
let round =
sdf_stroked_rounded_rect(corner, (12.0, 12.0), [0.0; 4], 2.0, StrokeJoin::Round);
let bevel =
sdf_stroked_rounded_rect(corner, (12.0, 12.0), [0.0; 4], 2.0, StrokeJoin::Bevel);
assert!(miter < 0.0, "miter fills the corner point: {miter}");
assert!(round > 0.0, "round cuts the corner off: {round}");
assert!(bevel > 0.0, "bevel cuts the corner off: {bevel}");
assert!(
bevel > round,
"the bevel chord must cut deeper than the round arc: \
bevel={bevel} round={round}"
);
}
#[test]
fn full_ring_has_no_seam_at_the_wrap_point() {
let ring = arc(8.0, 12.0, 0.0, TAU, StrokeCap::Butt);
for step in 0..64 {
let angle = step as f32 / 64.0 * TAU;
let (sin, cos) = angle.sin_cos();
let p = Point::new(cos * 10.0, sin * 10.0);
let d = sdf_arc_band(p, &ring);
assert!(
d < 0.0,
"the ring centerline must be covered at angle {angle}: d={d}"
);
}
}
#[test]
fn butt_caps_cut_the_band_at_the_radial_ends() {
let band = arc(8.0, 12.0, 0.0, FRAC_PI_2, StrokeCap::Butt);
let inside = Point::new(10.0 * INV_SQRT2, 10.0 * INV_SQRT2);
assert!(sdf_arc_band(inside, &band) < 0.0);
let past_end = Point::new(-1.0, 10.0);
assert!(
sdf_arc_band(past_end, &band) > 0.0,
"butt cap must not bulge past the radial end"
);
let before_start = Point::new(10.0, -1.0);
assert!(sdf_arc_band(before_start, &band) > 0.0);
}
#[test]
fn round_caps_bulge_past_the_radial_ends_and_square_caps_project() {
let round = arc(8.0, 12.0, 0.0, FRAC_PI_2, StrokeCap::Round);
let square = arc(8.0, 12.0, 0.0, FRAC_PI_2, StrokeCap::Square);
let before_start = Point::new(10.0, -1.0);
assert!(
sdf_arc_band(before_start, &round) < 0.0,
"round cap must cover the semicircle past the end"
);
assert!(
sdf_arc_band(before_start, &square) < 0.0,
"square cap must cover the projection past the end"
);
let far = Point::new(10.0, -3.0);
assert!(sdf_arc_band(far, &round) > 0.0);
assert!(sdf_arc_band(far, &square) > 0.0);
}
#[test]
fn annular_sector_has_flat_radial_edges() {
let sector = arc(6.0, 12.0, 0.0, PI, StrokeCap::Butt);
for radius in [6.5, 8.0, 10.0, 11.5] {
let p = Point::new(radius * (0.01f32).cos(), radius * (0.01f32).sin());
assert!(
sdf_arc_band(p, §or) < 0.0,
"radius {radius} just inside the sweep must be covered"
);
let q = Point::new(radius * (-0.2f32).cos(), radius * (-0.2f32).sin());
assert!(
sdf_arc_band(q, §or) > 0.0,
"radius {radius} just outside the sweep must be empty"
);
}
}
#[test]
fn wedge_with_zero_inner_radius_reaches_the_center() {
let wedge = arc(0.0, 10.0, 0.0, FRAC_PI_2, StrokeCap::Butt);
assert!(sdf_arc_band(Point::new(0.5, 0.5), &wedge) < 0.0);
assert!(sdf_arc_band(Point::new(-0.5, -0.5), &wedge) > 0.0);
}
#[test]
fn degenerate_arcs_never_produce_nan_coverage() {
for geometry in [
arc(0.0, 0.0, 0.0, 0.0, StrokeCap::Butt),
arc(5.0, 5.0, 0.0, 1.0, StrokeCap::Round),
arc(0.0, 10.0, 0.0, 0.0, StrokeCap::Square),
ArcGeometry::new(Point::ZERO, f32::NAN, 1.0, 0.0, 1.0, StrokeCap::Butt),
] {
for p in [Point::ZERO, Point::new(3.0, -4.0), Point::new(-9.0, 9.0)] {
let value = arc_coverage(p, &geometry);
assert!(value.is_finite(), "coverage must stay finite: {value}");
assert!((0.0..=1.0).contains(&value), "{value}");
}
}
}
#[test]
fn coverage_saturates_and_antialiases() {
assert_eq!(coverage_for_distance(-5.0), 1.0);
assert_eq!(coverage_for_distance(5.0), 0.0);
assert!((coverage_for_distance(0.0) - 0.5).abs() < 1e-5);
assert_eq!(coverage_for_distance(f32::NAN), 0.0);
}
#[test]
fn stroked_rect_coverage_uses_the_inflated_bounds() {
let bounds = Rect {
x: 8.0,
y: 8.0,
width: 24.0,
height: 24.0,
};
let on_edge =
stroked_rect_coverage(Point::new(10.0, 20.0), bounds, None, 2.0, StrokeJoin::Miter);
assert!(on_edge > 0.9, "the stroked edge must be opaque: {on_edge}");
let interior =
stroked_rect_coverage(Point::new(20.0, 20.0), bounds, None, 2.0, StrokeJoin::Miter);
assert_eq!(interior, 0.0, "a stroke must not fill its interior");
}
}