Skip to content

Commit

Permalink
Merge pull request #887 from hannobraun/math
Browse files Browse the repository at this point in the history
Make `Triangle::from_points` fallible; add `Line::is_coincident_with`
  • Loading branch information
hannobraun authored Jul 28, 2022
2 parents cf98894 + 4fe3aa0 commit 7684e10
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 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
42 changes: 41 additions & 1 deletion crates/fj-math/src/line.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Point, Vector};
use crate::{Point, Triangle, Vector};

/// An n-dimensional line, defined by an origin and a direction
///
Expand Down Expand Up @@ -33,6 +33,33 @@ impl<const D: usize> Line<D> {
}
}

/// Determine if this line is coincident with another line
///
/// # Implementation Note
///
/// This method only returns `true`, if the lines are precisely coincident.
/// This will probably not be enough going forward, but it'll do for now.
pub fn is_coincident_with(&self, other: &Self) -> bool {
let other_origin_is_not_on_self = {
let a = other.origin;
let b = self.origin;
let c = self.origin + self.direction;

// The triangle is valid only, if the three points are not on the
// same line.
Triangle::from_points([a, b, c]).is_some()
};

if other_origin_is_not_on_self {
return false;
}

let d1 = self.direction.normalize();
let d2 = other.direction.normalize();

d1 == d2 || d1 == -d2
}

/// Create a new instance that is reversed
#[must_use]
pub fn reverse(mut self) -> Self {
Expand Down Expand Up @@ -102,6 +129,19 @@ mod tests {

use super::Line;

#[test]
fn is_coincident_with() {
let line = Line::from_points([[0., 0.], [1., 0.]]);

let a = Line::from_points([[0., 0.], [1., 0.]]);
let b = Line::from_points([[0., 0.], [-1., 0.]]);
let c = Line::from_points([[0., 1.], [1., 1.]]);

assert!(line.is_coincident_with(&a));
assert!(line.is_coincident_with(&b));
assert!(!line.is_coincident_with(&c));
}

#[test]
fn convert_point_to_line_coords() {
let line = Line {
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 7684e10

Please sign in to comment.