From 4ad8e50fc85101344a7257e4fc6a2834124f4ed2 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 28 Jun 2022 15:31:09 +0200 Subject: [PATCH 1/2] Accept `Vec` as source in `sweep` --- crates/fj-kernel/src/algorithms/sweep.rs | 10 ++++++---- crates/fj-operations/src/sweep.rs | 4 +++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep.rs b/crates/fj-kernel/src/algorithms/sweep.rs index 0f6cf3f50..d4ad4efcf 100644 --- a/crates/fj-kernel/src/algorithms/sweep.rs +++ b/crates/fj-kernel/src/algorithms/sweep.rs @@ -12,7 +12,7 @@ use super::{CycleApprox, Tolerance}; /// Create a solid by sweeping a sketch pub fn sweep( - source: Shape, + source: Vec, path: impl Into>, tolerance: Tolerance, color: [u8; 4], @@ -455,11 +455,13 @@ mod tests { let mut shape = Shape::new(); - let _sketch = Face::builder(Surface::xy_plane(), &mut shape) + let sketch = Face::builder(Surface::xy_plane(), &mut shape) .with_exterior_polygon([[0., 0.], [1., 0.], [0., 1.]]) - .build(); + .build() + .get(); - let solid = super::sweep(shape, direction, tolerance, [255, 0, 0, 255]); + let solid = + super::sweep(vec![sketch], direction, tolerance, [255, 0, 0, 255]); let expected_vertices: Vec<_> = expected_vertices .into_iter() diff --git a/crates/fj-operations/src/sweep.rs b/crates/fj-operations/src/sweep.rs index b06b2e7b9..af541b2a7 100644 --- a/crates/fj-operations/src/sweep.rs +++ b/crates/fj-operations/src/sweep.rs @@ -1,6 +1,7 @@ use fj_interop::debug::DebugInfo; use fj_kernel::{ algorithms::{sweep, Tolerance}, + iter::ObjectIters, shape::Shape, validation::{validate, Validated, ValidationConfig, ValidationError}, }; @@ -19,7 +20,8 @@ impl ToShape for fj::Sweep { let path = Vector::from(self.path()); let color = self.shape().color(); - let swept = sweep(shape.into_inner(), path, tolerance, color); + let shape = shape.face_iter().collect::>(); + let swept = sweep(shape, path, tolerance, color); let swept = validate(swept, config)?; Ok(swept) From cf8cd08a9cba7377fdf31d931f2e69667dec1ee5 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Tue, 28 Jun 2022 16:08:44 +0200 Subject: [PATCH 2/2] Return `Vec` from `sweep` --- crates/fj-kernel/src/algorithms/sweep.rs | 38 +++++++++++++----------- crates/fj-operations/src/sweep.rs | 8 ++++- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/sweep.rs b/crates/fj-kernel/src/algorithms/sweep.rs index d4ad4efcf..93e343f74 100644 --- a/crates/fj-kernel/src/algorithms/sweep.rs +++ b/crates/fj-kernel/src/algorithms/sweep.rs @@ -16,13 +16,13 @@ pub fn sweep( path: impl Into>, tolerance: Tolerance, color: [u8; 4], -) -> Shape { +) -> Vec { let path = path.into(); let is_sweep_along_negative_direction = path.dot(&Vector::from([0., 0., 1.])) < Scalar::ZERO; - let mut target = Shape::new(); + let mut target = Vec::new(); for face in source.face_iter() { create_bottom_faces( @@ -59,8 +59,10 @@ pub fn sweep( fn create_bottom_faces( face: &Face, is_sweep_along_negative_direction: bool, - target: &mut Shape, + target: &mut Vec, ) { + let mut tmp = Shape::new(); + let mut surface = face.surface(); let mut exteriors = face.brep().exteriors.clone(); @@ -73,7 +75,7 @@ fn create_bottom_faces( interiors = reverse_local_coordinates_in_cycle(&interiors); }; - let surface = target.insert(surface); + let surface = tmp.insert(surface); let face = Face::new( surface, @@ -81,14 +83,14 @@ fn create_bottom_faces( interiors.as_local_form().cloned(), face.color(), ); - target.merge(face); + target.push(face); } fn create_top_face( face: &Face, path: Vector<3>, is_sweep_along_negative_direction: bool, - target: &mut Shape, + target: &mut Vec, ) { let mut surface = face.surface(); @@ -110,7 +112,7 @@ fn create_top_face( interiors = reverse_local_coordinates_in_cycle(&interiors); }; - let surface = target.insert(surface); + let surface = tmp.insert(surface); let face = Face::new( surface, @@ -118,7 +120,7 @@ fn create_top_face( interiors.as_local_form().cloned(), face.color(), ); - target.merge(face); + target.push(face); } fn reverse_local_coordinates_in_cycle(cycles: &CyclesInFace) -> CyclesInFace { @@ -230,8 +232,10 @@ fn create_non_continuous_side_face( is_sweep_along_negative_direction: bool, vertices_bottom: [Vertex; 2], color: [u8; 4], - target: &mut Shape, + target: &mut Vec, ) { + let mut tmp = Shape::new(); + let vertices = { let vertices_top = vertices_bottom.map(|vertex| { let point = vertex.point + path; @@ -246,14 +250,14 @@ fn create_non_continuous_side_face( [a, b, d, c] }; - vertices.map(|vertex| target.get_handle_or_insert(vertex)) + vertices.map(|vertex| tmp.get_handle_or_insert(vertex)) }; let surface = { let [a, b, _, c] = vertices.clone().map(|vertex| vertex.get().point); Surface::plane_from_points([a, b, c]) }; - let surface = target.get_handle_or_insert(surface); + let surface = tmp.get_handle_or_insert(surface); let cycle = { let [a, b, c, d] = vertices; @@ -277,7 +281,7 @@ fn create_non_continuous_side_face( let global = [a, b].map(|vertex| vertex.1.get().point); let global = Curve::line_from_points(global); - let global = target.get_handle_or_insert(global); + let global = tmp.get_handle_or_insert(global); LocalForm::new(local, global) }; @@ -297,7 +301,7 @@ fn create_non_continuous_side_face( curve: LocalForm::canonical_only(curve.canonical()), vertices, }; - let global = target.get_handle_or_insert(global); + let global = tmp.get_handle_or_insert(global); LocalForm::new(local, global) }; @@ -310,7 +314,7 @@ fn create_non_continuous_side_face( let global = Cycle::new(local.edges.iter().map(|edge| edge.canonical())); - let global = target.get_handle_or_insert(global); + let global = tmp.get_handle_or_insert(global); LocalForm::new(local, global) }; @@ -319,7 +323,7 @@ fn create_non_continuous_side_face( }; let face = Face::new(surface, [cycle], [], color); - target.get_handle_or_insert(face); + target.push(face); } fn create_continuous_side_face( @@ -327,7 +331,7 @@ fn create_continuous_side_face( path: Vector<3>, tolerance: Tolerance, color: [u8; 4], - target: &mut Shape, + target: &mut Vec, ) { let translation = Transform::translation(path); @@ -353,7 +357,7 @@ fn create_continuous_side_face( side_face.push(([v0, v2, v3].into(), color)); } - target.insert(Face::Triangles(side_face)); + target.push(Face::Triangles(side_face)); } #[cfg(test)] diff --git a/crates/fj-operations/src/sweep.rs b/crates/fj-operations/src/sweep.rs index af541b2a7..90c3bd915 100644 --- a/crates/fj-operations/src/sweep.rs +++ b/crates/fj-operations/src/sweep.rs @@ -22,7 +22,13 @@ impl ToShape for fj::Sweep { let shape = shape.face_iter().collect::>(); let swept = sweep(shape, path, tolerance, color); - let swept = validate(swept, config)?; + + let mut shape = Shape::new(); + for face in swept { + shape.merge(face); + } + + let swept = validate(shape, config)?; Ok(swept) }