Debian Codegraph @

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

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