Files
makeCAD/crates/ui-egui/src/selection.rs
T
Karl Von Richter 27d2fe6ec0 Frames: profile library, members along frame sketch lines, and the Frame Member dialog (#4003) (#4004)
* claim: #4003

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Frames: the profile library (#4003)

The first slice of frames and weldments: the cross-sections structural members
will be made of, each SolidCraft's own parametric definition built in code
(no vendor or standard size tables are shipped).

- solidcraft_doc::frame: bar, round bar, rectangular and round tube, angle,
  channel, I-beam and a generic T-slot extrusion as exact line/arc regions,
  with defaults worked out from the profile's size, refusals that name the
  dimension that does not fit, anchors and placement, and exact section
  properties (area, centroid, Ixx/Iyy/Ixy by Green's theorem).
- frame.profile.list, frame.profile.properties (with mass per metre for a
  density) and frame.profile.insert (draws a profile into a sketch, locked by
  default, at a point, anchor and angle).
- Tests against hand-computed values (rectangles, circles, composite
  rectangles by the parallel-axis theorem, fillet spandrels), hostile input,
  extrusion volume, undo; catalog entries in every locale; docs/frames.md;
  ROADMAP M15.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Frames: drop the built-in T-slot profile (#4003)

Aluminium extrusions and other vendor sections come from the user's own
sketch (drawn or inserted from the vendor's DXF), so the library keeps the
plain steel shapes only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Frames: members along frame sketch lines (#4003)

frame.member lays one body per straight line of a frame sketch (2D lines or
3D sketch lines), its cross-section square to the line from end to end:

- profile: a built-in profile, or profile_sketch, a sketch whose material
  (nested loops alternating, so two circles make a tube) is the section in
  its own x and y around its origin; profiles picks override it.
- anchor (origin, centre, centroid or a bounding-box corner or edge middle),
  angle (an angle expression, so a parameter), mirror.
- Orientation: the profile's y as near world up as the line allows, x
  square to both; a vertical member's x is world X (frame::member_axes).
- Members follow sketch and parameter edits, undo as one step, save and
  reopen (format: pending/frame-members.rs), and are named after the profile
  (Rect Tube 40x40x3, (2)…), numbering on past names already taken.

Tests against hand-computed volumes and bounding boxes (rectangle, 3D
tower lines, parameter edit, sketch tube profile and picks, anchor/angle/
mirror), refusals, undo/redo and reopen; walk fixtures and goldens; docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Frames: fixes from the adversarial review (#4003)

- Default member lines are what was drawn: projected and other linked curves
  and 3D wires (a face sketch's edges) and lines of no length are left out.
- A profile sketch's material starts from its drawn profiles, as
  solid.extrude's "all" does, so a section sketched on a face works; the
  even-odd test uses outlines and boxes worked out once, capped at 256.
- Member names are pinned to their lines (`names`, by line id; `body_name`
  is the base): deleting or adding a line, or deleting another feature, no
  longer renames other members, and frame.member stores every name it gives.
  body.rename renames a member on its line (also for later member features);
  a copy whose body_names were set still names its members by position.
- Built-in profile dimensions are expressions (ProfileSpec): they follow
  parameters, bare numbers take the design's units, and they are feature
  inputs. frame.profile.properties/insert read them the same way.
- Default root/toe radii shrink to the room a thick section leaves; radii
  given are checked by name; a radius must be 0 or at least 0.01 mm; error
  messages report the wall or flange value given, not twice it.
- A line within 1e-6 rad of vertical orients as vertical, so rounding never
  turns a post a quarter turn.
- A 2D frame sketch in a differently placed component is mapped into the
  member's frame, and member features recompute when placements change.
- Caps before lookups for a design's `lines`; a line listed twice is
  refused; a 3D wire that runs back on itself is not a line.

Regression tests for each, from the review's scenarios.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Frames: the Frame Member dialog on the ribbon (#4003)

- Frame Member (MODEL > Create) opens a dialog: the frame sketch's lines
  picked in the view (or All Lines), a built-in profile and its dimensions or
  a section picked in another sketch, anchor, angle, mirror and name, with a
  live preview. Edit Feature reopens it filled in.
- Lines picked in the view keep their sketch (a SKETCH_LINES input picks
  ExternalSketchCurve): line ids are each sketch's own, so two 3D sketches
  both have a `w2`, and the members went on the newer sketch's lines.
- timeline.redefine takes frame.member. Members keep their names through an
  edit unless it gives them a new body_name; frame.member takes `names`.
- A member whose section is a whole profile sketch keeps it when edited
  (the dialog shows the sketch instead of picking every region, which would
  fill a tube's hole).
- An original frame_member icon, open for review, on the ribbon, timeline
  and browser.
- The angle profile is listed as L-Angle, and web and flange as Web/Flange
  thickness, so they translate apart from the existing Angle, Web and Flange;
  the dialog's labels are in en, ja and zh. en-GB no longer repeats the frame
  command labels it spells the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 14:22:41 +00:00

341 lines
14 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Selection inputs and the geometry behind picking: what a command input accepts, the origin
//! widget (a fixed size on screen), ray tests against planar quads, and window/crossing box
//! selection. The pure parts are unit tested; the viewport uses them.
use egui::{Pos2, Rect};
use solidcraft_engine::Sel;
use solidcraft_engine::geom::Vec3;
use crate::viewport::Hit;
/// Model picks retain an on-surface point, but their identity is the body and topology index.
pub(crate) fn same_item(a: &Sel, b: &Sel) -> bool {
match (a, b) {
(Sel::Edge { body: a, index: i, .. }, Sel::Edge { body: b, index: j, .. })
| (Sel::Face { body: a, index: i, .. }, Sel::Face { body: b, index: j, .. }) => a == b && i == j,
_ => a == b,
}
}
/// What a selection input accepts (bit set).
pub type Accept = u16;
pub const PROFILES: Accept = 1;
pub const EDGES: Accept = 2;
pub const FACES: Accept = 4;
/// Planar faces only.
pub const PLANAR_FACES: Accept = 8;
pub const BODIES: Accept = 16;
/// Origin and construction planes.
pub const PLANES: Accept = 32;
/// Origin axes and straight sketch curves.
pub const AXES: Accept = 64;
pub const VERTICES: Accept = 128;
/// Any sketch curve (a sweep path).
pub const CURVES: Accept = 256;
/// Sketch points of any visible sketch (hole centres); picked as "<sketch id>:<point id>".
pub const POINTS: Accept = 512;
/// Timeline features (pattern objects), picked in the timeline.
pub const FEATURES: Accept = 1024;
/// Points of the sketch being modified; unlike POINTS these never enable cross-sketch picking.
pub const SKETCH_POINTS: Accept = 2048;
/// The origin point, picked as a vertex of no body (in the viewport or the browser's Origin).
pub const ORIGIN: Accept = 4096;
/// Curves of any sketch, picked with their sketch (`ExternalSketchCurve`): curve ids are a
/// sketch's own, so two sketches can both have a `w2` (frame member lines).
pub const SKETCH_LINES: Accept = 8192;
/// One selection input of a command dialog ("Profiles", "Edges", "Plane", ...).
#[derive(Clone, Debug, PartialEq)]
pub struct SelInput {
pub label: &'static str,
pub accept: Accept,
/// Many items (clicks toggle them), or one (a click replaces it).
pub multi: bool,
/// Face picks are places on the face, not the face: several on one face add up (a click on
/// the very spot of one takes it out). For points that pick pieces a face is cut into.
pub places: bool,
pub items: Vec<Sel>,
}
impl SelInput {
pub fn new(label: &'static str, accept: Accept, multi: bool) -> SelInput {
SelInput { label, accept, multi, places: false, items: Vec::new() }
}
/// This input with face picks as places (see [`SelInput::places`]).
pub fn places(mut self) -> SelInput {
self.places = true;
self
}
/// The selection a hit stands for, if this input accepts it.
pub fn accepts(&self, h: &Hit, planar: impl Fn(&str, usize) -> bool) -> Option<Sel> {
let a = self.accept;
match h {
Hit::Profile { sketch, index } if a & PROFILES != 0 => Some(Sel::Profile { sketch: *sketch, index: *index }),
Hit::Edge { body, index, mid } if a & EDGES != 0 => Some(Sel::Edge { body: body.clone(), index: *index, point: *mid }),
Hit::Face { body, index, point } if a & FACES != 0 || (a & PLANAR_FACES != 0 && planar(body, *index)) => {
Some(Sel::Face { body: body.clone(), index: *index, point: *point })
}
Hit::Face { body, .. } if a & BODIES != 0 => Some(Sel::Body { name: body.clone() }),
Hit::Plane { name, .. } if a & PLANES != 0 => Some(Sel::Plane { name: name.clone() }),
Hit::Axis { name } if a & AXES != 0 => Some(Sel::Axis { name: name.clone() }),
Hit::SketchCurve { id, straight: true, .. } if a & AXES != 0 => Some(Sel::SketchCurve { id: id.clone() }),
Hit::SketchCurve { sketch, id, .. } if a & SKETCH_LINES != 0 => Some(Sel::ExternalSketchCurve { sketch: *sketch, id: id.clone() }),
Hit::SketchCurve { id, .. } if a & CURVES != 0 => Some(Sel::SketchCurve { id: id.clone() }),
Hit::SketchPoint { id, .. } if a & SKETCH_POINTS != 0 => Some(Sel::SketchPoint { id: id.clone() }),
Hit::SketchPoint { sketch, id, .. } if a & POINTS != 0 => Some(Sel::SketchPoint { id: format!("{sketch}:{id}") }),
Hit::Vertex { body, point } if body.is_empty() && a & ORIGIN != 0 => Some(Sel::Vertex { body: String::new(), point: *point }),
Hit::Vertex { body, point } if !body.is_empty() && a & VERTICES != 0 => Some(Sel::Vertex { body: body.clone(), point: *point }),
_ => None,
}
}
/// Add or remove items: in a multi input a pick toggles, otherwise it replaces.
pub fn toggle(&mut self, picked: Vec<Sel>) {
if picked.is_empty() {
return;
}
if !self.multi {
self.items = picked.into_iter().take(1).collect();
return;
}
// A pick of something already in (or a chain fully in) takes it out.
let places = self.places;
// Whether two picks are the same item of this input.
let same = |a: &Sel, b: &Sel| match (a, b) {
(Sel::Face { point: p, .. }, Sel::Face { point: q, .. }) if places => same_item(a, b) && p.dist(*q) <= 1e-9,
_ => same_item(a, b),
};
if picked.iter().all(|p| self.items.iter().any(|x| same(x, p))) {
self.items.retain(|x| !picked.iter().any(|p| same(x, p)));
} else {
for p in picked {
if !self.items.iter().any(|x| same(x, &p)) {
self.items.push(p);
}
}
}
}
}
/// Projections preserve pick order and repetitions; callers own any deduplication or frames.
pub(crate) trait SelectionExt {
fn body_names(&self) -> Vec<String>;
fn curve_ids(&self) -> Vec<String>;
fn profiles(&self) -> Vec<(u64, usize)>;
fn edge_points(&self) -> Vec<Vec3>;
fn face_points(&self) -> Vec<Vec3>;
}
impl SelectionExt for [Sel] {
fn body_names(&self) -> Vec<String> {
self.iter().filter_map(|x| if let Sel::Body { name } = x { Some(name.clone()) } else { None }).collect()
}
fn curve_ids(&self) -> Vec<String> {
self.iter().filter_map(|x| if let Sel::SketchCurve { id } = x { Some(id.clone()) } else { None }).collect()
}
fn profiles(&self) -> Vec<(u64, usize)> {
self.iter().filter_map(|x| if let Sel::Profile { sketch, index } = x { Some((*sketch, *index)) } else { None }).collect()
}
fn edge_points(&self) -> Vec<Vec3> {
self.iter().filter_map(|x| if let Sel::Edge { point, .. } = x { Some(*point) } else { None }).collect()
}
fn face_points(&self) -> Vec<Vec3> {
self.iter().filter_map(|x| if let Sel::Face { point, .. } = x { Some(*point) } else { None }).collect()
}
}
pub(crate) fn items(inputs: &[SelInput], i: usize) -> &[Sel] {
inputs.get(i).map(|x| x.items.as_slice()).unwrap_or(&[])
}
pub(crate) fn point_value(v: Vec3) -> serde_json::Value {
serde_json::json!([v.x, v.y, v.z])
}
pub(crate) fn polyline_mid(pts: &[Vec3]) -> Vec3 {
let len: f64 = pts.windows(2).map(|w| w[0].dist(w[1])).sum();
let mut acc = 0.0;
for w in pts.windows(2) {
let l = w[0].dist(w[1]);
if acc + l >= len * 0.5 && l > 0.0 {
return w[0].lerp(w[1], (len * 0.5 - acc) / l);
}
acc += l;
}
pts.first().copied().unwrap_or_default()
}
/// World size of the origin widget: a fixed fraction of the viewport height, whatever the zoom
/// (`half_height` is half the visible height in mm at the target).
pub fn origin_size(half_height: f64) -> f64 {
(half_height * 2.0 * 0.075).max(1e-6)
}
/// The three origin plane squares (positive quadrant): name, plane normal and corners.
pub fn origin_quads(size: f64) -> [(&'static str, Vec3, [Vec3; 4]); 3] {
let s = size;
[
("XY", Vec3::Z, [Vec3::ZERO, Vec3::new(s, 0.0, 0.0), Vec3::new(s, s, 0.0), Vec3::new(0.0, s, 0.0)]),
("XZ", Vec3::new(0.0, -1.0, 0.0), [Vec3::ZERO, Vec3::new(s, 0.0, 0.0), Vec3::new(s, 0.0, s), Vec3::new(0.0, 0.0, s)]),
("YZ", Vec3::X, [Vec3::ZERO, Vec3::new(0.0, s, 0.0), Vec3::new(0.0, s, s), Vec3::new(0.0, 0.0, s)]),
]
}
/// Ray parameter where the ray (o, d) hits the planar quad (corners in order), if it does.
pub fn ray_quad(o: Vec3, d: Vec3, q: &[Vec3; 4]) -> Option<f64> {
let [a, b, _, dd] = *q;
let (u, v) = (b - a, dd - a);
let n = u.cross(v);
let den = n.dot(d);
if den.abs() < 1e-12 {
return None;
}
let t = n.dot(a - o) / den;
if !(t.is_finite() && t >= 0.0) {
return None;
}
let p = o + d * t - a;
// Coordinates in the (u, v) frame (the quad is a parallelogram).
let (uu, uv, vv) = (u.dot(u), u.dot(v), v.dot(v));
let det = uu * vv - uv * uv;
if det.abs() < 1e-300 {
return None;
}
let (pu, pv) = (p.dot(u), p.dot(v));
let s = (pu * vv - pv * uv) / det;
let r = (pv * uu - pu * uv) / det;
((0.0..=1.0).contains(&s) && (0.0..=1.0).contains(&r)).then_some(t)
}
/// A rectangle dragged in the viewport selects by window (dragged left to right: items fully
/// inside) or by crossing (right to left: items touching it).
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BoxSel {
pub rect: Rect,
pub crossing: bool,
}
impl BoxSel {
pub fn from_drag(start: Pos2, end: Pos2) -> BoxSel {
BoxSel { rect: Rect::from_two_pos(start, end), crossing: end.x < start.x }
}
/// Does the polyline (screen points) count as selected?
pub fn takes(&self, pts: &[Pos2]) -> bool {
if pts.is_empty() {
return false;
}
if !self.crossing {
return pts.iter().all(|p| self.rect.contains(*p));
}
pts.iter().any(|p| self.rect.contains(*p)) || pts.windows(2).any(|w| segment_hits_rect(w[0], w[1], self.rect))
}
}
/// Does the segment a–b cross the rectangle's boundary or lie inside it?
pub fn segment_hits_rect(a: Pos2, b: Pos2, r: Rect) -> bool {
if r.contains(a) || r.contains(b) {
return true;
}
let c = [r.left_top(), r.right_top(), r.right_bottom(), r.left_bottom()];
(0..4).any(|i| segments_cross(a, b, c[i], c[(i + 1) % 4]))
}
fn segments_cross(p: Pos2, q: Pos2, r: Pos2, s: Pos2) -> bool {
let o = |a: Pos2, b: Pos2, c: Pos2| (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
let (d1, d2, d3, d4) = (o(r, s, p), o(r, s, q), o(p, q, r), o(p, q, s));
(d1 > 0.0) != (d2 > 0.0) && (d3 > 0.0) != (d4 > 0.0)
}
#[cfg(test)]
mod tests {
use super::*;
use egui::pos2;
use solidcraft_engine::render::{Camera, StandardView};
#[test]
fn origin_widget_keeps_its_screen_size() {
let (w, h) = (1200.0, 800.0);
let mut sizes = Vec::new();
for distance in [20.0, 200.0, 5000.0] {
let mut cam = Camera { distance, ..Default::default() };
cam.set_view(StandardView::Front);
let s = origin_size(cam.half_height());
let a = cam.to_screen(Vec3::ZERO, w, h, distance).unwrap();
let b = cam.to_screen(Vec3::new(s, 0.0, 0.0), w, h, distance).unwrap();
sizes.push(b.0 - a.0);
}
assert!(sizes.iter().all(|x| (x - sizes[0]).abs() < 1e-6 && *x > 40.0), "{sizes:?}");
}
#[test]
fn rays_hit_origin_quads() {
let q = origin_quads(10.0);
// Straight down onto XY at (3, 4).
let t = ray_quad(Vec3::new(3.0, 4.0, 50.0), Vec3::new(0.0, 0.0, -1.0), &q[0].2).unwrap();
assert!((t - 50.0).abs() < 1e-12);
// Outside the quadrant: misses.
assert!(ray_quad(Vec3::new(-3.0, 4.0, 50.0), Vec3::new(0.0, 0.0, -1.0), &q[0].2).is_none());
// Parallel: misses.
assert!(ray_quad(Vec3::new(3.0, 4.0, 50.0), Vec3::X, &q[0].2).is_none());
// From the front onto XZ.
assert!(ray_quad(Vec3::new(5.0, -100.0, 5.0), Vec3::Y, &q[1].2).is_some());
// Behind the ray origin: misses.
assert!(ray_quad(Vec3::new(3.0, 4.0, -50.0), Vec3::new(0.0, 0.0, -1.0), &q[0].2).is_none());
}
#[test]
fn window_and_crossing_selection() {
let inside = [pos2(20.0, 20.0), pos2(40.0, 30.0)];
let partly = [pos2(20.0, 20.0), pos2(140.0, 30.0)];
let through = [pos2(-10.0, 50.0), pos2(200.0, 50.0)];
let away = [pos2(300.0, 300.0), pos2(320.0, 330.0)];
// Left to right: window.
let w = BoxSel::from_drag(pos2(0.0, 0.0), pos2(100.0, 100.0));
assert!(!w.crossing);
assert!(w.takes(&inside) && !w.takes(&partly) && !w.takes(&through) && !w.takes(&away));
// Right to left: crossing.
let c = BoxSel::from_drag(pos2(100.0, 100.0), pos2(0.0, 0.0));
assert!(c.crossing);
assert!(c.takes(&inside) && c.takes(&partly) && c.takes(&through) && !c.takes(&away));
}
#[test]
fn inputs_toggle_and_filter() {
let mut inp = SelInput::new("Edges", EDGES, true);
let e = |i: usize| Sel::Edge { body: "B".into(), index: i, point: Vec3::new(i as f64, 0.0, 0.0) };
inp.toggle(vec![e(1)]);
inp.toggle(vec![e(2), e(3)]);
assert_eq!(inp.items.len(), 3);
inp.toggle(vec![e(2), e(3)]);
assert_eq!(inp.items, vec![e(1)]);
let face = Hit::Face { body: "B".into(), index: 0, point: Vec3::ZERO };
assert!(inp.accepts(&face, |_, _| true).is_none());
let planes = SelInput::new("Plane", PLANES | PLANAR_FACES, false);
assert!(planes.accepts(&face, |_, _| true).is_some());
assert!(planes.accepts(&face, |_, _| false).is_none());
let mut one = SelInput::new("Plane", PLANES, false);
one.toggle(vec![Sel::Plane { name: "XY".into() }]);
one.toggle(vec![Sel::Plane { name: "YZ".into() }]);
assert_eq!(one.items, vec![Sel::Plane { name: "YZ".into() }]);
}
}
#[cfg(test)]
mod shared_geometry_baseline_tests {
use super::*;
#[test]
fn polyline_mid_preserves_degenerate_and_unequal_segments() {
let p = Vec3::new(10.0, -3.0, 7.0);
assert_eq!(polyline_mid(&[]), Vec3::ZERO);
assert_eq!(polyline_mid(&[p]), p);
assert_eq!(polyline_mid(&[p, p, p]), p);
assert_eq!(polyline_mid(&[p, p + Vec3::X, p + Vec3::X * 10.0]), p + Vec3::X * 5.0);
assert_eq!(polyline_mid(&[p, p, p + Vec3::X * 2.0, p + Vec3::X * 2.0 + Vec3::Y * 6.0]), p + Vec3::X * 2.0 + Vec3::Y * 2.0);
assert_eq!(polyline_mid(&[p, Vec3::new(f64::NAN, 0.0, 0.0)]), p);
}
}