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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
//! Traits for [`Vector`]s that implement very handy and convient functions.
//! [`FloatingPointVector`] for example implements `normalized()`, which is used heavily in game development.
//!
//! # Traits
//! ```rust
//! pub trait Vector<T, const LEN: usize>: IntoIterator { .. } // For representing a basic [`Vector`].
//! pub trait IntegerVector<T, const LEN: usize>: Vector { .. } // For representing a [`Vector`] with Integers as its generic.
//! pub trait FloatingPointVector<T, const LEN: usize>: Vector { .. } // For representing a [`Vector`] with Floating-point Numbers as its generic.
//! pub trait TuplableVector<T, const LEN: usize>: Vector { .. } // For representing a [`Vector`] that can be converted into a tuple.
//! ```
/// Trait for structs that represent a [`Vector`], will be implemented by default when using the [`impl_vector`] Macro.
pub trait Vector<T, const LEN: usize>: IntoIterator {
/// Returns the name of the [`Vector`] struct.
///
/// # Example
/// ```rust
/// let vector = Vector4::new(0, 0, 0, 0);
/// assert_eq!(vector.name(), "Vector4");
/// ```
fn name(&self) -> &'static str;
/// Returns the size of the [`Vector`] struct in bytes.
///
/// # Example
/// ```rust
/// let vector = Vector2::<u16>::new(0, 0);
/// assert_eq!(vector.size(), 4);
/// ```
fn size(&self) -> usize {
return core::mem::size_of::<T>() * LEN;
}
/// Returns the number of fields in the [`Vector`] struct.
///
/// # Example
/// ```rust
/// let vec2 = Vector2::new(0, 0);
/// let vec3 = Vector3::new(0, 0, 0);
/// let vec4 = Vector4::new(0, 0, 0, 0);
///
/// assert_eq!(vec2.len(), 2);
/// assert_eq!(vec3.len(), 3);
/// assert_eq!(vec4.len(), 4);
/// ```
fn fields(&self) -> usize {
return LEN;
}
/// Converts the given [`Vector`] into an array coresponding to the size of the [`Vector`].
///
/// # Example
/// ```rust
/// let vector = Vector3::new(1, 2, 3);
/// assert_eq!(vector.as_array(), [1, 2, 3]);
/// ```
fn as_array(self) -> [T; LEN];
/// Converts the given [`Vector`] into a [`Vec`] coresponding to the size of the [`Vector`].
///
/// # Example
/// ```rust
/// let vector = Vector3::new(1, 2, 3);
/// assert_eq!(vector.as_vec(), vec![1, 2, 3]);
/// ```
fn as_vec(self) -> std::vec::Vec<T>;
}
/// Trait for structs that represent a [`Vector`] that contains primitive integer data types.
pub trait IntegerVector<T: num_traits::PrimInt, const LEN: usize>: Vector<T, LEN> {
/// Raises all numbers within the [`IntegerVector`] to the specified power.
///
/// # Example
/// ```rust
/// let vector = Vector3::new(2, 4, 6).pow(2);
/// assert_eq!(vector, Vector3::new(4, 16, 36));
/// ```
fn pow(self, n: u32) -> Self;
}
/// Trait for structs that represent a [`Vector`] that contains floating-point data types.
pub trait FloatingPointVector<T: num_traits::Float, const LEN: usize>: Vector<T, LEN> {
/// Converts all numbers within the [`FloatingPointVector`] to zero.
///
/// # Example
/// ```rust
/// let vector = Vector4::new(1.0, 2.0, 3.0, 4.0).zero();
/// assert_eq!(vector, Vector4::new(0.0, 0.0, 0.0, 0.0));
/// ```
fn zero(self) -> Self;
/// Converts all numbers within the [`FloatingPointVector`] to the largest integer less than or equal to the value.
///
/// # Example
/// ```rust
/// let vector = Vector2::new(4.25, 5.9).floor();
/// assert_eq!(vector, Vector2::new(4.0, 5.0));
/// ```
fn floor(self) -> Self;
/// Converts all numbers within the [`FloatingPointVector`] to the largest integer greater than or equal to the value.
///
/// # Example
/// ```rust
/// let vector = Vector2::new(4.25, 5.9).ceil();
/// assert_eq!(vector, Vector2::new(5.0, 6.0));
/// ```
fn ceil(self) -> Self;
/// Converts all numbers within the [`FloatingPointVector`] to the nearest integer.
///
/// # Example
/// ```rust
/// let vector = Vector2::new(4.25, 5.9).round();
/// assert_eq!(vector, Vector2::new(4.0, 6.0));
/// ```
fn round(self) -> Self;
/// Converts all numbers within the [`FloatingPointVector`] to their absolute value.
///
/// # Example
/// ```rust
/// let vector = Vector4::new(-3.0, 4.0, 5.3, -9.87).abs();
/// assert_eq!(vector, Vector4::new(3.0, 4.0, 5.3, 9.87));
/// ```
fn abs(self) -> Self;
/// Raises all numbers within the [`FloatingPointVector`] to an integer power.
///
/// # Example
/// ```rust
/// let vector = Vector2::new(2.0, 4.0).powi(2);
/// assert_eq!(vector, Vector2::new(4, 16));
/// ```
fn powi(self, n: i32) -> Self;
/// Raises all numbers within the [`FloatingPointVector`] to a floating point power.
///
/// # Example
/// ```rust
/// let vector = Vector2::new(2.0, 4.0).powf(2.0);
/// assert_eq!(vector, Vector2::new(4.0, 16.0));
/// ```
fn powf(self, n: T) -> Self;
/// Sets all numbers within the [`FloatingPointVector`] to their integer parts.
///
/// # Example
/// ```rust
/// let vector = Vector3::new(1.5, 2.34, 3.33).trunc();
/// assert_eq!(vector, Vector3::new(1.0, 2.0, 3.0));
/// ```
fn trunc(self) -> Self;
/// Sets all numbers within the [`FloatingPointVector`] to their fractional parts.
///
/// # Example
/// ```rust
/// let vector = Vector3::new(1.5, 2.34, 3.33).fract();
/// assert_eq!(vector, Vector3::new(0.5, 0.34, 0.33));
/// ```
fn fract(self) -> Self;
/// Sets all numbers within the [`FloatingPointVector`] to their square-root.
///
/// # Example
/// ```rust
/// let vector = Vector2::new(64.0, 25.0);
/// assert_eq!(vector, Vector2::new(8.0, 5.0));
/// ```
fn sqrt(self) -> Self;
/// Normalizes the [`FloatingPointVector`].
///
/// # Example
/// ```rust
/// let vector = Vector2::new(14.3, 7.9).normalized();
/// assert_eq!(vector, Vector2::new(0.8753097187762677, 0.48356271177150456));
/// ```
fn normalized(self) -> Self;
/// Linearly interpolates between two [`FloatingPointVector`]s by a normalized `weight`.
///
/// # Example
/// ```rust
/// let vector = Vector2::new(1.0, 2.0).lerp(Vector2::new(2.0, 3.0), 1.0);
/// assert_eq!(vector, Vector2::new(2.0, 3.0));
/// ```
fn lerp(self, to: Self, weight: T) -> Self;
/// Returns the dot product of two [`FloatingPointVector`]s,
/// this can be used to compare the angle between two [`FloatingPointVector`]s.
///
/// # Example
/// ```rust
/// let dot = Vector2::new(1.0, 2.0).dot(Vector2::new(2.0, 4.0));
/// assert_eq!(dot, 10.0);
/// ```
fn dot(self, b: Self) -> T;
/// Returns the squared magnitude of the [`FloatingPointVector`].
/// This will always run faster than [`length`], this method should prefered over it if applicable.
///
/// # Example
/// ```rust
/// let length_sq = Vector3::new(3.33, 2.04, 1.337).length_squared();
/// assert_eq!(length_sq, 17.038069);
/// ```
fn length_squared(self) -> T;
/// Returns the magnitude of the [`FloatingPointVector`].
///
/// # Example
/// ```rust
/// let length = Vector3::new(1.5, 2.0, 3.33).length();
/// assert_eq!(length, 4.16400048030737);
/// ```
fn length(self) -> T
where
Self: Sized
{
return self.length_squared().sqrt();
}
/// Returns a normalized [`FloatingPointVector`] pointing from it to `to`.
///
/// # Example
/// ```rust
/// let from = Vector2::new(1.0, 2.0);
/// let to = Vector2::new(5.0, 6.0);
///
/// let direction = from.direction(to);
///
/// assert_eq!(direction, Vector2::new(0.7071067811865475, 0.7071067811865475));
/// ```
fn direction(self, to: Self) -> Self
where
Self: Sized + core::ops::Sub<Output = Self>
{
return (to - self).normalized();
}
}
/// Trait for structs that represent a [`Vector`] and that can be converted into tuples.
///
/// # Example
/// ```rust
/// pub struct Vector1<T> {
/// pub x: T,
/// }
///
/// // Implement [`Vector`] Trait for `Vector1`
/// impl_vector!(Vector1 { x }, 1);
///
/// impl<T> TuplableVector<T, { Vector1::<()>::LEN }> for Vector1<T> {
/// type Output = (T);
///
/// fn as_tuple(self) -> Self::Output {
/// return (self.x);
/// }
/// }
/// ```
pub trait TuplableVector<T, const LEN: usize>: Vector<T, LEN> {
type Output;
/// Converts the [`TuplableVector`] into a tuple representing its values.
///
/// # Example:
/// ```rust
/// let tuple = Vector2::new(1, 2).as_tuple();
/// assert_eq!(tuple, (1, 2));
/// ```
fn as_tuple(self) -> Self::Output;
}