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

Add PartialCycle::with_poly_chain #1213

Merged
merged 1 commit into from
Oct 12, 2022
Merged
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
66 changes: 34 additions & 32 deletions crates/fj-kernel/src/partial/objects/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ impl PartialCycle {
}

/// Update the partial cycle with a polygonal chain from the provided points
pub fn with_poly_chain_from_points(
pub fn with_poly_chain(
mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
vertices: impl IntoIterator<Item = MaybePartial<SurfaceVertex>>,
) -> Self {
let iter = self
.half_edges
Expand All @@ -51,50 +51,40 @@ impl PartialCycle {
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");

(position, Some(vertex))
last.surface_form()
.expect("Need surface vertex to extend cycle")
})
.into_iter()
.chain(points.into_iter().map(|point| (point.into(), None)));
.chain(vertices);

let mut previous: Option<(
Point<2>,
Option<MaybePartial<SurfaceVertex>>,
)> = None;
let mut previous: Option<MaybePartial<SurfaceVertex>> = None;

for (position, vertex) in iter {
if let Some((previous_position, previous_vertex)) = previous {
for vertex_next in iter {
if let Some(vertex_prev) = previous {
let surface = self
.surface
.clone()
.expect("Need surface to extend cycle with poly-chain");

let from = previous_vertex.unwrap_or_else(|| {
SurfaceVertex::partial()
.with_surface(Some(surface.clone()))
.with_position(Some(previous_position))
.into()
let position_prev = vertex_prev
.position()
.expect("Need surface position to extend cycle");
let position_next = vertex_next
.position()
.expect("Need surface position to extend cycle");

let from = vertex_prev.update_partial(|partial| {
partial.with_surface(Some(surface.clone()))
});
let to = vertex.unwrap_or_else(|| {
SurfaceVertex::partial()
.with_surface(Some(surface.clone()))
.with_position(Some(position))
.into()
let to = vertex_next.update_partial(|partial| {
partial.with_surface(Some(surface.clone()))
});

previous = Some((position, Some(to.clone())));
previous = Some(to.clone());

let curve = Handle::<Curve>::partial()
.with_surface(Some(surface.clone()))
.as_line_from_points([previous_position, position]);
.as_line_from_points([position_prev, position_next]);

let [from, to] =
[(0., from), (1., to)].map(|(position, surface_form)| {
Expand All @@ -114,12 +104,24 @@ impl PartialCycle {
continue;
}

previous = Some((position, vertex));
previous = Some(vertex_next);
}

self
}

/// Update the partial cycle with a polygonal chain from the provided points
pub fn with_poly_chain_from_points(
self,
points: impl IntoIterator<Item = impl Into<Point<2>>>,
) -> Self {
self.with_poly_chain(points.into_iter().map(|position| {
SurfaceVertex::partial()
.with_position(Some(position))
.into()
}))
}

/// Update the partial cycle by closing it with a line segment
///
/// Builds a line segment from the last and first vertex, closing the cycle.
Expand Down