Debian Codegraph @

debian/missing-sources/glam-0.22.0/src/features/impl_mint.rs

raw
1 use mint::IntoMint;
2
3 use crate::{
4 DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4, IVec2, IVec3, IVec4, Mat2, Mat3, Mat3A, Mat4,
5 Quat, UVec2, UVec3, UVec4, Vec2, Vec3, Vec3A, Vec4,
6 };
7
8 macro_rules! impl_vec_types {
9 ($t:ty, $vec2:ty, $vec3:ty, $vec4:ty) => {
10 impl From<mint::Point2<$t>> for $vec2 {
11 fn from(v: mint::Point2<$t>) -> Self {
12 Self::new(v.x, v.y)
13 }
14 }
15
16 impl From<$vec2> for mint::Point2<$t> {
17 fn from(v: $vec2) -> Self {
18 Self { x: v.x, y: v.y }
19 }
20 }
21
22 impl From<mint::Vector2<$t>> for $vec2 {
23 fn from(v: mint::Vector2<$t>) -> Self {
24 Self::new(v.x, v.y)
25 }
26 }
27
28 impl From<$vec2> for mint::Vector2<$t> {
29 fn from(v: $vec2) -> Self {
30 Self { x: v.x, y: v.y }
31 }
32 }
33
34 impl IntoMint for $vec2 {
35 type MintType = mint::Vector2<$t>;
36 }
37
38 impl From<mint::Point3<$t>> for $vec3 {
39 fn from(v: mint::Point3<$t>) -> Self {
40 Self::new(v.x, v.y, v.z)
41 }
42 }
43
44 impl From<$vec3> for mint::Point3<$t> {
45 fn from(v: $vec3) -> Self {
46 Self {
47 x: v.x,
48 y: v.y,
49 z: v.z,
50 }
51 }
52 }
53
54 impl From<mint::Vector3<$t>> for $vec3 {
55 fn from(v: mint::Vector3<$t>) -> Self {
56 Self::new(v.x, v.y, v.z)
57 }
58 }
59
60 impl From<$vec3> for mint::Vector3<$t> {
61 fn from(v: $vec3) -> Self {
62 Self {
63 x: v.x,
64 y: v.y,
65 z: v.z,
66 }
67 }
68 }
69
70 impl IntoMint for $vec3 {
71 type MintType = mint::Vector3<$t>;
72 }
73
74 impl From<mint::Vector4<$t>> for $vec4 {
75 fn from(v: mint::Vector4<$t>) -> Self {
76 Self::new(v.x, v.y, v.z, v.w)
77 }
78 }
79
80 impl From<$vec4> for mint::Vector4<$t> {
81 fn from(v: $vec4) -> Self {
82 Self {
83 x: v.x,
84 y: v.y,
85 z: v.z,
86 w: v.w,
87 }
88 }
89 }
90
91 impl IntoMint for $vec4 {
92 type MintType = mint::Vector4<$t>;
93 }
94 };
95 }
96
97 macro_rules! impl_float_types {
98 ($t:ty, $mat2:ty, $mat3:ty, $mat4:ty, $quat:ty, $vec2:ty, $vec3:ty, $vec4:ty) => {
99 impl_vec_types!($t, $vec2, $vec3, $vec4);
100
101 impl From<mint::Quaternion<$t>> for $quat {
102 fn from(q: mint::Quaternion<$t>) -> Self {
103 Self::from_xyzw(q.v.x, q.v.y, q.v.z, q.s)
104 }
105 }
106
107 impl From<$quat> for mint::Quaternion<$t> {
108 fn from(q: $quat) -> Self {
109 Self {
110 s: q.w,
111 v: mint::Vector3 {
112 x: q.x,
113 y: q.y,
114 z: q.z,
115 },
116 }
117 }
118 }
119
120 impl IntoMint for $quat {
121 type MintType = mint::Quaternion<$t>;
122 }
123
124 impl From<mint::RowMatrix2<$t>> for $mat2 {
125 fn from(m: mint::RowMatrix2<$t>) -> Self {
126 Self::from_cols(m.x.into(), m.y.into()).transpose()
127 }
128 }
129
130 impl From<$mat2> for mint::RowMatrix2<$t> {
131 fn from(m: $mat2) -> Self {
132 let mt = m.transpose();
133 Self {
134 x: mt.x_axis.into(),
135 y: mt.y_axis.into(),
136 }
137 }
138 }
139
140 impl From<mint::ColumnMatrix2<$t>> for $mat2 {
141 fn from(m: mint::ColumnMatrix2<$t>) -> Self {
142 Self::from_cols(m.x.into(), m.y.into())
143 }
144 }
145
146 impl From<$mat2> for mint::ColumnMatrix2<$t> {
147 fn from(m: $mat2) -> Self {
148 Self {
149 x: m.x_axis.into(),
150 y: m.y_axis.into(),
151 }
152 }
153 }
154
155 impl IntoMint for $mat2 {
156 type MintType = mint::ColumnMatrix2<$t>;
157 }
158
159 impl From<mint::RowMatrix3<$t>> for $mat3 {
160 fn from(m: mint::RowMatrix3<$t>) -> Self {
161 Self::from_cols(m.x.into(), m.y.into(), m.z.into()).transpose()
162 }
163 }
164
165 impl From<$mat3> for mint::RowMatrix3<$t> {
166 fn from(m: $mat3) -> Self {
167 let mt = m.transpose();
168 Self {
169 x: mt.x_axis.into(),
170 y: mt.y_axis.into(),
171 z: mt.z_axis.into(),
172 }
173 }
174 }
175
176 impl From<mint::ColumnMatrix3<$t>> for $mat3 {
177 fn from(m: mint::ColumnMatrix3<$t>) -> Self {
178 Self::from_cols(m.x.into(), m.y.into(), m.z.into())
179 }
180 }
181
182 impl From<$mat3> for mint::ColumnMatrix3<$t> {
183 fn from(m: $mat3) -> Self {
184 Self {
185 x: m.x_axis.into(),
186 y: m.y_axis.into(),
187 z: m.z_axis.into(),
188 }
189 }
190 }
191
192 impl IntoMint for $mat3 {
193 type MintType = mint::ColumnMatrix3<$t>;
194 }
195
196 impl From<mint::RowMatrix4<$t>> for $mat4 {
197 fn from(m: mint::RowMatrix4<$t>) -> Self {
198 Self::from_cols(m.x.into(), m.y.into(), m.z.into(), m.w.into()).transpose()
199 }
200 }
201
202 impl From<$mat4> for mint::RowMatrix4<$t> {
203 fn from(m: $mat4) -> Self {
204 let mt = m.transpose();
205 Self {
206 x: mt.x_axis.into(),
207 y: mt.y_axis.into(),
208 z: mt.z_axis.into(),
209 w: mt.w_axis.into(),
210 }
211 }
212 }
213
214 impl From<mint::ColumnMatrix4<$t>> for $mat4 {
215 fn from(m: mint::ColumnMatrix4<$t>) -> Self {
216 Self::from_cols(m.x.into(), m.y.into(), m.z.into(), m.w.into())
217 }
218 }
219
220 impl From<$mat4> for mint::ColumnMatrix4<$t> {
221 fn from(m: $mat4) -> Self {
222 Self {
223 x: m.x_axis.into(),
224 y: m.y_axis.into(),
225 z: m.z_axis.into(),
226 w: m.w_axis.into(),
227 }
228 }
229 }
230
231 impl IntoMint for $mat4 {
232 type MintType = mint::ColumnMatrix4<$t>;
233 }
234 };
235 }
236
237 impl From<mint::Point3<f32>> for Vec3A {
238 fn from(v: mint::Point3<f32>) -> Self {
239 Self::new(v.x, v.y, v.z)
240 }
241 }
242
243 impl From<Vec3A> for mint::Point3<f32> {
244 fn from(v: Vec3A) -> Self {
245 Self {
246 x: v.x,
247 y: v.y,
248 z: v.z,
249 }
250 }
251 }
252
253 impl From<mint::Vector3<f32>> for Vec3A {
254 fn from(v: mint::Vector3<f32>) -> Self {
255 Self::new(v.x, v.y, v.z)
256 }
257 }
258
259 impl From<Vec3A> for mint::Vector3<f32> {
260 fn from(v: Vec3A) -> Self {
261 Self {
262 x: v.x,
263 y: v.y,
264 z: v.z,
265 }
266 }
267 }
268
269 impl IntoMint for Vec3A {
270 type MintType = mint::Vector3<f32>;
271 }
272
273 impl From<mint::RowMatrix3<f32>> for Mat3A {
274 fn from(m: mint::RowMatrix3<f32>) -> Self {
275 Self::from_cols(m.x.into(), m.y.into(), m.z.into()).transpose()
276 }
277 }
278
279 impl From<Mat3A> for mint::RowMatrix3<f32> {
280 fn from(m: Mat3A) -> Self {
281 let mt = m.transpose();
282 Self {
283 x: mt.x_axis.into(),
284 y: mt.y_axis.into(),
285 z: mt.z_axis.into(),
286 }
287 }
288 }
289
290 impl From<mint::ColumnMatrix3<f32>> for Mat3A {
291 fn from(m: mint::ColumnMatrix3<f32>) -> Self {
292 Self::from_cols(m.x.into(), m.y.into(), m.z.into())
293 }
294 }
295
296 impl From<Mat3A> for mint::ColumnMatrix3<f32> {
297 fn from(m: Mat3A) -> Self {
298 Self {
299 x: m.x_axis.into(),
300 y: m.y_axis.into(),
301 z: m.z_axis.into(),
302 }
303 }
304 }
305
306 impl IntoMint for Mat3A {
307 type MintType = mint::ColumnMatrix3<f32>;
308 }
309
310 impl_float_types!(f32, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4);
311 impl_float_types!(f64, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4);
312 impl_vec_types!(i32, IVec2, IVec3, IVec4);
313 impl_vec_types!(u32, UVec2, UVec3, UVec4);
314
315 #[cfg(test)]
316 mod test {
317 macro_rules! impl_vec_tests {
318 ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident) => {
319 use crate::{$vec2, $vec3, $vec4};
320 use mint;
321
322 #[test]
323 fn test_point2() {
324 let m = mint::Point2 {
325 x: 1 as $t,
326 y: 2 as $t,
327 };
328 let g = $vec2::from(m);
329 assert_eq!(g, $vec2::new(1 as $t, 2 as $t));
330 assert_eq!(m, g.into());
331 }
332
333 #[test]
334 fn test_point3() {
335 let m = mint::Point3 {
336 x: 1 as $t,
337 y: 2 as $t,
338 z: 3 as $t,
339 };
340 let g = $vec3::from(m);
341 assert_eq!(g, $vec3::new(1 as $t, 2 as $t, 3 as $t));
342 assert_eq!(m, g.into());
343 }
344
345 #[test]
346 fn test_vector2() {
347 let m = mint::Vector2 {
348 x: 1 as $t,
349 y: 2 as $t,
350 };
351 let g = $vec2::from(m);
352 assert_eq!(g, $vec2::new(1 as $t, 2 as $t));
353 assert_eq!(m, g.into());
354 }
355
356 #[test]
357 fn test_vector3() {
358 let m = mint::Vector3 {
359 x: 1 as $t,
360 y: 2 as $t,
361 z: 3 as $t,
362 };
363 let g = $vec3::from(m);
364 assert_eq!(g, $vec3::new(1 as $t, 2 as $t, 3 as $t));
365 assert_eq!(m, g.into());
366 }
367
368 #[test]
369 fn test_vector4() {
370 let m = mint::Vector4 {
371 x: 1 as $t,
372 y: 2 as $t,
373 z: 3 as $t,
374 w: 4 as $t,
375 };
376 let g = $vec4::from(m);
377 assert_eq!(g, $vec4::new(1 as $t, 2 as $t, 3 as $t, 4 as $t));
378 assert_eq!(m, g.into());
379 }
380 };
381 }
382
383 macro_rules! impl_float_tests {
384 ($t:ty, $mat2:ident, $mat3:ident, $mat4:ident, $quat:ident, $vec2:ident, $vec3:ident, $vec4:ident) => {
385 impl_vec_tests!($t, $vec2, $vec3, $vec4);
386
387 use crate::{$mat2, $mat3, $mat4, $quat};
388
389 #[test]
390 fn test_quaternion() {
391 let m = mint::Quaternion {
392 v: mint::Vector3 {
393 x: 1.0,
394 y: 2.0,
395 z: 3.0,
396 },
397 s: 4.0,
398 };
399 let g = $quat::from(m);
400 assert_eq!(g, $quat::from_xyzw(1.0, 2.0, 3.0, 4.0));
401 assert_eq!(m, g.into());
402 }
403
404 #[test]
405 fn test_matrix2() {
406 let g = $mat2::from_cols_array_2d(&[[1.0, 2.0], [3.0, 4.0]]);
407 let m = mint::ColumnMatrix2::from(g);
408 assert_eq!(g, $mat2::from(m));
409 let mt = mint::RowMatrix2::from(g);
410 assert_eq!(mt, mint::RowMatrix2::from([[1.0, 3.0], [2.0, 4.0]]));
411 assert_eq!(g, $mat2::from(mt));
412 }
413
414 #[test]
415 fn test_matrix3() {
416 let g =
417 $mat3::from_cols_array_2d(&[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]);
418 let m = mint::ColumnMatrix3::from(g);
419 assert_eq!(g, $mat3::from(m));
420 let mt = mint::RowMatrix3::from(g);
421 assert_eq!(
422 mt,
423 mint::RowMatrix3::from([[1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]])
424 );
425 assert_eq!(g, $mat3::from(mt));
426 }
427
428 #[test]
429 fn test_matrix4() {
430 let g = $mat4::from_cols_array_2d(&[
431 [1.0, 2.0, 3.0, 4.0],
432 [5.0, 6.0, 7.0, 8.0],
433 [9.0, 10.0, 11.0, 12.0],
434 [13.0, 14.0, 15.0, 16.0],
435 ]);
436 let m = mint::ColumnMatrix4::from(g);
437 assert_eq!(g, $mat4::from(m));
438 let mt = mint::RowMatrix4::from(g);
439 assert_eq!(
440 mt,
441 mint::RowMatrix4::from([
442 [1.0, 5.0, 9.0, 13.0],
443 [2.0, 6.0, 10.0, 14.0],
444 [3.0, 7.0, 11.0, 15.0],
445 [4.0, 8.0, 12.0, 16.0]
446 ])
447 );
448 assert_eq!(g, $mat4::from(mt));
449 }
450 };
451 }
452
453 mod f32 {
454 impl_float_tests!(f32, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4);
455
456 #[test]
457 fn test_point3a() {
458 use crate::Vec3A;
459 let m = mint::Point3 {
460 x: 1.0,
461 y: 2.0,
462 z: 3.0,
463 };
464 let g = Vec3A::from(m);
465 assert_eq!(g, Vec3A::new(1.0, 2.0, 3.0));
466 assert_eq!(m, g.into());
467 }
468
469 #[test]
470 fn test_vector3a() {
471 use crate::Vec3A;
472 let m = mint::Vector3 {
473 x: 1.0,
474 y: 2.0,
475 z: 3.0,
476 };
477 let g = Vec3A::from(m);
478 assert_eq!(g, Vec3A::new(1.0, 2.0, 3.0));
479 assert_eq!(m, g.into());
480 }
481
482 #[test]
483 fn test_mat3a_col_major() {
484 use crate::Mat3A;
485 let m = mint::ColumnMatrix3 {
486 x: [0.0, 1.0, 2.0].into(),
487 y: [3.0, 4.0, 5.0].into(),
488 z: [6.0, 7.0, 8.0].into(),
489 };
490 let expected = Mat3A::from_cols(
491 [0.0, 1.0, 2.0].into(),
492 [3.0, 4.0, 5.0].into(),
493 [6.0, 7.0, 8.0].into(),
494 );
495 assert_eq!(expected, m.into());
496 assert_eq!(m, expected.into());
497 }
498
499 #[test]
500 fn test_mat3a_row_major() {
501 use crate::Mat3A;
502 let m = mint::RowMatrix3 {
503 x: [0.0, 1.0, 2.0].into(),
504 y: [3.0, 4.0, 5.0].into(),
505 z: [6.0, 7.0, 8.0].into(),
506 };
507 let expected = Mat3A::from_cols(
508 [0.0, 3.0, 6.0].into(),
509 [1.0, 4.0, 7.0].into(),
510 [2.0, 5.0, 8.0].into(),
511 );
512 assert_eq!(expected, m.into());
513 assert_eq!(m, expected.into());
514 }
515 }
516
517 mod f64 {
518 impl_float_tests!(f64, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4);
519 }
520
521 mod i32 {
522 impl_vec_tests!(i32, IVec2, IVec3, IVec4);
523 }
524
525 mod u32 {
526 impl_vec_tests!(u32, UVec2, UVec3, UVec4);
527 }
528 }