grafix_toolbox/kit/policies/math/cast/
nalgebra.rs1use crate::lib::*;
2
3macro_rules! arr_cast {
4 ($from: ty, $to: ty, $dim: literal) => {
5 impl Cast<$from> for $to {
6 #[inline(always)]
7 fn to(v: $from) -> Self {
8 <[_; $dim]>::from(v).into()
9 }
10 }
11 impl Cast<$to> for $from {
12 #[inline(always)]
13 fn to(v: $to) -> Self {
14 <[_; $dim]>::from(v).into()
15 }
16 }
17 };
18}
19macro_rules! impl_vec {
20 ($($t: ty),+) => {
21 $(
22 arr_cast!(na::Vector2<$t>, vec2<$t>, 2);
23 arr_cast!(na::Vector3<$t>, vec3<$t>, 3);
24 arr_cast!(na::Vector4<$t>, vec4<$t>, 4);
25 arr_cast!(na::Point2<$t>, vec2<$t>, 2);
26 arr_cast!(na::Point3<$t>, vec3<$t>, 3);
27 arr_cast!(na::Point4<$t>, vec4<$t>, 4);
28 )+
29 };
30}
31impl_vec!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, f16, f32, f64);
32
33macro_rules! mat_cast {
34 ($from: ty, $to: ty, $l: literal, $c: literal, $r: literal) => {
35 impl Cast<$to> for $from {
36 #[inline(always)]
37 fn to(v: $to) -> Self {
38 let m: [_; $c] = v.into();
39 Self::from_column_slice(&m.map(Into::<[_; $r]>::into).concat())
40 }
41 }
42 impl Cast<$from> for $to {
43 #[inline(always)]
44 fn to(v: $from) -> Self {
45 let m: [_; $l] = v.as_slice().try_into().valid();
46 let m: [[_; $r]; $c] = unsafe { mem::transmute(m) };
47 m.map(Into::into).into()
48 }
49 }
50 };
51}
52macro_rules! impl_mat {
53 ($($t: ty),+) => {
54 $(
55 mat_cast!(na::Matrix2<$t>, mat2<$t>, 4, 2, 2);
56 mat_cast!(na::Matrix3<$t>, mat3<$t>, 9, 3, 3);
57 mat_cast!(na::Matrix4<$t>, mat4<$t>, 16, 4, 4);
58 mat_cast!(na::Matrix2x3<$t>, mat3x2<$t>, 6, 3, 2);
59 mat_cast!(na::Matrix2x4<$t>, mat4x2<$t>, 8, 4, 2);
60 mat_cast!(na::Matrix3x2<$t>, mat2x3<$t>, 6, 2, 3);
61 mat_cast!(na::Matrix3x4<$t>, mat4x3<$t>, 12, 4, 3);
62 mat_cast!(na::Matrix4x2<$t>, mat2x4<$t>, 8, 2, 4);
63 mat_cast!(na::Matrix4x3<$t>, mat3x4<$t>, 12, 3, 4);
64 )+
65 };
66}
67impl_mat!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, f16, f32, f64);
68
69use nalgebra as na;