Debian Codegraph @

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

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