mathverse-geometry 0.1.3

Production-grade 2D/3D geometry library: shapes, spatial structures, transforms, intersections, distances, mesh ray tracing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! 2D primitives: line segment, ray, arc, sector, circular segment, polyline, Bézier curve.

use mathverse_core::constants::TAU;

use super::shapes2d::Point2;

// ---------------------------------------------------------------------------
// LineSegment2
// ---------------------------------------------------------------------------

/// Directed line segment from `a` to `b`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LineSegment2 {
    pub a: Point2,
    pub b: Point2,
}

impl LineSegment2 {
    pub fn new(a: Point2, b: Point2) -> Self {
        Self { a, b }
    }
    pub fn length(self) -> f64 {
        self.a.distance_to(self.b)
    }
    pub fn midpoint(self) -> Point2 {
        Point2::new((self.a.x + self.b.x) / 2.0, (self.a.y + self.b.y) / 2.0)
    }
    /// Direction angle in radians.
    pub fn angle(self) -> f64 {
        (self.b.y - self.a.y).atan2(self.b.x - self.a.x)
    }
    /// Closest point on this segment to `p`.
    pub fn closest_point(self, p: Point2) -> Point2 {
        let dx = self.b.x - self.a.x;
        let dy = self.b.y - self.a.y;
        let len_sq = dx * dx + dy * dy;
        if len_sq < 1e-30 {
            return self.a;
        }
        let t = ((p.x - self.a.x) * dx + (p.y - self.a.y) * dy) / len_sq;
        let t = t.clamp(0.0, 1.0);
        Point2::new(self.a.x + t * dx, self.a.y + t * dy)
    }
    /// Distance from point `p` to this segment.
    pub fn distance_to_point(self, p: Point2) -> f64 {
        p.distance_to(self.closest_point(p))
    }
}

// ---------------------------------------------------------------------------
// Ray2
// ---------------------------------------------------------------------------

/// Ray starting at `origin` going in direction `dir`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Ray2 {
    pub origin: Point2,
    pub dir: Point2,
}

impl Ray2 {
    /// Panics if `dir` is zero.
    pub fn new(origin: Point2, dir: Point2) -> Self {
        assert!(
            (dir.x * dir.x + dir.y * dir.y) > 1e-30,
            "ray direction must be non-zero"
        );
        Self { origin, dir }
    }
    pub fn point_at(self, t: f64) -> Point2 {
        Point2::new(self.origin.x + t * self.dir.x, self.origin.y + t * self.dir.y)
    }
    /// Distance from point `p` to the ray (closest point on the half-line).
    pub fn distance_to_point(self, p: Point2) -> f64 {
        let vx = p.x - self.origin.x;
        let vy = p.y - self.origin.y;
        let t = (vx * self.dir.x + vy * self.dir.y) / (self.dir.x * self.dir.x + self.dir.y * self.dir.y);
        if t <= 0.0 {
            return p.distance_to(self.origin);
        }
        p.distance_to(self.point_at(t))
    }
}

// ---------------------------------------------------------------------------
// Arc
// ---------------------------------------------------------------------------

/// Circular arc defined by center, radius, start angle, and sweep angle (radians).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Arc {
    pub center: Point2,
    pub radius: f64,
    pub start: f64,
    pub sweep: f64,
}

impl Arc {
    /// Panics if `radius < 0`. `sweep` may be negative (clockwise).
    pub fn new(center: Point2, radius: f64, start: f64, sweep: f64) -> Self {
        assert!(radius >= 0.0, "arc radius must be non-negative");
        Self {
            center,
            radius,
            start,
            sweep,
        }
    }
    pub fn start_point(self) -> Point2 {
        Point2::new(
            self.center.x + self.radius * self.start.cos(),
            self.center.y + self.radius * self.start.sin(),
        )
    }
    pub fn end_point(self) -> Point2 {
        let a = self.start + self.sweep;
        Point2::new(
            self.center.x + self.radius * a.cos(),
            self.center.y + self.radius * a.sin(),
        )
    }
    /// Arc length.
    pub fn length(self) -> f64 {
        self.radius * self.sweep.abs()
    }
    /// Area of the arc (sector minus triangle if sweep < 2π).
    pub fn area(self) -> f64 {
        0.5 * self.radius * self.radius * self.sweep.abs()
    }
    /// Whether point `p` lies inside the arc (within the sector and on the arc).
    pub fn contains(self, p: Point2) -> bool {
        let dx = p.x - self.center.x;
        let dy = p.y - self.center.y;
        let dist = (dx * dx + dy * dy).sqrt();
        if (dist - self.radius).abs() > 1e-10 {
            return false;
        }
        let mut angle = dy.atan2(dx);
        let mut start = self.start;
        let mut end = self.start + self.sweep;
        // Normalize angles to [0, 2π)
        while angle < 0.0 {
            angle += TAU;
        }
        while angle >= TAU {
            angle -= TAU;
        }
        while start < 0.0 {
            start += TAU;
        }
        while start >= TAU {
            start -= TAU;
        }
        while end < 0.0 {
            end += TAU;
        }
        while end >= TAU {
            end -= TAU;
        }
        if self.sweep >= 0.0 {
            if start <= end {
                angle >= start && angle <= end
            } else {
                angle >= start || angle <= end
            }
        } else {
            if end <= start {
                angle >= end && angle <= start
            } else {
                angle >= end || angle <= start
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Sector
// ---------------------------------------------------------------------------

/// Circular sector (pie slice) defined by center, radius, start angle, sweep angle.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Sector {
    pub center: Point2,
    pub radius: f64,
    pub start: f64,
    pub sweep: f64,
}

impl Sector {
    pub fn new(center: Point2, radius: f64, start: f64, sweep: f64) -> Self {
        assert!(radius >= 0.0, "sector radius must be non-negative");
        Self {
            center,
            radius,
            start,
            sweep,
        }
    }
    pub fn area(self) -> f64 {
        0.5 * self.radius * self.radius * self.sweep.abs()
    }
    pub fn perimeter(self) -> f64 {
        2.0 * self.radius + self.radius * self.sweep.abs()
    }
    pub fn contains(self, p: Point2) -> bool {
        let dx = p.x - self.center.x;
        let dy = p.y - self.center.y;
        let dist = (dx * dx + dy * dy).sqrt();
        if dist > self.radius + 1e-10 {
            return false;
        }
        let angle = dy.atan2(dx);
        let start = self.start;
        let sweep = self.sweep;
        if sweep >= 0.0 {
            let mut a = angle - start;
            while a < 0.0 {
                a += TAU;
            }
            while a >= TAU {
                a -= TAU;
            }
            a <= sweep
        } else {
            let mut a = angle - start;
            while a < 0.0 {
                a += TAU;
            }
            while a >= TAU {
                a -= TAU;
            }
            a >= sweep + TAU || a == 0.0
        }
    }
    /// Arc portion of the sector.
    pub fn arc(self) -> Arc {
        Arc::new(self.center, self.radius, self.start, self.sweep)
    }
}

// ---------------------------------------------------------------------------
// CircularSegment
// ---------------------------------------------------------------------------

/// Region between a chord and the arc it subtends.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CircularSegment {
    pub center: Point2,
    pub radius: f64,
    pub start: f64,
    pub sweep: f64,
}

impl CircularSegment {
    pub fn new(center: Point2, radius: f64, start: f64, sweep: f64) -> Self {
        assert!(radius >= 0.0, "segment radius must be non-negative");
        Self {
            center,
            radius,
            start,
            sweep,
        }
    }
    /// Area = sector area - triangle area.
    pub fn area(self) -> f64 {
        let sector = 0.5 * self.radius * self.radius * self.sweep.abs();
        let triangle =
            0.5 * self.radius * self.radius * self.sweep.abs().sin();
        sector - triangle
    }
    /// Chord length.
    pub fn chord_length(self) -> f64 {
        2.0 * self.radius * (self.sweep.abs() / 2.0).sin()
    }
    /// Arc length.
    pub fn arc_length(self) -> f64 {
        self.radius * self.sweep.abs()
    }
    /// Perimeter = chord + arc.
    pub fn perimeter(self) -> f64 {
        self.chord_length() + self.arc_length()
    }
}

// ---------------------------------------------------------------------------
// Polyline
// ---------------------------------------------------------------------------

/// Open or closed polyline (ordered vertex list).
#[derive(Debug, Clone, PartialEq)]
pub struct Polyline {
    pub points: Vec<Point2>,
    pub closed: bool,
}

impl Polyline {
    pub fn open(points: Vec<Point2>) -> Self {
        Self {
            points,
            closed: false,
        }
    }
    pub fn closed(points: Vec<Point2>) -> Self {
        Self {
            points,
            closed: true,
        }
    }
    pub fn total_length(&self) -> f64 {
        self.points
            .windows(2)
            .map(|w| w[0].distance_to(w[1]))
            .sum::<f64>()
            + if self.closed && self.points.len() > 1 {
                self.points.last().unwrap().distance_to(self.points[0])
            } else {
                0.0
            }
    }
    /// Point at parameter `t` ∈ [0, 1] along the polyline.
    pub fn point_at(&self, t: f64) -> Point2 {
        let total = self.total_length();
        let target = t * total;
        let mut acc = 0.0;
        for w in self.points.windows(2) {
            let seg = w[0].distance_to(w[1]);
            if acc + seg >= target {
                let frac = if seg > 1e-30 {
                    (target - acc) / seg
                } else {
                    0.0
                };
                return Point2::new(
                    w[0].x + frac * (w[1].x - w[0].x),
                    w[0].y + frac * (w[1].y - w[0].y),
                );
            }
            acc += seg;
        }
        if self.closed && self.points.len() > 1 {
            *self.points.last().unwrap()
        } else {
            *self.points.last().unwrap()
        }
    }
}

// ---------------------------------------------------------------------------
// BezierCurve
// ---------------------------------------------------------------------------

/// Cubic Bézier curve with 4 control points.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BezierCurve {
    pub p0: Point2,
    pub p1: Point2,
    pub p2: Point2,
    pub p3: Point2,
}

impl BezierCurve {
    pub fn new(p0: Point2, p1: Point2, p2: Point2, p3: Point2) -> Self {
        Self { p0, p1, p2, p3 }
    }
    /// Evaluate at parameter `t` ∈ [0, 1].
    pub fn point_at(self, t: f64) -> Point2 {
        let u = 1.0 - t;
        let u2 = u * u;
        let u3 = u2 * u;
        let t2 = t * t;
        let t3 = t2 * t;
        Point2::new(
            u3 * self.p0.x + 3.0 * u2 * t * self.p1.x + 3.0 * u * t2 * self.p2.x + t3 * self.p3.x,
            u3 * self.p0.y + 3.0 * u2 * t * self.p1.y + 3.0 * u * t2 * self.p2.y + t3 * self.p3.y,
        )
    }
    /// Approximate length via adaptive subdivision.
    pub fn length(self) -> f64 {
        fn approx_len(p0: Point2, p1: Point2, p2: Point2, p3: Point2, depth: u32) -> f64 {
            let d = p0.distance_to(p3);
            let chord = d;
            let poly = p0.distance_to(p1) + p1.distance_to(p2) + p2.distance_to(p3);
            if (poly - chord).abs() < 1e-6 || depth > 10 {
                return (chord + poly) / 2.0;
            }
            let m01 = Point2::new(
                (p0.x + p1.x) / 2.0,
                (p0.y + p1.y) / 2.0,
            );
            let m12 = Point2::new(
                (p1.x + p2.x) / 2.0,
                (p1.y + p2.y) / 2.0,
            );
            let m23 = Point2::new(
                (p2.x + p3.x) / 2.0,
                (p2.y + p3.y) / 2.0,
            );
            let m012 = Point2::new(
                (m01.x + m12.x) / 2.0,
                (m01.y + m12.y) / 2.0,
            );
            let m123 = Point2::new(
                (m12.x + m23.x) / 2.0,
                (m12.y + m23.y) / 2.0,
            );
            let mid = Point2::new(
                (m012.x + m123.x) / 2.0,
                (m012.y + m123.y) / 2.0,
            );
            approx_len(p0, m01, m012, mid, depth + 1)
                + approx_len(mid, m123, m23, p3, depth + 1)
        }
        approx_len(self.p0, self.p1, self.p2, self.p3, 0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mathverse_core::constants::{PI, TAU};
    use std::f64::consts::FRAC_PI_2;

    fn pt(x: f64, y: f64) -> Point2 {
        Point2::new(x, y)
    }

    #[test]
    fn segment_basics() {
        let s = LineSegment2::new(pt(0.0, 0.0), pt(3.0, 4.0));
        assert!((s.length() - 5.0).abs() < 1e-12);
        assert_eq!(s.midpoint(), pt(1.5, 2.0));
    }

    #[test]
    fn segment_closest_point() {
        let s = LineSegment2::new(pt(0.0, 0.0), pt(1.0, 0.0));
        assert_eq!(s.closest_point(pt(0.5, 3.0)), pt(0.5, 0.0));
        assert_eq!(s.closest_point(pt(-1.0, 0.0)), pt(0.0, 0.0));
        assert_eq!(s.closest_point(pt(2.0, 0.0)), pt(1.0, 0.0));
    }

    #[test]
    fn ray_distance() {
        let r = Ray2::new(pt(0.0, 0.0), pt(1.0, 0.0));
        assert!((r.distance_to_point(pt(0.0, 1.0)) - 1.0).abs() < 1e-12);
        assert!((r.distance_to_point(pt(-1.0, 0.0)) - 1.0).abs() < 1e-12);
    }

    #[test]
    fn arc_area() {
        let a = Arc::new(pt(0.0, 0.0), 2.0, 0.0, core::f64::consts::FRAC_PI_2);
        assert!((a.area() - PI).abs() < 1e-12);
    }

    #[test]
    fn sector_contains() {
        let s = Sector::new(pt(0.0, 0.0), 1.0, 0.0, FRAC_PI_2);
        assert!(s.contains(pt(0.5, 0.5)));
        assert!(!s.contains(pt(2.0, 2.0)));
    }

    #[test]
    fn segment_area() {
        let s = CircularSegment::new(pt(0.0, 0.0), 1.0, 0.0, PI);
        let expected = PI / 2.0 - 0.0; // sector - triangle = π/2 - 0
        // sector = 0.5*1*π = π/2, triangle = 0.5*1*sin(π) = 0
        assert!((s.area() - expected).abs() < 1e-12);
    }

    #[test]
    fn polyline_length() {
        let pl = Polyline::open(vec![pt(0.0, 0.0), pt(1.0, 0.0), pt(1.0, 1.0)]);
        assert!((pl.total_length() - 2.0).abs() < 1e-12);
    }

    #[test]
    fn bezier_at_zero_one() {
        let b = BezierCurve::new(pt(0.0, 0.0), pt(1.0, 2.0), pt(3.0, 2.0), pt(4.0, 0.0));
        assert_eq!(b.point_at(0.0), pt(0.0, 0.0));
        assert_eq!(b.point_at(1.0), pt(4.0, 0.0));
    }
}