diff --git a/crates/doc/src/sheet_face_names.rs b/crates/doc/src/sheet_face_names.rs index 3e37736f5..8d42e849c 100644 --- a/crates/doc/src/sheet_face_names.rs +++ b/crates/doc/src/sheet_face_names.rs @@ -5,7 +5,7 @@ //! neighbouring flange), the rim along the bend there, `:rim.ext0` and `rim.ext1`. A panel is `base` or a flange's tag (`F.`), so the names //! say what each face is and stay with it when sizes change or other flanges come and go. -use solidcraft_geom::{Vec2, Vec3}; +use solidcraft_geom::{Plane, Vec2, Vec3}; use super::SheetBody; use crate::{ModelBody, ModelState, apply_point, mat_inverse}; @@ -13,6 +13,9 @@ use crate::{ModelBody, ModelState, apply_point, mat_inverse}; /// The share of a face's sample points that must agree on a name for the face to take it. const AGREE: f64 = 0.8; +/// The sine of the angle within which a flat face's plane faces along a triangle's normal. +const FACING: f64 = 1e-3; + impl SheetBody { /// The readable name of the face of this sheet a point lies on (sheet coordinates, within /// `tol`, kept below a quarter of the thickness): a flat face's (see @@ -21,6 +24,24 @@ impl SheetBody { self.panel_face_at(p, tol).map(|(name, _)| name).or_else(|| self.bend_face_at(p, tol)).or_else(|| self.ext_rim_at(p, tol)) } + /// [`face_name_at`](Self::face_name_at) for a point on a face whose normal is `normal` (a + /// mesh triangle's, either way round): a flat face's name is taken only where its plane faces + /// along `normal`. A rim is as narrow as the sheet is thick, so the centres of its triangles + /// can lie within `tol` of the panel's top or bottom face beside it. + fn face_name_facing(&self, p: Vec3, normal: Vec3, tol: f64) -> Option { + let facing = |plane: &Plane| plane.normal().cross(normal).len() < FACING; + match self.panel_face_at(p, tol) { + Some((name, plane)) if facing(&plane) => Some(name), + Some(_) => self + .rim_face_at(p, tol) + .filter(|(_, plane)| facing(plane)) + .map(|(name, _)| name) + .or_else(|| self.bend_face_at(p, tol)) + .or_else(|| self.ext_rim_at(p, tol)), + None => self.bend_face_at(p, tol).or_else(|| self.ext_rim_at(p, tol)), + } + } + /// The rim of a flange's panel along its bend line, where the panel runs on past the /// bend's start (`:rim.ext0`) or end (`rim.ext1`). fn ext_rim_at(&self, p: Vec3, tol: f64) -> Option { @@ -106,12 +127,13 @@ pub fn body_sheet_face_names(st: &ModelState, b: &ModelBody) -> Vec= 48 { continue; } *n += 1; - if let Some(name) = sheet.face_name_at((a + b2 + c) * (1.0 / 3.0), tol) { + if let Some(name) = sheet.face_name_facing((a + b2 + c) * (1.0 / 3.0), normal, tol) { match v.iter_mut().find(|(x, _)| *x == name) { Some(x) => x.1 += 1, None => v.push((name, 1)),