Skip to content

Commit

Permalink
Merge pull request #2091 from hannobraun/replace
Browse files Browse the repository at this point in the history
Make `operations::replace` more consistent with other operations APIs
  • Loading branch information
hannobraun authored Nov 14, 2023
2 parents 847e3b6 + 64b488d commit ebaef85
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 107 deletions.
69 changes: 27 additions & 42 deletions crates/fj-core/src/operations/replace/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub trait ReplaceCurve: Sized {
/// Replace the curve
#[must_use]
fn replace_curve(
self,
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
Expand All @@ -30,15 +30,15 @@ impl ReplaceCurve for Handle<HalfEdge> {
type BareObject = HalfEdge;

fn replace_curve(
self,
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
_: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
if original.id() == self.curve().id() {
ReplaceOutput::Updated(self.update_curve(|_| replacement))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Expand All @@ -47,7 +47,7 @@ impl ReplaceCurve for Handle<Cycle> {
type BareObject = Cycle;

fn replace_curve(
self,
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
Expand All @@ -56,7 +56,7 @@ impl ReplaceCurve for Handle<Cycle> {

let mut half_edges = Vec::new();
for half_edge in self.half_edges() {
let half_edge = half_edge.clone().replace_curve(
let half_edge = half_edge.replace_curve(
original,
replacement.clone(),
services,
Expand All @@ -68,7 +68,7 @@ impl ReplaceCurve for Handle<Cycle> {
if replacement_happened {
ReplaceOutput::Updated(Cycle::new(half_edges))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Expand All @@ -77,14 +77,14 @@ impl ReplaceCurve for Handle<Region> {
type BareObject = Region;

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

let exterior = self.exterior().clone().replace_curve(
let exterior = self.exterior().replace_curve(
original,
replacement.clone(),
services,
Expand All @@ -93,11 +93,8 @@ impl ReplaceCurve for Handle<Region> {

let mut interiors = Vec::new();
for cycle in self.interiors() {
let cycle = cycle.clone().replace_curve(
original,
replacement.clone(),
services,
);
let cycle =
cycle.replace_curve(original, replacement.clone(), services);
replacement_happened |= cycle.was_updated();
interiors.push(cycle.into_inner(services));
}
Expand All @@ -109,7 +106,7 @@ impl ReplaceCurve for Handle<Region> {
self.color(),
))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Expand All @@ -118,7 +115,7 @@ impl ReplaceCurve for Handle<Sketch> {
type BareObject = Sketch;

fn replace_curve(
self,
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
Expand All @@ -127,19 +124,16 @@ impl ReplaceCurve for Handle<Sketch> {

let mut regions = Vec::new();
for region in self.regions() {
let region = region.clone().replace_curve(
original,
replacement.clone(),
services,
);
let region =
region.replace_curve(original, replacement.clone(), services);
replacement_happened |= region.was_updated();
regions.push(region.into_inner(services));
}

if replacement_happened {
ReplaceOutput::Updated(Sketch::new(regions))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Expand All @@ -148,24 +142,21 @@ impl ReplaceCurve for Handle<Face> {
type BareObject = Face;

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

if region.was_updated() {
ReplaceOutput::Updated(Face::new(
self.surface().clone(),
region.into_inner(services),
))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Expand All @@ -174,7 +165,7 @@ impl ReplaceCurve for Handle<Shell> {
type BareObject = Shell;

fn replace_curve(
self,
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
Expand All @@ -183,19 +174,16 @@ impl ReplaceCurve for Handle<Shell> {

let mut faces = Vec::new();
for face in self.faces() {
let face = face.clone().replace_curve(
original,
replacement.clone(),
services,
);
let face =
face.replace_curve(original, replacement.clone(), services);
replacement_happened |= face.was_updated();
faces.push(face.into_inner(services));
}

if replacement_happened {
ReplaceOutput::Updated(Shell::new(faces))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Expand All @@ -204,7 +192,7 @@ impl ReplaceCurve for Handle<Solid> {
type BareObject = Solid;

fn replace_curve(
self,
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
Expand All @@ -213,19 +201,16 @@ impl ReplaceCurve for Handle<Solid> {

let mut shells = Vec::new();
for shell in self.shells() {
let shell = shell.clone().replace_curve(
original,
replacement.clone(),
services,
);
let shell =
shell.replace_curve(original, replacement.clone(), services);
replacement_happened |= shell.was_updated();
shells.push(shell.into_inner(services));
}

if replacement_happened {
ReplaceOutput::Updated(Solid::new(shells))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
44 changes: 21 additions & 23 deletions crates/fj-core/src/operations/replace/half_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait ReplaceHalfEdge: Sized {
/// Replace the half-edge
#[must_use]
fn replace_half_edge<const N: usize>(
self,
&self,
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
Expand All @@ -29,7 +29,7 @@ impl ReplaceHalfEdge for Handle<Cycle> {
type BareObject = Cycle;

fn replace_half_edge<const N: usize>(
self,
&self,
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
_: &mut Services,
Expand All @@ -39,7 +39,7 @@ impl ReplaceHalfEdge for Handle<Cycle> {
{
ReplaceOutput::Updated(Cycle::new(half_edges))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Expand All @@ -48,14 +48,14 @@ impl ReplaceHalfEdge for Handle<Region> {
type BareObject = Region;

fn replace_half_edge<const N: usize>(
self,
&self,
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
let mut replacement_happened = false;

let exterior = self.exterior().clone().replace_half_edge(
let exterior = self.exterior().replace_half_edge(
original,
replacements.clone(),
services,
Expand All @@ -64,7 +64,7 @@ impl ReplaceHalfEdge for Handle<Region> {

let mut interiors = Vec::new();
for cycle in self.interiors() {
let cycle = cycle.clone().replace_half_edge(
let cycle = cycle.replace_half_edge(
original,
replacements.clone(),
services,
Expand All @@ -80,7 +80,7 @@ impl ReplaceHalfEdge for Handle<Region> {
self.color(),
))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Expand All @@ -89,7 +89,7 @@ impl ReplaceHalfEdge for Handle<Sketch> {
type BareObject = Sketch;

fn replace_half_edge<const N: usize>(
self,
&self,
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
Expand All @@ -98,7 +98,7 @@ impl ReplaceHalfEdge for Handle<Sketch> {

let mut regions = Vec::new();
for region in self.regions() {
let region = region.clone().replace_half_edge(
let region = region.replace_half_edge(
original,
replacements.clone(),
services,
Expand All @@ -110,7 +110,7 @@ impl ReplaceHalfEdge for Handle<Sketch> {
if replacement_happened {
ReplaceOutput::Updated(Sketch::new(regions))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Expand All @@ -119,24 +119,22 @@ impl ReplaceHalfEdge for Handle<Face> {
type BareObject = Face;

fn replace_half_edge<const N: usize>(
self,
&self,
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
let region = self.region().clone().replace_half_edge(
original,
replacements,
services,
);
let region =
self.region()
.replace_half_edge(original, replacements, services);

if region.was_updated() {
ReplaceOutput::Updated(Face::new(
self.surface().clone(),
region.into_inner(services),
))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Expand All @@ -145,7 +143,7 @@ impl ReplaceHalfEdge for Handle<Shell> {
type BareObject = Shell;

fn replace_half_edge<const N: usize>(
self,
&self,
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
Expand All @@ -154,7 +152,7 @@ impl ReplaceHalfEdge for Handle<Shell> {

let mut faces = Vec::new();
for face in self.faces() {
let face = face.clone().replace_half_edge(
let face = face.replace_half_edge(
original,
replacements.clone(),
services,
Expand All @@ -166,7 +164,7 @@ impl ReplaceHalfEdge for Handle<Shell> {
if replacement_happened {
ReplaceOutput::Updated(Shell::new(faces))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Expand All @@ -175,7 +173,7 @@ impl ReplaceHalfEdge for Handle<Solid> {
type BareObject = Solid;

fn replace_half_edge<const N: usize>(
self,
&self,
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
Expand All @@ -184,7 +182,7 @@ impl ReplaceHalfEdge for Handle<Solid> {

let mut shells = Vec::new();
for shell in self.shells() {
let shell = shell.clone().replace_half_edge(
let shell = shell.replace_half_edge(
original,
replacements.clone(),
services,
Expand All @@ -196,7 +194,7 @@ impl ReplaceHalfEdge for Handle<Solid> {
if replacement_happened {
ReplaceOutput::Updated(Solid::new(shells))
} else {
ReplaceOutput::Original(self)
ReplaceOutput::Original(self.clone())
}
}
}
Loading

0 comments on commit ebaef85

Please sign in to comment.