mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
ui-canvas: path_runs flattens per segment with a bounded point count and breaks runs at non-finite segments
This commit is contained in:
@@ -28,6 +28,10 @@ const EDGE_ALPHA: f32 = 0.95;
|
||||
/// How far, in screen points, a flattened curve may leave the path.
|
||||
const FLATTEN_TOLERANCE: f64 = 0.25;
|
||||
|
||||
/// About how many points one flattened segment may have (the tolerance stays at `FLATTEN_TOLERANCE` for segments
|
||||
/// up to about 250k screen points across).
|
||||
const MAX_SEGMENT_POINTS: f64 = 1000.0;
|
||||
|
||||
/// What a click under the pointer would pick, in document units.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
@@ -88,25 +92,63 @@ pub fn path_runs(view: &CanvasView, path: &BezPath) -> Vec<(Vec<Pos2>, bool)> {
|
||||
let screen = view.affine() * path.clone();
|
||||
let mut out = Vec::new();
|
||||
let mut points = Vec::new();
|
||||
let mut end = |points: &mut Vec<Pos2>, closed: bool| {
|
||||
let end = |out: &mut Vec<_>, points: &mut Vec<Pos2>, closed: bool| {
|
||||
let run = std::mem::take(points);
|
||||
if run.len() > 1 {
|
||||
out.push((run, closed));
|
||||
}
|
||||
};
|
||||
kurbo::flatten(screen.elements().iter().copied(), FLATTEN_TOLERANCE, |element| match element {
|
||||
PathEl::MoveTo(p) => {
|
||||
end(&mut points, false);
|
||||
points.push(pos2(p.x as f32, p.y as f32));
|
||||
let (mut start, mut current) = (kurbo::Point::ZERO, kurbo::Point::ZERO);
|
||||
for element in screen.elements().iter().copied() {
|
||||
match element {
|
||||
PathEl::MoveTo(p) => {
|
||||
end(&mut out, &mut points, false);
|
||||
points.push(pos2(p.x as f32, p.y as f32));
|
||||
(start, current) = (p, p);
|
||||
}
|
||||
PathEl::ClosePath => {
|
||||
end(&mut out, &mut points, true);
|
||||
current = start;
|
||||
}
|
||||
segment => {
|
||||
let Some(to) = segment.end_point() else { continue };
|
||||
let hull = segment_hull(current, segment);
|
||||
current = to;
|
||||
if !hull.iter().all(|p| p.is_finite()) {
|
||||
// A segment with a non-finite point has no drawable shape; the run breaks there and the next
|
||||
// one starts at its end.
|
||||
end(&mut out, &mut points, false);
|
||||
if to.is_finite() {
|
||||
points.push(pos2(to.x as f32, to.y as f32));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let extent = hull.iter().fold(Rect::from_points(hull[0], hull[0]), |r, p| r.union_pt(*p));
|
||||
// The point count of a flattened curve grows with the square root of its size over the tolerance;
|
||||
// a segment far larger than any screen keeps a bounded count at a coarser tolerance.
|
||||
let tolerance = FLATTEN_TOLERANCE.max(extent.width().max(extent.height()) / MAX_SEGMENT_POINTS.powi(2));
|
||||
kurbo::flatten([PathEl::MoveTo(hull[0]), segment], tolerance, |flat| {
|
||||
if let PathEl::LineTo(p) = flat {
|
||||
points.push(pos2(p.x as f32, p.y as f32));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
PathEl::LineTo(p) => points.push(pos2(p.x as f32, p.y as f32)),
|
||||
PathEl::ClosePath => end(&mut points, true),
|
||||
_ => {}
|
||||
});
|
||||
end(&mut points, false);
|
||||
}
|
||||
end(&mut out, &mut points, false);
|
||||
out
|
||||
}
|
||||
|
||||
/// The start point and every point of one drawing segment.
|
||||
fn segment_hull(from: kurbo::Point, segment: PathEl) -> Vec<kurbo::Point> {
|
||||
match segment {
|
||||
PathEl::LineTo(p) => vec![from, p],
|
||||
PathEl::QuadTo(a, p) => vec![from, a, p],
|
||||
PathEl::CurveTo(a, b, p) => vec![from, a, b, p],
|
||||
PathEl::MoveTo(_) | PathEl::ClosePath => vec![from],
|
||||
}
|
||||
}
|
||||
|
||||
/// Strokes one run: the kit's halo-and-line outline in the overlay colours, or a line of the host's `color` alone.
|
||||
fn stroke_run(painter: &ContentPainterCx<'_>, run: Vec<Pos2>, closed: bool, color: Option<DataColor>, width: f32) {
|
||||
match color {
|
||||
|
||||
@@ -8,4 +8,6 @@ mod guides;
|
||||
|
||||
mod marquee;
|
||||
|
||||
mod outline;
|
||||
|
||||
mod task_bar;
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
//! `path_runs`: the screen polylines a path outline is stroked from.
|
||||
|
||||
use craft_behavior::CanvasView;
|
||||
use craft_behavior::kurbo::{BezPath, Point};
|
||||
use craft_ui::canvas_controls::path_runs;
|
||||
use craft_ui::testing::trial;
|
||||
use egui::{Pos2, Rect, vec2};
|
||||
|
||||
fn view() -> CanvasView {
|
||||
CanvasView::new(Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)), Point::new(200.0, 150.0), 1.0)
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_curve_far_larger_than_the_screen_flattens_to_a_bounded_run() {
|
||||
let far = Point::new(1e40, 0.0);
|
||||
let mut path = BezPath::new();
|
||||
path.move_to(far);
|
||||
path.curve_to(far, Point::new(20.0, 20.0), Point::new(20.0, 20.0));
|
||||
path.curve_to((60.0, 20.0), (60.0, 120.0), (40.0, 120.0));
|
||||
let runs = path_runs(&view(), &path);
|
||||
assert_eq!(runs.len(), 1);
|
||||
assert!(runs[0].0.len() < 10_000);
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_non_finite_segment_breaks_the_run_and_the_rest_still_draws() {
|
||||
let mut path = BezPath::new();
|
||||
path.move_to((10.0, 10.0));
|
||||
path.line_to((50.0, 10.0));
|
||||
path.line_to((f64::NAN, 10.0));
|
||||
path.line_to((50.0, 80.0));
|
||||
path.line_to((90.0, 80.0));
|
||||
let runs = path_runs(&view(), &path);
|
||||
assert_eq!(runs.len(), 2);
|
||||
assert!(runs.iter().flat_map(|(points, _)| points).all(|p| p.x.is_finite() && p.y.is_finite()));
|
||||
}
|
||||
Reference in New Issue
Block a user