Skip to content

Commit

Permalink
Merge pull request #561 from hannobraun/surface
Browse files Browse the repository at this point in the history
Update the surface API for point/vector conversion
  • Loading branch information
hannobraun authored May 10, 2022
2 parents 96d9f65 + a4d7004 commit f5d7102
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 29 deletions.
10 changes: 7 additions & 3 deletions crates/fj-kernel/src/algorithms/triangulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ pub fn triangulate(
.map(|vertex| {
// Can't panic, unless the approximation wrongfully
// generates points that are not in the surface.
surface.point_model_to_surface(vertex)
surface.convert_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.point_model_to_surface(point).native()
surface
.convert_point_to_surface_coords(point)
.native()
},
))
.with_interiors(approx.interiors.into_iter().map(
Expand All @@ -49,7 +51,9 @@ pub fn triangulate(
// Can't panic, unless the approximation
// wrongfully generates points that are not in
// the surface.
surface.point_model_to_surface(point).native()
surface
.convert_point_to_surface_coords(point)
.native()
})
},
));
Expand Down
5 changes: 3 additions & 2 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.point_surface_to_model(&ray.origin),
self.surface.convert_point_from_surface_coords(&ray.origin),
);

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

let edge =
Segment::from_points(edge.points().map(|point| {
self.surface.point_surface_to_model(&point)
self.surface
.convert_point_from_surface_coords(&point)
}));
check.hits.push(edge);
}
Expand Down
20 changes: 14 additions & 6 deletions crates/fj-kernel/src/geometry/surfaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,39 @@ impl Surface {
}

/// Convert a point in model coordinates to surface coordinates
pub fn point_model_to_surface(
pub fn convert_point_to_surface_coords(
&self,
point_3d: Point<3>,
) -> geometry::Point<2> {
let point_2d = match self {
Self::SweptCurve(surface) => {
surface.point_model_to_surface(&point_3d)
surface.convert_point_to_surface_coords(&point_3d)
}
};

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

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

/// Convert a vector in surface coordinates to model coordinates
pub fn vector_surface_to_model(&self, vector: &Vector<2>) -> Vector<3> {
pub fn convert_vector_from_surface_coords(
&self,
vector: &Vector<2>,
) -> Vector<3> {
match self {
Self::SweptCurve(surface) => {
surface.vector_surface_to_model(vector)
surface.convert_vector_from_surface_coords(vector)
}
}
}
Expand Down
46 changes: 29 additions & 17 deletions crates/fj-kernel/src/geometry/surfaces/swept.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fj_math::{Point, Transform, Vector};
use fj_math::{Line, Point, Transform, Vector};

use crate::geometry::Curve;

Expand All @@ -16,8 +16,6 @@ impl SweptCurve {
/// Construct a plane from 3 points
#[cfg(test)]
pub fn plane_from_points([a, b, c]: [Point<3>; 3]) -> Self {
use fj_math::Line;

let curve = Curve::Line(Line::from_points([a, b]));
let path = c - a;

Expand All @@ -40,22 +38,36 @@ impl SweptCurve {
}

/// Convert a point in model coordinates to surface coordinates
pub fn point_model_to_surface(&self, point: &Point<3>) -> Point<2> {
pub fn convert_point_to_surface_coords(
&self,
point: &Point<3>,
) -> Point<2> {
let u = self.curve.point_model_to_curve(point).t;
let v = (point - self.curve.origin())
.scalar_projection_onto(&self.path)
/ self.path.magnitude();
let v = {
let line = Line {
origin: self.curve.origin(),
direction: self.path,
};

line.convert_point_to_line_coords(point).t
};

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

/// Convert a point in surface coordinates to model coordinates
pub fn point_surface_to_model(&self, point: &Point<2>) -> Point<3> {
pub fn convert_point_from_surface_coords(
&self,
point: &Point<2>,
) -> Point<3> {
self.curve.point_curve_to_model(&point.to_t()) + self.path * point.v
}

/// Convert a vector in surface coordinates to model coordinates
pub fn vector_surface_to_model(&self, vector: &Vector<2>) -> Vector<3> {
pub fn convert_vector_from_surface_coords(
&self,
vector: &Vector<2>,
) -> Vector<3> {
self.curve.vector_curve_to_model(&vector.to_t()) + self.path * vector.v
}
}
Expand All @@ -70,13 +82,13 @@ mod tests {
use super::SweptCurve;

#[test]
fn point_model_to_surface() {
fn convert_point_to_surface_coords() {
let swept = SweptCurve {
curve: Curve::Line(Line {
origin: Point::from([1., 0., 0.]),
direction: Vector::from([0., 2., 0.]),
}),
path: Vector::from([0., 0., 2.]),
path: Vector::from([0., 0., 3.]),
};

verify(&swept, Point::from([-1., -1.]));
Expand All @@ -85,15 +97,15 @@ mod tests {
verify(&swept, Point::from([2., 3.]));

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

assert_eq!(result, surface_point);
}
}

#[test]
fn point_surface_to_model() {
fn convert_point_from_surface_coords() {
let swept = SweptCurve {
curve: Curve::Line(Line {
origin: Point::from([1., 0., 0.]),
Expand All @@ -103,13 +115,13 @@ mod tests {
};

assert_eq!(
swept.point_surface_to_model(&Point::from([2., 4.])),
swept.convert_point_from_surface_coords(&Point::from([2., 4.])),
Point::from([1., 4., 8.]),
);
}

#[test]
fn vector_surface_to_model() {
fn convert_vector_from_surface_coords() {
let swept = SweptCurve {
curve: Curve::Line(Line {
origin: Point::from([1., 0., 0.]),
Expand All @@ -119,7 +131,7 @@ mod tests {
};

assert_eq!(
swept.vector_surface_to_model(&Vector::from([2., 4.])),
swept.convert_vector_from_surface_coords(&Vector::from([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.point_surface_to_model(&point));
.map(|point| surface.convert_point_from_surface_coords(&point));

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

0 comments on commit f5d7102

Please sign in to comment.