use serde::Serialize;
pub const NEAR_LIMIT_DEG: f64 = 0.05;
pub const CAP_TOL_DEG: f64 = 0.01;
const FLAT_TOL: f64 = 1e-6;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum DraftClass {
Positive,
Negative,
NeedsDraft,
Straddle,
Cap,
}
impl DraftClass {
pub const ALL: [DraftClass; 5] =
[DraftClass::Positive, DraftClass::Negative, DraftClass::NeedsDraft, DraftClass::Straddle, DraftClass::Cap];
pub fn label(self) -> &'static str {
match self {
DraftClass::Positive => "Positive draft",
DraftClass::Negative => "Negative draft",
DraftClass::NeedsDraft => "Needs draft",
DraftClass::Straddle => "Straddle",
DraftClass::Cap => "Parting / cap (perpendicular)",
}
}
pub fn key(self) -> &'static str {
match self {
DraftClass::Positive => "positive",
DraftClass::Negative => "negative",
DraftClass::NeedsDraft => "needsDraft",
DraftClass::Straddle => "straddle",
DraftClass::Cap => "cap",
}
}
pub fn color(self) -> [f32; 3] {
match self {
DraftClass::Positive => [0.20, 0.72, 0.30],
DraftClass::Negative => [0.85, 0.22, 0.20],
DraftClass::NeedsDraft => [0.95, 0.80, 0.15],
DraftClass::Straddle => [0.96, 0.52, 0.12],
DraftClass::Cap => [0.55, 0.62, 0.70],
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FaceDraft {
pub solid: String,
pub face: String,
pub min_deg: f64,
pub max_deg: f64,
pub class: DraftClass,
pub curved: bool,
pub near_limit: bool,
}
impl FaceDraft {
pub fn hover_text(&self) -> String {
let angle = if self.curved && (self.max_deg - self.min_deg).abs() > 0.005 {
format!("{:+.2}° to {:+.2}° (sampled)", self.min_deg, self.max_deg)
} else {
format!("{:+.2}°", 0.5 * (self.min_deg + self.max_deg))
};
let mut text = format!("{}: {angle}, {}", self.face, self.class.label());
if self.near_limit {
text.push_str(&format!(
"\nnear limit: the sampled range ends within {NEAR_LIMIT_DEG}° of the minimum, and sampling can miss the exact extreme"
));
}
text
}
}
pub fn classify(min_deg: f64, max_deg: f64, minimum_deg: f64) -> DraftClass {
let m = minimum_deg.abs();
if min_deg >= 90.0 - CAP_TOL_DEG || max_deg <= -(90.0 - CAP_TOL_DEG) {
DraftClass::Cap
} else if min_deg >= m {
DraftClass::Positive
} else if max_deg <= -m {
DraftClass::Negative
} else if min_deg <= -m && max_deg >= m && m > 0.0 {
DraftClass::Straddle
} else {
DraftClass::NeedsDraft
}
}
pub fn near_limit(min_deg: f64, max_deg: f64, minimum_deg: f64) -> bool {
let m = minimum_deg.abs();
[min_deg, max_deg].iter().any(|&a| (a.abs() - m).abs() < NEAR_LIMIT_DEG)
}
fn norm(v: [f64; 3]) -> f64 {
(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt()
}
fn unit(v: [f64; 3]) -> Option<[f64; 3]> {
let l = norm(v);
(l > 1e-12).then(|| [v[0] / l, v[1] / l, v[2] / l])
}
fn dot(a: [f64; 3], b: [f64; 3]) -> f64 {
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
fn to64(v: [f32; 3]) -> [f64; 3] {
[v[0] as f64, v[1] as f64, v[2] as f64]
}
fn angle_deg(normal: [f64; 3], pull: [f64; 3]) -> f64 {
dot(normal, pull).clamp(-1.0, 1.0).asin().to_degrees()
}
pub fn face_normals(solid: &crate::scene::SolidDisplay, face_index: usize) -> Option<(Vec<[f64; 3]>, bool)> {
let face = solid.faces.get(face_index)?;
let mesh = &solid.mesh;
let start = face.tri_start as usize;
let end = start + face.tri_count as usize;
let mut tri_normals: Vec<[f64; 3]> = Vec::new();
let mut vertex_normals: Vec<[f64; 3]> = Vec::new();
for tri in start..end.min(mesh.indices.len() / 3) {
let ids = [mesh.indices[3 * tri] as usize, mesh.indices[3 * tri + 1] as usize, mesh.indices[3 * tri + 2] as usize];
let (Some(a), Some(b), Some(c)) = (mesh.positions.get(ids[0]), mesh.positions.get(ids[1]), mesh.positions.get(ids[2])) else {
continue;
};
let (a, b, c) = (to64(*a), to64(*b), to64(*c));
let e1 = [b[0] - a[0], b[1] - a[1], b[2] - a[2]];
let e2 = [c[0] - a[0], c[1] - a[1], c[2] - a[2]];
let mut geometric = [e1[1] * e2[2] - e1[2] * e2[1], e1[2] * e2[0] - e1[0] * e2[2], e1[0] * e2[1] - e1[1] * e2[0]];
let mut shading = [0.0; 3];
for &id in &ids {
if let Some(n) = mesh.normals.get(id).copied().map(to64).and_then(unit) {
vertex_normals.push(n);
shading = [shading[0] + n[0], shading[1] + n[1], shading[2] + n[2]];
}
}
if dot(geometric, shading) < 0.0 {
geometric = [-geometric[0], -geometric[1], -geometric[2]];
}
if let Some(n) = unit(geometric) {
tri_normals.push(n);
}
}
let first = *tri_normals.first()?;
let flat = tri_normals.iter().all(|n| dot(*n, first) >= 1.0 - FLAT_TOL);
let normals = if flat {
let sum = tri_normals.iter().fold([0.0; 3], |s, n| [s[0] + n[0], s[1] + n[1], s[2] + n[2]]);
vec![unit(sum).unwrap_or(first)]
} else {
vertex_normals
};
Some((normals, !flat))
}
pub fn analyse_solid(solid: &crate::scene::SolidDisplay, pull: [f64; 3], minimum_deg: f64) -> Vec<FaceDraft> {
let mut out = Vec::with_capacity(solid.faces.len());
for (index, face) in solid.faces.iter().enumerate() {
if face.kind == crate::scene::FaceKind::SectionCap {
continue;
}
let Some((normals, curved)) = face_normals(solid, index) else {
continue;
};
let (mut lo, mut hi) = (f64::INFINITY, f64::NEG_INFINITY);
for n in &normals {
let a = angle_deg(*n, pull);
lo = lo.min(a);
hi = hi.max(a);
}
out.push(FaceDraft {
solid: solid.name.clone(),
face: face.name.clone(),
min_deg: lo,
max_deg: hi,
class: classify(lo, hi, minimum_deg),
curved,
near_limit: curved && near_limit(lo, hi, minimum_deg),
});
}
out
}