Files
makeCAD/crates/geom/src/aabb2.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

105 lines
3.9 KiB
Rust

//! A box in the plane, the 2D [`crate::Aabb3`].
use crate::Vec2;
/// Axis-aligned box in the plane; [`Aabb2::EMPTY`] (low +∞, high −∞) holds nothing.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Aabb2 {
pub min: Vec2,
pub max: Vec2,
}
impl Default for Aabb2 {
fn default() -> Self {
Aabb2::EMPTY
}
}
impl Aabb2 {
pub const EMPTY: Aabb2 = Aabb2 { min: Vec2::new(f64::INFINITY, f64::INFINITY), max: Vec2::new(f64::NEG_INFINITY, f64::NEG_INFINITY) };
pub fn is_empty(&self) -> bool {
!(self.min.x <= self.max.x && self.min.y <= self.max.y)
}
/// Grow to hold `p`; a point that is not finite is skipped.
pub fn add(&mut self, p: Vec2) {
if p.is_finite() {
self.min = Vec2::new(self.min.x.min(p.x), self.min.y.min(p.y));
self.max = Vec2::new(self.max.x.max(p.x), self.max.y.max(p.y));
}
}
pub fn union(&self, o: &Aabb2) -> Aabb2 {
if self.is_empty() {
return *o;
}
if o.is_empty() {
return *self;
}
Aabb2 { min: Vec2::new(self.min.x.min(o.min.x), self.min.y.min(o.min.y)), max: Vec2::new(self.max.x.max(o.max.x), self.max.y.max(o.max.y)) }
}
pub fn center(&self) -> Vec2 {
if self.is_empty() { Vec2::ZERO } else { (self.min + self.max) * 0.5 }
}
pub fn size(&self) -> Vec2 {
if self.is_empty() { Vec2::ZERO } else { self.max - self.min }
}
pub fn diagonal(&self) -> f64 {
self.size().len()
}
/// The box grown by `by` on every side; an empty box stays empty.
pub fn inflated(&self, by: f64) -> Aabb2 {
if self.is_empty() {
return *self;
}
let d = Vec2::new(by, by);
Aabb2 { min: self.min - d, max: self.max + d }
}
/// Whether `p` lies in the box, its sides included (never in an empty box, never for NaN).
pub fn contains(&self, p: Vec2) -> bool {
p.x >= self.min.x && p.y >= self.min.y && p.x <= self.max.x && p.y <= self.max.y
}
/// Whether the two boxes share a point (never when either is empty).
pub fn meets(&self, o: &Aabb2) -> bool {
self.min.x <= o.max.x && o.min.x <= self.max.x && self.min.y <= o.max.y && o.min.y <= self.max.y
}
/// Where `p` sits in the box: (0, 0) at `min`, (1, 1) at `max`, each side taken as at least
/// 1e-9 long so a flat box still divides.
pub fn relative(&self, p: Vec2) -> Vec2 {
Vec2::new((p.x - self.min.x) / (self.max.x - self.min.x).max(1e-9), (p.y - self.min.y) / (self.max.y - self.min.y).max(1e-9))
}
}
/// The box around the finite points (non-finite ones are skipped, as [`Aabb2::add`] does).
impl FromIterator<Vec2> for Aabb2 {
fn from_iter<I: IntoIterator<Item = Vec2>>(points: I) -> Aabb2 {
let mut b = Aabb2::EMPTY;
b.extend(points);
b
}
}
impl Extend<Vec2> for Aabb2 {
fn extend<I: IntoIterator<Item = Vec2>>(&mut self, points: I) {
for p in points {
self.add(p);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_box_from_points_holds_them_and_grows() {
let b: Aabb2 = [Vec2::new(1.0, 2.0), Vec2::new(-1.0, 0.0), Vec2::new(f64::INFINITY, 0.0)].into_iter().collect();
assert_eq!((b.min, b.max), (Vec2::new(-1.0, 0.0), Vec2::new(1.0, 2.0)), "the infinite point is skipped");
assert!(b.contains(Vec2::new(0.0, 1.0)) && !b.contains(Vec2::new(0.0, 3.0)) && !b.contains(Vec2::new(f64::NAN, 1.0)));
assert!(b.inflated(1.0).contains(Vec2::new(0.0, 3.0)));
assert!(Aabb2::EMPTY.inflated(1e9).is_empty() && !Aabb2::EMPTY.contains(Vec2::ZERO));
let far = Aabb2 { min: Vec2::new(2.0, 0.0), max: Vec2::new(3.0, 1.0) };
assert!(!b.meets(&far) && b.inflated(1.0).meets(&far) && !b.meets(&Aabb2::EMPTY));
assert_eq!(b.union(&far).max, Vec2::new(3.0, 2.0));
assert_eq!(b.relative(Vec2::new(0.0, 1.0)), Vec2::new(0.5, 0.5));
}
}