mirror of
https://github.com/hannobraun/Fornjot
synced 2025-05-05 10:28:27 +00:00
Re-use fj_math::Scalar
This commit is contained in:
parent
e9c32da93b
commit
a3f19e81bb
@ -1,114 +1 @@
|
||||
use std::{cmp::Ordering, ops};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Scalar {
|
||||
value: f64,
|
||||
}
|
||||
|
||||
impl Scalar {
|
||||
pub const ZERO: Self = Self { value: 0. };
|
||||
|
||||
pub fn from_f64(value: f64) -> Self {
|
||||
if value.is_nan() {
|
||||
panic!("`Scalar` value must not be NaN");
|
||||
}
|
||||
if value.is_infinite() {
|
||||
panic!("`Scalar` value must not be infinite. Value: `{value}`");
|
||||
}
|
||||
|
||||
Self { value }
|
||||
}
|
||||
|
||||
pub fn into_f64(self) -> f64 {
|
||||
self.value
|
||||
}
|
||||
|
||||
pub fn sqrt(self) -> Self {
|
||||
let value = self.into_f64().sqrt();
|
||||
Self::from_f64(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for Scalar {}
|
||||
|
||||
impl Ord for Scalar {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
let Some(ordering) = self.value.partial_cmp(&other.value) else {
|
||||
unreachable!(
|
||||
"Failed to compare `Scalar` values `{}` and `{}`",
|
||||
self.value, other.value
|
||||
);
|
||||
};
|
||||
|
||||
ordering
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Scalar {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f64> for Scalar {
|
||||
fn from(value: f64) -> Self {
|
||||
Self::from_f64(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ops::Add<S> for Scalar
|
||||
where
|
||||
S: Into<Scalar>,
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, other: S) -> Self::Output {
|
||||
let value = self.into_f64() + other.into().into_f64();
|
||||
Self::from_f64(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ops::Div<S> for Scalar
|
||||
where
|
||||
S: Into<Scalar>,
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, other: S) -> Self::Output {
|
||||
let value = self.into_f64() / other.into().into_f64();
|
||||
Self::from_f64(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ops::Mul<S> for Scalar
|
||||
where
|
||||
S: Into<Scalar>,
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, other: S) -> Self::Output {
|
||||
let value = self.into_f64() * other.into().into_f64();
|
||||
Self::from_f64(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Neg for Scalar {
|
||||
type Output = Self;
|
||||
|
||||
fn neg(self) -> Self::Output {
|
||||
let value = -self.into_f64();
|
||||
Self::from_f64(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ops::Sub<S> for Scalar
|
||||
where
|
||||
S: Into<Scalar>,
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, other: S) -> Self::Output {
|
||||
let value = self.into_f64() - other.into().into_f64();
|
||||
Self::from_f64(value)
|
||||
}
|
||||
}
|
||||
pub use fj_math::Scalar;
|
||||
|
Loading…
Reference in New Issue
Block a user