Skip to content

Commit

Permalink
Merge pull request #716 from hannobraun/triangulate
Browse files Browse the repository at this point in the history
Break `triangulate`'s dependency on `Shape`
  • Loading branch information
hannobraun authored Jun 22, 2022
2 parents 3367b0d + fff4359 commit ad7e808
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
26 changes: 14 additions & 12 deletions crates/fj-kernel/src/algorithms/triangulation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ mod ray;
use fj_interop::{debug::DebugInfo, mesh::Mesh};
use fj_math::Point;

use crate::{objects::Face, shape::Shape};
use crate::objects::Face;

use self::polygon::Polygon;

use super::{FaceApprox, Tolerance};

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

for face in shape.faces() {
let face = face.get();
for face in faces {
match &face {
Face::Face(brep) => {
let surface = brep.surface.get();
Expand Down Expand Up @@ -102,16 +101,18 @@ mod tests {
let c = [2., 2.];
let d = [0., 1.];

Face::builder(Surface::xy_plane(), &mut shape)
let face = Face::builder(Surface::xy_plane(), &mut shape)
.with_exterior_polygon([a, b, c, d])
.build();
.build()
.get();

let a = Point::from(a).to_xyz();
let b = Point::from(b).to_xyz();
let c = Point::from(c).to_xyz();
let d = Point::from(d).to_xyz();

let triangles = triangulate(shape)?;
let triangles = triangulate(face)?;

assert!(triangles.contains_triangle([a, b, d]));
assert!(triangles.contains_triangle([b, c, d]));
assert!(!triangles.contains_triangle([a, b, c]));
Expand All @@ -134,12 +135,13 @@ mod tests {
let g = [3., 3.];
let h = [1., 2.];

Face::builder(Surface::xy_plane(), &mut shape)
let face = Face::builder(Surface::xy_plane(), &mut shape)
.with_exterior_polygon([a, b, c, d])
.with_interior_polygon([e, f, g, h])
.build();
.build()
.get();

let triangles = triangulate(shape)?;
let triangles = triangulate(face)?;

let a = Point::from(a).to_xyz();
let d = Point::from(d).to_xyz();
Expand All @@ -162,10 +164,10 @@ mod tests {
Ok(())
}

fn triangulate(shape: Shape) -> anyhow::Result<Mesh<Point<3>>> {
fn triangulate(face: Face) -> anyhow::Result<Mesh<Point<3>>> {
let tolerance = Tolerance::from_scalar(Scalar::ONE)?;

let mut debug_info = DebugInfo::new();
Ok(super::triangulate(shape, tolerance, &mut debug_info))
Ok(super::triangulate(vec![face], tolerance, &mut debug_info))
}
}
8 changes: 6 additions & 2 deletions crates/fj-operations/src/shape_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ impl ShapeProcessor {

let config = ValidationConfig::default();
let mut debug_info = DebugInfo::new();
let shape = shape.to_shape(&config, tolerance, &mut debug_info)?;
let mesh = triangulate(shape.into_inner(), tolerance, &mut debug_info);
let shape = shape
.to_shape(&config, tolerance, &mut debug_info)?
.faces()
.map(|handle| handle.get())
.collect();
let mesh = triangulate(shape, tolerance, &mut debug_info);

Ok(ProcessedShape {
aabb,
Expand Down

0 comments on commit ad7e808

Please sign in to comment.