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

Simplify Triangulate trait #1122

Merged
merged 1 commit into from
Sep 20, 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
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