* claim: #5457 * fix: align new straight-edge joint snaps with the edge (#5457)
This commit is contained in:
committed by
GitHub
parent
0477e0ba4e
commit
1f93b6d39c
@@ -422,6 +422,19 @@ pub fn circle_of(pts: &[Vec3]) -> Option<(Vec3, Vec3)> {
|
||||
pts.iter().all(|p| (p.dist(center) - r).abs() < tol && (*p - center).dot(normal).abs() < tol).then_some((center, normal))
|
||||
}
|
||||
|
||||
/// Axis of a straight edge. Its largest component points positive so reversing the
|
||||
/// tessellation does not reverse a joint's drive direction.
|
||||
fn straight_edge_axis(pts: &[Vec3]) -> Option<Vec3> {
|
||||
let (a, b) = (*pts.first()?, *pts.last()?);
|
||||
let dir = (b - a).normalized()?;
|
||||
let tol = 1e-6 * a.dist(b).max(1.0);
|
||||
if !pts.iter().all(|p| (*p - a).cross(dir).len() < tol) {
|
||||
return None;
|
||||
}
|
||||
let leading = [dir.x, dir.y, dir.z].into_iter().max_by(|a, b| a.abs().total_cmp(&b.abs()))?;
|
||||
Some(if leading < 0.0 { -dir } else { dir })
|
||||
}
|
||||
|
||||
/// Normal of the mesh face `f` near `p`.
|
||||
fn face_normal_near(m: &Mesh, f: u32, p: Vec3) -> Option<Vec3> {
|
||||
m.triangles
|
||||
@@ -473,7 +486,9 @@ pub fn snap_of(s: &Session, sel: &Sel) -> Option<SnapPt> {
|
||||
}
|
||||
None => {
|
||||
let mid = polyline_mid(e);
|
||||
let z = m.edge_faces.get(*index).and_then(|fs| fs.first()).and_then(|f| face_normal_near(&m, *f, mid)).unwrap_or(Vec3::Z);
|
||||
let z = straight_edge_axis(e)
|
||||
.or_else(|| m.edge_faces.get(*index).and_then(|fs| fs.first()).and_then(|f| face_normal_near(&m, *f, mid)))
|
||||
.unwrap_or(Vec3::Z);
|
||||
(body, SnapPt { at: mid, z, kind: SnapKind::EdgeMid, param: json!({"point": pt(mid), "z": pt(z)}) })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use solidcraft_engine::Session;
|
||||
|
||||
use super::*;
|
||||
use crate::Services;
|
||||
use crate::dialogs::{apply_commands, face_sel, preview_commands};
|
||||
use crate::dialogs::{apply_commands, edge_sel, face_sel, preview_commands};
|
||||
|
||||
/// Components A (grounded) and B: a 20 x 20 x 10 box each, B 30 mm along X.
|
||||
fn two_boxes() -> SolveApp {
|
||||
@@ -60,6 +60,44 @@ fn joint_from_two_snaps() {
|
||||
assert_eq!(app.session.doc.assembly.joints.len(), 1);
|
||||
}
|
||||
|
||||
/// A straight edge is the revolute axis, even when both adjacent faces are perpendicular to it.
|
||||
#[test]
|
||||
fn revolute_joint_uses_the_picked_straight_edges() {
|
||||
let mut app = two_boxes();
|
||||
app.start("joint.create");
|
||||
let mut d = app.dialog.clone().unwrap();
|
||||
for point in [Vec3::new(20.0, 10.0, 10.0), Vec3::new(30.0, 10.0, 10.0)] {
|
||||
let edge = edge_sel(&app.session, point).unwrap();
|
||||
let snap = snap_of(&app.session, &edge).unwrap();
|
||||
assert_eq!(snap.kind, SnapKind::EdgeMid);
|
||||
assert!(snap.at.dist(point) < 1e-9, "{snap:?}");
|
||||
assert!(snap.z.cross(Vec3::Y).len() < 1e-9, "the selected edge runs along Y: {snap:?}");
|
||||
d.pick(&app.session, edge);
|
||||
}
|
||||
if let Kind::Assembly(Asm::Joint(f)) = &mut d.kind {
|
||||
f.kind = 1;
|
||||
f.fit_limits();
|
||||
}
|
||||
for (id, p) in apply_commands(&app, &d).unwrap() {
|
||||
app.run(&id, p).unwrap();
|
||||
}
|
||||
let a = app.session.doc.find_component("A").unwrap();
|
||||
let b = app.session.doc.find_component("B").unwrap();
|
||||
let ground = app.session.doc.occurrence_of(a).unwrap().transform;
|
||||
let placed = |app: &SolveApp, p| solidcraft_engine::doc::apply_point(&app.session.doc.occurrence_of(b).unwrap().transform, p);
|
||||
let ends = [Vec3::new(30.0, 0.0, 10.0), Vec3::new(30.0, 20.0, 10.0)];
|
||||
let before = ends.map(|p| placed(&app, p));
|
||||
let off_axis = Vec3::new(50.0, 10.0, 10.0);
|
||||
let off_before = placed(&app, off_axis);
|
||||
let joint = app.session.doc.assembly.joints[0].id;
|
||||
app.run("joint.drive", json!({"joint": joint, "values": ["90 deg"]})).unwrap();
|
||||
for (p, at) in ends.into_iter().zip(before) {
|
||||
assert!(placed(&app, p).dist(at) < 1e-6, "both ends of the hinge edge must stay put");
|
||||
}
|
||||
assert!((placed(&app, off_axis).dist(off_before) - 20.0 * 2.0_f64.sqrt()).abs() < 1e-6, "the box makes a quarter turn");
|
||||
assert_eq!(app.session.doc.occurrence_of(a).unwrap().transform, ground);
|
||||
}
|
||||
|
||||
/// The preview moves B where the joint puts it and plays the motion while animating.
|
||||
#[test]
|
||||
fn joint_and_drive_preview_on_the_placed_model() {
|
||||
@@ -132,6 +170,33 @@ fn edit_joint_runs_edit_and_limits() {
|
||||
assert!((j.offset - 2.0).abs() < 1e-12);
|
||||
}
|
||||
|
||||
/// An old edge pick saved as point/z keeps its face-normal axis when reopened and edited.
|
||||
#[test]
|
||||
fn editing_a_saved_joint_keeps_its_explicit_axis() {
|
||||
let mut app = two_boxes();
|
||||
app.run(
|
||||
"joint.create",
|
||||
json!({"type": "revolute",
|
||||
"a": {"occurrence": occ(&app, "A"), "point": [20, 10, 10], "z": [0, 0, 1]},
|
||||
"b": {"occurrence": occ(&app, "B"), "point": [30, 10, 10], "z": [0, 0, 1]}}),
|
||||
)
|
||||
.unwrap();
|
||||
let joint = app.session.doc.assembly.joints[0].clone();
|
||||
let saved = app.session.doc.to_json();
|
||||
app = SolveApp::new(Session::new(solidcraft_engine::doc::format::read(&saved).unwrap()), Services::default());
|
||||
let mut d = edit_joint(&app, joint.id).unwrap();
|
||||
if let Kind::Assembly(Asm::Joint(f)) = &mut d.kind {
|
||||
f.flip = true;
|
||||
}
|
||||
for (id, p) in apply_commands(&app, &d).unwrap() {
|
||||
app.run(&id, p).unwrap();
|
||||
}
|
||||
let edited = &app.session.doc.assembly.joints[0];
|
||||
assert!(edited.flip);
|
||||
assert_eq!(edited.a, joint.a);
|
||||
assert_eq!(edited.b, joint.b);
|
||||
}
|
||||
|
||||
/// Edit Joint shows an offset typed as an expression as that expression, and OK keeps it; the
|
||||
/// dialog's plain numbers keep no expression.
|
||||
#[test]
|
||||
@@ -239,6 +304,47 @@ fn circles_from_edge_polylines() {
|
||||
assert!(circle_of(&line).is_none());
|
||||
}
|
||||
|
||||
/// A moved component's edge axis is measured in the displayed world frame.
|
||||
#[test]
|
||||
fn straight_edge_snap_follows_a_rotated_occurrence() {
|
||||
let mut app = two_boxes();
|
||||
let b = app.session.doc.find_component("B").unwrap();
|
||||
app.run("occurrence.move", json!({"occurrence": occ(&app, "B"), "axis": [1, 2, 3], "angle": "37 deg", "translate": [50, 20, 10]})).unwrap();
|
||||
let transform = app.session.doc.occurrence_of(b).unwrap().transform;
|
||||
let mid = solidcraft_engine::doc::apply_point(&transform, Vec3::new(30.0, 10.0, 10.0));
|
||||
let axis = solidcraft_engine::doc::apply_vector(&transform, Vec3::Y);
|
||||
let st = app.session.world_state();
|
||||
let mesh = st.body("Body2").unwrap().mesh();
|
||||
let index = mesh.edges.iter().position(|e| polyline_mid(e).dist(mid) < 1e-6).unwrap();
|
||||
let snap = snap_of(&app.session, &Sel::Edge { body: "Body2".into(), index, point: mid }).unwrap();
|
||||
assert!(snap.at.dist(mid) < 1e-6);
|
||||
assert!(snap.z.cross(axis).len() < 1e-9, "{snap:?}");
|
||||
assert_eq!(snap.param["occurrence"], occ(&app, "B"));
|
||||
}
|
||||
|
||||
/// Only finite straight polylines supply an axis; reversing their samples keeps its direction.
|
||||
#[test]
|
||||
fn straight_edge_axes_reject_curves_and_degenerate_samples() {
|
||||
for dir in [Vec3::X, Vec3::Y, Vec3::Z, Vec3::new(-2.0, 3.0, 1.0)] {
|
||||
let mut pts = vec![Vec3::ZERO, dir * 0.5, dir];
|
||||
let axis = straight_edge_axis(&pts).unwrap();
|
||||
assert!(axis.cross(dir).len() < 1e-9);
|
||||
assert!((axis.len() - 1.0).abs() < 1e-9);
|
||||
pts.reverse();
|
||||
assert_eq!(straight_edge_axis(&pts), Some(axis));
|
||||
}
|
||||
for pts in [
|
||||
vec![],
|
||||
vec![Vec3::X],
|
||||
vec![Vec3::X, Vec3::X],
|
||||
vec![Vec3::ZERO, Vec3::Y, Vec3::X],
|
||||
vec![Vec3::ZERO, Vec3::new(f64::NAN, 0.0, 0.0), Vec3::X],
|
||||
vec![Vec3::ZERO, Vec3::X * f64::INFINITY],
|
||||
] {
|
||||
assert!(straight_edge_axis(&pts).is_none(), "{pts:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The centre of the first text shape reading `text` in a frame's output.
|
||||
fn text_at(out: &egui::FullOutput, text: &str) -> Option<egui::Pos2> {
|
||||
out.shapes.iter().find_map(|c| match &c.shape {
|
||||
|
||||
@@ -62,6 +62,11 @@ made. Contact sets stop motion where bodies would pass through each other,
|
||||
**Motion Study** keys joint values along a timeline, and **Interference** shows overlapping
|
||||
volumes in red, lists the pairs closer than its **Clearance** and each body's nearest neighbour.
|
||||
|
||||
In the Joint and Joint Origin dialogs, picking a straight edge puts the origin at its midpoint
|
||||
with the motion axis along that edge. A revolute joint turns about the picked edge; a slider
|
||||
moves along it. Round edges use their circle's centre and normal. Existing saved joint origins
|
||||
keep their axes.
|
||||
|
||||
A joint origin on a face or a hole keeps the turn it had when it was picked: when the face tilts
|
||||
(a bend angle or a rotation changing with a parameter) the part on it tilts with the face but
|
||||
doesn't spin about it.
|
||||
|
||||
Reference in New Issue
Block a user