diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 6bc558f23..11f59691d 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -132,6 +132,11 @@ pub fn anchor(app: &SolveApp, d: &Dialog) -> Option<(Vec3, Option)> { (at, along_face.or_else(|| st.body(body).and_then(|b| blend_grow_dir(&b.mesh(), *index, at)))) } Kind::Fillet { .. } | Kind::Shell { .. } => (at, normal.map(|n| -n)), + // A hem's handle starts on the centre line of its layer and runs to its tip (#5828). + Kind::Sheet(crate::dialogs_sheet::Sm::Hem { .. }) => match crate::dialogs_sheet::hem_handle(app, d) { + Some((start, dir)) => (start, Some(dir)), + None => (at, normal), + }, Kind::Sheet(_) => (at, crate::dialogs_sheet::arrow(app, d).or(normal)), Kind::Plastic(_) => (at, normal), Kind::PatternRect { .. } => (at, d.inputs.get(1).and_then(|i| i.items.first()).and_then(|x| crate::dialogs::axis_of(app, x)).map(|a| a.1)), diff --git a/crates/ui-egui/src/dialogs_sheet.rs b/crates/ui-egui/src/dialogs_sheet.rs index e5138081d..560b10ca6 100644 --- a/crates/ui-egui/src/dialogs_sheet.rs +++ b/crates/ui-egui/src/dialogs_sheet.rs @@ -715,15 +715,24 @@ pub fn for_feature(app: &SolveApp, kind: &FeatureKind) -> Option<(Kind, Vec Option { let Kind::Sheet(k) = &d.kind else { return None }; let flip = match k { Sm::Flange { ty: 1, flip, .. } => *flip, - Sm::Hem { flip, .. } => !flip, + Sm::Hem { .. } => return hem_handle(app, d).map(|(_, dir)| dir), _ => return None, }; + let (n, _, _) = edge_frame(app, d)?; + Some(if flip { -n } else { n }) +} + +/// The sheet at the dialog's first picked edge: the unit normal of the larger neighbouring face +/// (out of the sheet), the unit normal of the other neighbour (the edge's end face, pointing away +/// from the sheet), and the sheet's thickness along the first. +fn edge_frame(app: &SolveApp, d: &Dialog) -> Option<(Vec3, Vec3, f64)> { let Sel::Edge { body, index, point } = d.inputs.first()?.items.first()? else { return None }; let st = app.session.world_state(); let m = st.body(body)?.mesh(); @@ -737,16 +746,40 @@ pub fn arrow(app: &SolveApp, d: &Dialog) -> Option { .map(|[a, b, c]| (b - a).cross(c - a).len() * 0.5) .sum() }; + let normal = |f: u32| -> Option { + m.triangles + .iter() + .zip(&m.tri_face) + .filter(|(_, tf)| **tf == f) + .filter_map(|(t, _)| m.tri(t)) + .min_by(|a, b| ((a[0] + a[1] + a[2]) / 3.0).dist(*point).total_cmp(&((b[0] + b[1] + b[2]) / 3.0).dist(*point))) + .and_then(|[a, b, c]| (b - a).cross(c - a).normalized()) + }; let f = faces.iter().copied().max_by(|a, b| area(*a).total_cmp(&area(*b)))?; - let n = m - .triangles - .iter() - .zip(&m.tri_face) - .filter(|(_, tf)| **tf == f) - .filter_map(|(t, _)| m.tri(t)) - .min_by(|a, b| ((a[0] + a[1] + a[2]) / 3.0).dist(*point).total_cmp(&((b[0] + b[1] + b[2]) / 3.0).dist(*point))) - .and_then(|[a, b, c]| (b - a).cross(c - a).normalized())?; - Some(if flip { -n } else { n }) + let g = faces.iter().copied().find(|g| *g != f)?; + let (n, end) = (normal(f)?, normal(g)?); + // The end face spans the sheet's thickness along the normal. + let heights = m.triangles.iter().zip(&m.tri_face).filter(|(_, tf)| **tf == g).filter_map(|(t, _)| m.tri(t)).flatten().map(|v| v.dot(n)); + let (lo, hi) = heights.fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), h| (lo.min(h), hi.max(h))); + let thickness = hi - lo; + (thickness.is_finite() && thickness > 0.0).then_some((n, end, thickness)) +} + +/// Where a hem's handle starts and where it points (#5828). Its Length is measured from the outer +/// face of the fold to the hem's tip, so the handle starts on the centre line of the hem layer at +/// that face and runs along the hem toward the tip: square to the picked edge, back over the sheet. +/// The layer lies on the far side of the sheet from the larger face unless flipped. The Gap is +/// taken from the dialog when it holds a number; otherwise the layer sits flat against the sheet. +pub fn hem_handle(app: &SolveApp, d: &Dialog) -> Option<(Vec3, Vec3)> { + let Kind::Sheet(Sm::Hem { gap, flip, .. }) = &d.kind else { return None }; + let Sel::Edge { point, .. } = d.inputs.first()?.items.first()? else { return None }; + let (n, end, thickness) = edge_frame(app, d)?; + let gap = app.session.doc.eval(gap, ValueKind::Length).ok().filter(|g| g.is_finite() && *g >= 0.0).unwrap_or(0.0); + // The hem's side of the sheet, as a unit vector along the larger face's normal, and the + // distance of the layer's centre line from the face the edge lies on. + let (side, depth) = if *flip { (n, gap + thickness / 2.0) } else { (-n, thickness + gap + thickness / 2.0) }; + let start = *point + end * (thickness + gap) + side * depth; + Some((start, -end)) } /// Bend line colours: up and down (theme tokens, legible on either theme and in every colour diff --git a/crates/ui-egui/src/dialogs_sheet_tests.rs b/crates/ui-egui/src/dialogs_sheet_tests.rs index b752350f2..eef150156 100644 --- a/crates/ui-egui/src/dialogs_sheet_tests.rs +++ b/crates/ui-egui/src/dialogs_sheet_tests.rs @@ -69,33 +69,6 @@ fn flange_follows_the_picks() { } } -#[test] -fn hem_arrow_points_to_the_side_the_hem_builds_on() { - for mode in [crate::shell::LayoutMode::Floating, crate::shell::LayoutMode::Docked] { - for z in [0.0, 2.5] { - for flipped in [false, true] { - let mut app = plate(mode); - app.run("sheet.flange", json!({"sketch": "Base"})).unwrap(); - let body = app.session.model.state().sheets[0].body.clone(); - let before = app.session.world_state().body(&body).unwrap().mesh().bounds(); - app.session.selection = vec![edge_sel(&app.session, Vec3::new(50.0, 0.0, z)).unwrap()]; - app.start("sheet.hem"); - let mut d = app.dialog.clone().unwrap(); - let Kind::Sheet(Sm::Hem { flip, .. }) = &mut d.kind else { panic!("hem dialog") }; - *flip = flipped; - let direction = arrow(&app, &d).unwrap(); - let commands = apply_commands(&app, &d).unwrap(); - run_all(&mut app, commands); - let after = app.session.world_state().body(&body).unwrap().mesh().bounds(); - assert!( - direction.dot(after.center() - before.center()) > 1e-6, - "the arrow must point toward the added hem: z={z}, flip={flipped}, arrow={direction:?}, before={before:?}, after={after:?}" - ); - } - } - } -} - /// Hem takes edges and an optional gap; rules make and pick a rule with its expressions. #[test] fn hem_and_rules() { @@ -953,3 +926,34 @@ fn flat_perforations_cull_offscreen_and_subpixel_holes() { app.flat_view.cam.distance = fitted_distance * 1000.0; assert!(flat_circles(&mut app, &dialog, rect).is_empty()); } + +/// The hem's handle sits on the centre line of the hem layer, at the outer face of its fold, and +/// points along the hem toward its tip (#5828): dragging it changes the Length, which is measured +/// from the fold's outer face to the tip, so the handle's start plus Length lands on the tip. +#[test] +fn hem_handle_runs_along_the_hem_to_its_tip() { + for flipped in [false, true] { + let mut app = plate(crate::shell::LayoutMode::Floating); + app.run("sheet.flange", json!({"sketch": "Base"})).unwrap(); + let body = app.session.model.state().sheets[0].body.clone(); + app.session.selection = vec![edge_sel(&app.session, Vec3::new(50.0, 0.0, 2.5)).unwrap()]; + app.start("sheet.hem"); + let mut d = app.dialog.clone().unwrap(); + let Kind::Sheet(Sm::Hem { flip, .. }) = &mut d.kind else { panic!("hem dialog") }; + *flip = flipped; + let (at, dir) = crate::canvas::anchor(&app, &d).unwrap(); + let dir = dir.unwrap(); + let commands = apply_commands(&app, &d).unwrap(); + run_all(&mut app, commands); + // The 5 mm hem layer: its tip is the far end of the added layer, along the sheet. + let m = app.session.world_state().body(&body).unwrap().mesh(); + // The straight part of the layer, past the fold. + let layer_z = |z: f64| if flipped { z > 2.505 } else { z < -0.01 }; + let tip_y = m.positions.iter().filter(|v| layer_z(v.z)).map(|v| v.y).fold(f64::NEG_INFINITY, f64::max); + let layer: Vec = m.positions.iter().filter(|v| layer_z(v.z) && v.y > 0.5).map(|v| v.z).collect(); + let centre_z = (layer.iter().copied().fold(f64::INFINITY, f64::min) + layer.iter().copied().fold(f64::NEG_INFINITY, f64::max)) / 2.0; + assert!(dir.dist(Vec3::Y) < 1e-6, "flip={flipped}: the handle points along the hem to its tip, got {dir:?}"); + assert!((at.z - centre_z).abs() < 0.05, "flip={flipped}: the handle starts on the hem's centre line: {at:?} vs z={centre_z}"); + assert!((at.y + 5.0 - tip_y).abs() < 0.05, "flip={flipped}: start plus Length reaches the tip: {at:?} vs y={tip_y}"); + } +}