design: a whole-boundaries loop loads in any curve order (#6499) (#6516)

* claim: #6499

* Design: a whole-boundaries loop loads in any curve order (#6499)

---------

Co-authored-by: Framewrk CI <ci@framewrklabs.ai>
This commit is contained in:
Christopher KochandFramewrk CI authored and GitHub committed 2026-10-26 11:29:00 +00:00
1 parent c6d361347f
commit e6fb3793dc
3 files changed
+90 -17

No files matched your search

+25 -2
View File
@@ -98,13 +98,36 @@ pub enum PlaneRef {
/// Complete source curves of an outer boundary and each of its holes. Unlike a
/// span multiset, this identity is independent of adaptive spline subdivision.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
///
/// A loop is a set of curves: `outer` is kept sorted, and `holes` sorted by their own ids, as
/// the sketch's profiles produce them. Any start and either direction of the same loop is
/// accepted and made canonical on load, so a design written by hand or by another writer still
/// opens (#6499).
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct WholeProfileBoundary {
pub outer: Vec<String>,
pub holes: Vec<Vec<String>>,
}
impl<'de> Deserialize<'de> for WholeProfileBoundary {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> std::result::Result<Self, D::Error> {
/// The written form, before the loop order is dropped.
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Written {
outer: Vec<String>,
holes: Vec<Vec<String>>,
}
let Written { mut outer, mut holes } = Written::deserialize(deserializer)?;
outer.sort();
for hole in &mut holes {
hole.sort();
}
holes.sort();
Ok(Self { outer, holes })
}
}
/// Which closed profiles of a sketch a feature uses.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
+45 -15
View File
@@ -49,32 +49,35 @@ fn validate_whole_reference(value: &Value) -> Result<()> {
if total > 20_000 {
return Err(invalid());
}
let mut previous = None;
// Bound each raw ID before any owned allocation. Require canonical order.
// Bound each raw ID before any owned allocation. A loop is a set of curves: its
// written order (any start, either direction) carries no meaning, so it is made
// canonical here instead of refusing the design (#6499).
let mut ids = Vec::with_capacity(values.len());
for value in values {
let current = id(value)?;
if previous.is_some_and(|previous| previous >= current) {
return Err(invalid());
}
previous = Some(current);
ids.push(id(value)?.to_owned());
}
values.iter().map(|value| Ok(id(value)?.to_owned())).collect()
ids.sort();
if ids.windows(2).any(|pair| pair[0] == pair[1]) {
return Err(invalid());
}
Ok(ids)
};
let outer = boundary(profile.get("outer").ok_or_else(invalid)?)?;
let holes = profile.get("holes").and_then(Value::as_array).ok_or_else(invalid)?;
if holes.len() > 128 {
return Err(invalid());
}
let mut sorted_holes = Vec::with_capacity(holes.len());
let mut all: BTreeSet<_> = outer.iter().cloned().collect();
let mut canonical_holes = Vec::with_capacity(holes.len());
for hole in holes {
let hole = boundary(hole)?;
if sorted_holes.last().is_some_and(|previous| previous >= &hole) || hole.iter().any(|curve| !all.insert(curve.clone())) {
if hole.iter().any(|curve| !all.insert(curve.clone())) {
return Err(invalid());
}
sorted_holes.push(hole);
canonical_holes.push(hole);
}
if !identities.insert((outer, sorted_holes)) {
canonical_holes.sort();
if canonical_holes.windows(2).any(|pair| pair[0] == pair[1]) || !identities.insert((outer, canonical_holes)) {
return Err(invalid());
}
}
@@ -262,19 +265,19 @@ mod tests {
}
#[test]
fn whole_boundaries_raw_identity_is_bounded_canonical_and_complete() {
fn whole_boundaries_raw_identity_is_bounded_unique_and_complete() {
let good = json!({"type":"whole_boundaries","profiles":[{"outer":["s2"],"holes":[["s1"]]}]});
assert!(matches!(whole_profile_selection_from_value(&good).unwrap(), ProfileSel::WholeBoundaries { profiles } if profiles.len() == 1));
let malformed = [
json!({"type":"whole_boundaries","profiles":[]}),
json!({"type":"whole_boundaries","profiles":[{"outer":[],"holes":[]}]}),
json!({"type":"whole_boundaries","profiles":[{"outer":["s1","s1"],"holes":[]}]}),
json!({"type":"whole_boundaries","profiles":[{"outer":["s2","s1"],"holes":[]}]}),
json!({"type":"whole_boundaries","profiles":[{"outer":["s1"],"holes":[["s1"]]}]}),
json!({"type":"whole_boundaries","profiles":[{"outer":["s2"],"holes":[["s1"],["s1"]]}]}),
json!({"type":"whole_boundaries","profiles":[{"outer":["s2"],"holes":[["s3"],["s1"]]}]}),
json!({"type":"whole_boundaries","profiles":[{"outer":["s2"],"holes":[["s3"],["s1"]]},{"outer":["s2"],"holes":[["s1"],["s3"]]}]}),
json!({"type":"whole_boundaries","profiles":[{"outer":["s2"],"holes":[],"extra":true}]}),
json!({"type":"whole_boundaries","profiles":[{"outer":["s2"],"holes":[]},{"outer":["s2"],"holes":[]}]}),
json!({"type":"whole_boundaries","profiles":[{"outer":["s1","s2"],"holes":[]},{"outer":["s2","s1"],"holes":[]}]}),
json!({"type":"whole_boundaries","profiles":[{"outer":["x".repeat(513)],"holes":[]}]}),
json!({"type":"whole_boundaries","profiles":[{"outer":["a\nb"],"holes":[]}]}),
];
@@ -300,6 +303,33 @@ mod tests {
assert!(whole_profile_selection_from_value(&json!({"type":"whole_boundaries","profiles":aggregate})).is_err());
}
#[test]
fn a_whole_boundary_loop_is_accepted_in_any_written_order() {
// Any start and either direction of one closed loop name the same region (#6499).
for curves in
[json!(["s1", "s2", "s3", "s4"]), json!(["s2", "s3", "s4", "s1"]), json!(["s4", "s3", "s2", "s1"]), json!(["s3", "s4", "s1", "s2"])]
{
let value = json!({"type":"whole_boundaries","profiles":[{"outer":curves,"holes":[]}]});
let Some(ProfileSel::WholeBoundaries { profiles }) = whole_profile_selection_from_value(&value).ok() else {
panic!("reversed loop {value} refused");
};
assert_eq!(profiles.first().map(|profile| profile.outer.clone()), Some(["s1", "s2", "s3", "s4"].map(str::to_string).to_vec()));
assert!(profiles.first().is_some_and(|profile| profile.holes.is_empty()));
assert!(validate_raw(&json!({"features":[{"type":"extrude","profiles":value}]})).is_ok());
}
// Holes are a set of loops too, and the identity is canonical, so the same loop written
// in another order is still the same profile.
let holey = json!({"type":"whole_boundaries","profiles":[{"outer":["o2","o1"],"holes":[["h2","h3"],["h1"]]}]});
let Some(ProfileSel::WholeBoundaries { profiles }) = whole_profile_selection_from_value(&holey).ok() else {
panic!("hole loop order refused");
};
let profile = profiles.first().cloned().unwrap();
assert_eq!(profile.outer, ["o1".to_string(), "o2".to_string()]);
assert_eq!(profile.holes, vec![vec!["h1".to_string()], vec!["h2".to_string(), "h3".to_string()]]);
let saved = serde_json::to_value(ProfileSel::WholeBoundaries { profiles }).unwrap_or_default();
assert_eq!(saved, json!({"type":"whole_boundaries","profiles":[{"outer":["o1","o2"],"holes":[["h1"],["h2","h3"]]}]}));
}
#[test]
fn raw_offset_roles_reject_aliases_order_damage_and_hostile_shapes() {
let good = fixture();
+20
View File
@@ -363,6 +363,7 @@ pub fn capture_profile_selection(ss: &SolvedSketch, indices: &[usize]) -> Result
mod tests {
#![allow(clippy::unwrap_used, clippy::panic)]
use super::*;
use serde_json::json;
use solidcraft_geom::{Plane, Vec2};
use solidcraft_sketch::{Sketch, find_profiles, solve};
@@ -518,6 +519,25 @@ mod tests {
assert!(profile_selection_indices(&ss, &ProfileSel::Indices { indices: vec![1] }).is_err());
}
#[test]
fn a_whole_source_selection_loads_in_any_curve_order() {
let ss = solved_square(&[]);
assert_eq!(ss.profiles.len(), 1);
let canonical = ProfileSel::WholeBoundaries {
profiles: vec![WholeProfileBoundary { outer: ["a", "b", "c", "d"].map(str::to_string).to_vec(), holes: Vec::new() }],
};
assert_eq!(profile_selection_indices(&ss, &canonical).unwrap(), vec![0]);
// The same loop written from another corner, or the other way round, names the same
// region: a design saved that way opens instead of being refused as damaged (#6499).
for order in [["b", "c", "d", "a"], ["d", "c", "b", "a"], ["c", "d", "a", "b"]] {
let written = json!({"type":"whole_boundaries","profiles":[{"outer":order,"holes":[]}]});
let loaded: ProfileSel = serde_json::from_value(written).unwrap();
assert_eq!(loaded, canonical, "the loaded identity is canonical");
assert_eq!(profile_selection_indices(&ss, &loaded).unwrap(), vec![0]);
assert_eq!(crate::select_profiles(&ss, &loaded).unwrap().len(), 1);
}
}
fn solved_square(extra: &[(&str, [f64; 4])]) -> SolvedSketch {
let mut sketch = Sketch::new();
let corners = [Vec2::new(0., 0.), Vec2::new(10., 0.), Vec2::new(10., 10.), Vec2::new(0., 10.)];