Wave 210/509: doc, engine, kernel: solid.fillet and solid.chamfer round each picked edge's tangent chain by default (and 3 more)
4 commits, squashed; the originals stay reachable from tag archive/merge-main-develop. - 84bd2daad jyviko: doc, engine, kernel: solid.fillet and solid.chamfer round each picked edge's tangent chain by default - d0ff1dcfa jyviko: doc: an extrude's taper without a corner rule picks one per profile - 5e7aba8df jyviko: loft sections matched to their neighbours - 2752eee61 jyviko: kernel: merged topology counts no seam edge (a face on both sides of it) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
//! ([`crate::blend_smlib`]): the blend faces, the corners where blends meet (a sphere, a patch, a
|
||||
//! bevel or a mitre, as its fillet corners choose) and the end faces rebuilt round them. A blend
|
||||
//! whose contact lines would leave the faces it rolls on is SMLib's refusal, reported as
|
||||
//! [`KernelError::TooLarge`]. A tangent chain is the caller's to select.
|
||||
//! [`KernelError::TooLarge`]. A tangent chain is the caller's to select ([`tangent_chain`]).
|
||||
//!
|
||||
//! The helpers here read a body's schema 4 record: planar faces' outward normals, the direction
|
||||
//! into a face across an edge, an edge's tangent at its ends.
|
||||
@@ -389,6 +389,67 @@ pub fn chamfer_sides(body: &Body, edges: &[Vec3], d1: f64, side: ChamferSide, fl
|
||||
crate::blend_smlib::chamfer_sides(body, edges, d1, side, flip)
|
||||
}
|
||||
|
||||
/// The picked edges' tangent chains, for a blend that is to run along them: `picks` (each the
|
||||
/// nearest edge of `body` to it), then a point on every other edge that continues one of them
|
||||
/// tangentially (within 0.5° at a shared vertex), until no more do. An edge only one face uses
|
||||
/// ends a chain. Picks that find no edge are refused.
|
||||
pub fn tangent_chain(body: &Body, picks: &[Vec3]) -> Result<Vec<Vec3>> {
|
||||
/// Edges meeting at an angle below this continue one another.
|
||||
const TANGENT_DEGREES: f64 = 0.5;
|
||||
if picks.is_empty() || picks.len() > 1000 || picks.iter().any(|p| !p.is_finite()) {
|
||||
return Err(KernelError::Invalid("select 1…1000 edges by finite points".into()));
|
||||
}
|
||||
let rec = Faces::of(body)?;
|
||||
let size = body.size();
|
||||
let mut chain: Vec<usize> = Vec::new();
|
||||
for p in picks {
|
||||
let (e, dist) = body.nearest_edge(*p, (size * 2e-3).max(1e-3))?.ok_or_else(|| KernelError::Invalid("the body has no edges".into()))?;
|
||||
if dist > size * 0.05 + 1e-3 {
|
||||
return Err(KernelError::Invalid(format!("no edge near the pick at ({:.3}, {:.3}, {:.3})", p.x, p.y, p.z)));
|
||||
}
|
||||
if !chain.contains(&e) {
|
||||
chain.push(e);
|
||||
}
|
||||
}
|
||||
let picked = chain.len();
|
||||
// The unit direction edge `e` leaves its end `v` in.
|
||||
let leaving = |e: usize, v: u32| -> Option<Vec3> {
|
||||
let [a, b] = rec.rec.edges.get(e)?.vertices;
|
||||
if a == b {
|
||||
return None;
|
||||
}
|
||||
if a == v { rec.tangent(e, false) } else { rec.tangent(e, true).map(|t| t * -1.0) }
|
||||
};
|
||||
let cos = TANGENT_DEGREES.to_radians().cos();
|
||||
let mut k = 0;
|
||||
while k < chain.len() {
|
||||
let Some(&e) = chain.get(k) else { break };
|
||||
k += 1;
|
||||
let Some(edge) = rec.rec.edges.get(e) else { continue };
|
||||
for v in edge.vertices {
|
||||
let Some(d) = leaving(e, v) else { continue };
|
||||
for (f, other) in rec.rec.edges.iter().enumerate() {
|
||||
if chain.contains(&f) || other.uses.len() < 2 || !other.vertices.contains(&v) {
|
||||
continue;
|
||||
}
|
||||
if leaving(f, v).is_some_and(|g| d.dot(g) < -cos) {
|
||||
chain.push(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out = picks.to_vec();
|
||||
for e in chain.into_iter().skip(picked) {
|
||||
let edge = rec.rec.edges.get(e).ok_or_else(|| KernelError::Failed("a chain edge".into()))?;
|
||||
let mid = CurveEval::new(&edge.curve)
|
||||
.ok()
|
||||
.and_then(|c| c.point(0.5 * (edge.interval[0] + edge.interval[1])))
|
||||
.ok_or_else(|| KernelError::Failed("a point on a chain edge".into()))?;
|
||||
out.push(mid);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used, clippy::expect_used)]
|
||||
mod tests {
|
||||
|
||||
+30
-35
@@ -1056,7 +1056,12 @@ pub(crate) fn loft_reversed(sections: &[(Plane, Loop2)]) -> Result<bool> {
|
||||
|
||||
/// The loft's sections as the walls are built through them: in canonical order (`reversed`
|
||||
/// turns them round), each oriented the same way, re-split to the same number of segments and
|
||||
/// started at the segment that lines up with the lead section's first.
|
||||
/// started at the segment that lines up with the section before it. Every section is matched to
|
||||
/// its neighbour, as the walls pair them: it winds the way the section before it does about the
|
||||
/// normal they share (its plane's normal, turned over where it faces back from the one before),
|
||||
/// so sections whose planes turn on round the loft (a bar bent half a turn) keep one winding;
|
||||
/// and it starts where its vertices line up best with the section before it. The first section is
|
||||
/// matched to the one with the most segments, whose count every section takes.
|
||||
pub(crate) fn loft_numbered_loops(sections: &[(Plane, NumberedLoop)], reversed: bool) -> Result<Vec<(Plane, NumberedLoop)>> {
|
||||
let n = sections.iter().map(|(_, l)| l.lp.segs.len()).max().unwrap_or(0);
|
||||
if n == 0 || n > MAX_SEGS {
|
||||
@@ -1067,45 +1072,35 @@ pub(crate) fn loft_numbered_loops(sections: &[(Plane, NumberedLoop)], reversed:
|
||||
if reversed {
|
||||
ordered.reverse();
|
||||
}
|
||||
// Orient every section counter-clockwise about the first section's normal (`Body::new`
|
||||
// turns the solid outward), then match segment counts and starting points to the section
|
||||
// with the most segments.
|
||||
let n0 = ordered.first().map(|(p, _)| p.normal()).unwrap_or(Vec3::Z);
|
||||
let parallel = ordered.iter().all(|(p, _)| p.normal().dot(n0).abs() > 1.0 - 1e-9);
|
||||
let oriented: Vec<(Plane, NumberedLoop)> = ordered
|
||||
.iter()
|
||||
.map(|(p, l)| {
|
||||
let ccw = p.normal().dot(n0) >= 0.0;
|
||||
(*p, l.oriented(!ccw))
|
||||
})
|
||||
.collect();
|
||||
let lead = oriented.iter().max_by_key(|(_, l)| l.lp.segs.len()).ok_or_else(|| KernelError::Invalid("no sections".into()))?;
|
||||
let lead_c = lead.0.to_world(lead.1.lp.centroid());
|
||||
let lead_dirs: Vec<Vec3> = lead.1.lp.segs.iter().map(|s| lead.0.to_world(s.start()) - lead_c).collect();
|
||||
let mut out = Vec::with_capacity(oriented.len());
|
||||
for (p, l) in &oriented {
|
||||
let c = l.lp.centroid();
|
||||
// Break angles: the lead section's vertex directions seen in this plane.
|
||||
let angles: Vec<f64> = lead_dirs.iter().map(|d| Vec2::new(d.dot(p.x), d.dot(p.y)).angle()).collect();
|
||||
let mut r = l.resample(n, &angles);
|
||||
// Circle resampling creates increasing local angles. An antiparallel sketch
|
||||
// plane must run clockwise locally to keep the same winding in world space.
|
||||
// Curving a run through a right angle does not reverse a section's winding.
|
||||
if parallel && p.normal().dot(n0) < 0.0 {
|
||||
r = r.oriented(true);
|
||||
}
|
||||
// Start where every vertex lines up best with the lead's vertex of the same number (the
|
||||
// largest sum of dot products of their directions from the centroids). The one vertex
|
||||
// closest in direction to the lead's first vertex twisted non-convex sections (#881): a
|
||||
// zig-zag's centroid sits off its outline, so its first vertex can point away from the
|
||||
// lead's.
|
||||
let dirs: Vec<Vec2> = lead_dirs.iter().map(|d| Vec2::new(d.dot(p.x), d.dot(p.y))).collect();
|
||||
let lead = ordered.iter().max_by_key(|(_, l)| l.lp.segs.len()).ok_or_else(|| KernelError::Invalid("no sections".into()))?;
|
||||
let corners = |p: &Plane, l: &NumberedLoop| -> (Vec3, Vec<Vec3>) {
|
||||
let c = p.to_world(l.lp.centroid());
|
||||
(c, l.lp.segs.iter().map(|s| p.to_world(s.start()) - c).collect())
|
||||
};
|
||||
let first_normal = ordered.first().map(|(p, _)| p.normal()).ok_or_else(|| KernelError::Invalid("no sections".into()))?;
|
||||
// What each section is matched to: the normal the windings share, and the corners' directions
|
||||
// from the centre (the lead's for the first section, then the section before).
|
||||
let (mut normal, mut dirs) = (first_normal, corners(&lead.0, &lead.1.oriented(lead.0.normal().dot(first_normal) < 0.0)).1);
|
||||
let mut out = Vec::with_capacity(ordered.len());
|
||||
for (p, l) in &ordered {
|
||||
let back = p.normal().dot(normal) < 0.0;
|
||||
let local: Vec<Vec2> = dirs.iter().map(|d| Vec2::new(d.dot(p.x), d.dot(p.y))).collect();
|
||||
// Break angles: the matched corners seen in this plane. A re-split circle runs counter-
|
||||
// clockwise in its plane; wound the other way when the plane faces back.
|
||||
let angles: Vec<f64> = local.iter().map(|d| d.angle()).collect();
|
||||
let mut r = l.oriented(back).resample(n, &angles).oriented(back);
|
||||
// Start where every vertex lines up best with the matched one of the same number (the
|
||||
// largest sum of dot products of their directions from the centroids): the single vertex
|
||||
// closest in direction twisted non-convex sections (#881).
|
||||
let c = r.lp.centroid();
|
||||
let m = r.lp.segs.len();
|
||||
let fit =
|
||||
|k: usize| -> f64 { dirs.iter().enumerate().filter_map(|(i, d)| r.lp.segs.get((i + k) % m).map(|s| (s.start() - c).dot(*d))).sum() };
|
||||
|k: usize| -> f64 { local.iter().enumerate().filter_map(|(i, d)| r.lp.segs.get((i + k) % m).map(|s| (s.start() - c).dot(*d))).sum() };
|
||||
if let Some(k) = (0..m).min_by(|a, b| fit(*b).total_cmp(&fit(*a))) {
|
||||
r.rotate_left(k);
|
||||
}
|
||||
normal = if back { p.normal() * -1.0 } else { p.normal() };
|
||||
dirs = corners(p, &r).1;
|
||||
out.push((*p, r));
|
||||
}
|
||||
Ok(out)
|
||||
|
||||
@@ -39,7 +39,7 @@ impl DraftCorner {
|
||||
pub enum CornerRefusal {
|
||||
/// The continued curves do not meet (two parallel lines, two circles that part).
|
||||
Apart,
|
||||
/// A curve at the corner is neither a line nor an arc: its wall has no exact continuation.
|
||||
/// A curve of the profile is neither a line nor an arc: its wall has no exact continuation.
|
||||
NotExact,
|
||||
/// A wall at the corner closes before the far end (its arc shrinks to its centre).
|
||||
WallCloses,
|
||||
@@ -97,6 +97,9 @@ pub fn draft_corners(region: &Region2, distance: f64, taper: f64) -> Result<Vec<
|
||||
let segs = &outer.segs;
|
||||
let d = distance * taper.tan();
|
||||
let n = segs.len();
|
||||
// The walls are built as one exact set: a profile with a sharp corner keeps its curvature only
|
||||
// when every curve is a line or an arc.
|
||||
let free = segs.iter().any(|s| matches!(carrier(s, d), Carrier::Free));
|
||||
let mut out = Vec::new();
|
||||
for k in 0..n {
|
||||
let (Some(a), Some(b)) = (segs.get((k + n - 1) % n), segs.get(k)) else { continue };
|
||||
@@ -111,7 +114,7 @@ pub fn draft_corners(region: &Region2, distance: f64, taper: f64) -> Result<Vec<
|
||||
let (ea, eb) = (a.end() + right(ta) * d, b.start() + right(tb) * d);
|
||||
// A left turn opens a gap when the profile grows, a right turn when it shrinks.
|
||||
let gap = (turn > 0.0) == (d > 0.0);
|
||||
let curvature = continued(ca, cb, ea, eb, p, d);
|
||||
let curvature = if free { Err(CornerRefusal::NotExact) } else { continued(ca, cb, ea, eb, p, d) };
|
||||
let options = if gap {
|
||||
[
|
||||
CornerOption { rule: DraftCorner::PreserveCurvature, fill: curvature },
|
||||
|
||||
@@ -150,7 +150,7 @@ mod validity;
|
||||
pub mod watch;
|
||||
mod whole_face;
|
||||
|
||||
pub use blend::{ChamferSide, chamfer, chamfer_sides, fillet, fillet_each, fillet_g2, fillet_g2_corners, fillet_max, fillet_max_each};
|
||||
pub use blend::{ChamferSide, chamfer, chamfer_sides, fillet, fillet_each, fillet_g2, fillet_g2_corners, fillet_max, fillet_max_each, tangent_chain};
|
||||
pub use blend_corner::fillet_radii;
|
||||
pub use blend_variable::{RadiusSpline, Taper, fillet_chord, fillet_tapered, fillet_variable, fillet_variable_points, fillet_variable_spline, taper};
|
||||
pub use body::{Body, CapSignature, DISPLAY_ANGLE, DisplayShare, EdgeInfo, FaceInfo, FacePaint, FaceSource, MeshTolerance, Paint};
|
||||
|
||||
@@ -122,6 +122,8 @@ pub fn merged_topology(b: &Body, mesh: &Mesh) -> Result<TopoCounts> {
|
||||
let mut weld_e: HashMap<(usize, usize, (i64, i64, i64)), usize> = HashMap::new();
|
||||
let mut welded_faces: Vec<Vec<usize>> = Vec::new();
|
||||
let mut welded_ends: Vec<(usize, usize)> = Vec::new();
|
||||
// How many face uses each welded edge has (a seam: two, on one face).
|
||||
let mut welded_uses: Vec<usize> = Vec::new();
|
||||
// A record edge standing for each welded edge (for its points).
|
||||
let mut rep: Vec<u32> = Vec::new();
|
||||
for (ei, (e, between)) in record.edges.iter().zip(edge_faces(record)).enumerate() {
|
||||
@@ -133,8 +135,12 @@ pub fn merged_topology(b: &Body, mesh: &Mesh) -> Result<TopoCounts> {
|
||||
if we == welded_faces.len() {
|
||||
welded_faces.push(Vec::new());
|
||||
welded_ends.push((a, z));
|
||||
welded_uses.push(0);
|
||||
rep.push(ei as u32);
|
||||
}
|
||||
if let Some(n) = welded_uses.get_mut(we) {
|
||||
*n += e.uses.len();
|
||||
}
|
||||
if let Some(fs) = welded_faces.get_mut(we) {
|
||||
for f in between {
|
||||
if !fs.contains(&f) {
|
||||
@@ -206,8 +212,12 @@ pub fn merged_topology(b: &Body, mesh: &Mesh) -> Result<TopoCounts> {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// An edge between two pieces of one merged face, or a seam (one face on both its sides),
|
||||
// is no edge of the merged topology; one face on one side only is an open edge.
|
||||
let seam = welded_uses.get(we).is_some_and(|n| *n >= 2);
|
||||
let pair = match gs[..] {
|
||||
[a, b] if a == b => continue,
|
||||
[_] if seam => continue,
|
||||
[a, b] => (a.min(b), a.max(b)),
|
||||
[a] => (a, a),
|
||||
_ => (usize::MAX, usize::MAX),
|
||||
|
||||
Reference in New Issue
Block a user