Skip to content

Commit

Permalink
Merge pull request #1419 from hannobraun/partial
Browse files Browse the repository at this point in the history
Make some cleanups in partial object API
  • Loading branch information
hannobraun authored Dec 5, 2022
2 parents 4e966cf + b74f977 commit decba7e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
17 changes: 17 additions & 0 deletions crates/fj-interop/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ pub trait ArrayExt<T, const N: usize> {
fn try_map_ext<F, U, E>(self, f: F) -> Result<[U; N], E>
where
F: FnMut(T) -> Result<U, E>;

/// Stable replacement for `zip`
///
/// <https://doc.rust-lang.org/std/primitive.array.html#method.zip>
fn zip_ext<U>(self, rhs: [U; N]) -> [(T, U); N];
}

impl<T> ArrayExt<T, 2> for [T; 2] {
Expand All @@ -28,6 +33,12 @@ impl<T> ArrayExt<T, 2> for [T; 2] {
let [a, b] = self.map(f);
Ok([a?, b?])
}

fn zip_ext<U>(self, rhs: [U; 2]) -> [(T, U); 2] {
let [a, b] = self;
let [q, r] = rhs;
[(a, q), (b, r)]
}
}

impl<T> ArrayExt<T, 4> for [T; 4] {
Expand All @@ -43,6 +54,12 @@ impl<T> ArrayExt<T, 4> for [T; 4] {
let [a, b, c, d] = self.map(f);
Ok([a?, b?, c?, d?])
}

fn zip_ext<U>(self, rhs: [U; 4]) -> [(T, U); 4] {
let [a, b, c, d] = self;
let [q, r, s, t] = rhs;
[(a, q), (b, r), (c, s), (d, t)]
}
}

/// Extension trait for arrays
Expand Down
18 changes: 8 additions & 10 deletions crates/fj-kernel/src/builder/edge.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use fj_interop::ext::ArrayExt;
use fj_math::{Point, Scalar};
use iter_fixed::IntoIteratorFixed;

Expand All @@ -6,7 +7,7 @@ use crate::{
objects::{Curve, Objects, Surface, Vertex, VerticesInNormalizedOrder},
partial::{
MaybePartial, MergeWith, PartialGlobalEdge, PartialHalfEdge,
PartialSurfaceVertex, PartialVertex, Replace,
PartialSurfaceVertex, PartialVertex,
},
services::{Service, Services},
storage::Handle,
Expand Down Expand Up @@ -108,21 +109,18 @@ impl HalfEdgeBuilder for PartialHalfEdge {
surface: Handle<Surface>,
points: [impl Into<Point<2>>; 2],
) -> Self {
let vertices = points.map(|point| {
let surface_form = PartialSurfaceVertex {
self.vertices = self.vertices.zip_ext(points).map(|(vertex, point)| {
let mut vertex = vertex.into_partial();

vertex.surface_form = MaybePartial::from(PartialSurfaceVertex {
position: Some(point.into()),
surface: Some(surface.clone()),
..Default::default()
};
});

PartialVertex {
surface_form: surface_form.into(),
..Default::default()
}
vertex.into()
});

self.replace(surface);
self.vertices = vertices.map(Into::into);
self.update_as_line_segment()
}

Expand Down
20 changes: 1 addition & 19 deletions crates/fj-kernel/src/partial/objects/cycle.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::{
builder::HalfEdgeBuilder,
objects::{Cycle, HalfEdge, Objects, Surface},
partial::{
MaybePartial, MergeWith, PartialHalfEdge, PartialVertex, Replace,
},
partial::{MaybePartial, MergeWith, PartialHalfEdge, PartialVertex},
services::Service,
storage::Handle,
};
Expand Down Expand Up @@ -49,22 +47,6 @@ impl PartialCycle {
self.half_edges.push(half_edge);
}

self.with_surface(surface)
}

/// Update the partial cycle with the provided surface
///
/// All [`HalfEdge`]s will be updated with this surface.
pub fn with_surface(mut self, surface: Option<Handle<Surface>>) -> Self {
if let Some(surface) = surface {
for half_edge in &mut self.half_edges {
*half_edge =
half_edge.clone().update_partial(|mut half_edge| {
half_edge.replace(surface.clone());
half_edge
});
}
}
self
}

Expand Down
10 changes: 1 addition & 9 deletions crates/fj-kernel/src/partial/objects/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ impl MergeWith for PartialVertex {

impl Replace<Surface> for PartialVertex {
fn replace(&mut self, surface: Handle<Surface>) -> &mut Self {
self.curve.replace(surface.clone());
self.surface_form.replace(surface);
self.curve.replace(surface);
self
}
}
Expand Down Expand Up @@ -136,13 +135,6 @@ impl MergeWith for PartialSurfaceVertex {
}
}

impl Replace<Surface> for PartialSurfaceVertex {
fn replace(&mut self, surface: Handle<Surface>) -> &mut Self {
self.surface = Some(surface);
self
}
}

impl From<&SurfaceVertex> for PartialSurfaceVertex {
fn from(surface_vertex: &SurfaceVertex) -> Self {
Self {
Expand Down

0 comments on commit decba7e

Please sign in to comment.