Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement operations::replace traits for bare objects #2092

Merged
merged 6 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 160 additions & 30 deletions crates/fj-core/src/operations/replace/curve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::ops::Deref;

use crate::{
objects::{Curve, Cycle, Face, HalfEdge, Region, Shell, Sketch, Solid},
operations::update::UpdateHalfEdge,
operations::{insert::Insert, update::UpdateHalfEdge},
services::Services,
storage::Handle,
};
Expand All @@ -23,18 +25,18 @@ pub trait ReplaceCurve: Sized {
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject>;
) -> ReplaceOutput<Self, Self::BareObject>;
}

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

fn replace_curve(
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
_: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
if original.id() == self.curve().id() {
ReplaceOutput::Updated(self.update_curve(|_| replacement))
} else {
Expand All @@ -43,15 +45,15 @@ impl ReplaceCurve for Handle<HalfEdge> {
}
}

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

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

let mut half_edges = Vec::new();
Expand All @@ -62,7 +64,11 @@ impl ReplaceCurve for Handle<Cycle> {
services,
);
replacement_happened |= half_edge.was_updated();
half_edges.push(half_edge.into_inner(services));
half_edges.push(
half_edge
.map_updated(|updated| updated.insert(services))
.into_inner(),
);
}

if replacement_happened {
Expand All @@ -73,15 +79,15 @@ impl ReplaceCurve for Handle<Cycle> {
}
}

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

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

let exterior = self.exterior().replace_curve(
Expand All @@ -96,12 +102,18 @@ impl ReplaceCurve for Handle<Region> {
let cycle =
cycle.replace_curve(original, replacement.clone(), services);
replacement_happened |= cycle.was_updated();
interiors.push(cycle.into_inner(services));
interiors.push(
cycle
.map_updated(|updated| updated.insert(services))
.into_inner(),
);
}

if replacement_happened {
ReplaceOutput::Updated(Region::new(
exterior.into_inner(services),
exterior
.map_updated(|updated| updated.insert(services))
.into_inner(),
interiors,
self.color(),
))
Expand All @@ -111,23 +123,27 @@ impl ReplaceCurve for Handle<Region> {
}
}

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

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

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

if replacement_happened {
Expand All @@ -138,46 +154,51 @@ impl ReplaceCurve for Handle<Sketch> {
}
}

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

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

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

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

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

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

if replacement_happened {
Expand All @@ -188,23 +209,27 @@ impl ReplaceCurve for Handle<Shell> {
}
}

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

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

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

if replacement_happened {
Expand All @@ -214,3 +239,108 @@ impl ReplaceCurve for Handle<Solid> {
}
}
}

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

fn replace_curve(
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self, Self::BareObject> {
self.deref()
.replace_curve(original, replacement, services)
.map_original(|_| self.clone())
}
}

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

fn replace_curve(
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self, Self::BareObject> {
self.deref()
.replace_curve(original, replacement, services)
.map_original(|_| self.clone())
}
}

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

fn replace_curve(
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self, Self::BareObject> {
self.deref()
.replace_curve(original, replacement, services)
.map_original(|_| self.clone())
}
}

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

fn replace_curve(
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self, Self::BareObject> {
self.deref()
.replace_curve(original, replacement, services)
.map_original(|_| self.clone())
}
}

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

fn replace_curve(
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self, Self::BareObject> {
self.deref()
.replace_curve(original, replacement, services)
.map_original(|_| self.clone())
}
}

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

fn replace_curve(
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self, Self::BareObject> {
self.deref()
.replace_curve(original, replacement, services)
.map_original(|_| self.clone())
}
}

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

fn replace_curve(
&self,
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self, Self::BareObject> {
self.deref()
.replace_curve(original, replacement, services)
.map_original(|_| self.clone())
}
}
Loading