Merge pull request #1213 from hannobraun/partial

Add `PartialCycle::with_poly_chain`
This commit is contained in:
Hanno Braun 2022-10-12 16:43:34 +02:00 committed by GitHub
commit e7c007a4b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,9 +40,9 @@ impl PartialCycle {
} }
/// Update the partial cycle with a polygonal chain from the provided points /// 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, mut self,
points: impl IntoIterator<Item = impl Into<Point<2>>>, vertices: impl IntoIterator<Item = MaybePartial<SurfaceVertex>>,
) -> Self { ) -> Self {
let iter = self let iter = self
.half_edges .half_edges
@ -51,50 +51,40 @@ impl PartialCycle {
let [_, last] = half_edge.vertices().map(|vertex| { let [_, last] = half_edge.vertices().map(|vertex| {
vertex.expect("Need half-edge vertices to extend cycle") vertex.expect("Need half-edge vertices to extend cycle")
}); });
let last = last last.surface_form()
.surface_form() .expect("Need surface vertex to extend cycle")
.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))
}) })
.into_iter() .into_iter()
.chain(points.into_iter().map(|point| (point.into(), None))); .chain(vertices);
let mut previous: Option<( let mut previous: Option<MaybePartial<SurfaceVertex>> = None;
Point<2>,
Option<MaybePartial<SurfaceVertex>>,
)> = None;
for (position, vertex) in iter { for vertex_next in iter {
if let Some((previous_position, previous_vertex)) = previous { if let Some(vertex_prev) = previous {
let surface = self let surface = self
.surface .surface
.clone() .clone()
.expect("Need surface to extend cycle with poly-chain"); .expect("Need surface to extend cycle with poly-chain");
let from = previous_vertex.unwrap_or_else(|| { let position_prev = vertex_prev
SurfaceVertex::partial() .position()
.with_surface(Some(surface.clone())) .expect("Need surface position to extend cycle");
.with_position(Some(previous_position)) let position_next = vertex_next
.into() .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(|| { let to = vertex_next.update_partial(|partial| {
SurfaceVertex::partial() partial.with_surface(Some(surface.clone()))
.with_surface(Some(surface.clone()))
.with_position(Some(position))
.into()
}); });
previous = Some((position, Some(to.clone()))); previous = Some(to.clone());
let curve = Handle::<Curve>::partial() let curve = Handle::<Curve>::partial()
.with_surface(Some(surface.clone())) .with_surface(Some(surface.clone()))
.as_line_from_points([previous_position, position]); .as_line_from_points([position_prev, position_next]);
let [from, to] = let [from, to] =
[(0., from), (1., to)].map(|(position, surface_form)| { [(0., from), (1., to)].map(|(position, surface_form)| {
@ -114,12 +104,24 @@ impl PartialCycle {
continue; continue;
} }
previous = Some((position, vertex)); previous = Some(vertex_next);
} }
self 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 /// Update the partial cycle by closing it with a line segment
/// ///
/// Builds a line segment from the last and first vertex, closing the cycle. /// Builds a line segment from the last and first vertex, closing the cycle.