Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully decouple sweep algorithm from Shape #732

Merged
merged 2 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions crates/fj-kernel/src/algorithms/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ use super::{CycleApprox, Tolerance};

/// Create a solid by sweeping a sketch
pub fn sweep(
source: Shape,
source: Vec<Face>,
path: impl Into<Vector<3>>,
tolerance: Tolerance,
color: [u8; 4],
) -> Shape {
) -> Vec<Face> {
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(
Expand Down Expand Up @@ -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<Face>,
) {
let mut tmp = Shape::new();

let mut surface = face.surface();

let mut exteriors = face.brep().exteriors.clone();
Expand All @@ -73,22 +75,22 @@ 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,
exteriors.as_local_form().cloned(),
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<Face>,
) {
let mut surface = face.surface();

Expand All @@ -110,15 +112,15 @@ 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,
exteriors.as_local_form().cloned(),
interiors.as_local_form().cloned(),
face.color(),
);
target.merge(face);
target.push(face);
}

fn reverse_local_coordinates_in_cycle(cycles: &CyclesInFace) -> CyclesInFace {
Expand Down Expand Up @@ -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<Face>,
) {
let mut tmp = Shape::new();

let vertices = {
let vertices_top = vertices_bottom.map(|vertex| {
let point = vertex.point + path;
Expand All @@ -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;
Expand All @@ -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)
};
Expand All @@ -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)
};
Expand All @@ -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)
};
Expand All @@ -319,15 +323,15 @@ 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(
edge: Edge<3>,
path: Vector<3>,
tolerance: Tolerance,
color: [u8; 4],
target: &mut Shape,
target: &mut Vec<Face>,
) {
let translation = Transform::translation(path);

Expand All @@ -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)]
Expand Down Expand Up @@ -455,11 +459,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()
Expand Down
12 changes: 10 additions & 2 deletions crates/fj-operations/src/sweep.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use fj_interop::debug::DebugInfo;
use fj_kernel::{
algorithms::{sweep, Tolerance},
iter::ObjectIters,
shape::Shape,
validation::{validate, Validated, ValidationConfig, ValidationError},
};
Expand All @@ -19,8 +20,15 @@ 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 swept = validate(swept, config)?;
let shape = shape.face_iter().collect::<Vec<_>>();
let swept = sweep(shape, path, tolerance, color);

let mut shape = Shape::new();
for face in swept {
shape.merge(face);
}

let swept = validate(shape, config)?;

Ok(swept)
}
Expand Down