mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 12:27:37 +00:00
Twist: preserve boundary tangents
This commit is contained in:
@@ -508,8 +508,8 @@ pub fn map_smooth_within(path: &PathData, max_piece: f64, max: usize, f: impl Fn
|
||||
map_pieces_within(path, max_piece, max, &f, |p, h| mapped_handle(&f, p, h))
|
||||
}
|
||||
|
||||
/// Handle `h` of point `p` mapped through `f`: `f(p)` plus `f`'s derivative at `p` along `h - p`
|
||||
/// (a central difference), so the image curve leaves `f(p)` the way `f` takes the curve.
|
||||
/// The mapped handle uses `f`'s derivative at `p` toward `h`, computed with a second-order
|
||||
/// one-sided difference.
|
||||
fn mapped_handle(f: &impl Fn(Point) -> Point, p: Point, h: Point) -> Point {
|
||||
let v = h - p;
|
||||
let len = v.hypot();
|
||||
@@ -517,13 +517,23 @@ fn mapped_handle(f: &impl Fn(Point) -> Point, p: Point, h: Point) -> Point {
|
||||
if len.is_nan() || len <= 0.0 || len.is_infinite() {
|
||||
return f(p);
|
||||
}
|
||||
// A thousandth of the handle, or more where the coordinates are so large that so short a
|
||||
// step would round away (#7069): the samples stay a billionth of the coordinates apart, and
|
||||
// at most at the handle's own end.
|
||||
// The step grows with the coordinates when a thousandth of the handle would round away.
|
||||
// Both samples lie between the anchor and the handle.
|
||||
let size = p.x.abs().max(p.y.abs()).max(1.0);
|
||||
let step = (1e-3f64).max(size * 1e-9 / len).min(1.0);
|
||||
let d = (f(p + v * step) - f(p - v * step)) / (2.0 * step);
|
||||
f(p) + d
|
||||
let at = f(p);
|
||||
let midpoint = p + v * (step * 0.5);
|
||||
let sample = p + v * step;
|
||||
let far = f(sample) - at;
|
||||
let near_offset = midpoint - p;
|
||||
let far_offset = sample - p;
|
||||
let uneven = (near_offset * 2.0 - far_offset).hypot() > far_offset.hypot() * 1e-6;
|
||||
// The second-order formula requires evenly spaced samples.
|
||||
if midpoint == p || midpoint == sample || uneven {
|
||||
return at + far / step;
|
||||
}
|
||||
let near = f(midpoint) - at;
|
||||
at + (near * 4.0 - far) / step
|
||||
}
|
||||
|
||||
/// [`map_nonlinear_within`]'s pieces, with `handle(point, handle)` giving each mapped handle.
|
||||
@@ -613,3 +623,19 @@ pub(super) fn resample(poly: &[Point], m: usize) -> Vec<Point> {
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod handle_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_midpoint_rounded_to_an_endpoint_keeps_the_handle_direction() {
|
||||
for x in [1e16, 1e16 + 2.0, -1e16] {
|
||||
for offset in [-6.0, -2.0, 2.0, 6.0] {
|
||||
let p = Point::new(x, 0.0);
|
||||
let h = Point::new(x + offset, 0.0);
|
||||
assert_eq!(mapped_handle(&|point| point, p, h), h);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -277,7 +277,13 @@ pub fn effect_catalog() -> Vec<EffectInfo> {
|
||||
"{h: amount (10), v: amount (10), relative: bool (true: % of size), anchors: bool (true), in: bool (true), out: bool (true), seed: int (0)}",
|
||||
json!({"h": 10.0, "v": 10.0, "relative": true, "anchors": true, "in": true, "out": true, "seed": 0}),
|
||||
),
|
||||
g("distort.twist", "Twist…", DT, "{angle: deg (-3600..3600, 10)}", json!({"angle": 10.0})),
|
||||
g(
|
||||
"distort.twist",
|
||||
"Twist…",
|
||||
DT,
|
||||
"{angle: deg (-3600..3600, 10)} Outer-circle anchors stay fixed; their handles follow the inward tangent.",
|
||||
json!({"angle": 10.0}),
|
||||
),
|
||||
g(
|
||||
"distort.zigZag",
|
||||
"Zig Zag…",
|
||||
|
||||
@@ -1708,6 +1708,28 @@ fn twist_joins_its_pieces_smoothly() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twist_boundary_handles_follow_the_inward_tangent() {
|
||||
let line = PathData::single(SubPath::polyline(&[Point::new(40.0, 30.0), Point::new(60.0, 30.0)], false));
|
||||
let bounds = Rect::new(40.0, 30.0, 60.0, 30.0);
|
||||
for angle in [90.0_f64, 400.0, -720.0] {
|
||||
let out = apply_geometry(&[fx("distort.twist", json!({"angle": angle}))], &line, bounds);
|
||||
let anchors = &out.subpaths[0].anchors;
|
||||
let start = anchors.first().unwrap();
|
||||
let end = anchors.last().unwrap();
|
||||
assert_eq!(start.p, Point::new(40.0, 30.0));
|
||||
assert_eq!(end.p, Point::new(60.0, 30.0));
|
||||
for (actual, expected) in [
|
||||
(start.h_out - start.p, vectorcraft_geom::Vec2::new(1.0, -angle.to_radians())),
|
||||
(end.h_in - end.p, vectorcraft_geom::Vec2::new(-1.0, angle.to_radians())),
|
||||
] {
|
||||
assert!(actual.hypot() > 1e-9);
|
||||
let error = (actual.x * expected.y - actual.y * expected.x).atan2(actual.dot(expected)).to_degrees().abs();
|
||||
assert!(error < 0.05, "{angle} degrees: boundary tangent differs by {error} degrees");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twist_fixes_the_outer_circle_and_rotates_inside() {
|
||||
let out = run("distort.twist", json!({"angle": 0}), &square());
|
||||
|
||||
@@ -809,6 +809,9 @@ stroke instead of the whole object: `run_command effect.apply {"effect": "path.o
|
||||
`effect.remove`, `effect.setParams` (`visible` toggles one) and `effect.duplicate` address that item's effects;
|
||||
`apply_effect` uses the active item. `effect.list` reports each object's item effects under `applied[].items`.
|
||||
|
||||
`distort.twist` keeps anchors on its outer circle fixed. Their handles follow the tangent of the
|
||||
rotation inside that circle, including for positive and negative angles.
|
||||
|
||||
On a path directly inside a graph series, `appearance.setItem`, `appearance.addFill`, `appearance.addStroke`,
|
||||
`appearance.removeItem`, `appearance.moveItem`, `appearance.duplicateItem`, `appearance.clear`,
|
||||
`appearance.reduceToBasic`, `appearance.showAllHidden`, `appearance.transfer`, `appearance.copyFrom`, `paint.default`,
|
||||
|
||||
@@ -588,6 +588,8 @@ since 2026-10-01.
|
||||
|
||||
## Evidence: what ships today, by area
|
||||
|
||||
- Twist keeps boundary handles tangent to the rotation inside its outer circle. Regressions cover 90, 400, and -720 degree twists, unchanged boundary anchors, and a sampling midpoint that rounds to an endpoint at large coordinates (`twist_boundary_handles_follow_the_inward_tangent` and `a_midpoint_rounded_to_an_endpoint_keeps_the_handle_direction`).
|
||||
|
||||
- **Recolor picker follow-up (#8031/#4028):** native Lab numeric entry, document/loaded-library swatches in the Color Picker, and Assign-row Pick from Swatches / Limit to Library menus. #4090 provided the double-click picker and #5113 the Assign sliders. Regression tests `native_lab_entry_in_color_picker` and `row_context_menu_pick_from_swatches_and_limit_to_library` exercise the fields and menu callbacks rather than writing their result state. Area score and hours unchanged.
|
||||
|
||||
- **Save refusal (#6146):** ordinary File > Save to its own location offers Save As after a terminal permission/read-only refusal, suggesting the original name and format, preserving options for the unchanged format and opening the chosen format’s Save options on a change. Cancel keeps the failed-save state. Busy pickers, recovery-copy errors, other failures, explicit destinations and Close/Quit keep the existing dialog. Engine, UI and MCP regression cases are in `cmd/fileio/tests_readonly.rs`, `io/tests_read_only_save.rs` and MCP `tests/fileio.rs`; focused UI regressions passed; engine regression execution and native panel acceptance pending. Scores and hours unchanged.
|
||||
|
||||
Reference in New Issue
Block a user