Skip to content

Commit

Permalink
Merge pull request #2219 from hannobraun/derive
Browse files Browse the repository at this point in the history
Add `DeriveFrom`
  • Loading branch information
hannobraun authored Feb 16, 2024
2 parents b0c2188 + cb123b8 commit fc0b8ce
Show file tree
Hide file tree
Showing 21 changed files with 303 additions and 115 deletions.
19 changes: 19 additions & 0 deletions crates/fj-core/src/operations/derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Mark a stored object as derived from another
//!
//! See [`DeriveFrom`].
use crate::{storage::Handle, Core};

/// Mark a store object as derived from another
pub trait DeriveFrom {
/// Mark this object as derived from the other object provided
fn derive_from(self, other: &Self, core: &mut Core) -> Self;
}

impl<T> DeriveFrom for Handle<T> {
fn derive_from(self, _other: &Self, _core: &mut Core) -> Self {
// This is currently a no-op. Eventually, it will trigger a command to
// the layers that this information is relevant for.
self
}
}
1 change: 1 addition & 0 deletions crates/fj-core/src/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
//! send a pull request!
pub mod build;
pub mod derive;
pub mod holes;
pub mod insert;
pub mod join;
Expand Down
78 changes: 55 additions & 23 deletions crates/fj-core/src/operations/replace/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
objects::{
Curve, Cycle, Face, HalfEdge, IsObject, Region, Shell, Sketch, Solid,
},
operations::{insert::Insert, update::UpdateHalfEdge},
operations::{derive::DeriveFrom, insert::Insert, update::UpdateHalfEdge},
storage::Handle,
Core,
};
Expand Down Expand Up @@ -52,13 +52,20 @@ impl ReplaceCurve for Cycle {
let mut replacement_happened = false;

let mut half_edges = Vec::new();
for half_edge in self.half_edges() {
let half_edge =
half_edge.replace_curve(original, replacement.clone(), core);
for original_half_edge in self.half_edges() {
let half_edge = original_half_edge.replace_curve(
original,
replacement.clone(),
core,
);
replacement_happened |= half_edge.was_updated();
half_edges.push(
half_edge
.map_updated(|updated| updated.insert(core))
.map_updated(|updated| {
updated
.insert(core)
.derive_from(original_half_edge, core)
})
.into_inner(),
);
}
Expand Down Expand Up @@ -86,21 +93,28 @@ impl ReplaceCurve for Region {
replacement_happened |= exterior.was_updated();

let mut interiors = Vec::new();
for cycle in self.interiors() {
let cycle =
cycle.replace_curve(original, replacement.clone(), core);
for original_cycle in self.interiors() {
let cycle = original_cycle.replace_curve(
original,
replacement.clone(),
core,
);
replacement_happened |= cycle.was_updated();
interiors.push(
cycle
.map_updated(|updated| updated.insert(core))
.map_updated(|updated| {
updated.insert(core).derive_from(original_cycle, core)
})
.into_inner(),
);
}

if replacement_happened {
ReplaceOutput::Updated(Region::new(
exterior
.map_updated(|updated| updated.insert(core))
.map_updated(|updated| {
updated.insert(core).derive_from(self.exterior(), core)
})
.into_inner(),
interiors,
self.color(),
Expand All @@ -121,13 +135,18 @@ impl ReplaceCurve for Sketch {
let mut replacement_happened = false;

let mut regions = Vec::new();
for region in self.regions() {
let region =
region.replace_curve(original, replacement.clone(), core);
for original_region in self.regions() {
let region = original_region.replace_curve(
original,
replacement.clone(),
core,
);
replacement_happened |= region.was_updated();
regions.push(
region
.map_updated(|updated| updated.insert(core))
.map_updated(|updated| {
updated.insert(core).derive_from(original_region, core)
})
.into_inner(),
);
}
Expand All @@ -153,7 +172,9 @@ impl ReplaceCurve for Face {
ReplaceOutput::Updated(Face::new(
self.surface().clone(),
region
.map_updated(|updated| updated.insert(core))
.map_updated(|updated| {
updated.insert(core).derive_from(self.region(), core)
})
.into_inner(),
))
} else {
Expand All @@ -172,12 +193,18 @@ impl ReplaceCurve for Shell {
let mut replacement_happened = false;

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

Expand All @@ -199,13 +226,18 @@ impl ReplaceCurve for Solid {
let mut replacement_happened = false;

let mut shells = Vec::new();
for shell in self.shells() {
let shell =
shell.replace_curve(original, replacement.clone(), core);
for original_shell in self.shells() {
let shell = original_shell.replace_curve(
original,
replacement.clone(),
core,
);
replacement_happened |= shell.was_updated();
shells.push(
shell
.map_updated(|updated| updated.insert(core))
.map_updated(|updated| {
updated.insert(core).derive_from(original_shell, core)
})
.into_inner(),
);
}
Expand Down
64 changes: 44 additions & 20 deletions crates/fj-core/src/operations/replace/half_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ops::Deref;

use crate::{
objects::{Cycle, Face, HalfEdge, IsObject, Region, Shell, Sketch, Solid},
operations::insert::Insert,
operations::{derive::DeriveFrom, insert::Insert},
storage::Handle,
Core,
};
Expand Down Expand Up @@ -59,21 +59,28 @@ impl ReplaceHalfEdge for Region {
replacement_happened |= exterior.was_updated();

let mut interiors = Vec::new();
for cycle in self.interiors() {
let cycle =
cycle.replace_half_edge(original, replacements.clone(), core);
for original_cycle in self.interiors() {
let cycle = original_cycle.replace_half_edge(
original,
replacements.clone(),
core,
);
replacement_happened |= cycle.was_updated();
interiors.push(
cycle
.map_updated(|updated| updated.insert(core))
.map_updated(|updated| {
updated.insert(core).derive_from(original_cycle, core)
})
.into_inner(),
);
}

if replacement_happened {
ReplaceOutput::Updated(Region::new(
exterior
.map_updated(|updated| updated.insert(core))
.map_updated(|updated| {
updated.insert(core).derive_from(self.exterior(), core)
})
.into_inner(),
interiors,
self.color(),
Expand All @@ -94,13 +101,18 @@ impl ReplaceHalfEdge for Sketch {
let mut replacement_happened = false;

let mut regions = Vec::new();
for region in self.regions() {
let region =
region.replace_half_edge(original, replacements.clone(), core);
for original_region in self.regions() {
let region = original_region.replace_half_edge(
original,
replacements.clone(),
core,
);
replacement_happened |= region.was_updated();
regions.push(
region
.map_updated(|updated| updated.insert(core))
.map_updated(|updated| {
updated.insert(core).derive_from(original_region, core)
})
.into_inner(),
);
}
Expand Down Expand Up @@ -128,7 +140,9 @@ impl ReplaceHalfEdge for Face {
ReplaceOutput::Updated(Face::new(
self.surface().clone(),
region
.map_updated(|updated| updated.insert(core))
.map_updated(|updated| {
updated.insert(core).derive_from(self.region(), core)
})
.into_inner(),
))
} else {
Expand All @@ -147,13 +161,18 @@ impl ReplaceHalfEdge for Shell {
let mut replacement_happened = false;

let mut faces = Vec::new();
for face in self.faces() {
let face =
face.replace_half_edge(original, replacements.clone(), core);
for original_face in self.faces() {
let face = original_face.replace_half_edge(
original,
replacements.clone(),
core,
);
replacement_happened |= face.was_updated();
faces.push(
face.map_updated(|updated| updated.insert(core))
.into_inner(),
face.map_updated(|updated| {
updated.insert(core).derive_from(original_face, core)
})
.into_inner(),
);
}

Expand All @@ -175,13 +194,18 @@ impl ReplaceHalfEdge for Solid {
let mut replacement_happened = false;

let mut shells = Vec::new();
for shell in self.shells() {
let shell =
shell.replace_half_edge(original, replacements.clone(), core);
for original_shell in self.shells() {
let shell = original_shell.replace_half_edge(
original,
replacements.clone(),
core,
);
replacement_happened |= shell.was_updated();
shells.push(
shell
.map_updated(|updated| updated.insert(core))
.map_updated(|updated| {
updated.insert(core).derive_from(original_shell, core)
})
.into_inner(),
);
}
Expand Down
Loading

0 comments on commit fc0b8ce

Please sign in to comment.