Skip to content

Commit

Permalink
Merge pull request #1122 from hannobraun/triangulate
Browse files Browse the repository at this point in the history
Simplify `Triangulate` trait
  • Loading branch information
hannobraun authored Sep 20, 2022
2 parents 92e0829 + faf7876 commit cc65ed9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
33 changes: 11 additions & 22 deletions crates/fj-kernel/src/algorithms/triangulate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,37 @@ use super::approx::{face::FaceApprox, Approx, Tolerance};
/// Triangulate a shape
pub trait Triangulate: Sized {
/// Triangulate the shape
fn triangulate(self, tolerance: impl Into<Tolerance>) -> Mesh<Point<3>> {
fn triangulate(self) -> Mesh<Point<3>> {
let mut mesh = Mesh::new();
self.triangulate_into_mesh(tolerance, &mut mesh);
self.triangulate_into_mesh(&mut mesh);
mesh
}

/// Triangulate a partial shape into the provided mesh
///
/// This is a low-level method, intended for implementation of
/// `Triangulate`. Most callers should prefer [`Triangulate::triangulate`].
fn triangulate_into_mesh(
self,
tolerance: impl Into<Tolerance>,
mesh: &mut Mesh<Point<3>>,
);
fn triangulate_into_mesh(self, mesh: &mut Mesh<Point<3>>);
}

impl<T> Triangulate for T
impl<T> Triangulate for (T, Tolerance)
where
T: Approx,
T::Approximation: IntoIterator<Item = FaceApprox>,
{
fn triangulate_into_mesh(
self,
tolerance: impl Into<Tolerance>,
mesh: &mut Mesh<Point<3>>,
) {
let tolerance = tolerance.into();
let approx = self.approx(tolerance);
fn triangulate_into_mesh(self, mesh: &mut Mesh<Point<3>>) {
let (approx, tolerance) = self;

let approx = approx.approx(tolerance);

for approx in approx {
approx.triangulate_into_mesh(tolerance, mesh);
approx.triangulate_into_mesh(mesh);
}
}
}

impl Triangulate for FaceApprox {
fn triangulate_into_mesh(
self,
_: impl Into<Tolerance>,
mesh: &mut Mesh<Point<3>>,
) {
fn triangulate_into_mesh(self, mesh: &mut Mesh<Point<3>>) {
let points: Vec<_> = self
.points()
.into_iter()
Expand Down Expand Up @@ -221,6 +210,6 @@ mod tests {

fn triangulate(face: impl Into<Face>) -> anyhow::Result<Mesh<Point<3>>> {
let tolerance = Tolerance::from_scalar(Scalar::ONE)?;
Ok(face.into().approx(tolerance).triangulate(tolerance))
Ok(face.into().approx(tolerance).triangulate())
}
}
2 changes: 1 addition & 1 deletion crates/fj-operations/src/shape_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ShapeProcessor {
let stores = Stores::new();
let mut debug_info = DebugInfo::new();
let shape = shape.compute_brep(&config, &stores, &mut debug_info)?;
let mesh = shape.into_inner().triangulate(tolerance);
let mesh = (&shape.into_inner(), tolerance).triangulate();

Ok(ProcessedShape {
aabb,
Expand Down

0 comments on commit cc65ed9

Please sign in to comment.