Skip to content

Commit

Permalink
Merge pull request #855 from hannobraun/face
Browse files Browse the repository at this point in the history
Clean up `Face`
  • Loading branch information
hannobraun authored Jul 20, 2022
2 parents 2bb68e2 + 0ba85d0 commit d331f99
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 272 deletions.
4 changes: 2 additions & 2 deletions crates/fj-kernel/src/algorithms/approx/faces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ impl FaceApprox {
let mut interiors = HashSet::new();

for cycle in face.exteriors() {
let cycle = CycleApprox::new(&cycle, tolerance);
let cycle = CycleApprox::new(cycle, tolerance);

points.extend(cycle.points.iter().copied());
exteriors.push(cycle);
}
for cycle in face.interiors() {
let cycle = CycleApprox::new(&cycle, tolerance);
let cycle = CycleApprox::new(cycle, tolerance);

points.extend(cycle.points.iter().copied());
interiors.insert(cycle);
Expand Down
29 changes: 12 additions & 17 deletions crates/fj-kernel/src/algorithms/reverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@ use fj_math::{Circle, Line, Point, Vector};

use crate::{
local::Local,
objects::{Curve, Cycle, CyclesInFace, Edge, Face},
objects::{Curve, Cycle, Edge, Face},
};

/// Reverse the direction of a face
pub fn reverse_face(face: &Face) -> Face {
let face = match face {
Face::Face(face) => face,
Face::Triangles(_) => {
panic!("Reversing tri-rep faces is not supported")
}
};
if face.triangles().is_some() {
panic!("Reversing tri-rep faces is not supported");
}

let surface = face.surface().reverse();

let exteriors = reverse_local_coordinates_in_cycle(&face.exteriors);
let interiors = reverse_local_coordinates_in_cycle(&face.interiors);
let exteriors = reverse_local_coordinates_in_cycle(face.exteriors());
let interiors = reverse_local_coordinates_in_cycle(face.interiors());

Face::new(surface, exteriors, interiors, face.color)
Face::new(surface, exteriors, interiors, face.color())
}

fn reverse_local_coordinates_in_cycle(
cycles: &CyclesInFace,
) -> impl Iterator<Item = Cycle> + '_ {
let cycles = cycles.as_local().map(|cycle| {
fn reverse_local_coordinates_in_cycle<'r>(
cycles: impl IntoIterator<Item = &'r Cycle> + 'r,
) -> impl Iterator<Item = Cycle> + 'r {
cycles.into_iter().map(|cycle| {
let edges = cycle
.edges
.iter()
Expand Down Expand Up @@ -57,9 +54,7 @@ fn reverse_local_coordinates_in_cycle(
.collect();

Cycle { edges }
});

cycles
})
}

#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions crates/fj-kernel/src/algorithms/sweep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn sweep(
);

for cycle in face.all_cycles() {
for edge in cycle.edges {
for edge in &cycle.edges {
if let Some(vertices) = edge.vertices().get() {
create_non_continuous_side_face(
path,
Expand All @@ -52,7 +52,7 @@ pub fn sweep(
}

create_continuous_side_face(
edge,
*edge,
path,
tolerance,
color,
Expand Down Expand Up @@ -193,7 +193,7 @@ fn create_continuous_side_face(
side_face.push(([v0, v2, v3].into(), color));
}

target.push(Face::Triangles(side_face));
target.push(Face::from_triangles(side_face));
}

#[cfg(test)]
Expand Down
53 changes: 22 additions & 31 deletions crates/fj-kernel/src/algorithms/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use fj_math::{Transform, Vector};
use crate::{
local::Local,
objects::{
Curve, Cycle, CyclesInFace, Edge, Face, FaceBRep, GlobalVertex, Sketch,
Solid, Surface, Vertex,
Curve, Cycle, Edge, Face, GlobalVertex, Sketch, Solid, Surface, Vertex,
},
};

Expand Down Expand Up @@ -72,33 +71,25 @@ impl TransformObject for Edge {

impl TransformObject for Face {
fn transform(self, transform: &Transform) -> Self {
match self {
Self::Face(face) => {
let surface = face.surface.transform(transform);
if let Some(triangles) = self.triangles() {
let mut target = Vec::new();

let exteriors = transform_cycles(&face.exteriors, transform);
let interiors = transform_cycles(&face.interiors, transform);
for (triangle, color) in triangles.clone() {
let triangle = transform.transform_triangle(&triangle);
target.push((triangle, color));
}

let color = face.color;
return Self::from_triangles(target);
}

Self::Face(FaceBRep {
surface,
exteriors,
interiors,
color,
})
}
Self::Triangles(triangles) => {
let mut target = Vec::new();
let surface = self.surface().transform(transform);

for (triangle, color) in triangles {
let triangle = transform.transform_triangle(&triangle);
target.push((triangle, color));
}
let exteriors = transform_cycles(self.exteriors(), transform);
let interiors = transform_cycles(self.interiors(), transform);

Self::Triangles(target)
}
}
let color = self.color();

Face::new(surface, exteriors, interiors, color)
}
}

Expand Down Expand Up @@ -152,11 +143,11 @@ pub fn transform_faces(faces: &mut Vec<Face>, transform: &Transform) {
}
}

fn transform_cycles(
cycles: &CyclesInFace,
transform: &Transform,
) -> CyclesInFace {
let cycles = cycles.as_local().map(|cycle| cycle.transform(transform));

CyclesInFace::new(cycles)
fn transform_cycles<'a>(
cycles: impl IntoIterator<Item = &'a Cycle> + 'a,
transform: &'a Transform,
) -> impl Iterator<Item = Cycle> + 'a {
cycles
.into_iter()
.map(|cycle| cycle.clone().transform(transform))
}
73 changes: 33 additions & 40 deletions crates/fj-kernel/src/algorithms/triangulate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,40 @@ pub fn triangulate(
let mut mesh = Mesh::new();

for face in faces {
match &face {
Face::Face(brep) => {
let surface = brep.surface;
let approx = FaceApprox::new(&face, tolerance);

let points: Vec<_> = approx.points.into_iter().collect();
let face_as_polygon = Polygon::new(surface)
.with_exterior(
approx
.exterior
.points
.into_iter()
.map(|point| point.local()),
)
.with_interiors(approx.interiors.into_iter().map(
|interior| {
interior
.points
.into_iter()
.map(|point| point.local())
},
));

let mut triangles = delaunay::triangulate(points);
triangles.retain(|triangle| {
face_as_polygon.contains_triangle(
triangle.map(|point| point.local()),
debug_info,
)
});

for triangle in triangles {
let points = triangle.map(|point| point.global());
mesh.push_triangle(points, brep.color);
}
}
Face::Triangles(triangles) => {
for &(triangle, color) in triangles {
mesh.push_triangle(triangle.points(), color);
}
if let Some(triangles) = face.triangles() {
for &(triangle, color) in triangles {
mesh.push_triangle(triangle.points(), color);
}
continue;
}

let surface = face.surface();
let approx = FaceApprox::new(&face, tolerance);

let points: Vec<_> = approx.points.into_iter().collect();
let face_as_polygon = Polygon::new(*surface)
.with_exterior(
approx
.exterior
.points
.into_iter()
.map(|point| point.local()),
)
.with_interiors(approx.interiors.into_iter().map(|interior| {
interior.points.into_iter().map(|point| point.local())
}));

let mut triangles = delaunay::triangulate(points);
triangles.retain(|triangle| {
face_as_polygon.contains_triangle(
triangle.map(|point| point.local()),
debug_info,
)
});

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

Expand Down
Loading

0 comments on commit d331f99

Please sign in to comment.