Skip to content

Commit

Permalink
Merge pull request #2089 from hannobraun/replace
Browse files Browse the repository at this point in the history
Don't insert updated replacement result
  • Loading branch information
hannobraun authored Nov 13, 2023
2 parents 3513c86 + 614c740 commit c91347f
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 94 deletions.
76 changes: 46 additions & 30 deletions crates/fj-core/src/operations/replace/curve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
objects::{Curve, Cycle, Face, HalfEdge, Region, Shell, Sketch, Solid},
operations::{insert::Insert, update::UpdateHalfEdge},
operations::update::UpdateHalfEdge,
services::Services,
storage::Handle,
};
Expand All @@ -13,40 +13,45 @@ use super::ReplaceOutput;
///
/// [module documentation]: super
pub trait ReplaceCurve: Sized {
/// The bare object type that this trait is implemented for
type BareObject;

/// Replace the curve
#[must_use]
fn replace_curve(
self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self>;
) -> ReplaceOutput<Self::BareObject>;
}

impl ReplaceCurve for Handle<HalfEdge> {
type BareObject = HalfEdge;

fn replace_curve(
self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self> {
_: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
if original.id() == self.curve().id() {
ReplaceOutput::Updated(
self.update_curve(|_| replacement).insert(services),
)
ReplaceOutput::Updated(self.update_curve(|_| replacement))
} else {
ReplaceOutput::Original(self)
}
}
}

impl ReplaceCurve for Handle<Cycle> {
type BareObject = Cycle;

fn replace_curve(
self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self> {
) -> ReplaceOutput<Self::BareObject> {
let mut replacement_happened = false;

let mut half_edges = Vec::new();
Expand All @@ -57,24 +62,26 @@ impl ReplaceCurve for Handle<Cycle> {
services,
);
replacement_happened |= half_edge.was_updated();
half_edges.push(half_edge.into_inner());
half_edges.push(half_edge.into_inner(services));
}

if replacement_happened {
ReplaceOutput::Updated(Cycle::new(half_edges).insert(services))
ReplaceOutput::Updated(Cycle::new(half_edges))
} else {
ReplaceOutput::Original(self)
}
}
}

impl ReplaceCurve for Handle<Region> {
type BareObject = Region;

fn replace_curve(
self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self> {
) -> ReplaceOutput<Self::BareObject> {
let mut replacement_happened = false;

let exterior = self.exterior().clone().replace_curve(
Expand All @@ -92,27 +99,30 @@ impl ReplaceCurve for Handle<Region> {
services,
);
replacement_happened |= cycle.was_updated();
interiors.push(cycle.into_inner());
interiors.push(cycle.into_inner(services));
}

if replacement_happened {
ReplaceOutput::Updated(
Region::new(exterior.into_inner(), interiors, self.color())
.insert(services),
)
ReplaceOutput::Updated(Region::new(
exterior.into_inner(services),
interiors,
self.color(),
))
} else {
ReplaceOutput::Original(self)
}
}
}

impl ReplaceCurve for Handle<Sketch> {
type BareObject = Sketch;

fn replace_curve(
self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self> {
) -> ReplaceOutput<Self::BareObject> {
let mut replacement_happened = false;

let mut regions = Vec::new();
Expand All @@ -123,48 +133,52 @@ impl ReplaceCurve for Handle<Sketch> {
services,
);
replacement_happened |= region.was_updated();
regions.push(region.into_inner());
regions.push(region.into_inner(services));
}

if replacement_happened {
ReplaceOutput::Updated(Sketch::new(regions).insert(services))
ReplaceOutput::Updated(Sketch::new(regions))
} else {
ReplaceOutput::Original(self)
}
}
}

impl ReplaceCurve for Handle<Face> {
type BareObject = Face;

fn replace_curve(
self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self> {
) -> ReplaceOutput<Self::BareObject> {
let region = self.region().clone().replace_curve(
original,
replacement,
services,
);

if region.was_updated() {
ReplaceOutput::Updated(
Face::new(self.surface().clone(), region.into_inner())
.insert(services),
)
ReplaceOutput::Updated(Face::new(
self.surface().clone(),
region.into_inner(services),
))
} else {
ReplaceOutput::Original(self)
}
}
}

impl ReplaceCurve for Handle<Shell> {
type BareObject = Shell;

fn replace_curve(
self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self> {
) -> ReplaceOutput<Self::BareObject> {
let mut replacement_happened = false;

let mut faces = Vec::new();
Expand All @@ -175,24 +189,26 @@ impl ReplaceCurve for Handle<Shell> {
services,
);
replacement_happened |= face.was_updated();
faces.push(face.into_inner());
faces.push(face.into_inner(services));
}

if replacement_happened {
ReplaceOutput::Updated(Shell::new(faces).insert(services))
ReplaceOutput::Updated(Shell::new(faces))
} else {
ReplaceOutput::Original(self)
}
}
}

impl ReplaceCurve for Handle<Solid> {
type BareObject = Solid;

fn replace_curve(
self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self> {
) -> ReplaceOutput<Self::BareObject> {
let mut replacement_happened = false;

let mut shells = Vec::new();
Expand All @@ -203,11 +219,11 @@ impl ReplaceCurve for Handle<Solid> {
services,
);
replacement_happened |= shell.was_updated();
shells.push(shell.into_inner());
shells.push(shell.into_inner(services));
}

if replacement_happened {
ReplaceOutput::Updated(Solid::new(shells).insert(services))
ReplaceOutput::Updated(Solid::new(shells))
} else {
ReplaceOutput::Original(self)
}
Expand Down
Loading

0 comments on commit c91347f

Please sign in to comment.