Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update conversion API for geometry types #564

Merged
merged 12 commits into from
May 11, 2022
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/approx/curves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn approx_circle(

for i in 0..n {
let angle = Scalar::PI * 2. / n as f64 * i as f64;
let point = circle.point_curve_to_model(&Point::from([angle]));
let point = circle.point_from_circle_coords([angle]);
out.push(point);
}
}
Expand Down
10 changes: 3 additions & 7 deletions crates/fj-kernel/src/algorithms/triangulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@ pub fn triangulate(
.map(|vertex| {
// Can't panic, unless the approximation wrongfully
// generates points that are not in the surface.
surface.convert_point_to_surface_coords(vertex)
surface.point_to_surface_coords(vertex)
})
.collect();
let face_as_polygon = Polygon::new(surface)
.with_exterior(approx.exterior.points.into_iter().map(
|point| {
// Can't panic, unless the approximation wrongfully
// generates points that are not in the surface.
surface
.convert_point_to_surface_coords(point)
.native()
surface.point_to_surface_coords(point).native()
},
))
.with_interiors(approx.interiors.into_iter().map(
Expand All @@ -51,9 +49,7 @@ pub fn triangulate(
// Can't panic, unless the approximation
// wrongfully generates points that are not in
// the surface.
surface
.convert_point_to_surface_coords(point)
.native()
surface.point_to_surface_coords(point).native()
})
},
));
Expand Down
5 changes: 2 additions & 3 deletions crates/fj-kernel/src/algorithms/triangulation/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Polygon {
};

let mut check = TriangleEdgeCheck::new(
self.surface.convert_point_from_surface_coords(&ray.origin),
self.surface.point_from_surface_coords(ray.origin),
);

let mut num_hits = 0;
Expand Down Expand Up @@ -201,8 +201,7 @@ impl Polygon {

let edge =
Segment::from_points(edge.points().map(|point| {
self.surface
.convert_point_from_surface_coords(&point)
self.surface.point_from_surface_coords(point)
}));
check.hits.push(edge);
}
Expand Down
29 changes: 19 additions & 10 deletions crates/fj-kernel/src/geometry/curves/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ impl Circle {
/// Callers are advised to be careful about the points they pass, as the
/// point not being on the curve, intentional or not, will not result in an
/// error.
pub fn point_model_to_curve(&self, point: &Point<3>) -> Point<1> {
let v = point - self.center;
pub fn point_to_circle_coords(
&self,
point: impl Into<Point<3>>,
) -> Point<1> {
let v = point.into() - self.center;
let atan = Scalar::atan2(v.y, v.x);
let coord = if atan >= Scalar::ZERO {
atan
Expand All @@ -67,13 +70,19 @@ impl Circle {
}

/// Convert a point on the curve into model coordinates
pub fn point_curve_to_model(&self, point: &Point<1>) -> Point<3> {
self.center + self.vector_curve_to_model(&point.coords)
pub fn point_from_circle_coords(
&self,
point: impl Into<Point<1>>,
) -> Point<3> {
self.center + self.vector_from_circle_coords(point.into().coords)
}

/// Convert a vector on the curve into model coordinates
pub fn vector_curve_to_model(&self, vector: &Vector<1>) -> Vector<3> {
let angle = vector.t;
pub fn vector_from_circle_coords(
&self,
vector: impl Into<Vector<1>>,
) -> Vector<3> {
let angle = vector.into().t;
let (sin, cos) = angle.sin_cos();

self.a * cos + self.b * sin
Expand All @@ -97,19 +106,19 @@ mod tests {
};

assert_eq!(
circle.point_model_to_curve(&Point::from([2., 2., 3.])),
circle.point_to_circle_coords([2., 2., 3.]),
Point::from([0.]),
);
assert_eq!(
circle.point_model_to_curve(&Point::from([1., 3., 3.])),
circle.point_to_circle_coords([1., 3., 3.]),
Point::from([FRAC_PI_2]),
);
assert_eq!(
circle.point_model_to_curve(&Point::from([0., 2., 3.])),
circle.point_to_circle_coords([0., 2., 3.]),
Point::from([PI]),
);
assert_eq!(
circle.point_model_to_curve(&Point::from([1., 1., 3.])),
circle.point_to_circle_coords([1., 1., 3.]),
Point::from([FRAC_PI_2 * 3.]),
);
}
Expand Down
27 changes: 18 additions & 9 deletions crates/fj-kernel/src/geometry/curves/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,35 @@ impl Curve {
/// Callers are advised to be careful about the points they pass, as the
/// point not being on the curve, intentional or not, will never result in
/// an error.
pub fn point_model_to_curve(&self, point: &Point<3>) -> Point<1> {
pub fn point_to_curve_coords(
&self,
point: impl Into<Point<3>>,
) -> Point<1> {
match self {
Self::Circle(curve) => curve.point_model_to_curve(point),
Self::Line(curve) => curve.point_to_line_coords(*point),
Self::Circle(curve) => curve.point_to_circle_coords(point),
Self::Line(curve) => curve.point_to_line_coords(point),
}
}

/// Convert a point on the curve into model coordinates
pub fn point_curve_to_model(&self, point: &Point<1>) -> Point<3> {
pub fn point_from_curve_coords(
&self,
point: impl Into<Point<1>>,
) -> Point<3> {
match self {
Self::Circle(curve) => curve.point_curve_to_model(point),
Self::Line(curve) => curve.point_from_line_coords(*point),
Self::Circle(curve) => curve.point_from_circle_coords(point),
Self::Line(curve) => curve.point_from_line_coords(point),
}
}

/// Convert a vector on the curve into model coordinates
pub fn vector_curve_to_model(&self, point: &Vector<1>) -> Vector<3> {
pub fn vector_from_curve_coords(
&self,
point: impl Into<Vector<1>>,
) -> Vector<3> {
match self {
Self::Circle(curve) => curve.vector_curve_to_model(point),
Self::Line(curve) => curve.vector_from_line_coords(*point),
Self::Circle(curve) => curve.vector_from_circle_coords(point),
Self::Line(curve) => curve.vector_from_line_coords(point),
}
}
}
20 changes: 11 additions & 9 deletions crates/fj-kernel/src/geometry/surfaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,41 @@ impl Surface {
}

/// Convert a point in model coordinates to surface coordinates
pub fn convert_point_to_surface_coords(
pub fn point_to_surface_coords(
&self,
point_3d: Point<3>,
point_3d: impl Into<Point<3>>,
) -> geometry::Point<2> {
let point_3d = point_3d.into();

let point_2d = match self {
Self::SweptCurve(surface) => {
surface.convert_point_to_surface_coords(&point_3d)
surface.point_to_surface_coords(point_3d)
}
};

geometry::Point::new(point_2d, point_3d)
}

/// Convert a point in surface coordinates to model coordinates
pub fn convert_point_from_surface_coords(
pub fn point_from_surface_coords(
&self,
point: &Point<2>,
point: impl Into<Point<2>>,
) -> Point<3> {
match self {
Self::SweptCurve(surface) => {
surface.convert_point_from_surface_coords(point)
surface.point_from_surface_coords(point)
}
}
}

/// Convert a vector in surface coordinates to model coordinates
pub fn convert_vector_from_surface_coords(
pub fn vector_from_surface_coords(
&self,
vector: &Vector<2>,
vector: impl Into<Vector<2>>,
) -> Vector<3> {
match self {
Self::SweptCurve(surface) => {
surface.convert_vector_from_surface_coords(vector)
surface.vector_from_surface_coords(vector)
}
}
}
Expand Down
32 changes: 18 additions & 14 deletions crates/fj-kernel/src/geometry/surfaces/swept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,41 @@ impl SweptCurve {
}

/// Convert a point in model coordinates to surface coordinates
pub fn convert_point_to_surface_coords(
pub fn point_to_surface_coords(
&self,
point: &Point<3>,
point: impl Into<Point<3>>,
) -> Point<2> {
let u = self.curve.point_model_to_curve(point).t;
let point = point.into();

let u = self.curve.point_to_curve_coords(point).t;
let v = {
let line = Line {
origin: self.curve.origin(),
direction: self.path,
};

line.point_to_line_coords(*point).t
line.point_to_line_coords(point).t
};

Point::from([u, v])
}

/// Convert a point in surface coordinates to model coordinates
pub fn convert_point_from_surface_coords(
pub fn point_from_surface_coords(
&self,
point: &Point<2>,
point: impl Into<Point<2>>,
) -> Point<3> {
self.curve.point_curve_to_model(&point.to_t()) + self.path * point.v
let point = point.into();
self.curve.point_from_curve_coords([point.u]) + self.path * point.v
}

/// Convert a vector in surface coordinates to model coordinates
pub fn convert_vector_from_surface_coords(
pub fn vector_from_surface_coords(
&self,
vector: &Vector<2>,
vector: impl Into<Vector<2>>,
) -> Vector<3> {
self.curve.vector_curve_to_model(&vector.to_t()) + self.path * vector.v
let vector = vector.into();
self.curve.vector_from_curve_coords([vector.u]) + self.path * vector.v
}
}

Expand Down Expand Up @@ -97,8 +101,8 @@ mod tests {
verify(&swept, Point::from([2., 3.]));

fn verify(swept: &SweptCurve, surface_point: Point<2>) {
let point = swept.convert_point_from_surface_coords(&surface_point);
let result = swept.convert_point_to_surface_coords(&point);
let point = swept.point_from_surface_coords(surface_point);
let result = swept.point_to_surface_coords(point);

assert_eq!(result, surface_point);
}
Expand All @@ -115,7 +119,7 @@ mod tests {
};

assert_eq!(
swept.convert_point_from_surface_coords(&Point::from([2., 4.])),
swept.point_from_surface_coords([2., 4.]),
Point::from([1., 4., 8.]),
);
}
Expand All @@ -131,7 +135,7 @@ mod tests {
};

assert_eq!(
swept.convert_vector_from_surface_coords(&Vector::from([2., 4.])),
swept.vector_from_surface_coords([2., 4.]),
Vector::from([0., 4., 8.]),
);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-operations/src/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl ToShape for fj::Sketch {
.to_points()
.into_iter()
.map(Point::from)
.map(|point| surface.convert_point_from_surface_coords(&point));
.map(|point| surface.point_from_surface_coords(point));

Face::builder(surface, &mut shape)
.with_exterior_polygon(points)
Expand Down