Files
solidcraft/crates/geom/src/section_tests.rs
T
CamWightandClaude Opus 5.5 de7bf4fc4d Drawing W17a: view definitions as data (#4816) (#4817)
* claim: #4816

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Drawing views as data: named views, scales and the map onto paper (#4816, W17a P1)

What a drawing view is, before anything draws it (geom::drawing_view):
- NamedView: the six principal views and the isometric views from the four upper corners, each
  an exact basis (a Plane whose normal points back at the viewer), never taken from a camera,
  whose pitch clamp would tilt a top view, with no -0 to write out; `iso` reads as the
  front-right one.
- basis_looking: any other view from its direction, an up and an origin (paper x = direction x up,
  paper y = x x direction), refusing a zero direction, an up along it, or an origin that isn't
  finite or within 1e12 mm. W07's
  {direction, up, origin} view reads through it.
- Scale: paper : model as two positive numbers, read and written as "1:2", from 1:100000 to
  1000:1; Scale::fit picks the largest ISO 5455 preset (50:1 to 1:10000) at which a view fits a
  space (W07's "fits the page" default), compared as size x paper <= room x model so an exact
  fit at a reduction isn't lost to rounding.
- ViewFrame: a view's basis, scale and place on paper, the point of its plane at `centre`
  landing at `position_mm`. Its paper map moves the view's curves and their accuracy onto
  paper (paper error = scale x model error); `boundary` maps a box of its plane onto paper from
  its four corners.

In geom, where Seg2 lives:
- Similarity2 and Seg2::similar (new): scale, turn and shift a segment exactly, an arc staying an
  arc. Similarity2::new refuses a scale that isn't above 0 and works the turn out once, not per
  point;
- Seg2::map_controls: a segment's control points mapped by any function. It was doc's
  sheet::map_controls; its four callers in doc use the method now, unchanged.

CurveAccuracy::scaled gives a curve's accuracy at another scale.

Tests: each named view's basis and viewer side, and the same bases from a direction and up; a 50 mm cube in every principal view at 1:1 and
1:2, and in isometric (edges 50 x sqrt(2/3), outline 50 x sqrt(2)); the view's centre on its
position (W07's 100 x 20 front view on A3 at 2:1); scales read, written and refused; the fitted
scale, an exact fit at 1:5 and ISO's smallest; no -0 in any named view's axes; an origin refused by
its own message; hostile bases, centres and positions; every segment kind moved by a similarity,
and the similarities refused; scaled accuracy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Drawing views: the bottom view pinned, square near parallel, bounded scales and boxes (#4816)

From DevonHope's review of #4817:
- The tests pin all six principal views exactly. The direction-and-up test uses literal ups,
  so a bottom view turned half round now fails it.
- basis_looking squares x to the direction before normalising. Near the 1e-9 threshold the
  normal was up to 8.8e-9 rad off; now it is about 1e-16.
- Scale::fit fits nothing for a size or room that is negative or not finite. A size of 0 still
  sets no limit that way.
- Each side of a scale is 1e-6 to 1e6, so a scale writes at most 49 characters.
- ViewFrame::boundary returns a Result. It refuses a box that is inside out or not finite on
  paper, instead of dropping a corner.
- paper_map, boundary and seg_to_paper say which coordinates they take.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 00:18:01 -04:00

424 lines
20 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use std::f64::consts::{PI, TAU};
use super::*;
use crate::{MAX_BEZIER_CTRL, bezier};
const TOL: f64 = 1e-6;
fn empty(plane: Plane) -> SectionGeometry {
SectionGeometry { plane, curves: vec![], wires: vec![], regions: vec![], contacts: vec![] }
}
fn analytic(geometry: Seg2) -> SectionCurve {
SectionCurve { geometry, accuracy: CurveAccuracy::Analytic }
}
fn used(curve: u32, forward: bool) -> CurveUse {
CurveUse { curve, forward }
}
fn plate() -> SectionGeometry {
let p = Vec2::new;
SectionGeometry {
plane: Plane::XY.offset(10.0),
curves: vec![
// Deliberately stored backwards: the wire's use restores the CCW rectangle.
analytic(Seg2::Line { a: p(100.0, 0.0), b: p(0.0, 0.0) }),
analytic(Seg2::Line { a: p(100.0, 0.0), b: p(100.0, 60.0) }),
analytic(Seg2::Line { a: p(100.0, 60.0), b: p(0.0, 60.0) }),
analytic(Seg2::Line { a: p(0.0, 60.0), b: p(0.0, 0.0) }),
analytic(Seg2::Arc { center: p(50.0, 30.0), radius: 5.0, start: 0.0, sweep: PI }),
analytic(Seg2::Arc { center: p(50.0, 30.0), radius: 5.0, start: PI, sweep: PI }),
],
wires: vec![
SectionWire { curves: vec![used(0, false), used(1, true), used(2, true), used(3, true)], closed: true },
SectionWire { curves: vec![used(5, false), used(4, false)], closed: true },
],
regions: vec![SectionRegion { outer: 0, holes: vec![1] }],
contacts: vec![],
}
}
#[test]
fn section_frame_preserves_named_translated_tilted_offset_and_flip_conventions() {
for plane in [Plane::XY, Plane::XZ, Plane::YZ] {
validate_plane(&plane).unwrap();
}
assert_eq!(Plane::XZ.normal(), Vec3::Y);
assert_eq!(Plane::XZ.to_world(Vec2::new(3.0, 4.0)), Vec3::new(3.0, 0.0, -4.0));
let plane = Plane::from_normal(Vec3::new(13.0, -7.0, 19.0), Vec3::new(2.0, -3.0, 5.0)).unwrap();
validate_plane(&plane).unwrap();
let local = Vec2::new(7.0, -11.0);
let height = 4.5;
let point = plane.to_world(local) + plane.normal() * height;
assert!(plane.to_local(point).dist(local) < 1e-12);
assert!((plane.height(point) - height).abs() < 1e-12);
let moved = plane.offset(8.0);
validate_plane(&moved).unwrap();
assert!((plane.height(moved.origin) - 8.0).abs() < 1e-12);
for about_y in [false, true] {
let flipped = moved.flipped(about_y);
validate_plane(&flipped).unwrap();
assert_eq!(flipped.origin, moved.origin);
assert_eq!(flipped.normal(), -moved.normal());
assert_eq!(flipped.flipped(about_y), moved);
let on_plane = moved.to_world(local);
let mirrored = if about_y { Vec2::new(-local.x, local.y) } else { Vec2::new(local.x, -local.y) };
assert!(flipped.to_local(on_plane).dist(mirrored) < 1e-12);
assert!((flipped.height(point) + moved.height(point)).abs() < 1e-12);
}
}
#[test]
fn strict_frame_validation_does_not_repair_raw_or_deserialized_axes() {
let bad = Plane { x: Vec3::X * 2.0, ..Plane::XY };
assert!(Plane::new(bad.origin, bad.x, bad.y).is_some());
assert_eq!(validate_plane(&bad), Err(Invalid("nonorthonormal plane")));
assert_eq!(bad.x, Vec3::X * 2.0);
let restored: Plane = serde_json::from_value(serde_json::json!({
"origin": [0, 0, 0], "x_dir": [1, 0, 0], "y_dir": [1, 1, 0]
}))
.unwrap();
assert_eq!(validate_plane(&restored), Err(Invalid("nonorthonormal plane")));
for plane in [
Plane { origin: Vec3::new(f64::NAN, 0.0, 0.0), ..Plane::XY },
Plane { origin: Vec3::new(1e12 + 1.0, 0.0, 0.0), ..Plane::XY },
Plane { x: Vec3::new(f64::MAX, 0.0, 0.0), ..Plane::XY },
Plane { y: Vec3::new(0.0, f64::INFINITY, 0.0), ..Plane::XY },
Plane { x: Vec3::ZERO, ..Plane::XY },
Plane { y: Vec3::X, ..Plane::XY },
] {
assert!(validate_plane(&plane).is_err());
}
validate_plane(&Plane { origin: Vec3::new(1e12, -1e12, 1e12), ..Plane::XY }).unwrap();
}
#[test]
fn line_arc_region_conversion_preserves_winding_and_analytic_area() {
let section = plate();
let before = section.clone();
section.validate_structure(TOL).unwrap();
let outer = section.to_loop(0, TOL).unwrap();
let hole = section.to_loop(1, TOL).unwrap();
assert!(outer.is_closed(TOL) && hole.is_closed(TOL));
assert!((outer.signed_area() - 6000.0).abs() < 1e-10);
assert!((hole.signed_area() + 25.0 * PI).abs() < 1e-10);
let region = section.to_region(0, TOL).unwrap();
assert_eq!(region.outer, outer);
assert_eq!(region.holes, vec![hole]);
assert!((region.area() - (6000.0 - 25.0 * PI)).abs() < 1e-10);
assert_eq!(section, before);
}
#[test]
fn empty_contacts_and_open_wires_remain_distinct_without_inferred_material() {
let empty = empty(Plane::XY);
empty.validate_structure(TOL).unwrap();
assert_eq!(empty.to_region(0, TOL), Err(Invalid("region reference")));
let mut contact = empty.clone();
contact.contacts.push(Vec2::new(2.0, 3.0));
contact.validate_structure(TOL).unwrap();
assert_ne!(contact, empty);
let mut open = empty.clone();
open.curves = vec![analytic(Seg2::Line { a: Vec2::ZERO, b: Vec2::X }), analytic(Seg2::Line { a: Vec2::X, b: Vec2::new(1.0, 1.0) })];
open.wires = vec![SectionWire { curves: vec![used(0, true), used(1, true)], closed: false }];
open.validate_structure(TOL).unwrap();
assert_eq!(open.to_loop(0, TOL), Err(Invalid("wire is not closed")));
open.regions.push(SectionRegion { outer: 0, holes: vec![] });
assert_eq!(open.validate_structure(TOL), Err(Invalid("region boundary is not closed")));
// Even a geometrically closed chain remains explicitly open when the producer says so.
let mut declared_open = plate();
declared_open.regions.clear();
declared_open.wires[0].closed = false;
declared_open.validate_structure(TOL).unwrap();
assert_eq!(declared_open.to_loop(0, TOL), Err(Invalid("wire is not closed")));
}
#[test]
fn bad_references_empty_disconnected_and_unclosed_wires_fail() {
let mut section = plate();
section.wires[0].curves[0].curve = u32::MAX;
assert_eq!(section.validate_structure(TOL), Err(Invalid("curve reference")));
let mut section = plate();
section.wires[0].curves.clear();
assert_eq!(section.validate_structure(TOL), Err(Invalid("empty wire")));
let mut section = plate();
section.wires[0].curves[1].forward = false;
assert_eq!(section.validate_structure(TOL), Err(Invalid("disconnected wire")));
let mut section = plate();
section.wires[0].curves.pop();
assert_eq!(section.validate_structure(TOL), Err(Invalid("open closed wire")));
let mut section = plate();
section.regions[0].outer = u32::MAX;
assert_eq!(section.validate_structure(TOL), Err(Invalid("wire reference")));
let mut section = plate();
section.regions[0].holes[0] = u32::MAX;
assert_eq!(section.validate_structure(TOL), Err(Invalid("wire reference")));
assert_eq!(plate().to_loop(u32::MAX, TOL), Err(Invalid("wire reference")));
assert_eq!(plate().to_region(u32::MAX, TOL), Err(Invalid("region reference")));
}
#[test]
fn duplicate_boundaries_are_local_to_a_region_and_large_reference_tables_are_bounded() {
let mut section = plate();
section.regions[0].holes.push(1);
assert_eq!(section.validate_structure(TOL), Err(Invalid("duplicate region boundary")));
let mut section = plate();
section.regions[0].holes.push(0);
assert_eq!(section.validate_structure(TOL), Err(Invalid("duplicate region boundary")));
let mut section = plate();
section.regions.push(section.regions[0].clone());
section.validate_structure(TOL).unwrap();
assert_eq!(section.to_region(0, TOL).unwrap(), section.to_region(1, TOL).unwrap());
// Deliberately coincident boundaries: this tests a large legal reference graph, not material.
let mut many = empty(Plane::XY);
many.curves.push(analytic(Seg2::Arc { center: Vec2::ZERO, radius: 1.0, start: 0.0, sweep: TAU }));
many.wires = vec![SectionWire { curves: vec![used(0, true)], closed: true }; 201];
many.regions = vec![SectionRegion { outer: 0, holes: (1..=200).collect() }; 200];
many.validate_structure(TOL).unwrap();
let last = many.regions.last_mut().unwrap();
*last.holes.last_mut().unwrap() = 1;
assert_eq!(many.validate_structure(TOL), Err(Invalid("duplicate region boundary")));
}
#[test]
fn aggregate_counts_fail_before_geometry_or_reference_inspection() {
let mut section = empty(Plane::XY);
section.contacts = vec![Vec2::new(f64::NAN, 0.0); MAX_ITEMS + 1];
assert_eq!(section.validate_structure(TOL), Err(Invalid("item budget")));
let mut section = empty(Plane::XY);
section.wires.push(SectionWire { curves: vec![used(u32::MAX, false); MAX_ITEMS], closed: false });
assert_eq!(section.validate_structure(TOL), Err(Invalid("item budget")));
let mut section = empty(Plane::XY);
section.regions.push(SectionRegion { outer: u32::MAX, holes: vec![u32::MAX; MAX_ITEMS] });
assert_eq!(section.validate_structure(TOL), Err(Invalid("item budget")));
let mut section = empty(Plane::XY);
section.contacts = vec![Vec2::ZERO; MAX_ITEMS / 2];
section.wires.push(SectionWire { curves: vec![used(u32::MAX, false); MAX_ITEMS / 2], closed: false });
assert_eq!(section.validate_structure(TOL), Err(Invalid("item budget")));
// Exactly the item limit is accepted, independently of these points' material meaning.
let mut boundary = empty(Plane::XY);
boundary.contacts = vec![Vec2::ZERO; MAX_ITEMS];
boundary.validate_structure(TOL).unwrap();
let mut count = Budget(MAX_ITEMS);
assert_eq!(count.add(usize::MAX), Err(Invalid("item budget")));
}
#[test]
fn closure_tolerance_is_bounded_and_not_used_to_collapse_short_curves() {
for tolerance in [f64::NAN, f64::INFINITY, -1.0, 0.0, 1e-10, 1.01] {
assert!(plate().validate_structure(tolerance).is_err());
}
for tolerance in [1e-9, 1.0] {
plate().validate_structure(tolerance).unwrap();
}
let mut section = empty(Plane::XY);
section.curves.push(analytic(Seg2::Line { a: Vec2::ZERO, b: Vec2::new(1e-10, 0.0) }));
section.validate_structure(1.0).unwrap();
let mut gap = plate();
if let Seg2::Line { b, .. } = &mut gap.curves[3].geometry {
b.x = 5e-7;
}
gap.validate_structure(1e-6).unwrap();
assert_eq!(gap.validate_structure(1e-8), Err(Invalid("open closed wire")));
}
#[test]
fn curve_number_and_domain_errors_are_rejected_before_evaluation() {
let p = Vec2::new;
let arc = |radius, start, sweep| Seg2::Arc { center: Vec2::ZERO, radius, start, sweep };
let conic = |w| Seg2::Conic { a: Vec2::X, apex: p(1.0, 1.0), b: Vec2::Y, w };
let invalid = [
Seg2::Line { a: Vec2::ZERO, b: Vec2::ZERO },
Seg2::Line { a: Vec2::ZERO, b: p(f64::NAN, 0.0) },
Seg2::Line { a: Vec2::ZERO, b: p(f64::MAX, 0.0) },
arc(-1.0, 0.0, PI),
arc(0.0, 0.0, PI),
arc(1e-13, 0.0, PI),
arc(1e12 + 1.0, 0.0, PI),
arc(f64::INFINITY, 0.0, PI),
arc(1.0, f64::NAN, PI),
arc(1.0, 1e6 + 1.0, PI),
arc(1.0, 0.0, 0.0),
arc(1.0, 0.0, 1e-13),
arc(1.0, 0.0, TAU + 1e-8),
arc(1.0, 0.0, f64::INFINITY),
conic(-0.5),
conic(0.0),
conic(1e-13),
conic(1e12 + 1.0),
conic(f64::NAN),
Seg2::Cubic { p0: Vec2::X, p1: Vec2::X, p2: Vec2::X, p3: Vec2::X },
Seg2::Conic { a: Vec2::X, apex: Vec2::X, b: Vec2::X, w: 1.0 },
];
for geometry in invalid {
let mut section = empty(Plane::XY);
section.curves.push(analytic(geometry));
assert!(section.validate_structure(TOL).is_err(), "accepted {geometry:?}");
}
let mut section = empty(Plane::XY);
section.contacts.push(p(f64::INFINITY, 0.0));
assert!(section.validate_structure(TOL).is_err());
}
#[test]
fn bezier_degree_and_every_serialized_control_slot_are_validated() {
for degree in [4u8, 9] {
let mut p = [Vec2::ZERO; MAX_BEZIER_CTRL];
for (i, point) in p.iter_mut().enumerate().take(usize::from(degree) + 1) {
*point = Vec2::new(i as f64, (i % 2) as f64);
}
let mut section = empty(Plane::XY);
section.curves.push(analytic(Seg2::Bezier { n: degree, p }));
section.validate_structure(TOL).unwrap();
for wrong in [0, 3, 10, u8::MAX] {
section.curves[0].geometry = Seg2::Bezier { n: wrong, p };
assert_eq!(section.validate_structure(TOL), Err(Invalid("Bezier degree")));
}
}
let mut p = [Vec2::ZERO; MAX_BEZIER_CTRL];
p[4] = Vec2::X;
for value in [f64::NAN, f64::INFINITY, 1e12 + 1.0] {
p[MAX_BEZIER_CTRL - 1] = Vec2::new(value, 0.0);
let mut section = empty(Plane::XY);
section.curves.push(analytic(Seg2::Bezier { n: 4, p }));
assert_eq!(section.validate_structure(TOL), Err(Invalid("non-finite or excessive coordinate")));
}
let mut section = empty(Plane::XY);
section.curves.push(analytic(Seg2::Bezier { n: 4, p: [Vec2::ZERO; MAX_BEZIER_CTRL] }));
assert_eq!(section.validate_structure(TOL), Err(Invalid("collapsed curve")));
}
#[test]
fn supported_curve_limits_keep_evaluation_finite_without_claiming_accuracy() {
let p = Vec2::new;
let mut section = empty(Plane::XY);
section.curves = vec![
analytic(Seg2::Line { a: p(-1e12, 1e12), b: p(1e12, -1e12) }),
analytic(Seg2::Arc { center: p(1e12, 1e12), radius: 1e12, start: 1e6, sweep: -TAU }),
analytic(Seg2::Arc { center: Vec2::ZERO, radius: 1e-12, start: 0.0, sweep: 1e-12 }),
analytic(Seg2::Conic { a: p(-1e12, 0.0), apex: p(0.0, 1e12), b: p(1e12, 0.0), w: 1e12 }),
analytic(Seg2::Conic { a: p(-1e12, 0.0), apex: p(0.0, 1e12), b: p(1e12, 0.0), w: 1e-12 }),
analytic(bezier(&[p(0.0, 0.0), p(1e12, 0.0), p(0.0, 1e12), p(-1e12, 0.0)]).unwrap()),
];
let controls: Vec<Vec2> = (0..=9).map(|i| p(i as f64 * 1e11, if i % 2 == 0 { 1e12 } else { -1e12 })).collect();
section.curves.push(analytic(bezier(&controls).unwrap()));
section.validate_structure(TOL).unwrap();
for curve in &section.curves {
for t in [0.0, 0.5, 1.0] {
assert!(curve.geometry.point_at(t).is_finite());
assert!(curve.geometry.derivative(t).is_finite());
assert!(curve.geometry.derivatives(t, 4).is_some_and(|d| d.iter().all(|v| v.is_finite())));
}
}
}
#[test]
fn transient_serialization_preserves_accuracy_tags_and_plane_names() {
let mut section = plate();
section.curves[1].accuracy = CurveAccuracy::Bounded { max_deviation_mm: 0.01 };
section.curves[2].accuracy = CurveAccuracy::Sampled { max_sample_deviation_mm: 0.0 };
section.validate_structure(TOL).unwrap();
let value = serde_json::to_value(&section).unwrap();
assert!(value["plane"].get("x_dir").is_some());
assert!(value["plane"].get("y_dir").is_some());
assert_eq!(value["curves"][0]["accuracy"], serde_json::json!({"kind": "analytic"}));
assert_eq!(value["curves"][1]["accuracy"], serde_json::json!({"kind": "bounded", "max_deviation_mm": 0.01}));
assert_eq!(value["curves"][2]["accuracy"], serde_json::json!({"kind": "sampled", "max_sample_deviation_mm": 0.0}));
let restored: SectionGeometry = serde_json::from_value(value.clone()).unwrap();
restored.validate_structure(TOL).unwrap();
assert_eq!(section, restored);
let mut unknown = value;
unknown["certificate"] = serde_json::json!(true);
assert!(serde_json::from_value::<SectionGeometry>(unknown).is_err());
assert!(serde_json::from_value::<CurveAccuracy>(serde_json::json!({"kind": "bounded", "max_deviation_mm": 0.1, "exact": true})).is_err());
assert!(serde_json::from_value::<CurveAccuracy>(serde_json::json!({"kind": "exact"})).is_err());
}
#[test]
fn accuracy_values_are_validated_without_promoting_zero_sampled_error() {
for value in [f64::NAN, f64::INFINITY, -0.01, 1e12 + 1.0] {
for accuracy in [CurveAccuracy::Bounded { max_deviation_mm: value }, CurveAccuracy::Sampled { max_sample_deviation_mm: value }] {
let mut section = plate();
section.curves[0].accuracy = accuracy;
assert_eq!(section.validate_structure(TOL), Err(Invalid("accuracy deviation")));
}
}
for value in [0.0, 1e12] {
let mut section = plate();
section.curves[0].accuracy = CurveAccuracy::Sampled { max_sample_deviation_mm: value };
section.validate_structure(TOL).unwrap();
section.to_region(0, TOL).unwrap();
assert_eq!(section.curves[0].accuracy, CurveAccuracy::Sampled { max_sample_deviation_mm: value });
}
}
#[test]
fn conversion_revalidates_mutated_data_and_does_not_trust_an_earlier_success() {
let mut section = plate();
section.validate_structure(TOL).unwrap();
section.curves[0].geometry = Seg2::Line { a: Vec2::ZERO, b: Vec2::new(f64::NAN, 0.0) };
assert_eq!(section.to_loop(0, TOL), Err(Invalid("non-finite or excessive coordinate")));
assert_eq!(section.to_region(0, TOL), Err(Invalid("non-finite or excessive coordinate")));
let mut section = plate();
section.to_region(0, TOL).unwrap();
section.contacts = vec![Vec2::ZERO; MAX_ITEMS];
assert_eq!(section.to_region(0, TOL), Err(Invalid("item budget")));
}
#[test]
fn reversed_large_arc_conversion_rechecks_actual_connectivity_without_snapping() {
let arc = Seg2::Arc { center: Vec2::ZERO, radius: 1e12, start: 0.1, sweep: 1.0 };
let mut section = empty(Plane::XY);
section.curves = vec![analytic(arc), analytic(Seg2::Line { a: arc.start(), b: arc.end() })];
section.wires.push(SectionWire { curves: vec![used(0, false), used(1, true)], closed: true });
section.regions.push(SectionRegion { outer: 0, holes: vec![] });
let before = section.clone();
// Source endpoints coincide exactly, but (start + sweep) - sweep changes the reversed
// arc's endpoint by more than this tolerance at this radius.
section.validate_structure(1e-9).unwrap();
assert!(arc.reversed().end().dist(arc.start()) > 1e-9);
assert_eq!(section.to_loop(0, 1e-9), Err(Invalid("converted loop is not closed within tolerance")));
assert_eq!(section.to_region(0, 1e-9), Err(Invalid("converted loop is not closed within tolerance")));
assert_eq!(section, before);
// A caller's explicit larger tolerance can accept the same result without moving endpoints.
let converted = section.to_loop(0, 0.01).unwrap();
assert_eq!(converted.segs[0], arc.reversed());
assert!(converted.is_closed(0.01));
}
#[test]
fn connected_bow_tie_and_wrong_winding_are_not_certified_or_silently_repaired() {
let p = Vec2::new;
let loop_ = Loop2::polygon(&[p(0.0, 0.0), p(2.0, 2.0), p(0.0, 2.0), p(2.0, 0.0)]);
let mut bow = empty(Plane::XY);
bow.curves = loop_.segs.iter().copied().map(analytic).collect();
bow.wires.push(SectionWire { curves: (0..4).map(|i| used(i, true)).collect(), closed: true });
bow.regions.push(SectionRegion { outer: 0, holes: vec![] });
bow.validate_structure(TOL).unwrap();
assert_eq!(bow.to_loop(0, TOL).unwrap(), loop_);
// A successful structural check does not establish a material region, including its winding.
let mut reversed = plate();
reversed.wires[0].curves.reverse();
for use_ in &mut reversed.wires[0].curves {
use_.forward = !use_.forward;
}
reversed.validate_structure(TOL).unwrap();
let converted = reversed.to_region(0, TOL).unwrap();
assert!((converted.outer.signed_area() + 6000.0).abs() < 1e-10);
}
/// A curve's accuracy scales with it: a bound or a sampled deviation by the factor, and an analytic
/// curve stays analytic (W17a: paper error = scale × model error).
#[test]
fn an_accuracy_scales_with_its_curve() {
assert_eq!(CurveAccuracy::Analytic.scaled(0.5), CurveAccuracy::Analytic);
assert_eq!(CurveAccuracy::Sampled { max_sample_deviation_mm: 0.01 }.scaled(3.0), CurveAccuracy::Sampled { max_sample_deviation_mm: 0.03 });
assert_eq!(CurveAccuracy::Bounded { max_deviation_mm: 0.01 }.scaled(2.0), CurveAccuracy::Bounded { max_deviation_mm: 0.02 });
assert_eq!(
CurveAccuracy::Sampled { max_sample_deviation_mm: 0.0 }.scaled(5.0),
CurveAccuracy::Sampled { max_sample_deviation_mm: 0.0 },
"a sampled zero stays sampled"
);
}