Merge pull request #311 from hannobraun/diff

Clean up 2D difference code
This commit is contained in:
Hanno Braun 2022-03-08 17:23:10 +01:00 committed by GitHub
commit 9ec81bd97b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,20 +16,11 @@ impl ToShape for fj::Difference2d {
let mut shape = Shape::new(); let mut shape = Shape::new();
let mut a = self.a.to_shape(tolerance, debug_info); let [mut a, mut b] = [&self.a, &self.b]
let mut b = self.b.to_shape(tolerance, debug_info); .map(|shape| shape.to_shape(tolerance, debug_info));
if a.cycles().all().count() == 1 && b.cycles().all().count() == 1 { for shape in [&mut a, &mut b] {
let a = a.cycles().all().next().unwrap(); if shape.cycles().all().count() != 1 {
let b = b.cycles().all().next().unwrap();
shape.cycles().add(Cycle {
edges: a.edges.clone(),
});
shape.cycles().add(Cycle {
edges: b.edges.clone(),
});
} else {
// See issue: // See issue:
// https://github.com/hannobraun/Fornjot/issues/95 // https://github.com/hannobraun/Fornjot/issues/95
todo!( todo!(
@ -37,27 +28,32 @@ impl ToShape for fj::Difference2d {
cycle in each operand." cycle in each operand."
); );
} }
if shape.faces().all().count() != 1 {
{
let (a, b) = if a.faces().all().count() == 1
&& b.faces().all().count() == 1
{
// Can't panic. We just checked that length of `a` and `b` is 1.
(
a.faces().all().next().unwrap(),
b.faces().all().next().unwrap(),
)
} else {
// See issue: // See issue:
// https://github.com/hannobraun/Fornjot/issues/95 // https://github.com/hannobraun/Fornjot/issues/95
todo!( todo!(
"The 2-dimensional difference operation only supports one \ "The 2-dimensional difference operation only supports one \
face in each operand." face in each operand."
); );
}; }
}
let (a, b, surface_a, surface_b) = // Can't panic, as we just verified that both shapes have one cycle.
match ((*a).clone(), (*b).clone()) { let cycles =
[&mut a, &mut b].map(|shape| shape.cycles().all().next().unwrap());
for cycle in cycles {
shape.cycles().add(Cycle {
edges: cycle.edges.clone(),
});
}
// Can't panic, as we just verified that both shapes have one face.
let [face_a, face_b] =
[&mut a, &mut b].map(|shape| shape.faces().all().next().unwrap());
let (cycles_a, cycles_b, surface_a, surface_b) =
match ((*face_a).clone(), (*face_b).clone()) {
( (
Face::Face { Face::Face {
cycles: a, cycles: a,
@ -69,25 +65,21 @@ impl ToShape for fj::Difference2d {
}, },
) => (a, b, surface_a, surface_b), ) => (a, b, surface_a, surface_b),
_ => { _ => {
// None of the 2D types still use triangle // None of the 2D types still use triangle representation.
// representation.
unreachable!() unreachable!()
} }
}; };
assert!( assert!(
surface_a == surface_b, surface_a == surface_b,
// Panicking is not great, but as long as we don't have a real
// error handling mechanism, it will do.
"Trying to subtract sketches with different surfaces." "Trying to subtract sketches with different surfaces."
); );
let surface = surface_a; let surface = surface_a;
let mut cycles = a; let mut cycles = cycles_a;
cycles.extend(b); cycles.extend(cycles_b);
shape.faces().add(Face::Face { cycles, surface }); shape.faces().add(Face::Face { cycles, surface });
};
shape shape
} }