Show the displacement of a single shape move

This commit is contained in:
Jan Petykiewicz 2026-11-18 23:01:57 -08:00
commit 4789638e22
3 changed files with 94 additions and 27 deletions

View file

@ -54,15 +54,17 @@ Text reports start with the result. The headline includes cell counts for
reports with more than three changed cells, incomplete comparisons, or omitted
changes. The `Cells:` section lists removals with `-`, additions with `+`, and
modifications with `*`. Modified cells show counts of rectangles, polygons, and
boxes changed, moved, added, or removed on each layer. Repeated reference
additions or removals to the same target appear as a count. When both occur for
one target, they share a row. A complete set of edits to a target also shows
boxes changed, moved, added, or removed on each layer. When the only displayed
shape change on a layer is a move, the row shows its displacement in database
units. Repeated reference additions or removals to the same target appear as a
count. When both occur for one target, they share a row. A complete set of edits
to a target also shows
the total direct placements when there are unchanged placements, for example
`leaf · -3 refs, +6 refs · placements 10 -> 13`.
An unchanged layer name appears inline when the layer occurs on one displayed
row, as in `1/0 (WG): 1 rectangle moved`. A layer on several displayed rows
gets one legend entry. Object details use `-` for removals, `+` for additions,
row, as in `1/0 (WG): rectangle moved by (20, -5) dbu`. A layer on several
displayed rows gets one legend entry. Object details use `-` for removals, `+` for additions,
and `~` for likely replacements. Identical unmatched objects share a row
with a count such as `+ 12× 1/0: path …`. Unmatched paths with five or more
vertices show their width, vertex count, and vertex bounds in one line.

View file

@ -169,6 +169,10 @@ struct ShapeCounts {
}
impl ShapeCounts {
fn total(&self) -> usize {
self.rectangles + self.polygons + self.boxes
}
fn add(&mut self, object: &ObjectChange, replacement: Option<&ObjectChange>) {
let is_rectangle =
|object: &ObjectChange| XY::closed_rectangle(object_xy(object)).is_some();
@ -188,6 +192,7 @@ struct LayerPolygonChanges {
moved: ShapeCounts,
removed: ShapeCounts,
added: ShapeCounts,
displacement: Option<[i64; 2]>,
}
#[derive(Default)]
@ -196,15 +201,18 @@ pub(super) struct PolygonChanges(BTreeMap<Option<Layer>, LayerPolygonChanges>);
impl PolygonChanges {
pub fn replacement(&mut self, old: &ObjectChange, new: &ObjectChange) {
let counts = self.0.entry(old.layer).or_default();
let moved = old.properties == new.properties
&& translation(object_xy(old), object_xy(new)).is_some()
&& field_changes(old, new).all(|(tag, _, _)| tag == RTAG_XY);
let shapes = if moved {
&mut counts.moved
let displacement = if old.properties == new.properties {
translation(object_xy(old), object_xy(new))
.filter(|_| field_changes(old, new).all(|(tag, _, _)| tag == RTAG_XY))
} else {
&mut counts.changed
None
};
shapes.add(old, Some(new));
if let Some(shift) = displacement {
counts.displacement = Some(shift);
counts.moved.add(old, Some(new));
} else {
counts.changed.add(old, Some(new));
}
}
pub fn unmatched(&mut self, object: &ObjectChange, removed: bool) {
@ -226,13 +234,31 @@ impl PolygonChanges {
} else {
out.push_str("unknown layer: ");
}
let mut separator = "";
for (action, shapes) in [
let groups = [
("changed", &counts.changed),
("moved", &counts.moved),
("removed", &counts.removed),
("added", &counts.added),
] {
];
if let Some([dx, dy]) = counts.displacement
&& groups
.iter()
.map(|(_, shapes)| shapes.total())
.sum::<usize>()
== 1
{
let shape = if counts.moved.rectangles == 1 {
"rectangle"
} else if counts.moved.boxes == 1 {
"box"
} else {
"polygon"
};
writeln!(out, "{shape} moved by ({dx}, {dy}) dbu").unwrap();
continue;
}
let mut separator = "";
for (action, shapes) in groups {
let terms = [
(shapes.rectangles, "rectangle", "rectangles"),
(shapes.polygons, "polygon", "polygons"),

View file

@ -1759,7 +1759,7 @@ fn replacement_explanations_are_unique_and_keep_full_evidence() {
}
#[test]
fn polygon_diff_reports_moves_and_other_changes_without_individual_geometry() {
fn polygon_diff_reports_single_displacements_and_groups_bulk_moves() {
let before = layout(&[("CHIP", vec![with_property(square(), "old")])]);
let before = Document::parse(&before).unwrap();
for (shape, label) in [
@ -1787,26 +1787,65 @@ fn polygon_diff_reports_moves_and_other_changes_without_individual_geometry() {
assert!(!text.contains("xy="));
}
let moved_triangle = polygon(&[[2, -1], [12, -1], [2, 9], [2, -1]]);
let moved_square = polygon(&[[2, -1], [12, -1], [12, 9], [2, 9], [2, -1]]);
let named_layout = |mut objects: Vec<Vec<u8>>| {
objects.extend([label((900, 7), LAYER_SENTINEL), label((900, 7), "1:0:WG")]);
layout(&[("CHIP", objects)])
};
for (shape, shifted, name) in [
(triangle(), moved_triangle, "polygon"),
(square(), moved_square.clone(), "rectangle"),
(
triangle(),
polygon(&[[2, -1], [12, -1], [2, 9], [2, -1]]),
"polygon",
ring(
RTAG_BOX,
RTAG_BOXTYPE,
1,
&[[0, 0], [10, 0], [10, 10], [0, 10], [0, 0]],
),
(
square(),
polygon(&[[2, -1], [12, -1], [12, 9], [2, 9], [2, -1]]),
"rectangle",
ring(
RTAG_BOX,
RTAG_BOXTYPE,
1,
&[[2, -1], [12, -1], [12, 9], [2, 9], [2, -1]],
),
"box",
),
] {
let before = layout(&[("CHIP", vec![shape])]);
let after = layout(&[("CHIP", vec![shifted])]);
let before = named_layout(vec![shape]);
let after = named_layout(vec![shifted]);
let diff = Comparison::between(
&Document::parse(&before).unwrap(),
&Document::parse(&after).unwrap(),
);
assert!(diff.text(100).contains(&format!(" 1/0: 1 {name} moved\n")));
assert!(
diff.text(100)
.contains(&format!(" 1/0 (WG): {name} moved by (2, -1) dbu\n"))
);
}
let before = named_layout(vec![square()]);
let after = named_layout(vec![
moved_square,
ring(
RTAG_BOX,
RTAG_BOXTYPE,
1,
&[[20, 0], [30, 0], [30, 10], [20, 10], [20, 0]],
),
]);
let diff = Comparison::between(
&Document::parse(&before).unwrap(),
&Document::parse(&after).unwrap(),
);
assert!(
diff.text(100)
.contains(" 1/0 (WG): 1 rectangle moved · 1 box added\n")
);
assert!(
diff.text(2)
.contains(" 1/0 (WG): rectangle moved by (2, -1) dbu\n")
);
}
#[test]