use serde_json::Value;
use crate::AffineTransform;
pub const MOUNT_BLOCK: &str = "mount";
const IDENTITY_TOLERANCE: f64 = 1e-6;
pub fn part_mount(document: &Value) -> Result<Option<AffineTransform>, String> {
let Some(block) = document.get(MOUNT_BLOCK).filter(|block| !block.is_null()) else {
return Ok(None);
};
mount_from_block(block).map(Some)
}
pub fn mount_from_block(block: &Value) -> Result<AffineTransform, String> {
let object = block
.as_object()
.ok_or("mount: must be an object {translate, rotateEulerDeg}")?;
if let Some(key) = object.keys().find(|key| !matches!(key.as_str(), "translate" | "rotateEulerDeg")) {
return Err(format!("mount: unknown key '{key}' (a mount has translate and rotateEulerDeg)"));
}
for key in ["translate", "rotateEulerDeg"] {
let Some(value) = object.get(key) else { continue };
let numbers = value.as_array().filter(|values| values.len() == 3);
let finite = numbers.is_some_and(|values| values.iter().all(|v| v.as_f64().is_some_and(f64::is_finite)));
if !finite {
return Err(format!("mount: {key} must be three finite numbers, found {value}"));
}
}
super::assembly::pose_params_to_transform(block)
}
pub fn mount_block_of(transform: &AffineTransform) -> Option<Value> {
if is_identity(transform) {
return None;
}
Some(super::assembly::transform_to_pose_params(transform))
}
pub fn is_identity(transform: &AffineTransform) -> bool {
transform
.elements
.iter()
.enumerate()
.all(|(index, value)| (value - if index % 5 == 0 { 1.0 } else { 0.0 }).abs() <= IDENTITY_TOLERANCE)
}
fn translation(x: f64, y: f64, z: f64) -> AffineTransform {
AffineTransform { elements: [1.0, 0.0, 0.0, x, 0.0, 1.0, 0.0, y, 0.0, 0.0, 1.0, z, 0.0, 0.0, 0.0, 1.0] }
}
fn identity() -> AffineTransform {
translation(0.0, 0.0, 0.0)
}
fn linear(transform: &AffineTransform, v: [f64; 3]) -> [f64; 3] {
let e = &transform.elements;
[0, 1, 2].map(|row| e[row * 4] * v[0] + e[row * 4 + 1] * v[1] + e[row * 4 + 2] * v[2])
}
fn turn_down(n: [f64; 3]) -> Option<AffineTransform> {
let length = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt();
if !(length.is_finite() && length > 1e-12) {
return None;
}
let n = n.map(|c| c / length);
let (axis, cos) = ([-n[1], n[0], 0.0], -n[2]);
let sin = (axis[0] * axis[0] + axis[1] * axis[1]).sqrt();
Some(if sin <= 1e-12 && cos > 0.0 {
identity()
} else if sin <= 1e-12 {
AffineTransform { elements: [1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0] }
} else {
let k = axis.map(|c| c / sin);
let v = 1.0 - cos;
let r = |i: usize, j: usize| {
let cross = match (i, j) {
(0, 1) => -k[2],
(1, 0) => k[2],
(0, 2) => k[1],
(2, 0) => -k[1],
(1, 2) => -k[0],
(2, 1) => k[0],
_ => 0.0,
};
(if i == j { cos } else { 0.0 }) + sin * cross + v * k[i] * k[j]
};
AffineTransform {
elements: [r(0, 0), r(0, 1), r(0, 2), 0.0, r(1, 0), r(1, 1), r(1, 2), 0.0, r(2, 0), r(2, 1), r(2, 2), 0.0, 0.0, 0.0, 0.0, 1.0],
}
})
}
pub fn mount_origin_to(current: Option<&AffineTransform>, point: [f64; 3]) -> Result<AffineTransform, String> {
let current = current.copied().unwrap_or_else(identity);
let at = current.point(crate::Vec3::new(point[0], point[1], point[2]));
translation(-at.x, -at.y, 0.0).compose(¤t)
}
pub fn mount_seated_on(
current: Option<&AffineTransform>,
origin: [f64; 3],
normal: [f64; 3],
) -> Result<AffineTransform, String> {
let current = current.copied().unwrap_or_else(identity);
let rotation = turn_down(linear(¤t, normal)).ok_or("the picked face has no normal to seat on")?;
let turned = rotation.compose(¤t)?;
let at = turned.point(crate::Vec3::new(origin[0], origin[1], origin[2]));
translation(0.0, 0.0, -at.z).compose(&turned)
}
pub fn reference_plane(prefix: &[crate::feature_pipeline::FeatureResult], name: &str) -> Result<([f64; 3], [f64; 3]), String> {
use crate::feature_pipeline::pmi::resolve::resolve_reference;
use crate::{resolve_face_selection, SelectionGeometry};
let name = name.trim();
if name.is_empty() {
return Err("nothing was picked".into());
}
let mut scene = crate::feature_pipeline::SceneMap::default();
for result in prefix {
scene.apply(result);
}
let geometry = match scene.resolve_face(name) {
Some(face) => crate::with_registered_solid_str(face.handle, |solid| {
resolve_face_selection(solid, face.face_id).map_err(|error| format!("face '{name}': {error:?}"))
})?,
None => resolve_reference(&scene, name)?,
};
match geometry {
SelectionGeometry::Plane { origin, normal } => {
let out = ([origin.x, origin.y, origin.z], [normal.x, normal.y, normal.z]);
match out.0.iter().chain(out.1.iter()).all(|c| c.is_finite()) {
true => Ok(out),
false => Err(format!("'{name}' did not evaluate to a finite plane")),
}
}
_ => Err(format!("'{name}' is not flat: pick a planar face (the part's seating face) or a datum plane")),
}
}
const ALIGN_TILT_AGREEMENT_DEG: f64 = 5.0;
const ALIGN_MIRROR_FLOOR: f64 = 0.05;
#[derive(Debug, Clone, PartialEq)]
pub struct AlignSeat {
pub pin: String,
pub position: [f64; 3],
pub direction: [f64; 3],
pub support: Vec<[f64; 3]>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct AlignPad {
pub number: String,
pub at: [f64; 2],
pub smd: bool,
}
#[derive(Debug, Clone)]
pub struct AlignReport {
pub mount: AffineTransform,
pub largest: f64,
pub rms: f64,
pub worst: String,
pub pins: usize,
pub notes: Vec<String>,
}
fn has_tilt(transform: &AffineTransform) -> bool {
let z = linear(transform, [0.0, 0.0, 1.0]);
(z[0] * z[0] + z[1] * z[1]).sqrt() > IDENTITY_TOLERANCE || z[2] < 0.0
}
fn fit_2d(from: &[[f64; 2]], to: &[[f64; 2]]) -> (f64, [f64; 2], f64) {
let n = from.len() as f64;
let centre = |points: &[[f64; 2]]| {
let sum = points.iter().fold([0.0, 0.0], |s, p| [s[0] + p[0], s[1] + p[1]]);
[sum[0] / n, sum[1] / n]
};
let (p, q) = (centre(from), centre(to));
let (mut dot, mut cross) = (0.0, 0.0);
for (a, b) in from.iter().zip(to) {
let (a, b) = ([a[0] - p[0], a[1] - p[1]], [b[0] - q[0], b[1] - q[1]]);
dot += a[0] * b[0] + a[1] * b[1];
cross += a[0] * b[1] - a[1] * b[0];
}
let angle = if from.len() < 2 { 0.0 } else { cross.atan2(dot) };
let (s, c) = angle.sin_cos();
let t = [q[0] - (c * p[0] - s * p[1]), q[1] - (s * p[0] + c * p[1])];
let squares = from
.iter()
.zip(to)
.map(|(a, b)| {
let (x, y) = (c * a[0] - s * a[1] + t[0] - b[0], s * a[0] + c * a[1] + t[1] - b[1]);
x * x + y * y
})
.sum();
(angle, t, squares)
}
pub fn mount_aligned_to_footprint(
current: Option<&AffineTransform>,
seats: &[AlignSeat],
pads: &[AlignPad],
) -> Result<AlignReport, String> {
let current = current.copied().unwrap_or_else(identity);
let pads_of = |pin: &str| pads.iter().filter(|pad| pad.number.trim() == pin.trim()).collect::<Vec<_>>();
let seats: Vec<(&AlignSeat, Vec<&AlignPad>)> = seats
.iter()
.map(|seat| (seat, pads_of(&seat.pin)))
.filter(|(_, pads)| !pads.is_empty())
.collect();
if seats.is_empty() {
return Err("no seated pcb point has a pad of its number: seat the pins first".into());
}
let mut notes = Vec::new();
let planed = if has_tilt(¤t) {
current
} else {
let directions: Vec<[f64; 3]> = seats
.iter()
.map(|(seat, _)| {
let d = linear(¤t, seat.direction);
let length = (d[0] * d[0] + d[1] * d[1] + d[2] * d[2]).sqrt();
d.map(|c| c / length)
})
.collect();
let sum = directions.iter().fold([0.0; 3], |s, d| [s[0] + d[0], s[1] + d[1], s[2] + d[2]]);
let length = (sum[0] * sum[0] + sum[1] * sum[1] + sum[2] * sum[2]).sqrt();
if !(length.is_finite() && length > 1e-9) {
return Err("the pins' wire directions give no seating plane: pick its seating face".into());
}
let mean = sum.map(|c| c / length);
let cos_limit = ALIGN_TILT_AGREEMENT_DEG.to_radians().cos();
let mut apart: Vec<&str> = seats
.iter()
.zip(&directions)
.filter(|(_, d)| !(d[0] * mean[0] + d[1] * mean[1] + d[2] * mean[2] >= cos_limit))
.map(|((seat, _), _)| seat.pin.trim())
.collect();
apart.dedup();
if !apart.is_empty() {
return Err(format!(
"pins {} leave the part more than {ALIGN_TILT_AGREEMENT_DEG}ยฐ away from the others, so they give no seating plane: pick its seating face",
apart.join(", ")
));
}
let turn = turn_down(mean).ok_or("the pins' wire directions give no seating plane")?;
let turned = turn.compose(¤t)?;
let lowest = seats
.iter()
.filter(|(_, pads)| pads.iter().all(|pad| pad.smd))
.flat_map(|(seat, _)| if seat.support.is_empty() { vec![seat.position] } else { seat.support.clone() })
.map(|p| turned.point(crate::Vec3::new(p[0], p[1], p[2])).z)
.fold(f64::INFINITY, f64::min);
if lowest.is_finite() {
translation(0.0, 0.0, -lowest).compose(&turned)?
} else {
notes.push("its pins are all through-hole, so its height was not set: pick its seating face".to_string());
turned
}
};
let flat = |transform: &AffineTransform, seat: &AlignSeat| {
let p = transform.point(crate::Vec3::new(seat.position[0], seat.position[1], seat.position[2]));
[p.x, p.y]
};
let driving: Vec<(&AlignSeat, [f64; 2])> =
seats.iter().filter(|(_, pads)| pads.len() == 1).map(|(seat, pads)| (*seat, pads[0].at)).collect();
if driving.is_empty() {
return Err("every seated pin has several pads of its number, so none fixes where the part goes".into());
}
let from: Vec<[f64; 2]> = driving.iter().map(|(seat, _)| flat(&planed, seat)).collect();
let to: Vec<[f64; 2]> = driving.iter().map(|(_, at)| *at).collect();
let (angle, t, squares) = fit_2d(&from, &to);
if driving.len() == 1 {
notes.push(format!("only pin {} has a single pad, so the part was moved and its rotation kept", driving[0].0.pin.trim()));
} else {
let mirrored: Vec<[f64; 2]> = from.iter().map(|p| [-p[0], p[1]]).collect();
let (_, _, mirror_squares) = fit_2d(&mirrored, &to);
let n = driving.len() as f64;
let (proper, mirror) = ((squares / n).sqrt(), (mirror_squares / n).sqrt());
if proper > ALIGN_MIRROR_FLOOR && mirror < 0.5 * proper {
notes.push(format!(
"a mirrored fit would leave {mirror:.3} mm RMS instead of {proper:.3} mm: the part may be modelled mirrored (Align never mirrors it)"
));
}
}
let (s, c) = angle.sin_cos();
let in_plane = AffineTransform { elements: [c, -s, 0.0, t[0], s, c, 0.0, t[1], 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0] };
let mount = in_plane.compose(&planed)?;
let mut squares = 0.0;
let (mut largest, mut worst) = (0.0_f64, String::new());
for (seat, pads) in &seats {
let at = flat(&mount, seat);
let distance = pads
.iter()
.map(|pad| ((at[0] - pad.at[0]).powi(2) + (at[1] - pad.at[1]).powi(2)).sqrt())
.fold(f64::INFINITY, f64::min);
squares += distance * distance;
if distance > largest || worst.is_empty() {
largest = distance.max(largest);
worst = seat.pin.trim().to_string();
}
}
Ok(AlignReport { mount, largest, rms: (squares / seats.len() as f64).sqrt(), worst, pins: seats.len(), notes })
}