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

Expand API of fj-math #1004

Merged
merged 4 commits into from
Aug 26, 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
2 changes: 1 addition & 1 deletion crates/fj-kernel/src/algorithms/approx/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn approx_circle(
let n = number_of_vertices_for_circle(tolerance, radius);

for i in 0..n {
let angle = Scalar::PI * 2. / n as f64 * i as f64;
let angle = Scalar::TAU / n as f64 * i as f64;
let point = circle.point_from_circle_coords([angle]);
out.push(Local::new([angle], point));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-math/src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<const D: usize> Circle<D> {
let coord = if atan >= Scalar::ZERO {
atan
} else {
atan + Scalar::PI * 2.
atan + Scalar::TAU
};
Point::from([coord])
}
Expand Down
2 changes: 1 addition & 1 deletion crates/fj-math/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<const D: usize> Line<D> {

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

if other_origin_is_not_on_self {
Expand Down
23 changes: 22 additions & 1 deletion crates/fj-math/src/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::{cmp, f64::consts::PI, fmt, hash::Hash, ops};
use std::{
cmp,
f64::consts::{PI, TAU},
fmt,
hash::Hash,
ops,
};

use decorum::R64;

Expand Down Expand Up @@ -28,6 +34,9 @@ impl Scalar {
/// The `Scalar` instance that represents pi
pub const PI: Self = Self(PI);

/// The `Scalar` instance that represents tau
pub const TAU: Self = Self(TAU);

/// Construct a `Scalar` from an `f64`
///
/// # Panics
Expand Down Expand Up @@ -61,6 +70,18 @@ impl Scalar {
self.0 as u64
}

/// The sign of the scalar
///
/// Return `Scalar::ZERO`, if the scalar is zero, `Scalar::ONE`, if it is
/// positive, `-Scalar::ONE`, if it is negative.
pub fn sign(self) -> Scalar {
if self == Self::ZERO {
Self::ZERO
} else {
Self(self.0.signum())
}
}

/// Compute the absolute value of the scalar
pub fn abs(self) -> Self {
self.0.abs().into()
Expand Down
18 changes: 12 additions & 6 deletions crates/fj-math/src/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ pub struct Triangle<const D: usize> {
impl<const D: usize> Triangle<D> {
/// Construct a triangle from three points
///
/// # Panics
///
/// Panics, if the points don't form a triangle.
pub fn from_points(points: [impl Into<Point<D>>; 3]) -> Option<Self> {
/// Returns an error, if the points don't form a triangle.
pub fn from_points(
points: [impl Into<Point<D>>; 3],
) -> Result<Self, NotATriangle<D>> {
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) {
Some(Self { points })
Ok(Self { points })
} else {
None
Err(NotATriangle { points })
}
}

Expand Down Expand Up @@ -107,6 +107,12 @@ where
}
}

/// Returned by [`Triangle::from_points`], if the points don't form a triangle
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct NotATriangle<const D: usize> {
pub points: [Point<D>; 3],
}

/// Winding direction of a triangle.
pub enum Winding {
/// Counter-clockwise
Expand Down