Share methods between Path<2> and Path<3>

This commit is contained in:
Hanno Braun 2024-08-15 20:02:13 +02:00
parent df4ccd1581
commit 3798e3f709

View File

@ -15,14 +15,6 @@ pub enum Path<const D: usize> {
} }
impl Path<2> { impl Path<2> {
/// Build a circle from the given radius
pub fn circle_from_center_and_radius(
center: impl Into<Point<2>>,
radius: impl Into<Scalar>,
) -> Self {
Self::Circle(Circle::from_center_and_radius(center, radius))
}
/// Build a line that represents the u-axis of the surface its on /// Build a line that represents the u-axis of the surface its on
pub fn u_axis() -> Self { pub fn u_axis() -> Self {
let a = Point::origin(); let a = Point::origin();
@ -40,43 +32,6 @@ impl Path<2> {
let (self_, _) = Self::line_from_points([a, b]); let (self_, _) = Self::line_from_points([a, b]);
self_ self_
} }
/// Construct a line from two points
///
/// Also returns the coordinates of the points on the path.
pub fn line_from_points(
points: [impl Into<Point<2>>; 2],
) -> (Self, [Point<1>; 2]) {
let (line, coords) = Line::from_points(points);
(Self::Line(line), coords)
}
/// Create a line from two points that include line coordinates
pub fn line_from_points_with_coords(
points: [(impl Into<Point<1>>, impl Into<Point<2>>); 2],
) -> Self {
Self::Line(Line::from_points_with_line_coords(points))
}
/// Convert a point on the path into surface coordinates
pub fn point_from_path_coords(
&self,
point: impl Into<Point<1>>,
) -> Point<2> {
match self {
Self::Circle(circle) => circle.point_from_circle_coords(point),
Self::Line(line) => line.point_from_line_coords(point),
}
}
/// Create a new path that is the reverse of this one
#[must_use]
pub fn reverse(self) -> Self {
match self {
Self::Circle(circle) => Self::Circle(circle.reverse()),
Self::Line(line) => Self::Line(line.reverse()),
}
}
} }
impl Path<3> { impl Path<3> {
@ -104,53 +59,6 @@ impl Path<3> {
)) ))
} }
/// Build a circle from the given radius
pub fn circle_from_radius(radius: impl Into<Scalar>) -> Self {
let radius = radius.into();
Self::Circle(Circle::from_center_and_radius(Point::origin(), radius))
}
/// Construct a line from two points
///
/// Also returns the coordinates of the points on the path.
pub fn line_from_points(
points: [impl Into<Point<3>>; 2],
) -> (Self, [Point<1>; 2]) {
let (line, coords) = Line::from_points(points);
(Self::Line(line), coords)
}
/// Access the origin of the path's coordinate system
pub fn origin(&self) -> Point<3> {
match self {
Self::Circle(circle) => circle.center() + circle.a(),
Self::Line(line) => line.origin(),
}
}
/// Convert a point on the path into global coordinates
pub fn point_from_path_coords(
&self,
point: impl Into<Point<1>>,
) -> Point<3> {
match self {
Self::Circle(circle) => circle.point_from_circle_coords(point),
Self::Line(line) => line.point_from_line_coords(point),
}
}
/// Convert a vector on the path into global coordinates
pub fn vector_from_path_coords(
&self,
vector: impl Into<Vector<1>>,
) -> Vector<3> {
match self {
Self::Circle(circle) => circle.vector_from_circle_coords(vector),
Self::Line(line) => line.vector_from_line_coords(vector),
}
}
/// Transform the path /// Transform the path
#[must_use] #[must_use]
pub fn transform(self, transform: &Transform) -> Self { pub fn transform(self, transform: &Transform) -> Self {
@ -162,3 +70,76 @@ impl Path<3> {
} }
} }
} }
impl<const D: usize> Path<D> {
/// Build a circle from the given radius
pub fn circle_from_center_and_radius(
center: impl Into<Point<D>>,
radius: impl Into<Scalar>,
) -> Self {
Self::Circle(Circle::from_center_and_radius(center, radius))
}
/// Build a circle from the given radius
pub fn circle_from_radius(radius: impl Into<Scalar>) -> Self {
let radius = radius.into();
Self::Circle(Circle::from_center_and_radius(Point::origin(), radius))
}
/// Construct a line from two points
///
/// Also returns the coordinates of the points on the path.
pub fn line_from_points(
points: [impl Into<Point<D>>; 2],
) -> (Self, [Point<1>; 2]) {
let (line, coords) = Line::from_points(points);
(Self::Line(line), coords)
}
/// Create a line from two points that include line coordinates
pub fn line_from_points_with_coords(
points: [(impl Into<Point<1>>, impl Into<Point<D>>); 2],
) -> Self {
Self::Line(Line::from_points_with_line_coords(points))
}
/// Access the origin of the path's coordinate system
pub fn origin(&self) -> Point<D> {
match self {
Self::Circle(circle) => circle.center() + circle.a(),
Self::Line(line) => line.origin(),
}
}
/// Convert a point on the path into surface coordinates
pub fn point_from_path_coords(
&self,
point: impl Into<Point<1>>,
) -> Point<D> {
match self {
Self::Circle(circle) => circle.point_from_circle_coords(point),
Self::Line(line) => line.point_from_line_coords(point),
}
}
/// Convert a vector on the path into global coordinates
pub fn vector_from_path_coords(
&self,
vector: impl Into<Vector<1>>,
) -> Vector<D> {
match self {
Self::Circle(circle) => circle.vector_from_circle_coords(vector),
Self::Line(line) => line.vector_from_line_coords(vector),
}
}
/// Create a new path that is the reverse of this one
#[must_use]
pub fn reverse(self) -> Self {
match self {
Self::Circle(circle) => Self::Circle(circle.reverse()),
Self::Line(line) => Self::Line(line.reverse()),
}
}
}