Re-use fj_math::Scalar

This commit is contained in:
Hanno Braun 2025-03-21 22:21:10 +01:00
parent e9c32da93b
commit a3f19e81bb

View File

@ -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;