Canvas: keep the moved anchor marks mesh only when every square edge rounds the same

The kept mesh moved when each anchor's screen position moved by whole
pixels, but a square's edges are computed in f32 from that position and
rounded to pixels by the tessellator. A 5 pt anchor at x = 252.99998
moving one pixel has its right edge go from 255.49998 to 256.5, which
round to 255 and 257, so the moved mesh drew that edge at 256.

The move now checks the squares the marks draw at each point, at the
anchor size and at the handle size: each must keep its f32 size and
have every edge round to pixels moved by the same whole pixels.
Squares thin enough for the tessellator to draw as a line are
tessellated again after any move.
This commit is contained in:
dotCooCoo committed 2026-12-05 09:25:09 -08:00
1 parent d15584f20b
commit 65ad024ee8
2 files changed
+89 -15

No files matched your search

+39 -9
View File
@@ -699,12 +699,15 @@ const PIXEL_SLACK: f64 = 1e-6;
impl KeptMesh {
/// Moves the mesh by `by` in the document when tessellating the moved marks would give the same mesh moved, and
/// returns whether it did. `points` are the marks' anchors and direction handles before the move ([`knot_points`]).
/// The mesh moves when the marks' whole move since they were tessellated is a whole number of physical pixels on
/// screen, and when every point's `f32` screen position moves by this move's whole pixels too, so the tessellator's
/// rounding to pixels lands the same. It also needs every point inside the canvas before and after the move, so the
/// marks are drawn near and inside the clip on both sides and no anchor, handle or outline is left out or culled.
fn translate(&mut self, by: Vec2, points: impl IntoIterator<Item = Point>) -> bool {
/// returns whether it did. `points` are the marks' anchors and direction handles before the move ([`knot_points`]),
/// and `options` the Path Options they are drawn with. The mesh moves when the marks' whole move since they were
/// tessellated is a whole number of physical pixels on screen, and when every point's `f32` screen position moves
/// by this move's whole pixels too. Every square [`mark_shapes`] could draw at a point, at the anchor side and at
/// the handle side, must keep its `f32` size and have each edge round to pixels as the tessellator rounds it, moved
/// by the same whole pixels; a square thin enough for the tessellator to draw as a line is tessellated again. It
/// also needs every point inside the canvas before and after the move, so the marks are drawn near and inside the
/// clip on both sides and no anchor, handle or outline is left out or culled.
fn translate(&mut self, by: Vec2, points: impl IntoIterator<Item = Point>, options: &PathOptionsSettings) -> bool {
let key = &self.key;
let ppp = f64::from(key.pixels_per_point);
let view = key.view.affine();
@@ -722,11 +725,32 @@ impl KeptMesh {
egui::pos2(s.x as f32, s.y as f32)
};
let whole = |from: f32, to: f32, px: f64| ((f64::from(to) - f64::from(from)) * ppp - px).abs() <= PIXEL_SLACK;
// The tessellator's `f32` rounding of an edge to pixels (`GuiRounding::round_to_pixels`), in whole pixels.
let ppp_f32 = key.pixels_per_point;
let snapped = |edge: f32| f64::from((edge * ppp_f32).round());
let lands = |from: f32, to: f32, px: f64| snapped(to) - snapped(from) == px;
let sides = mark_sides(options, &key.look);
// Below this side the tessellator draws a filled square as a line, which it rounds other ways.
let feathering = if key.options.feathering { key.options.feathering_size_in_pixels / ppp_f32 } else { 0.0 };
if sides.iter().any(|side| *side <= 2.0 * feathering) {
return false;
}
for p in points {
let (from, to) = (screen(p), screen(p + by));
if !(area.contains(from) && area.contains(to) && whole(from.x, to.x, pixels[0]) && whole(from.y, to.y, pixels[1])) {
return false;
}
for side in sides {
let (a, b) = (Rect::from_center_size(from, egui::Vec2::splat(side)), Rect::from_center_size(to, egui::Vec2::splat(side)));
if a.size() != b.size()
|| !(lands(a.min.x, b.min.x, pixels[0])
&& lands(a.max.x, b.max.x, pixels[0])
&& lands(a.min.y, b.min.y, pixels[1])
&& lands(a.max.y, b.max.y, pixels[1]))
{
return false;
}
}
}
let offset = vec2((pixels[0] / ppp) as f32, (pixels[1] / ppp) as f32);
self.moved = moved;
@@ -921,14 +945,20 @@ enum MarkFill {
Path,
}
/// The side of an anchor's mark, then of a direction handle's end, under `options`.
fn mark_sides(options: &PathOptionsSettings, look: &MarkLook) -> [f32; 2] {
let anchor = options.anchor_size.unwrap_or(look.anchor);
let handle = if options.handles == HandleStyle::Large { look.large } else { look.anchor };
[anchor, handle]
}
/// The egui shapes `BezierOverlay` paints for `set` under `BezierTool::Anchors`, in its order: every outline, the
/// selected segment, then the anchors and handles near `rect`, then the handles of the other paths' selected anchors.
fn mark_shapes(out: &mut Vec<Shape>, set: &MarkSet<'_>, view: &CanvasView, rect: Rect, look: &MarkLook) {
let options = set.options;
let option_color = options.color.map(|[r, g, b]| Color32::from(Srgba::rgb(f32::from(r) / 255.0, f32::from(g) / 255.0, f32::from(b) / 255.0)));
let path_color = |path: &EditPath| path.color.map(Color32::from);
let anchor_side = options.anchor_size.unwrap_or(look.anchor);
let handle_side = if options.handles == HandleStyle::Large { look.large } else { look.anchor };
let [anchor_side, handle_side] = mark_sides(options, look);
let circles = set.shown == AnchorsShown::Curvature;
let line = |out: &mut Vec<Shape>, points: Vec<egui::Pos2>, closed: bool, segment: bool, color: Option<Color32>| {
let named = if segment { look.thick } else { look.medium };
@@ -1268,7 +1298,7 @@ impl SelectionMarks {
/// ([`KeptMesh::translate`]); otherwise the next paint tessellates the marks again.
fn translate(&mut self, by: Vec2) {
let mesh = self.mesh.get_mut();
if mesh.as_mut().is_some_and(|kept| !kept.translate(by, knot_points(&self.anchors))) {
if mesh.as_mut().is_some_and(|kept| !kept.translate(by, knot_points(&self.anchors), &self.anchors.options)) {
*mesh = None;
}
let shift = vectorcraft_geom::Affine::translate(by);
+50 -6
View File
@@ -1366,6 +1366,16 @@ fn a_drag_of_the_selection_moves_the_anchor_marks_mesh_without_tessellating_it()
// squares would round to other pixels than the moved mesh's.
#[test]
fn a_whole_pixel_move_keeps_the_mesh_only_when_the_screen_positions_move_by_whole_pixels() {
let by = Vec2::new(1.0, 0.0);
let options = PathOptionsSettings::default();
assert!(kept_at_one_pixel_per_point().translate(by, [Point::new(255.5, 10.0)], &options), "a point whose screen position moves one whole pixel");
let mut off = kept_at_one_pixel_per_point();
assert!(!off.translate(by, [Point::new(255.5, 10.0), Point::new(255.999_991, 10.0)], &options), "a point that moves 1.0000153 pixels");
assert_eq!(off.moved, [0.0; 2], "the refused move leaves the mesh where it was");
}
/// A kept mesh of no marks at one physical pixel per point, with the document's coordinates on screen unchanged.
fn kept_at_one_pixel_per_point() -> KeptMesh {
let screen = Rect::from_min_size(egui::pos2(0.0, 0.0), vec2(1000.0, 1000.0));
let look = MarkLook {
line: Color32::BLACK,
@@ -1378,7 +1388,7 @@ fn a_whole_pixel_move_keeps_the_mesh_only_when_the_screen_positions_move_by_whol
thick: 3.0,
reach: 20.0,
};
let kept = || KeptMesh {
KeptMesh {
key: MeshKey {
marks: 0,
view: CanvasView::new(screen, Point::new(500.0, 500.0), 1.0),
@@ -1390,10 +1400,44 @@ fn a_whole_pixel_move_keeps_the_mesh_only_when_the_screen_positions_move_by_whol
},
mesh: Arc::new(egui::Mesh::default()),
moved: [0.0; 2],
};
}
}
// A point whose `f32` screen position moves by exactly one pixel can still put a square's edge on other pixels: an
// anchor at x = 252.99998 moves to 253.99998, but its 5 pt square's right edge goes from 255.49998 to 256.5, which
// round to 255 and 257, so the moved mesh's edge at 256 is not the one a new tessellation draws. A 6 pt square there
// has edges that round one pixel on, but its `f32` width grows from 6 to 6.0000153, and the tessellator compares the
// width with its thresholds for drawing a square as a line or a stroke as a fill.
#[test]
fn a_whole_pixel_move_keeps_the_mesh_only_when_every_square_edge_rounds_by_those_pixels() {
let by = Vec2::new(1.0, 0.0);
assert!(kept().translate(by, [Point::new(255.5, 10.0)]), "a point whose screen position moves one whole pixel");
let mut off = kept();
assert!(!off.translate(by, [Point::new(255.5, 10.0), Point::new(255.999_991, 10.0)]), "a point that moves 1.0000153 pixels");
assert_eq!(off.moved, [0.0; 2], "the refused move leaves the mesh where it was");
let options = PathOptionsSettings::default();
// The `f32` just under 253: 252.99998474121094.
let center = f64::from(253.0_f32 - 2.0_f32.powi(-16));
let mut kept = kept_at_one_pixel_per_point();
assert!(!kept.translate(by, [Point::new(center, 10.0)], &options), "a right edge that rounds two pixels on");
assert_eq!(kept.moved, [0.0; 2], "the refused move leaves the mesh where it was");
assert!(kept_at_one_pixel_per_point().translate(by, [Point::new(252.5, 10.0)], &options), "edges on whole pixels");
// Near the origin a move is whole within the rounding noise: an anchor at x = 2.9999998 (the `f32` just under 3)
// moved to 4 keeps its square's width, but the left edge goes from 0.4999998 to 1.5, which round to 0 and 2.
let near = f64::from(3.0_f32 - 2.0_f32.powi(-22));
let step = Vec2::new(4.0 - near, 0.0);
assert!(!kept_at_one_pixel_per_point().translate(step, [Point::new(near, 10.0)], &options), "a left edge that rounds two pixels on");
// Anchors and handle ends 6 pt across.
let mut six = kept_at_one_pixel_per_point();
six.key.look.anchor = 6.0;
assert!(!six.translate(by, [Point::new(center, 10.0)], &options), "a square whose width changes");
}
// The tessellator draws a filled square no wider than twice its feathering as a line and rounds it another way, so a
// square that small is tessellated again after any move.
#[test]
#[allow(clippy::field_reassign_with_default)] // `PathOptionsSettings` is non-exhaustive: no struct literal outside its crate
fn a_move_tessellates_the_marks_again_when_a_square_is_drawn_as_a_line() {
let by = Vec2::new(1.0, 0.0);
let mut options = PathOptionsSettings::default();
options.anchor_size = Some(2.0);
assert!(!kept_at_one_pixel_per_point().translate(by, [Point::new(252.5, 10.0)], &options), "a 2 pt anchor at 1 pixel per point");
options.anchor_size = Some(3.0);
assert!(kept_at_one_pixel_per_point().translate(by, [Point::new(252.5, 10.0)], &options), "a 3 pt anchor");
}