Skip to main content

euv_engine/math/
trait.rs

1use super::*;
2
3/// A trait for types that support linear interpolation between two values.
4pub trait Interpolable {
5    /// Performs linear interpolation between `self` and `other` by the factor `t`.
6    ///
7    /// # Arguments
8    ///
9    /// - `f64` - The interpolation factor, typically in the range 0.0 to 1.0.
10    /// - `Self` - The target value to interpolate towards.
11    ///
12    /// # Returns
13    ///
14    /// - `Self` - The interpolated result.
15    fn lerp(&self, other: Self, t: f64) -> Self;
16}
17
18/// A trait abstracting the operations common to `Vector2D` and `Vector3D`.
19///
20/// Provides a single abstraction surface so that dimension-agnostic algorithms
21/// (e.g. `integrate_linear` in physics) can be written once. Method signatures
22/// match the inherent implementations on both vectors: taking `&self` and
23/// returning new owned values where the inherent methods do, taking `self` by
24/// value where they already do.
25///
26/// The arithmetic operator supertraits (`Add`, `Sub`, `Mul<f64>`, `Neg`, and
27/// the `*Assign` variants) are required so generic code can use natural
28/// `a + b`, `v * 2.0`, `-v`, `v += other`, `v *= scalar` syntax without
29/// importing additional bounds.
30///
31/// `Vector2D` adds 2D-specific operations (`perp`, `cross -> f64`,
32/// `from_angle`) and `Vector3D` adds 3D-specific ones (`cross -> Vector3D`).
33/// These are intentionally **not** part of the trait — they have different
34/// return types or only exist in one dimension.
35pub trait Vector:
36    Copy
37    + Clone
38    + Default
39    + Debug
40    + PartialEq
41    + Add<Output = Self>
42    + Sub<Output = Self>
43    + Mul<f64, Output = Self>
44    + Neg<Output = Self>
45    + AddAssign
46    + SubAssign
47    + MulAssign<f64>
48{
49    /// Returns the additive identity (`Self::default()`).
50    ///
51    /// # Returns
52    ///
53    /// - `Self` - The zero vector for this dimension.
54    fn zero() -> Self;
55
56    /// Computes the dot product with another vector.
57    ///
58    /// # Arguments
59    ///
60    /// - `&Self` - The other vector.
61    ///
62    /// # Returns
63    ///
64    /// - `f64` - The dot product.
65    fn dot(&self, other: Self) -> f64;
66
67    /// Returns the magnitude (length) of the vector.
68    ///
69    /// # Returns
70    ///
71    /// - `f64` - The magnitude.
72    fn magnitude(&self) -> f64;
73
74    /// Returns the squared magnitude of the vector.
75    ///
76    /// Faster than `magnitude` for comparison-only use cases because it
77    /// avoids a square root.
78    ///
79    /// # Returns
80    ///
81    /// - `f64` - The squared magnitude.
82    fn magnitude_squared(&self) -> f64;
83
84    /// Returns a normalized (unit length) copy of this vector.
85    ///
86    /// Implementations may return the zero vector when the magnitude is
87    /// below an epsilon threshold.
88    ///
89    /// # Returns
90    ///
91    /// - `Self` - The normalized vector.
92    fn normalized(&self) -> Self;
93
94    /// Returns a copy of this vector scaled by the given factor.
95    ///
96    /// # Arguments
97    ///
98    /// - `f64` - The scalar factor.
99    ///
100    /// # Returns
101    ///
102    /// - `Self` - The scaled vector.
103    fn scaled(&self, scalar: f64) -> Self;
104
105    /// Performs linear interpolation between `self` and `other` by `t`.
106    ///
107    /// # Arguments
108    ///
109    /// - `&Self` - The target vector.
110    /// - `f64` - The interpolation factor, typically in the range 0.0 to 1.0.
111    ///
112    /// # Returns
113    ///
114    /// - `Self` - The interpolated vector.
115    fn lerp(&self, other: Self, t: f64) -> Self;
116}