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 API of VerticesOfEdge #998

Merged
merged 4 commits into from
Aug 25, 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
6 changes: 3 additions & 3 deletions crates/fj-kernel/src/algorithms/intersect/face_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ impl Intersect for (&Face, &Point<2>) {
);
}
(Some(RaySegmentIntersection::RayStartsOnOnFirstVertex), _) => {
let vertex = edge.vertices().expect_vertices()[0];
let vertex = *edge.vertices().get_or_panic()[0];
return Some(
FacePointIntersection::PointIsOnVertex(vertex)
);
}
(Some(RaySegmentIntersection::RayStartsOnSecondVertex), _) => {
let vertex = edge.vertices().expect_vertices()[1];
let vertex = *edge.vertices().get_or_panic()[1];
return Some(
FacePointIntersection::PointIsOnVertex(vertex)
);
Expand Down Expand Up @@ -236,7 +236,7 @@ mod tests {
.edge_iter()
.copied()
.find(|edge| {
let [a, b] = edge.vertices().expect_vertices();
let [a, b] = edge.vertices().get_or_panic();
a.global().position() == Point::from([0., 0., 0.])
&& b.global().position() == Point::from([2., 0., 0.])
})
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/intersect/ray_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Intersect for (&HorizontalRayToTheRight<2>, &Edge) {
}
};

let points = edge.vertices().expect_vertices().map(|vertex| {
let points = edge.vertices().get_or_panic().map(|vertex| {
let point = vertex.position();
line.point_from_line_coords(point)
});
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/intersect/ray_face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ mod tests {
.edge_iter()
.copied()
.find(|edge| {
let [a, b] = edge.vertices().expect_vertices();
let [a, b] = edge.vertices().get_or_panic();
dbg!((a.global().position(), b.global().position()));
a.global().position() == Point::from([1., 0., 1.])
&& b.global().position() == Point::from([1., 0., -1.])
Expand Down
40 changes: 3 additions & 37 deletions crates/fj-kernel/src/objects/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::builder::EdgeBuilder;

use super::{Curve, Vertex};

/// An edge of a shape
/// An edge
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct Edge {
curve: Curve,
Expand Down Expand Up @@ -62,11 +62,6 @@ impl fmt::Display for Edge {
pub struct VerticesOfEdge(Option<[Vertex; 2]>);

impl VerticesOfEdge {
/// Construct an instance of `VerticesOfEdge` from zero or two vertices
pub fn new(vertices: Option<[Vertex; 2]>) -> Self {
Self(vertices)
}

/// Construct an instance of `VerticesOfEdge` from two vertices
pub fn from_vertices(vertices: [Vertex; 2]) -> Self {
Self(Some(vertices))
Expand All @@ -77,19 +72,6 @@ impl VerticesOfEdge {
Self(None)
}

/// Determine whether the other instance has the same vertices
///
/// The order of vertices is ignored.
pub fn are_same(&self, other: &Self) -> bool {
if let Some([a, b]) = self.0 {
if let Some(other) = other.0 {
return [a, b] == other || [b, a] == other;
}
}

false
}

/// Access the vertices
pub fn get(&self) -> Option<[&Vertex; 2]> {
self.0.as_ref().map(|vertices| {
Expand All @@ -105,8 +87,8 @@ impl VerticesOfEdge {
/// # Panics
///
/// Panics, if the edge has no vertices.
pub fn expect_vertices(self) -> [Vertex; 2] {
self.0.expect("Expected edge to have vertices")
pub fn get_or_panic(&self) -> [&Vertex; 2] {
self.get().expect("Expected edge to have vertices")
}

/// Iterate over the vertices
Expand Down Expand Up @@ -141,20 +123,4 @@ impl VerticesOfEdge {
{
self.0.map(|vertices| vertices.map(f))
}

/// Convert each vertex using the provided fallible function
pub fn try_convert<F, T, E>(self, f: F) -> Result<Option<[T; 2]>, E>
where
F: FnMut(Vertex) -> Result<T, E>,
{
// Can be cleaned up using `try_map`, once that is stable:
// https://doc.rust-lang.org/std/primitive.array.html#method.try_map
let vertices: Option<[Result<_, E>; 2]> = self.convert(f);
let vertices = match vertices {
Some([a, b]) => Some([a?, b?]),
None => None,
};

Ok(vertices)
}
}