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