Files
makeCAD/crates/kernel/src/tess/pieces.rs
T
JIJINBEI fe3b14e798 refactor: Aabb2 and point-built Aabb3 for every hand-rolled box (#2428)
* Iterate instead of indexing in nine loops

Replace index loops that look up neighbours by position with enumerate,
zip(cycle().skip(1)) for wrap-around pairs and zip(skip(1)) for
consecutive pairs. No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* geom: ring_edges for the edges of a closed ring

Every walk over a polygon's edges, the last back to the first, spelled
x.iter().zip(x.iter().cycle().skip(1)); eighteen of them now call
geom::ring_edges. Three redundant take(len) calls go with them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* geom: Ring, a closed ring that knows it is closed

A Ring holds items in order around a closed polygon, loop or fan; edges()
walks each with the next and the last with the first, corners() each
with both neighbours. Ring<Vec2> has signed_area and even-odd contains.

Loop2::polyline returns a Ring, as do doc's face_loops and
boundary_loops, kernel's coplanar wire samples, CapLoop's segments,
tess pieces' rings, the gizmo's plane quads and the navigation cube's
facets. Their walks take edges() or corners() instead of wrapping an
index; sketch's text-hole test and doc's poly_area become contains and
signed_area. Loop2::joints pairs each segment with the next.

Replaces the ring_edges free function.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* geom: Chain, Ring and Path; bounds for a chain of points

Ring becomes Chain<T, Closed> beside Path = Chain<T, Open>: edges()
closes a ring and stops at a path's end, Path::close makes the ring,
and bounds() (the empty box for no points) serves both. Eight
hand-rolled min/max folds over a loop's polyline call bounds(); doc's
flat_polyline returns a Ring.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* geom: area and inside on Ring for any planar point

Ring's signed_area and contains take any Planar point (Vec2 or
[f64; 2]). The cam profile in joints_cam is a Ring: its own even-odd
test goes, and its edge walks, gap and area take edges(). face_params'
loop area, the text path's winding, tess pieces' ring area and the
copies in three tests call signed_area or contains; the formulas are
the same term for term.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* doc: sheet's closed outlines go through Ring

dedup takes and returns a Ring (dropping a last point back on the first
is what makes it a ring's), near_outline walks a Ring's edges, and the
copy of it beside fold checks calls it. point_in goes: its callers ask
the Ring. Its formula anchored each crossing on the other end of the
edge, so the two can differ only for a point on an edge to the last
digit; one caller sets such points aside first, the others test a
region's interior point, a cut's midpoint or another outline's corners.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* doc: cam tests build their outlines with Ring::from

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* geom: area_vector on Ring<Vec3>; three Newell sums use it

Ring<Vec3>::area_vector is half the sum of a × b round the ring. heal's
loop_area, lip's outer-loop turning and the revolve's loop orientation
summed it by hand with a wrapped index; they take it from the Ring
(the half is exact, and two of them only read its sign).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* geom: Aabb3 from points; boxes built and asked through it

Aabb3 collects from points (skipping non-finite ones, as add does) and
gains inflated, contains and meets. Fifteen hand-rolled min/max folds
over points or boxes in kernel, doc and engine become collect or union,
and their "within tol of the box" tests become inflated(tol).contains.
blend's refused-edge boxes and naming's face index hold an Aabb3.

A NaN point is now outside every box, where the old negated
comparisons let it in; an infinite point no longer stretches a box.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* geom: Aabb2, the plane's box; 2D folds and box tuples use it

Aabb2 mirrors Aabb3 (collect, add, union, inflated, contains, meets)
and adds relative, a point's place in the box. sketch's link and
link_loops each had that formula; link_loops' boundaries, prism's
segment and loop boxes, profiles' shape boxes and grid, and ends'
segment box hold an Aabb2 instead of a (low, high) pair. Folds in the
cam, sketch bounds, sheet's perforation grid and the sketch clipboard
collect one.

Boxes are built from the same terms (a round's box keeps c - r - tol).
As with Aabb3, a NaN point is outside every box and an infinite point
no longer stretches one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* kernel: prism's segment boxes are plain Aabb2s

The SegBox alias named a (low, high) pair; Aabb2 says it itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* geom: Chain::bounds is an Aabb2; the boxes #2382 left as pairs follow

Chain::bounds returns an Aabb2. Its callers keep it: sheet's hole and
cut boxes (loop_box), letter boxes, island outlines and line-near-cut
test use meets, contains and inflated; profile's outline overlap and
max_clearance, profiles' box tests and the cam test read its corners.
flat_size and the flat pattern's JSON size are the union of the loops'
boxes (0 × 0 for no loop, where the pair fold gave -∞).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* geom: Aabb3::intersection; hand-grown and hand-met boxes use the box

Aabb3::intersection is the box both hold. ops' four overlap boxes of a
boolean's operands take it, and seven boxes grown by hand (ops' body
boxes, the distance set's bounds, coplanar's face box, eval_more's
padded mesh box, compare's group and region boxes) call inflated with
the same amount on every side.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
2026-10-20 03:44:49 +00:00

339 lines
14 KiB
Rust

//! A trimmed face cut into four-sided patches. The cuts are segments in (u, v): the squares
//! round the face's holes (each joined to its hole at four points, so the ring between them is
//! four collar patches) and the motorcycle traces that extend the squares' sides. Each cut is
//! sampled once and both patches beside it use those nodes (as faces share their edges' nodes),
//! so the patches meet exactly. A cut never adds a node to the face's boundary: it ends on a
//! boundary node, at a point inside the face (a square's corner, or where two traces met
//! head-on), or on another cut (a T-node, which that cut then passes through).
use std::collections::HashMap;
use solidcraft_geom::Ring;
/// Where a cut segment ends.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum End {
/// A boundary node.
Node(u32),
/// A point inside the face; points at the same place are one node.
Point([f64; 2]),
/// A point on cut segment `.0` (a T-node: that segment passes through it).
On(usize, [f64; 2]),
}
/// A cut from `a` to `b`, straight in (u, v).
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct Segment {
pub a: End,
pub b: End,
/// Segments to sample it in (spread over its pieces between T-nodes by length), instead of
/// the seed rule: a butterfly's square side takes the count of the hole arc facing it.
pub count: Option<usize>,
}
/// A patch: its outer ring and any holes inside it, as indices into the boundary nodes followed
/// by the cut nodes, counter-clockwise (holes clockwise) in (u, v).
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Piece {
pub ring: Ring<u32>,
pub holes: Vec<Ring<u32>>,
}
/// Cut the face bounded by `loops` (ranges of `boundary`) along `segments`, measured in the
/// metric `scale`. `seeds(a, b, sa, sb)`: where to put nodes between two nodes of a cut (in
/// (u, v)), as fractions strictly between 0 and 1, given the spacing wanted at each (in the
/// metric): the boundary's own spacing at a boundary node, unbounded inside the face (the seed
/// rule then sets it). Returns the cut nodes (numbered after the boundary) and the patches;
/// `None` when the cuts do not make a planar subdivision (the face is then meshed whole).
pub(crate) fn cut(
boundary: &[[f64; 2]],
loops: &[std::ops::Range<usize>],
segments: &[Segment],
scale: [f64; 2],
seeds: &dyn Fn([f64; 2], [f64; 2], f64, f64) -> Vec<f64>,
) -> Option<(Vec<[f64; 2]>, Vec<Piece>)> {
let n = boundary.len();
let metric = |q: [f64; 2]| [q[0] * scale[0], q[1] * scale[1]];
let dist = |a: [f64; 2], b: [f64; 2]| {
let (a, b) = (metric(a), metric(b));
(a[0] - b[0]).hypot(a[1] - b[1])
};
let mut cut_uv: Vec<[f64; 2]> = Vec::new();
let mut points: HashMap<[u64; 2], u32> = HashMap::new();
// Each segment's end nodes, and the T-nodes other segments put on it.
let mut ends: Vec<(u32, u32)> = Vec::with_capacity(segments.len());
let mut along: Vec<Vec<u32>> = vec![Vec::new(); segments.len()];
for s in segments {
let mut key = |e: End| -> Option<u32> {
let mut point = |q: [f64; 2]| -> Option<u32> {
let bits = [q[0].to_bits(), q[1].to_bits()];
if let Some(&k) = points.get(&bits) {
return Some(k);
}
let k = u32::try_from(n + cut_uv.len()).ok()?;
cut_uv.push(q);
points.insert(bits, k);
Some(k)
};
match e {
End::Node(k) => ((k as usize) < n).then_some(k),
End::Point(q) => point(q),
End::On(on, q) => {
let k = point(q)?;
along.get_mut(on)?.push(k);
Some(k)
}
}
};
let (a, b) = (key(s.a)?, key(s.b)?);
ends.push((a, b));
}
let at_in = |cut_uv: &[[f64; 2]], k: u32| -> Option<[f64; 2]> {
let k = k as usize;
if k < n { boundary.get(k).copied() } else { cut_uv.get(k - n).copied() }
};
// The spacing wanted at a node: at a boundary node the mean of its two boundary segments.
let spacing = |k: u32| -> f64 {
let k = k as usize;
let Some(r) = loops.iter().find(|r| r.contains(&k)) else { return f64::INFINITY };
let m = r.len();
let i = k - r.start;
let (prev, next) = (r.start + (i + m - 1) % m, r.start + (i + 1) % m);
match (boundary.get(prev), boundary.get(k), boundary.get(next)) {
(Some(&p), Some(&q), Some(&x)) => 0.5 * (dist(p, q) + dist(q, x)),
_ => f64::INFINITY,
}
};
// Boundary sides, so a cut that is one of them is left out.
let mut sides: std::collections::HashSet<(u32, u32)> = std::collections::HashSet::new();
for r in loops {
let m = r.len();
for k in 0..m {
let (a, b) = (u32::try_from(r.start + k).ok()?, u32::try_from(r.start + (k + 1) % m).ok()?);
sides.insert((a.min(b), a.max(b)));
}
}
let mut cuts: Vec<(u32, u32)> = Vec::new();
let mut taken: std::collections::HashSet<(u32, u32)> = std::collections::HashSet::new();
for (i, &(s0, s1)) in ends.iter().enumerate() {
if s0 == s1 {
continue;
}
let p0 = at_in(&cut_uv, s0)?;
let mut nodes: Vec<(f64, u32)> = vec![(0.0, s0)];
for &k in along.get(i)? {
nodes.push((dist(p0, at_in(&cut_uv, k)?), k));
}
let total = dist(p0, at_in(&cut_uv, s1)?);
nodes.push((total, s1));
nodes.sort_by(|a, b| a.0.total_cmp(&b.0));
nodes.dedup_by_key(|x| x.1);
let count = segments.get(i).and_then(|s| s.count);
for w in nodes.windows(2) {
let (Some(&(_, a)), Some(&(_, b))) = (w.first(), w.get(1)) else { continue };
if a == b {
continue;
}
// The same cut twice (two segments between the same nodes) is one cut.
let key = (a.min(b), a.max(b));
if sides.contains(&key) || !taken.insert(key) {
continue;
}
let (pa, pb) = (at_in(&cut_uv, a)?, at_in(&cut_uv, b)?);
let fractions: Vec<f64> = match count {
Some(k) if total > 0.0 => {
let share = dist(pa, pb) / total;
let k = ((k as f64 * share).round() as usize).clamp(1, 4096);
(1..k).map(|j| j as f64 / k as f64).collect()
}
_ => seeds(pa, pb, spacing(a), spacing(b)),
};
let mut prev = a;
for f in fractions.into_iter().take(4096) {
let k = u32::try_from(n + cut_uv.len()).ok()?;
cut_uv.push([pa[0] + (pb[0] - pa[0]) * f, pa[1] + (pb[1] - pa[1]) * f]);
cuts.push((prev, k));
prev = k;
}
cuts.push((prev, b));
}
}
if cuts.is_empty() {
return None;
}
let at = |k: u32| at_in(&cut_uv, k);
let pos = |k: u32| at(k).map(metric);
// Half-edges: boundary sides with the face on their left, cut sides both ways.
let inside = |q: [f64; 2]| {
let mut odd = false;
for r in loops {
let m = r.len();
for k in 0..m {
let (Some(a), Some(b)) = (boundary.get(r.start + k).map(|p| metric(*p)), boundary.get(r.start + (k + 1) % m).map(|p| metric(*p)))
else {
continue;
};
if (a[1] > q[1]) != (b[1] > q[1]) && q[0] < a[0] + (q[1] - a[1]) / (b[1] - a[1]) * (b[0] - a[0]) {
odd = !odd;
}
}
}
odd
};
let mut half: Vec<(u32, u32)> = Vec::new();
for r in loops {
let m = r.len();
let side = |k: usize| -> Option<(u32, u32)> { Some((u32::try_from(r.start + k).ok()?, u32::try_from(r.start + (k + 1) % m).ok()?)) };
// Which way round the face lies to the left: test beside the loop's longest side.
let longest = (0..m).max_by(|&x, &y| {
let l = |k: usize| side(k).and_then(|(a, b)| Some(dist(at(a)?, at(b)?))).unwrap_or(0.0);
l(x).total_cmp(&l(y))
})?;
let (a, b) = side(longest)?;
let (pa, pb) = (pos(a)?, pos(b)?);
let len = (pb[0] - pa[0]).hypot(pb[1] - pa[1]);
if !(len > 0.0) {
return None;
}
let left = [0.5 * (pa[0] + pb[0]) - (pb[1] - pa[1]) * 1e-4, 0.5 * (pa[1] + pb[1]) + (pb[0] - pa[0]) * 1e-4];
let forward = inside(left);
for k in 0..m {
let (a, b) = side(k)?;
if a == b {
continue;
}
half.push(if forward { (a, b) } else { (b, a) });
}
}
for &(a, b) in &cuts {
half.push((a, b));
half.push((b, a));
}
// Outgoing half-edges per node, by angle.
let angle = |a: u32, b: u32| -> Option<f64> {
let (p, q) = (pos(a)?, pos(b)?);
Some((q[1] - p[1]).atan2(q[0] - p[0]))
};
let mut out: HashMap<u32, Vec<(f64, usize)>> = HashMap::new();
for (h, &(a, b)) in half.iter().enumerate() {
out.entry(a).or_default().push((angle(a, b)?, h));
}
for v in out.values_mut() {
v.sort_by(|x, y| x.0.total_cmp(&y.0));
}
// The face on the left of u→v continues at v with the first outgoing side clockwise from
// v→u.
let next = |h: usize| -> Option<usize> {
let &(u, v) = half.get(h)?;
let back = angle(v, u)?;
let list = out.get(&v)?;
let before = list
.iter()
.rev()
.find(|(a, e)| *a < back - 1e-12 && half.get(*e).is_some_and(|x| x.1 != u))
.or_else(|| list.iter().rev().find(|(_, e)| half.get(*e).is_some_and(|x| x.1 != u)));
before.map(|(_, e)| *e).or_else(|| list.iter().find(|(_, e)| half.get(*e).is_some_and(|x| x.1 == u)).map(|(_, e)| *e))
};
let mut seen = vec![false; half.len()];
let mut rings: Vec<Ring<u32>> = Vec::new();
for h0 in 0..half.len() {
if *seen.get(h0)? {
continue;
}
let mut ring = Vec::new();
let mut h = h0;
loop {
*seen.get_mut(h)? = true;
ring.push(half.get(h)?.0);
h = next(h)?;
if h == h0 {
break;
}
if ring.len() > half.len() || *seen.get(h)? {
return None;
}
}
rings.push(Ring::new(ring));
}
let area = |ring: &Ring<u32>| -> Option<f64> { Some(ring.iter().map(|&k| pos(k)).collect::<Option<Ring<_>>>()?.signed_area()) };
let mut pieces: Vec<Piece> = Vec::new();
let mut holes: Vec<Ring<u32>> = Vec::new();
for r in rings {
if area(&r)? > 0.0 {
pieces.push(Piece { ring: r, holes: Vec::new() });
} else {
holes.push(r);
}
}
// A loop no cut reached is a hole of the patch around it.
for h in holes {
let p = pos(*h.first()?)?;
let contains = |ring: &Ring<u32>| {
let mut odd = false;
for (&a, &b) in ring.edges() {
let (Some(a), Some(b)) = (pos(a), pos(b)) else { continue };
if (a[1] > p[1]) != (b[1] > p[1]) && p[0] < a[0] + (p[1] - a[1]) / (b[1] - a[1]) * (b[0] - a[0]) {
odd = !odd;
}
}
odd
};
let host = pieces.iter_mut().filter(|pc| contains(&pc.ring)).min_by(|a, b| {
let s = |pc: &Piece| area(&pc.ring).unwrap_or(f64::MAX);
s(a).total_cmp(&s(b))
})?;
host.holes.push(h);
}
Some((cut_uv, pieces))
}
#[cfg(test)]
mod tests {
use super::*;
fn area(all: &[[f64; 2]], ring: &Ring<u32>) -> f64 {
ring.iter().map(|&k| all[k as usize]).collect::<Ring<_>>().signed_area()
}
/// A square cut by four segments meeting at its centre (one point inside the face, shared)
/// gives four quarters covering it.
#[test]
fn a_cross_cuts_a_square_into_four() {
let b = vec![[0.0, 0.0], [1.0, 0.0], [2.0, 0.0], [2.0, 1.0], [2.0, 2.0], [1.0, 2.0], [0.0, 2.0], [0.0, 1.0]];
let loops = [std::ops::Range { start: 0, end: 8 }];
let c = End::Point([1.0, 1.0]);
let segs = [
Segment { a: End::Node(1), b: c, count: None },
Segment { a: End::Node(3), b: c, count: None },
Segment { a: End::Node(5), b: c, count: None },
Segment { a: End::Node(7), b: c, count: None },
];
let Some((cut_uv, pieces)) = cut(&b, &loops, &segs, [1.0, 1.0], &|_, _, _, _| vec![0.5]) else { panic!("cuts") };
assert_eq!(pieces.len(), 4);
let mut all = b.clone();
all.extend(cut_uv);
let total: f64 = pieces.iter().map(|p| area(&all, &p.ring)).sum();
assert!((total - 4.0).abs() < 1e-12, "{total}");
}
/// A segment ending on another (a T-node) splits it there: three patches.
#[test]
fn a_t_node_splits_the_segment_it_lands_on() {
let b = vec![[0.0, 0.0], [1.0, 0.0], [2.0, 0.0], [2.0, 1.0], [2.0, 2.0], [0.0, 2.0], [0.0, 1.0]];
let loops = [std::ops::Range { start: 0, end: 7 }];
let segs = [Segment { a: End::Node(6), b: End::Node(3), count: None }, Segment { a: End::Node(1), b: End::On(0, [1.0, 1.0]), count: None }];
let Some((cut_uv, pieces)) = cut(&b, &loops, &segs, [1.0, 1.0], &|_, _, _, _| Vec::new()) else { panic!("cuts") };
assert_eq!(pieces.len(), 3, "{pieces:?}");
let mut all = b.clone();
all.extend(cut_uv);
let total: f64 = pieces.iter().map(|p| area(&all, &p.ring)).sum();
assert!((total - 4.0).abs() < 1e-12, "{total}");
}
#[test]
fn no_segments_no_cut() {
let b = [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]];
assert!(cut(&b, &[std::ops::Range { start: 0, end: 4 }], &[], [1.0, 1.0], &|_, _, _, _| Vec::new()).is_none());
}
}