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

Make PartialHalfEdge more flexible #1212

Merged
merged 6 commits into from
Oct 12, 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
6 changes: 4 additions & 2 deletions crates/fj-kernel/src/partial/maybe_partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ impl MaybePartial<GlobalEdge> {

impl MaybePartial<HalfEdge> {
/// Access the vertices
pub fn vertices(&self) -> Option<[MaybePartial<Vertex>; 2]> {
pub fn vertices(&self) -> [Option<MaybePartial<Vertex>>; 2] {
match self {
Self::Full(full) => Some(full.vertices().clone().map(Into::into)),
Self::Full(full) => {
full.vertices().clone().map(|vertex| Some(vertex.into()))
}
Self::Partial(partial) => partial.vertices.clone(),
}
}
Expand Down
22 changes: 12 additions & 10 deletions crates/fj-kernel/src/partial/objects/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ impl PartialCycle {
.half_edges
.last()
.map(|half_edge| {
let [_, last] = half_edge.vertices().expect(
"Need half-edge vertices to extend cycle with poly-chain",
);
let last = last.surface_form().expect(
"Need surface vertex to extend cycle with poly-chain",
);
let [_, last] = half_edge.vertices().map(|vertex| {
vertex.expect("Need half-edge vertices to extend cycle")
});
let last = last
.surface_form()
.expect("Need surface vertex to extend cycle");

let vertex = last.clone();
let position = last.position().expect(
"Need surface position to extend cycle with poly-chain",
);
let position = last
.position()
.expect("Need surface position to extend cycle");

(position, Some(vertex))
})
Expand Down Expand Up @@ -129,7 +129,9 @@ impl PartialCycle {

let vertices = [first, last].map(|option| {
option.map(|half_edge| {
half_edge.vertices().expect("Need vertices to close cycle")
half_edge
.vertices()
.map(|vertex| vertex.expect("Need vertices to close cycle"))
})
});

Expand Down
67 changes: 45 additions & 22 deletions crates/fj-kernel/src/partial/objects/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct PartialHalfEdge {
pub curve: Option<MaybePartial<Handle<Curve>>>,

/// The vertices that bound this [`HalfEdge`] in the [`Curve`]
pub vertices: Option<[MaybePartial<Vertex>; 2]>,
pub vertices: [Option<MaybePartial<Vertex>>; 2],

/// The global form of the [`HalfEdge`]
///
Expand Down Expand Up @@ -60,13 +60,38 @@ impl PartialHalfEdge {
self
}

/// Update the partial half-edge with the given from vertex
pub fn with_from_vertex(
mut self,
vertex: Option<impl Into<MaybePartial<Vertex>>>,
) -> Self {
if let Some(vertex) = vertex {
let [from, _] = &mut self.vertices;
*from = Some(vertex.into());
}
self
}

/// Update the partial half-edge with the given from vertex
pub fn with_to_vertex(
mut self,
vertex: Option<impl Into<MaybePartial<Vertex>>>,
) -> Self {
if let Some(vertex) = vertex {
let [_, to] = &mut self.vertices;
*to = Some(vertex.into());
}
self
}

/// Update the partial half-edge with the given vertices
pub fn with_vertices(
mut self,
vertices: Option<[impl Into<MaybePartial<Vertex>>; 2]>,
) -> Self {
if let Some(vertices) = vertices {
self.vertices = Some(vertices.map(Into::into));
let vertices = vertices.map(|vertices| vertices.map(Into::into));
if let Some([a, b]) = vertices {
self.vertices = [Some(a), Some(b)];
}
self
}
Expand All @@ -89,7 +114,7 @@ impl PartialHalfEdge {
.with_surface(self.surface.clone())
.as_circle_from_radius(radius);

let vertices = {
let [a, b] = {
let [a_curve, b_curve] =
[Scalar::ZERO, Scalar::TAU].map(|coord| Point::from([coord]));

Expand All @@ -106,11 +131,12 @@ impl PartialHalfEdge {
.with_position(Some(point_curve))
.with_curve(Some(curve.clone()))
.with_surface_form(Some(surface_form.clone()))
.into()
})
};

self.curve = Some(curve.into());
self.vertices = Some(vertices.map(Into::into));
self.vertices = [Some(a), Some(b)];

self
}
Expand All @@ -133,10 +159,9 @@ impl PartialHalfEdge {

/// Update partial half-edge as a line segment, reusing existing vertices
pub fn as_line_segment(mut self) -> Self {
let [from, to] = self
.vertices
.clone()
.expect("Can't infer line segment without vertices");
let [from, to] = self.vertices.clone().map(|vertex| {
vertex.expect("Can't infer line segment without vertices")
});
let [from_surface, to_surface] = [&from, &to].map(|vertex| {
vertex
.surface_form()
Expand All @@ -161,7 +186,7 @@ impl PartialHalfEdge {
.with_surface(Some(surface))
.as_line_from_points(points);

let vertices = [(from, 0.), (to, 1.)].map(|(vertex, position)| {
let [a, b] = [(from, 0.), (to, 1.)].map(|(vertex, position)| {
vertex.update_partial(|vertex| {
vertex
.with_position(Some([position]))
Expand All @@ -170,7 +195,7 @@ impl PartialHalfEdge {
});

self.curve = Some(curve.into());
self.vertices = Some(vertices);
self.vertices = [Some(a), Some(b)];

self
}
Expand All @@ -183,16 +208,12 @@ impl PartialHalfEdge {
.expect("Can't build `HalfEdge` without curve")
.update_partial(|curve| curve.with_surface(surface))
.into_full(objects);
let vertices = self
.vertices
.expect("Can't build `HalfEdge` without vertices")
.map(|vertex| {
vertex
.update_partial(|vertex| {
vertex.with_curve(Some(curve.clone()))
})
.into_full(objects)
});
let vertices = self.vertices.map(|vertex| {
vertex
.expect("Can't build `HalfEdge` without vertices")
.update_partial(|vertex| vertex.with_curve(Some(curve.clone())))
.into_full(objects)
});

let global_form = self
.global_form
Expand All @@ -208,10 +229,12 @@ impl PartialHalfEdge {

impl From<&HalfEdge> for PartialHalfEdge {
fn from(half_edge: &HalfEdge) -> Self {
let [a, b] = half_edge.vertices().clone().map(Into::into);

Self {
surface: Some(half_edge.curve().surface().clone()),
curve: Some(half_edge.curve().clone().into()),
vertices: Some(half_edge.vertices().clone().map(Into::into)),
vertices: [Some(a), Some(b)],
global_form: Some(half_edge.global_form().clone().into()),
}
}
Expand Down