Skip to content

Commit

Permalink
Merge pull request #1777 from hannobraun/update
Browse files Browse the repository at this point in the history
Use consistent nomenclature across update API
  • Loading branch information
hannobraun authored Apr 21, 2023
2 parents ecbd04b + 4e08525 commit a6c8e10
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
4 changes: 2 additions & 2 deletions crates/fj-kernel/src/algorithms/sweep/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ impl Sweep for (&HalfEdge, &Handle<Vertex>, &Surface, Option<Color>) {
Some(boundary),
objects,
)
.update_start_vertex(start_vertex);
.replace_start_vertex(start_vertex);

let half_edge = if let Some(global_edge) = global_edge {
half_edge.update_global_form(global_edge)
half_edge.replace_global_form(global_edge)
} else {
half_edge
};
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-kernel/src/builder/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ impl CycleBuilder {
.circular_tuple_windows()
.map(|((prev, _, _), (half_edge, curve, boundary))| {
HalfEdge::unjoined(curve, boundary, objects)
.update_start_vertex(prev.start_vertex().clone())
.update_global_form(half_edge.global_form().clone())
.replace_start_vertex(prev.start_vertex().clone())
.replace_global_form(half_edge.global_form().clone())
})
.collect();

Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/operations/build/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub trait BuildFace {
);

if let Some(global_form) = global_form {
half_edge = half_edge.update_global_form(global_form);
half_edge = half_edge.replace_global_form(global_form);
}

half_edge.insert(objects)
Expand Down
36 changes: 32 additions & 4 deletions crates/fj-kernel/src/operations/update/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ pub trait UpdateCycle {
) -> Cycle;

/// Replace the provided half-edge
///
/// # Panics
///
/// Panics, unless this operation replaces exactly one half-edge.
fn replace_half_edge(
&self,
original: &Handle<HalfEdge>,
replacement: Handle<HalfEdge>,
) -> Cycle;

/// Replace the half-edge at the given index
fn replace_nth_half_edge(
///
/// # Panics
///
/// Panics, unless this operation replaces exactly one half-edge.
fn update_nth_half_edge(
&self,
index: usize,
f: impl FnMut(&Handle<HalfEdge>) -> Handle<HalfEdge>,
Expand All @@ -40,30 +48,50 @@ impl UpdateCycle for Cycle {
original: &Handle<HalfEdge>,
replacement: Handle<HalfEdge>,
) -> Cycle {
let mut num_replacements = 0;

let half_edges = self.half_edges().map(|half_edge| {
if half_edge.id() == original.id() {
num_replacements += 1;
replacement.clone()
} else {
half_edge.clone()
}
});

Cycle::new(half_edges)
let cycle = Cycle::new(half_edges);

assert_eq!(
num_replacements, 1,
"Expected operation to replace exactly one half-edge"
);

cycle
}

fn replace_nth_half_edge(
fn update_nth_half_edge(
&self,
index: usize,
mut f: impl FnMut(&Handle<HalfEdge>) -> Handle<HalfEdge>,
) -> Cycle {
let mut num_replacements = 0;

let half_edges = self.half_edges().enumerate().map(|(i, half_edge)| {
if i == index {
num_replacements += 1;
f(half_edge)
} else {
half_edge.clone()
}
});

Cycle::new(half_edges)
let cycle = Cycle::new(half_edges);

assert_eq!(
num_replacements, 1,
"Expected operation to replace exactly one half-edge"
);

cycle
}
}
8 changes: 4 additions & 4 deletions crates/fj-kernel/src/operations/update/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use crate::{
/// Update a [`HalfEdge`]
pub trait UpdateHalfEdge {
/// Update the start vertex of the half-edge
fn update_start_vertex(&self, start_vertex: Handle<Vertex>) -> HalfEdge;
fn replace_start_vertex(&self, start_vertex: Handle<Vertex>) -> HalfEdge;

/// Update the global form of the half-edge
fn update_global_form(&self, global_form: Handle<GlobalEdge>) -> HalfEdge;
fn replace_global_form(&self, global_form: Handle<GlobalEdge>) -> HalfEdge;
}

impl UpdateHalfEdge for HalfEdge {
fn update_start_vertex(&self, start_vertex: Handle<Vertex>) -> HalfEdge {
fn replace_start_vertex(&self, start_vertex: Handle<Vertex>) -> HalfEdge {
HalfEdge::new(
self.curve(),
self.boundary(),
Expand All @@ -22,7 +22,7 @@ impl UpdateHalfEdge for HalfEdge {
)
}

fn update_global_form(&self, global_form: Handle<GlobalEdge>) -> HalfEdge {
fn replace_global_form(&self, global_form: Handle<GlobalEdge>) -> HalfEdge {
HalfEdge::new(
self.curve(),
self.boundary(),
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-kernel/src/validate/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ mod tests {
let invalid = valid.shell.update_face(&valid.face_abc, |face| {
face.update_exterior(|cycle| {
cycle
.replace_nth_half_edge(0, |half_edge| {
.update_nth_half_edge(0, |half_edge| {
let global_form =
GlobalEdge::new().insert(&mut services.objects);
half_edge
.update_global_form(global_form)
.replace_global_form(global_form)
.insert(&mut services.objects)
})
.insert(&mut services.objects)
Expand Down

0 comments on commit a6c8e10

Please sign in to comment.