Skip to content

Commit

Permalink
Merge pull request #1050 from hannobraun/triangulate
Browse files Browse the repository at this point in the history
Make triangulation more flexible
  • Loading branch information
hannobraun authored Sep 7, 2022
2 parents 37f21be + a085baf commit 8d3dfb1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
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

0 comments on commit 8d3dfb1

Please sign in to comment.