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,78 +16,70 @@ 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(); // See issue:
// https://github.com/hannobraun/Fornjot/issues/95
shape.cycles().add(Cycle { todo!(
edges: a.edges.clone(), "The 2-dimensional difference operation only supports one \
}); cycle in each operand."
shape.cycles().add(Cycle { );
edges: b.edges.clone(), }
}); if shape.faces().all().count() != 1 {
} else {
// See issue:
// https://github.com/hannobraun/Fornjot/issues/95
todo!(
"The 2-dimensional difference operation only supports one \
cycle in each operand."
);
}
{
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."
); );
}
}
// Can't panic, as we just verified that both shapes have one cycle.
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 {
cycles: a,
surface: surface_a,
},
Face::Face {
cycles: b,
surface: surface_b,
},
) => (a, b, surface_a, surface_b),
_ => {
// None of the 2D types still use triangle representation.
unreachable!()
}
}; };
let (a, b, surface_a, surface_b) = assert!(
match ((*a).clone(), (*b).clone()) { surface_a == surface_b,
( "Trying to subtract sketches with different surfaces."
Face::Face { );
cycles: a, let surface = surface_a;
surface: surface_a,
},
Face::Face {
cycles: b,
surface: surface_b,
},
) => (a, b, surface_a, surface_b),
_ => {
// None of the 2D types still use triangle
// representation.
unreachable!()
}
};
assert!( let mut cycles = cycles_a;
surface_a == surface_b, cycles.extend(cycles_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."
);
let surface = surface_a;
let mut cycles = a; shape.faces().add(Face::Face { cycles, surface });
cycles.extend(b);
shape.faces().add(Face::Face { cycles, surface });
};
shape shape
} }