* claim: #5968 * Joint dialog: Find Limits fills the limits from where parts collide (#5968) joint.find_limits sweeps each value of a joint both ways from where it is, on a copy and with its own limits left out, and answers where the parts it moves first touch another part, where a closed loop stops closing, or that nothing is met within a turn (angles) or the parts' reach (distances). Edit Joint's Limits section gets a Find Limits button that runs it on the joint as the dialog has it, off the window thread, and fills the rows. joint.sweep now tells touching bodies apart from the other bodies of the same two parts: on the Hinge Assembly sample the pins touch where it starts, and the plates used to pass through each other unnoticed. * find_limits: read the session only * find_limits: a joint with nothing to meet, edits during a search, tiny ranges * find_limits: keep touching at the start per pair of parts Per body, the Hinge Assembly sample's plates, which only come flush in the end plane the two leaves share, stopped the search. The leaves never pass into each other: the sample's answer is a whole turn. * find_limits: touching boxes cost no collision work; stale results and rounding * find_limits: each pair of bodies of resting parts held to its own depth; loops without bodies * find_limits: a 32-bit budget it can pass, a repaint with the result, a bad form says so first * find_limits: each pair of resting bodies gets its own margin * find_limits: Arabic, Hebrew and Vietnamese strings (complete catalogs on main) * find_limits: search the joint with Edit Joint's side picks Edit Joint now keeps the joint's sides as picks in its inputs, and its OK commands compare them with what it opened on. Built without them, the commands Find Limits applies first asked for the sides again, so every search failed. --------- Co-authored-by: MateuszSychXpl <151755914+MateuszSychXpl@users.noreply.github.com>
This commit is contained in:
@@ -62,6 +62,7 @@ pub mod file;
|
||||
pub mod inspect;
|
||||
pub mod joints;
|
||||
pub mod mass_properties;
|
||||
pub mod motion;
|
||||
pub mod print_face;
|
||||
pub mod render;
|
||||
pub mod render_lights;
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
//! Contracts and result types for the commands in `cmd/motion.rs`.
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
|
||||
use serde::de::Error as _;
|
||||
use serde::{Deserialize, Deserializer, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
/// Find where each value of a joint can go before the parts it moves touch another body. The
|
||||
/// design is not changed.
|
||||
#[derive(Debug, Deserialize, JsonSchema)]
|
||||
#[schemars(deny_unknown_fields)]
|
||||
pub struct FindLimitsParams {
|
||||
pub joint: JointRef,
|
||||
/// Which value (0 for a revolute's angle). Default: every value of the joint.
|
||||
#[serde(default)]
|
||||
pub index: Option<u64>,
|
||||
/// mm: each stop is bisected until no part moves more than this between the last clear value
|
||||
/// and the touching one (above 0, at most 1000). Default: 0.01.
|
||||
#[serde(default)]
|
||||
pub refine: Option<f64>,
|
||||
/// Between samples, in the value's unit (degrees for angles, mm for distances; above 0).
|
||||
/// Default: fine enough that no part passes through another between two.
|
||||
#[serde(default)]
|
||||
pub step: Option<f64>,
|
||||
/// The parts checked against each other. Default: every occurrence.
|
||||
#[serde(default, deserialize_with = "super::present", skip_serializing_if = "Option::is_none")]
|
||||
#[schemars(with = "Vec<PartRef>")]
|
||||
pub occurrences: Option<Vec<PartRef>>,
|
||||
/// The parts that stay: each of `occurrences` is checked only against these.
|
||||
#[serde(default)]
|
||||
pub against: Option<Vec<PartRef>>,
|
||||
/// Check the pairs of the design's contact sets instead (not with `occurrences`).
|
||||
#[serde(default)]
|
||||
pub contact_sets: Option<bool>,
|
||||
}
|
||||
|
||||
/// A joint by its id (a number) or its name.
|
||||
#[derive(Debug)]
|
||||
pub struct JointRef;
|
||||
|
||||
impl<'de> Deserialize<'de> for JointRef {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
match Value::deserialize(d)? {
|
||||
Value::Number(_) | Value::String(_) => Ok(JointRef),
|
||||
_ => Err(D::Error::custom("must be an id or a name")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl JsonSchema for JointRef {
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"JointRef".into()
|
||||
}
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
json_schema!({"description": "A joint's id (a number) or its name.", "type": ["number", "string"]})
|
||||
}
|
||||
}
|
||||
|
||||
/// An occurrence by its id (a number) or its name.
|
||||
#[derive(Debug)]
|
||||
pub struct PartRef;
|
||||
|
||||
impl<'de> Deserialize<'de> for PartRef {
|
||||
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
match Value::deserialize(d)? {
|
||||
Value::Number(_) | Value::String(_) => Ok(PartRef),
|
||||
_ => Err(D::Error::custom("an occurrence must be an id or a name")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl JsonSchema for PartRef {
|
||||
fn schema_name() -> Cow<'static, str> {
|
||||
"PartRef".into()
|
||||
}
|
||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||
json_schema!({"description": "An occurrence's id (a number) or its name.", "type": ["number", "string"]})
|
||||
}
|
||||
}
|
||||
|
||||
/// What stopped one side of a sweep.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, JsonSchema)]
|
||||
#[serde(untagged)]
|
||||
pub enum Stop {
|
||||
/// Two parts touch.
|
||||
Contact {
|
||||
a: String,
|
||||
b: String,
|
||||
/// Where they touch (world, mm).
|
||||
at: [f64; 3],
|
||||
},
|
||||
/// `loop`: a closed loop can no longer close; `limit`: the limit of another of the joint's
|
||||
/// values, or of another joint through a motion link; `turn`: no contact within one turn, so the side is a turn from the other;
|
||||
/// `range`: no contact within the parts' reach (a distance), so the side has no limit.
|
||||
Other(Why),
|
||||
}
|
||||
|
||||
/// Why a side stopped short of contact.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, JsonSchema)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Why {
|
||||
Loop,
|
||||
Limit,
|
||||
Turn,
|
||||
Range,
|
||||
}
|
||||
|
||||
/// One value's limits.
|
||||
#[derive(Debug, Serialize, JsonSchema)]
|
||||
pub struct FoundLimits {
|
||||
/// Which value of the joint.
|
||||
pub index: usize,
|
||||
/// Its name (angle, distance, …).
|
||||
pub dof: String,
|
||||
/// `deg` or `mm`.
|
||||
pub unit: String,
|
||||
/// Where it is now, which the search starts from.
|
||||
pub value: f64,
|
||||
/// The lowest value clear of contact, or `null` (`min_by` is `range`).
|
||||
pub min: Option<f64>,
|
||||
/// The highest value clear of contact, or `null` (`max_by` is `range`).
|
||||
pub max: Option<f64>,
|
||||
pub min_by: Stop,
|
||||
pub max_by: Stop,
|
||||
/// How far each way the search went: a turn (360) for an angle, the parts' reach for a
|
||||
/// distance.
|
||||
pub range: f64,
|
||||
}
|
||||
|
||||
/// Two parts already passing into each other where the search starts.
|
||||
#[derive(Debug, Serialize, JsonSchema)]
|
||||
pub struct Overlap {
|
||||
pub a: String,
|
||||
pub b: String,
|
||||
/// How deep (mm).
|
||||
pub depth_mm: f64,
|
||||
}
|
||||
|
||||
/// What `joint.find_limits` returns.
|
||||
#[derive(Debug, Serialize, JsonSchema)]
|
||||
pub struct FindLimitsResult {
|
||||
pub joint: u64,
|
||||
pub name: String,
|
||||
pub values: Vec<FoundLimits>,
|
||||
pub refine_mm: f64,
|
||||
/// `mesh`: distances between the parts' tessellations.
|
||||
pub gap_method: String,
|
||||
/// How far those can be off: 0 when every face is flat.
|
||||
pub gap_tolerance_mm: f64,
|
||||
/// How many pairs of parts are checked.
|
||||
pub pairs: usize,
|
||||
/// Pairs of parts touching where the search starts: they stop it only once they pass into
|
||||
/// each other deeper than they start.
|
||||
pub touching_at_start: Vec<[String; 2]>,
|
||||
/// Pairs of those already passing into each other, deeper than a touch.
|
||||
pub overlapping_at_start: Vec<Overlap>,
|
||||
/// In the warn parameter-check mode: what the call's parameters got wrong.
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub param_warnings: Vec<String>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "motion_tests.rs"]
|
||||
mod tests;
|
||||
@@ -0,0 +1,59 @@
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use super::{FindLimitsParams, FindLimitsResult};
|
||||
use crate::Session;
|
||||
use crate::schema::tests::{assert_keys, assert_strict_keeps_todays_errors, conforms};
|
||||
use crate::schema::{ParamCheck, input_schema_for, output_schema_for};
|
||||
|
||||
#[test]
|
||||
fn schemas_and_the_key_check_agree() {
|
||||
assert_keys(input_schema_for::<FindLimitsParams>(), &["against", "contact_sets", "index", "joint", "occurrences", "refine", "step"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_keeps_todays_errors() {
|
||||
assert_strict_keeps_todays_errors(&[
|
||||
("joint.find_limits", json!({"joint": true})),
|
||||
("joint.find_limits", json!({"joint": null})),
|
||||
("joint.find_limits", json!({"joint": "Hinge"})),
|
||||
]);
|
||||
}
|
||||
|
||||
/// A 100 × 4 × 50 door hinged about Z at the origin, a block in its way and a slider beside it.
|
||||
fn door() -> Session {
|
||||
let mut s = Session::default();
|
||||
for (name, corner, size) in [("Door", [0, 0, 0], [100, 4, 50]), ("Block", [60, 30, 0], [20, 20, 50]), ("Slide", [0, -40, 0], [10, 10, 10])] {
|
||||
for (id, p) in [
|
||||
("component.activate", json!({"component": "root"})),
|
||||
("component.create", json!({"name": name, "grounded": false})),
|
||||
("solid.box", json!({"length": size[0], "width": size[1], "height": size[2], "corner": corner})),
|
||||
] {
|
||||
s.execute(id, &p).unwrap_or_else(|e| panic!("{id}: {e}"));
|
||||
}
|
||||
}
|
||||
for p in [
|
||||
json!({"type": "revolute", "a": {"occurrence": 0, "point": [0, 0, 0]}, "b": {"occurrence": "Door", "point": [0, 0, 0]}, "flip": true, "name": "Hinge"}),
|
||||
json!({"type": "slider", "a": {"occurrence": 0, "point": [0, -40, 0], "z": [1, 0, 0]}, "b": {"occurrence": "Slide", "point": [0, -40, 0], "z": [1, 0, 0]}, "flip": true, "name": "Rail"}),
|
||||
] {
|
||||
s.execute("joint.create", &p).unwrap_or_else(|e| panic!("{e}"));
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replies_match_the_output_schema() {
|
||||
let schema = output_schema_for::<FindLimitsResult>();
|
||||
let schema = schema.as_value();
|
||||
let mut s = door();
|
||||
s.param_check = ParamCheck::Warn;
|
||||
for p in [json!({"joint": "Hinge"}), json!({"joint": "Rail", "index": 0, "refine": 0.1, "occurrences": ["Slide", "Door"]})] {
|
||||
let r = s.execute("joint.find_limits", &p).unwrap_or_else(|e| panic!("{e}"));
|
||||
assert!(r.get("param_warnings").is_none(), "{r}");
|
||||
assert!(conforms(schema, schema, &r), "{r} doesn't match {schema}");
|
||||
}
|
||||
// A misspelt key still runs in warn, and says so.
|
||||
let r = s.execute("joint.find_limits", &json!({"joint": "Hinge", "refin": 0.1})).unwrap_or_else(|e| panic!("{e}"));
|
||||
assert!(r["param_warnings"].as_array().is_some_and(|w| !w.is_empty()) && conforms(schema, schema, &r), "{r}");
|
||||
let required: Vec<&str> = schema.get("required").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).collect();
|
||||
assert!(!required.contains(&"param_warnings"));
|
||||
}
|
||||
Reference in New Issue
Block a user