Another project
1use bone_types::{Parameter, Point2, Tolerance, UnitVec2, Vec2};
2
3use crate::aabb::Aabb2;
4use crate::arc2::Arc2;
5use crate::circle2::Circle2;
6use crate::closest::ClosestPoint2;
7use crate::curvature::Curvature;
8use crate::line2::Line2;
9
10pub trait Curve2: Copy {
11 fn evaluate(&self, t: Parameter) -> Point2;
12 fn derivative(&self, t: Parameter) -> Vec2;
13 fn tangent(&self, t: Parameter) -> UnitVec2;
14 fn curvature(&self, t: Parameter) -> Curvature;
15 fn bounding_box(&self) -> Aabb2;
16 fn closest_point(&self, p: Point2, tolerance: Tolerance) -> ClosestPoint2;
17 fn as_kind(&self) -> Curve2Kind;
18}
19
20#[derive(Copy, Clone, Debug, PartialEq)]
21pub enum Curve2Kind {
22 Line(Line2),
23 Arc(Arc2),
24 Circle(Circle2),
25}
26
27impl Curve2 for Curve2Kind {
28 fn evaluate(&self, t: Parameter) -> Point2 {
29 match self {
30 Self::Line(c) => c.evaluate(t),
31 Self::Arc(c) => c.evaluate(t),
32 Self::Circle(c) => c.evaluate(t),
33 }
34 }
35 fn derivative(&self, t: Parameter) -> Vec2 {
36 match self {
37 Self::Line(c) => c.derivative(t),
38 Self::Arc(c) => c.derivative(t),
39 Self::Circle(c) => c.derivative(t),
40 }
41 }
42 fn tangent(&self, t: Parameter) -> UnitVec2 {
43 match self {
44 Self::Line(c) => c.tangent(t),
45 Self::Arc(c) => c.tangent(t),
46 Self::Circle(c) => c.tangent(t),
47 }
48 }
49 fn curvature(&self, t: Parameter) -> Curvature {
50 match self {
51 Self::Line(c) => c.curvature(t),
52 Self::Arc(c) => c.curvature(t),
53 Self::Circle(c) => c.curvature(t),
54 }
55 }
56 fn bounding_box(&self) -> Aabb2 {
57 match self {
58 Self::Line(c) => c.bounding_box(),
59 Self::Arc(c) => c.bounding_box(),
60 Self::Circle(c) => c.bounding_box(),
61 }
62 }
63 fn closest_point(&self, p: Point2, tolerance: Tolerance) -> ClosestPoint2 {
64 match self {
65 Self::Line(c) => c.closest_point(p, tolerance),
66 Self::Arc(c) => c.closest_point(p, tolerance),
67 Self::Circle(c) => c.closest_point(p, tolerance),
68 }
69 }
70 fn as_kind(&self) -> Curve2Kind {
71 *self
72 }
73}