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