use serde::Serialize;
use super::Mark;
use super::turned::{self, State};
use crate::bridge::Feature;
use crate::bridge::holes::{frame, point, principal, to_part};
use crate::model::Analysis;
pub const PARAMETERS: [&str; 3] = ["width.length", "depth.length", "height.length"];
const TOL: f64 = 1e-3;
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Outcome {
pub axis: &'static str,
pub parameter: &'static str,
pub extent: f64,
pub state: State,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub annotations: Vec<String>,
}
#[derive(Clone, Debug, PartialEq)]
struct Carrier {
axis: usize,
lo: f64,
hi: f64,
chain: Option<[i64; 3]>,
mark: String,
}
fn turning(analysis: &Analysis, frame: &[[f64; 3]; 4]) -> Option<(usize, [f64; 3], f64)> {
if let Some(body) = crate::bridge::turned_body(analysis) {
return Some(body);
}
let mut found: Option<(usize, [f64; 3], f64)> = None;
for f in analysis.features.iter().filter(|f| f.kind == "turned_step") {
let b = turned::band(frame, f).ok()?;
match &mut found {
None => found = Some((b.axis, b.on, b.diameter)),
Some((k, on, d)) => {
if *k != b.axis || (0..3).any(|j| j != *k && (on[j] - b.on[j]).abs() > TOL) {
return None;
}
*d = d.max(b.diameter);
}
}
}
found
}
fn across(k: usize, on: [f64; 3], d: f64, mark: &str) -> Vec<Carrier> {
(0..3)
.filter(|&j| j != k)
.map(|j| Carrier {
axis: j,
lo: on[j] - d / 2.0,
hi: on[j] + d / 2.0,
chain: None,
mark: mark.into(),
})
.collect()
}
fn boss(frame: &[[f64; 3]; 4], f: &Feature) -> Option<(usize, [f64; 3], f64)> {
let p = &f.parameters;
let axis = super::parameters::direction(frame, p.get("axis"))?;
let at = point(p.get("location")).map(|l| to_part(frame, l))?;
Some((
principal(axis)?,
at,
super::parameters::size(f, "diameter").ok()?,
))
}
fn stock(
analysis: &Analysis,
frame: &[[f64; 3]; 4],
f: &Feature,
parameter: &str,
mark: &str,
states: impl Fn(f64) -> bool,
) -> Vec<Carrier> {
let Some(values) = super::polygonal::stock_wanted(analysis)
.into_iter()
.find(|w| w.feature.id == f.id && w.parameter == parameter)
.and_then(|w| w.values.ok())
else {
return Vec::new();
};
let p = &f.parameters;
let number = |name: &str| p.get(name).and_then(|v| v.as_f64());
let (Some(q), Some(centre), Some(base), Some(top)) = (
p.get("axis")
.and_then(|v| v.as_str())
.and_then(|a| ["x", "y", "z"].iter().position(|x| *x == a)),
point(p.get("center")),
number("base"),
number("top"),
) else {
return Vec::new();
};
let mut unit = [0.0; 3];
unit[q] = 1.0;
let Some(k) = principal(crate::bridge::holes::dir_to_part(frame, unit)) else {
return Vec::new();
};
let at = |l: [f64; 3]| to_part(frame, l);
if parameter == "stock_length.length" {
let (mut b, mut t) = (centre, centre);
(b[q], t[q]) = (base, top);
let (lo, hi) = (at(b)[k], at(t)[k]);
return if values.iter().any(|&v| states(v)) {
vec![Carrier {
axis: k,
lo: lo.min(hi),
hi: lo.max(hi),
chain: None,
mark: mark.into(),
}]
} else {
Vec::new()
};
}
let Some(&af) = values.first() else {
return Vec::new();
};
if !states(af) {
return Vec::new();
}
let (i, j) = match q {
0 => (1, 2),
1 => (0, 2),
_ => (0, 1),
};
let Some(normals) = p
.get("flat_directions")
.and_then(|v| v.as_array())
.map(|a| {
a.iter()
.filter_map(|v| point(Some(v)))
.map(|d| {
let l = d[i].hypot(d[j]);
[d[i] / l, d[j] / l]
})
.collect::<Vec<_>>()
})
else {
return Vec::new();
};
let n = normals.len();
let half = af / 2.0;
let corners: Vec<[f64; 3]> = (0..n)
.map(|m| {
let (a, o) = (normals[m], normals[(m + 1) % n]);
let det = a[0] * o[1] - a[1] * o[0];
let mut c = centre;
c[i] += half * (o[1] - a[1]) / det;
c[j] += half * (a[0] - o[0]) / det;
at(c)
})
.collect();
let [lo, hi] = analysis.envelope;
let tol = 1e-3 * af;
(0..3)
.filter(|&a| a != k)
.filter_map(|a| {
let low = corners.iter().map(|c| c[a]).fold(f64::INFINITY, f64::min);
let high = corners
.iter()
.map(|c| c[a])
.fold(f64::NEG_INFINITY, f64::max);
((low - lo[a]).abs() <= tol && (high - hi[a]).abs() <= tol).then(|| Carrier {
axis: a,
lo: lo[a],
hi: hi[a],
chain: None,
mark: mark.into(),
})
})
.collect()
}
fn carriers(analysis: &Analysis, marks: &[Mark]) -> Vec<Carrier> {
let frame = frame(analysis);
let [lo, hi] = analysis.envelope;
let mut out = Vec::new();
for m in marks {
for c in &m.claims {
let states = |v: f64| m.states_text(&m.text, v);
if c.feature == "envelope" {
if let Some(k) = PARAMETERS.iter().position(|p| *p == c.parameter) {
if states(hi[k] - lo[k]) {
out.push(Carrier {
axis: k,
lo: lo[k],
hi: hi[k],
chain: None,
mark: m.name.clone(),
});
}
} else if c.parameter == "od.diameter"
&& let Some((k, on, d)) = turning(analysis, &frame)
&& states(d)
{
out.extend(across(k, on, d, &m.name));
}
continue;
}
if let Some(s) = super::redundancy::span(analysis, &c.feature, &c.parameter)
&& !(c.feature.starts_with("f:turned_step:") && c.parameter == "step.length")
&& states(s.hi - s.lo)
{
out.push(Carrier {
axis: s.axis,
lo: s.lo.min(s.hi),
hi: s.lo.max(s.hi),
chain: None,
mark: m.name.clone(),
});
}
let Some(f) = analysis.features.iter().find(|f| f.id == c.feature) else {
continue;
};
if f.kind == "helical_spring"
&& let Some(axis) = point(f.parameters.get("axis").and_then(|a| a.get("direction")))
.and_then(haecceity::geom::unit)
&& let Some(k) = principal(axis)
&& let Some(on) = point(f.parameters.get("axis").and_then(|a| a.get("point")))
{
let number = |name: &str| f.parameters.get(name).and_then(|v| v.as_f64());
if c.parameter == "outer_diameter"
&& let Some(outer) = number("outer_diameter")
&& states(outer)
{
out.extend(across(k, on, outer, &m.name));
} else if c.parameter == "free_length"
&& let (Some(start), Some(end)) = (number("start"), number("end"))
&& states(end - start)
{
let ends = [on[k] + axis[k] * start, on[k] + axis[k] * end];
out.push(Carrier {
axis: k,
lo: ends[0].min(ends[1]),
hi: ends[0].max(ends[1]),
chain: None,
mark: m.name.clone(),
});
}
}
match (f.kind.as_str(), c.parameter.as_str()) {
("turned_step", "step.length") => {
if let Ok(b) = turned::band(&frame, f)
&& states(b.hi - b.lo)
{
let mut line = b.on.map(|x| (x / TOL).round() as i64);
line[b.axis] = 0;
out.push(Carrier {
axis: b.axis,
lo: b.lo,
hi: b.hi,
chain: Some(line),
mark: m.name.clone(),
});
}
}
("turned_step", "step.diameter") => {
if let Ok(b) = turned::band(&frame, f)
&& states(b.diameter)
{
out.extend(across(b.axis, b.on, b.diameter, &m.name));
}
}
("polygonal_stock", "stock_length.length" | "polygon_across_flats.length") => {
out.extend(stock(analysis, &frame, f, &c.parameter, &m.name, states));
}
("boss", "boss.diameter") => {
if let Some((k, on, d)) = boss(&frame, f)
&& states(d)
{
out.extend(across(k, on, d, &m.name));
}
}
_ => {}
}
}
}
out
}
fn proof(carriers: &[Carrier], k: usize, lo: f64, hi: f64) -> Option<Vec<String>> {
let on: Vec<&Carrier> = carriers.iter().filter(|c| c.axis == k).collect();
if let Some(c) = on
.iter()
.find(|c| (c.lo - lo).abs() <= TOL && (c.hi - hi).abs() <= TOL)
{
return Some(vec![c.mark.clone()]);
}
let mut chains: Vec<[i64; 3]> = on.iter().filter_map(|c| c.chain).collect();
chains.sort();
chains.dedup();
for chain in chains {
let mut links: Vec<&Carrier> = on
.iter()
.copied()
.filter(|c| c.chain == Some(chain))
.collect();
links.sort_by(|a, b| (a.lo, a.hi).partial_cmp(&(b.lo, b.hi)).unwrap());
links.dedup_by(|a, b| a.lo == b.lo && a.hi == b.hi);
if (links[0].lo - lo).abs() > TOL {
continue;
}
let mut cursor = links[0].hi;
let mut whole = true;
for l in &links[1..] {
if (l.lo - cursor).abs() > TOL {
whole = false;
break;
}
cursor = l.hi;
}
if whole && (cursor - hi).abs() <= TOL {
return Some(links.iter().map(|l| l.mark.clone()).collect());
}
}
None
}
pub fn outcomes(analysis: &Analysis, marks: &[Mark]) -> Vec<Outcome> {
let carriers = carriers(analysis, marks);
let [lo, hi] = analysis.envelope;
(0..3)
.filter(|&k| hi[k] - lo[k] > TOL)
.map(|k| {
let found = proof(&carriers, k, lo[k], hi[k]);
Outcome {
axis: ["x", "y", "z"][k],
parameter: PARAMETERS[k],
extent: hi[k] - lo[k],
state: if found.is_some() {
State::Placed
} else {
State::Missing
},
annotations: found.unwrap_or_default(),
}
})
.collect()
}
pub fn coverage(analysis: &Analysis, marks: &[Mark]) -> Vec<super::LintIssue> {
outcomes(analysis, marks).into_iter().filter(|o| o.state == State::Missing).map(|o| {
super::uncovered(analysis, "missing_principal_dimension", "envelope", o.parameter,
format!("The overall {} extent ({}) is not stated by a full span, centred diameter or contiguous turned-length chain.", o.axis, super::parameters::fmt(o.extent)))
}).collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::output::Claim;
use serde_json::{Value, json};
fn part(envelope: [[f64; 3]; 2], frame: Value, features: &[(&str, Value)]) -> Analysis {
let features: Vec<Value> = features
.iter()
.enumerate()
.map(|(i, (kind, r))| {
json!({
"id": format!("f:{kind}:{}", i + 1), "kind": kind, "faces": [i],
"parameters": r,
"fingerprint": { "version": "test", "family": "test", "subtype": "test",
"sizes": {}, "position": [0.0, 0.0, 0.0],
"neighbourhood": [] },
})
})
.collect();
serde_json::from_value(json!({
"schema": "draftwright.analysis/2",
"binding": { "source_sha256": "", "part": "test", "face_count": 4, "loader": "test" },
"envelope": envelope,
"features": features,
"iso_bounds": [0.0, 0.0, 0.0, 0.0],
"recognition": { "recogniser": "test", "frame": frame },
}))
.unwrap()
}
fn mark(name: &str, text: &str, feature: &str, parameter: &str) -> Mark {
Mark {
precision: None,
units: Default::default(),
name: name.into(),
view: Some("v:front".into()),
text: text.into(),
claims: vec![Claim {
feature: feature.into(),
parameter: parameter.into(),
}],
}
}
fn states(a: &Analysis, marks: &[Mark]) -> Vec<String> {
outcomes(a, marks)
.into_iter()
.map(|o| format!("{} {:?} {:?}", o.axis, o.state, o.annotations))
.collect()
}
#[test]
fn each_overall_extent_is_stated() {
let identity = json!([
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0]
]);
let a = part([[0.0, 0.0, 0.0], [28.794, 32.268, 44.729]], identity, &[]);
let marks = [
mark("m_env_width", "28.8", "envelope", "width.length"),
mark("m_env_depth", "32.4", "envelope", "depth.length"),
];
assert_eq!(
states(&a, &marks),
["x Placed [\"m_env_width\"]", "y Missing []", "z Missing []"]
);
}
#[test]
fn a_turned_parts_extents_are_its_chain_and_its_diameter() {
let profile = json!({ "axis": "x", "axis_origin": [0.0, 0.0, 0.0] });
let step = |lo: f64, hi: f64, d: f64| json!({ "axis": "x", "lo": lo, "hi": hi, "diameter": d, "profile": profile.clone() });
let frame = json!([
[0.0, 0.0, 10.004],
[0.0, 0.0, -1.0],
[-1.0, 0.0, 0.0],
[0.0, 1.0, 0.0]
]);
let a = part(
[[-3.75, -3.75, 0.0], [3.75, 3.75, 14.8]],
frame,
&[
("turned_step", step(-4.796, -3.796, 7.5)),
("turned_step", step(-3.796, 1.704, 6.0)),
("turned_step", step(1.704, 2.804, 5.0)),
("turned_step", step(2.804, 10.004, 3.4)),
],
);
let lengths = [
mark("m_steplen0", "7.2", "f:turned_step:4", "step.length"),
mark("m_steplen1", "1.1", "f:turned_step:3", "step.length"),
mark("m_steplen2", "5.5", "f:turned_step:2", "step.length"),
mark("m_steplen3", "1", "f:turned_step:1", "step.length"),
];
let mut marks = lengths.to_vec();
marks.push(mark("dim_od", "ø7.5", "envelope", "od.diameter"));
assert_eq!(
states(&a, &marks),
[
"x Placed [\"dim_od\"]",
"y Placed [\"dim_od\"]",
"z Placed [\"m_steplen0\", \"m_steplen1\", \"m_steplen2\", \"m_steplen3\"]"
]
);
let mut gap = lengths[..3].to_vec();
gap.push(mark("m_dia_z3", "ø7.5", "f:turned_step:1", "step.diameter"));
assert_eq!(
states(&a, &gap),
[
"x Placed [\"m_dia_z3\"]",
"y Placed [\"m_dia_z3\"]",
"z Missing []"
]
);
}
#[test]
fn any_measurement_spanning_the_box_states_the_extent() {
let identity = json!([
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0]
]);
let boss = |z: f64, h: f64| {
json!({ "axis": [0.0, 0.0, 1.0], "location": [14.0, 16.0, z], "diameter": 8.4,
"height": h })
};
let envelope = [[0.0, 0.0, 0.0], [28.794, 32.268, 44.729]];
let a = part(
envelope,
identity.clone(),
&[("boss", boss(44.729, 44.729))],
);
let height = [mark("m_boss_h", "44.7", "f:boss:1", "boss_height.length")];
assert_eq!(
states(&a, &height),
["x Missing []", "y Missing []", "z Placed [\"m_boss_h\"]"]
);
let wrong = [mark("m_boss_h", "44", "f:boss:1", "boss_height.length")];
assert_eq!(states(&a, &wrong)[2], "z Missing []");
let short = part(envelope, identity, &[("boss", boss(44.729, 42.729))]);
let height = [mark("m_boss_h", "42.7", "f:boss:1", "boss_height.length")];
assert_eq!(states(&short, &height)[2], "z Missing []");
}
}