mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 12:27:37 +00:00
Twist: keep fractional boundary tangents accurate
This commit is contained in:
@@ -564,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);
|
||||
}
|
||||
@@ -629,6 +630,17 @@ pub(super) fn resample(poly: &[Point], m: usize) -> Vec<Point> {
|
||||
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);
|
||||
|
||||
@@ -336,11 +336,26 @@ 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)
|
||||
};
|
||||
@@ -354,8 +369,9 @@ pub fn twist(path: &PathData, b: Rect, angle: f64, max: usize) -> PathData {
|
||||
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 gradient = if length < r || (length == r && radial < 0.0) { -ang / r * radial } else { 0.0 };
|
||||
let tangent = v + Vec2::new(-d.y, d.x) * gradient;
|
||||
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)
|
||||
|
||||
@@ -1762,6 +1762,61 @@ fn twist_large_coordinate_joins_preserve_boundary_sides() {
|
||||
}
|
||||
}
|
||||
|
||||
#[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());
|
||||
|
||||
Reference in New Issue
Block a user