mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-23 02:44:24 +01:00
3D: the bevels file stops cutting a path once it passes the point limit
A bevel's path is cut into straight lines one segment at a time, and the reader stops at the segment that passes 8,192 points, so a detailed symbol is skipped without cutting the rest of its path.
This commit is contained in:
1 parent
0e74240620
commit
dc956060f8
2 files changed
+40
-7
No files matched your search
@@ -145,14 +145,25 @@ fn points_of(n: &Node) -> std::result::Result<Vec<(f64, f64)>, &'static str> {
|
||||
return Err("its path has too many anchors for a bevel");
|
||||
}
|
||||
let mut line: Vec<Point> = vec![];
|
||||
let mut too_many = false;
|
||||
let tolerance = path.bounds().map_or(1.0, |b| b.width().hypot(b.height()) / 2000.0).max(1e-9);
|
||||
vectorcraft_geom::flatten(path.to_bezpath().elements().iter().copied(), tolerance, |el| match el {
|
||||
PathEl::MoveTo(p) | PathEl::LineTo(p) if line.len() < MAX_LINE_POINTS => line.push(p),
|
||||
PathEl::MoveTo(_) | PathEl::LineTo(_) => too_many = true,
|
||||
_ => {}
|
||||
});
|
||||
if too_many {
|
||||
// One segment at a time, so a path past the limit stops being cut at the segment that passes it.
|
||||
let mut at: Option<Point> = None;
|
||||
for &el in path.to_bezpath().elements() {
|
||||
if line.len() > MAX_LINE_POINTS {
|
||||
return Err("its path is too detailed for a bevel");
|
||||
}
|
||||
match (el, at) {
|
||||
(PathEl::MoveTo(p), _) => line.push(p),
|
||||
(_, Some(start)) => vectorcraft_geom::flatten([PathEl::MoveTo(start), el], tolerance, |e| {
|
||||
if let PathEl::LineTo(p) = e {
|
||||
line.push(p);
|
||||
}
|
||||
}),
|
||||
(_, None) => {}
|
||||
}
|
||||
at = el.end_point().or(at);
|
||||
}
|
||||
if line.len() > MAX_LINE_POINTS {
|
||||
return Err("its path is too detailed for a bevel");
|
||||
}
|
||||
let (Some(&first), Some(&last)) = (line.first(), line.last()) else { return Err("its path has no length") };
|
||||
|
||||
@@ -102,6 +102,28 @@ fn curves_and_long_paths_fit_in_the_points_a_bevel_holds() {
|
||||
assert!(arc.points.iter().all(|&(x, y)| (((x - 0.0).powi(2) + (y - 1.0).powi(2)).sqrt() - 1.0).abs() < 0.01), "{:?}", arc.points);
|
||||
}
|
||||
|
||||
/// A path of many tall curves, cut into more straight lines than a bevel's path may take, is
|
||||
/// skipped as too detailed.
|
||||
#[test]
|
||||
fn a_path_cut_into_too_many_lines_is_skipped() {
|
||||
let mut d = Document::new(100.0, 100.0);
|
||||
let anchors: Vec<Anchor> = (0..4000)
|
||||
.map(|i| {
|
||||
let p = Point::new(f64::from(i), f64::from(i) / 4.0);
|
||||
let swing = if i % 2 == 0 { 1000.0 } else { -1000.0 };
|
||||
let mut a = Anchor::corner(p);
|
||||
a.h_in = Point::new(p.x - 0.5, p.y - swing);
|
||||
a.h_out = Point::new(p.x + 0.5, p.y + swing);
|
||||
a
|
||||
})
|
||||
.collect();
|
||||
let wild = Node::path(NodeId(0), PathData::single(SubPath::new(anchors, false)), Appearance::default());
|
||||
add_symbol(&mut d, "Wild", wild);
|
||||
let read = bevels_of(&d);
|
||||
assert!(read.bevels.is_empty());
|
||||
assert_eq!(read.skipped, [("Wild".to_string(), "its path is too detailed for a bevel".to_string())]);
|
||||
}
|
||||
|
||||
fn rect(s: &mut Session, x: u32) -> NodeId {
|
||||
NodeId(s.execute("shape.rectangle", &json!({"x": x, "y": 100, "width": 60, "height": 60})).unwrap()["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user