* claim: #8155 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019FAWPuFoh9qxn2uS2j53QZ * DXF import: an elliptical arc comes in as exact conic arcs (#8155) A partial ELLIPSE (how Fusion, and SolidCraft's own DXF export, write an elliptical arc) was read as a degree-3 spline fitted through 16 samples, which strays from the ellipse between them: 0.84 mm on a 300-degree arc of a 200 x 40 mm ellipse. It is now read as exact rational-quadratic conic pieces of at most a quarter turn each, which the sketch keeps as conics. A conic arc written by SolidCraft's DXF export now reads back as the same conic. The arc's sweep is taken with rem_euclid instead of a loop that added a turn at a time, which ran for billions of turns on a start angle of 1e11. sketch.drawing_bounds and the width/height sizing of sketch.insert_dxf measure a conic by points along it, not by its apex, which can lie outside the curve. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019FAWPuFoh9qxn2uS2j53QZ --------- Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -152,6 +152,12 @@ fn bounds_of(geom: &[Geom2]) -> Option<(Vec2, Vec2)> {
|
||||
pts.extend([*c + Vec2::new(r, r), *c - Vec2::new(r, r)]);
|
||||
}
|
||||
Geom2::Spline { pts: sp, .. } => pts.extend(sp.iter().copied()),
|
||||
// Points along the curve, not its apex, which can lie well outside it (an elliptical
|
||||
// arc of a DXF comes in as conics, #8155).
|
||||
Geom2::Conic { a, apex, b, rho } if *rho > 0.0 && *rho < 1.0 => {
|
||||
let seg = solidcraft_geom::Seg2::Conic { a: *a, apex: *apex, b: *b, w: rho / (1.0 - rho) };
|
||||
pts.extend((0..=64).map(|i| seg.point_at(f64::from(i) / 64.0)));
|
||||
}
|
||||
Geom2::Conic { a, apex, b, .. } => pts.extend([*a, *apex, *b]),
|
||||
Geom2::Text { at, .. } => pts.push(*at),
|
||||
}
|
||||
@@ -352,6 +358,32 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// #8155: an elliptical arc of a DXF (as Fusion writes one) comes into the sketch as exact
|
||||
/// conics, and the drawing's size is the arc's own, not the box round the conics' apexes.
|
||||
#[test]
|
||||
fn an_elliptical_arc_comes_in_as_conics_at_its_own_size() {
|
||||
// Centre (0, 0), major radius 10 along X, minor 5, from -45° to 45°: one quarter turn,
|
||||
// whose apex is at x = 10 / cos 45° = 14.1, past the arc's end at x = 10.
|
||||
let dxf = format!(
|
||||
"0\nSECTION\n2\nENTITIES\n0\nELLIPSE\n10\n0\n20\n0\n11\n10\n21\n0\n40\n0.5\n41\n{}\n42\n{}\n0\nENDSEC\n0\nEOF\n",
|
||||
-std::f64::consts::FRAC_PI_4,
|
||||
std::f64::consts::FRAC_PI_4
|
||||
);
|
||||
let mut s = Session::default();
|
||||
let b = s.execute("sketch.drawing_bounds", &json!({"text": dxf, "format": "dxf"})).unwrap();
|
||||
let (w, h) = (b["width"].as_f64().unwrap(), b["height"].as_f64().unwrap());
|
||||
let half = std::f64::consts::FRAC_1_SQRT_2;
|
||||
assert!((w - 10.0 * (1.0 - half)).abs() < 1e-3 && (h - 10.0 * half).abs() < 1e-3, "{b}");
|
||||
let r = s.execute("sketch.insert_dxf", &json!({"text": dxf})).unwrap();
|
||||
assert_eq!(r["curves"], 1, "{r}");
|
||||
let st = s.model.state();
|
||||
let sk = &st.sketch(r["sketch"].as_u64().unwrap()).unwrap().sketch;
|
||||
assert!(matches!(sk.curves[0].kind, solidcraft_sketch::CurveKind::Conic { .. }), "{:?}", sk.curves[0].kind);
|
||||
// Every point the sketch draws it with lies on the ellipse.
|
||||
let worst = sk.polyline(0).iter().map(|p| ((p.x / 10.0).powi(2) + (p.y / 5.0).powi(2) - 1.0).abs()).fold(0.0, f64::max);
|
||||
assert!(worst < 1e-9, "{worst}");
|
||||
}
|
||||
|
||||
const PLATE: &str =
|
||||
r#"<svg xmlns="http://www.w3.org/2000/svg" width="20mm" height="10mm" viewBox="0 0 20 10"><rect x="0" y="0" width="20" height="10"/></svg>"#;
|
||||
|
||||
|
||||
+76
-13
@@ -243,24 +243,31 @@ fn convert(ents: &[Ent], k: f64, blocks: &HashMap<String, (Vec2, Vec<Ent>)>, dep
|
||||
let major = c + Vec2::new(mx, my) * k;
|
||||
let a = Vec2::new(mx, my).len() * k;
|
||||
let (t0, t1) = (e.get(41).unwrap_or(0.0), e.get(42).unwrap_or(std::f64::consts::TAU));
|
||||
if (t1 - t0 - std::f64::consts::TAU).abs() < 1e-9 || (t1 - t0).abs() < 1e-12 {
|
||||
// The arc runs counter-clockwise from t0 to t1, a turn at most; whole turns
|
||||
// more or less make no difference.
|
||||
let sweep = (t1 - t0).rem_euclid(std::f64::consts::TAU);
|
||||
if sweep < 1e-9 || std::f64::consts::TAU - sweep < 1e-9 {
|
||||
out.push(Geom2::Ellipse { c, major, minor: a * ratio });
|
||||
} else {
|
||||
// Partial ellipse: a fit spline through samples.
|
||||
// Partial ellipse (an elliptical arc, as Fusion and SolidCraft's own DXF
|
||||
// export write one): exact conic pieces of at most a quarter turn each, not
|
||||
// a spline fitted through samples, which strays from the ellipse between
|
||||
// them (#8155). A piece from s to e is the rational quadratic with its apex
|
||||
// where the end tangents meet and the middle weight cos((e - s) / 2), the
|
||||
// ellipse's image of a circle's arc; its rho is weight / (1 + weight).
|
||||
let u = (major - c) / a.max(1e-12);
|
||||
let v = u.perp() * (a * ratio);
|
||||
let mut sweep = t1 - t0;
|
||||
while sweep <= 0.0 {
|
||||
sweep += std::f64::consts::TAU;
|
||||
let at = |t: f64| c + u * (a * t.cos()) + v * t.sin();
|
||||
// (A quarter turn written out is a hair over or under one; it stays one piece.)
|
||||
let pieces = (sweep / std::f64::consts::FRAC_PI_2 - 1e-9).ceil().clamp(1.0, 4.0) as usize;
|
||||
let step = sweep / pieces as f64;
|
||||
let w = (step / 2.0).cos();
|
||||
for j in 0..pieces {
|
||||
let (from, to) = (t0 + step * j as f64, t0 + step * (j + 1) as f64);
|
||||
let mid = (from + to) / 2.0;
|
||||
let apex = c + (u * (a * mid.cos()) + v * mid.sin()) / w;
|
||||
out.push(Geom2::Conic { a: at(from), apex, b: at(to), rho: w / (1.0 + w) });
|
||||
}
|
||||
let n = 16;
|
||||
let pts = (0..=n)
|
||||
.map(|j| {
|
||||
let t = t0 + sweep * j as f64 / n as f64;
|
||||
c + u * (a * t.cos()) + v * t.sin()
|
||||
})
|
||||
.collect();
|
||||
out.push(Geom2::Spline { pts, control: false, degree: 3 });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -481,6 +488,10 @@ mod tests {
|
||||
format!("0\nSECTION\n2\nBLOCKS\n{}0\nENDSEC\n0\nEOF\n", "0\nBLOCK\n2\nb\n0\nENDBLK\n".repeat(300)),
|
||||
"0\nSECTION\n2\nBLOCKS\n0\nBLOCK\n2\na\n1\nINSERT\n2\na\n0\nENDBLK\n0\nENDSEC\n0\nEOF\n".into(),
|
||||
format!("0\nSECTION\n2\nENTITIES\n{}0\nENDSEC\n0\nEOF\n", "0\nLINE\n10\n0\n20\n0\n11\n1\n21\n1\n".repeat(5000)),
|
||||
// An ELLIPSE arc ending far before it starts (once counted up a turn at a time) and
|
||||
// one with no length to its axes.
|
||||
"0\nSECTION\n2\nENTITIES\n0\nELLIPSE\n10\n0\n20\n0\n11\n5\n21\n0\n40\n0.5\n41\n99999999999\n42\n0\n0\nENDSEC\n0\nEOF\n".into(),
|
||||
"0\nSECTION\n2\nENTITIES\n0\nELLIPSE\n10\n0\n20\n0\n11\n0\n21\n0\n40\n0\n41\n1\n42\n2\n0\nENDSEC\n0\nEOF\n".into(),
|
||||
];
|
||||
for cut in (0..good.len()).step_by(11) {
|
||||
cases.push(good.get(..cut).unwrap_or("").to_string());
|
||||
@@ -494,6 +505,58 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// #8155: an ELLIPSE arc (as Fusion writes one) reads as conic pieces of at most a quarter turn,
|
||||
/// end to end from the arc's start to its end, that lie on the ellipse all along, not as a
|
||||
/// spline fitted through samples. A whole turn stays one ellipse, and whole turns more or less
|
||||
/// make no difference.
|
||||
#[test]
|
||||
fn an_elliptical_arc_reads_as_exact_conic_pieces() {
|
||||
use std::f64::consts::TAU;
|
||||
// Centre (5, 2), major radius 10 along 30°, minor radius 4 (ratio 0.4).
|
||||
let (c, u) = (Vec2::new(5.0, 2.0), Vec2::new(30f64.to_radians().cos(), 30f64.to_radians().sin()));
|
||||
let ellipse = |t0: f64, t1: f64| {
|
||||
format!(
|
||||
"0\nSECTION\n2\nENTITIES\n0\nELLIPSE\n10\n5\n20\n2\n11\n{}\n21\n{}\n40\n0.4\n41\n{t0}\n42\n{t1}\n0\nENDSEC\n0\nEOF\n",
|
||||
10.0 * u.x,
|
||||
10.0 * u.y
|
||||
)
|
||||
};
|
||||
let at = |t: f64| c + u * (10.0 * t.cos()) + u.perp() * (4.0 * t.sin());
|
||||
// How far off the ellipse a point is, in its own equation.
|
||||
let off = |p: Vec2| {
|
||||
let q = p - c;
|
||||
((q.dot(u) / 10.0).powi(2) + (q.dot(u.perp()) / 4.0).powi(2) - 1.0).abs()
|
||||
};
|
||||
// A point of the rational quadratic a sketch conic is, at t in 0..1.
|
||||
let point = |a: Vec2, apex: Vec2, b: Vec2, rho: f64, t: f64| {
|
||||
let w = rho / (1.0 - rho);
|
||||
let (k0, k1, k2) = ((1.0 - t) * (1.0 - t), 2.0 * t * (1.0 - t) * w, t * t);
|
||||
(a * k0 + apex * k1 + b * k2) / (k0 + k1 + k2)
|
||||
};
|
||||
for (t0, t1, pieces) in [(20f64, 250f64, 3), (0.0, 90.0, 1), (300.0, 40.0, 2), (10.0, 359.0, 4)] {
|
||||
let (t0, t1) = (t0.to_radians(), t1.to_radians());
|
||||
let back = read_dxf(&ellipse(t0, t1)).unwrap();
|
||||
assert_eq!(back.len(), pieces, "{back:?}");
|
||||
let mut worst = 0f64;
|
||||
let mut from = at(t0);
|
||||
for g in &back {
|
||||
let Geom2::Conic { a, apex, b, rho } = g else { panic!("not a conic: {g:?}") };
|
||||
assert!(a.dist(from) < 1e-9 && *rho > 0.0 && *rho < 0.5, "{g:?}");
|
||||
for k in 0..=50 {
|
||||
worst = worst.max(off(point(*a, *apex, *b, *rho, f64::from(k) / 50.0)));
|
||||
}
|
||||
from = *b;
|
||||
}
|
||||
assert!(from.dist(at(t1)) < 1e-9 && worst < 1e-9, "{worst}: {back:?}");
|
||||
}
|
||||
for (t0, t1) in [(0.0, TAU), (1.0, 1.0), (1.0, 1.0 + 2.0 * TAU)] {
|
||||
let back = read_dxf(&ellipse(t0, t1)).unwrap();
|
||||
assert!(matches!(&back[..], [Geom2::Ellipse { minor, .. }] if (minor - 4.0).abs() < 1e-9), "{t0}..{t1}: {back:?}");
|
||||
}
|
||||
let (once, again) = (read_dxf(&ellipse(1.0, 2.0)).unwrap(), read_dxf(&ellipse(1.0, 2.0 + 3.0 * TAU)).unwrap());
|
||||
assert_eq!(once.len(), again.len());
|
||||
}
|
||||
|
||||
/// A SPLINE keeps its degree up to the 9 a sketch spline (a G4 blend) has (solidcraft#5269); a
|
||||
/// degree past that is held to 9, and one that isn't a finite number (which the reader drops)
|
||||
/// reads as a missing one, 3.
|
||||
|
||||
@@ -786,13 +786,11 @@ mod tests {
|
||||
"{:?}",
|
||||
back[3]
|
||||
);
|
||||
// The reader makes an elliptical arc a spline through points of it: from (75, 40) to (50, 52).
|
||||
let Geom2::Spline { pts, control: false, .. } = &back[4] else { panic!("{:?}", back[4]) };
|
||||
let on_ellipse = |p: &Vec2| (((p.x - 50.0) / 25.0).powi(2) + ((p.y - 40.0) / 12.0).powi(2) - 1.0).abs() < 1e-9;
|
||||
assert!(
|
||||
pts.first().is_some_and(|p| p.dist(Vec2::new(75.0, 40.0)) < 1e-9) && pts.last().is_some_and(|p| p.dist(Vec2::new(50.0, 52.0)) < 1e-9)
|
||||
);
|
||||
assert!(pts.iter().all(on_ellipse));
|
||||
// The quarter of the ellipse comes back as the one conic it was: from (75, 40) to (50, 52)
|
||||
// with its apex at (75, 52) and its weight (#8155).
|
||||
let Geom2::Conic { a, apex, b, rho } = &back[4] else { panic!("{:?}", back[4]) };
|
||||
assert!(a.dist(Vec2::new(75.0, 40.0)) < 1e-9 && apex.dist(Vec2::new(75.0, 52.0)) < 1e-9 && b.dist(Vec2::new(50.0, 52.0)) < 1e-9);
|
||||
assert!((rho - FRAC_1_SQRT_2 / (1.0 + FRAC_1_SQRT_2)).abs() < 1e-12, "{rho}");
|
||||
// The cubic: degree and control points.
|
||||
let Geom2::Spline { pts, control: true, degree: 3 } = &back[5] else { panic!("{:?}", back[5]) };
|
||||
assert!(pts.iter().zip(&cubic).all(|(a, b)| a.dist(*b) < 1e-9) && pts.len() == 4);
|
||||
|
||||
Reference in New Issue
Block a user