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

Improve Line conversion methods #563

Merged
merged 2 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fj_math::{Aabb, Line, Point, Scalar, Segment, Vector};
use fj_math::{Aabb, Line, Scalar, Segment, Vector};

/// Determine the intersection between a [`Line`] and a [`Segment`]
pub fn line_segment(
Expand Down Expand Up @@ -34,7 +34,7 @@ pub fn line_segment(
let t = n_dot_origin / n_dot_direction;

let point_is_on_segment = Aabb::<2>::from_points(segment.points())
.contains(line.convert_point_from_line_coords(&Point::from([t])));
.contains(line.point_from_line_coords([t]));
if !point_is_on_segment {
return None;
}
Expand Down
6 changes: 3 additions & 3 deletions crates/fj-kernel/src/geometry/curves/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,23 @@ impl Curve {
pub fn point_model_to_curve(&self, point: &Point<3>) -> Point<1> {
match self {
Self::Circle(curve) => curve.point_model_to_curve(point),
Self::Line(curve) => curve.convert_point_to_line_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> {
match self {
Self::Circle(curve) => curve.point_curve_to_model(point),
Self::Line(curve) => curve.convert_point_from_line_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> {
match self {
Self::Circle(curve) => curve.vector_curve_to_model(point),
Self::Line(curve) => curve.convert_vector_from_line_coords(point),
Self::Line(curve) => curve.vector_from_line_coords(*point),
}
}
}
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/geometry/surfaces/swept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl SweptCurve {
direction: self.path,
};

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

Point::from([u, v])
Expand Down
27 changes: 15 additions & 12 deletions crates/fj-math/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,36 @@ impl<const D: usize> Line<D> {
/// Callers are advised to be careful about the points they pass, as the
/// point not being on the line, intentional or not, will never result in an
/// error.
pub fn convert_point_to_line_coords(&self, point: &Point<D>) -> Point<1> {
pub fn point_to_line_coords(&self, point: impl Into<Point<D>>) -> Point<1> {
Point {
coords: self.convert_vector_to_line_coords(&(point - self.origin)),
coords: self.vector_to_line_coords(point.into() - self.origin),
}
}

/// Convert a `D`-dimensional vector to line coordinates
pub fn convert_vector_to_line_coords(
pub fn vector_to_line_coords(
&self,
vector: &Vector<D>,
vector: impl Into<Vector<D>>,
) -> Vector<1> {
let t = vector.scalar_projection_onto(&self.direction)
let t = vector.into().scalar_projection_onto(&self.direction)
/ self.direction.magnitude();
Vector::from([t])
}

/// Convert a point in line coordinates into a `D`-dimensional point
pub fn convert_point_from_line_coords(&self, point: &Point<1>) -> Point<D> {
self.origin + self.convert_vector_from_line_coords(&point.coords)
pub fn point_from_line_coords(
&self,
point: impl Into<Point<1>>,
) -> Point<D> {
self.origin + self.vector_from_line_coords(point.into().coords)
}

/// Convert a vector in line coordinates into a `D`-dimensional vector
pub fn convert_vector_from_line_coords(
pub fn vector_from_line_coords(
&self,
vector: &Vector<1>,
vector: impl Into<Vector<1>>,
) -> Vector<D> {
self.direction * vector.t
self.direction * vector.into().t
}
}

Expand Down Expand Up @@ -108,8 +111,8 @@ mod tests {
verify(line, 2.);

fn verify(line: Line<3>, t: f64) {
let point = line.convert_point_from_line_coords(&Point::from([t]));
let t_result = line.convert_point_to_line_coords(&point);
let point = line.point_from_line_coords([t]);
let t_result = line.point_to_line_coords(point);

assert_eq!(Point::from([t]), t_result);
}
Expand Down