mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-03 20:43:18 -05:00
Merge pull request #10443 from dotCooCoo/fix/test-tolerances-geom
Tests: tighten loose tolerances in geom, pathops, trace and three-d
This commit is contained in:
4 files changed
+70
-5
No files matched your search
@@ -1382,7 +1382,12 @@ mod tests {
|
||||
"{n}: {b:?}"
|
||||
);
|
||||
let area = p.subpaths[0].area().abs();
|
||||
assert!((area - PI * 50.0 * 30.0).abs() / area < if n == 2 { 0.1 } else { 0.01 }, "{n}: {area}");
|
||||
if n == 2 {
|
||||
// Two cubics with handles 4/3 of the half height long: each encloses 2/5 of the box.
|
||||
assert!((area - 0.8 * 100.0 * 60.0).abs() < 1e-8, "{n}: {area}");
|
||||
} else {
|
||||
assert!((area - PI * 50.0 * 30.0).abs() / area < 0.01, "{n}: {area}");
|
||||
}
|
||||
}
|
||||
// A box with no height leaves the path flat rather than dividing by zero.
|
||||
let flat = ellipse_with_anchors(Rect::new(0.0, 5.0, 40.0, 5.0), 6);
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
//! 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.
|
||||
//! non-zero, repeated keys for Merge) at a few counts. The filled regions of Unite, Minus Front,
|
||||
//! Minus Back, Intersect and Exclude on the same inputs are also checked point by point against the
|
||||
//! inputs' own coverage, so the pins do not rest on the sweep that computed them alone.
|
||||
|
||||
use kurbo::{BezPath, Shape as _};
|
||||
use vectorcraft_geom::{FillRule, PathData, Point, Rect, SubPath, shapes};
|
||||
use vectorcraft_pathops::{PathfinderOp, Shape, pathfinder};
|
||||
|
||||
@@ -78,6 +81,62 @@ fn every_pathfinder_operation_is_pinned() {
|
||||
assert_eq!(got, PINNED);
|
||||
}
|
||||
|
||||
/// Whether an operation's output covers a point, from which inputs cover it.
|
||||
type CoverRule = fn(&[bool]) -> bool;
|
||||
|
||||
/// Shapes as kurbo paths with their fill rules.
|
||||
type Filled = Vec<(BezPath, FillRule)>;
|
||||
|
||||
fn inside(path: &BezPath, rule: FillRule, p: kurbo::Point) -> bool {
|
||||
let w = path.winding(p);
|
||||
match rule {
|
||||
FillRule::NonZero => w != 0,
|
||||
FillRule::EvenOdd => w % 2 != 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_pinned_inputs_fill_what_their_coverage_says() {
|
||||
use PathfinderOp::*;
|
||||
// Which inputs cover a point decides whether each operation's output covers it.
|
||||
let rules: [(PathfinderOp, CoverRule); 5] = [
|
||||
(Unite, |m| m.iter().any(|&c| c)),
|
||||
(MinusFront, |m| m[0] && !m[1..].iter().any(|&c| c)),
|
||||
(MinusBack, |m| m[m.len() - 1] && !m[..m.len() - 1].iter().any(|&c| c)),
|
||||
(Intersect, |m| m.iter().all(|&c| c)),
|
||||
(Exclude, |m| m.iter().filter(|&&c| c).count() % 2 == 1),
|
||||
];
|
||||
for (n, seed) in [(2, 3), (5, 7), (12, 11), (30, 13)] {
|
||||
let input = shapes_of(n, seed);
|
||||
let paths: Filled = input.iter().map(|s| (s.path.to_bezpath(), s.rule)).collect();
|
||||
let cover = |p: kurbo::Point| paths.iter().map(|(b, r)| inside(b, *r, p)).collect::<Vec<bool>>();
|
||||
let outputs: Vec<(PathfinderOp, CoverRule, Filled)> =
|
||||
rules.iter().map(|&(op, rule)| (op, rule, pathfinder(op, &input).iter().map(|s| (s.path.to_bezpath(), s.rule)).collect())).collect();
|
||||
let (mut checked, mut filled) = (0, 0);
|
||||
// A grid whose pitch shares no factor with the inputs' coordinates; points within 0.25 pt
|
||||
// of an input edge are skipped, since the output's edge may sit there within the precision.
|
||||
for i in 0..200 {
|
||||
for j in 0..160 {
|
||||
let p = kurbo::Point::new(-60.0 + f64::from(i) * 1.37, -60.0 + f64::from(j) * 1.37);
|
||||
let m = cover(p);
|
||||
let near_edge = [(0.25, 0.25), (-0.25, 0.25), (0.25, -0.25), (-0.25, -0.25)]
|
||||
.iter()
|
||||
.any(|&(dx, dy)| cover(kurbo::Point::new(p.x + dx, p.y + dy)) != m);
|
||||
if near_edge {
|
||||
continue;
|
||||
}
|
||||
checked += 1;
|
||||
for (op, rule, out) in &outputs {
|
||||
let got = out.iter().any(|(b, r)| inside(b, *r, p));
|
||||
assert_eq!(got, rule(&m), "{op:?} n={n} at {p:?}, covered by {m:?}");
|
||||
filled += usize::from(got);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(checked > 10_000 && filled > 1_000, "n={n}: {checked} points, {filled} filled");
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
|
||||
@@ -49,9 +49,10 @@ fn a_light_straight_in_front_casts_the_silhouette_behind_the_object() {
|
||||
for (got, want) in [(b.x0, 100.0), (b.y0, 100.0), (b.x1, 200.0), (b.y1, 200.0)] {
|
||||
assert!((got - want).abs() < 1e-6, "{b:?}");
|
||||
}
|
||||
// Every face of the box projects into the square, and the nonzero union fills it whole.
|
||||
// Every face of the box projects into the square, and the nonzero union fills it whole. Each
|
||||
// sample stands for 1 pt² and none lies on the square's edge, so the count is exact.
|
||||
let area = covered(&path, Rect::new(90.0, 90.0, 210.0, 210.0), 120);
|
||||
assert!((area - 10_000.0).abs() < 300.0, "{area}");
|
||||
assert_eq!(area, 10_000.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -311,7 +311,7 @@ mod tests {
|
||||
assert!(keys(seam).iter().all(|k| lower.contains(k) && upper.contains(k)));
|
||||
assert!(seam.path.anchor_count() <= res.anchor_count());
|
||||
for a in seam.path.subpaths.iter().flat_map(|sp| &sp.anchors) {
|
||||
assert!((a.p.x - 10.4).abs() < 0.5, "{a:?}");
|
||||
assert!((a.p.x - 10.4).abs() < 0.05, "{a:?}");
|
||||
}
|
||||
let ys: Vec<f64> = seam.path.subpaths.iter().flat_map(|sp| sp.anchors.iter().map(|a| a.p.y)).collect();
|
||||
assert_eq!((ys.iter().copied().fold(f64::INFINITY, f64::min), ys.iter().copied().fold(f64::NEG_INFINITY, f64::max)), (0.0, 16.0));
|
||||
|
||||
Reference in new issue
Block a user