use serde_json::Value;
use super::parameters::{self, Wanted, fmt, fmt3};
use super::{LintIssue, Mark};
use crate::bridge::Feature;
use crate::bridge::holes::{dir_to_part, frame, principal, to_part};
use crate::model::Analysis;
const AXES: [&str; 3] = ["x", "y", "z"];
const PLANE_AXES: [[usize; 2]; 3] = [[1, 2], [2, 0], [0, 1]];
#[derive(Clone, Debug, PartialEq)]
pub struct Placed {
pub axis: usize,
pub bounds: [[f64; 2]; 3],
}
impl Placed {
fn extent(&self, k: usize) -> f64 {
self.bounds[k][1] - self.bounds[k][0]
}
fn centre(&self) -> [f64; 3] {
self.bounds.map(|b| (b[0] + b[1]) / 2.0)
}
}
pub fn placed(analysis: &Analysis, f: &Feature) -> Result<Placed, String> {
let frame = frame(analysis);
let p = &f.parameters;
let axis = p
.get("axis")
.and_then(Value::as_str)
.and_then(|a| AXES.iter().position(|x| *x == a))
.ok_or("its record gives no axis")?;
let corner = |end: usize| -> Result<[f64; 3], String> {
let mut c = [0.0; 3];
for (k, a) in AXES.iter().enumerate() {
c[k] = parameters::size(f, &format!("{a}{end}"))?;
}
Ok(to_part(&frame, c))
};
let (a, b) = (corner(0)?, corner(1)?);
let mut e = [0.0; 3];
e[axis] = 1.0;
let axis =
principal(dir_to_part(&frame, e)).ok_or("its axis lies along no axis of the part")?;
Ok(Placed {
axis,
bounds: [0, 1, 2].map(|k| [a[k].min(b[k]), a[k].max(b[k])]),
})
}
pub fn locations(p: &Placed, datum: [f64; 3]) -> Vec<(String, f64)> {
let c = p.centre();
if p.axis == 2 {
return vec![
("location_pad.location.x".into(), (c[0] - datum[0]).abs()),
("location_pad.location.y".into(), (c[1] - datum[1]).abs()),
];
}
PLANE_AXES[p.axis]
.iter()
.map(|&k| (format!("location_pad.{}", AXES[k]), (c[k] - datum[k]).abs()))
.filter(|(_, v)| *v > 1e-6)
.collect()
}
pub fn wanted(analysis: &Analysis) -> Vec<Wanted<'_>> {
let mut out = Vec::new();
for f in analysis.features.iter().filter(|f| f.kind == "pad") {
let p = match placed(analysis, f) {
Ok(p) => p,
Err(why) => {
out.push(Wanted {
feature: f,
parameter: "pad.?".into(),
values: Err(why),
callout: "its sizes and location".into(),
});
continue;
}
};
let [long, wide] = PLANE_AXES[p.axis];
for (parameter, v, word) in [
("pad_width.length", p.extent(wide), "wide"),
("pad_length.length", p.extent(long), "long"),
("pad_height.length", p.extent(p.axis), "high"),
] {
out.push(Wanted {
feature: f,
parameter: parameter.into(),
values: Ok(vec![v]),
callout: format!("{} {word}", fmt(v)),
});
}
for (parameter, v) in locations(&p, analysis.envelope[0]) {
let axis = parameter.rsplit('.').next().unwrap_or("?").to_string();
out.push(Wanted {
feature: f,
parameter,
values: Ok(vec![v]),
callout: format!(
"its location {} along {axis} from the part's corner",
fmt(v)
),
});
}
}
out
}
pub fn coverage(analysis: &Analysis, marks: &[Mark]) -> Vec<LintIssue> {
let noun = |f: &Feature| {
let mut s = format!("raised pad {}", f.id);
if let Ok(p) = placed(analysis, f) {
let [long, wide] = PLANE_AXES[p.axis];
s += &format!(
" {} × {} × {} high along {}, at {}",
fmt(p.extent(long)),
fmt(p.extent(wide)),
fmt(p.extent(p.axis)),
AXES[p.axis],
fmt3(p.centre())
);
}
s
};
parameters::coverage(
"pad",
&wanted(analysis),
marks,
noun,
"the pad's sizes in the view that looks down on it and its height side-on, and linear \
dimensions of its location from the part's corner",
)
}
#[cfg(test)]
mod tests {
use super::super::paired_ramp_steps::tests::part;
use super::*;
use crate::output::Claim;
use serde_json::json;
fn mark(text: &str, parameters: &[&str]) -> Mark {
Mark {
name: format!("m:{text}"),
view: Some("v:plan".into()),
text: text.into(),
claims: parameters
.iter()
.map(|p| Claim {
feature: "f:pad:1".into(),
parameter: p.to_string(),
})
.collect(),
}
}
#[test]
fn a_raised_pad_states_its_sizes_height_and_location() {
let mut a = part(
"pad",
json!({ "x0": 5.0, "x1": 25.0, "y0": 8.0, "y1": 18.0, "z0": 0.0, "z1": 5.0,
"axis": "z", "direction": -1 }),
);
a.envelope = [[0.0, 0.0, 0.0], [40.0, 30.0, 15.0]];
let got: Vec<(String, String)> = wanted(&a)
.into_iter()
.map(|w| (w.parameter, fmt(w.values.unwrap()[0])))
.collect();
let want: Vec<(String, String)> = [
("pad_width.length", "10"),
("pad_length.length", "20"),
("pad_height.length", "5"),
("location_pad.location.x", "15"),
("location_pad.location.y", "13"),
]
.iter()
.map(|(p, v)| (p.to_string(), v.to_string()))
.collect();
assert_eq!(got, want);
let sizes = mark(
"20 × 10 × 5 HIGH",
&["pad_width.length", "pad_length.length", "pad_height.length"],
);
let x = mark("15", &["location_pad.location.x"]);
let y = mark("13", &["location_pad.location.y"]);
assert!(coverage(&a, &[sizes.clone(), x, y]).is_empty());
let swapped = [
sizes,
mark("13", &["location_pad.location.x"]),
mark("15", &["location_pad.location.y"]),
];
let codes: Vec<String> = coverage(&a, &swapped)
.into_iter()
.map(|i| format!("{} {}", i.code, i.requirements[0].parameter))
.collect();
assert_eq!(
codes,
[
"pad_requirement_unverifiable location_pad.location.x",
"pad_requirement_unverifiable location_pad.location.y"
]
);
assert_eq!(coverage(&a, &[]).len(), 5);
}
#[test]
fn a_side_pad_is_located_along_its_footprint_axes() {
let mut a = part(
"pad",
json!({ "x0": 5.0, "x1": 20.0, "y0": 0.0, "y1": 5.0, "z0": 8.0, "z1": 18.0,
"axis": "y", "direction": -1 }),
);
a.envelope = [[0.0, 0.0, 0.0], [40.0, 15.0, 30.0]];
let p = placed(&a, &a.features[0]).unwrap();
let named = |l: Vec<(String, f64)>| -> Vec<(String, String)> {
l.into_iter().map(|(p, v)| (p, fmt(v))).collect()
};
assert_eq!(
named(locations(&p, a.envelope[0])),
[
("location_pad.z".to_string(), "13".to_string()),
("location_pad.x".to_string(), "12.5".to_string())
]
);
assert_eq!(
named(locations(&p, [12.5, 0.0, 0.0])),
[("location_pad.z".to_string(), "13".to_string())]
);
let sizes: Vec<(String, String)> = wanted(&a)
.into_iter()
.take(3)
.map(|w| (w.parameter, fmt(w.values.unwrap()[0])))
.collect();
assert_eq!(
sizes,
[
("pad_width.length".to_string(), "15".to_string()),
("pad_length.length".to_string(), "10".to_string()),
("pad_height.length".to_string(), "5".to_string())
]
);
}
}