1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use num_traits::{real::Real, One, Zero};
use std::{
    cmp::PartialOrd,
    ops::{Add, Div, Mul, Neg, Sub},
};

/// This trait specifies some type to be a vector type.
/// It specifies the scalar type and is required for other vector types.
pub trait VectorSpace: Copy + Zero + PartialEq
where
    Self: Add<Self, Output = Self>,
    Self: Sub<Self, Output = Self>,
    Self: Mul<<Self as VectorSpace>::Scalar, Output = Self>,
    Self: Div<<Self as VectorSpace>::Scalar, Output = Self>,
    Self: Neg<Output = Self>,
{
    /// The scalar type of the vector space.
    type Scalar: Real + PartialOrd;

    /// The linear interpolation of two vectors.
    fn lerp(self, other: Self, amount: Self::Scalar) -> Self {
        self * (Self::Scalar::one() - amount) + other * amount
    }
}

impl VectorSpace for f32 {
    type Scalar = f32;
}

impl VectorSpace for f64 {
    type Scalar = f64;
}