wip(dxf): certify full round-wall outlines (#5318)
Targeted doc suite 390 pass/0 fail/1 existing ignore; sheet suite 198 pass/1 known trimmed-cap failure/1 existing ignore; view DXF 14 pass/0 fail and 53 original byte pins pass. Full CI and strict Clippy/wasm on this WIP are still pending. This commit preserves a reviewed partial improvement; do not mark PR #6184 ready.
This commit is contained in:
1 parent
0ff4438c32
commit
3fb2eef92d
1 file changed
+93
-1
@@ -121,6 +121,47 @@ pub(crate) fn outline_loop(pts: &[(Vec2, Option<u32>)], wall: &mut impl FnMut(u3
|
||||
Loop2 { segs: outline_segs(pts, wall, eps).into_iter().map(|(seg, _)| seg).collect() }
|
||||
}
|
||||
|
||||
/// The whole outline is one round wall only when every edge was admitted on
|
||||
/// the same cylindrical support by outline_segs. Recover the directed winding
|
||||
/// from those ordered points, rather than using a long sum of rounded angles.
|
||||
/// A monotone polar walk with winding +/-1 rejects retraces and multiple turns.
|
||||
/// This producer-owned certificate never loosens Loop2::as_circle's rules.
|
||||
fn certified_round_wall_turn(pts: &[(Vec2, Option<u32>)], center: Vec2) -> Option<f64> {
|
||||
if pts.len() < 3 || !center.is_finite() {
|
||||
return None;
|
||||
}
|
||||
let mut direction = 0i64;
|
||||
let mut winding = 0i64;
|
||||
for i in 0..pts.len() {
|
||||
let (Some(a), Some(b)) = (pts.get(i), pts.get((i + 1) % pts.len())) else {
|
||||
return None;
|
||||
};
|
||||
let (a, b) = (a.0 - center, b.0 - center);
|
||||
if !a.is_finite() || !b.is_finite() {
|
||||
return None;
|
||||
}
|
||||
let angle = (b.angle() - a.angle() + PI).rem_euclid(TAU) - PI;
|
||||
if !angle.is_finite() || angle == 0.0 || angle.abs() >= PI {
|
||||
return None;
|
||||
}
|
||||
let sign = if angle > 0.0 { 1 } else { -1 };
|
||||
if direction != 0 && direction != sign {
|
||||
return None;
|
||||
}
|
||||
direction = sign;
|
||||
let cross = a.cross(b);
|
||||
if !cross.is_finite() {
|
||||
return None;
|
||||
}
|
||||
if a.y <= 0.0 && b.y > 0.0 && cross > 0.0 {
|
||||
winding = winding.checked_add(1)?;
|
||||
} else if a.y > 0.0 && b.y <= 0.0 && cross < 0.0 {
|
||||
winding = winding.checked_sub(1)?;
|
||||
}
|
||||
}
|
||||
(direction != 0 && winding == direction).then_some(TAU * direction as f64)
|
||||
}
|
||||
|
||||
/// [`outline_loop`], each segment with its wall (an arc's: that of its first point).
|
||||
fn outline_segs(pts: &[(Vec2, Option<u32>)], wall: &mut impl FnMut(u32) -> Option<(Vec2, f64)>, eps: f64) -> Vec<(Seg2, Option<u32>)> {
|
||||
let m = pts.len();
|
||||
@@ -150,12 +191,20 @@ fn outline_segs(pts: &[(Vec2, Option<u32>)], wall: &mut impl FnMut(u32) -> Optio
|
||||
while j < m && same(start + j, (c, r)) {
|
||||
j += 1;
|
||||
}
|
||||
let sweep: f64 = (i..j)
|
||||
let summed_sweep: f64 = (i..j)
|
||||
.map(|s| {
|
||||
let d = (point(start + s + 1) - c).angle() - (point(start + s) - c).angle();
|
||||
(d + PI).rem_euclid(TAU) - PI
|
||||
})
|
||||
.sum();
|
||||
// When one admitted round-wall run covers every source edge, its ordered
|
||||
// points may prove one turn even if floating sums miss +/-TAU by a few ULP.
|
||||
// Partial and uncertified runs keep their originally computed sweep.
|
||||
let sweep = if i == 0 && j == m && c.is_finite() && r.is_finite() && r > 0.0 {
|
||||
certified_round_wall_turn(pts, c).unwrap_or(summed_sweep)
|
||||
} else {
|
||||
summed_sweep
|
||||
};
|
||||
// Arcs of up to half a turn each.
|
||||
let pieces = ((sweep.abs() / PI - 1e-9).ceil() as usize).clamp(1, 4);
|
||||
let (a0, step) = ((point(k) - c).angle(), sweep / pieces as f64);
|
||||
@@ -1042,6 +1091,49 @@ pub fn convert_body(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sampled_round_wall_turn_is_one_circle_without_widening_recognition() {
|
||||
let center = Vec2::new(80.0, 30.0);
|
||||
let radius = 8.0;
|
||||
let mut ring: Vec<(Vec2, Option<u32>)> = (0..128)
|
||||
.map(|i| {
|
||||
let angle = 10.0f64.to_radians() - TAU * i as f64 / 128.0;
|
||||
(center + Vec2::from_angle(angle) * radius, Some((i / 32) as u32))
|
||||
})
|
||||
.collect();
|
||||
// The seam and any of the four wall-face partitions must not matter.
|
||||
for shift in [0, 7, 31] {
|
||||
ring.rotate_left(shift);
|
||||
let loop2 = outline_loop(&ring, &mut |_id| Some((center, radius)), 1e-9);
|
||||
assert_eq!(loop2.as_circle(), Some((center, radius)));
|
||||
}
|
||||
ring.reverse();
|
||||
let reversed = outline_loop(&ring, &mut |_id| Some((center, radius)), 1e-9);
|
||||
assert_eq!(reversed.as_circle(), Some((center, radius)));
|
||||
|
||||
// Repeated turns, reversal midway and an untagged chord are not circles.
|
||||
let twice: Vec<(Vec2, Option<u32>)> = (0..128)
|
||||
.map(|i| {
|
||||
let angle = 10.0f64.to_radians() - 2.0 * TAU * i as f64 / 128.0;
|
||||
(center + Vec2::from_angle(angle) * radius, Some((i / 32) as u32))
|
||||
})
|
||||
.collect();
|
||||
assert!(outline_loop(&twice, &mut |_id| Some((center, radius)), 1e-9).as_circle().is_none());
|
||||
|
||||
let backtrack: Vec<(Vec2, Option<u32>)> = (0..128)
|
||||
.map(|i| {
|
||||
let step = if i <= 64 { i } else { 128 - i };
|
||||
let angle = 10.0f64.to_radians() - TAU * step as f64 / 128.0;
|
||||
(center + Vec2::from_angle(angle) * radius, Some((i / 32) as u32))
|
||||
})
|
||||
.collect();
|
||||
assert!(outline_loop(&backtrack, &mut |_id| Some((center, radius)), 1e-9).as_circle().is_none());
|
||||
|
||||
let mut missing_wall = ring;
|
||||
missing_wall[17].1 = None;
|
||||
assert!(outline_loop(&missing_wall, &mut |_id| Some((center, radius)), 1e-9).as_circle().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edges_move_in_by_their_own_amounts_and_step_along_one_line() {
|
||||
let v = Vec2::new;
|
||||
|
||||
Reference in new issue
Block a user