Skip to content

Commit

Permalink
Merge pull request #1287 from hannobraun/partial
Browse files Browse the repository at this point in the history
Simplify handling of `MaybePartial`
  • Loading branch information
hannobraun authored Oct 28, 2022
2 parents d594348 + 113f495 commit 4a7df1e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 162 deletions.
44 changes: 16 additions & 28 deletions crates/fj-kernel/src/algorithms/transform/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use fj_interop::ext::ArrayExt;
use fj_math::Transform;

use crate::{
objects::{Curve, Objects},
objects::Objects,
partial::{MaybePartial, PartialGlobalEdge, PartialHalfEdge},
validate::ValidationError,
};
Expand All @@ -19,40 +19,28 @@ impl TransformObject for PartialHalfEdge {
.surface
.map(|surface| surface.transform(transform, objects))
.transpose()?;
let curve = self
let curve: MaybePartial<_> = self
.curve
.clone()
.map(|curve| -> Result<_, ValidationError> {
Ok(curve
.into_partial()
.transform(transform, objects)?
.with_surface(surface.clone())
.into();
let vertices = self.vertices.clone().try_map_ext(
|vertex| -> Result<_, ValidationError> {
Ok(vertex
.into_partial()
.transform(transform, objects)?
.with_surface(surface.clone())
.with_curve(Some(curve.clone()))
.into())
})
.transpose()?;
let vertices = self.vertices.clone().try_map_ext(|vertices| {
vertices
.map(|vertex| -> Result<_, ValidationError> {
Ok(vertex
.into_partial()
.transform(transform, objects)?
.with_curve(curve.clone())
.into())
})
.transpose()
})?;
},
)?;
let global_form = self
.global_form
.map(|global_form| -> Result<_, ValidationError> {
Ok(global_form
.into_partial()
.transform(transform, objects)?
.with_curve(curve.as_ref().and_then(
|curve: &MaybePartial<Curve>| curve.global_form(),
))
.into())
})
.transpose()?;
.into_partial()
.transform(transform, objects)?
.with_curve(curve.global_form())
.into();

Ok(Self {
surface,
Expand Down
25 changes: 5 additions & 20 deletions crates/fj-kernel/src/algorithms/transform/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,19 @@ impl TransformObject for PartialVertex {
transform: &Transform,
objects: &Objects,
) -> Result<Self, ValidationError> {
let curve = self
.curve
.map(|curve| curve.transform(transform, objects))
.transpose()?;
let curve = self.curve.transform(transform, objects)?;
let surface_form = self
.surface_form
.map(|surface_form| -> Result<_, ValidationError> {
Ok(surface_form
.into_partial()
.transform(transform, objects)?
.into())
})
.transpose()?;
let global_form = self
.global_form
.map(|global_form| global_form.transform(transform, objects))
.transpose()?;
.into_partial()
.transform(transform, objects)?
.into();

// Don't need to transform `self.position`, as that is in curve
// coordinates and thus transforming the curve takes care of it.
Ok(Self {
position: self.position,
curve,
surface_form,
global_form,
})
}
}
Expand All @@ -53,10 +41,7 @@ impl TransformObject for PartialSurfaceVertex {
.surface
.map(|surface| surface.transform(transform, objects))
.transpose()?;
let global_form = self
.global_form
.map(|global_form| global_form.transform(transform, objects))
.transpose()?;
let global_form = self.global_form.transform(transform, objects)?;

// Don't need to transform `self.position`, as that is in surface
// coordinates and thus transforming the surface takes care of it.
Expand Down
35 changes: 16 additions & 19 deletions crates/fj-kernel/src/partial/maybe_partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ impl<T: HasPartial> MaybePartial<T> {
}
}

impl<T> Default for MaybePartial<T>
where
T: HasPartial,
T::Partial: Default,
{
fn default() -> Self {
Self::Partial(T::Partial::default())
}
}

impl<T> From<Handle<T>> for MaybePartial<T>
where
T: HasPartial,
Expand Down Expand Up @@ -119,21 +129,10 @@ impl MaybePartial<GlobalEdge> {
}

impl MaybePartial<HalfEdge> {
/// Access the back vertex
pub fn back(&self) -> Option<MaybePartial<Vertex>> {
match self {
Self::Full(full) => Some(full.back().clone().into()),
Self::Partial(partial) => {
let [back, _] = &partial.vertices;
back.clone()
}
}
}

/// Access the front vertex
pub fn front(&self) -> Option<MaybePartial<Vertex>> {
pub fn front(&self) -> MaybePartial<Vertex> {
match self {
Self::Full(full) => Some(full.front().clone().into()),
Self::Full(full) => full.front().clone().into(),
Self::Partial(partial) => {
let [_, front] = &partial.vertices;
front.clone()
Expand All @@ -142,11 +141,9 @@ impl MaybePartial<HalfEdge> {
}

/// Access the vertices
pub fn vertices(&self) -> [Option<MaybePartial<Vertex>>; 2] {
pub fn vertices(&self) -> [MaybePartial<Vertex>; 2] {
match self {
Self::Full(full) => {
full.vertices().clone().map(|vertex| Some(vertex.into()))
}
Self::Full(full) => full.vertices().clone().map(Into::into),
Self::Partial(partial) => partial.vertices.clone(),
}
}
Expand All @@ -172,9 +169,9 @@ impl MaybePartial<SurfaceVertex> {

impl MaybePartial<Vertex> {
/// Access the surface form
pub fn surface_form(&self) -> Option<MaybePartial<SurfaceVertex>> {
pub fn surface_form(&self) -> MaybePartial<SurfaceVertex> {
match self {
Self::Full(full) => Some(full.surface_form().clone().into()),
Self::Full(full) => full.surface_form().clone().into(),
Self::Partial(partial) => partial.surface_form.clone(),
}
}
Expand Down
35 changes: 12 additions & 23 deletions crates/fj-kernel/src/partial/objects/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ impl PartialCycle {
.half_edges
.last()
.map(|half_edge| {
let [_, last] = half_edge.vertices().map(|vertex| {
vertex.expect("Need half-edge vertices to extend cycle")
});
let [_, last] = half_edge.vertices();
last.surface_form()
.expect("Need surface vertex to extend cycle")
})
.into_iter()
.chain(vertices);
Expand Down Expand Up @@ -130,19 +127,13 @@ impl PartialCycle {
let first = self.half_edges.first();
let last = self.half_edges.last();

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

if let [Some([first, _]), Some([_, last])] = vertices {
let vertices = [last, first].map(|vertex| {
vertex
.surface_form()
.expect("Need surface vertex to close cycle")
.position()
.expect("Need surface position to close cycle")
});
Expand Down Expand Up @@ -171,13 +162,13 @@ impl PartialCycle {
let last_vertex = self
.half_edges
.last_mut()
.and_then(|half_edge| {
half_edge.front().map(|vertex| (half_edge, vertex))
.map(|half_edge| {
let vertex = half_edge.front();
(half_edge, vertex)
})
.and_then(|(half_edge, vertex)| {
vertex.surface_form().map(|surface_vertex| {
(half_edge, vertex, surface_vertex)
})
.map(|(half_edge, vertex)| {
let surface_vertex = vertex.surface_form();
(half_edge, vertex, surface_vertex)
})
.map(|(half_edge, vertex, surface_vertex)|
-> Result<_, ValidationError>
Expand Down Expand Up @@ -211,15 +202,13 @@ impl PartialCycle {
let half_edge = half_edge
.update_partial(|half_edge| {
let [back, _] = half_edge.vertices.clone();
let back = back.map(|vertex| {
vertex.update_partial(|partial| {
partial.with_surface_form(previous_vertex)
})
let back = back.update_partial(|partial| {
partial.with_surface_form(previous_vertex)
});

half_edge
.with_surface(Some(surface_for_edges.clone()))
.with_back_vertex(back)
.with_back_vertex(Some(back))
})
.into_full(objects)?;

Expand Down
Loading

0 comments on commit 4a7df1e

Please sign in to comment.