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

Clean up Face #855

Merged
merged 26 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
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