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, UVec2, UVec3, UVec4, Vec2, 7 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 { from(v: mint::Point3<f32>) -> Self240 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> { from(v: Vec3A) -> Self246 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 { from(v: mint::Vector3<f32>) -> Self256 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> { from(v: Vec3A) -> Self262 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 { from(m: mint::RowMatrix3<f32>) -> Self276 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> { from(m: Mat3A) -> Self282 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 { from(m: mint::ColumnMatrix3<f32>) -> Self293 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> { from(m: Mat3A) -> Self299 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 323 #[cfg(test)] 324 mod test { 325 macro_rules! impl_vec_tests { 326 ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident) => { 327 use crate::{$vec2, $vec3, $vec4}; 328 use mint; 329 330 #[test] 331 fn test_point2() { 332 let m = mint::Point2 { 333 x: 1 as $t, 334 y: 2 as $t, 335 }; 336 let g = $vec2::from(m); 337 assert_eq!(g, $vec2::new(1 as $t, 2 as $t)); 338 assert_eq!(m, g.into()); 339 } 340 341 #[test] 342 fn test_point3() { 343 let m = mint::Point3 { 344 x: 1 as $t, 345 y: 2 as $t, 346 z: 3 as $t, 347 }; 348 let g = $vec3::from(m); 349 assert_eq!(g, $vec3::new(1 as $t, 2 as $t, 3 as $t)); 350 assert_eq!(m, g.into()); 351 } 352 353 #[test] 354 fn test_vector2() { 355 let m = mint::Vector2 { 356 x: 1 as $t, 357 y: 2 as $t, 358 }; 359 let g = $vec2::from(m); 360 assert_eq!(g, $vec2::new(1 as $t, 2 as $t)); 361 assert_eq!(m, g.into()); 362 } 363 364 #[test] 365 fn test_vector3() { 366 let m = mint::Vector3 { 367 x: 1 as $t, 368 y: 2 as $t, 369 z: 3 as $t, 370 }; 371 let g = $vec3::from(m); 372 assert_eq!(g, $vec3::new(1 as $t, 2 as $t, 3 as $t)); 373 assert_eq!(m, g.into()); 374 } 375 376 #[test] 377 fn test_vector4() { 378 let m = mint::Vector4 { 379 x: 1 as $t, 380 y: 2 as $t, 381 z: 3 as $t, 382 w: 4 as $t, 383 }; 384 let g = $vec4::from(m); 385 assert_eq!(g, $vec4::new(1 as $t, 2 as $t, 3 as $t, 4 as $t)); 386 assert_eq!(m, g.into()); 387 } 388 }; 389 } 390 391 macro_rules! impl_float_tests { 392 ($t:ty, $mat2:ident, $mat3:ident, $mat4:ident, $quat:ident, $vec2:ident, $vec3:ident, $vec4:ident) => { 393 impl_vec_tests!($t, $vec2, $vec3, $vec4); 394 395 use crate::{$mat2, $mat3, $mat4, $quat}; 396 397 #[test] 398 fn test_quaternion() { 399 let m = mint::Quaternion { 400 v: mint::Vector3 { 401 x: 1.0, 402 y: 2.0, 403 z: 3.0, 404 }, 405 s: 4.0, 406 }; 407 let g = $quat::from(m); 408 assert_eq!(g, $quat::from_xyzw(1.0, 2.0, 3.0, 4.0)); 409 assert_eq!(m, g.into()); 410 } 411 412 #[test] 413 fn test_matrix2() { 414 let g = $mat2::from_cols_array_2d(&[[1.0, 2.0], [3.0, 4.0]]); 415 let m = mint::ColumnMatrix2::from(g); 416 assert_eq!(g, $mat2::from(m)); 417 let mt = mint::RowMatrix2::from(g); 418 assert_eq!(mt, mint::RowMatrix2::from([[1.0, 3.0], [2.0, 4.0]])); 419 assert_eq!(g, $mat2::from(mt)); 420 } 421 422 #[test] 423 fn test_matrix3() { 424 let g = 425 $mat3::from_cols_array_2d(&[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]); 426 let m = mint::ColumnMatrix3::from(g); 427 assert_eq!(g, $mat3::from(m)); 428 let mt = mint::RowMatrix3::from(g); 429 assert_eq!( 430 mt, 431 mint::RowMatrix3::from([[1.0, 4.0, 7.0], [2.0, 5.0, 8.0], [3.0, 6.0, 9.0]]) 432 ); 433 assert_eq!(g, $mat3::from(mt)); 434 } 435 436 #[test] 437 fn test_matrix4() { 438 let g = $mat4::from_cols_array_2d(&[ 439 [1.0, 2.0, 3.0, 4.0], 440 [5.0, 6.0, 7.0, 8.0], 441 [9.0, 10.0, 11.0, 12.0], 442 [13.0, 14.0, 15.0, 16.0], 443 ]); 444 let m = mint::ColumnMatrix4::from(g); 445 assert_eq!(g, $mat4::from(m)); 446 let mt = mint::RowMatrix4::from(g); 447 assert_eq!( 448 mt, 449 mint::RowMatrix4::from([ 450 [1.0, 5.0, 9.0, 13.0], 451 [2.0, 6.0, 10.0, 14.0], 452 [3.0, 7.0, 11.0, 15.0], 453 [4.0, 8.0, 12.0, 16.0] 454 ]) 455 ); 456 assert_eq!(g, $mat4::from(mt)); 457 } 458 }; 459 } 460 461 mod f32 { 462 impl_float_tests!(f32, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4); 463 464 #[test] test_point3a()465 fn test_point3a() { 466 use crate::Vec3A; 467 let m = mint::Point3 { 468 x: 1.0, 469 y: 2.0, 470 z: 3.0, 471 }; 472 let g = Vec3A::from(m); 473 assert_eq!(g, Vec3A::new(1.0, 2.0, 3.0)); 474 assert_eq!(m, g.into()); 475 } 476 477 #[test] test_vector3a()478 fn test_vector3a() { 479 use crate::Vec3A; 480 let m = mint::Vector3 { 481 x: 1.0, 482 y: 2.0, 483 z: 3.0, 484 }; 485 let g = Vec3A::from(m); 486 assert_eq!(g, Vec3A::new(1.0, 2.0, 3.0)); 487 assert_eq!(m, g.into()); 488 } 489 490 #[test] test_mat3a_col_major()491 fn test_mat3a_col_major() { 492 use crate::Mat3A; 493 let m = mint::ColumnMatrix3 { 494 x: [0.0, 1.0, 2.0].into(), 495 y: [3.0, 4.0, 5.0].into(), 496 z: [6.0, 7.0, 8.0].into(), 497 }; 498 let expected = Mat3A::from_cols( 499 [0.0, 1.0, 2.0].into(), 500 [3.0, 4.0, 5.0].into(), 501 [6.0, 7.0, 8.0].into(), 502 ); 503 assert_eq!(expected, m.into()); 504 assert_eq!(m, expected.into()); 505 } 506 507 #[test] test_mat3a_row_major()508 fn test_mat3a_row_major() { 509 use crate::Mat3A; 510 let m = mint::RowMatrix3 { 511 x: [0.0, 1.0, 2.0].into(), 512 y: [3.0, 4.0, 5.0].into(), 513 z: [6.0, 7.0, 8.0].into(), 514 }; 515 let expected = Mat3A::from_cols( 516 [0.0, 3.0, 6.0].into(), 517 [1.0, 4.0, 7.0].into(), 518 [2.0, 5.0, 8.0].into(), 519 ); 520 assert_eq!(expected, m.into()); 521 assert_eq!(m, expected.into()); 522 } 523 } 524 525 mod f64 { 526 impl_float_tests!(f64, DMat2, DMat3, DMat4, DQuat, DVec2, DVec3, DVec4); 527 } 528 529 mod i32 { 530 impl_vec_tests!(i32, IVec2, IVec3, IVec4); 531 } 532 533 mod u32 { 534 impl_vec_tests!(u32, UVec2, UVec3, UVec4); 535 } 536 } 537