mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 20:22:15 -05:00
* Viewports keep their own UCS and their other settings through save and copy (part of #1316) A layout viewport's own UCS and every setting CADCraft doesn't model were dropped on read, so saving a sheet set lost them for good. - Viewport::ucs (origin, X/Y axes, elevation, orthographic type, named and base UCS by name) and ucs_per_viewport (UCSVP), read from and written as VIEWPORT groups 110-112, 146, 79, 345/346 and 71. - Viewport::extra: the viewport's other groups as read (snap, grid, lens and clipping, render mode, plot style sheet, shade plot, visual style, background, lighting). They belong to the entity, so COPY and MIRROR carry them; dxf_raw::finish writes them before the xdata and points their references at what they named (ForeignExtra::tail). - Not carried raw: the view twist, direction and target (51, 16, 17), which say where the view centre is measured from; writing them back while the centre is in drawing coordinates would move the view. - DWG: the bridge drops 345/346, the plot style sheet, shade plot and lighting groups, so CADCraft xdata keeps a copy (VPUCS, VPEXTRA) that the reader falls back on. The visual style, background and shade plot references are still lost through DWG (file-format-parity.md). - A UCS with unreadable or zero-length axes is no UCS; a named UCS that's no longer in the table isn't pointed at. Tests: another program's viewport read; DXF save and reread; two DWG round trips; a copied viewport; a deleted named UCS; a viewport without a UCS written as before; hostile groups (5000 extras, NaN axes, bad handles); COPY and MIRROR through the engine. * Viewports: VPEXTRA keeps only a viewport's own groups; a base UCS handle alone is kept - CADCraft's VPEXTRA xdata copy of a viewport's unmodelled groups is merged only for groups that can stand inside the AcDbViewport subclass: a record start (0), handles (5, 105), a subclass (100), a group brace (102) or xdata (1000 on) from a crafted file are dropped, so a save can't write records or handles the user didn't draw. - A VIEWPORT whose only UCS group is its base UCS handle (346) now reads a UCS, so the base UCS is resolved and written back. * Viewports: drop structural groups from a record's own extras; index UCS names for the xdata fallback - A VIEWPORT's own AcDbViewport groups are kept raw only when they can stand in the subclass, as CADCraft's VPEXTRA copy already was: a second handle (5, 105) or a group brace (102) written back would give the record two handles. The record's own pointer groups stay, to be remapped with it. - The named and base UCS that CADCraft's xdata names are looked up in a case-insensitive index built once with the UCS table, not by scanning every UCS for each viewport. * Parity checklist: regenerated after merging main * Parity checklist: regenerated after merging main * Parity checklist: regenerated after merging main * xtask: take main's stats.rs (the merge kept a test import Linux clippy rejects) * Viewport tests built on main set the UCS fields * WIP row 74: Codex's test edits kept; production normalisation (identity UCS -> None, default-valued extras dropped in dxf_read viewport_ucs/viewport_extra) still to do * Viewports: a default viewport reads back with no UCS or extra groups (#1484) A DWG save writes an identity UCS and the default snap, grid, lens, clip, render and shade groups for every viewport, so a viewport nobody gave a UCS came back with one and a dozen extras. The DXF reader now reads an unnamed identity UCS (UCSVP 0) as none, and keeps a group in extra only when it differs from the defaults, listed in one table. Tests: a_default_viewport_reads_back_without_ucs_or_extras, a_viewport_with_non_default_snap_keeps_it; layouts_and_paperspace passes. * Viewports: clippy range patterns; raster_image test builds with the viewport's UCS fields * Tests: move the viewport UCS suites into the it entry points (#1316) viewport_ucs_roundtrip.rs and viewport_ucs_copy.rs were separate test binaries; AGENTS section 30 wants the existing entry points. All 12 DXF/DWG persistence tests and the COPY/MIRROR tests are kept. * Tests: move the viewport UCS suites into the it entry points (#1316) viewport_ucs_roundtrip.rs and viewport_ucs_copy.rs were separate test binaries; AGENTS section 30 wants the existing entry points. All 12 DXF/DWG persistence tests and the COPY/MIRROR tests are kept. --------- Signed-off-by: FredApps <[email protected]> Co-authored-by: Claude <[email protected]> Co-authored-by: SBaksa <[email protected]> Co-authored-by: FredApps <[email protected]>
184 lines
8.2 KiB
Rust
184 lines
8.2 KiB
Rust
//! Plot style line ends, joins and fill patterns (issue #564): wide lines become outlines with
|
|
//! the style's ends and joins, solid fills become the style's pattern.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use cadcraft_color::Color;
|
|
use cadcraft_doc::{
|
|
Common, Drawing, EndStyle, EntityKind, FillStyle, JoinStyle, Line, LwPolyline, PlotStyle, PlotStyleTable, Solid, Space, Viewport,
|
|
};
|
|
use cadcraft_geom::{PolyVertex, Vec2, Vec3};
|
|
use cadcraft_render::{DisplayList, Kind, Options, VIEWPORT_CONTENT, build, build_plot, pen};
|
|
|
|
/// A colour-dependent table whose colour 1 prints 1.00 mm wide with `style`'s end/join/fill.
|
|
fn table(end: EndStyle, join: JoinStyle, fill: FillStyle) -> Options {
|
|
let t = PlotStyleTable::color_dependent("pens.ctb", "", |i| {
|
|
(i == 1).then(|| PlotStyle { lineweight: Some(100), end, join, fill, ..PlotStyle::default() })
|
|
});
|
|
Options { plot_style_table: Some(Arc::new(t)), lineweights: true, ..Default::default() }
|
|
}
|
|
|
|
fn red() -> Common {
|
|
Common { color: Color::Index(1), ..Default::default() }
|
|
}
|
|
|
|
fn tri_area(l: &DisplayList) -> f64 {
|
|
l.prims
|
|
.iter()
|
|
.filter(|p| p.kind == Kind::Tris)
|
|
.flat_map(|p| l.points(p).as_chunks::<3>().0.iter().map(|[a, b, c]| (*b - *a).cross(*c - *a) / 2.0))
|
|
.sum()
|
|
}
|
|
|
|
/// A 10-unit line plotted at 1 mm per unit.
|
|
fn line_plot(end: EndStyle, join: JoinStyle) -> DisplayList {
|
|
let mut d = Drawing::new_metric();
|
|
d.add(&Space::Model, red(), EntityKind::Line(Line { a: Vec3::new(0.0, 0.0, 0.0), b: Vec3::new(10.0, 0.0, 0.0) })).unwrap();
|
|
let mut l = build_plot(&d, &Space::Model, &table(end, join, FillStyle::Object));
|
|
pen::apply(&mut l, 1.0);
|
|
l
|
|
}
|
|
|
|
#[test]
|
|
fn line_ends_shape_wide_lines() {
|
|
let object = line_plot(EndStyle::Object, JoinStyle::Object);
|
|
assert_eq!(object.prims[0].kind, Kind::Polyline, "use-object ends keep the stroke");
|
|
assert_eq!(object.prims[0].lw, 1.0);
|
|
let butt = line_plot(EndStyle::Butt, JoinStyle::Object);
|
|
assert_eq!(butt.prims.len(), 1);
|
|
assert_eq!(butt.prims[0].kind, Kind::Tris);
|
|
assert!((tri_area(&butt) - 10.0).abs() < 1e-9, "butt: {}", tri_area(&butt));
|
|
let square = line_plot(EndStyle::Square, JoinStyle::Object);
|
|
assert!((tri_area(&square) - 11.0).abs() < 1e-9, "square: {}", tri_area(&square));
|
|
let diamond = line_plot(EndStyle::Diamond, JoinStyle::Object);
|
|
assert!((tri_area(&diamond) - 10.5).abs() < 1e-9, "diamond: {}", tri_area(&diamond));
|
|
let round = line_plot(EndStyle::Round, JoinStyle::Object);
|
|
assert!(tri_area(&round) > 10.7 && tri_area(&round) < 10.0 + std::f64::consts::PI / 4.0);
|
|
// Applying again changes nothing; pens are spent.
|
|
let mut again = butt.clone();
|
|
pen::apply(&mut again, 1.0);
|
|
assert_eq!(again.prims, butt.prims);
|
|
assert!(butt.pens.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn joins_shape_the_corner() {
|
|
// An L: (0,0) → (10,0) → (10,10), 1 mm wide, butt ends.
|
|
let corner = |join| {
|
|
let mut d = Drawing::new_metric();
|
|
let v = |x, y| PolyVertex::new(Vec2::new(x, y));
|
|
let pl =
|
|
LwPolyline { vertices: vec![v(0.0, 0.0), v(10.0, 0.0), v(10.0, 10.0)], closed: false, const_width: 0.0, elevation: 0.0, plinegen: false };
|
|
d.add(&Space::Model, red(), EntityKind::LwPolyline(pl)).unwrap();
|
|
let mut l = build_plot(&d, &Space::Model, &table(EndStyle::Butt, join, FillStyle::Object));
|
|
pen::apply(&mut l, 1.0);
|
|
// The union of the outline: rasterise coarsely by sampling the outer corner region.
|
|
let inside = |p: Vec2| {
|
|
l.prims.iter().any(|pr| {
|
|
l.points(pr).as_chunks::<3>().0.iter().any(|[a, b, c]| {
|
|
let s = |u: Vec2, w: Vec2| (w - u).cross(p - u) >= -1e-12;
|
|
s(*a, *b) && s(*b, *c) && s(*c, *a)
|
|
})
|
|
})
|
|
};
|
|
(inside(Vec2::new(10.45, -0.45)), inside(Vec2::new(10.3, -0.3)), inside(Vec2::new(10.2, -0.2)))
|
|
};
|
|
assert_eq!(corner(JoinStyle::Miter), (true, true, true), "miter fills the outer corner");
|
|
assert_eq!(corner(JoinStyle::Bevel), (false, false, true), "bevel cuts it");
|
|
assert_eq!(corner(JoinStyle::Round), (false, true, true), "round rounds it");
|
|
assert_eq!(corner(JoinStyle::Diamond), (false, true, true), "diamond points into it");
|
|
}
|
|
|
|
#[test]
|
|
fn fill_patterns_replace_solid_fills() {
|
|
let mut d = Drawing::new_metric();
|
|
let c = [Vec3::new(0.0, 0.0, 0.0), Vec3::new(10.0, 0.0, 0.0), Vec3::new(0.0, 10.0, 0.0), Vec3::new(10.0, 10.0, 0.0)];
|
|
d.add(&Space::Model, red(), EntityKind::Solid(Solid { corners: c })).unwrap();
|
|
let area = |fill| {
|
|
let mut l = build_plot(&d, &Space::Model, &table(EndStyle::Object, JoinStyle::Object, fill));
|
|
assert_eq!(l.prims.len(), 1);
|
|
pen::apply(&mut l, 1.0);
|
|
assert_eq!(l.prims.len(), 1, "one primitive per object");
|
|
tri_area(&l)
|
|
};
|
|
assert!((area(FillStyle::Solid) - 100.0).abs() < 1e-9);
|
|
assert!((area(FillStyle::Object) - 100.0).abs() < 1e-9);
|
|
for (fill, expect) in [
|
|
(FillStyle::Checkerboard, 50.0),
|
|
(FillStyle::HorizontalBars, 100.0 / 3.0),
|
|
(FillStyle::VerticalBars, 100.0 / 3.0),
|
|
(FillStyle::SquareDots, 100.0 * 0.16),
|
|
] {
|
|
let a = area(fill);
|
|
assert!((a - expect).abs() < 1.5, "{fill:?}: {a} (expected about {expect})");
|
|
}
|
|
for fill in [FillStyle::Crosshatch, FillStyle::Diamonds, FillStyle::SlantLeft, FillStyle::SlantRight] {
|
|
let a = area(fill);
|
|
assert!(a > 15.0 && a < 60.0, "{fill:?}: {a}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn layouts_draw_pens_while_building() {
|
|
let space = Space::Paper("Layout1".into());
|
|
let mut d = Drawing::new_metric();
|
|
d.add(&space, red(), EntityKind::Line(Line { a: Vec3::new(0.0, 0.0, 0.0), b: Vec3::new(10.0, 0.0, 0.0) })).unwrap();
|
|
let opts = table(EndStyle::Square, JoinStyle::Object, FillStyle::Object);
|
|
let l = build(&d, &space, &opts);
|
|
assert_eq!(l.prims[0].kind, Kind::Tris);
|
|
assert!(l.pens.is_empty(), "pens are used up");
|
|
assert!((tri_area(&l) - 11.0).abs() < 1e-9);
|
|
// Thin lines stay strokes.
|
|
let thin = PlotStyleTable::color_dependent("thin.ctb", "", |i| {
|
|
(i == 1).then(|| PlotStyle { lineweight: Some(18), end: EndStyle::Square, ..PlotStyle::default() })
|
|
});
|
|
let l = build(&d, &space, &Options { plot_style_table: Some(Arc::new(thin)), lineweights: true, ..Default::default() });
|
|
assert_eq!(l.prims[0].kind, Kind::Polyline);
|
|
}
|
|
|
|
#[test]
|
|
fn hostile_scales_and_huge_fills_do_not_hang() {
|
|
let mut d = Drawing::new_metric();
|
|
let c = [Vec3::new(0.0, 0.0, 0.0), Vec3::new(1e12, 0.0, 0.0), Vec3::new(0.0, 1e12, 0.0), Vec3::new(1e12, 1e12, 0.0)];
|
|
d.add(&Space::Model, red(), EntityKind::Solid(Solid { corners: c })).unwrap();
|
|
d.add(&Space::Model, red(), EntityKind::Line(Line { a: Vec3::new(0.0, 0.0, 0.0), b: Vec3::new(1e300, 0.0, 0.0) })).unwrap();
|
|
let l = build_plot(&d, &Space::Model, &table(EndStyle::Diamond, JoinStyle::Diamond, FillStyle::Checkerboard));
|
|
for mm in [1.0, 1e-300, 1e300, 0.0, -1.0, f64::NAN, f64::INFINITY] {
|
|
let mut x = l.clone();
|
|
pen::apply(&mut x, mm);
|
|
assert_eq!(x.prims.len(), l.prims.len());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn viewport_contents_get_their_pens() {
|
|
// A 2:1 viewport onto a red model-space line: 1 mm wide on paper, square ends.
|
|
let mut d = Drawing::new_metric();
|
|
d.add(&Space::Model, red(), EntityKind::Line(Line { a: Vec3::new(-10.0, 0.0, 0.0), b: Vec3::new(10.0, 0.0, 0.0) })).unwrap();
|
|
let vp = Viewport {
|
|
center: Vec3::new(50.0, 50.0, 0.0),
|
|
width: 80.0,
|
|
height: 60.0,
|
|
view_center: Vec2::ZERO,
|
|
view_height: 30.0,
|
|
id: 2,
|
|
locked: false,
|
|
frozen_layers: Vec::new(),
|
|
layer_colors: Vec::new(),
|
|
clip: None,
|
|
on: true,
|
|
ucs: None,
|
|
ucs_per_viewport: false,
|
|
extra: Vec::new(),
|
|
twist: 0.0,
|
|
};
|
|
let space = Space::Paper("Layout1".into());
|
|
d.add(&space, Common::default(), EntityKind::Viewport(vp)).unwrap();
|
|
let l = build_plot(&d, &space, &table(EndStyle::Square, JoinStyle::Object, FillStyle::Object));
|
|
let content: Vec<_> = l.prims.iter().filter(|p| p.handle == VIEWPORT_CONTENT).collect();
|
|
assert_eq!(content.len(), 1);
|
|
assert_eq!(content[0].kind, Kind::Tris);
|
|
// 40 mm long on paper plus two 0.5 mm square ends, 1 mm wide.
|
|
assert!((tri_area(&l) - 41.0).abs() < 1e-6, "{}", tri_area(&l));
|
|
}
|