Skip to content

Commit

Permalink
Merge pull request #1361 from hannobraun/transform
Browse files Browse the repository at this point in the history
Clean up `MaybePartial` transform code
  • Loading branch information
hannobraun authored Nov 16, 2022
2 parents ccaef87 + e34db7e commit b22f73f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
8 changes: 2 additions & 6 deletions crates/fj-kernel/src/algorithms/transform/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use fj_math::Transform;

use crate::{
objects::Objects,
partial::{MaybePartial, PartialGlobalEdge, PartialHalfEdge},
partial::{PartialGlobalEdge, PartialHalfEdge},
validate::ValidationError,
};

Expand All @@ -15,11 +15,7 @@ impl TransformObject for PartialHalfEdge {
transform: &Transform,
objects: &Objects,
) -> Result<Self, ValidationError> {
let curve: MaybePartial<_> = self
.curve
.into_partial()
.transform(transform, objects)?
.into();
let curve = self.curve.transform(transform, objects)?;
let vertices = self.vertices.try_map_ext(
|vertex| -> Result<_, ValidationError> {
let mut vertex =
Expand Down
16 changes: 10 additions & 6 deletions crates/fj-kernel/src/algorithms/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ where
transform: &Transform,
objects: &Objects,
) -> Result<Self, ValidationError> {
match self {
let transformed = match self {
Self::Full(full) => {
Ok(Self::Full(full.transform(transform, objects)?))
full.to_partial().transform(transform, objects)?
}
Self::Partial(partial) => {
Ok(Self::Partial(partial.transform(transform, objects)?))
}
}
Self::Partial(partial) => partial.transform(transform, objects)?,
};

// Transforming a `MaybePartial` *always* results in a partial object.
// This provides the most flexibility to the caller, who might want to
// use the transformed partial object for merging or whatever else,
// before building it themselves.
Ok(Self::Partial(transformed))
}
}
10 changes: 10 additions & 0 deletions crates/fj-kernel/src/partial/maybe_partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ impl MaybePartial<GlobalEdge> {
}
}

impl MaybePartial<GlobalVertex> {
/// Access the position
pub fn position(&self) -> Option<Point<3>> {
match self {
Self::Full(full) => Some(full.position()),
Self::Partial(partial) => partial.position,
}
}
}

impl MaybePartial<HalfEdge> {
/// Access the curve
pub fn curve(&self) -> MaybePartial<Curve> {
Expand Down
18 changes: 10 additions & 8 deletions crates/fj-kernel/src/partial/objects/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub struct PartialSurfaceVertex {
impl PartialSurfaceVertex {
/// Build a full [`SurfaceVertex`] from the partial surface vertex
pub fn build(
self,
mut self,
objects: &Objects,
) -> Result<SurfaceVertex, ValidationError> {
let position = self
Expand All @@ -112,13 +112,15 @@ impl PartialSurfaceVertex {
.surface
.expect("Can't build `SurfaceVertex` without `Surface`");

let global_form = self
.global_form
.merge_with(PartialGlobalVertex::from_surface_and_position(
&surface.geometry(),
position,
))
.into_full(objects)?;
if self.global_form.position().is_none() {
self.global_form = self.global_form.merge_with(
PartialGlobalVertex::from_surface_and_position(
&surface.geometry(),
position,
),
)
}
let global_form = self.global_form.into_full(objects)?;

Ok(SurfaceVertex::new(position, surface, global_form))
}
Expand Down

0 comments on commit b22f73f

Please sign in to comment.