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

Continue cleanup of partial object API #1310

Merged
merged 24 commits into from
Nov 4, 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
18 changes: 10 additions & 8 deletions crates/fj-kernel/src/algorithms/intersect/curve_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ mod tests {
.update_as_u_axis()
.build(&objects)?;
let half_edge = HalfEdge::partial()
.with_surface(Some(surface))
.update_as_line_segment_from_points([[1., -1.], [1., 1.]])
.update_as_line_segment_from_points(surface, [[1., -1.], [1., 1.]])
.build(&objects)?;

let intersection = CurveEdgeIntersection::compute(&curve, &half_edge);
Expand All @@ -117,8 +116,10 @@ mod tests {
.update_as_u_axis()
.build(&objects)?;
let half_edge = HalfEdge::partial()
.with_surface(Some(surface))
.update_as_line_segment_from_points([[-1., -1.], [-1., 1.]])
.update_as_line_segment_from_points(
surface,
[[-1., -1.], [-1., 1.]],
)
.build(&objects)?;

let intersection = CurveEdgeIntersection::compute(&curve, &half_edge);
Expand All @@ -142,8 +143,10 @@ mod tests {
.update_as_u_axis()
.build(&objects)?;
let half_edge = HalfEdge::partial()
.with_surface(Some(surface))
.update_as_line_segment_from_points([[-1., -1.], [1., -1.]])
.update_as_line_segment_from_points(
surface,
[[-1., -1.], [1., -1.]],
)
.build(&objects)?;

let intersection = CurveEdgeIntersection::compute(&curve, &half_edge);
Expand All @@ -162,8 +165,7 @@ mod tests {
.update_as_u_axis()
.build(&objects)?;
let half_edge = HalfEdge::partial()
.with_surface(Some(surface))
.update_as_line_segment_from_points([[-1., 0.], [1., 0.]])
.update_as_line_segment_from_points(surface, [[-1., 0.], [1., 0.]])
.build(&objects)?;

let intersection = CurveEdgeIntersection::compute(&curve, &half_edge);
Expand Down
12 changes: 8 additions & 4 deletions crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,10 @@ mod tests {
let objects = Objects::new();

let half_edge = HalfEdge::partial()
.with_surface(Some(objects.surfaces.xy_plane()))
.update_as_line_segment_from_points([[0., 0.], [1., 0.]])
.update_as_line_segment_from_points(
objects.surfaces.xy_plane(),
[[0., 0.], [1., 0.]],
)
.build(&objects)?;

let face =
Expand All @@ -210,8 +212,10 @@ mod tests {
let surface = objects.surfaces.xz_plane();

let bottom = HalfEdge::partial()
.with_surface(Some(surface.clone()))
.update_as_line_segment_from_points([[0., 0.], [1., 0.]])
.update_as_line_segment_from_points(
surface.clone(),
[[0., 0.], [1., 0.]],
)
.build(&objects)?;
let side_up = HalfEdge::partial()
.with_surface(Some(surface.clone()))
Expand Down
12 changes: 8 additions & 4 deletions crates/fj-kernel/src/algorithms/sweep/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ mod tests {
.array_windows_ext()
.map(|&[a, b]| {
let half_edge = HalfEdge::partial()
.with_surface(Some(objects.surfaces.xy_plane()))
.update_as_line_segment_from_points([a, b])
.update_as_line_segment_from_points(
objects.surfaces.xy_plane(),
[a, b],
)
.build(&objects)?;
(half_edge, Color::default()).sweep(UP, &objects)
})
Expand Down Expand Up @@ -167,8 +169,10 @@ mod tests {
.array_windows_ext()
.map(|&[a, b]| {
let half_edge = HalfEdge::partial()
.with_surface(Some(objects.surfaces.xy_plane()))
.update_as_line_segment_from_points([a, b])
.update_as_line_segment_from_points(
objects.surfaces.xy_plane(),
[a, b],
)
.build(&objects)?
.reverse(&objects)?;
(half_edge, Color::default()).sweep(DOWN, &objects)
Expand Down
3 changes: 1 addition & 2 deletions crates/fj-kernel/src/algorithms/sweep/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ mod tests {
(vertex, surface.clone()).sweep([0., 0., 1.], &objects)?;

let expected_half_edge = HalfEdge::partial()
.with_surface(Some(surface))
.update_as_line_segment_from_points([[0., 0.], [0., 1.]])
.update_as_line_segment_from_points(surface, [[0., 0.], [0., 1.]])
.build(&objects)?;
assert_eq!(half_edge, expected_half_edge);
Ok(())
Expand Down
15 changes: 2 additions & 13 deletions crates/fj-kernel/src/algorithms/transform/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,11 @@ impl TransformObject for PartialCycle {
transform: &Transform,
objects: &Objects,
) -> Result<Self, ValidationError> {
let surface = self
.surface()
.map(|surface| surface.transform(transform, objects))
.transpose()?;
let half_edges = self
.half_edges()
.map(|edge| {
Ok(edge
.into_partial()
.transform(transform, objects)?
.with_surface(surface.clone()))
})
.map(|edge| edge.into_partial().transform(transform, objects))
.collect::<Result<Vec<_>, ValidationError>>()?;

Ok(Self::default()
.with_surface(surface)
.with_half_edges(half_edges))
Ok(Self::default().with_half_edges(half_edges))
}
}
6 changes: 0 additions & 6 deletions crates/fj-kernel/src/algorithms/transform/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ impl TransformObject for PartialHalfEdge {
transform: &Transform,
objects: &Objects,
) -> Result<Self, ValidationError> {
let surface = self
.surface()
.map(|surface| surface.transform(transform, objects))
.transpose()?;
let curve: MaybePartial<_> = self
.curve()
.into_partial()
.transform(transform, objects)?
.with_surface(surface.clone())
.into();
let vertices = self.vertices().try_map_ext(
|vertex| -> Result<_, ValidationError> {
Expand All @@ -41,7 +36,6 @@ impl TransformObject for PartialHalfEdge {
.into();

Ok(Self::default()
.with_surface(surface)
.with_curve(Some(curve))
.with_vertices(Some(vertices))
.with_global_form(global_form))
Expand Down
23 changes: 11 additions & 12 deletions crates/fj-kernel/src/builder/cycle.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use fj_math::Point;

use crate::{
objects::{Curve, HalfEdge, SurfaceVertex, Vertex},
objects::{Curve, HalfEdge, Surface, SurfaceVertex, Vertex},
partial::{HasPartial, MaybePartial, PartialCycle},
storage::Handle,
};

use super::{CurveBuilder, HalfEdgeBuilder};
Expand All @@ -18,6 +19,7 @@ pub trait CycleBuilder {
/// Update the partial cycle with a polygonal chain from the provided points
fn with_poly_chain_from_points(
self,
surface: Handle<Surface>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self;

Expand Down Expand Up @@ -49,9 +51,8 @@ impl CycleBuilder for PartialCycle {
let mut half_edges = Vec::new();
for vertex_next in iter {
if let Some(vertex_prev) = previous {
let surface = self
let surface = vertex_prev
.surface()
.clone()
.expect("Need surface to extend cycle with poly-chain");

let position_prev = vertex_prev
Expand All @@ -61,12 +62,8 @@ impl CycleBuilder for PartialCycle {
.position()
.expect("Need surface position to extend cycle");

let from = vertex_prev.update_partial(|partial| {
partial.with_surface(Some(surface.clone()))
});
let to = vertex_next.update_partial(|partial| {
partial.with_surface(Some(surface.clone()))
});
let from = vertex_prev;
let to = vertex_next;

previous = Some(to.clone());

Expand Down Expand Up @@ -99,10 +96,13 @@ impl CycleBuilder for PartialCycle {

fn with_poly_chain_from_points(
self,
surface: Handle<Surface>,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self {
self.with_poly_chain(points.into_iter().map(|position| {
SurfaceVertex::partial().with_position(Some(position))
SurfaceVertex::partial()
.with_surface(Some(surface.clone()))
.with_position(Some(position))
}))
}

Expand All @@ -127,8 +127,7 @@ impl CycleBuilder for PartialCycle {

self.with_half_edges(Some(
HalfEdge::partial()
.with_surface(Some(surface))
.update_as_line_segment_from_points(vertices),
.update_as_line_segment_from_points(surface, vertices),
))
}
}
25 changes: 16 additions & 9 deletions crates/fj-kernel/src/builder/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use fj_math::{Point, Scalar};

use crate::{
objects::{
Curve, GlobalVertex, Objects, SurfaceVertex, Vertex,
Curve, GlobalVertex, Objects, Surface, SurfaceVertex, Vertex,
VerticesInNormalizedOrder,
},
partial::{HasPartial, PartialGlobalEdge, PartialHalfEdge},
Expand Down Expand Up @@ -31,6 +31,7 @@ pub trait HalfEdgeBuilder: Sized {
/// Update partial half-edge as a line segment, from the given points
fn update_as_line_segment_from_points(
self,
surface: Handle<Surface>,
points: [impl Into<Point<2>>; 2],
) -> Self;

Expand All @@ -44,9 +45,10 @@ impl HalfEdgeBuilder for PartialHalfEdge {
radius: impl Into<Scalar>,
objects: &Objects,
) -> Result<Self, ValidationError> {
let curve = Curve::partial()
let curve = self
.curve()
.into_partial()
.with_global_form(Some(self.extract_global_curve()))
.with_surface(self.surface())
.update_as_circle_from_radius(radius);

let path = curve.path().expect("Expected path that was just created");
Expand All @@ -55,7 +57,8 @@ impl HalfEdgeBuilder for PartialHalfEdge {
[Scalar::ZERO, Scalar::TAU].map(|coord| Point::from([coord]));

let global_vertex = self
.extract_global_vertices()
.global_form()
.vertices()
.map(|[global_form, _]| global_form)
.unwrap_or_else(|| {
GlobalVertex::partial()
Expand All @@ -65,7 +68,7 @@ impl HalfEdgeBuilder for PartialHalfEdge {

let surface_vertex = SurfaceVertex::partial()
.with_position(Some(path.point_from_path_coords(a_curve)))
.with_surface(self.surface())
.with_surface(curve.surface())
.with_global_form(Some(global_vertex))
.build(objects)?;

Expand All @@ -83,18 +86,20 @@ impl HalfEdgeBuilder for PartialHalfEdge {

fn update_as_line_segment_from_points(
self,
surface: Handle<Surface>,
points: [impl Into<Point<2>>; 2],
) -> Self {
let surface = self.surface();
let vertices = points.map(|point| {
let surface_form = SurfaceVertex::partial()
.with_surface(surface.clone())
.with_surface(Some(surface.clone()))
.with_position(Some(point));

Vertex::partial().with_surface_form(Some(surface_form))
});

self.with_vertices(Some(vertices)).update_as_line_segment()
self.with_surface(Some(surface))
.with_vertices(Some(vertices))
.update_as_line_segment()
}

fn update_as_line_segment(self) -> Self {
Expand All @@ -103,6 +108,7 @@ impl HalfEdgeBuilder for PartialHalfEdge {
[&from, &to].map(|vertex| vertex.surface_form());

let surface = self
.curve()
.surface()
.or_else(|| from_surface.surface())
.or_else(|| to_surface.surface())
Expand Down Expand Up @@ -147,7 +153,8 @@ impl HalfEdgeBuilder for PartialHalfEdge {
must_switch_order
};

self.extract_global_vertices()
self.global_form()
.vertices()
.map(
|[a, b]| {
if must_switch_order {
Expand Down
16 changes: 12 additions & 4 deletions crates/fj-kernel/src/builder/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ impl<'a> FaceBuilder<'a> {
mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self {
let surface = self
.surface
.as_ref()
.expect("Need surface to create polygon");

self.exterior = Some(
Cycle::partial()
.with_surface(self.surface.clone())
.with_poly_chain_from_points(points)
.with_poly_chain_from_points(surface.clone(), points)
.close_with_line_segment()
.build(self.objects)
.unwrap(),
Expand All @@ -75,10 +79,14 @@ impl<'a> FaceBuilder<'a> {
mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self {
let surface = self
.surface
.as_ref()
.expect("Need surface to build polygon.");

self.interiors.push(
Cycle::partial()
.with_surface(self.surface.clone())
.with_poly_chain_from_points(points)
.with_poly_chain_from_points(surface.clone(), points)
.close_with_line_segment()
.build(self.objects)
.unwrap(),
Expand Down
13 changes: 5 additions & 8 deletions crates/fj-kernel/src/builder/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ impl<'a> ShellBuilder<'a> {
.zip(&surfaces)
.map(|(half_edge, surface)| {
HalfEdge::partial()
.with_surface(Some(surface.clone()))
.with_global_form(Some(half_edge.global_form().clone()))
.update_as_line_segment_from_points([
[Z, Z],
[edge_length, Z],
])
.update_as_line_segment_from_points(
surface.clone(),
[[Z, Z], [edge_length, Z]],
)
.build(self.objects)
.unwrap()
})
Expand Down Expand Up @@ -188,10 +187,8 @@ impl<'a> ShellBuilder<'a> {
.zip(sides_up)
.zip(tops.clone())
.zip(sides_down)
.zip(surfaces)
.map(|((((bottom, side_up), top), side_down), surface)| {
.map(|(((bottom, side_up), top), side_down)| {
let cycle = Cycle::partial()
.with_surface(Some(surface))
.with_half_edges([bottom, side_up, top, side_down])
.build(self.objects)
.unwrap();
Expand Down
Loading