vector_basis/
lib.rs

1#![no_std]
2#![deny(missing_docs)]
3/*!
4This crate contains a trait to access the basis vectors of a vector.
5It's recommended not to use this crate and instead stick with the vector space crate.
6Only use it, when it's really necessary to access the vector components, for example when communicating with a render engine.
7**/
8
9use num_traits::One;
10use vector_space::VectorSpace;
11
12/// This trait makes it possible to deal with the bases of a vector in a generic way.
13pub trait Basis<const I: usize>: VectorSpace {
14    /// Creates an unit basis of the vector space.
15    fn unit_basis() -> Self {
16        Self::basis_of(<Self as VectorSpace>::Scalar::one())
17    }
18    /// Creates a scaled basis of the vector space of the specified magnitude.
19    fn basis_of(magnitude: Self::Scalar) -> Self {
20        Self::unit_basis() * magnitude
21    }
22
23    /// Queries a basis of a vector space.
24    fn basis(&self) -> Self::Scalar;
25    /// Queries a basis of a vector space as a mutable reference.
26    fn basis_mut(&mut self) -> &mut Self::Scalar;
27
28    /// Creates a new vector with a single basis set to a different value.
29    fn with_basis(mut self, magnitude: Self::Scalar) -> Self {
30        *self.basis_mut() = magnitude;
31        self
32    }
33}
34
35/// This trait makes it easy to deal with specified bases of a vector in a generic way.
36pub trait Bases: VectorSpace {
37    #[inline]
38    /// Creates the specified unit basis of the vector space.
39    fn unit_bases<const I: usize>() -> Self
40    where
41        Self: Basis<I>,
42    {
43        Basis::unit_basis()
44    }
45    #[inline]
46    /// Creates the specified basis of the vector of the specified magnitude.
47    fn bases_of<const I: usize>(magnitude: Self::Scalar) -> Self
48    where
49        Self: Basis<I>,
50    {
51        Basis::basis_of(magnitude)
52    }
53
54    #[inline]
55    /// Queries the specified basis of a vector space.
56    fn bases<const I: usize>(&self) -> Self::Scalar
57    where
58        Self: Basis<I>,
59    {
60        Basis::basis(self)
61    }
62    #[inline]
63    /// Queries the specified basis of a vector space as a mutable reference.
64    fn bases_mut<const I: usize>(&mut self) -> &mut Self::Scalar
65    where
66        Self: Basis<I>,
67    {
68        Basis::basis_mut(self)
69    }
70
71    #[inline]
72    /// Creates a new vector with the specified basis set to a different value.
73    fn with_bases<const I: usize>(self, magnitude: Self::Scalar) -> Self
74    where
75        Self: Basis<I>,
76    {
77        Basis::with_basis(self, magnitude)
78    }
79}
80
81impl<T: VectorSpace> Bases for T {}