mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 02:35:37 +01:00
All 40 outputs keep their shape, subpath and anchor counts. Their areas and lengths agree with the earlier pins to better than 1e-6 relative; only the last digits of the coordinates changed.
126 lines
4.5 KiB
Rust
126 lines
4.5 KiB
Rust
//! The exact output of every Pathfinder operation, pinned so that work on the windings and the
|
|
//! coverage tests can't change it: overlapping ellipses, rectangles and stars (even-odd and
|
|
//! non-zero, repeated keys for Merge) at a few counts.
|
|
|
|
use vectorcraft_geom::{FillRule, PathData, Point, Rect, SubPath, shapes};
|
|
use vectorcraft_pathops::{PathfinderOp, Shape, pathfinder};
|
|
|
|
fn shapes_of(n: usize, seed: u64) -> Vec<Shape> {
|
|
let mut r = seed | 1;
|
|
let mut next = || {
|
|
r ^= r << 13;
|
|
r ^= r >> 7;
|
|
r ^= r << 17;
|
|
(r % 1_000_000) as f64 / 1_000_000.0
|
|
};
|
|
(0..n)
|
|
.map(|i| {
|
|
let (x, y, s) = (next() * 200.0, next() * 150.0, 15.0 + next() * 40.0);
|
|
let path = match i % 3 {
|
|
0 => shapes::ellipse(Rect::from_center_size(Point::new(x, y), (2.0 * s, 1.4 * s))),
|
|
1 => shapes::rectangle(Rect::new(x, y, x + 1.5 * s, y + s)),
|
|
_ => star(Point::new(x, y), s, s / 2.0),
|
|
};
|
|
let rule = if i % 4 == 3 { FillRule::EvenOdd } else { FillRule::NonZero };
|
|
Shape::new(path, rule, (i % 5) as u64)
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// Builds a star as `shapes::star(center, r1, r2, 5, 0.3)` does, with the cosines and sines read
|
|
/// from `STAR_UNIT_VECTORS`.
|
|
fn star(center: Point, r1: f64, r2: f64) -> PathData {
|
|
let pts: Vec<Point> = STAR_UNIT_VECTORS
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(i, &(cos, sin))| {
|
|
let r = if i % 2 == 0 { r1 } else { r2 };
|
|
Point::new(center.x + r * f64::from_bits(cos), center.y + r * f64::from_bits(sin))
|
|
})
|
|
.collect();
|
|
PathData::single(SubPath::polyline(&pts, true))
|
|
}
|
|
|
|
/// Each row holds the correctly rounded cosine and sine, as bits, of one of the 10 angles that
|
|
/// `shapes::star` computes for 5 points and a rotation of 0.3°. A platform's libm can differ from
|
|
/// them in the last bit.
|
|
const STAR_UNIT_VECTORS: [(u64, u64); 10] = [
|
|
(0x3f75_724e_56b4_2a6c, 0xbfef_ffe3_40b4_d4f6),
|
|
(0x3fe2_f1c5_9f37_cbf7, 0xbfe9_ca2a_1be5_b900),
|
|
(0x3fee_7c33_ee44_9408, 0xbfd3_7547_1564_f9f7),
|
|
(0x3fee_61b1_8a0e_c1cb, 0x3fd4_1873_d064_86bd),
|
|
(0x3fe2_ac5e_9e54_9166, 0x3fe9_fc96_97b3_dc51),
|
|
(0xbf75_724e_56b4_29df, 0x3fef_ffe3_40b4_d4f6),
|
|
(0xbfe2_f1c5_9f37_cbf5, 0x3fe9_ca2a_1be5_b901),
|
|
(0xbfee_7c33_ee44_9407, 0x3fd3_7547_1564_f9fd),
|
|
(0xbfee_61b1_8a0e_c1cc, 0xbfd4_1873_d064_86b7),
|
|
(0xbfe2_ac5e_9e54_9169, 0xbfe9_fc96_97b3_dc4f),
|
|
];
|
|
|
|
fn fnv(s: &str) -> u64 {
|
|
s.bytes().fold(0xcbf2_9ce4_8422_2325, |h, b| (h ^ u64::from(b)).wrapping_mul(0x100_0000_01b3))
|
|
}
|
|
|
|
#[test]
|
|
fn every_pathfinder_operation_is_pinned() {
|
|
use PathfinderOp::*;
|
|
let ops = [Unite, MinusFront, MinusBack, Intersect, Exclude, Divide, Trim, Merge, Crop, Outline];
|
|
let mut got = vec![];
|
|
for (n, seed) in [(2, 3), (5, 7), (12, 11), (30, 13)] {
|
|
let input = shapes_of(n, seed);
|
|
for op in ops {
|
|
let out = pathfinder(op, &input);
|
|
let text: String = out.iter().map(|s| format!("{}|{:?}|{:?};", s.key, s.rule, s.path)).collect();
|
|
got.push(fnv(&text));
|
|
}
|
|
}
|
|
assert_eq!(got.len(), PINNED.len());
|
|
assert_eq!(got, PINNED);
|
|
}
|
|
|
|
/// Computed with the sweep of craft-libs 610c86792a, which places output curves closer to their
|
|
/// inputs than the one before (craft-libs#721): every output keeps its shapes, subpaths and
|
|
/// anchors, and its area and length agree with the earlier pins to 1e-6 relative.
|
|
const PINNED: [u64; 40] = [
|
|
0x955c_8b2d_f025_1d4a,
|
|
0x7f0e_0ba6_f322_01d3,
|
|
0xe7b6_dfcc_7d22_84b0,
|
|
0xe65f_7d83_0896_6cb0,
|
|
0xf077_5d80_f61c_2011,
|
|
0x9e74_9802_4395_ea61,
|
|
0x58d2_be7a_b723_a56e,
|
|
0x58d2_be7a_b723_a56e,
|
|
0x5959_96b9_2bf6_4d87,
|
|
0xc194_6236_14c2_0591,
|
|
0xb633_97b1_90e6_a7f8,
|
|
0x3a40_3bb7_87e3_a9d2,
|
|
0x1a11_084e_721e_ac3b,
|
|
0xcbf2_9ce4_8422_2325,
|
|
0x5302_5520_518c_4b14,
|
|
0x6ab1_e2ae_913c_fd02,
|
|
0x1d33_b251_5b9e_6034,
|
|
0x1d33_b251_5b9e_6034,
|
|
0xcbf2_9ce4_8422_2325,
|
|
0x2c80_b697_1843_0971,
|
|
0x2a36_ff1a_6452_6645,
|
|
0xcc2d_7af8_e01f_e960,
|
|
0x3d9f_67a5_a5bd_ee09,
|
|
0xcbf2_9ce4_8422_2325,
|
|
0x3396_f715_a71b_b1e2,
|
|
0xd1bd_dbfc_4e57_80ac,
|
|
0x15a1_d47a_be5a_dc72,
|
|
0x2548_d208_a79c_4974,
|
|
0x2088_06d5_a0a5_d65e,
|
|
0x597f_2517_c677_5429,
|
|
0x25d0_bc2c_70e1_c3a4,
|
|
0xcbf2_9ce4_8422_2325,
|
|
0xeb6a_6614_cea5_a508,
|
|
0xcbf2_9ce4_8422_2325,
|
|
0x81f7_7a26_7885_0bc5,
|
|
0xfb2d_d20c_1930_44c7,
|
|
0xcd17_209c_b57f_208d,
|
|
0x8fc2_2057_7075_76b4,
|
|
0x59dc_9268_a73f_e92e,
|
|
0x5454_509c_877a_852b,
|
|
];
|