From 6d50d25146f46d22e5fe80552d11e37e60188997 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 22 Mar 2022 15:13:21 +0100 Subject: [PATCH] Simplify `approximate_edge` --- fj-kernel/src/algorithms/approximation.rs | 46 ++++++++--------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/fj-kernel/src/algorithms/approximation.rs b/fj-kernel/src/algorithms/approximation.rs index 0dcb166c0..97f397731 100644 --- a/fj-kernel/src/algorithms/approximation.rs +++ b/fj-kernel/src/algorithms/approximation.rs @@ -59,7 +59,7 @@ impl Approximation { fn approximate_edge( mut points: Vec>, vertices: Option<[Vertex; 2]>, -) -> Approximation { +) -> Vec> { // Insert the exact vertices of this edge into the approximation. This means // we don't rely on the curve approximation to deliver accurate // representations of these vertices, which they might not be able to do. @@ -82,18 +82,7 @@ fn approximate_edge( } } - let mut segments = HashSet::new(); - for segment in points.windows(2) { - let p0 = segment[0]; - let p1 = segment[1]; - - segments.insert(Segment::from([p0, p1])); - } - - Approximation { - points: points.into_iter().collect(), - segments, - } + points } /// Compute an approximation for a cycle @@ -108,10 +97,18 @@ pub fn approximate_cycle(cycle: &Cycle, tolerance: Scalar) -> Approximation { let mut edge_points = Vec::new(); edge.curve().approx(tolerance, &mut edge_points); - let approx = approximate_edge(edge_points, edge.vertices()); + let edge_points = approximate_edge(edge_points, edge.vertices()); - points.extend(approx.points); - segments.extend(approx.segments); + let mut edge_segments = Vec::new(); + for segment in edge_points.windows(2) { + let p0 = segment[0]; + let p1 = segment[1]; + + edge_segments.push(Segment::from([p0, p1])); + } + + points.extend(edge_points); + segments.extend(edge_segments); } Approximation { points, segments } @@ -153,24 +150,11 @@ mod tests { points.clone(), Some([v1.get().clone(), v2.get().clone()]) ), - Approximation { - points: set![a, b, c, d], - segments: set![ - Segment::from([a, b]), - Segment::from([b, c]), - Segment::from([c, d]), - ], - } + vec![a, b, c, d], ); // Continuous edge - assert_eq!( - super::approximate_edge(points, None), - Approximation { - points: set![b, c], - segments: set![Segment::from([b, c]), Segment::from([c, b])], - } - ); + assert_eq!(super::approximate_edge(points, None), vec![b, c, b],); } #[test]