From 3798e3f709d4a9876c544601c7f2a9d300644f9b Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 15 Aug 2024 20:02:13 +0200 Subject: [PATCH] Share methods between `Path<2>` and `Path<3>` --- crates/fj-core/src/geometry/path.rs | 165 ++++++++++++---------------- 1 file changed, 73 insertions(+), 92 deletions(-) diff --git a/crates/fj-core/src/geometry/path.rs b/crates/fj-core/src/geometry/path.rs index cc2c4cb4e..fd0f6ac60 100644 --- a/crates/fj-core/src/geometry/path.rs +++ b/crates/fj-core/src/geometry/path.rs @@ -15,14 +15,6 @@ pub enum Path { } impl Path<2> { - /// Build a circle from the given radius - pub fn circle_from_center_and_radius( - center: impl Into>, - radius: impl Into, - ) -> Self { - Self::Circle(Circle::from_center_and_radius(center, radius)) - } - /// Build a line that represents the u-axis of the surface its on pub fn u_axis() -> Self { let a = Point::origin(); @@ -40,43 +32,6 @@ impl Path<2> { let (self_, _) = Self::line_from_points([a, b]); 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>; 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>, impl Into>); 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<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> { @@ -104,53 +59,6 @@ impl Path<3> { )) } - /// Build a circle from the given radius - pub fn circle_from_radius(radius: impl Into) -> 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>; 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<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<3> { - match self { - Self::Circle(circle) => circle.vector_from_circle_coords(vector), - Self::Line(line) => line.vector_from_line_coords(vector), - } - } - /// Transform the path #[must_use] pub fn transform(self, transform: &Transform) -> Self { @@ -162,3 +70,76 @@ impl Path<3> { } } } + +impl Path { + /// Build a circle from the given radius + pub fn circle_from_center_and_radius( + center: impl Into>, + radius: impl Into, + ) -> 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) -> 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>; 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>, impl Into>); 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 { + 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 { + 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 { + 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()), + } + } +}