diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 8cfa8d482..65bce83f9 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -520,19 +520,8 @@ fn thread_on_impl( }) } -/// The helical space between the teeth of a modelled ISO metric thread (ISO 68-1 basic -/// profile, 60° flanks): cut from the shaft (external) or the hole wall (internal), running from -/// `start` to `end` along the axis. `radius` is the face's own radius; `open` says which ends -/// of the threaded length are free ends of the face. -/// The groove of [`thread_groove_shape`] as the faceted solid the truck kernel cuts. -fn thread_groove(t: &ThreadInfo, radius: f64, open: (bool, bool)) -> Result { - let g = thread_groove_shape(t, radius, open)?; - Ok(kernel::sweep_helix_mesh(&g.plane, &g.section, g.base, g.axis, g.pitch, g.turns, g.stations)?) -} - /// A thread's groove as a screw sweep: its trapezoid `section` in `plane` (an axial plane -/// through `base`), carried `turns` turns about `axis` through `base`, rising `pitch` per turn; -/// `stations` per turn where it is faceted. +/// through `base`), carried `turns` turns about `axis` through `base`, rising `pitch` per turn. struct GrooveShape { plane: Plane, section: [Vec2; 4], @@ -540,10 +529,12 @@ struct GrooveShape { axis: Vec3, pitch: f64, turns: f64, - stations: usize, } -/// The shape of [`thread_groove`]. +/// The helical space between the teeth of a modelled ISO metric thread (ISO 68-1 basic +/// profile, 60° flanks): cut from the shaft (external) or the hole wall (internal), running from +/// `start` to `end` along the axis. `radius` is the face's own radius; `open` says which ends +/// of the threaded length are free ends of the face. fn thread_groove_shape(t: &ThreadInfo, radius: f64, open: (bool, bool)) -> Result { let (p, d) = (t.pitch, t.major_diameter); let h = p * 3f64.sqrt() / 2.0; @@ -611,26 +602,17 @@ fn thread_groove_shape(t: &ThreadInfo, radius: f64, open: (bool, bool)) -> Resul let base = t.axis_point + k * s0; let plane = Plane { origin: base, x: u, y: *k }; let section = [Vec2::new(r0, -w0), Vec2::new(r1, -w1), Vec2::new(r1, w1), Vec2::new(r0, w0)]; - // Keep radial chord error below 1% of the pitch, rather than a fixed fraction of the - // diameter. At 48 stations a large fine thread cuts too deeply; its outer tool facets - // can even dip under the shaft surface. The existing coarse sizes still use 48. - let outer = r0.abs().max(r1.abs()); - if !outer.is_finite() { + if !(r0.is_finite() && r1.is_finite()) { return Err(DocError::Invalid("thread dimensions are too large".into())); } - let angle = (1.0 - (p * 0.01 / outer).min(1.0)).acos(); - let stations = (std::f64::consts::PI / angle).ceil().max(48.0); - if !stations.is_finite() || stations > 720.0 { - return Err(DocError::Invalid("the modelled thread is too fine for this diameter; use a cosmetic thread".into())); - } // The kernel rotates positively about its sweep axis. Reversing both that axis and the // pitch mirrors the angular motion while the lift stays +k * p per turn. The physical - // axis, start/end, radial profile and station count remain exactly as for a right thread. + // axis, start/end and radial profile remain exactly as for a right thread. let (sweep_axis, sweep_pitch) = match t.handedness { crate::ThreadHandedness::Right => (*k, p), crate::ThreadHandedness::Left => (-*k, -p), }; - Ok(GrooveShape { plane, section, base, axis: sweep_axis, pitch: sweep_pitch, turns: (s1 - s0) / p, stations: stations as usize }) + Ok(GrooveShape { plane, section, base, axis: sweep_axis, pitch: sweep_pitch, turns: (s1 - s0) / p }) } /// What a modelled external thread turns off a shaft thicker than its major diameter before it diff --git a/crates/kernel/src/fill.rs b/crates/kernel/src/fill.rs index 166d5e100..ac084be61 100644 --- a/crates/kernel/src/fill.rs +++ b/crates/kernel/src/fill.rs @@ -16,7 +16,7 @@ use solidcraft_geom::brep::eval::SurfaceEval; use solidcraft_geom::brep::{self as b, Curve2, eval::CurveEval}; use solidcraft_geom::bspline::{HPoint, SplineError}; use solidcraft_geom::fill_general::{Across, MAX_FILL_POINTS}; -use solidcraft_geom::fill_iso::{IsoEdge, IsoSpec}; +use solidcraft_geom::fill_iso::{IsoEdge, IsoFill, IsoSpec}; use crate::body::Body; use crate::bridge::Continuity; @@ -139,8 +139,12 @@ fn general_fill( points: &[Vec3], crown: f64, ) -> Result<(Body, PatchReport)> { - if !(3..=4).contains(&chain.len()) { - return Err(KernelError::Invalid(format!("not supported yet: a general patch on a loop of {} edges (three or four)", chain.len()))); + if !(3..=solidcraft_geom::fill_iso::MAX_ISO_EDGES).contains(&chain.len()) { + return Err(KernelError::Invalid(format!( + "not supported yet: a general patch on a loop of {} edges (three to {})", + chain.len(), + solidcraft_geom::fill_iso::MAX_ISO_EDGES + ))); } let edges: Vec = chain.iter().map(|&(e, forward)| iso_edge(record, e, forward)).collect::>()?; let size = body.size().max(solidcraft_geom::LINEAR_TOL); @@ -169,8 +173,7 @@ fn general_fill( }; let spec = IsoSpec { tol: face_tolerance(body), angle_tol: PATCH_ANGLE_TOL_DEG.to_radians(), crown, points }; let fill = solidcraft_geom::fill_iso::iso_fill(&edges, &mut across, &clash, &spec).map_err(spline_error)?; - let surface = surface_record(&fill.surface)?; - let made = patch_body(body, record, chain, surface, true, &Pcurves::Sides(&fill.sides), face_tolerance(body))?; + let made = quilt_body(body, record, chain, &fill, face_tolerance(body))?; let a = fill.achieved; let report = PatchReport { general: true, @@ -221,6 +224,15 @@ fn iso_edge(record: &b::Brep, e: usize, forward: bool) -> Result { if !forward { knots = knots.iter().rev().map(|k| a + z - k).collect(); points.reverse(); + // The interval's ends exactly (the reflection's rounding), so a piece's ends name them. + let n = knots.len(); + for (i, k) in knots.iter_mut().enumerate() { + if i <= degree { + *k = a; + } else if i + degree + 1 >= n { + *k = z; + } + } } // Its ends exactly on the loop's vertices. let vertex = |end: usize| record.vertices.get(edge.vertices[end] as usize).map(|v| v3(&v.point)); @@ -536,12 +548,11 @@ fn conic_on(frame: &b::Frame, radii: [f64; 2], on: &dyn Fn([f64; 3]) -> bool, n: on(frame.origin) && radii[0] * axis(frame.x_axis) + radii[1] * axis(frame.y_axis) <= tol } -/// Where a patch's pcurves come from: the record's routine (a plane), the exact projection onto -/// a height field's plane, or the patch's own sides (each edge a straight line in (u, v)). +/// Where a patch's pcurves come from: the record's routine (a plane), or the exact projection +/// onto a height field's plane. enum Pcurves<'a> { Computed, Projected(&'a Projection), - Sides(&'a [[[f64; 2]; 2]]), } /// The plane a height field stands on: its parameters are coordinates along `axes` from `origin`. @@ -629,13 +640,6 @@ fn patch_body(body: &Body, record: &b::Brep, chain: &[(usize, bool)], surface: b } } let given = match pcurves { - Pcurves::Sides(sides) => { - let [s, z] = sides.get(k).copied().ok_or_else(|| fail("side"))?; - let [a, b] = edge.interval; - // The loop's start of the edge is its interval's start when the loop runs along it. - let (at_a, at_b) = if forward { (s, z) } else { (z, s) }; - Some(Curve2::Spline { knots: vec![a, a, b, b], points: vec![at_a, at_b] }) - } Pcurves::Projected(p) => Some(p.pcurve(&edge.curve, edge.interval).ok_or_else(|| { KernelError::Invalid( "not supported yet: a fitted patch on a circular, elliptic or traced edge (it has no exact pcurve on the fit)".into(), @@ -669,6 +673,140 @@ fn patch_body(body: &Body, record: &b::Brep, chain: &[(usize, bool)], surface: b patch.with_history(history, &[body]) } +/// The sheet of the general fill's patches on the loop `chain` of the record's edges: each edge +/// whole, or (a loop of five or more) its two halves at the middle of its interval, both on its +/// exact curve record; the spokes from the halves' common vertex to the centre; every pcurve the +/// straight line in (u, v) the patch gives its side. Its history is over `[body]`: every face is +/// new, each loop edge (or half) a piece of the body's own, each spoke new. +fn quilt_body(body: &Body, record: &b::Brep, chain: &[(usize, bool)], fill: &IsoFill, tol: f64) -> Result { + use solidcraft_geom::fill_iso::IsoSide; + let fail = |m: &str| KernelError::Failed(format!("patch: {m}")); + let index = |n: usize| u32::try_from(n).map_err(|_| fail("budget")); + let mut vertices: Vec<[f64; 3]> = Vec::new(); + let mut vertex_of: std::collections::HashMap = std::collections::HashMap::new(); + let mut middle_of: std::collections::HashMap = std::collections::HashMap::new(); + let mut edges: Vec = Vec::new(); + // Each part edge's origin: the record edge it is a piece of (`None` for a spoke). + let mut origin: Vec> = Vec::new(); + let mut piece_of: std::collections::HashMap<(usize, u64, u64), u32> = std::collections::HashMap::new(); + let mut spoke_of: std::collections::HashMap = std::collections::HashMap::new(); + let centre = match fill.spokes.first().and_then(|s| s.points.last()) { + Some(c) => { + vertices.push([c.x, c.y, c.z]); + Some(0u32) + } + None => None, + }; + let mut faces = Vec::with_capacity(fill.patches.len()); + for patch in &fill.patches { + let mut uses = Vec::with_capacity(patch.sides.len()); + for side in &patch.sides { + let line = |interval: [f64; 2], at: [[f64; 2]; 2]| Curve2::Spline { + knots: vec![interval[0], interval[0], interval[1], interval[1]], + points: at.to_vec(), + }; + match *side { + IsoSide::Edge { edge: k, from, to, uv } => { + let &(e, forward) = chain.get(k).ok_or_else(|| fail("side"))?; + let edge = record.edges.get(e).ok_or_else(|| fail("edge reference"))?; + let [a, z] = edge.interval; + // The patch's parameter runs the loop's way: the edge's own, reversed where the + // loop runs against it. + let tau = |t: f64| if forward { t } else { a + z - t }; + let (tf, tt) = (tau(from), tau(to)); + let (lo, hi, along) = if tf < tt { (tf, tt, true) } else { (tt, tf, false) }; + let key = (e, lo.to_bits(), hi.to_bits()); + let id = match piece_of.get(&key) { + Some(id) => *id, + None => { + let mut ends = [0u32; 2]; + for (end, t) in ends.iter_mut().zip([lo, hi]) { + *end = if t == a || t == z { + let [start, finish] = edge.vertices; + let v = if t == a { start } else { finish }; + let next = index(vertices.len())?; + let id = *vertex_of.entry(v).or_insert(next); + if id == next { + vertices.push(record.vertices.get(v as usize).ok_or_else(|| fail("vertex reference"))?.point); + } + id + } else { + middle(&mut vertices, &mut middle_of, k, edge, t)? + }; + } + let id = index(edges.len())?; + edges.push(PartEdge { curve: edge.curve.clone(), interval: [lo, hi], vertices: ends }); + origin.push(Some(e)); + piece_of.insert(key, id); + id + } + }; + let at = if along { uv } else { [uv[1], uv[0]] }; + uses.push(PartUse { edge: id, forward: along, given: Some(line([lo, hi], at)) }); + } + IsoSide::Spoke { spoke, forward, uv } => { + let id = match spoke_of.get(&spoke) { + Some(id) => *id, + None => { + let curve = fill.spokes.get(spoke).ok_or_else(|| fail("spoke"))?; + let &(e, _) = chain.get(spoke).ok_or_else(|| fail("spoke"))?; + let edge = record.edges.get(e).ok_or_else(|| fail("edge reference"))?; + let start = middle(&mut vertices, &mut middle_of, spoke, edge, 0.5 * (edge.interval[0] + edge.interval[1]))?; + let end = centre.ok_or_else(|| fail("centre"))?; + let id = index(edges.len())?; + let points = curve.points.iter().map(|p| [p.x, p.y, p.z]).collect(); + edges.push(PartEdge { + curve: b::Curve::Spline { knots: curve.knots.clone(), points }, + interval: [0.0, 1.0], + vertices: [start, end], + }); + origin.push(None); + spoke_of.insert(spoke, id); + id + } + }; + let at = if forward { uv } else { [uv[1], uv[0]] }; + uses.push(PartUse { edge: id, forward, given: Some(line([0.0, 1.0], at)) }); + } + } + } + faces.push(PartFace { surface: surface_record(&patch.surface)?, forward: true, loops: vec![uses] }); + } + let made = assemble(Parts { vertices, edges, faces, tolerance: tol }, false)?; + let smlib = solidcraft_smlib::Brep::from_record(&made.brep, true).map_err(|e| KernelError::Failed(format!("patch: SMLib: {e}")))?; + let (sheet, order) = crate::native::body_from_smlib_ordered(&smlib)?; + let Some(order) = order else { return Ok(sheet) }; + let place = |list: &[usize], c: usize| if list.is_empty() { Some(c) } else { list.get(c).copied() }; + // Each body edge's origin and the body faces beside it. + let mut prov: Vec> = vec![None; sheet.edge_count()]; + let record_out = &made.brep; + for (pe, c) in made.edges.iter().enumerate() { + let Some(c) = *c else { continue }; + let j = place(&order.edges, c).ok_or_else(|| fail("edge order"))?; + let mut beside: Vec = + record_out.edges.get(c).map(|x| x.uses.iter().filter_map(|u| place(&order.faces, u.face as usize)).collect()).unwrap_or_default(); + beside.sort_unstable(); + beside.dedup(); + let origins = origin.get(pe).copied().flatten().map(|edge| vec![EdgeOrigin::Edge { operand: 0, edge }]).unwrap_or_default(); + *prov.get_mut(j).ok_or_else(|| fail("edge order"))? = Some(EdgeProv { faces: beside, origins }); + } + let Some(edges) = prov.into_iter().collect::>>() else { return Ok(sheet) }; + let history = History { operands: 1, faces: vec![FaceProv { origins: Vec::new(), change: Change::Generated }; sheet.face_count()], edges }; + sheet.with_history(history, &[body]) +} + +/// The vertex at parameter `t` inside loop edge `k` (record edge `edge`), made once. +fn middle(vertices: &mut Vec<[f64; 3]>, made: &mut std::collections::HashMap, k: usize, edge: &b::Edge, t: f64) -> Result { + if let Some(v) = made.get(&k) { + return Ok(*v); + } + let p = CurveEval::new(&edge.curve).ok().and_then(|c| c.point(t)).ok_or_else(|| KernelError::Failed("patch: an edge has no middle".into()))?; + let v = u32::try_from(vertices.len()).map_err(|_| KernelError::Failed("patch: vertex budget".into()))?; + vertices.push([p.x, p.y, p.z]); + made.insert(k, v); + Ok(v) +} + /// How far a loop's edges may be from a plane for its patch to be that plane: `10⁻⁷` of the /// body's size, at least the linear tolerance (`10⁻⁶` mm) — the body's own "same point" /// tolerance, since a planar face is exact and other operations treat it as containing its diff --git a/crates/kernel/src/loft_blend.rs b/crates/kernel/src/loft_blend.rs index 2364b200a..7b0f98079 100644 --- a/crates/kernel/src/loft_blend.rs +++ b/crates/kernel/src/loft_blend.rs @@ -41,14 +41,12 @@ //! each end span and leaves every joint's conditions alone. use solidcraft_geom::Vec3; +use solidcraft_geom::brep::eval::SurfaceEval; use solidcraft_geom::bspline::{CurveN, HPoint, MAX_SPLINE_DEGREE, NurbsSurface}; -use truck_modeling as mt; -use truck_modeling::{ParametricSurface3D, SearchNearestParameter}; -use crate::body::{Body, from_p3, p3}; +use crate::body::Body; use crate::bridge::{Continuity, Frame}; -use crate::surfaces::surface_body; -use crate::{KernelError, Result, guard}; +use crate::{KernelError, Result}; /// Most cross-sections one blend may run through. pub const MAX_BLEND_SECTIONS: usize = 50; @@ -81,7 +79,7 @@ const ROWS_PER_SOLVE: usize = 16; pub fn loft_blend(sections: &[Vec], orders: [Continuity; 2], crown: [f64; 2], samples: usize) -> Result { let blend = Blend::new(sections, orders, crown, samples)?; let net = blend.net()?; - guard("loft blend surface", || crate::spline_conv::tensor_face(&net).map(|f| surface_body(vec![f]))) + crate::surfaces::tensor_sheet(&net) } /// The frames of one cross-section picked as an edge chain of a body, sampled at `stations` @@ -94,34 +92,36 @@ pub fn loft_blend(sections: &[Vec], orders: [Continuity; 2], crown: [f64; /// tangent plane (a direction normal to it has no tangent plane to leave in). The last section's /// `leave` runs back into the blend, towards the section before it. pub fn section_frames(body: &Body, edges: &[Vec3], leave: Vec3, stations: usize) -> Result> { - guard("loft blend section", || { - let chain = crate::bridge::chain(body, edges)?; - let leave = - leave.normalized().filter(|l| l.is_finite()).ok_or_else(|| KernelError::Invalid("the leave direction must be a finite vector".into()))?; - let first = chain.first().ok_or_else(|| KernelError::Failed("loft blend: no chain".into()))?; - let mid = (from_p3(first.front().point()) + from_p3(first.back().point())) * 0.5; - // The face the chain lies on that the blend leaves through: the one whose tangent plane - // holds as much of `leave` as possible. - let mut best: Option<(f64, mt::Face)> = None; - for face in body.solid.face_iter() { - if !chain.iter().all(|e| face.edge_iter().any(|f| f.id() == e.id())) { - continue; - } - let surf = face.surface(); - let Some((u, v)) = surf.search_nearest_parameter(p3(mid), None, 100) else { continue }; - let normal = surf.normal(u, v); - let n = Vec3::new(normal.x, normal.y, normal.z).normalized().unwrap_or(Vec3::ZERO); - let score = (leave - n * leave.dot(n)).len(); - if best.as_ref().is_none_or(|(b, _)| score > *b) { - best = Some((score, face.clone())); - } + let chain = crate::bridge::chain(body, edges)?; + let leave = + leave.normalized().filter(|l| l.is_finite()).ok_or_else(|| KernelError::Invalid("the leave direction must be a finite vector".into()))?; + let r = body.record(); + let &(first, _) = chain.first().ok_or_else(|| KernelError::Failed("loft blend: no chain".into()))?; + let ends = r.edges.get(first).map(|e| e.vertices).ok_or_else(|| KernelError::Failed("loft blend: an edge reference".into()))?; + let point = |v: u32| r.vertices.get(v as usize).map(|x| Vec3::new(x.point[0], x.point[1], x.point[2])); + let (Some(a), Some(b)) = (point(ends[0]), point(ends[1])) else { return Err(KernelError::Failed("loft blend: a vertex reference".into())) }; + let mid = (a + b) * 0.5; + // The face the chain lies on that the blend leaves through: the one whose tangent plane holds + // as much of `leave` as possible. + let bounds = |f: usize, e: usize| r.edges.get(e).is_some_and(|x| x.uses.iter().any(|u| u.face as usize == f)); + let mut best: Option<(f64, usize)> = None; + for (f, face) in r.faces.iter().enumerate() { + if !chain.iter().all(|(e, _)| bounds(f, *e)) { + continue; } - let (score, face) = best.ok_or_else(|| KernelError::Invalid("no face of the body holds the picked edges".into()))?; - if score < 0.2 { - return Err(KernelError::Invalid("the direction towards the next section lies in no face at the picked edges".into())); + let Ok(surf) = SurfaceEval::new(&face.surface) else { continue }; + let Some((_, su, sv)) = surf.project(mid, None, f64::INFINITY).and_then(|uv| surf.derivs(uv[0], uv[1])) else { continue }; + let n = su.cross(sv).normalized().unwrap_or(Vec3::ZERO); + let score = (leave - n * leave.dot(n)).len(); + if best.is_none_or(|(b, _)| score > b) { + best = Some((score, f)); } - crate::bridge::frames(&face, &chain, leave, stations) - }) + } + let (score, face) = best.ok_or_else(|| KernelError::Invalid("no face of the body holds the picked edges".into()))?; + if score < 0.2 { + return Err(KernelError::Invalid("the direction towards the next section lies in no face at the picked edges".into())); + } + crate::bridge::frames(body, face, &chain, leave, stations) } /// What the face has to do at one section: where it is, and the run direction's first, second and diff --git a/crates/kernel/src/matchsurf.rs b/crates/kernel/src/matchsurf.rs index 8134ea341..623a1ef1f 100644 --- a/crates/kernel/src/matchsurf.rs +++ b/crates/kernel/src/matchsurf.rs @@ -16,15 +16,14 @@ //! of the target meet, with shapes that part company, is a seam the target itself does not hold //! beyond that order, and no surface can be told to carry a condition the target has not got. -use mt::{BoundedCurve, ParametricCurve, ParametricSurface, ParametricSurface3D, SearchNearestParameter}; use solidcraft_geom::Vec3; -use truck_modeling as mt; +use solidcraft_geom::brep::{self as b, eval::CurveEval, eval::SurfaceEval}; -use crate::body::{Body, from_p3, p3}; +use crate::body::Body; use crate::bridge::{self, Continuity, Frame}; -use crate::record::Rebuild; -use crate::surfaces::surface_body; -use crate::{KernelError, Result, guard}; +use crate::history::{Change, EdgeProv, History}; +use crate::surfaces::Chain; +use crate::{KernelError, Result}; /// Stations a match reads along the seam and along the boundary it keeps. const STATIONS: usize = 33; @@ -61,27 +60,31 @@ const SUPPLY_BEND_REL: f64 = 1e-1; /// target's own shape along it up to `order` — position, tangent, curvature vector, and the /// change of that curvature vector along the direction the match leaves in. The boundary /// opposite keeps its place and the original surface's own frame, and the middle is shaped to -/// stay as close to the original surface as the conditions allow. +/// stay as close to the original surface as the conditions allow. Its history is over +/// `[body, target]`: the face is the body's face, modified; its edges are new. pub fn match_surface(body: &Body, faces: &[Vec3], target: &Body, target_pick: &[Vec3], order: Continuity) -> Result { body.require_brep("match")?; target.require_brep("match")?; if !body.is_surface() { return Err(KernelError::Invalid("match rebuilds a surface body: this one is closed".into())); } - let face = face_near(body, faces)?; + let (record, target_record) = (body.record(), target.record()); + let face = face_near(&record, faces, body.size() * 0.05 + 1e-6)?; + let original = Patch::of(&record, face)?; let chain = bridge::chain(target, target_pick)?; - let near = nearest_side(&face, &chain)?; + let line = chain_polyline(&target_record, &chain)?; + let near = nearest_side(&original, &line)?; let far = near.opposite(); - let far_edges = side_edges(&face, far, body.size() * 1e-6 + 1e-9)?; - let (mid_near, mid_far) = (chain_mid(&chain)?, Side::at(&face.surface(), &ranges(&face)?, far, 0.5)); + let far_edges = side_edges(&record, face, &original, far, body.size() * 1e-6 + 1e-9)?; + let (mid_near, mid_far) = (chain_mid(&line)?, original.at(far, 0.5)?); let leave = (mid_far - mid_near) .normalized() .filter(|l| l.is_finite()) .ok_or_else(|| KernelError::Invalid("the far boundary sits on the picked chain: there is nothing to match".into()))?; // Which face of the target the match continues from, and how much of the requested order the // target itself holds along that chain. - let candidates = support_faces(target, &chain, leave)?; - let supply = supply_order(&candidates, &chain, leave, target.size())?; + let candidates = support_faces(target, &chain, &line, leave)?; + let supply = supply_order(target, &candidates, &chain, leave)?; if order.conditions() > supply.conditions() { return Err(KernelError::Invalid(format!( "the target's own surfaces part company at the picked edges: they hold only {} there, so a seam cannot hold {}", @@ -89,13 +92,13 @@ pub fn match_surface(body: &Body, faces: &[Vec3], target: &Body, target_pick: &[ order.as_str() ))); } - let Some((target_face, _)) = candidates.first() else { + let Some(&(target_face, _)) = candidates.first() else { return Err(KernelError::Invalid("no face of the target holds the picked edges".into())); }; // The stations of the seam: what the target does there along the direction the match leaves // in, and what the far boundary does coming back the other way. - let mut near_frames = bridge::frames(target_face, &chain, leave, STATIONS)?; - let far_frames = bridge::frames(&face, &far_edges, leave * -1.0, STATIONS)?; + let mut near_frames = bridge::frames(target, target_face, &chain, leave, STATIONS)?; + let far_frames = bridge::frames(body, face, &far_edges, leave * -1.0, STATIONS)?; // The two chains are sampled from their own starts: turn one round when that pairs the // stations better (a match is built station to station). let direct: f64 = near_frames.iter().zip(&far_frames).map(|(a, b)| a.p.dist(b.p)).sum(); @@ -103,37 +106,36 @@ pub fn match_surface(body: &Body, faces: &[Vec3], target: &Body, target_pick: &[ if reversed < direct { near_frames.reverse(); } - guard("surface match", || { - // The crown is the free knob: keep the one whose surface stays closest to the original. - let mut best: Option<(f64, mt::Face)> = None; - for crown_near in CROWNS { - for crown_far in CROWNS { - let made = bridge::bridge(&near_frames, &far_frames, [order, Continuity::G1], [crown_near, crown_far])?; - let Some(made) = made.solid.face_iter().next().cloned() else { - return Err(KernelError::Failed("match: the bridge made no face".into())); - }; - let stray = deviation(&made, &face)?; - if best.as_ref().is_none_or(|(b, _)| stray < *b) { - best = Some((stray, made)); - } + // The crown is the free knob: keep the one whose surface stays closest to the original. + let mut best: Option<(f64, Body)> = None; + for crown_near in CROWNS { + for crown_far in CROWNS { + let made = bridge::bridge(&near_frames, &far_frames, [order, Continuity::G1], [crown_near, crown_far])?; + let stray = deviation(&Patch::of(&made.record(), 0)?, &original)?; + if best.as_ref().is_none_or(|(b, _)| stray < *b) { + best = Some((stray, made)); } } - let Some((_, made)) = best else { - return Err(KernelError::Failed("match: no crown built a surface".into())); - }; - let mut kept = made; - // The rebuilt face takes the target's own sense at the seam: the two surfaces then meet - // the same way round, which is what a stitch, a thicken or a display of the result reads. - let seam = chain_mid(&chain)?; - if let (Some((made_n, _)), Some((target_n, _))) = (normal_near(&kept, seam), normal_near(target_face, seam)) - && made_n.dot(target_n) < 0.0 - { - kept.invert(); - } - let mut rec = Rebuild::default(); - rec.face(&kept, &face); - Ok(crate::record::rebuilt_body(surface_body(vec![kept]), &[&body.solid, &target.solid], &rec)) - }) + } + let Some((_, made)) = best else { + return Err(KernelError::Failed("match: no crown built a surface".into())); + }; + // The rebuilt face takes the target's own sense at the seam: the two surfaces then meet the + // same way round, which is what a stitch, a thicken or a display of the result reads. + let seam = chain_mid(&line)?; + let made_record = made.record(); + let turn = match (Patch::of(&made_record, 0)?.normal_near(seam), Patch::of(&target_record, target_face)?.normal_near(seam)) { + (Some((m, _)), Some((t, _))) => m.dot(t) < 0.0, + _ => false, + }; + let kept = crate::surfaces::Parts::of(&made_record, &[(0, turn)])?.canonical()?; + let sheet = crate::surfaces::surface_body(&kept.brep)?; + let history = History { + operands: 2, + faces: vec![crate::record::from_face(0, face, Change::Modified)], + edges: (0..sheet.edge_count()).map(|_| EdgeProv { faces: vec![0], origins: Vec::new() }).collect(), + }; + sheet.with_history(history, &[body, target]) } /// How well the rebuilt surface `made` meets `target` along the chain picked by `target_pick`: @@ -143,68 +145,61 @@ pub fn match_surface(body: &Body, faces: &[Vec3], target: &Body, target_pick: &[ pub fn seam_continuity(made: &Body, target: &Body, target_pick: &[Vec3]) -> Result<(f64, f64, Continuity)> { made.require_brep("match")?; target.require_brep("match")?; + let (record, target_record) = (made.record(), target.record()); let chain = bridge::chain(target, target_pick)?; - let poly = chain_polyline(&chain)?; + let poly = chain_polyline(&target_record, &chain)?; // The face whose boundary runs along the chain: the one nearest it, so that a body that has // not been matched yet — where the seam is still some way off — can be measured too. - let mid = chain_mid(&chain)?; - let face = made - .solid - .face_iter() - .filter_map(|f| face_distance(f, mid).map(|d| (d, f))) + let mid = chain_mid(&poly)?; + let face = (0..record.faces.len()) + .filter_map(|f| Patch::of(&record, f).ok().and_then(|p| p.normal_near(mid).map(|(_, d)| (d, p)))) .min_by(|a, b| a.0.total_cmp(&b.0)) - .map(|(_, f)| f.clone()) + .map(|(_, p)| p) .ok_or_else(|| KernelError::Failed("match: the body has no faces".into()))?; - let near = nearest_side(&face, &chain)?; + let near = nearest_side(&face, &poly)?; let far = near.opposite(); - let ranges = ranges(&face)?; - let mid_far = Side::at(&face.surface(), &ranges, far, 0.5); - let leave = (mid_far - chain_mid(&chain)?) + let leave = (face.at(far, 0.5)? - mid) .normalized() .ok_or_else(|| KernelError::Invalid("the seam and the far boundary coincide: there is nothing to measure".into()))?; - let candidates = support_faces(target, &chain, leave)?; - let Some((target_face, _)) = candidates.first() else { + let candidates = support_faces(target, &chain, &poly, leave)?; + let Some(&(target_face, _)) = candidates.first() else { return Err(KernelError::Invalid("no face of the target holds the picked edges".into())); }; - let frames = bridge::frames(target_face, &chain, leave, STATIONS)?; + let target_patch = Patch::of(&target_record, target_face)?; + let frames = bridge::frames(target, target_face, &chain, leave, STATIONS)?; let size = target.size(); - guard("surface match", || { - let surf = face.surface(); - let (mut gap, mut deg, mut curv, mut bend) = (0.0f64, 0.0f64, 0.0f64, 0.0f64); - for i in 0..STATIONS { - let t = i as f64 / (STATIONS - 1) as f64; - let p = Side::at(&surf, &ranges, near, t); - gap = gap.max(chain_distance(&poly, p)); - // What the target says there, and what the rebuilt surface actually does there. - let Some(frame) = frames.iter().min_by(|a, b| a.p.dist(p).total_cmp(&b.p.dist(p))) else { - return Err(KernelError::Failed("match: the target has no frames".into())); - }; - let (_, t_made, bend_made, change_made) = seam_shape(&surf, &ranges, near, t)?; - if let Some((n_made, _)) = normal_near(&face, p) - && let Some((n_target, _)) = normal_near(target_face, p) - { - deg = deg.max(angle_deg(n_made, n_target)); - } - let scale = frame.bend.len().max(bend_made.len()).max(1.0 / size); - curv = curv.max((bend_made - frame.bend).len() / scale); - // The change of curvature is what a surface can hold: only its normal part counts, - // and it turns round with the direction of travel. - let change = if t_made.dot(frame.leave) < 0.0 { change_made * -1.0 } else { change_made }; - let scale3 = frame.bend_change.len().max(change.len()).max(scale / size); - bend = bend.max((change - frame.bend_change).len() / scale3); + let (mut gap, mut deg, mut curv, mut bend) = (0.0f64, 0.0f64, 0.0f64, 0.0f64); + for i in 0..STATIONS { + let t = i as f64 / (STATIONS - 1) as f64; + let p = face.at(near, t)?; + gap = gap.max(chain_distance(&poly, p)); + // What the target says there, and what the rebuilt surface actually does there. + let Some(frame) = frames.iter().min_by(|a, b| a.p.dist(p).total_cmp(&b.p.dist(p))) else { + return Err(KernelError::Failed("match: the target has no frames".into())); + }; + let (_, t_made, bend_made, change_made) = face.seam_shape(near, t)?; + if let (Some((n_made, _)), Some((n_target, _))) = (face.normal_near(p), target_patch.normal_near(p)) { + deg = deg.max(angle_deg(n_made, n_target)); } - let mut order = Continuity::G0; - if deg <= G1_DEG { - order = Continuity::G1; - if curv <= G2_REL { - order = Continuity::G2; - if bend <= G3_REL { - order = Continuity::G3; - } + let scale = frame.bend.len().max(bend_made.len()).max(1.0 / size); + curv = curv.max((bend_made - frame.bend).len() / scale); + // The change of curvature is what a surface can hold: only its normal part counts, and it + // turns round with the direction of travel. + let change = if t_made.dot(frame.leave) < 0.0 { change_made * -1.0 } else { change_made }; + let scale3 = frame.bend_change.len().max(change.len()).max(scale / size); + bend = bend.max((change - frame.bend_change).len() / scale3); + } + let mut order = Continuity::G0; + if deg <= G1_DEG { + order = Continuity::G1; + if curv <= G2_REL { + order = Continuity::G2; + if bend <= G3_REL { + order = Continuity::G3; } } - Ok((gap, deg, order)) - }) + } + Ok((gap, deg, order)) } /// Which side of a face's parameter rectangle a boundary runs along: the match reads its face as @@ -251,58 +246,89 @@ impl Side { } /// The point at the normalized station `t` along the side. - fn at(surf: &mt::Surface, ranges: &[(f64, f64); 2], side: Side, t: f64) -> Vec3 { + fn at(surf: &SurfaceEval, ranges: &[(f64, f64); 2], side: Side, t: f64) -> Result { let (u, v) = (ranges[0], ranges[1]); let (fixed, along) = side.fixed_and_along(u, v); let t = along.0 + (along.1 - along.0) * t.clamp(0.0, 1.0); - if side.is_u() { from_p3(surf.subs(fixed, t)) } else { from_p3(surf.subs(t, fixed)) } + surf.point(if side.is_u() { [fixed, t] } else { [t, fixed] }).ok_or_else(|| KernelError::Failed("match: a side has no point there".into())) } } -/// The bounded parameter ranges of a face's surface. -fn ranges(face: &mt::Face) -> Result<[(f64, f64); 2]> { - let surf = face.surface(); - let (Some(u), Some(v)) = surf.try_range_tuple() else { - return Err(KernelError::Failed("match: the face's surface is unbounded".into())); - }; - Ok([u, v]) +/// A face read as a tensor patch: its record surface and its parameter box. +struct Patch { + surface: SurfaceEval, + ranges: [(f64, f64); 2], } -/// How far `p` is from the face's own surface. -fn face_distance(face: &mt::Face, p: Vec3) -> Option { - let surf = face.surface(); - let (u, v) = surf.search_nearest_parameter(p3(p), None, 100)?; - Some(from_p3(surf.subs(u, v)).dist(p)) +impl Patch { + fn of(r: &b::Brep, face: usize) -> Result { + let f = r.faces.get(face).ok_or_else(|| KernelError::Invalid(format!("no face {face}")))?; + let surface = SurfaceEval::new(&f.surface).map_err(|e| KernelError::Failed(format!("match: face {face}: {e}")))?; + let [[u0, u1], [v0, v1]] = f.domain; + Ok(Patch { surface, ranges: [(u0, u1), (v0, v1)] }) + } + + fn at(&self, side: Side, t: f64) -> Result { + Side::at(&self.surface, &self.ranges, side, t) + } + + /// The surface's unit normal at the point nearest `p`, and how far `p` is from that point. + fn normal_near(&self, p: Vec3) -> Option<(Vec3, f64)> { + let uv = self.surface.project(p, None, f64::INFINITY)?; + let (q, su, sv) = self.surface.derivs(uv[0], uv[1])?; + Some((su.cross(sv).normalized()?, q.dist(p))) + } + + /// The parameters of the point nearest `p`. + fn parameters(&self, p: Vec3) -> Option<[f64; 2]> { + self.surface.project(p, None, f64::INFINITY) + } + + /// The shape of the surface along its cross direction at the station `t`, read inwards from + /// the boundary `near` over five one-sided samples: the point, the unit tangent, the + /// curvature vector and the change of that curvature. The same finite difference + /// [`crate::bridge::frames`] reads a support surface with, so that a rebuilt seam can be + /// compared with what it matched. + fn seam_shape(&self, near: Side, t: f64) -> Result<(Vec3, Vec3, Vec3, Vec3)> { + let ranges = self.ranges; + let (station, cross, at_start) = if near.is_u() { + (ranges[1].0 + (ranges[1].1 - ranges[1].0) * t, ranges[0], near == Side::U0) + } else { + (ranges[0].0 + (ranges[0].1 - ranges[0].0) * t, ranges[1], near == Side::V0) + }; + let span = (cross.1 - cross.0).abs().max(1e-12); + let h = span * 1e-3; + let c0 = if at_start { cross.0 } else { cross.1 }; + let sgn = if at_start { 1.0 } else { -1.0 }; + let at = |x: f64| self.surface.point(if near.is_u() { [x, station] } else { [station, x] }); + let q: Vec = + (0..5).map(|k| at(c0 + sgn * k as f64 * h)).collect::>().ok_or_else(|| KernelError::Failed("match: seam samples".into()))?; + let (Some(&q0), Some(&q1), Some(&q2), Some(&q3), Some(&q4)) = (q.first(), q.get(1), q.get(2), q.get(3), q.get(4)) else { + return Err(KernelError::Failed("match: seam samples".into())); + }; + let d1 = (q0 * -25.0 + q1 * 48.0 + q2 * -36.0 + q3 * 16.0 + q4 * -3.0) * (sgn / (12.0 * h)); + let d2 = (q0 * 35.0 + q1 * -104.0 + q2 * 114.0 + q3 * -56.0 + q4 * 11.0) * (1.0 / (12.0 * h * h)); + let d3 = (q0 * -5.0 + q1 * 18.0 + q2 * -24.0 + q3 * 14.0 + q4 * -3.0) * (sgn / (2.0 * h * h * h)); + let (t, bend, change) = shape(d1, d2, d3)?; + Ok((q0, t, bend, change)) + } } -/// The surface's unit normal at the point nearest `p`, and how far `p` is from that point. -fn normal_near(face: &mt::Face, p: Vec3) -> Option<(Vec3, f64)> { - let surf = face.surface(); - let (u, v) = surf.search_nearest_parameter(p3(p), None, 100)?; - let n = surf.normal(u, v); - let n = Vec3::new(n.x, n.y, n.z).normalized()?; - Some((n, from_p3(surf.subs(u, v)).dist(p))) -} - -/// The face of `body` the picks lie on: they must all pick the same one. A pick is a point the -/// caller clicked, so the face nearest it within the body's own pick tolerance is the face it -/// means (`crate::surfaces::edge_near` reads points the same way). -fn face_near(body: &Body, picks: &[Vec3]) -> Result { +/// The face of the record the picks lie on: they must all pick the same one, each within `reach` +/// of its surface (a pick is a point the caller clicked). +fn face_near(r: &b::Brep, picks: &[Vec3], reach: f64) -> Result { if picks.is_empty() || picks.len() > MAX_PICKS || picks.iter().any(|p| !p.is_finite()) { return Err(KernelError::Invalid(format!("pick 1 to {MAX_PICKS} points on the face to match"))); } - let tol = body.size() * 0.05 + 1e-6; - let mut found: Option = None; + let mut found: Option = None; for p in picks { - let hit = body - .solid - .face_iter() - .filter_map(|f| face_distance(f, *p).map(|d| (d, f))) + let hit = (0..r.faces.len()) + .filter_map(|f| Patch::of(r, f).ok().and_then(|x| x.normal_near(*p)).map(|(_, d)| (d, f))) .min_by(|a, b| a.0.total_cmp(&b.0)) - .filter(|(d, _)| *d <= tol) - .map(|(_, f)| f.clone()) + .filter(|(d, _)| *d <= reach) + .map(|(_, f)| f) .ok_or_else(|| KernelError::Invalid(format!("no face of the body passes through {p:?}")))?; - if found.as_ref().is_some_and(|f| f.id() != hit.id()) { + if found.is_some_and(|f| f != hit) { return Err(KernelError::Invalid("the picks are on different faces: match one face".into())); } found = Some(hit); @@ -310,28 +336,27 @@ fn face_near(body: &Body, picks: &[Vec3]) -> Result { found.ok_or_else(|| KernelError::Invalid("pick a point on the face to match".into())) } -/// A chain of edges as a polyline (each edge sampled), for lengths and distances. -fn chain_polyline(chain: &[mt::Edge]) -> Result> { +/// A chain of the record's edges as a polyline (each edge sampled the chain's way), for lengths +/// and distances. +fn chain_polyline(r: &b::Brep, chain: &Chain) -> Result> { if chain.is_empty() { return Err(KernelError::Invalid("the chain has no edges".into())); } let mut out = Vec::with_capacity(chain.len() * (PER_EDGE + 1)); - for e in chain { - let c = e.curve(); - let (t0, t1) = c.range_tuple(); - if !(t1 > t0) || ![t0, t1].into_iter().all(f64::is_finite) { - return Err(KernelError::Failed("match: an edge has no parameter range".into())); - } + for &(e, forward) in chain { + let edge = r.edges.get(e).ok_or_else(|| KernelError::Invalid("an edge reference".into()))?; + let c = CurveEval::new(&edge.curve).map_err(|err| KernelError::Failed(format!("match: {err}")))?; + let [t0, t1] = edge.interval; + let (a, z) = if forward { (t0, t1) } else { (t1, t0) }; for k in 0..=PER_EDGE { - out.push(from_p3(c.subs(t0 + (t1 - t0) * k as f64 / PER_EDGE as f64))); + out.push(c.point(a + (z - a) * k as f64 / PER_EDGE as f64).ok_or_else(|| KernelError::Failed("match: an edge has no point".into()))?); } } Ok(out) } -/// The point at the middle of a chain, by length. -fn chain_mid(chain: &[mt::Edge]) -> Result { - let poly = chain_polyline(chain)?; +/// The point at the middle of a polyline, by length. +fn chain_mid(poly: &[Vec3]) -> Result { let mut cum = Vec::with_capacity(poly.len()); cum.push(0.0); for w in poly.windows(2) { @@ -345,10 +370,7 @@ fn chain_mid(chain: &[mt::Edge]) -> Result { } let half = total * 0.5; let hi = cum.partition_point(|x| *x < half).clamp(1, poly.len().saturating_sub(1)); - let (Some(a), Some(b)) = (poly.get(hi - 1), poly.get(hi)) else { - return Err(KernelError::Failed("match: chain".into())); - }; - let (Some(&lo), Some(&up)) = (cum.get(hi - 1), cum.get(hi)) else { + let (Some(a), Some(b), Some(&lo), Some(&up)) = (poly.get(hi - 1), poly.get(hi), cum.get(hi - 1), cum.get(hi)) else { return Err(KernelError::Failed("match: chain".into())); }; let span = up - lo; @@ -368,19 +390,15 @@ fn chain_distance(poly: &[Vec3], p: Vec3) -> f64 { best } -/// The side of the face's own parameter rectangle the picked chain sits nearest. -fn nearest_side(face: &mt::Face, chain: &[mt::Edge]) -> Result { - let surf = face.surface(); - let ranges = ranges(face)?; - let poly = chain_polyline(chain)?; +/// The side of the face's own parameter rectangle the picked chain (as a polyline) sits nearest. +fn nearest_side(face: &Patch, poly: &[Vec3]) -> Result { /// Samples of each side used to tell which one the chain runs along. const SAMPLES: usize = 17; let mut best: Option<(f64, Side)> = None; for side in Side::ALL { let mut total = 0.0; for k in 0..SAMPLES { - let t = k as f64 / (SAMPLES - 1) as f64; - total += chain_distance(&poly, Side::at(&surf, &ranges, side, t)); + total += chain_distance(poly, face.at(side, k as f64 / (SAMPLES - 1) as f64)?); } let mean = total / SAMPLES as f64; if best.is_none_or(|(b, _)| mean < b) { @@ -392,49 +410,48 @@ fn nearest_side(face: &mt::Face, chain: &[mt::Edge]) -> Result { /// The face's own edges that run along a side of its parameter rectangle, chained head to tail. /// A match keeps the far side's own edges, so they must be a chain that runs on. -fn side_edges(face: &mt::Face, side: Side, tol: f64) -> Result> { - let surf = face.surface(); - let ranges = ranges(face)?; - let (fixed, along) = side.fixed_and_along(ranges[0], ranges[1]); +fn side_edges(r: &b::Brep, face: usize, patch: &Patch, side: Side, tol: f64) -> Result { + let (fixed, along) = side.fixed_and_along(patch.ranges[0], patch.ranges[1]); let band = (along.1 - along.0).abs().max(1e-12) * 1e-3; - let mut pool: Vec = Vec::new(); - for e in face.edge_iter() { - let c = e.curve(); - let (t0, t1) = c.range_tuple(); - let p = from_p3(c.subs(0.5 * (t0 + t1))); - let Some((u, v)) = surf.search_nearest_parameter(p3(p), None, 100) else { continue }; - if from_p3(surf.subs(u, v)).dist(p) > tol { - continue; - } - let got = if side.is_u() { u } else { v }; - if (got - fixed).abs() <= band { - pool.push(e.clone()); + let f = r.faces.get(face).ok_or_else(|| KernelError::Invalid(format!("no face {face}")))?; + let mut pool: Vec = Vec::new(); + for l in &f.loops { + let b::Loop::Edges { edges } = l else { continue }; + for u in edges { + let e = u.edge as usize; + let Some(edge) = r.edges.get(e) else { continue }; + let Ok(c) = CurveEval::new(&edge.curve) else { continue }; + let Some(p) = c.point(0.5 * (edge.interval[0] + edge.interval[1])) else { continue }; + let Some(uv) = patch.parameters(p) else { continue }; + if patch.surface.point(uv).is_none_or(|q| q.dist(p) > tol) { + continue; + } + let got = if side.is_u() { uv[0] } else { uv[1] }; + if (got - fixed).abs() <= band && !pool.contains(&e) { + pool.push(e); + } } } if pool.is_empty() { return Err(KernelError::Invalid("the face has no edges along the boundary a match would keep".into())); } + let ends = |e: usize| r.edges.get(e).map(|x| x.vertices).unwrap_or([u32::MAX; 2]); let first = pool.remove(0); - let mut chain = vec![first]; - let mut end = match chain.first() { - Some(e) => e.back().clone(), - None => return Err(KernelError::Failed("match: chain".into())), - }; - while let Some(k) = pool.iter().position(|e| e.front().id() == end.id() || e.back().id() == end.id()) { + let mut chain: Chain = vec![(first, true)]; + let [mut start, mut end] = ends(first); + while let Some(k) = pool.iter().position(|e| ends(*e).contains(&end)) { let e = pool.remove(k); - let e = if e.front().id() == end.id() { e } else { e.inverse() }; - end = e.back().clone(); - chain.push(e); + let [a, z] = ends(e); + let forward = a == end; + end = if forward { z } else { a }; + chain.push((e, forward)); } - let mut start = match chain.first() { - Some(e) => e.front().clone(), - None => return Err(KernelError::Failed("match: chain".into())), - }; - while let Some(k) = pool.iter().position(|e| e.front().id() == start.id() || e.back().id() == start.id()) { + while let Some(k) = pool.iter().position(|e| ends(*e).contains(&start)) { let e = pool.remove(k); - let e = if e.back().id() == start.id() { e } else { e.inverse() }; - start = e.front().clone(); - chain.insert(0, e); + let [a, z] = ends(e); + let forward = z == start; + start = if forward { a } else { z }; + chain.insert(0, (e, forward)); } if !pool.is_empty() { return Err(KernelError::Invalid("the face's edges along that boundary do not run on from one another".into())); @@ -442,23 +459,26 @@ fn side_edges(face: &mt::Face, side: Side, tol: f64) -> Result> { Ok(chain) } -/// The faces of `target` that meet the chain and whose tangent plane holds the leave direction, -/// best first: the face a match continues from, and the ones it might have been read from. -fn support_faces(target: &Body, chain: &[mt::Edge], leave: Vec3) -> Result> { - let mid = chain_mid(chain)?; +/// The faces of `target` that bound every edge of the chain and whose tangent plane holds the +/// leave direction, best first: the face a match continues from, and the ones it might have been +/// read from. +fn support_faces(target: &Body, chain: &Chain, poly: &[Vec3], leave: Vec3) -> Result> { + let r = target.record(); + let mid = chain_mid(poly)?; let tol = target.size() * 1e-6 + 1e-9; - let mut out: Vec<(mt::Face, f64)> = Vec::new(); - for face in target.solid.face_iter() { - if !chain.iter().all(|e| face.edge_iter().any(|f| f.id() == e.id())) { + let bounds = |f: usize, e: usize| r.edges.get(e).is_some_and(|x| x.uses.iter().any(|u| u.face as usize == f)); + let mut out: Vec<(usize, f64)> = Vec::new(); + for f in 0..r.faces.len() { + if !chain.iter().all(|(e, _)| bounds(f, *e)) { continue; } - let Some((n, d)) = normal_near(face, mid) else { continue }; + let Some((n, d)) = Patch::of(&r, f).ok().and_then(|p| p.normal_near(mid)) else { continue }; if d > tol { continue; } let score = (leave - n * leave.dot(n)).len(); if score.is_finite() && score >= 0.2 { - out.push((face.clone(), score)); + out.push((f, score)); } } if out.is_empty() { @@ -474,25 +494,29 @@ fn support_faces(target: &Body, chain: &[mt::Edge], leave: Vec3) -> Result Result { - let Some((first, best)) = candidates.first() else { +fn supply_order(target: &Body, candidates: &[(usize, f64)], chain: &Chain, leave: Vec3) -> Result { + let Some(&(first, best)) = candidates.first() else { return Err(KernelError::Invalid("no face of the target holds the picked edges".into())); }; - let keep: Vec<&mt::Face> = candidates.iter().filter(|(_, s)| *s >= 0.5 * best).map(|(f, _)| f).collect(); + let keep: Vec = candidates.iter().filter(|(_, s)| *s >= 0.5 * best).map(|(f, _)| *f).collect(); let mut order = Continuity::G3; if keep.len() < 2 { return Ok(order); } - let primary: Vec = bridge::frames(first, chain, leave, STATIONS)?; - for other in keep.iter().skip(1) { + let r = target.record(); + let size = target.size(); + let first_patch = Patch::of(&r, first)?; + let primary: Vec = bridge::frames(target, first, chain, leave, STATIONS)?; + for &other in keep.iter().skip(1) { // A face that cannot be read along the whole chain says nothing here: the face the match // is built on is the one that must be able to supply it. - let Ok(other_frames) = bridge::frames(other, chain, leave, STATIONS) else { continue }; + let Ok(other_frames) = bridge::frames(target, other, chain, leave, STATIONS) else { continue }; + let other_patch = Patch::of(&r, other)?; let mut worst_deg = 0.0f64; let mut worst = 0.0f64; let mut worst_change = 0.0f64; for (a, b) in primary.iter().zip(&other_frames) { - if let (Some((na, _)), Some((nb, _))) = (normal_near(first, a.p), normal_near(other, b.p)) { + if let (Some((na, _)), Some((nb, _))) = (first_patch.normal_near(a.p), other_patch.normal_near(b.p)) { worst_deg = worst_deg.max(angle_deg(na, nb)); } let scale = a.bend.len().max(b.bend.len()).max(1.0 / size); @@ -544,46 +568,18 @@ fn shape(d1: Vec3, d2: Vec3, d3: Vec3) -> Result<(Vec3, Vec3, Vec3)> { Ok((t, nn * (1.0 / (sigma * sigma)), change)) } -/// The shape of a face's surface along its cross direction at the station `t`, read inwards from -/// the boundary `near` over five one-sided samples: the point, the unit tangent, the curvature -/// vector and the change of that curvature. The same finite difference [`crate::bridge::frames`] -/// reads a support surface with, so that a rebuilt seam can be compared with what it matched. -fn seam_shape(surf: &mt::Surface, ranges: &[(f64, f64); 2], near: Side, t: f64) -> Result<(Vec3, Vec3, Vec3, Vec3)> { - let (station, cross, at_start) = if near.is_u() { - (ranges[1].0 + (ranges[1].1 - ranges[1].0) * t, ranges[0], near == Side::U0) - } else { - (ranges[0].0 + (ranges[0].1 - ranges[0].0) * t, ranges[1], near == Side::V0) - }; - let span = (cross.1 - cross.0).abs().max(1e-12); - let h = span * 1e-3; - let c0 = if at_start { cross.0 } else { cross.1 }; - let sgn = if at_start { 1.0 } else { -1.0 }; - let at = |x: f64| if near.is_u() { from_p3(surf.subs(x, station)) } else { from_p3(surf.subs(station, x)) }; - let q: Vec = (0..5).map(|k| at(c0 + sgn * k as f64 * h)).collect(); - let (Some(&q0), Some(&q1), Some(&q2), Some(&q3), Some(&q4)) = (q.first(), q.get(1), q.get(2), q.get(3), q.get(4)) else { - return Err(KernelError::Failed("match: seam samples".into())); - }; - let d1 = (q0 * -25.0 + q1 * 48.0 + q2 * -36.0 + q3 * 16.0 + q4 * -3.0) * (sgn / (12.0 * h)); - let d2 = (q0 * 35.0 + q1 * -104.0 + q2 * 114.0 + q3 * -56.0 + q4 * 11.0) * (1.0 / (12.0 * h * h)); - let d3 = (q0 * -5.0 + q1 * 18.0 + q2 * -24.0 + q3 * 14.0 + q4 * -3.0) * (sgn / (2.0 * h * h * h)); - let (t, bend, change) = shape(d1, d2, d3)?; - Ok((q0, t, bend, change)) -} - /// How far the rebuilt surface strays from the original one: the root mean square of the /// distances from a grid of its samples to the original surface. -fn deviation(made: &mt::Face, original: &mt::Face) -> Result { - let (surf, os) = (made.surface(), original.surface()); - let ranges = ranges(made)?; +fn deviation(made: &Patch, original: &Patch) -> Result { + let ranges = made.ranges; let mut sum = 0.0f64; let mut n = 0usize; for i in 0..GRID { for j in 0..GRID { let u = ranges[0].0 + (ranges[0].1 - ranges[0].0) * i as f64 / (GRID - 1) as f64; let v = ranges[1].0 + (ranges[1].1 - ranges[1].0) * j as f64 / (GRID - 1) as f64; - let q = from_p3(surf.subs(u, v)); - let Some((a, b)) = os.search_nearest_parameter(p3(q), None, 100) else { continue }; - let d = from_p3(os.subs(a, b)).dist(q); + let Some(q) = made.surface.point([u, v]) else { continue }; + let Some((_, d)) = original.normal_near(q) else { continue }; if d.is_finite() { sum += d * d; n += 1;