mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 02:38:30 -06:00
style(geom): remove review churn
This commit is contained in:
@@ -24,7 +24,6 @@ impl Default for Bounds2 {
|
||||
impl Bounds2 {
|
||||
pub const EMPTY: Bounds2 = Bounds2 { min: Vec2 { x: f64::INFINITY, y: f64::INFINITY }, max: Vec2 { x: f64::NEG_INFINITY, y: f64::NEG_INFINITY } };
|
||||
|
||||
#[must_use]
|
||||
pub fn new(a: Vec2, b: Vec2) -> Self {
|
||||
Bounds2 { min: a.min(b), max: a.max(b) }
|
||||
}
|
||||
@@ -35,7 +34,6 @@ impl Bounds2 {
|
||||
}
|
||||
b
|
||||
}
|
||||
#[must_use]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
!(self.min.x <= self.max.x && self.min.y <= self.max.y)
|
||||
}
|
||||
@@ -45,7 +43,6 @@ impl Bounds2 {
|
||||
self.max = self.max.max(p);
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn union(&self, o: &Bounds2) -> Bounds2 {
|
||||
if o.is_empty() {
|
||||
return *self;
|
||||
@@ -55,15 +52,12 @@ impl Bounds2 {
|
||||
}
|
||||
Bounds2 { min: self.min.min(o.min), max: self.max.max(o.max) }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn width(&self) -> f64 {
|
||||
if self.is_empty() { 0.0 } else { span(self.min.x, self.max.x) }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn height(&self) -> f64 {
|
||||
if self.is_empty() { 0.0 } else { span(self.min.y, self.max.y) }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn center(&self) -> Vec2 {
|
||||
if self.is_empty() {
|
||||
return Vec2::ZERO;
|
||||
@@ -75,27 +69,22 @@ impl Bounds2 {
|
||||
};
|
||||
Vec2::new(mid(self.min.x, self.max.x), mid(self.min.y, self.max.y))
|
||||
}
|
||||
#[must_use]
|
||||
pub fn contains(&self, p: Vec2) -> bool {
|
||||
p.x >= self.min.x && p.x <= self.max.x && p.y >= self.min.y && p.y <= self.max.y
|
||||
}
|
||||
/// True when `o` lies entirely inside.
|
||||
#[must_use]
|
||||
pub fn contains_box(&self, o: &Bounds2) -> bool {
|
||||
!o.is_empty() && self.contains(o.min) && self.contains(o.max)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn intersects(&self, o: &Bounds2) -> bool {
|
||||
!(self.is_empty() || o.is_empty() || o.min.x > self.max.x || o.max.x < self.min.x || o.min.y > self.max.y || o.max.y < self.min.y)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn expand(&self, d: f64) -> Bounds2 {
|
||||
if self.is_empty() {
|
||||
return *self;
|
||||
}
|
||||
Bounds2 { min: self.min - Vec2::new(d, d), max: self.max + Vec2::new(d, d) }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn corners(&self) -> [Vec2; 4] {
|
||||
[self.min, Vec2::new(self.max.x, self.min.y), self.max, Vec2::new(self.min.x, self.max.y)]
|
||||
}
|
||||
|
||||
@@ -10,53 +10,42 @@ pub struct Line {
|
||||
}
|
||||
|
||||
impl Line {
|
||||
#[must_use]
|
||||
pub fn new(a: Vec2, b: Vec2) -> Self {
|
||||
Line { a, b }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn len(&self) -> f64 {
|
||||
self.a.dist(self.b)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn dir(&self) -> Vec2 {
|
||||
(self.b - self.a).normalized()
|
||||
}
|
||||
#[must_use]
|
||||
pub fn mid(&self) -> Vec2 {
|
||||
self.a.mid(self.b)
|
||||
}
|
||||
/// Parameter (0..1 on the segment) of the projection of `p`.
|
||||
#[must_use]
|
||||
pub fn param_of(&self, p: Vec2) -> f64 {
|
||||
let d = self.b - self.a;
|
||||
let l2 = d.len2();
|
||||
if l2 <= EPS * EPS { 0.0 } else { (p - self.a).dot(d) / l2 }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn at(&self, t: f64) -> Vec2 {
|
||||
self.a.lerp(self.b, t)
|
||||
}
|
||||
/// Closest point on the segment.
|
||||
#[must_use]
|
||||
pub fn closest(&self, p: Vec2) -> Vec2 {
|
||||
self.at(self.param_of(p).clamp(0.0, 1.0))
|
||||
}
|
||||
/// Foot of the perpendicular on the infinite line.
|
||||
#[must_use]
|
||||
pub fn project(&self, p: Vec2) -> Vec2 {
|
||||
self.at(self.param_of(p))
|
||||
}
|
||||
#[must_use]
|
||||
pub fn dist(&self, p: Vec2) -> f64 {
|
||||
self.closest(p).dist(p)
|
||||
}
|
||||
/// Signed side: > 0 left of a→b.
|
||||
#[must_use]
|
||||
pub fn side(&self, p: Vec2) -> f64 {
|
||||
(self.b - self.a).cross(p - self.a)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn offset(&self, d: f64) -> Line {
|
||||
let n = (self.b - self.a).normalized().perp() * d;
|
||||
Line::new(self.a + n, self.b + n)
|
||||
@@ -73,41 +62,32 @@ pub struct Arc {
|
||||
}
|
||||
|
||||
impl Arc {
|
||||
#[must_use]
|
||||
pub fn new(center: Vec2, radius: f64, start: f64, end: f64) -> Self {
|
||||
Arc { center, radius: radius.abs(), start: norm_angle(start), end: norm_angle(end) }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn sweep(&self) -> f64 {
|
||||
ccw_sweep(self.start, self.end)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn start_point(&self) -> Vec2 {
|
||||
Vec2::polar(self.center, self.radius, self.start)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn end_point(&self) -> Vec2 {
|
||||
Vec2::polar(self.center, self.radius, self.end)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn mid_point(&self) -> Vec2 {
|
||||
Vec2::polar(self.center, self.radius, self.start + self.sweep() / 2.0)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn len(&self) -> f64 {
|
||||
self.radius * self.sweep()
|
||||
}
|
||||
#[must_use]
|
||||
pub fn contains_angle(&self, a: f64) -> bool {
|
||||
angle_in_sweep(a, self.start, self.end)
|
||||
}
|
||||
/// Point at parameter `t` in 0..1 along the sweep.
|
||||
#[must_use]
|
||||
pub fn at(&self, t: f64) -> Vec2 {
|
||||
Vec2::polar(self.center, self.radius, self.start + self.sweep() * t)
|
||||
}
|
||||
/// Arc through three points, or `None` when collinear.
|
||||
#[must_use]
|
||||
pub fn from_3_points(p1: Vec2, p2: Vec2, p3: Vec2) -> Option<Arc> {
|
||||
let c = Circle::from_3_points(p1, p2, p3)?;
|
||||
let a1 = c.center.angle_to(p1);
|
||||
@@ -117,11 +97,9 @@ impl Arc {
|
||||
if angle_in_sweep(a2, a1, a3) { Some(Arc::new(c.center, c.radius, a1, a3)) } else { Some(Arc::new(c.center, c.radius, a3, a1)) }
|
||||
}
|
||||
/// Arc from start point, center and end point (CCW).
|
||||
#[must_use]
|
||||
pub fn from_start_center_end(s: Vec2, c: Vec2, e: Vec2) -> Arc {
|
||||
Arc::new(c, s.dist(c), c.angle_to(s), c.angle_to(e))
|
||||
}
|
||||
#[must_use]
|
||||
pub fn closest(&self, p: Vec2) -> Vec2 {
|
||||
let a = self.center.angle_to(p);
|
||||
if self.contains_angle(a) && p.dist(self.center) > EPS {
|
||||
@@ -132,7 +110,6 @@ impl Arc {
|
||||
if s.dist(p) <= e.dist(p) { s } else { e }
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn bounds(&self) -> Bounds2 {
|
||||
let mut b = Bounds2::from_points([self.start_point(), self.end_point()]);
|
||||
for k in 0..4 {
|
||||
@@ -161,11 +138,9 @@ pub struct Circle {
|
||||
}
|
||||
|
||||
impl Circle {
|
||||
#[must_use]
|
||||
pub fn new(center: Vec2, radius: f64) -> Self {
|
||||
Circle { center, radius: radius.abs() }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn from_3_points(p1: Vec2, p2: Vec2, p3: Vec2) -> Option<Circle> {
|
||||
// Work in point-relative coordinates. Absolute coordinate squares lose the small
|
||||
// differences between points in drawings far from the origin.
|
||||
@@ -191,16 +166,13 @@ impl Circle {
|
||||
}
|
||||
Some(Circle::new(c, radius))
|
||||
}
|
||||
#[must_use]
|
||||
pub fn from_2_points(p1: Vec2, p2: Vec2) -> Circle {
|
||||
Circle::new(p1.mid(p2), p1.dist(p2) / 2.0)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn closest(&self, p: Vec2) -> Vec2 {
|
||||
let d = p - self.center;
|
||||
if d.len() < EPS { self.center + Vec2::X * self.radius } else { self.center + d.normalized() * self.radius }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn bounds(&self) -> Bounds2 {
|
||||
Bounds2::new(self.center - Vec2::new(self.radius, self.radius), self.center + Vec2::new(self.radius, self.radius))
|
||||
}
|
||||
@@ -208,7 +180,6 @@ impl Circle {
|
||||
Arc { center: self.center, radius: self.radius, start: 0.0, end: TAU }.tessellate(tol, out);
|
||||
}
|
||||
/// Tangent points from external point `p`.
|
||||
#[must_use]
|
||||
pub fn tangent_points(&self, p: Vec2) -> Vec<Vec2> {
|
||||
let d = p.dist(self.center);
|
||||
if d <= self.radius + EPS {
|
||||
@@ -222,7 +193,6 @@ impl Circle {
|
||||
/// up to two outer (belt) tangents and two inner (crossed) tangents. Empty for concentric
|
||||
/// circles or when one circle lies inside the other; tangents that touch both circles at the
|
||||
/// same point (circles touching each other) are left out.
|
||||
#[must_use]
|
||||
#[expect(clippy::many_single_char_names, reason = "Circle-tangent equations use conventional r/v/d/u/c/h/n/p/q notation.")]
|
||||
pub fn common_tangents(&self, other: &Circle) -> Vec<(Vec2, Vec2)> {
|
||||
let (r1, r2) = (self.radius, other.radius);
|
||||
@@ -264,23 +234,18 @@ pub struct Ellipse {
|
||||
}
|
||||
|
||||
impl Ellipse {
|
||||
#[must_use]
|
||||
pub fn full(center: Vec2, major: Vec2, ratio: f64) -> Self {
|
||||
Ellipse { center, major, ratio, start: 0.0, end: TAU }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn is_full(&self) -> bool {
|
||||
(ccw_sweep(self.start, self.end) - TAU).abs() < 1e-9
|
||||
}
|
||||
#[must_use]
|
||||
pub fn minor(&self) -> Vec2 {
|
||||
self.major.perp() * self.ratio
|
||||
}
|
||||
#[must_use]
|
||||
pub fn at_param(&self, t: f64) -> Vec2 {
|
||||
self.center + self.major * t.cos() + self.minor() * t.sin()
|
||||
}
|
||||
#[must_use]
|
||||
pub fn sweep(&self) -> f64 {
|
||||
ccw_sweep(self.start, self.end)
|
||||
}
|
||||
@@ -310,7 +275,6 @@ impl Ellipse {
|
||||
out.push(self.at_param(self.start + sweep * i as f64 / n as f64));
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn bounds(&self) -> Bounds2 {
|
||||
// Sampled bounds can cut off the true extrema, especially on a small arc or
|
||||
// a large drawing. The derivative of each coordinate gives two exact
|
||||
@@ -325,7 +289,6 @@ impl Ellipse {
|
||||
bounds
|
||||
}
|
||||
/// Parameter of the point on the ellipse nearest the direction of `p` (approximate).
|
||||
#[must_use]
|
||||
pub fn param_of(&self, p: Vec2) -> f64 {
|
||||
let ma = self.major.len();
|
||||
if ma < EPS {
|
||||
@@ -338,7 +301,6 @@ impl Ellipse {
|
||||
let mb = ma * self.ratio;
|
||||
norm_angle((y / mb.max(EPS)).atan2(x / ma))
|
||||
}
|
||||
#[must_use]
|
||||
pub fn closest(&self, p: Vec2) -> Vec2 {
|
||||
let mut pts = Vec::new();
|
||||
self.tessellate(self.major.len() * 1e-3, &mut pts);
|
||||
@@ -373,7 +335,6 @@ pub enum Segment {
|
||||
}
|
||||
|
||||
impl Segment {
|
||||
#[must_use]
|
||||
pub fn start(&self) -> Vec2 {
|
||||
match self {
|
||||
Segment::Line(l) => l.a,
|
||||
@@ -381,7 +342,6 @@ impl Segment {
|
||||
Segment::Arc { arc, ccw: false } => arc.end_point(),
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn end(&self) -> Vec2 {
|
||||
match self {
|
||||
Segment::Line(l) => l.b,
|
||||
@@ -389,28 +349,24 @@ impl Segment {
|
||||
Segment::Arc { arc, ccw: false } => arc.start_point(),
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn len(&self) -> f64 {
|
||||
match self {
|
||||
Segment::Line(l) => l.len(),
|
||||
Segment::Arc { arc, .. } => arc.len(),
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn mid(&self) -> Vec2 {
|
||||
match self {
|
||||
Segment::Line(l) => l.mid(),
|
||||
Segment::Arc { arc, .. } => arc.mid_point(),
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn closest(&self, p: Vec2) -> Vec2 {
|
||||
match self {
|
||||
Segment::Line(l) => l.closest(p),
|
||||
Segment::Arc { arc, .. } => arc.closest(p),
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn bounds(&self) -> Bounds2 {
|
||||
match self {
|
||||
Segment::Line(l) => Bounds2::new(l.a, l.b),
|
||||
@@ -434,7 +390,6 @@ impl Segment {
|
||||
}
|
||||
}
|
||||
/// Point at parameter `t` (0..1) in segment direction.
|
||||
#[must_use]
|
||||
pub fn at(&self, t: f64) -> Vec2 {
|
||||
match self {
|
||||
Segment::Line(l) => l.at(t),
|
||||
@@ -442,7 +397,6 @@ impl Segment {
|
||||
Segment::Arc { arc, ccw: false } => arc.at(1.0 - t),
|
||||
}
|
||||
}
|
||||
#[must_use]
|
||||
pub fn reversed(&self) -> Segment {
|
||||
match *self {
|
||||
Segment::Line(l) => Segment::Line(Line::new(l.b, l.a)),
|
||||
@@ -450,7 +404,6 @@ impl Segment {
|
||||
}
|
||||
}
|
||||
/// Tangent direction (unit) at parameter t.
|
||||
#[must_use]
|
||||
pub fn tangent(&self, t: f64) -> Vec2 {
|
||||
match self {
|
||||
Segment::Line(l) => l.dir(),
|
||||
@@ -462,7 +415,6 @@ impl Segment {
|
||||
}
|
||||
}
|
||||
/// Offset by `d` to the left of travel direction. `None` if an arc collapses.
|
||||
#[must_use]
|
||||
pub fn offset(&self, d: f64) -> Option<Segment> {
|
||||
match *self {
|
||||
Segment::Line(l) => Some(Segment::Line(l.offset(d))),
|
||||
@@ -491,18 +443,15 @@ pub struct PolyVertex {
|
||||
}
|
||||
|
||||
impl PolyVertex {
|
||||
#[must_use]
|
||||
pub fn new(p: Vec2) -> Self {
|
||||
PolyVertex { p, ..Default::default() }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn with_bulge(p: Vec2, bulge: f64) -> Self {
|
||||
PolyVertex { p, bulge, ..Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a bulge segment to an arc. `None` for a straight (zero-bulge or degenerate) segment.
|
||||
#[must_use]
|
||||
pub fn bulge_to_arc(a: Vec2, b: Vec2, bulge: f64) -> Option<(Arc, bool)> {
|
||||
if bulge.abs() < 1e-12 || !bulge.is_finite() || a.near(b, EPS) {
|
||||
return None;
|
||||
@@ -527,7 +476,6 @@ pub fn bulge_to_arc(a: Vec2, b: Vec2, bulge: f64) -> Option<(Arc, bool)> {
|
||||
}
|
||||
|
||||
/// Bulge for an arc traversed from `a` to `b` with the given sweep (signed: + = CCW).
|
||||
#[must_use]
|
||||
pub fn arc_to_bulge(signed_sweep: f64) -> f64 {
|
||||
(signed_sweep / 4.0).tan()
|
||||
}
|
||||
@@ -543,12 +491,10 @@ pub struct Polyline {
|
||||
pub const MAX_TESSELLATED_POINTS: usize = 4_000_000;
|
||||
|
||||
impl Polyline {
|
||||
#[must_use]
|
||||
pub fn from_points(pts: &[Vec2], closed: bool) -> Self {
|
||||
Polyline { vertices: pts.iter().map(|p| PolyVertex::new(*p)).collect(), closed }
|
||||
}
|
||||
/// The segments, including the closing segment when closed.
|
||||
#[must_use]
|
||||
pub fn segments(&self) -> Vec<Segment> {
|
||||
let n = self.vertices.len();
|
||||
let mut out = Vec::with_capacity(n);
|
||||
@@ -568,7 +514,6 @@ impl Polyline {
|
||||
pub fn len(&self) -> f64 {
|
||||
self.segments().iter().map(Segment::len).sum()
|
||||
}
|
||||
#[must_use]
|
||||
pub fn bounds(&self) -> Bounds2 {
|
||||
let mut b = Bounds2::from_points(self.vertices.iter().map(|v| v.p));
|
||||
for s in self.segments() {
|
||||
@@ -576,7 +521,6 @@ impl Polyline {
|
||||
}
|
||||
b
|
||||
}
|
||||
#[must_use]
|
||||
pub fn tessellate(&self, tol: f64) -> Vec<Vec2> {
|
||||
let mut budget = MAX_TESSELLATED_POINTS;
|
||||
self.tessellate_within(tol, &mut budget)
|
||||
@@ -605,7 +549,6 @@ impl Polyline {
|
||||
out
|
||||
}
|
||||
/// Signed area (CCW positive) of a closed polyline including bulge areas.
|
||||
#[must_use]
|
||||
pub fn area(&self) -> f64 {
|
||||
// Integrate the chords exactly and add the signed circular-segment area
|
||||
// for every bulge. A tessellated shoelace loses precision on large arcs.
|
||||
@@ -633,7 +576,6 @@ impl Polyline {
|
||||
}
|
||||
twice_area / 2.0
|
||||
}
|
||||
#[must_use]
|
||||
pub fn closest(&self, p: Vec2) -> Option<Vec2> {
|
||||
let mut best: Option<(f64, Vec2)> = None;
|
||||
for s in self.segments() {
|
||||
@@ -646,7 +588,6 @@ impl Polyline {
|
||||
best.map(|(_, c)| c).or_else(|| self.vertices.first().map(|v| v.p))
|
||||
}
|
||||
/// True if `p` is inside a closed polyline (even-odd on the tessellation).
|
||||
#[must_use]
|
||||
pub fn contains(&self, p: Vec2) -> bool {
|
||||
point_in_polygon(&self.tessellate(1e-3), p)
|
||||
}
|
||||
@@ -685,13 +626,11 @@ impl Polyline {
|
||||
}
|
||||
|
||||
/// Area centroid of a closed polygon. `None` at zero area.
|
||||
#[must_use]
|
||||
pub fn polygon_centroid(pts: &[Vec2]) -> Option<Vec2> {
|
||||
Polyline::from_points(pts, true).centroid()
|
||||
}
|
||||
|
||||
/// Signed polygon area (CCW positive).
|
||||
#[must_use]
|
||||
pub fn shoelace(pts: &[Vec2]) -> f64 {
|
||||
let n = pts.len();
|
||||
if n < 3 {
|
||||
@@ -712,7 +651,6 @@ pub fn shoelace(pts: &[Vec2]) -> f64 {
|
||||
}
|
||||
|
||||
/// Even-odd point-in-polygon test.
|
||||
#[must_use]
|
||||
pub fn point_in_polygon(poly: &[Vec2], p: Vec2) -> bool {
|
||||
let n = poly.len();
|
||||
if n < 3 {
|
||||
|
||||
@@ -49,7 +49,6 @@ struct Range {
|
||||
|
||||
impl TanCurve {
|
||||
/// Point at parameter `t`.
|
||||
#[must_use]
|
||||
pub fn point(&self, t: f64) -> Vec2 {
|
||||
match self {
|
||||
TanCurve::Line(l) => l.a + (l.b - l.a) * t,
|
||||
@@ -60,7 +59,6 @@ impl TanCurve {
|
||||
}
|
||||
|
||||
/// First derivative at parameter `t`.
|
||||
#[must_use]
|
||||
pub fn derivative(&self, t: f64) -> Vec2 {
|
||||
match self {
|
||||
TanCurve::Line(l) => l.b - l.a,
|
||||
@@ -240,7 +238,6 @@ fn refine(c1: &TanCurve, r1: &Range, d1: f64, c2: &TanCurve, r2: &Range, d2: f64
|
||||
/// Circles of radius `r` tangent to both curves (`r = 0`: the points where they cross). `near`
|
||||
/// (the pick points) widens the search window for lines. Every distinct solution is returned;
|
||||
/// callers choose one, usually the nearest the picks.
|
||||
#[must_use]
|
||||
pub fn tangent_circles(c1: &TanCurve, c2: &TanCurve, r: f64, near: &[Vec2]) -> Vec<CornerFit> {
|
||||
if !(r.is_finite() && r >= 0.0) {
|
||||
return Vec::new();
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::{Arc, Circle, EPS, Line, Segment, Vec2};
|
||||
|
||||
/// Intersection of two infinite lines through (a1,a2) and (b1,b2), with parameters on each.
|
||||
#[must_use]
|
||||
pub fn line_line_infinite(a1: Vec2, a2: Vec2, b1: Vec2, b2: Vec2) -> Option<(Vec2, f64, f64)> {
|
||||
let r = a2 - a1;
|
||||
let s = b2 - b1;
|
||||
@@ -15,7 +14,6 @@ pub fn line_line_infinite(a1: Vec2, a2: Vec2, b1: Vec2, b2: Vec2) -> Option<(Vec
|
||||
}
|
||||
|
||||
/// Intersection of two finite segments.
|
||||
#[must_use]
|
||||
#[expect(clippy::many_single_char_names, reason = "Segment endpoints and parameters use conventional a/b/p/t/u geometric notation.")]
|
||||
pub fn line_line(a: &Line, b: &Line) -> Option<Vec2> {
|
||||
let (p, t, u) = line_line_infinite(a.a, a.b, b.a, b.b)?;
|
||||
@@ -24,7 +22,6 @@ pub fn line_line(a: &Line, b: &Line) -> Option<Vec2> {
|
||||
}
|
||||
|
||||
/// Intersections of the infinite line through `l` with a circle. Returns points with line params.
|
||||
#[must_use]
|
||||
#[expect(clippy::many_single_char_names, reason = "The line/circle quadratic uses conventional a/d/f/t mathematical notation.")]
|
||||
pub fn line_circle(l: &Line, c: &Circle) -> Vec<(Vec2, f64)> {
|
||||
let d = l.b - l.a;
|
||||
@@ -57,7 +54,6 @@ pub fn line_circle(l: &Line, c: &Circle) -> Vec<(Vec2, f64)> {
|
||||
}
|
||||
|
||||
/// Intersections of two full circles.
|
||||
#[must_use]
|
||||
pub fn circle_circle(c1: &Circle, c2: &Circle) -> Vec<Vec2> {
|
||||
let delta = c2.center - c1.center;
|
||||
let d = delta.len();
|
||||
@@ -107,14 +103,12 @@ fn on_arc(a: &Arc, p: Vec2) -> bool {
|
||||
}
|
||||
|
||||
/// Intersections between two segments (finite).
|
||||
#[must_use]
|
||||
pub fn intersect_segments(s1: &Segment, s2: &Segment) -> Vec<Vec2> {
|
||||
intersect_segments_ext(s1, s2, false)
|
||||
}
|
||||
|
||||
/// Intersections; with `extend` true, lines are infinite and arcs are full circles
|
||||
/// (used by TRIM/EXTEND edge mode and apparent intersections).
|
||||
#[must_use]
|
||||
pub fn intersect_segments_ext(s1: &Segment, s2: &Segment, extend: bool) -> Vec<Vec2> {
|
||||
let tol = 1e-9;
|
||||
match (s1, s2) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! `CADCraft` geometry: double-precision 2D/3D math for drafting.
|
||||
//! CADCraft geometry: double-precision 2D/3D math for drafting.
|
||||
//!
|
||||
//! Everything a CAD kernel needs below the document model: vectors and affine transforms,
|
||||
//! bounding boxes, angles, lines, circular arcs (and polyline bulges), ellipses, NURBS splines,
|
||||
@@ -36,7 +36,6 @@ pub const TAU: f64 = std::f64::consts::TAU;
|
||||
pub const PI: f64 = std::f64::consts::PI;
|
||||
|
||||
/// Normalize an angle to `[0, 2π)`.
|
||||
#[must_use]
|
||||
pub fn norm_angle(a: f64) -> f64 {
|
||||
if !a.is_finite() {
|
||||
return 0.0;
|
||||
@@ -46,31 +45,26 @@ pub fn norm_angle(a: f64) -> f64 {
|
||||
}
|
||||
|
||||
/// Counter-clockwise sweep from `start` to `end`, in `(0, 2π]`.
|
||||
#[must_use]
|
||||
pub fn ccw_sweep(start: f64, end: f64) -> f64 {
|
||||
let s = norm_angle(end - start);
|
||||
if s <= EPS { TAU } else { s }
|
||||
}
|
||||
|
||||
/// True when angle `a` lies on the CCW sweep from `start` to `end` (inclusive, with tolerance).
|
||||
#[must_use]
|
||||
pub fn angle_in_sweep(a: f64, start: f64, end: f64) -> bool {
|
||||
let sweep = ccw_sweep(start, end);
|
||||
let d = norm_angle(a - start);
|
||||
d <= sweep + 1e-12 || (TAU - d) < 1e-12
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn deg(r: f64) -> f64 {
|
||||
r.to_degrees()
|
||||
}
|
||||
#[must_use]
|
||||
pub fn rad(d: f64) -> f64 {
|
||||
d.to_radians()
|
||||
}
|
||||
|
||||
/// Number of segments needed to approximate an arc of `radius` and `sweep` within `tol`.
|
||||
#[must_use]
|
||||
#[expect(
|
||||
clippy::cast_possible_truncation,
|
||||
clippy::cast_sign_loss,
|
||||
|
||||
@@ -22,30 +22,24 @@ impl Default for Mat3 {
|
||||
impl Mat3 {
|
||||
pub const IDENTITY: Mat3 = Mat3 { a: 1.0, b: 0.0, c: 0.0, d: 1.0, e: 0.0, f: 0.0 };
|
||||
|
||||
#[must_use]
|
||||
pub fn translate(v: Vec2) -> Mat3 {
|
||||
Mat3 { e: v.x, f: v.y, ..Mat3::IDENTITY }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn scale(sx: f64, sy: f64) -> Mat3 {
|
||||
Mat3 { a: sx, d: sy, ..Mat3::IDENTITY }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn rotate(angle: f64) -> Mat3 {
|
||||
let (s, c) = angle.sin_cos();
|
||||
Mat3 { a: c, b: s, c: -s, d: c, e: 0.0, f: 0.0 }
|
||||
}
|
||||
/// Scale about `base`.
|
||||
#[must_use]
|
||||
pub fn scale_about(base: Vec2, s: f64) -> Mat3 {
|
||||
Mat3::translate(base).then_before(Mat3::scale(s, s)).then_before(Mat3::translate(-base))
|
||||
}
|
||||
#[must_use]
|
||||
pub fn rotate_about(base: Vec2, angle: f64) -> Mat3 {
|
||||
Mat3::translate(base).then_before(Mat3::rotate(angle)).then_before(Mat3::translate(-base))
|
||||
}
|
||||
/// Reflection across the line through `p1`, `p2`.
|
||||
#[must_use]
|
||||
pub fn mirror(p1: Vec2, p2: Vec2) -> Mat3 {
|
||||
let d = (p2 - p1).normalized();
|
||||
if d == Vec2::ZERO {
|
||||
@@ -56,7 +50,6 @@ impl Mat3 {
|
||||
Mat3::translate(p1).then_before(r).then_before(Mat3::translate(-p1))
|
||||
}
|
||||
/// `self * o`: apply `o` first, then `self`.
|
||||
#[must_use]
|
||||
pub fn then_before(self, o: Mat3) -> Mat3 {
|
||||
Mat3 {
|
||||
a: self.a * o.a + self.c * o.b,
|
||||
@@ -68,23 +61,18 @@ impl Mat3 {
|
||||
}
|
||||
}
|
||||
/// Apply `self` first, then `o`.
|
||||
#[must_use]
|
||||
pub fn then(self, o: Mat3) -> Mat3 {
|
||||
o.then_before(self)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn apply(&self, p: Vec2) -> Vec2 {
|
||||
Vec2::new(self.a * p.x + self.c * p.y + self.e, self.b * p.x + self.d * p.y + self.f)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn apply_vec(&self, v: Vec2) -> Vec2 {
|
||||
Vec2::new(self.a * v.x + self.c * v.y, self.b * v.x + self.d * v.y)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn det(&self) -> f64 {
|
||||
self.a * self.d - self.b * self.c
|
||||
}
|
||||
#[must_use]
|
||||
#[expect(clippy::many_single_char_names, reason = "Matrix coefficients use the same a–f notation as Mat3 and the affine-transform formula.")]
|
||||
pub fn inverse(&self) -> Option<Mat3> {
|
||||
let det = self.det();
|
||||
@@ -101,16 +89,13 @@ impl Mat3 {
|
||||
Some(Mat3 { a, b, c, d, e, f })
|
||||
}
|
||||
/// Uniform scale factor (geometric mean of the axis scales).
|
||||
#[must_use]
|
||||
pub fn scale_factor(&self) -> f64 {
|
||||
self.det().abs().sqrt()
|
||||
}
|
||||
/// Rotation angle of the transformed X axis.
|
||||
#[must_use]
|
||||
pub fn rotation(&self) -> f64 {
|
||||
crate::norm_angle(self.b.atan2(self.a))
|
||||
}
|
||||
#[must_use]
|
||||
pub fn is_mirroring(&self) -> bool {
|
||||
self.det() < 0.0
|
||||
}
|
||||
@@ -138,7 +123,6 @@ impl Default for Mat4 {
|
||||
|
||||
impl Mat4 {
|
||||
pub const IDENTITY: Mat4 = Mat4 { m: [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]] };
|
||||
#[must_use]
|
||||
pub fn mul(&self, o: &Mat4) -> Mat4 {
|
||||
let mut r = [[0.0; 4]; 4];
|
||||
for (i, row) in r.iter_mut().enumerate() {
|
||||
@@ -148,7 +132,6 @@ impl Mat4 {
|
||||
}
|
||||
Mat4 { m: r }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn apply(&self, p: Vec3) -> Vec3 {
|
||||
let m = &self.m;
|
||||
let w = m[3][0] * p.x + m[3][1] * p.y + m[3][2] * p.z + m[3][3];
|
||||
@@ -159,12 +142,10 @@ impl Mat4 {
|
||||
(m[2][0] * p.x + m[2][1] * p.y + m[2][2] * p.z + m[2][3]) / w,
|
||||
)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn from_mat3(t: &Mat3) -> Mat4 {
|
||||
Mat4 { m: [[t.a, t.c, 0.0, t.e], [t.b, t.d, 0.0, t.f], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]] }
|
||||
}
|
||||
/// The "arbitrary axis algorithm" frame for an extrusion direction (DXF OCS → WCS).
|
||||
#[must_use]
|
||||
pub fn ocs(normal: Vec3) -> Mat4 {
|
||||
let n = normal.normalized();
|
||||
if n == Vec3::ZERO || (n.x.abs() < 1e-12 && n.y.abs() < 1e-12 && n.z > 0.0) {
|
||||
|
||||
@@ -128,7 +128,6 @@ impl Spline {
|
||||
}
|
||||
|
||||
/// True when the spline carries weights (a NURBS rather than a plain B-spline).
|
||||
#[must_use]
|
||||
pub fn is_rational(&self) -> bool {
|
||||
!self.weights.is_empty()
|
||||
}
|
||||
@@ -155,7 +154,6 @@ impl Spline {
|
||||
}
|
||||
|
||||
/// First derivative dC/dt (zero on an invalid spline).
|
||||
#[must_use]
|
||||
pub fn derivative(&self, t: f64) -> Vec2 {
|
||||
if !self.is_valid() {
|
||||
return Vec2::ZERO;
|
||||
@@ -216,7 +214,6 @@ impl Spline {
|
||||
|
||||
/// Unit tangent at `t` in the direction of increasing parameter; a central difference stands
|
||||
/// in where the derivative vanishes. Zero when the curve does not move at all.
|
||||
#[must_use]
|
||||
pub fn unit_tangent(&self, t: f64) -> Vec2 {
|
||||
let d = self.derivative(t).normalized();
|
||||
if d != Vec2::ZERO {
|
||||
@@ -229,7 +226,6 @@ impl Spline {
|
||||
|
||||
/// Insert knot `t` once (Boehm's algorithm, in homogeneous coordinates). The result has no
|
||||
/// fit data and is open.
|
||||
#[must_use]
|
||||
#[expect(clippy::many_single_char_names, reason = "Boehm knot insertion keeps the standard p/n/k/i/h/a equation notation.")]
|
||||
pub fn insert_knot(&self, t: f64) -> Option<Spline> {
|
||||
if !self.is_valid() || !t.is_finite() || self.control.len() >= MAX_EDIT_CONTROL {
|
||||
@@ -298,7 +294,6 @@ impl Spline {
|
||||
}
|
||||
|
||||
/// Split at parameter `t` into (before, after). `None` at the ends or on invalid input.
|
||||
#[must_use]
|
||||
pub fn split_at(&self, t: f64) -> Option<(Spline, Spline)> {
|
||||
let (lo, hi) = self.domain();
|
||||
if !(t > lo + 1e-12 && t < hi - 1e-12) {
|
||||
@@ -309,7 +304,6 @@ impl Spline {
|
||||
}
|
||||
|
||||
/// The open piece between parameters `a < b`.
|
||||
#[must_use]
|
||||
pub fn sub_between(&self, a: f64, b: f64) -> Option<Spline> {
|
||||
let (lo, hi) = self.domain();
|
||||
let mut s = self.clone();
|
||||
@@ -326,7 +320,6 @@ impl Spline {
|
||||
|
||||
/// The same curve with clamped ends (end knots repeated degree + 1 times), so it starts and
|
||||
/// ends on its first and last control points. Fit data is dropped.
|
||||
#[must_use]
|
||||
pub fn clamped(&self) -> Option<Spline> {
|
||||
if !self.is_valid() {
|
||||
return None;
|
||||
@@ -349,7 +342,6 @@ impl Spline {
|
||||
|
||||
/// Reversed direction: control points, weights, fit points and knots (and the fit end
|
||||
/// tangents swap ends and turn round).
|
||||
#[must_use]
|
||||
pub fn reversed(&self) -> Spline {
|
||||
let (lo, hi) = (self.knots.first().copied().unwrap_or(0.0), self.knots.last().copied().unwrap_or(1.0));
|
||||
Spline {
|
||||
@@ -402,7 +394,6 @@ impl Spline {
|
||||
}
|
||||
|
||||
/// The same curve at a higher (or equal) degree, clamped.
|
||||
#[must_use]
|
||||
pub fn elevated(&self, degree: usize) -> Option<Spline> {
|
||||
if degree < self.degree || degree > MAX_EDIT_DEGREE {
|
||||
return None;
|
||||
@@ -428,7 +419,6 @@ impl Spline {
|
||||
/// point becomes their midpoint). Degrees are matched by elevation, weights rescaled so the
|
||||
/// junction carries one weight, and `other`'s parameters follow on at a similar speed. The
|
||||
/// junction is a knot of multiplicity `degree`, so the curve passes through it exactly.
|
||||
#[must_use]
|
||||
#[expect(
|
||||
clippy::many_single_char_names,
|
||||
reason = "Spline joining keeps the two curves, homogeneous points and knot spans in standard short notation."
|
||||
@@ -461,7 +451,6 @@ impl Spline {
|
||||
|
||||
/// A straight line from `a` to `b` as a clamped spline of `degree` (evenly spaced control
|
||||
/// points, parameter = distance).
|
||||
#[must_use]
|
||||
#[expect(clippy::cast_precision_loss, reason = "The degree is clamped by MAX_EDIT_DEGREE (25), so every control index is exact as f64.")]
|
||||
pub fn from_line(a: Vec2, b: Vec2, degree: usize) -> Spline {
|
||||
let p = degree.clamp(1, MAX_EDIT_DEGREE);
|
||||
@@ -476,7 +465,6 @@ impl Spline {
|
||||
/// The elliptical arc `center + u·cos θ + v·sin θ` for θ from `start` over `sweep` (negative
|
||||
/// runs clockwise) as an exact rational quadratic spline, in pieces of at most a quarter
|
||||
/// turn. Circular arcs are the case `|u| = |v|`, `u ⟂ v`.
|
||||
#[must_use]
|
||||
#[expect(
|
||||
clippy::cast_precision_loss,
|
||||
clippy::cast_possible_truncation,
|
||||
@@ -513,7 +501,6 @@ impl Spline {
|
||||
}
|
||||
|
||||
/// An ellipse or elliptical arc as an exact rational spline (counter-clockwise).
|
||||
#[must_use]
|
||||
pub fn from_ellipse(e: &Ellipse) -> Option<Spline> {
|
||||
let s = Spline::from_conic_arc(e.center, e.major, e.minor(), e.start, e.sweep())?;
|
||||
Some(Spline { closed: e.is_full(), ..s })
|
||||
@@ -521,7 +508,6 @@ impl Spline {
|
||||
|
||||
/// Run the spline on in a straight line along its end tangent by `dist` > 0: past the end
|
||||
/// (`at_end`) or before the start. The result is clamped, open and has no fit data.
|
||||
#[must_use]
|
||||
pub fn extended(&self, at_end: bool, dist: f64) -> Option<Spline> {
|
||||
if !(dist.is_finite() && dist > 0.0) || self.closed {
|
||||
return None;
|
||||
@@ -564,14 +550,12 @@ impl Spline {
|
||||
}
|
||||
|
||||
/// Arc length between parameters `a` and `b` (adaptive Gauss–Legendre over each knot span).
|
||||
#[must_use]
|
||||
pub fn length_between(&self, a: f64, b: f64) -> f64 {
|
||||
let l = self.integrated_length(a, b);
|
||||
if l.is_finite() { l } else { 0.0 }
|
||||
}
|
||||
|
||||
/// Arc length of the whole spline.
|
||||
#[must_use]
|
||||
pub fn length(&self) -> f64 {
|
||||
let (lo, hi) = self.domain();
|
||||
self.length_between(lo, hi)
|
||||
@@ -598,7 +582,6 @@ impl Spline {
|
||||
|
||||
/// The parameter `len` along the curve from parameter `from` (forward for positive `len`,
|
||||
/// backward for negative), clamped to the domain.
|
||||
#[must_use]
|
||||
pub fn param_at_length(&self, from: f64, len: f64) -> f64 {
|
||||
let (lo, hi) = self.domain();
|
||||
if !self.is_valid() {
|
||||
@@ -641,13 +624,11 @@ impl Spline {
|
||||
|
||||
impl Ellipse {
|
||||
/// dP/dθ at parameter `t`.
|
||||
#[must_use]
|
||||
pub fn derivative(&self, t: f64) -> Vec2 {
|
||||
self.minor() * t.cos() - self.major * t.sin()
|
||||
}
|
||||
|
||||
/// Arc length from parameter `t0` counter-clockwise over `sweep` (radians, >= 0).
|
||||
#[must_use]
|
||||
#[expect(
|
||||
clippy::cast_precision_loss,
|
||||
clippy::cast_possible_truncation,
|
||||
@@ -672,14 +653,12 @@ impl Ellipse {
|
||||
}
|
||||
|
||||
/// Arc length of the ellipse or elliptical arc.
|
||||
#[must_use]
|
||||
pub fn length(&self) -> f64 {
|
||||
self.arc_length_from(self.start, self.sweep())
|
||||
}
|
||||
|
||||
/// The parameter reached `len` along the ellipse from `t0`: counter-clockwise for positive
|
||||
/// `len`, clockwise for negative. `None` for a whole turn or more, or degenerate input.
|
||||
#[must_use]
|
||||
pub fn param_at_length(&self, t0: f64, len: f64) -> Option<f64> {
|
||||
let full = self.arc_length_from(0.0, TAU);
|
||||
if !(t0.is_finite() && len.is_finite() && full > EPS) || len.abs() >= full {
|
||||
|
||||
@@ -563,7 +563,6 @@ fn nearest_curve(polys: &[Vec<Vec2>], i: usize, e: Vec2, snap: f64, tol: f64, bu
|
||||
/// curve (which gains a vertex there), so the two meet exactly. A curve that is flattened
|
||||
/// into chords (an ellipse, a spline) lies up to the flattening tolerance off the true curve,
|
||||
/// so a line that ends on the true curve can stop just short of the chords.
|
||||
#[must_use]
|
||||
#[expect(clippy::many_single_char_names, reason = "Endpoint snapping keeps conventional polyline, segment and point notation.")]
|
||||
pub fn snap_ends(polys: &[Vec<Vec2>], snap: f64, tol: f64) -> Vec<Vec<Vec2>> {
|
||||
let mut out: Vec<Vec<Vec2>> = polys.to_vec();
|
||||
@@ -614,26 +613,22 @@ pub fn snap_ends(polys: &[Vec<Vec2>], snap: f64, tol: f64) -> Vec<Vec<Vec2>> {
|
||||
}
|
||||
|
||||
/// The closed loop enclosing `p`, counter-clockwise, or `None`.
|
||||
#[must_use]
|
||||
pub fn enclosing_loop(polys: &[Vec<Vec2>], p: Vec2, tol: f64) -> Option<Vec<Vec2>> {
|
||||
enclosing_loop_gap(polys, p, tol, 0.0)
|
||||
}
|
||||
|
||||
/// [`enclosing_loop`], with gaps of up to `gap` between curve ends closed (HPGAPTOL).
|
||||
#[must_use]
|
||||
pub fn enclosing_loop_gap(polys: &[Vec<Vec2>], p: Vec2, tol: f64, gap: f64) -> Option<Vec<Vec2>> {
|
||||
let g = build_gap(polys, tol, gap)?;
|
||||
points(&g, &outer_face(&g, p)?)
|
||||
}
|
||||
|
||||
/// The closed loop enclosing `p` plus the islands inside it, or `None` when `p` isn't enclosed.
|
||||
#[must_use]
|
||||
pub fn boundary_at(polys: &[Vec<Vec2>], p: Vec2, tol: f64) -> Option<Boundary> {
|
||||
boundary_at_gap(polys, p, tol, 0.0)
|
||||
}
|
||||
|
||||
/// [`boundary_at`], with gaps of up to `gap` between curve ends closed (HPGAPTOL).
|
||||
#[must_use]
|
||||
pub fn boundary_at_gap(polys: &[Vec<Vec2>], p: Vec2, tol: f64, gap: f64) -> Option<Boundary> {
|
||||
let g = build_gap(polys, tol, gap)?;
|
||||
let ids = outer_face(&g, p)?;
|
||||
@@ -649,7 +644,6 @@ pub fn boundary_at_gap(polys: &[Vec<Vec2>], p: Vec2, tol: f64, gap: f64) -> Opti
|
||||
/// The outside outline of curves that together enclose an area (counter-clockwise, dangling
|
||||
/// ends removed), or `None` when they enclose nothing or form more than one separate group.
|
||||
/// Gaps of up to `gap` between curve ends are closed (HPGAPTOL).
|
||||
#[must_use]
|
||||
#[expect(clippy::float_cmp, reason = "Exact x equality is required for the deterministic lexicographic tie-break.")]
|
||||
pub fn outline(polys: &[Vec<Vec2>], tol: f64, gap: f64) -> Option<Vec<Vec2>> {
|
||||
let g = build_gap(polys, tol, gap)?;
|
||||
|
||||
@@ -30,7 +30,6 @@ pub struct Spline {
|
||||
|
||||
impl Spline {
|
||||
/// A clamped uniform spline over control points.
|
||||
#[must_use]
|
||||
pub fn from_control(control: Vec<Vec2>, degree: usize) -> Spline {
|
||||
let n = control.len();
|
||||
let degree = degree.clamp(1, n.saturating_sub(1).max(1));
|
||||
@@ -40,7 +39,6 @@ impl Spline {
|
||||
|
||||
/// Cubic interpolation through fit points (chord-length parametrisation, natural-ish ends
|
||||
/// via a clamped knot vector: solve the banded system for control points).
|
||||
#[must_use]
|
||||
pub fn from_fit_points(fit: &[Vec2]) -> Spline {
|
||||
Spline::fit_with(fit, false, FitOptions::default())
|
||||
}
|
||||
@@ -49,24 +47,20 @@ impl Spline {
|
||||
/// include the closing chord; the knot vector is the periodic (unclamped) extension and the
|
||||
/// first `degree` control points repeat at the end, so the curve has continuous tangent and
|
||||
/// curvature at the seam. A repeated closing fit point is dropped.
|
||||
#[must_use]
|
||||
pub fn from_fit_points_closed(fit: &[Vec2]) -> Spline {
|
||||
Spline::fit_with(fit, true, FitOptions::default())
|
||||
}
|
||||
|
||||
/// Interpolation through fit points, open or closed (see `from_fit_points_closed`).
|
||||
#[must_use]
|
||||
pub fn from_fit(fit: &[Vec2], closed: bool) -> Spline {
|
||||
Spline::fit_with(fit, closed, FitOptions::default())
|
||||
}
|
||||
|
||||
/// Closed with an unclamped (periodic) knot vector, as DXF flags it (group 70 bit 2).
|
||||
#[must_use]
|
||||
pub fn is_periodic(&self) -> bool {
|
||||
self.closed && self.knots.first() != self.knots.get(self.degree)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_valid(&self) -> bool {
|
||||
let n = self.control.len();
|
||||
n > self.degree
|
||||
@@ -76,7 +70,6 @@ impl Spline {
|
||||
&& self.knots.windows(2).all(|w| w.first() <= w.get(1))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn domain(&self) -> (f64, f64) {
|
||||
let lo = self.knots.get(self.degree).copied().unwrap_or(0.0);
|
||||
let hi = self.knots.get(self.control.len()).copied().unwrap_or(1.0);
|
||||
@@ -84,7 +77,6 @@ impl Spline {
|
||||
}
|
||||
|
||||
/// Evaluate at parameter `t` (de Boor via basis functions).
|
||||
#[must_use]
|
||||
pub fn eval(&self, t: f64) -> Vec2 {
|
||||
if !self.is_valid() {
|
||||
return self.control.first().copied().unwrap_or_default();
|
||||
@@ -122,7 +114,6 @@ impl Spline {
|
||||
if wsum.abs() > 1e-300 { p / wsum } else { p }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn tessellate(&self, tol: f64) -> Vec<Vec2> {
|
||||
if !self.is_valid() {
|
||||
return self.control.clone();
|
||||
@@ -175,7 +166,6 @@ impl Spline {
|
||||
/// is its control polygon, so its control box is exact; a degenerate spline gets its control
|
||||
/// box, which contains the curve (the box of the finite control points when one is not
|
||||
/// finite: `hull` skips it, but it would poison every sample).
|
||||
#[must_use]
|
||||
#[expect(
|
||||
clippy::cast_precision_loss,
|
||||
clippy::cast_possible_truncation,
|
||||
|
||||
@@ -24,7 +24,6 @@ impl KnotParam {
|
||||
pub const ALL: [KnotParam; 3] = [KnotParam::Chord, KnotParam::SqrtChord, KnotParam::Uniform];
|
||||
|
||||
/// The name as the prompt and the "Current settings" line show it.
|
||||
#[must_use]
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
KnotParam::Chord => "Chord",
|
||||
@@ -35,7 +34,6 @@ impl KnotParam {
|
||||
|
||||
/// Parse a keyword or JSON value ("chord", "sqrt", "square root", "uniform", or 0/1/2 as in
|
||||
/// the SPLKNOTS setting).
|
||||
#[must_use]
|
||||
pub fn parse(s: &str) -> Option<KnotParam> {
|
||||
Some(match s.trim().to_ascii_lowercase().as_str() {
|
||||
"c" | "chord" | "0" => KnotParam::Chord,
|
||||
@@ -46,7 +44,6 @@ impl KnotParam {
|
||||
}
|
||||
|
||||
/// The SPLKNOTS code (0 chord, 1 square root, 2 uniform).
|
||||
#[must_use]
|
||||
pub fn code(self) -> i64 {
|
||||
match self {
|
||||
KnotParam::Chord => 0,
|
||||
@@ -84,7 +81,6 @@ pub struct FitOptions {
|
||||
impl FitOptions {
|
||||
/// Normalised tangents (zero or non-finite directions dropped) and a finite, non-negative
|
||||
/// tolerance: what a spline stores.
|
||||
#[must_use]
|
||||
pub fn sanitized(self) -> FitOptions {
|
||||
let dir = |t: Option<Vec2>| t.filter(|v| v.x.is_finite() && v.y.is_finite() && v.len() > 1e-12).map(Vec2::normalized);
|
||||
FitOptions {
|
||||
@@ -111,7 +107,6 @@ impl Spline {
|
||||
/// control points. A fit point given twice in a row is a kink (SPLINEDIT Fit data Kink): the
|
||||
/// curve passes through it with a corner (a knot of full multiplicity) and the pieces on
|
||||
/// either side are fitted on their own.
|
||||
#[must_use]
|
||||
pub fn fit_with(fit: &[Vec2], closed: bool, opts: FitOptions) -> Spline {
|
||||
let opts = opts.sanitized();
|
||||
let kinked = if fit.len() <= MAX_KINK_POINTS { kinked_fit(fit, closed, opts) } else { None };
|
||||
@@ -131,7 +126,6 @@ impl Spline {
|
||||
}
|
||||
|
||||
/// Rebuild from new fit points with this spline's knot parametrisation, tangents and tolerance.
|
||||
#[must_use]
|
||||
pub fn refit(&self, fit: &[Vec2], closed: bool) -> Spline {
|
||||
Spline::fit_with(fit, closed, self.fit_opts)
|
||||
}
|
||||
@@ -139,7 +133,6 @@ impl Spline {
|
||||
/// Which knot parametrisation produced this spline's knots from its fit points (DXF files
|
||||
/// keep the knots but not the parametrisation). Compares knot vectors only (no solving), so
|
||||
/// it is cheap for any number of fit points. Chord when nothing matches.
|
||||
#[must_use]
|
||||
pub fn infer_knot_param(&self) -> KnotParam {
|
||||
if self.fit.len() < 3 {
|
||||
return self.fit_opts.knots;
|
||||
|
||||
@@ -14,100 +14,78 @@ impl Vec2 {
|
||||
pub const X: Vec2 = Vec2 { x: 1.0, y: 0.0 };
|
||||
pub const Y: Vec2 = Vec2 { x: 0.0, y: 1.0 };
|
||||
|
||||
#[must_use]
|
||||
pub const fn new(x: f64, y: f64) -> Self {
|
||||
Vec2 { x, y }
|
||||
}
|
||||
/// Unit vector at `angle` radians.
|
||||
#[must_use]
|
||||
pub fn from_angle(angle: f64) -> Self {
|
||||
Vec2::new(angle.cos(), angle.sin())
|
||||
}
|
||||
#[must_use]
|
||||
pub fn polar(center: Vec2, radius: f64, angle: f64) -> Self {
|
||||
center + Vec2::from_angle(angle) * radius
|
||||
}
|
||||
#[must_use]
|
||||
pub fn dot(self, o: Vec2) -> f64 {
|
||||
self.x * o.x + self.y * o.y
|
||||
}
|
||||
/// z of the 3D cross product.
|
||||
#[must_use]
|
||||
pub fn cross(self, o: Vec2) -> f64 {
|
||||
self.x * o.y - self.y * o.x
|
||||
}
|
||||
#[must_use]
|
||||
pub fn len(self) -> f64 {
|
||||
self.x.hypot(self.y)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn len2(self) -> f64 {
|
||||
self.dot(self)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn dist(self, o: Vec2) -> f64 {
|
||||
(self - o).len()
|
||||
}
|
||||
#[must_use]
|
||||
pub fn angle(self) -> f64 {
|
||||
crate::norm_angle(self.y.atan2(self.x))
|
||||
}
|
||||
#[must_use]
|
||||
pub fn angle_to(self, o: Vec2) -> f64 {
|
||||
(o - self).angle()
|
||||
}
|
||||
/// Unit vector, or zero for a zero-length vector.
|
||||
#[must_use]
|
||||
pub fn normalized(self) -> Vec2 {
|
||||
let l = self.len();
|
||||
if l > crate::EPS { self / l } else { Vec2::ZERO }
|
||||
}
|
||||
/// Rotated 90° counter-clockwise.
|
||||
#[must_use]
|
||||
pub fn perp(self) -> Vec2 {
|
||||
Vec2::new(-self.y, self.x)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn rotate(self, a: f64) -> Vec2 {
|
||||
let (s, c) = a.sin_cos();
|
||||
Vec2::new(self.x * c - self.y * s, self.x * s + self.y * c)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn rotate_about(self, center: Vec2, a: f64) -> Vec2 {
|
||||
center + (self - center).rotate(a)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn lerp(self, o: Vec2, t: f64) -> Vec2 {
|
||||
self + (o - self) * t
|
||||
}
|
||||
#[must_use]
|
||||
pub fn mid(self, o: Vec2) -> Vec2 {
|
||||
Vec2::new(self.x.midpoint(o.x), self.y.midpoint(o.y))
|
||||
}
|
||||
#[must_use]
|
||||
pub fn is_finite(self) -> bool {
|
||||
self.x.is_finite() && self.y.is_finite()
|
||||
}
|
||||
#[must_use]
|
||||
pub fn near(self, o: Vec2, tol: f64) -> bool {
|
||||
// Squaring very large finite values can overflow on both sides and make
|
||||
// unrelated points compare equal (infinity <= infinity).
|
||||
tol.is_finite() && tol >= 0.0 && self.dist(o) <= tol
|
||||
}
|
||||
#[must_use]
|
||||
pub fn to3(self, z: f64) -> Vec3 {
|
||||
Vec3::new(self.x, self.y, z)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn min(self, o: Vec2) -> Vec2 {
|
||||
Vec2::new(self.x.min(o.x), self.y.min(o.y))
|
||||
}
|
||||
#[must_use]
|
||||
pub fn max(self, o: Vec2) -> Vec2 {
|
||||
Vec2::new(self.x.max(o.x), self.y.max(o.y))
|
||||
}
|
||||
/// Mirror across the infinite line through `a` and `b`.
|
||||
#[must_use]
|
||||
pub fn mirror(self, a: Vec2, b: Vec2) -> Vec2 {
|
||||
let d = (b - a).normalized();
|
||||
if d == Vec2::ZERO {
|
||||
@@ -173,33 +151,26 @@ pub struct Vec3 {
|
||||
impl Vec3 {
|
||||
pub const ZERO: Vec3 = Vec3 { x: 0.0, y: 0.0, z: 0.0 };
|
||||
pub const Z: Vec3 = Vec3 { x: 0.0, y: 0.0, z: 1.0 };
|
||||
#[must_use]
|
||||
pub const fn new(x: f64, y: f64, z: f64) -> Self {
|
||||
Vec3 { x, y, z }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn xy(self) -> Vec2 {
|
||||
Vec2::new(self.x, self.y)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn dot(self, o: Vec3) -> f64 {
|
||||
self.x * o.x + self.y * o.y + self.z * o.z
|
||||
}
|
||||
#[must_use]
|
||||
pub fn cross(self, o: Vec3) -> Vec3 {
|
||||
Vec3::new(self.y * o.z - self.z * o.y, self.z * o.x - self.x * o.z, self.x * o.y - self.y * o.x)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn len(self) -> f64 {
|
||||
// Avoid overflowing the squared components of large finite vectors.
|
||||
self.x.hypot(self.y).hypot(self.z)
|
||||
}
|
||||
#[must_use]
|
||||
pub fn normalized(self) -> Vec3 {
|
||||
let l = self.len();
|
||||
if l > crate::EPS { self * (1.0 / l) } else { Vec3::ZERO }
|
||||
}
|
||||
#[must_use]
|
||||
pub fn is_finite(self) -> bool {
|
||||
self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user