* STEP import: mesh band faces that wrap around a closed surface correctly Fixes two related bugs that caused SolidCraft to fail to mesh a face wrapping all the way around a closed, non-periodic surface (a revolved ridge band, or a tube-shaped B-spline surface): a missing face, a folded/self-overlapping triangulation, or a hole in the rendered body. 1. Ring edges that run slightly off their surface. STEP files write a surface and the curves bounding it to the file's own declared uncertainty, so a ring edge can legitimately sit a few micrometres off the surface it bounds. The code finding where a ring edge crosses the planned split line compared against the edge's raw (off-surface) point using a hardcoded tolerance far stricter than the file's own precision, rejecting a valid cut. Now matches against the point on the surface directly below the ring crossing, respecting the file's declared tolerance. 2. Closed B-spline surfaces, their slits and their slivers. truck only treats the rotation of a revolved surface as periodic; a face on a B-spline surface whose control net closes on itself (a tube) wasn't recognized as wrapping around at all. This produced a folded face, a hole (a boundary loop landing a full period away from where it should cut), and one near-zero-width sliver face costing the entire body its tessellation plan. Adds periodicity detection for closed B-spline surfaces, period- aware hole-loop alignment, dropping of zero-width slit boundaries, and isolating a too-thin-to-triangulate face so it no longer takes the rest of the body down with it. The GPU shader's periodic wrap mirrors the CPU math so the two don't drift apart. Regression tests: band_faces_split_when_ring_edges_run_off_their_surface, band_face_with_a_zero_width_slit_is_still_meshed, a_sliver_face_too_thin_to_triangulate_does_not_cost_the_body_its_plan. Ablation-tested: disabling either mechanism in (2) reproduces its target test's original failure message while leaving the other test unaffected. * STEP import: mesh the band's split faces, not just split them jyviko asked whether a band wall missing from the interactive app's render was a shading issue or a genuinely missing face. It was missing: the ring- edge split fix made the band's topology correct, but plan_face still produced zero triangles for the two new sub-faces. Two causes, both in plan_face (crates/kernel/src/tess/mod.rs): 1. The tolerance for how far an imported boundary may lie off its surface was tied to the requested mesh's chord tolerance instead of to the body's own size. A finer display tolerance could then reject a boundary a coarser one had just accepted, so the face would drop out exactly when zooming in on it. 2. Near a sharp corner of this flat-panelled band, several boundary points that are legitimately a few micrometres apart (within the STEP file's own declared precision) all projected onto the same surface point. The boundary loop then repeated a point in (u, v), which a triangulator can't follow, so the face's plan failed outright. One face without a plan costs the whole body its plan, and the fallback mesher can't handle a closed B-spline surface either, leaving both halves of the split band with no triangles at all: present in the topology, invisible in every renderer, and absent from the body's volume. Fix: scale the off-surface tolerance to the body's own extent (new Rules::extent), and collapse a run of boundary nodes that land on the same surface point to one (drop_collapsed) before triangulating. Regression test additions to band_faces_split_when_ring_edges_run_off_their_surface: the body's volume now matches the analytic pentagon prism (was a third of it - only the top cap - before this fix); display_plan leaves no face without triangles from coarse to fine tolerance; and the actual tolerance and facet angle the viewport draws with gives the split band its full lateral area on both halves. Confirmed on review_band_big.step, the fixture behind the review screenshots: volume's relative error against the analytic pentagon prism drops from 1.4e-4 to 4.1e-6, and the band wall now occludes the floor grid behind it in a snapshot the same way the top and bottom caps always did. Full kernel suite: 739 tests, 0 failed. * STEP import: hold an imported boundary to the file's own tolerance, not the part's size jyviko: the off-surface acceptance added in the last commit (rules.extent * OFF_SURFACE) scaled with the part, so a 2 m part silently accepted a boundary 2 mm off its surface while a 20 mm copy of the same part accepted only 0.02 mm. A STEP file's own declared uncertainty does not grow with the part, so the bound shouldn't either. Body gains boundary_tol: Option<f64>, Some(file's declared tolerance) for a body read from STEP (crate::step_in::Units::tol, already used by the ring-split fix in split.rs), None for anything this kernel built. It's set before the import's own unmeshed-face check runs, and carried over by the two face-split operations that re-cut an imported surface (splitface.rs, outline.rs), since the file's uncertainty still describes those boundaries afterward. plan_face's accept now reads: the file's own tolerance when the body declares one, or the old chord-tolerance/extent bound otherwise. The old bound's tol * 10.0 term had to go too, not just the extent term: at a 2 m part's typical display tolerance that term alone is ~20 mm, nearly ten times the extent bound jyviko objected to, so keeping it would have left the exact 2 m/2 mm example still silently accepted. Two new tests in step_in/tests.rs: - band_faces_split_when_a_scaled_copy_runs_off_its_surface: the same band fixture at a realistic 2 m scale with the same absolute slop and declared uncertainty as the small one, meshes the same way at every chord tolerance from coarse to fine. - off_surface_budget_follows_the_file_not_the_part_size: the actual regression. The same off-surface error is accepted or refused the same way whether the part is 20 mm or 2 m; a 2 mm error against a 1 µm declared uncertainty is refused at both sizes now (accepted at 2 m under the old bound, refused at 20 mm - that asymmetry was the bug). Checked at both a coarse and a fine display tolerance. Known gap, not fixed here: a STEP-imported body saved into a .solidcraft design and reopened loses boundary_tol (native.rs's decoder has no field to read it from) and falls back to the old extent-based bound on reload. Today's design format has no concept of this value at all; persisting it is a real format change, out of scope for this fix. Full kernel suite: 776 tests, 0 failed. clippy -D warnings clean. * Display plans for STEP threads and pointed ends (#3847) Three faces cost a threaded or pointed STEP part its whole display plan, so it was drawn from the reference mesh: cracked, faceted, and never refined up close. - A loop through a pole (a cone's apex on the boundary) seemed to wind around the surface: the jump at the apex, unwrapped to the nearest copy, was arbitrary. The loop is turned to end where it leaves the pole, so the step that closes it is an ordinary one (`seam::close_through_pole`). - Projection onto a spline with hundreds of spans (a helical flank) restarted from a 16 x 16 grid and settled on another turn; it now starts from two points per span, capped. - Sag refinement no longer inserts a point exactly on a boundary side, which split the side and failed the face. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * tess: a refused face's edges are divided finer and it is planned again (#2814) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LGeCa5jWWdroMWvXxN3LLM * tess: sliver faces plan empty; a face no method plans is filled flat from its nodes (#2814) A face whose boundary runs out and back along itself (two edges between the same vertices, a rounding apart, as STEP files write slivers) encloses nothing and is planned with no triangles. A face still refused after its edges are divided finer (a cusp, a sliver whose sides sag across each other, a small cap at a pole) is filled flat from its shared boundary nodes, so the rest of the body keeps its display plan instead of falling back to the reference mesh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LGeCa5jWWdroMWvXxN3LLM * display mesh tests: a rim the chord tolerance leaves a line is divided again (#2814) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LGeCa5jWWdroMWvXxN3LLM * portable B-rep: up to 8192 control points along a spline direction (#2814) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LGeCa5jWWdroMWvXxN3LLM * solidcraft-cli: --debug logs what the engine reports to stderr Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LGeCa5jWWdroMWvXxN3LLM * STEP import: a spline written over a tiny parameter range is read over one (#2814) Some writers parameterize splines over a few millionths (a humanoid robot's edges span 5.4e-6). The kernel tells parameters apart only to 1e-6, so over such a range it evaluated the curve's end wrongly, and trimming an edge to its vertices dropped a control point: the bodies could not be stored exactly and the whole model cache was left out of the design. The knots are now stretched to span one; the curve is the same and edges are trimmed by their vertices, so nothing read from the file refers to the old parameters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LGeCa5jWWdroMWvXxN3LLM * model cache: a body keeps its file's declared accuracy (#2814) A body restored from the cache dropped the accuracy the file it was read from declares, so the planner held its boundaries to the kernel's own bound and a reopened design drew differently from the import. It is stored with the body's runtime values, and only when there is one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LGeCa5jWWdroMWvXxN3LLM * tess: a boundary within a hundredth of its body lies on its surface, at every quality (#2814) #1721 held a body read from a file to the accuracy the file declares. A writer declares what it aims for, not a bound its edges keep: a 251-body robot assembly declaring 0.01 µm has boundaries 10 to 30 µm off their surfaces, and 95 of its bodies lost their display plans. The bound is a hundredth of the body again, what Normal quality always allowed, and now the same at every quality, so zooming in still never drops a face (#1721's point). A file declaring more raises it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LGeCa5jWWdroMWvXxN3LLM * STEP import: keep faces whose edge vertices sit slightly off their curves Some exporters write an edge's vertex beside the end of its curve, further off than the accuracy the file declares (a surface export declares 0.01 mm and puts a vertex 0.19 mm off two spline ends). The reader refused such edges and left the face, and with single-face bodies the body, out. A vertex may now sit up to the file's tolerance or 1/1000 of the model's size (the diagonal around its VERTEX_POINTs), whichever is larger, never more than the existing 1 mm ceiling. Spline and open-conic edges have the end control point moved onto the vertex, so every such edge meets its vertices exactly. The file's declared accuracy still governs closed-curve, seam and circle checks. * tess, io: review clean-ups (#2814) - A sliver face is one whose loops are all slit: drop_slits finds it in the retry after a failed plan, so the pre-check on every face (folded) is gone. - The flat fill uses cdt::triangulate (a lenient fill that leaves out sides crossing one already kept) and its flood-fill inside test, and the plane from geom::Plane. - Refinement only builds its indexes when a face was refused, takes the faces to plan again from edge_faces, and re-samples edges on the pool; Rules::finer for the finer rules. - One PackageWriter::fits for the model cache, the preview and the thumbnail. - The band split's ring tolerance lives in split.rs; comments say what the off-surface bound is now. - The CLI logger prints Level::as_str(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LGeCa5jWWdroMWvXxN3LLM --------- Co-authored-by: David Lackovic <lackovicdavid1234@gmail.com> Co-authored-by: bherbruck <brennen.herbruck@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: jyviko <2501648+jyviko@users.noreply.github.com> Co-authored-by: Ray <raykholo@users.noreply.github.com>
567 lines
23 KiB
Rust
567 lines
23 KiB
Rust
//! Union of bodies touching back to back over part of a planar face, imprinted along the
|
||
//! outline of the contact itself.
|
||
//!
|
||
//! [`crate::coplanar::imprint_union`] splits each body's face along the other's sides, extended
|
||
//! to whole planes and cylinders. Where a side's surface runs on into the contact (the inner
|
||
//! wall of a lip swept round a corner, whose straight part is tangent to its round part), the
|
||
//! two bodies' pieces don't match and the faces can't be glued. Here each face in the plane is
|
||
//! divided only by the other body's edges in the plane, as far as they run across it: both
|
||
//! bodies' faces in the plane are lines and circular arcs, every edge is cut where the other's
|
||
//! edges cross or touch it, the other's edges lying inside a face are copied into it, and the
|
||
//! face is rebuilt as the regions those edges bound. The common regions are then the same faces
|
||
//! in both bodies, and [`crate::coplanar::glue`] takes them away.
|
||
|
||
use std::collections::HashMap;
|
||
use std::f64::consts::TAU;
|
||
|
||
use mt::{BoundedCurve, ParametricCurve};
|
||
use solidcraft_geom::{Vec2, Vec3};
|
||
use truck_modeling as mt;
|
||
|
||
use crate::body::{Body, from_p3};
|
||
use crate::history::History;
|
||
use crate::record::Rebuild;
|
||
use crate::{KernelError, Result, guard, record};
|
||
|
||
/// Most edges of a body's faces in the plane the outline imprint takes on.
|
||
const MAX_EDGES: usize = 2000;
|
||
|
||
/// A frame in the plane, `x × y` its normal.
|
||
#[derive(Clone, Copy)]
|
||
struct Frame {
|
||
o: Vec3,
|
||
x: Vec3,
|
||
y: Vec3,
|
||
}
|
||
|
||
impl Frame {
|
||
/// The plane `n · p = d` (`n` a unit normal).
|
||
fn new(n: Vec3, d: f64) -> Frame {
|
||
let x = n.any_perp();
|
||
Frame { o: n * d, x, y: n.cross(x) }
|
||
}
|
||
|
||
fn normal(&self) -> Vec3 {
|
||
self.x.cross(self.y)
|
||
}
|
||
|
||
/// The same plane seen from its other side.
|
||
fn flipped(&self) -> Frame {
|
||
Frame { o: self.o, x: self.x, y: -self.y }
|
||
}
|
||
|
||
fn local(&self, p: Vec3) -> Vec2 {
|
||
let v = p - self.o;
|
||
Vec2::new(v.dot(self.x), v.dot(self.y))
|
||
}
|
||
|
||
fn world(&self, q: Vec2) -> Vec3 {
|
||
self.o + self.x * q.x + self.y * q.y
|
||
}
|
||
}
|
||
|
||
/// A line or circular arc in the plane, in the direction of the edge it is.
|
||
#[derive(Clone, Copy, Debug)]
|
||
enum Piece {
|
||
Line {
|
||
a: Vec2,
|
||
b: Vec2,
|
||
},
|
||
/// From the angle `t0` about `c`, turning by `sweep` (counter-clockwise when positive).
|
||
Arc {
|
||
c: Vec2,
|
||
r: f64,
|
||
t0: f64,
|
||
sweep: f64,
|
||
},
|
||
}
|
||
|
||
/// A fixed direction for counting crossings, clear of the axes and of round angles.
|
||
const RAY: (f64, f64) = (0.951_056_516_295_153_6, 0.309_016_994_374_947_45);
|
||
|
||
impl Piece {
|
||
fn at(&self, s: f64) -> Vec2 {
|
||
match *self {
|
||
Piece::Line { a, b } => a.lerp(b, s),
|
||
Piece::Arc { c, r, t0, sweep } => c + Vec2::from_angle(t0 + sweep * s) * r,
|
||
}
|
||
}
|
||
|
||
fn len(&self) -> f64 {
|
||
match *self {
|
||
Piece::Line { a, b } => a.dist(b),
|
||
Piece::Arc { r, sweep, .. } => r * sweep.abs(),
|
||
}
|
||
}
|
||
|
||
/// Where the angle `t` lies along the arc (0 at its start, 1 at its end), if it does.
|
||
fn arc_fraction(t0: f64, sweep: f64, t: f64) -> Option<f64> {
|
||
let turned = if sweep >= 0.0 { (t - t0).rem_euclid(TAU) } else { (t0 - t).rem_euclid(TAU) };
|
||
let span = sweep.abs();
|
||
if turned <= span {
|
||
Some(turned / span)
|
||
} else if span >= TAU - 1e-12 {
|
||
Some(0.0)
|
||
} else {
|
||
None
|
||
}
|
||
}
|
||
|
||
/// Distance from `p` to the piece.
|
||
fn dist(&self, p: Vec2) -> f64 {
|
||
match *self {
|
||
Piece::Line { a, b } => {
|
||
let d = b - a;
|
||
let l2 = d.len2();
|
||
let s = if l2 > 0.0 { ((p - a).dot(d) / l2).clamp(0.0, 1.0) } else { 0.0 };
|
||
p.dist(a + d * s)
|
||
}
|
||
Piece::Arc { c, r, t0, sweep } => match Self::arc_fraction(t0, sweep, (p - c).angle()) {
|
||
Some(_) => ((p - c).len() - r).abs(),
|
||
None => p.dist(self.at(0.0)).min(p.dist(self.at(1.0))),
|
||
},
|
||
}
|
||
}
|
||
|
||
/// How many times the ray from `p` along [`RAY`] crosses the piece.
|
||
fn crossings(&self, p: Vec2) -> usize {
|
||
let dir = Vec2::new(RAY.0, RAY.1);
|
||
match *self {
|
||
Piece::Line { a, b } => {
|
||
let e = b - a;
|
||
let den = dir.cross(e);
|
||
if den.abs() < 1e-300 {
|
||
return 0;
|
||
}
|
||
let w = a - p;
|
||
let t = w.cross(e) / den;
|
||
let s = w.cross(dir) / den;
|
||
usize::from(t > 0.0 && (0.0..1.0).contains(&s))
|
||
}
|
||
Piece::Arc { c, r, t0, sweep } => {
|
||
// p + t·dir on the circle: t² + 2t(w·dir) + |w|² − r² = 0, w = p − c.
|
||
let w = p - c;
|
||
let bq = w.dot(dir);
|
||
let disc = bq * bq - (w.len2() - r * r);
|
||
if !(disc > 0.0) {
|
||
return 0;
|
||
}
|
||
let root = disc.sqrt();
|
||
[-bq - root, -bq + root]
|
||
.into_iter()
|
||
.filter(|t| *t > 0.0)
|
||
.filter(|t| Self::arc_fraction(t0, sweep, (p + dir * *t - c).angle()).is_some_and(|f| f < 1.0 || sweep.abs() >= TAU - 1e-12))
|
||
.count()
|
||
}
|
||
}
|
||
}
|
||
|
||
/// The unit direction from the piece's start (`end` false) or end (`end` true) toward a
|
||
/// point a little along it: the direction it leaves or enters its vertex, with its bend.
|
||
fn leaving(&self, end: bool) -> Option<Vec2> {
|
||
let len = self.len();
|
||
let s = if len > 0.0 { (1e-3 / len).min(0.25) } else { 0.25 };
|
||
let (v, q) = if end { (self.at(1.0), self.at(1.0 - s)) } else { (self.at(0.0), self.at(s)) };
|
||
(q - v).normalized()
|
||
}
|
||
}
|
||
|
||
/// The edge `e` (in its own direction) as a line or arc in the frame's plane; `None` when it is
|
||
/// off the plane or neither.
|
||
fn piece_of(e: &mt::Edge, fr: &Frame, tol: f64) -> Option<Piece> {
|
||
let c = e.oriented_curve();
|
||
let (t0, t1) = c.range_tuple();
|
||
let n = fr.normal();
|
||
let pts: Vec<Vec3> = (0..=8).map(|k| from_p3(c.subs(t0 + (t1 - t0) * k as f64 / 8.0))).collect();
|
||
if pts.iter().any(|p| (*p - fr.o).dot(n).abs() > tol) {
|
||
return None;
|
||
}
|
||
let q: Vec<Vec2> = pts.iter().map(|p| fr.local(*p)).collect();
|
||
let (&a, &b, &m) = (q.first()?, q.last()?, q.get(4)?);
|
||
let closed = a.dist(b) <= tol;
|
||
if !closed {
|
||
let line = Piece::Line { a, b };
|
||
if q.iter().all(|p| line.dist(*p) <= tol) {
|
||
return Some(line);
|
||
}
|
||
}
|
||
// The circle through three of the points.
|
||
let (p1, p2, p3) = if closed { (a, *q.get(3)?, *q.get(6)?) } else { (a, m, b) };
|
||
let (u, v) = (p2 - p1, p3 - p1);
|
||
let den = 2.0 * u.cross(v);
|
||
if den.abs() < 1e-300 {
|
||
return None;
|
||
}
|
||
let centre = p1 + Vec2::new(v.y * u.len2() - u.y * v.len2(), u.x * v.len2() - v.x * u.len2()) * (1.0 / den);
|
||
let r = (a - centre).len();
|
||
if !(r > tol) || q.iter().any(|p| ((*p - centre).len() - r).abs() > tol) {
|
||
return None;
|
||
}
|
||
let start = (a - centre).angle();
|
||
let ccw = (a - centre).cross(*q.get(1)? - centre) > 0.0;
|
||
let sweep = if closed {
|
||
if ccw { TAU } else { -TAU }
|
||
} else if ccw {
|
||
((b - centre).angle() - start).rem_euclid(TAU)
|
||
} else {
|
||
-((start - (b - centre).angle()).rem_euclid(TAU))
|
||
};
|
||
// The samples run along it in order (a rational arc is not even in angle).
|
||
let mut last = -1e-9;
|
||
for p in q.iter().skip(1).take(7) {
|
||
let f = Piece::arc_fraction(start, sweep, (*p - centre).angle())?;
|
||
if f < last {
|
||
return None;
|
||
}
|
||
last = f;
|
||
}
|
||
Some(Piece::Arc { c: centre, r, t0: start, sweep })
|
||
}
|
||
|
||
/// Where two pieces cross or touch (each point within `tol` of both).
|
||
fn meetings(p: &Piece, q: &Piece, tol: f64) -> Vec<Vec2> {
|
||
let mut out: Vec<Vec2> = Vec::new();
|
||
let line_circle = |a: Vec2, b: Vec2, c: Vec2, r: f64, out: &mut Vec<Vec2>| {
|
||
let Some(dir) = (b - a).normalized() else { return };
|
||
let foot = a + dir * (c - a).dot(dir);
|
||
let h = (c - foot).len();
|
||
if h > r + tol {
|
||
return;
|
||
}
|
||
if (h - r).abs() <= tol {
|
||
out.push(foot);
|
||
} else {
|
||
let k = (r * r - h * h).max(0.0).sqrt();
|
||
out.extend([foot - dir * k, foot + dir * k]);
|
||
}
|
||
};
|
||
match (*p, *q) {
|
||
(Piece::Line { a, b }, Piece::Line { a: c, b: d }) => {
|
||
let (e, f) = (b - a, d - c);
|
||
let den = e.cross(f);
|
||
if den.abs() > 1e-12 * e.len() * f.len() {
|
||
let s = (c - a).cross(f) / den;
|
||
out.push(a + e * s);
|
||
}
|
||
}
|
||
(Piece::Line { a, b }, Piece::Arc { c, r, .. }) | (Piece::Arc { c, r, .. }, Piece::Line { a, b }) => line_circle(a, b, c, r, &mut out),
|
||
(Piece::Arc { c: c1, r: r1, .. }, Piece::Arc { c: c2, r: r2, .. }) => {
|
||
let d = c1.dist(c2);
|
||
if d > tol && d <= r1 + r2 + tol && d + tol >= (r1 - r2).abs() {
|
||
let a = (r1 * r1 - r2 * r2 + d * d) / (2.0 * d);
|
||
let h = (r1 * r1 - a * a).max(0.0).sqrt();
|
||
let u = (c2 - c1) * (1.0 / d);
|
||
let base = c1 + u * a;
|
||
if h <= tol {
|
||
out.push(base);
|
||
} else {
|
||
out.extend([base + u.perp() * h, base - u.perp() * h]);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
// The ends of either lying on the other (a T, or where overlapping pieces part).
|
||
for (x, y) in [(p, q), (q, p)] {
|
||
for end in [x.at(0.0), x.at(1.0)] {
|
||
if y.dist(end) <= tol {
|
||
out.push(end);
|
||
}
|
||
}
|
||
}
|
||
out.retain(|m| p.dist(*m) <= tol && q.dist(*m) <= tol);
|
||
out
|
||
}
|
||
|
||
/// The edges of `body`'s faces in the plane of `fr` that face along its normal, once each, in
|
||
/// their absolute direction, with their pieces. `None` when one is neither a line nor an arc.
|
||
fn plane_edges(body: &Body, faces: &[usize], fr: &Frame, tol: f64) -> Option<Vec<(mt::Edge, Piece)>> {
|
||
let fs: Vec<&mt::Face> = body.solid.face_iter().collect();
|
||
let mut seen = std::collections::HashSet::new();
|
||
let mut out = Vec::new();
|
||
for f in faces.iter().filter_map(|i| fs.get(*i)) {
|
||
for e in f.edge_iter() {
|
||
if !seen.insert(e.id()) {
|
||
continue;
|
||
}
|
||
let abs = if e.orientation() { e.clone() } else { e.inverse() };
|
||
out.push((abs.clone(), piece_of(&abs, fr, tol)?));
|
||
if out.len() > MAX_EDGES {
|
||
return None;
|
||
}
|
||
}
|
||
}
|
||
Some(out)
|
||
}
|
||
|
||
/// Points where the edges of `own` are to be cut so that every piece of them lies inside,
|
||
/// outside or along the edges of `other` (in world coordinates).
|
||
fn cuts(own: &[(mt::Edge, Piece)], other: &[(mt::Edge, Piece)], fr: &Frame, tol: f64) -> Vec<Vec3> {
|
||
let mut out = Vec::new();
|
||
for (_, p) in own {
|
||
for (_, q) in other {
|
||
for m in meetings(p, q, tol) {
|
||
if m.dist(p.at(0.0)) > tol && m.dist(p.at(1.0)) > tol {
|
||
out.push(fr.world(m));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
out
|
||
}
|
||
|
||
/// One edge use in a face being rebuilt: the edge as the face uses it, its piece in that
|
||
/// direction, its ends, and its reverse use when it is a copy used both ways.
|
||
struct Use {
|
||
edge: mt::Edge,
|
||
piece: Piece,
|
||
from: mt::VertexID,
|
||
to: mt::VertexID,
|
||
twin: Option<usize>,
|
||
}
|
||
|
||
/// Does the point lie inside the region bounded by `pieces` (closed loops, holes included)?
|
||
fn inside(pieces: &[Piece], p: Vec2) -> bool {
|
||
pieces.iter().map(|x| x.crossings(p)).sum::<usize>() % 2 == 1
|
||
}
|
||
|
||
/// The loops of uses, each next use the one leaving the vertex furthest clockwise from the way
|
||
/// the last one came in: each loop runs round one region with it on the left.
|
||
fn loops(uses: &[Use]) -> Option<Vec<Vec<usize>>> {
|
||
let mut out_of: HashMap<mt::VertexID, Vec<usize>> = HashMap::new();
|
||
for (i, u) in uses.iter().enumerate() {
|
||
out_of.entry(u.from).or_default().push(i);
|
||
}
|
||
let mut used = vec![false; uses.len()];
|
||
let mut out = Vec::new();
|
||
for start in 0..uses.len() {
|
||
if used.get(start).copied().unwrap_or(true) {
|
||
continue;
|
||
}
|
||
let mut lp = vec![start];
|
||
if let Some(x) = used.get_mut(start) {
|
||
*x = true;
|
||
}
|
||
let mut cur = start;
|
||
loop {
|
||
let u = uses.get(cur)?;
|
||
let back = u.piece.leaving(true)?;
|
||
let next = out_of
|
||
.get(&u.to)?
|
||
.iter()
|
||
.copied()
|
||
.filter(|k| Some(*k) != u.twin)
|
||
.filter_map(|k| {
|
||
let d = uses.get(k)?.piece.leaving(false)?;
|
||
// Counter-clockwise from the way back, in (0, 2π].
|
||
let turn = back.cross(d).atan2(back.dot(d));
|
||
Some((k, if turn <= 0.0 { turn + TAU } else { turn }))
|
||
})
|
||
.max_by(|x, y| x.1.total_cmp(&y.1))
|
||
.map(|(k, _)| k)?;
|
||
if next == start {
|
||
break;
|
||
}
|
||
if used.get(next).copied().unwrap_or(true) || lp.len() > uses.len() {
|
||
return None;
|
||
}
|
||
if let Some(x) = used.get_mut(next) {
|
||
*x = true;
|
||
}
|
||
lp.push(next);
|
||
cur = next;
|
||
}
|
||
out.push(lp);
|
||
}
|
||
Some(out)
|
||
}
|
||
|
||
/// The signed area a loop bounds (positive counter-clockwise).
|
||
fn area(uses: &[Use], lp: &[usize]) -> f64 {
|
||
let pts: Vec<Vec2> = lp.iter().filter_map(|i| uses.get(*i)).flat_map(|u| (0..16).map(move |k| u.piece.at(k as f64 / 16.0))).collect();
|
||
(0..pts.len()).map(|i| pts.get(i).zip(pts.get((i + 1) % pts.len())).map(|(a, b)| a.cross(*b)).unwrap_or(0.0)).sum::<f64>() * 0.5
|
||
}
|
||
|
||
/// `body` with its faces `faces` (in the plane of `fr`, facing along its normal) divided by those
|
||
/// edges of `other` that run inside them: each such edge is copied into the face, and the face
|
||
/// is rebuilt as the regions its own edges and the copies bound. History over `[body]`. `None`
|
||
/// when a face can't be rebuilt so.
|
||
fn divided(body: &Body, faces: &[usize], other: &[(mt::Edge, Piece)], fr: &Frame, tol: f64) -> Result<Option<Body>> {
|
||
let fs: Vec<&mt::Face> = body.solid.face_iter().collect();
|
||
// Each face's own edge uses, in the direction it runs them (counter-clockwise about the
|
||
// frame's normal round its outside).
|
||
let mut own: Vec<(usize, Vec<(mt::Edge, Piece)>)> = Vec::new();
|
||
for &fi in faces {
|
||
let Some(f) = fs.get(fi) else { continue };
|
||
let mut es = Vec::new();
|
||
for w in f.boundaries() {
|
||
for e in w.edge_iter() {
|
||
let Some(p) = piece_of(e, fr, tol) else { return Ok(None) };
|
||
es.push((e.clone(), p));
|
||
}
|
||
}
|
||
own.push((fi, es));
|
||
}
|
||
let all_own: Vec<Piece> = own.iter().flat_map(|(_, es)| es.iter().map(|(_, p)| *p)).collect();
|
||
// The vertices the copies end on: the faces' own where they meet, else new ones shared.
|
||
let mut pool: Vec<mt::Vertex> = own.iter().flat_map(|(_, es)| es.iter().flat_map(|(e, _)| [e.front().clone(), e.back().clone()])).collect();
|
||
let mut vertex = |p: Vec3| -> mt::Vertex {
|
||
if let Some(v) = pool.iter().find(|v| from_p3(v.point()).dist(p) <= tol) {
|
||
return v.clone();
|
||
}
|
||
let v = mt::Vertex::new(crate::body::p3(p));
|
||
pool.push(v.clone());
|
||
v
|
||
};
|
||
let mut copies: HashMap<usize, Vec<(mt::Edge, Piece)>> = HashMap::new();
|
||
for (e, p) in other {
|
||
// Along the faces' own edges: nothing to add.
|
||
if [0.25, 0.5, 0.75].iter().all(|s| all_own.iter().any(|o| o.dist(p.at(*s)) <= tol * 10.0)) {
|
||
continue;
|
||
}
|
||
let mid = p.at(0.5);
|
||
let Some((fi, _)) = own.iter().find(|(_, es)| inside(&es.iter().map(|(_, x)| *x).collect::<Vec<_>>(), mid)) else { continue };
|
||
let (v0, v1) = (vertex(from_p3(e.front().point())), vertex(from_p3(e.back().point())));
|
||
// On the face's own vertices, which lie within `tol` of the edge's ends.
|
||
let copy = mt::Edge::new_unchecked(&v0, &v1, e.curve());
|
||
let Some(cp) = piece_of(©, fr, tol) else { return Ok(None) };
|
||
copies.entry(*fi).or_default().push((copy, cp));
|
||
}
|
||
if copies.is_empty() {
|
||
return Ok(None);
|
||
}
|
||
let mut rebuilt: HashMap<usize, Vec<mt::Face>> = HashMap::new();
|
||
for (fi, es) in &own {
|
||
let Some(cs) = copies.get(fi) else { continue };
|
||
let Some(f) = fs.get(*fi) else { continue };
|
||
let mut uses: Vec<Use> =
|
||
es.iter().map(|(e, p)| Use { edge: e.clone(), piece: *p, from: e.front().id(), to: e.back().id(), twin: None }).collect();
|
||
for (e, p) in cs {
|
||
let k = uses.len();
|
||
let back = e.inverse();
|
||
let Some(bp) = piece_of(&back, fr, tol) else { return Ok(None) };
|
||
uses.push(Use { edge: e.clone(), piece: *p, from: e.front().id(), to: e.back().id(), twin: Some(k + 1) });
|
||
uses.push(Use { from: back.front().id(), to: back.back().id(), edge: back, piece: bp, twin: Some(k) });
|
||
}
|
||
let Some(lps) = loops(&uses) else { return Ok(None) };
|
||
let (outer, holes): (Vec<(Vec<usize>, f64)>, Vec<(Vec<usize>, f64)>) = lps
|
||
.into_iter()
|
||
.map(|lp| {
|
||
let a = area(&uses, &lp);
|
||
(lp, a)
|
||
})
|
||
.partition(|(_, a)| *a > 0.0);
|
||
let pieces_of = |lp: &[usize]| -> Vec<Piece> { lp.iter().filter_map(|i| uses.get(*i)).map(|u| u.piece).collect() };
|
||
let mut groups: Vec<(Vec<usize>, f64, Vec<Vec<usize>>)> = outer.into_iter().map(|(lp, a)| (lp, a, Vec::new())).collect();
|
||
for (hole, _) in holes {
|
||
let Some(p) = hole.first().and_then(|i| uses.get(*i)).map(|u| u.piece.at(0.5)) else { return Ok(None) };
|
||
let Some(g) = groups.iter_mut().filter(|(lp, _, _)| inside(&pieces_of(lp), p)).min_by(|x, y| x.1.total_cmp(&y.1)) else {
|
||
return Ok(None);
|
||
};
|
||
g.2.push(hole);
|
||
}
|
||
let wire = |lp: &[usize]| -> mt::Wire { lp.iter().filter_map(|i| uses.get(*i)).map(|u| u.edge.clone()).collect::<Vec<_>>().into() };
|
||
let mut made = Vec::new();
|
||
for (lp, _, hs) in &groups {
|
||
let wires: Vec<mt::Wire> = std::iter::once(wire(lp)).chain(hs.iter().map(|h| wire(h))).collect();
|
||
// The loops run round the face as its normal sees them; a face turned against its
|
||
// surface stores them the other way.
|
||
let nf = if f.orientation() {
|
||
mt::Face::try_new(wires, f.surface())
|
||
} else {
|
||
mt::Face::try_new(wires.iter().map(|w| w.inverse()).collect(), f.surface()).map(|mut x| {
|
||
x.invert();
|
||
x
|
||
})
|
||
};
|
||
made.push(nf.map_err(|x| KernelError::Failed(format!("rebuilding a face of the contact: {x}")))?);
|
||
}
|
||
rebuilt.insert(*fi, made);
|
||
}
|
||
let mut rec = Rebuild::default();
|
||
let mut index = 0;
|
||
let mut shells = Vec::new();
|
||
for sh in body.solid.boundaries() {
|
||
let mut faces = Vec::new();
|
||
for f in sh.face_iter() {
|
||
match rebuilt.get(&index) {
|
||
Some(made) => {
|
||
for nf in made {
|
||
rec.whole(nf, f);
|
||
faces.push(nf.clone());
|
||
}
|
||
}
|
||
None => {
|
||
rec.whole(f, f);
|
||
faces.push(f.clone());
|
||
}
|
||
}
|
||
index += 1;
|
||
}
|
||
shells.push(mt::Shell::from(faces));
|
||
}
|
||
let Ok(solid) = mt::Solid::try_new(shells) else { return Ok(None) };
|
||
let split = Body {
|
||
solid: std::sync::Arc::new(solid),
|
||
size: 0.0,
|
||
boundary_tol: body.boundary_tol,
|
||
volume: Some(std::sync::Arc::new(std::sync::Mutex::new(None))),
|
||
coarse_missing: None,
|
||
mesh: None,
|
||
color: body.color,
|
||
paint: None,
|
||
splits: body.splits.clone(),
|
||
face_sources: None,
|
||
history: None,
|
||
regularization_protected: true,
|
||
display_of: None,
|
||
meshes: None,
|
||
phased: false,
|
||
};
|
||
Ok(Some(record::rebuilt_body(split, &[&body.solid], &rec)))
|
||
}
|
||
|
||
/// Union of bodies touching back to back over part of a planar face whose edges there are lines
|
||
/// and circular arcs (a lip swept along a rim, flush with a round side): imprinted on each other
|
||
/// along the outline of the contact and glued. `None` when that does not give a result
|
||
/// `accept`ed.
|
||
pub(crate) fn outline_union(a: &Body, b: &Body, accept: &dyn Fn(&Body) -> bool) -> Option<Body> {
|
||
let size = a.size().max(b.size());
|
||
let tol = (size * 1e-7).max(1e-9) * 10.0;
|
||
let back: Vec<(Vec3, f64)> = crate::coplanar::coincident_planes(a, b).into_iter().filter(|(_, _, same)| !*same).map(|(n, d, _)| (n, d)).collect();
|
||
if back.is_empty() {
|
||
return None;
|
||
}
|
||
let run = || -> Result<Option<Body>> {
|
||
let (mut a2, mut ha): (Body, Option<History>) = (a.clone(), crate::boolean_record::kept(a, 0, 1));
|
||
let (mut b2, mut hb): (Body, Option<History>) = (b.clone(), crate::boolean_record::kept(b, 0, 1));
|
||
for (n, d) in &back {
|
||
// a's faces face along n in the plane; b's the other way.
|
||
let fa = Frame::new(*n, *d);
|
||
let fb = fa.flipped();
|
||
let in_a = |x: &Body| crate::coplanar::faces_in(x, *n, *d, tol);
|
||
let in_b = |x: &Body| crate::coplanar::faces_in(x, -*n, -*d, tol);
|
||
let (Some(ea), Some(eb)) = (plane_edges(&a2, &in_a(&a2), &fa, tol), plane_edges(&b2, &in_b(&b2), &fa, tol)) else { return Ok(None) };
|
||
let a3 = crate::splitface::split_edges_at(&a2, &cuts(&ea, &eb, &fa, tol))?;
|
||
ha = crate::boolean_record::through(&a3, 0, ha.as_ref());
|
||
let b3 = crate::splitface::split_edges_at(&b2, &cuts(&eb, &ea, &fa, tol))?;
|
||
hb = crate::boolean_record::through(&b3, 0, hb.as_ref());
|
||
// Each side's edges as the other side's frame sees them.
|
||
let (Some(eb), Some(ea)) = (plane_edges(&b3, &in_b(&b3), &fa, tol), plane_edges(&a3, &in_a(&a3), &fb, tol)) else { return Ok(None) };
|
||
let a4 = divided(&a3, &in_a(&a3), &eb, &fa, tol)?.unwrap_or(a3);
|
||
let b4 = divided(&b3, &in_b(&b3), &ea, &fb, tol)?.unwrap_or(b3);
|
||
ha = crate::boolean_record::through(&a4, 0, ha.as_ref());
|
||
hb = crate::boolean_record::through(&b4, 0, hb.as_ref());
|
||
(a2, b2) = (a4, b4);
|
||
}
|
||
let Some(r) = crate::coplanar::glue(&a2, &b2) else { return Ok(None) };
|
||
let r = r?;
|
||
let (ha, hb) = (crate::boolean_record::placed(ha, 0, 2), crate::boolean_record::placed(hb, 1, 2));
|
||
let h = crate::boolean_record::of_pair(&r, &ha, &hb);
|
||
Ok(Some(r.with_checked_history(h, &record::counts(&[a, b]))))
|
||
};
|
||
match guard("outline union", run) {
|
||
Ok(Some(r)) if accept(&r) => Some(r),
|
||
_ => None,
|
||
}
|
||
}
|