Skip to content

Commit

Permalink
Merge pull request #865 from hannobraun/cycle
Browse files Browse the repository at this point in the history
Make `Cycle` immutable; clean up its API
  • Loading branch information
hannobraun authored Jul 22, 2022
2 parents 640dbaf + 8b772b0 commit 02d53b0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 44 deletions.
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/approx/cycles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl CycleApprox {
pub fn new(cycle: &Cycle, tolerance: Tolerance) -> Self {
let mut points = Vec::new();

for edge in &cycle.edges {
for edge in cycle.edges() {
let mut edge_points = Vec::new();
approx_curve(
edge.curve().global_form(),
Expand Down
52 changes: 24 additions & 28 deletions crates/fj-kernel/src/algorithms/reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,33 @@ fn reverse_local_coordinates_in_cycle<'r>(
cycles: impl IntoIterator<Item = &'r Cycle> + 'r,
) -> impl Iterator<Item = Cycle> + 'r {
cycles.into_iter().map(|cycle| {
let edges = cycle
.edges
.iter()
.map(|edge| {
let curve = {
let local = match edge.curve().local_form() {
Curve::Circle(Circle { center, a, b }) => {
let center = Point::from([center.u, -center.v]);

let a = Vector::from([a.u, -a.v]);
let b = Vector::from([b.u, -b.v]);

Curve::Circle(Circle { center, a, b })
}
Curve::Line(Line { origin, direction }) => {
let origin = Point::from([origin.u, -origin.v]);
let direction =
Vector::from([direction.u, -direction.v]);

Curve::Line(Line { origin, direction })
}
};

Local::new(local, *edge.curve().global_form())
let edges = cycle.edges().map(|edge| {
let curve = {
let local = match edge.curve().local_form() {
Curve::Circle(Circle { center, a, b }) => {
let center = Point::from([center.u, -center.v]);

let a = Vector::from([a.u, -a.v]);
let b = Vector::from([b.u, -b.v]);

Curve::Circle(Circle { center, a, b })
}
Curve::Line(Line { origin, direction }) => {
let origin = Point::from([origin.u, -origin.v]);
let direction =
Vector::from([direction.u, -direction.v]);

Curve::Line(Line { origin, direction })
}
};

Edge::new(curve, *edge.vertices())
})
.collect();
Local::new(local, *edge.curve().global_form())
};

Cycle { edges }
Edge::new(curve, *edge.vertices())
});

Cycle::new().with_edges(edges)
})
}

Expand Down
6 changes: 3 additions & 3 deletions crates/fj-kernel/src/algorithms/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn sweep(
);

for cycle in face.all_cycles() {
for edge in &cycle.edges {
for edge in cycle.edges() {
if let Some(vertices) = edge.vertices().get() {
create_non_continuous_side_face(
path,
Expand Down Expand Up @@ -158,7 +158,7 @@ fn create_non_continuous_side_face(
edges.push(edge);
}

Cycle { edges }
Cycle::new().with_edges(edges)
};

let face = Face::new(surface).with_exteriors([cycle]).with_color(color);
Expand All @@ -174,7 +174,7 @@ fn create_continuous_side_face(
) {
let translation = Transform::translation(path);

let cycle = Cycle { edges: vec![edge] };
let cycle = Cycle::new().with_edges([edge]);
let approx = CycleApprox::new(&cycle, tolerance);

let mut quads = Vec::new();
Expand Down
9 changes: 3 additions & 6 deletions crates/fj-kernel/src/algorithms/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ impl TransformObject for Curve<3> {
}

impl TransformObject for Cycle {
fn transform(mut self, transform: &Transform) -> Self {
for edge in &mut self.edges {
*edge = edge.transform(transform);
}

self
fn transform(self, transform: &Transform) -> Self {
Self::new()
.with_edges(self.into_edges().map(|edge| edge.transform(transform)))
}
}

Expand Down
30 changes: 27 additions & 3 deletions crates/fj-kernel/src/objects/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,30 @@ use super::{Edge, Surface};
/// one.
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Cycle {
/// The edges that make up the cycle
pub edges: Vec<Edge>,
edges: Vec<Edge>,
}

impl Cycle {
/// Create a new cycle
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
// Implementation note:
// As I'm writing this, this constructor has no arguments. I expect it
// to take a `Surface` at some point. Remove the `#[allow(...)]`
// attribute then.
// - @hannobraun

Self { edges: Vec::new() }
}

/// Add edges to the cycle
///
/// Consumes the cycle and returns the updated instance.
pub fn with_edges(mut self, edges: impl IntoIterator<Item = Edge>) -> Self {
self.edges.extend(edges);
self
}

/// Create a polygon from a list of points
pub fn polygon_from_points(
surface: &Surface,
Expand All @@ -40,8 +59,13 @@ impl Cycle {
Cycle { edges }
}

/// Access this cycle's edges
/// Access edges that make up the cycle
pub fn edges(&self) -> impl Iterator<Item = &Edge> + '_ {
self.edges.iter()
}

/// Consume the cycle and return its edges
pub fn into_edges(self) -> impl Iterator<Item = Edge> {
self.edges.into_iter()
}
}
4 changes: 2 additions & 2 deletions crates/fj-operations/src/difference_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl Shape for fj::Difference2d {

fn add_cycle(cycle: Cycle, reverse: bool) -> Cycle {
let mut edges = Vec::new();
for edge in cycle.edges {
for edge in cycle.edges() {
let curve_local = if reverse {
edge.curve().local_form().reverse()
} else {
Expand Down Expand Up @@ -120,5 +120,5 @@ fn add_cycle(cycle: Cycle, reverse: bool) -> Cycle {
edges.reverse();
}

Cycle { edges }
Cycle::new().with_edges(edges)
}
2 changes: 1 addition & 1 deletion crates/fj-operations/src/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Shape for fj::Sketch {

let edge =
Edge::circle_from_radius(Scalar::from_f64(circle.radius()));
let cycle = Cycle { edges: vec![edge] };
let cycle = Cycle::new().with_edges([edge]);

Face::new(surface)
.with_exteriors([cycle])
Expand Down

0 comments on commit 02d53b0

Please sign in to comment.