From 10a5f07d639f53ad4f17cabb4871d23d39473970 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 15 Sep 2022 19:22:10 +0200 Subject: [PATCH 1/4] Add `Scalar::is_negative` --- crates/fj-math/src/scalar.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/fj-math/src/scalar.rs b/crates/fj-math/src/scalar.rs index a977fcf54..cc9a92f8d 100644 --- a/crates/fj-math/src/scalar.rs +++ b/crates/fj-math/src/scalar.rs @@ -70,6 +70,11 @@ impl Scalar { self.0 as u64 } + /// Indicate whether the scalar is negative + pub fn is_negative(self) -> bool { + self < Self::ZERO + } + /// The sign of the scalar /// /// Return `Scalar::ZERO`, if the scalar is zero, `Scalar::ONE`, if it is From 2a126823cc83e5fdd92678361d555e40f6cd676a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 15 Sep 2022 19:48:27 +0200 Subject: [PATCH 2/4] Add `Scalar::is_positive` --- crates/fj-math/src/scalar.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/fj-math/src/scalar.rs b/crates/fj-math/src/scalar.rs index cc9a92f8d..e262d4304 100644 --- a/crates/fj-math/src/scalar.rs +++ b/crates/fj-math/src/scalar.rs @@ -75,6 +75,11 @@ impl Scalar { self < Self::ZERO } + /// Indicate whether the scalar is positive + pub fn is_positive(self) -> bool { + self > Self::ZERO + } + /// The sign of the scalar /// /// Return `Scalar::ZERO`, if the scalar is zero, `Scalar::ONE`, if it is From b03d0363f6b84cf5c6a733a025476fb8102dff49 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 15 Sep 2022 19:48:42 +0200 Subject: [PATCH 3/4] Add `Scalar::is_zero` --- crates/fj-math/src/scalar.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/fj-math/src/scalar.rs b/crates/fj-math/src/scalar.rs index e262d4304..877c16489 100644 --- a/crates/fj-math/src/scalar.rs +++ b/crates/fj-math/src/scalar.rs @@ -80,6 +80,11 @@ impl Scalar { self > Self::ZERO } + /// Indicate whether the scalar is zero + pub fn is_zero(self) -> bool { + self == Scalar::ZERO + } + /// The sign of the scalar /// /// Return `Scalar::ZERO`, if the scalar is zero, `Scalar::ONE`, if it is From 163b7fcde59e134c1078c84c5b5cffcf2d2839f9 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 15 Sep 2022 19:28:24 +0200 Subject: [PATCH 4/4] Improve `Scalar::sign` The returned enum is more useful, as it can be used in a `match` expression. --- crates/fj-math/src/lib.rs | 2 +- crates/fj-math/src/scalar.rs | 42 +++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/crates/fj-math/src/lib.rs b/crates/fj-math/src/lib.rs index 4f5a7ce55..ce3976e99 100644 --- a/crates/fj-math/src/lib.rs +++ b/crates/fj-math/src/lib.rs @@ -54,7 +54,7 @@ pub use self::{ line::Line, point::Point, poly_chain::PolyChain, - scalar::Scalar, + scalar::{Scalar, Sign}, segment::Segment, transform::Transform, triangle::{Triangle, Winding}, diff --git a/crates/fj-math/src/scalar.rs b/crates/fj-math/src/scalar.rs index 877c16489..a9d5b6061 100644 --- a/crates/fj-math/src/scalar.rs +++ b/crates/fj-math/src/scalar.rs @@ -89,12 +89,18 @@ impl 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()) + pub fn sign(self) -> Sign { + if self.is_negative() { + return Sign::Negative; + } + if self.is_positive() { + return Sign::Positive; } + if self.is_zero() { + return Sign::Zero; + } + + unreachable!("Sign is neither negative, nor positive, nor zero.") } /// Compute the absolute value of the scalar @@ -582,3 +588,29 @@ impl approx::AbsDiffEq for Scalar { self.0.abs_diff_eq(&other.0, epsilon.0) } } + +/// The sign of a [`Scalar`] +/// +/// See [`Scalar::sign`] +#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)] +pub enum Sign { + /// The scalar is negative + Negative, + + /// The scalar is positive + Positive, + + /// The scalar is zero + Zero, +} + +impl Sign { + /// Convert this sign back to a scalar + pub fn to_scalar(self) -> Scalar { + match self { + Sign::Negative => -Scalar::ONE, + Sign::Positive => Scalar::ONE, + Sign::Zero => Scalar::ZERO, + } + } +}