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

Make triangulation more flexible #1050

Merged
merged 6 commits into from
Sep 7, 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
5 changes: 1 addition & 4 deletions crates/fj-kernel/src/algorithms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
//! Algorithmic code is collected in this module, to keep other modules focused
//! on their respective purpose.

mod triangulate;

pub mod approx;
pub mod intersect;
pub mod reverse;
pub mod sweep;
pub mod transform;
pub mod triangulate;
pub mod validate;

pub use self::triangulate::triangulate;
79 changes: 59 additions & 20 deletions crates/fj-kernel/src/algorithms/triangulate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Shape triangulation

mod delaunay;
mod polygon;

Expand All @@ -11,23 +13,64 @@ use self::{delaunay::TriangulationPoint, polygon::Polygon};
use super::approx::{Approx, Tolerance};

/// Triangulate a shape
pub fn triangulate(
faces: Vec<Face>,
tolerance: Tolerance,
debug_info: &mut DebugInfo,
) -> Mesh<Point<3>> {
let mut mesh = Mesh::new();

for face in faces {
if let Some(triangles) = face.triangles() {
pub trait Triangulate: Sized {
/// Triangulate the shape
fn triangulate(
self,
tolerance: impl Into<Tolerance>,
debug_info: &mut DebugInfo,
) -> Mesh<Point<3>> {
let mut mesh = Mesh::new();
self.triangulate_into_mesh(tolerance, &mut mesh, debug_info);
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>>,
debug_info: &mut DebugInfo,
);
}

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

for face in self {
face.triangulate_into_mesh(tolerance, mesh, debug_info);
}
}
}

impl Triangulate for Face {
fn triangulate_into_mesh(
self,
tolerance: impl Into<Tolerance>,
mesh: &mut Mesh<Point<3>>,
debug_info: &mut DebugInfo,
) {
if let Some(triangles) = self.triangles() {
for &(triangle, color) in triangles {
mesh.push_triangle(triangle, color);
}
continue;
return;
}

let surface = face.surface();
let approx = face.approx(tolerance);
let surface = self.surface();
let approx = self.approx(tolerance.into());

let points: Vec<_> = approx
.points
Expand Down Expand Up @@ -62,11 +105,9 @@ pub fn triangulate(

for triangle in triangles {
let points = triangle.map(|point| point.point_global);
mesh.push_triangle(points, face.color());
mesh.push_triangle(points, self.color());
}
}

mesh
}

#[cfg(test)]
Expand All @@ -79,6 +120,8 @@ mod tests {
objects::{Face, Surface},
};

use super::Triangulate;

#[test]
fn simple() -> anyhow::Result<()> {
let a = [0., 0.];
Expand Down Expand Up @@ -190,10 +233,6 @@ mod tests {
let tolerance = Tolerance::from_scalar(Scalar::ONE)?;

let mut debug_info = DebugInfo::new();
Ok(super::triangulate(
vec![face.into()],
tolerance,
&mut debug_info,
))
Ok(vec![face.into()].triangulate(tolerance, &mut debug_info))
}
}
4 changes: 2 additions & 2 deletions crates/fj-operations/src/shape_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use fj_interop::{debug::DebugInfo, processed_shape::ProcessedShape};
use fj_kernel::algorithms::{
approx::{InvalidTolerance, Tolerance},
triangulate,
triangulate::Triangulate,
validate::{ValidationConfig, ValidationError},
};
use fj_math::Scalar;
Expand Down Expand Up @@ -42,7 +42,7 @@ impl ShapeProcessor {
let config = ValidationConfig::default();
let mut debug_info = DebugInfo::new();
let shape = shape.compute_brep(&config, tolerance, &mut debug_info)?;
let mesh = triangulate(shape.into_inner(), tolerance, &mut debug_info);
let mesh = shape.into_inner().triangulate(tolerance, &mut debug_info);

Ok(ProcessedShape {
aabb,
Expand Down