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

Add Approximation::for_face #247

Merged
merged 3 commits into from
Feb 24, 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
88 changes: 86 additions & 2 deletions src/kernel/approximation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::collections::HashSet;

use crate::math::{Point, Scalar, Segment};

use super::topology::edges::{Cycle, Edge, Edges};
use super::topology::{
edges::{Cycle, Edge, Edges},
faces::Face,
};

/// An approximation of an edge, multiple edges, or a face
#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -106,6 +109,35 @@ impl Approximation {

Self { points, segments }
}

/// Compute an approximation for a face
///
/// `tolerance` defines how far the approximation is allowed to deviate from
/// the actual edges.
pub fn for_face(face: &Face, tolerance: Scalar) -> Self {
// Curved faces whose curvature is not fully defined by their edges
// are not supported yet. For that reason, we can fully ignore `face`'s
// `surface` field and just pass the edges to `Self::for_edges`.
//
// An example of a curved face that is supported, is the cylinder. Its
// curvature is fully defined be the edges (circles) that border it. The
// circle approximations are sufficient to triangulate the surface.
//
// An example of a curved face that is currently not supported, and thus
// doesn't need to be handled here, is a sphere. A spherical face would
// would need to provide its own approximation, as the edges that bound
// it have nothing to do with its curvature.
match face {
Face::Face { surface: _, edges } => {
Self::for_edges(edges, tolerance)
}
_ => {
// No code that still uses triangle representation calls this
// method.
unreachable!()
}
}
}
}

#[cfg(test)]
Expand All @@ -116,9 +148,10 @@ mod tests {

use crate::{
kernel::{
geometry::Curve,
geometry::{Curve, Surface},
topology::{
edges::{Cycle, Edge, Edges},
faces::Face,
vertices::Vertex,
},
},
Expand Down Expand Up @@ -253,4 +286,55 @@ mod tests {
}
);
}

#[test]
fn for_face_closed() {
// Test a closed face, i.e. one that is completely encircled by edges.

let tolerance = Scalar::ONE;

let a = Point::from([1., 2., 3.]);
let b = Point::from([2., 3., 5.]);
let c = Point::from([3., 5., 8.]);
let d = Point::from([5., 8., 13.]);

let v1 = Vertex::create_at(a);
let v2 = Vertex::create_at(b);
let v3 = Vertex::create_at(c);
let v4 = Vertex::create_at(d);

let curve = Curve::Mock {
approx: Vec::new(),
coords: RefCell::new(vec![Point::from([0.]), Point::from([1.])]),
};

let ab = Edge::new(curve.clone(), Some([v1, v2]));
let bc = Edge::new(curve.clone(), Some([v2, v3]));
let cd = Edge::new(curve.clone(), Some([v3, v4]));
let da = Edge::new(curve.clone(), Some([v4, v1]));

let abcd = Cycle {
edges: vec![ab, bc, cd, da],
};

let edges = Edges { cycles: vec![abcd] };

let face = Face::Face {
surface: Surface::x_y_plane(),
edges,
};

assert_eq!(
Approximation::for_face(&face, tolerance),
Approximation {
points: set![a, b, c, d],
segments: set![
Segment::from([a, b]),
Segment::from([b, c]),
Segment::from([c, d]),
Segment::from([d, a]),
],
}
);
}
}
4 changes: 2 additions & 2 deletions src/kernel/topology/faces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ impl Face {
debug_info: &mut DebugInfo,
) {
match self {
Self::Face { edges, surface } => {
let approx = Approximation::for_edges(edges, tolerance);
Self::Face { surface, .. } => {
let approx = Approximation::for_face(self, tolerance);

let points: Vec<_> = approx
.points
Expand Down