Skip to content

Commit

Permalink
Make Triangle::from_points fallible
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Jul 28, 2022
1 parent cf98894 commit 58dceaa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/fj-kernel/src/algorithms/triangulate/delaunay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn triangulate(points: Vec<Local<Point<2>>>) -> Vec<[Local<Point<2>>; 3]> {
*v1.local_form(),
*v2.local_form(),
])
.expect("invalid triangle")
.winding_direction();

let triangle = match orientation {
Expand Down
8 changes: 4 additions & 4 deletions crates/fj-math/src/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<const D: usize> Triangle<D> {
/// # Panics
///
/// Panics, if the points don't form a triangle.
pub fn from_points(points: [impl Into<Point<D>>; 3]) -> Self {
pub fn from_points(points: [impl Into<Point<D>>; 3]) -> Option<Self> {
let points = points.map(Into::into);

let area = {
Expand All @@ -31,9 +31,9 @@ impl<const D: usize> Triangle<D> {

// A triangle is not valid if it doesn't span any area
if area != Scalar::from(0.0) {
Self { points }
Some(Self { points })
} else {
panic!("Invalid Triangle specified");
None
}
}

Expand Down Expand Up @@ -103,7 +103,7 @@ where
P: Into<Point<D>>,
{
fn from(points: [P; 3]) -> Self {
Self::from_points(points)
Self::from_points(points).expect("invalid triangle")
}
}

Expand Down

0 comments on commit 58dceaa

Please sign in to comment.