mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 12:38:18 +00:00
Merge pull request #8385 from dotCooCoo/fix/twist-boundary-tangent
Twist: boundary handles follow the inward tangent
This commit is contained in:
@@ -66,7 +66,8 @@ pub use expand::{
|
||||
use mesh::grid_eval;
|
||||
pub use mesh::{Coons, GradientMesh, MeshAppearance, MeshPiece, MeshPoint, MeshQuad, default_fidelity, grid_points, nodes_bounds};
|
||||
pub use warp::{
|
||||
WARP_NET, WarpNet, WarpStyle, fit_piece, map_nonlinear, map_nonlinear_within, map_smooth_within, pieces, uses_net, warp_point, warp_version,
|
||||
WARP_NET, WarpNet, WarpStyle, fit_piece, map_handles_within, map_nonlinear, map_nonlinear_within, map_smooth_within, pieces, uses_net,
|
||||
warp_point, warp_version,
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -508,6 +508,17 @@ 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))
|
||||
}
|
||||
|
||||
/// [`map_nonlinear_within`] with `handle(point, handle)` supplying each mapped handle position.
|
||||
pub fn map_handles_within(
|
||||
path: &PathData,
|
||||
max_piece: f64,
|
||||
max: usize,
|
||||
f: impl Fn(Point) -> Point,
|
||||
handle: impl Fn(Point, Point) -> Point,
|
||||
) -> PathData {
|
||||
map_pieces_within(path, max_piece, max, &f, handle)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
fn mapped_handle(f: &impl Fn(Point) -> Point, p: Point, h: Point) -> Point {
|
||||
@@ -553,7 +564,8 @@ fn map_pieces_within(path: &PathData, max_piece: f64, max: usize, f: &impl Fn(Po
|
||||
let c = seg_cubic(sp, i);
|
||||
let k = pieces(poly_len(&c), max_piece, MAX_PIECES);
|
||||
for j in 0..k {
|
||||
let sub = c.subsegment((j as f64 / k as f64)..((j + 1) as f64 / k as f64));
|
||||
// Evaluating an unchanged curve can overflow even when its control points are finite.
|
||||
let sub = if k == 1 { c } else { c.subsegment((j as f64 / k as f64)..((j + 1) as f64 / k as f64)) };
|
||||
if let Some(last) = res.last_mut() {
|
||||
last.h_out = handle(sub.p0, sub.p1);
|
||||
}
|
||||
@@ -613,3 +625,53 @@ pub(super) fn resample(poly: &[Point], m: usize) -> Vec<Point> {
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod handle_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn an_unsplit_curve_keeps_its_finite_control_points() {
|
||||
let start = Point::new(1e308, 0.0);
|
||||
let end = Point::new(1.03e308, 0.0);
|
||||
let path = PathData::new(vec![SubPath::new(
|
||||
vec![Anchor::with_handles(start, start, Point::new(1.01e308, 1.0)), Anchor::with_handles(end, Point::new(1.02e308, 1.0), end)],
|
||||
false,
|
||||
)]);
|
||||
assert_eq!(map_handles_within(&path, 1.0, 2, |p| p, |_, h| h), path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rounded_samples_preserve_a_smooth_interior_join() {
|
||||
let center = Point::new(1e16, 0.0);
|
||||
let path = PathData::new(vec![SubPath::new(
|
||||
vec![
|
||||
Anchor::corner(center + vectorcraft_geom::kurbo::Vec2::new(-18.0, 0.0)),
|
||||
Anchor::with_handles(
|
||||
center,
|
||||
center + vectorcraft_geom::kurbo::Vec2::new(-6.0, 0.0),
|
||||
center + vectorcraft_geom::kurbo::Vec2::new(6.0, 0.0),
|
||||
),
|
||||
Anchor::corner(center + vectorcraft_geom::kurbo::Vec2::new(18.0, 0.0)),
|
||||
],
|
||||
false,
|
||||
)]);
|
||||
let mapped = map_smooth_within(&path, f64::INFINITY, 3, |p| Point::new(p.x, p.y + (p.x - center.x).powi(2) * 0.1));
|
||||
let join = &mapped.subpaths[0].anchors[1];
|
||||
let incoming = join.p - join.h_in;
|
||||
let outgoing = join.h_out - join.p;
|
||||
assert!(incoming.cross(outgoing).abs() < 1e-9, "{incoming:?} {outgoing:?}");
|
||||
assert!(incoming.dot(outgoing) > 0.0);
|
||||
}
|
||||
|
||||
#[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…",
|
||||
|
||||
@@ -6,7 +6,7 @@ use vectorcraft_geom::{Affine, Anchor, ParamCurve, PathData, Point, Rect, SubPat
|
||||
use vectorcraft_pathops::addons::pathtools::insert_at;
|
||||
|
||||
use crate::util::{
|
||||
catmull_rom, fit_piece, flag, map_nonlinear_within, map_smooth_within, mean_size, noise, normal_at, num, pieces, seed, seg_cubic, smooth_points,
|
||||
catmull_rom, fit_piece, flag, map_handles_within, map_nonlinear_within, mean_size, noise, normal_at, num, pieces, seed, seg_cubic, smooth_points,
|
||||
};
|
||||
|
||||
fn diag(b: Rect) -> f64 {
|
||||
@@ -328,7 +328,7 @@ pub fn tweak(path: &PathData, b: Rect, p: &Value) -> PathData {
|
||||
|
||||
/// Twist: rotation that is strongest at the centre and fades to zero at the bounding circle,
|
||||
/// clockwise on the page for a positive `angle`, making at most `max` anchors. Its handles follow
|
||||
/// the rotation's derivative ([`map_smooth_within`]), so the pieces join without kinks (#7069).
|
||||
/// the rotation's directional derivative, including each side of the outer circle.
|
||||
pub fn twist(path: &PathData, b: Rect, angle: f64, max: usize) -> PathData {
|
||||
let ang = angle.clamp(-3600.0, 3600.0).to_radians();
|
||||
if ang == 0.0 {
|
||||
@@ -336,16 +336,46 @@ pub fn twist(path: &PathData, b: Rect, angle: f64, max: usize) -> PathData {
|
||||
}
|
||||
let c = b.center();
|
||||
let r = diag(b) / 2.0;
|
||||
let half = Vec2::new(b.width() * 0.5, b.height() * 0.5);
|
||||
let boundary = move |p: Point, length: f64| {
|
||||
if length == r {
|
||||
return true;
|
||||
}
|
||||
if !r.is_finite() || (length - r).abs() > 8.0 * f64::EPSILON * r {
|
||||
return false;
|
||||
}
|
||||
// A bounds-relative distance identifies boundary points affected by center rounding.
|
||||
(Vec2::new(p.x - b.x0, p.y - b.y0) - half).hypot() == r
|
||||
};
|
||||
let f = move |q: Point| {
|
||||
let d = q - c;
|
||||
let t = (1.0 - d.hypot() / r).max(0.0);
|
||||
let length = d.hypot();
|
||||
let t = (1.0 - length / r).max(0.0);
|
||||
// y runs down, so a positive turn is clockwise on the page.
|
||||
let a = ang * t;
|
||||
if a == 0.0 {
|
||||
return q;
|
||||
}
|
||||
let (s, co) = a.sin_cos();
|
||||
c + Vec2::new(d.x * co - d.y * s, d.x * s + d.y * co)
|
||||
};
|
||||
let piece = (r / 8.0).min(r / (1.0 + ang.abs() * 2.0));
|
||||
map_smooth_within(path, piece, max, f)
|
||||
map_handles_within(path, piece, max, f, |p, h| {
|
||||
let v = h - p;
|
||||
if !v.hypot().is_finite() {
|
||||
return f(p);
|
||||
}
|
||||
let d = p - c;
|
||||
let length = d.hypot();
|
||||
let radial = if length > 0.0 { (d / length).dot(v) } else { 0.0 };
|
||||
// The clamped rotation has different inward and outward derivatives at the circle.
|
||||
let inside = if boundary(p, length) { radial < 0.0 } else { length < r };
|
||||
let gradient = if inside { -ang / r * radial } else { 0.0 };
|
||||
let tangent = if gradient == 0.0 { v } else { v + Vec2::new(-d.y, d.x) * gradient };
|
||||
let a = ang * (1.0 - length / r).max(0.0);
|
||||
let (s, co) = a.sin_cos();
|
||||
f(p) + Vec2::new(tangent.x * co - tangent.y * s, tangent.x * s + tangent.y * co)
|
||||
})
|
||||
}
|
||||
|
||||
const ZIGZAG_ARCLEN_ACCURACY: f64 = 1e-4;
|
||||
|
||||
@@ -1708,6 +1708,115 @@ 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_large_coordinate_joins_preserve_boundary_sides() {
|
||||
use vectorcraft_geom::Anchor;
|
||||
|
||||
let center = 1e16;
|
||||
for offset in [32.0, 64.0] {
|
||||
let p = Point::new(center + offset, 0.0);
|
||||
let line = PathData::single(SubPath::new(
|
||||
vec![
|
||||
Anchor::corner(Point::new(p.x - 18.0, 0.0)),
|
||||
Anchor::with_handles(p, Point::new(p.x - 6.0, 0.0), Point::new(p.x + 6.0, 0.0)),
|
||||
Anchor::corner(Point::new(p.x + 18.0, 0.0)),
|
||||
],
|
||||
false,
|
||||
));
|
||||
let out = crate::distort::twist(&line, Rect::new(center - 64.0, 0.0, center + 64.0, 0.0), 90.0, 3);
|
||||
let join = &out.subpaths[0].anchors[1];
|
||||
if offset == 64.0 {
|
||||
assert_eq!(join.h_out, Point::new(p.x + 6.0, 0.0));
|
||||
let actual = join.h_in - join.p;
|
||||
let expected = vectorcraft_geom::Vec2::new(-1.0, std::f64::consts::FRAC_PI_2);
|
||||
let error = actual.cross(expected).atan2(actual.dot(expected)).abs();
|
||||
assert!(error < 1e-9, "{actual:?}");
|
||||
} else {
|
||||
let incoming = join.p - join.h_in;
|
||||
let outgoing = join.h_out - join.p;
|
||||
assert!(incoming.cross(outgoing).abs() < 1e-9, "{incoming:?} {outgoing:?}");
|
||||
assert!(incoming.dot(outgoing) > 0.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twist_fractional_boundary_handles_use_each_sides_derivative() {
|
||||
use vectorcraft_geom::Anchor;
|
||||
|
||||
for (bounds, step) in [(Rect::new(40.1, 30.0, 60.3, 30.0), 1.0), (Rect::new(-60.3, 30.0, -40.1, 30.0), 1.0)] {
|
||||
for x in [bounds.x0, bounds.x1] {
|
||||
for angle in [90.0_f64, 400.0, -720.0] {
|
||||
let p = Point::new(x, 30.0);
|
||||
let line = PathData::single(SubPath::new(
|
||||
vec![
|
||||
Anchor::corner(Point::new(x - 3.0 * step, 30.0)),
|
||||
Anchor::with_handles(p, Point::new(x - step, 30.0), Point::new(x + step, 30.0)),
|
||||
Anchor::corner(Point::new(x + 3.0 * step, 30.0)),
|
||||
],
|
||||
false,
|
||||
));
|
||||
let out = crate::distort::twist(&line, bounds, angle, 3);
|
||||
assert!(
|
||||
out.subpaths[0].anchors.iter().all(|a| a.p.is_finite() && a.h_in.is_finite() && a.h_out.is_finite()),
|
||||
"x={x} angle={angle}: {:?}",
|
||||
out.subpaths[0].anchors
|
||||
);
|
||||
let join = &out.subpaths[0].anchors[1];
|
||||
assert!(join.p.distance(p) < 1e-10, "{:?}", join.p);
|
||||
let a = angle.to_radians();
|
||||
let expected = if x == bounds.x0 { [(-1.0, 0.0), (1.0, -a)] } else { [(-1.0, a), (1.0, 0.0)] };
|
||||
for (actual, (dx, dy)) in [join.h_in - join.p, join.h_out - join.p].into_iter().zip(expected) {
|
||||
let expected = vectorcraft_geom::Vec2::new(dx, dy);
|
||||
let error = actual.cross(expected).atan2(actual.dot(expected)).abs();
|
||||
assert!(error < 1e-9, "x={x} angle={angle}: {actual:?}, join={join:?}, source={:?}", line.subpaths[0].anchors[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let bounds = Rect::new(1e16, 30.0, 1e16 + 18.0, 30.0);
|
||||
let center = bounds.center();
|
||||
let line = PathData::single(SubPath::polyline(&[Point::new(center.x - 4.0, center.y), center, Point::new(center.x + 4.0, center.y)], false));
|
||||
let out = crate::distort::twist(&line, bounds, 90.0, 3);
|
||||
assert_eq!(out.subpaths[0].anchors[1].p, center);
|
||||
let bounds = Rect::new(0.0, 30.0, 2e16, 30.0);
|
||||
let p = Point::new(bounds.x1 - 8.0, 30.0);
|
||||
let line = PathData::single(SubPath::new(
|
||||
vec![
|
||||
Anchor::corner(Point::new(p.x - 12.0, p.y)),
|
||||
Anchor::with_handles(p, Point::new(p.x - 4.0, p.y), Point::new(p.x + 4.0, p.y)),
|
||||
Anchor::corner(Point::new(p.x + 12.0, p.y)),
|
||||
],
|
||||
false,
|
||||
));
|
||||
let out = crate::distort::twist(&line, bounds, 90.0, 3);
|
||||
let join = &out.subpaths[0].anchors[1];
|
||||
assert!(join.p.y - p.y > 10.0, "{:?}", join.p);
|
||||
assert!(join.h_out.y - join.p.y < -4.0, "{join:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn twist_fixes_the_outer_circle_and_rotates_inside() {
|
||||
let out = run("distort.twist", json!({"angle": 0}), &square());
|
||||
|
||||
@@ -71,7 +71,7 @@ pub fn seg_cubic(sp: &SubPath, i: usize) -> CubicBez {
|
||||
|
||||
/// Non-linear path mapping (shared with live envelopes in `vectorcraft-doc`), and the split of
|
||||
/// segments into pieces within an anchor budget.
|
||||
pub use vectorcraft_doc::live::{fit_piece, map_nonlinear_within, map_smooth_within, pieces};
|
||||
pub use vectorcraft_doc::live::{fit_piece, map_handles_within, map_nonlinear_within, pieces};
|
||||
|
||||
/// About how many lines the renderer flattens `path` into at 1 px/pt: one for a straight
|
||||
/// segment, and for a curve the count Wang's formula gives at vello's tolerance of 0.25 px,
|
||||
|
||||
@@ -839,6 +839,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`,
|
||||
|
||||
@@ -591,6 +591,7 @@ since 2026-10-01.
|
||||
## Evidence: what ships today, by area
|
||||
|
||||
- `object.setBounds` uses the same reference point for whole-number integer and floating-point values, including `0` and `0.0`. It rejects negative and fractional indices without changing geometry, selection, or undo history. An omitted or null reference uses the center, and whole-number references above 8 use point 8. Object and direct-anchor selections follow these rules (`tests::bboxrotate::set_bounds_whole_number_references_match_integer_references`, `set_bounds_rejects_invalid_references_without_changes`).
|
||||
- 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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user