Skip to content

Commit

Permalink
Merge pull request #2029 from hannobraun/update
Browse files Browse the repository at this point in the history
Clean up `UpdateCycle`
  • Loading branch information
hannobraun authored Sep 25, 2023
2 parents aeeb1a0 + 3a886b4 commit d4e1a4b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 120 deletions.
24 changes: 12 additions & 12 deletions crates/fj-core/src/operations/build/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub trait BuildShell {
region
.update_exterior(|cycle| {
cycle
.update_nth_edge(0, |edge| {
.update_edge(cycle.edges().nth_circular(0), |edge| {
edge.reverse_curve_coordinate_systems(services)
.insert(services)
})
Expand All @@ -64,7 +64,7 @@ pub trait BuildShell {
region
.update_exterior(|cycle| {
cycle
.update_nth_edge(1, |edge| {
.update_edge(cycle.edges().nth_circular(1), |edge| {
edge.reverse_curve_coordinate_systems(services)
.insert(services)
})
Expand All @@ -74,7 +74,7 @@ pub trait BuildShell {
2..=2,
services,
)
.update_nth_edge(0, |edge| {
.update_edge(cycle.edges().nth_circular(0), |edge| {
edge.reverse_curve_coordinate_systems(services)
.insert(services)
})
Expand All @@ -92,7 +92,15 @@ pub trait BuildShell {
region
.update_exterior(|cycle| {
cycle
.update_nth_edge(0, |edge| {
.update_edge(cycle.edges().nth_circular(0), |edge| {
edge.reverse_curve_coordinate_systems(services)
.insert(services)
})
.update_edge(cycle.edges().nth_circular(1), |edge| {
edge.reverse_curve_coordinate_systems(services)
.insert(services)
})
.update_edge(cycle.edges().nth_circular(2), |edge| {
edge.reverse_curve_coordinate_systems(services)
.insert(services)
})
Expand All @@ -102,20 +110,12 @@ pub trait BuildShell {
1..=1,
services,
)
.update_nth_edge(1, |edge| {
edge.reverse_curve_coordinate_systems(services)
.insert(services)
})
.join_to(
bad.face.region().exterior(),
1..=1,
2..=2,
services,
)
.update_nth_edge(2, |edge| {
edge.reverse_curve_coordinate_systems(services)
.insert(services)
})
.join_to(
dac.face.region().exterior(),
2..=2,
Expand Down
55 changes: 24 additions & 31 deletions crates/fj-core/src/operations/join/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,37 +104,30 @@ impl JoinCycle for Cycle {
"Ranges have different lengths",
);

let mut cycle = self.clone();
range.zip(range_other).fold(
self.clone(),
|cycle, (index, index_other)| {
let edge_other = other.edges().nth_circular(index_other);

for (index, index_other) in range.zip(range_other) {
let edge = self.edges().nth_circular(index);
let edge_other = other.edges().nth_circular(index_other);

let vertex_a = other
.edges()
.after(edge_other)
.expect("Cycle must contain edge; just obtained edge from it")
.start_vertex()
.clone();
let vertex_b = edge_other.start_vertex().clone();

let next_edge = self
.edges()
.after(edge)
.expect("Cycle must contain edge; just obtained edge from it");

let this_joined = edge
.replace_curve(edge_other.curve().clone())
.replace_start_vertex(vertex_a)
.insert(services);
let next_joined =
next_edge.replace_start_vertex(vertex_b).insert(services);

cycle = cycle
.replace_edge(edge, this_joined)
.replace_edge(next_edge, next_joined)
}

cycle
cycle
.update_edge(self.edges().nth_circular(index), |edge| {
edge.replace_curve(edge_other.curve().clone())
.replace_start_vertex(
other
.edges()
.nth_circular(index_other + 1)
.start_vertex()
.clone(),
)
.insert(services)
})
.update_edge(self.edges().nth_circular(index + 1), |edge| {
edge.replace_start_vertex(
edge_other.start_vertex().clone(),
)
.insert(services)
})
},
)
}
}
78 changes: 15 additions & 63 deletions crates/fj-core/src/operations/update/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,12 @@ pub trait UpdateCycle {
#[must_use]
fn add_edges(&self, edges: impl IntoIterator<Item = Handle<Edge>>) -> Self;

/// Replace the provided edge
///
/// # Panics
///
/// Panics, unless this operation replaces exactly one edge.
/// Update the provided edge
#[must_use]
fn replace_edge(
fn update_edge(
&self,
original: &Handle<Edge>,
replacement: Handle<Edge>,
) -> Self;

/// Update the edge at the given index
///
/// # Panics
///
/// Panics, unless this operation updates exactly one edge.
#[must_use]
fn update_nth_edge(
&self,
index: usize,
f: impl FnMut(&Handle<Edge>) -> Handle<Edge>,
edge: &Handle<Edge>,
update: impl FnOnce(&Handle<Edge>) -> Handle<Edge>,
) -> Self;
}

Expand All @@ -40,55 +24,23 @@ impl UpdateCycle for Cycle {
Cycle::new(edges)
}

fn replace_edge(
&self,
original: &Handle<Edge>,
replacement: Handle<Edge>,
) -> Self {
let mut num_replacements = 0;

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

let cycle = Cycle::new(edges);

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

cycle
}

fn update_nth_edge(
fn update_edge(
&self,
index: usize,
mut f: impl FnMut(&Handle<Edge>) -> Handle<Edge>,
edge: &Handle<Edge>,
update: impl FnOnce(&Handle<Edge>) -> Handle<Edge>,
) -> Self {
let mut num_replacements = 0;
let mut updated = Some(update(edge));

let edges = self.edges().iter().enumerate().map(|(i, edge)| {
if i == index {
num_replacements += 1;
f(edge)
let edges = self.edges().iter().map(|e| {
if e.id() == edge.id() {
updated
.take()
.expect("Cycle should not contain same edge twice")
} else {
edge.clone()
e.clone()
}
});

let cycle = Cycle::new(edges);

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

cycle
Cycle::new(edges)
}
}
34 changes: 20 additions & 14 deletions crates/fj-core/src/validate/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,16 @@ mod tests {
region
.update_exterior(|cycle| {
cycle
.update_nth_edge(0, |edge| {
edge.replace_path(edge.path().reverse())
.replace_boundary(
edge.boundary().reverse(),
)
.insert(&mut services)
})
.update_edge(
cycle.edges().nth_circular(0),
|edge| {
edge.replace_path(edge.path().reverse())
.replace_boundary(
edge.boundary().reverse(),
)
.insert(&mut services)
},
)
.insert(&mut services)
})
.insert(&mut services)
Expand Down Expand Up @@ -442,13 +445,16 @@ mod tests {
region
.update_exterior(|cycle| {
cycle
.update_nth_edge(0, |edge| {
let curve =
Curve::new().insert(&mut services);

edge.replace_curve(curve)
.insert(&mut services)
})
.update_edge(
cycle.edges().nth_circular(0),
|edge| {
let curve =
Curve::new().insert(&mut services);

edge.replace_curve(curve)
.insert(&mut services)
},
)
.insert(&mut services)
})
.insert(&mut services)
Expand Down

0 comments on commit d4e1a4b

Please sign in to comment.