Skip to main content

euv_engine/math/
struct.rs

1use super::*;
2
3/// A zero-sized namespace struct providing static math utility methods.
4///
5/// This struct follows the same pattern as `App`, serving as a namespace for
6/// free-standing mathematical functions such as `clamp`, `lerp`, angle conversions,
7/// and interpolation helpers.
8#[derive(Clone, Copy, Data, Debug, Default, Eq, Hash, New, Ord, PartialEq, PartialOrd)]
9pub struct Numeric;
10
11/// A 2D vector with `x` and `y` components represented as `f64`.
12#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
13pub struct Vector2D {
14    /// The horizontal component of the vector.
15    #[get(type(copy))]
16    #[get_mut(pub(crate))]
17    pub x: f64,
18    /// The vertical component of the vector.
19    #[get(type(copy))]
20    #[get_mut(pub(crate))]
21    pub y: f64,
22}
23
24/// An axis-aligned rectangle defined by its top-left corner and dimensions.
25#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
26pub struct Rect {
27    /// The x coordinate of the top-left corner.
28    #[get(type(copy))]
29    #[get_mut(pub(crate))]
30    pub x: f64,
31    /// The y coordinate of the top-left corner.
32    #[get(type(copy))]
33    #[get_mut(pub(crate))]
34    pub y: f64,
35    /// The width of the rectangle.
36    #[get(type(copy))]
37    #[get_mut(pub(crate))]
38    pub width: f64,
39    /// The height of the rectangle.
40    #[get(type(copy))]
41    #[get_mut(pub(crate))]
42    pub height: f64,
43}
44
45/// A circle defined by its center point and radius.
46#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
47pub struct Circle {
48    /// The center point of the circle.
49    #[get(type(copy))]
50    #[get_mut(pub(crate))]
51    #[set(pub(crate))]
52    pub center: Vector2D,
53    /// The radius of the circle.
54    #[get(type(copy))]
55    #[get_mut(pub(crate))]
56    #[set(pub(crate))]
57    pub radius: f64,
58}
59
60/// A 2D transform composed of position, rotation (in radians), and scale.
61#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
62pub struct Transform2D {
63    /// The position offset of the transform.
64    #[get(type(copy))]
65    #[get_mut(pub(crate))]
66    pub position: Vector2D,
67    /// The rotation angle in radians.
68    #[get(pub(crate), type(copy))]
69    #[get_mut(pub(crate))]
70    #[set(pub(crate))]
71    pub(crate) rotation: f64,
72    /// The scale factors for each axis.
73    #[get(pub(crate), type(copy))]
74    #[get_mut(pub(crate))]
75    #[set(pub(crate))]
76    pub(crate) scale: Vector2D,
77}
78
79/// A color represented by red, green, blue, and alpha channels in the range 0.0 to 1.0.
80#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
81pub struct Color {
82    /// The red channel value in the range 0.0 to 1.0.
83    #[get(type(copy))]
84    #[get_mut(pub(crate))]
85    #[set(pub(crate))]
86    pub red: f64,
87    /// The green channel value in the range 0.0 to 1.0.
88    #[get(type(copy))]
89    #[get_mut(pub(crate))]
90    #[set(pub(crate))]
91    pub green: f64,
92    /// The blue channel value in the range 0.0 to 1.0.
93    #[get(type(copy))]
94    #[get_mut(pub(crate))]
95    #[set(pub(crate))]
96    pub blue: f64,
97    /// The alpha (opacity) channel value in the range 0.0 to 1.0.
98    #[get(type(copy))]
99    pub alpha: f64,
100}
101
102/// A 3D vector with `x`, `y`, and `z` components represented as `f64`.
103#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
104pub struct Vector3D {
105    /// The horizontal component of the vector.
106    #[get(type(copy))]
107    #[get_mut(pub(crate))]
108    pub x: f64,
109    /// The vertical component of the vector.
110    #[get(type(copy))]
111    #[get_mut(pub(crate))]
112    pub y: f64,
113    /// The depth component of the vector.
114    #[get(type(copy))]
115    #[get_mut(pub(crate))]
116    pub z: f64,
117}
118
119/// A quaternion representing a 3D rotation with `x`, `y`, `z`, and `w` components.
120#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
121pub struct Quaternion {
122    /// The x component of the quaternion.
123    #[get(type(copy))]
124    #[get_mut(pub(crate))]
125    pub x: f64,
126    /// The y component of the quaternion.
127    #[get(type(copy))]
128    #[get_mut(pub(crate))]
129    pub y: f64,
130    /// The z component of the quaternion.
131    #[get(type(copy))]
132    #[get_mut(pub(crate))]
133    pub z: f64,
134    /// The w (scalar) component of the quaternion.
135    #[get(type(copy))]
136    #[get_mut(pub(crate))]
137    #[set(pub(crate))]
138    pub w: f64,
139}
140
141/// A 4x4 matrix stored in column-major order, used for 3D transformations.
142#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
143pub struct Matrix4x4 {
144    /// The 16 elements of the matrix in column-major order.
145    ///
146    /// `get_elements` is public: `example/src/page/game_3d` reads the matrix
147    /// from outside this crate. `get_mut` / `set` stay crate-internal.
148    #[get_mut(pub(crate))]
149    #[get(type(copy))]
150    #[set(pub(crate))]
151    pub elements: [f64; 16],
152}
153
154/// A 3D transform composed of position, rotation (as a quaternion), and scale.
155#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
156pub struct Transform3D {
157    /// The position offset of the transform.
158    #[get(type(copy))]
159    #[get_mut(pub(crate))]
160    pub position: Vector3D,
161    /// The rotation as a quaternion.
162    #[get(pub(crate), type(copy))]
163    #[get_mut(pub(crate))]
164    #[set(pub(crate))]
165    pub(crate) rotation: Quaternion,
166    /// The scale factors for each axis.
167    #[get(pub(crate), type(copy))]
168    #[get_mut(pub(crate))]
169    #[set(pub(crate))]
170    pub(crate) scale: Vector3D,
171}
172
173/// A 3D axis-aligned bounding box defined by its minimum and maximum corners.
174#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
175pub struct AABB3D {
176    /// The minimum corner (smallest x, y, z).
177    #[get(type(copy))]
178    #[get_mut(pub(crate))]
179    #[set(pub(crate))]
180    pub min: Vector3D,
181    /// The maximum corner (largest x, y, z).
182    #[get(type(copy))]
183    #[get_mut(pub(crate))]
184    #[set(pub(crate))]
185    pub max: Vector3D,
186}
187
188/// A 3D sphere defined by its center point and radius.
189#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
190pub struct Sphere {
191    /// The center point of the sphere.
192    #[get(type(copy))]
193    #[get_mut(pub(crate))]
194    #[set(pub(crate))]
195    pub center: Vector3D,
196    /// The radius of the sphere.
197    #[get(type(copy))]
198    #[get_mut(pub(crate))]
199    #[set(pub(crate))]
200    pub radius: f64,
201}
202
203/// A 3D plane defined by a normal vector and a distance from the origin.
204#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
205pub struct Plane {
206    /// The normal vector of the plane (should be normalized).
207    #[get(type(copy), pub(crate))]
208    #[get_mut(pub(crate))]
209    #[set(pub(crate))]
210    pub(crate) normal: Vector3D,
211    /// The signed distance from the origin along the normal.
212    #[get(type(copy), pub(crate))]
213    #[get_mut(pub(crate))]
214    #[set(pub(crate))]
215    pub(crate) distance: f64,
216}
217
218/// A 3D ray defined by an origin point and a direction vector.
219#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
220pub struct Ray3D {
221    /// The origin point of the ray.
222    #[get(type(copy))]
223    #[get_mut(pub(crate))]
224    #[set(pub(crate))]
225    pub origin: Vector3D,
226    /// The direction vector of the ray (should be normalized).
227    #[get(type(copy))]
228    #[get_mut(pub(crate))]
229    pub direction: Vector3D,
230}
231
232/// A 2D ray defined by an origin point and a direction vector, used for
233/// raycasting against 2D shapes (`Rect`, `Circle`).
234#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
235pub struct Ray2D {
236    /// The origin point of the ray.
237    #[get(type(copy))]
238    #[get_mut(pub(crate))]
239    #[set(pub(crate))]
240    pub origin: Vector2D,
241    /// The direction vector of the ray (should be normalized).
242    #[get(type(copy))]
243    #[get_mut(pub(crate))]
244    pub direction: Vector2D,
245}