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(crate) x: f64,
18    /// The vertical component of the vector.
19    #[get(type(copy))]
20    #[get_mut(pub(crate))]
21    pub(crate) 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(crate) x: f64,
31    /// The y coordinate of the top-left corner.
32    #[get(type(copy))]
33    #[get_mut(pub(crate))]
34    pub(crate) y: f64,
35    /// The width of the rectangle.
36    #[get(type(copy))]
37    #[get_mut(pub(crate))]
38    pub(crate) width: f64,
39    /// The height of the rectangle.
40    #[get(type(copy))]
41    #[get_mut(pub(crate))]
42    pub(crate) 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(crate) center: Vector2D,
53    /// The radius of the circle.
54    #[get(type(copy))]
55    #[get_mut(pub(crate))]
56    #[set(pub(crate))]
57    pub(crate) 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    #[set(pub(crate))]
67    pub(crate) position: Vector2D,
68    /// The rotation angle in radians.
69    #[get(pub(crate), type(copy))]
70    #[get_mut(pub(crate))]
71    #[set(pub(crate))]
72    pub(crate) rotation: f64,
73    /// The scale factors for each axis.
74    #[get(pub(crate), type(copy))]
75    #[get_mut(pub(crate))]
76    #[set(pub(crate))]
77    pub(crate) scale: Vector2D,
78}
79
80/// A color represented by red, green, blue, and alpha channels in the range 0.0 to 1.0.
81#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
82pub struct Color {
83    /// The red channel value in the range 0.0 to 1.0.
84    #[get(type(copy))]
85    #[get_mut(pub(crate))]
86    #[set(pub(crate))]
87    pub(crate) red: f64,
88    /// The green channel value in the range 0.0 to 1.0.
89    #[get(type(copy))]
90    #[get_mut(pub(crate))]
91    #[set(pub(crate))]
92    pub(crate) green: f64,
93    /// The blue channel value in the range 0.0 to 1.0.
94    #[get(type(copy))]
95    #[get_mut(pub(crate))]
96    #[set(pub(crate))]
97    pub(crate) blue: f64,
98    /// The alpha (opacity) channel value in the range 0.0 to 1.0.
99    #[get(type(copy))]
100    pub(crate) alpha: f64,
101}
102
103/// A 3D vector with `x`, `y`, and `z` components represented as `f64`.
104#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
105pub struct Vector3D {
106    /// The horizontal component of the vector.
107    #[get(type(copy))]
108    #[get_mut(pub(crate))]
109    pub(crate) x: f64,
110    /// The vertical component of the vector.
111    #[get(type(copy))]
112    #[get_mut(pub(crate))]
113    pub(crate) y: f64,
114    /// The depth component of the vector.
115    #[get(type(copy))]
116    #[get_mut(pub(crate))]
117    #[set(pub(crate))]
118    pub(crate) z: f64,
119}
120
121/// A quaternion representing a 3D rotation with `x`, `y`, `z`, and `w` components.
122#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
123pub struct Quaternion {
124    /// The x component of the quaternion.
125    #[get(type(copy))]
126    #[get_mut(pub(crate))]
127    pub(crate) x: f64,
128    /// The y component of the quaternion.
129    #[get(type(copy))]
130    #[get_mut(pub(crate))]
131    pub(crate) y: f64,
132    /// The z component of the quaternion.
133    #[get(type(copy))]
134    #[get_mut(pub(crate))]
135    #[set(pub(crate))]
136    pub(crate) z: f64,
137    /// The w (scalar) component of the quaternion.
138    #[get(type(copy))]
139    #[get_mut(pub(crate))]
140    #[set(pub(crate))]
141    pub(crate) w: f64,
142}
143
144/// A 4x4 matrix stored in column-major order, used for 3D transformations.
145#[derive(Clone, Copy, Data, Debug, New, PartialEq, PartialOrd)]
146pub struct Matrix4x4 {
147    /// The 16 elements of the matrix in column-major order.
148    #[get(type(copy))]
149    #[get_mut(pub(crate))]
150    #[set(pub(crate))]
151    pub(crate) 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    #[set(pub(crate))]
161    pub(crate) position: Vector3D,
162    /// The rotation as a quaternion.
163    #[get(pub(crate), type(copy))]
164    #[get_mut(pub(crate))]
165    #[set(pub(crate))]
166    pub(crate) rotation: Quaternion,
167    /// The scale factors for each axis.
168    #[get(pub(crate), type(copy))]
169    #[get_mut(pub(crate))]
170    #[set(pub(crate))]
171    pub(crate) scale: Vector3D,
172}
173
174/// A 3D axis-aligned bounding box defined by its minimum and maximum corners.
175#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
176pub struct AABB3D {
177    /// The minimum corner (smallest x, y, z).
178    #[get(type(copy))]
179    #[get_mut(pub(crate))]
180    #[set(pub(crate))]
181    pub(crate) min: Vector3D,
182    /// The maximum corner (largest x, y, z).
183    #[get(type(copy))]
184    #[get_mut(pub(crate))]
185    #[set(pub(crate))]
186    pub(crate) max: Vector3D,
187}
188
189/// A 3D sphere defined by its center point and radius.
190#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
191pub struct Sphere {
192    /// The center point of the sphere.
193    #[get(type(copy))]
194    #[get_mut(pub(crate))]
195    #[set(pub(crate))]
196    pub(crate) center: Vector3D,
197    /// The radius of the sphere.
198    #[get(type(copy))]
199    #[get_mut(pub(crate))]
200    #[set(pub(crate))]
201    pub(crate) radius: f64,
202}
203
204/// A 3D plane defined by a normal vector and a distance from the origin.
205#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
206pub struct Plane {
207    /// The normal vector of the plane (should be normalized).
208    #[get(type(copy))]
209    #[get_mut(pub(crate))]
210    #[set(pub(crate))]
211    pub(crate) normal: Vector3D,
212    /// The signed distance from the origin along the normal.
213    #[get(type(copy))]
214    #[get_mut(pub(crate))]
215    #[set(pub(crate))]
216    pub(crate) distance: f64,
217}
218
219/// A 3D ray defined by an origin point and a direction vector.
220#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
221pub struct Ray3D {
222    /// The origin point of the ray.
223    #[get(type(copy))]
224    #[get_mut(pub(crate))]
225    #[set(pub(crate))]
226    pub(crate) origin: Vector3D,
227    /// The direction vector of the ray (should be normalized).
228    #[get(type(copy))]
229    #[get_mut(pub(crate))]
230    pub(crate) direction: Vector3D,
231}
232
233/// A 2D ray defined by an origin point and a direction vector, used for
234/// raycasting against 2D shapes (`Rect`, `Circle`).
235#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq, PartialOrd)]
236pub struct Ray2D {
237    /// The origin point of the ray.
238    #[get(type(copy))]
239    #[get_mut(pub(crate))]
240    #[set(pub(crate))]
241    pub(crate) origin: Vector2D,
242    /// The direction vector of the ray (should be normalized).
243    #[get(type(copy))]
244    #[get_mut(pub(crate))]
245    pub(crate) direction: Vector2D,
246}