Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Mesh imports: compare by pointer before comparing contents (#10490)

kaltenbrunnermarkusf233f000e808
diff --git a/crates/doc/src/document.rs b/crates/doc/src/document.rs
index 32b6128408..f00a62e145 100644
--- a/crates/doc/src/document.rs
+++ b/crates/doc/src/document.rs
@@ -2021,7 +2021,7 @@ fn fair_keep_edges() -> bool {
/// Triangles of one imported mesh body (millimetres). The arrays are shared by clones: every
/// edit keeps the design as it was for undo, and a scanned part's triangles (hundreds of
/// megabytes) are not copied for that.
-#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MeshData {
pub name: String,
/// x, y, z per vertex.
@@ -2032,6 +2032,20 @@ pub struct MeshData {
pub color: Option<[f32; 3]>,
}
+/// The same mesh: shared arrays are the same without reading them, so the dirty check and
+/// autosave after an edit elsewhere do not walk a scan's millions of coordinates. Arrays that
+/// are not shared compare value by value, as before. Coordinates are finite (a design that
+/// holds others does not load), so the shortcut only skips work.
+impl PartialEq for MeshData {
+ fn eq(&self, other: &Self) -> bool {
+ use std::sync::Arc;
+ self.name == other.name
+ && self.color == other.color
+ && (Arc::ptr_eq(&self.triangles, &other.triangles) || self.triangles == other.triangles)
+ && (Arc::ptr_eq(&self.positions, &other.positions) || self.positions == other.positions)
+ }
+}
+
/// Points of a sketch, by id.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, solidcraft_derive::Walk)]
pub struct SketchPoints {
diff --git a/crates/doc/src/eval_cost_tests.rs b/crates/doc/src/eval_cost_tests.rs
index 360eeaeb3a..db8cb6b634 100644
--- a/crates/doc/src/eval_cost_tests.rs
+++ b/crates/doc/src/eval_cost_tests.rs
@@ -851,3 +851,32 @@ fn saved_states_read_back_their_sketch_keys() {
assert!(with_sets(sets).final_state().is_err(), "base {base}");
}
}
+
+/// The dirty check and autosave compare the whole document after every edit; a scanned
+/// part's shared triangles must not be read for that. A coordinate that equals nothing (not a
+/// number) shows whether the shared arrays were read: only reading them makes a mesh differ
+/// from its own copy.
+#[test]
+fn comparing_a_copied_mesh_does_not_read_its_shared_arrays() {
+ let mesh = MeshData {
+ name: "Scan".into(),
+ positions: Arc::from([0., 0., 0., 1., 0., 0., 0., 1., f64::NAN]),
+ triangles: Arc::from([0, 1, 2]),
+ color: None,
+ };
+ let copy = mesh.clone();
+ assert!(copy == mesh, "a copy shares the arrays and is the same mesh without reading them");
+ let mut renamed = mesh.clone();
+ renamed.name = "Other".into();
+ assert!(renamed != mesh);
+ let mut coloured = mesh.clone();
+ coloured.color = Some([0.2, 0.4, 0.6]);
+ assert!(coloured != mesh);
+ let unshared = MeshData { positions: Arc::from(mesh.positions.to_vec()), ..mesh.clone() };
+ assert!(unshared != mesh, "arrays that are not shared still compare value by value");
+ let finite = MeshData { positions: Arc::from([0., 0., 0., 1., 0., 0., 0., 1., 0.]), ..mesh.clone() };
+ let equal = MeshData { positions: Arc::from(finite.positions.to_vec()), triangles: Arc::from(finite.triangles.to_vec()), ..finite.clone() };
+ assert!(equal == finite, "equal values in other arrays are the same mesh");
+ let flipped = MeshData { triangles: Arc::from([0, 2, 1]), ..finite.clone() };
+ assert!(flipped != finite);
+}