1#![allow(non_camel_case_types, missing_docs)]
11
12pub use data_types::*;
13
14#[rustfmt::skip]
15mod data_types {
16 use crate::TAG_PRIMITIVE;
17
18 #[doc = TAG_PRIMITIVE!()] pub type g_bool = bool;
22
23 #[doc = TAG_PRIMITIVE!()] pub type g_float = f32;
25 #[doc = TAG_PRIMITIVE!()] pub type g_double = f64;
27
28 #[doc = TAG_PRIMITIVE!()] pub type g_int = i32;
30 #[doc = TAG_PRIMITIVE!()] pub type g_uint = u32;
32
33 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
37 pub struct g_bvec2 { pub x: bool, pub y: bool }
38 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
40 pub struct g_bvec3 { pub x: bool, pub y: bool, pub z: bool }
41 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
43 pub struct g_bvec4 { pub x: bool, pub y: bool, pub z: bool, pub w: bool }
44
45 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
47 pub struct g_vec2 { pub x: g_float, pub y: g_float }
48 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
50 pub struct g_vec3 { pub x: g_float, pub y: g_float, pub z: g_float }
51 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
53 pub struct g_vec4 { pub x: g_float, pub y: g_float, pub z: g_float, pub w: g_float }
54
55 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
57 pub struct g_dvec2 { pub x: g_double, pub y: g_double }
58 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
60 pub struct g_dvec3 { pub x: g_double, pub y: g_double, pub z: g_double }
61 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
63 pub struct g_dvec4 { pub x: g_double, pub y: g_double, pub z: g_double, pub w: g_double }
64
65 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
67 pub struct g_ivec2 { pub x: g_int, pub y: g_int }
68 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
70 pub struct g_ivec3 { pub x: g_int, pub y: g_int, pub z: g_int }
71 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
73 pub struct g_ivec4 { pub x: g_int, pub y: g_int, pub z: g_int, pub w: g_int }
74
75 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
77 pub struct g_uvec2 { pub x: g_uint, pub y: g_uint }
78 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
80 pub struct g_uvec3 { pub x: g_uint, pub y: g_uint, pub z: g_uint }
81 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
83 pub struct g_uvec4 { pub x: g_uint, pub y: g_uint, pub z: g_uint, pub w: g_uint }
84
85 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
89 pub struct g_mat2(pub [g_float; 2 * 2]);
90 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
92 pub struct g_mat2x3(pub [g_float; 2 * 3]);
93 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
95 pub struct g_mat2x4(pub [g_float; 2 * 4]);
96
97 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
99 pub struct g_mat3x2(pub [g_float; 3 * 2]);
100 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
102 pub struct g_mat3(pub [g_float; 3 * 3]);
103 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
105 pub struct g_mat3x4(pub [g_float; 3 * 4]);
106
107 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
109 pub struct g_mat4x2(pub [g_float; 4 * 2]);
110 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
112 pub struct g_mat4x3(pub [g_float; 4 * 3]);
113 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
115 pub struct g_mat4(pub [g_float; 4 * 4]);
116
117 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
119 pub struct g_dmat2(pub [g_double; 2 * 2]);
120 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
122 pub struct g_dmat3(pub [g_double; 3 * 3]);
123 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
125 pub struct g_dmat4(pub [g_double; 4 * 4]);
126
127 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
131 pub struct g_vertex2 { pub pos: g_vec2, pub uv: g_vec2 }
132
133 #[repr(C)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
135 pub struct g_vertex3 { pub pos: g_vec3, pub uv: g_vec2 }
136}
137
138macro_rules! impl_matrix_methods {
139 ($($mat:ident, $inner:ty, $cols:expr, $rows:expr);* $(;)?) => {
140 $(impl $mat {
141 pub const fn get(&self, row: usize, col: usize) -> Option<$inner> {
143 if let Some(idx) = self.to_index(row, col) { Some(self.0[idx]) } else { None }
144 }
145 pub const fn to_index(&self, row: usize, col: usize) -> Option<usize> {
147 if row < $rows && col < $cols { Some(col * $rows + row) } else { None }
148 }
149 pub const fn from_index(&self, index: usize) -> Option<(usize, usize)> {
151 if index < self.0.len() { Some((index % $rows, index / $rows)) } else { None }
152 }
153 })*
154 };
155}
156impl_matrix_methods!(
157 g_mat2, g_float, 2, 2; g_mat2x3, g_float, 2, 3; g_mat2x4, g_float, 2, 4;
158 g_mat3x2, g_float, 3, 2; g_mat3, g_float, 3, 3; g_mat3x4, g_float, 3, 4;
159 g_mat4x2, g_float, 4, 2; g_mat4x3, g_float, 4, 3; g_mat4, g_float, 4, 4;
160 g_dmat2, g_double, 2, 2; g_dmat3, g_double, 3, 3; g_dmat4, g_double, 4, 4;
161);
162
163#[cfg(test)]
164mod tests {
165 use super::g_mat3;
166
167 #[test]
168 fn matrix_methods() {
169 let m = g_mat3([
170 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, ]);
174
175 assert_eq!(m.to_index(0, 0), Some(0));
177 assert_eq!(m.to_index(1, 2), Some(7));
178 assert_eq!(m.to_index(2, 2), Some(8));
179 assert_eq!(m.to_index(3, 2), None); assert_eq!(m.get(1, 2), Some(7.0));
183 assert_eq!(m.get(0, 0), Some(0.0));
184 assert_eq!(m.get(2, 1), Some(5.0));
185 assert_eq!(m.get(3, 1), None); assert_eq!(m.from_index(0), Some((0, 0)));
189 assert_eq!(m.from_index(7), Some((1, 2)));
190 assert_eq!(m.from_index(8), Some((2, 2)));
191 assert_eq!(m.from_index(9), None); }
193}