From 285ba0a9ae8596003d7a6260dfbcbf0272457d7d Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 26 Aug 2022 11:22:53 +0200 Subject: [PATCH 1/4] Add `Scalar::TAU` --- crates/fj-math/src/scalar.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/fj-math/src/scalar.rs b/crates/fj-math/src/scalar.rs index 96a258638..77b0972b3 100644 --- a/crates/fj-math/src/scalar.rs +++ b/crates/fj-math/src/scalar.rs @@ -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; @@ -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 From ad89452fe8944c2d41235e273e57107043dc5e39 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 26 Aug 2022 11:27:27 +0200 Subject: [PATCH 2/4] Make use of `Scalar::TAU` --- crates/fj-kernel/src/algorithms/approx/curve.rs | 2 +- crates/fj-math/src/circle.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/approx/curve.rs b/crates/fj-kernel/src/algorithms/approx/curve.rs index 34c0a9cde..25b6470a9 100644 --- a/crates/fj-kernel/src/algorithms/approx/curve.rs +++ b/crates/fj-kernel/src/algorithms/approx/curve.rs @@ -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)); } diff --git a/crates/fj-math/src/circle.rs b/crates/fj-math/src/circle.rs index 31ccba817..3e12173cd 100644 --- a/crates/fj-math/src/circle.rs +++ b/crates/fj-math/src/circle.rs @@ -107,7 +107,7 @@ impl Circle { let coord = if atan >= Scalar::ZERO { atan } else { - atan + Scalar::PI * 2. + atan + Scalar::TAU }; Point::from([coord]) } From b2f5e0999270a78fdcea38a5164eec0cab79a016 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 26 Aug 2022 11:40:58 +0200 Subject: [PATCH 3/4] Improve error handling of `Triangle::new` --- crates/fj-math/src/line.rs | 2 +- crates/fj-math/src/triangle.rs | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/fj-math/src/line.rs b/crates/fj-math/src/line.rs index 4bfed5413..9d247ee7e 100644 --- a/crates/fj-math/src/line.rs +++ b/crates/fj-math/src/line.rs @@ -74,7 +74,7 @@ impl Line { // 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 { diff --git a/crates/fj-math/src/triangle.rs b/crates/fj-math/src/triangle.rs index f4cd21888..02e87b7a4 100644 --- a/crates/fj-math/src/triangle.rs +++ b/crates/fj-math/src/triangle.rs @@ -18,10 +18,10 @@ pub struct Triangle { impl Triangle { /// Construct a triangle from three points /// - /// # Panics - /// - /// Panics, if the points don't form a triangle. - pub fn from_points(points: [impl Into>; 3]) -> Option { + /// Returns an error, if the points don't form a triangle. + pub fn from_points( + points: [impl Into>; 3], + ) -> Result> { let points = points.map(Into::into); let area = { @@ -31,9 +31,9 @@ impl Triangle { // 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 }) } } @@ -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 { + pub points: [Point; 3], +} + /// Winding direction of a triangle. pub enum Winding { /// Counter-clockwise From 26da77364f0320d8468886e7f6fb127616aafa72 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Fri, 26 Aug 2022 12:20:20 +0200 Subject: [PATCH 4/4] Add `Scalar::sign` --- crates/fj-math/src/scalar.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/fj-math/src/scalar.rs b/crates/fj-math/src/scalar.rs index 77b0972b3..74407ac9b 100644 --- a/crates/fj-math/src/scalar.rs +++ b/crates/fj-math/src/scalar.rs @@ -70,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()