Skip to main content

grafix_toolbox/kit/policies/
math.rs

1macro_rules! impl_func_gen_cast {
2	($($t: ident),+) => {
3		$(#[inline(always)]
4		pub fn $t<T, A>(v: A) -> $t<T>
5		where
6			$t<T>: Cast<A>,
7		{
8			$t::<T>::to(v)
9		})+
10	}
11}
12macro_rules! impl_func_cast {
13	($($t: ident),+) => {
14		$(#[inline(always)]
15		pub fn $t<A>(v: A) -> $t
16		where
17			$t: Cast<A>,
18		{
19			$t::to(v)
20		})+
21	}
22}
23macro_rules! impl_vec {
24	($n2: ident, $n3: ident, $n4: ident, $t: ty) => {
25		pub type $n2 = vec2<$t>;
26		pub type $n3 = vec3<$t>;
27		pub type $n4 = vec4<$t>;
28		impl_func_cast!($n2, $n3, $n4);
29	};
30}
31
32pub mod pre {
33	pub fn intersects<T: PartialOrd>((p1, p2): vec2<vec2<T>>, (rp1, rp2): vec2<vec2<T>>) -> bool {
34		!(p2.0 <= rp1.0 || p2.1 <= rp1.1 || p1.0 >= rp2.0 || p1.1 >= rp2.1)
35	}
36	pub fn contains<T: PartialOrd>((p1, p2): vec2<vec2<T>>, (rp1, rp2): vec2<vec2<T>>) -> bool {
37		!(rp1.0 < p1.0 || rp1.1 < p1.1 || rp2.0 > p2.0 || rp2.1 > p2.1)
38	}
39
40	pub use super::cast::{Cast, f16};
41	impl_func_cast!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, f16, f32, f64, usize, isize);
42	impl_func_gen_cast!(vec2, vec3, vec4, mat2, mat3, mat4);
43
44	impl_vec!(hVec2, hVec3, hVec4, f16);
45	impl_vec!(Vec2, Vec3, Vec4, f32);
46	impl_vec!(dVec2, dVec3, dVec4, f64);
47	impl_vec!(isVec2, isVec3, isVec4, i16);
48	impl_vec!(usVec2, usVec3, usVec4, u16);
49	impl_vec!(iVec2, iVec3, iVec4, i32);
50	impl_vec!(uVec2, uVec3, uVec4, u32);
51	impl_vec!(ilVec2, ilVec3, ilVec4, isize);
52	impl_vec!(ulVec2, ulVec3, ulVec4, usize);
53
54	pub type Mat2 = mat2<f32>;
55	pub type Mat3 = mat3<f32>;
56	pub type Mat4 = mat4<f32>;
57	pub type Mat2x3 = mat2x3<f32>;
58	pub type Mat3x2 = mat3x2<f32>;
59	pub type Mat2x4 = mat2x4<f32>;
60	pub type Mat4x2 = mat4x2<f32>;
61	pub type Mat3x4 = mat3x4<f32>;
62	pub type Mat4x3 = mat4x3<f32>;
63	impl_func_cast!(Mat2, Mat3, Mat4, Mat2x3, Mat3x2, Mat2x4, Mat4x2, Mat3x4, Mat4x3);
64
65	pub type vec2<T> = (T, T);
66	pub type vec3<T> = (T, T, T);
67	pub type vec4<T> = (T, T, T, T);
68
69	pub type mat2<T> = (vec2<T>, vec2<T>);
70	pub type mat3<T> = (vec3<T>, vec3<T>, vec3<T>);
71	pub type mat4<T> = (vec4<T>, vec4<T>, vec4<T>, vec4<T>);
72	pub type mat2x3<T> = (vec3<T>, vec3<T>);
73	pub type mat3x2<T> = (vec2<T>, vec2<T>, vec2<T>);
74	pub type mat2x4<T> = (vec4<T>, vec4<T>);
75	pub type mat4x2<T> = (vec2<T>, vec2<T>, vec2<T>, vec2<T>);
76	pub type mat3x4<T> = (vec4<T>, vec4<T>, vec4<T>);
77	pub type mat4x3<T> = (vec3<T>, vec3<T>, vec3<T>, vec3<T>);
78}
79
80pub mod ext {
81	pub use super::{cast::matrix::*, math_ext::*, pre::*, tuple::*};
82}
83
84pub mod la;
85
86mod cast;
87mod math_ext;
88mod tuple;