| 1 |
#[macro_use] |
| 2 |
mod support; |
| 3 |
|
| 4 |
macro_rules! impl_mat4_tests { |
| 5 |
($t:ident, $const_new:ident, $newmat4:ident, $newvec4:ident, $newvec3:ident, $mat4:ident, $quat:ident, $vec4:ident, $vec3:ident) => { |
| 6 |
use core::$t::INFINITY; |
| 7 |
use core::$t::NAN; |
| 8 |
use core::$t::NEG_INFINITY; |
| 9 |
|
| 10 |
const IDENTITY: [[$t; 4]; 4] = [ |
| 11 |
[1.0, 0.0, 0.0, 0.0], |
| 12 |
[0.0, 1.0, 0.0, 0.0], |
| 13 |
[0.0, 0.0, 1.0, 0.0], |
| 14 |
[0.0, 0.0, 0.0, 1.0], |
| 15 |
]; |
| 16 |
const MATRIX: [[$t; 4]; 4] = [ |
| 17 |
[1.0, 2.0, 3.0, 4.0], |
| 18 |
[5.0, 6.0, 7.0, 8.0], |
| 19 |
[9.0, 10.0, 11.0, 12.0], |
| 20 |
[13.0, 14.0, 15.0, 16.0], |
| 21 |
]; |
| 22 |
|
| 23 |
#[test] |
| 24 |
fn test_const() { |
| 25 |
const M0: $mat4 = $const_new!([0.0; 16]); |
| 26 |
const M1: $mat4 = $const_new!([ |
| 27 |
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, |
| 28 |
16.0 |
| 29 |
]); |
| 30 |
const M2: $mat4 = $const_new!( |
| 31 |
[1.0, 2.0, 3.0, 4.0], |
| 32 |
[5.0, 6.0, 7.0, 8.0], |
| 33 |
[9.0, 10.0, 11.0, 12.0], |
| 34 |
[13.0, 14.0, 15.0, 16.0] |
| 35 |
); |
| 36 |
assert_eq!($mat4::ZERO, M0); |
| 37 |
assert_eq!( |
| 38 |
$mat4::from_cols_array_2d(&[ |
| 39 |
[1.0, 2.0, 3.0, 4.0], |
| 40 |
[5.0, 6.0, 7.0, 8.0], |
| 41 |
[9.0, 10.0, 11.0, 12.0], |
| 42 |
[13.0, 14.0, 15.0, 16.0] |
| 43 |
]), |
| 44 |
M1 |
| 45 |
); |
| 46 |
assert_eq!( |
| 47 |
$mat4::from_cols_array_2d(&[ |
| 48 |
[1.0, 2.0, 3.0, 4.0], |
| 49 |
[5.0, 6.0, 7.0, 8.0], |
| 50 |
[9.0, 10.0, 11.0, 12.0], |
| 51 |
[13.0, 14.0, 15.0, 16.0] |
| 52 |
]), |
| 53 |
M2 |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn test_mat4_identity() { |
| 59 |
assert_eq!( |
| 60 |
$mat4::IDENTITY, |
| 61 |
$mat4::from_cols_array(&[ |
| 62 |
1., 0., 0., 0., // |
| 63 |
0., 1., 0., 0., // |
| 64 |
0., 0., 1., 0., // |
| 65 |
0., 0., 0., 1., // |
| 66 |
]) |
| 67 |
); |
| 68 |
let identity = $mat4::IDENTITY; |
| 69 |
assert_eq!(IDENTITY, identity.to_cols_array_2d()); |
| 70 |
assert_eq!($mat4::from_cols_array_2d(&IDENTITY), identity); |
| 71 |
assert_eq!(identity, identity * identity); |
| 72 |
assert_eq!(identity, $mat4::default()); |
| 73 |
} |
| 74 |
|
| 75 |
#[test] |
| 76 |
fn test_mat4_zero() { |
| 77 |
assert_eq!( |
| 78 |
$mat4::ZERO, |
| 79 |
$mat4::from_cols_array(&[ |
| 80 |
0., 0., 0., 0., // |
| 81 |
0., 0., 0., 0., // |
| 82 |
0., 0., 0., 0., // |
| 83 |
0., 0., 0., 0., // |
| 84 |
]) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn test_mat4_accessors() { |
| 90 |
let mut m = $mat4::ZERO; |
| 91 |
m.x_axis = $vec4::new(1.0, 2.0, 3.0, 4.0); |
| 92 |
m.y_axis = $vec4::new(5.0, 6.0, 7.0, 8.0); |
| 93 |
m.z_axis = $vec4::new(9.0, 10.0, 11.0, 12.0); |
| 94 |
m.w_axis = $vec4::new(13.0, 14.0, 15.0, 16.0); |
| 95 |
assert_eq!($mat4::from_cols_array_2d(&MATRIX), m); |
| 96 |
assert_eq!($vec4::new(1.0, 2.0, 3.0, 4.0), m.x_axis); |
| 97 |
assert_eq!($vec4::new(5.0, 6.0, 7.0, 8.0), m.y_axis); |
| 98 |
assert_eq!($vec4::new(9.0, 10.0, 11.0, 12.0), m.z_axis); |
| 99 |
assert_eq!($vec4::new(13.0, 14.0, 15.0, 16.0), m.w_axis); |
| 100 |
|
| 101 |
assert_eq!($vec4::new(1.0, 2.0, 3.0, 4.0), m.col(0)); |
| 102 |
assert_eq!($vec4::new(5.0, 6.0, 7.0, 8.0), m.col(1)); |
| 103 |
assert_eq!($vec4::new(9.0, 10.0, 11.0, 12.0), m.col(2)); |
| 104 |
assert_eq!($vec4::new(13.0, 14.0, 15.0, 16.0), m.col(3)); |
| 105 |
|
| 106 |
assert_eq!($newvec4(1.0, 5.0, 9.0, 13.0), m.row(0)); |
| 107 |
assert_eq!($newvec4(2.0, 6.0, 10.0, 14.0), m.row(1)); |
| 108 |
assert_eq!($newvec4(3.0, 7.0, 11.0, 15.0), m.row(2)); |
| 109 |
assert_eq!($newvec4(4.0, 8.0, 12.0, 16.0), m.row(3)); |
| 110 |
} |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn test_mat4_from_axes() { |
| 114 |
let a = $mat4::from_cols_array_2d(&[ |
| 115 |
[1.0, 2.0, 3.0, 4.0], |
| 116 |
[5.0, 6.0, 7.0, 8.0], |
| 117 |
[9.0, 10.0, 11.0, 12.0], |
| 118 |
[13.0, 14.0, 15.0, 16.0], |
| 119 |
]); |
| 120 |
assert_eq!(MATRIX, a.to_cols_array_2d()); |
| 121 |
let b = $mat4::from_cols( |
| 122 |
$newvec4(1.0, 2.0, 3.0, 4.0), |
| 123 |
$newvec4(5.0, 6.0, 7.0, 8.0), |
| 124 |
$newvec4(9.0, 10.0, 11.0, 12.0), |
| 125 |
$newvec4(13.0, 14.0, 15.0, 16.0), |
| 126 |
); |
| 127 |
assert_eq!(a, b); |
| 128 |
let c = $newmat4( |
| 129 |
$newvec4(1.0, 2.0, 3.0, 4.0), |
| 130 |
$newvec4(5.0, 6.0, 7.0, 8.0), |
| 131 |
$newvec4(9.0, 10.0, 11.0, 12.0), |
| 132 |
$newvec4(13.0, 14.0, 15.0, 16.0), |
| 133 |
); |
| 134 |
assert_eq!(a, c); |
| 135 |
let d = b.to_cols_array(); |
| 136 |
let f = $mat4::from_cols_array(&d); |
| 137 |
assert_eq!(b, f); |
| 138 |
} |
| 139 |
|
| 140 |
#[test] |
| 141 |
fn test_mat4_translation() { |
| 142 |
let translate = $mat4::from_translation($newvec3(1.0, 2.0, 3.0)); |
| 143 |
assert_eq!( |
| 144 |
$mat4::from_cols( |
| 145 |
$newvec4(1.0, 0.0, 0.0, 0.0), |
| 146 |
$newvec4(0.0, 1.0, 0.0, 0.0), |
| 147 |
$newvec4(0.0, 0.0, 1.0, 0.0), |
| 148 |
$newvec4(1.0, 2.0, 3.0, 1.0) |
| 149 |
), |
| 150 |
translate |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
#[test] |
| 155 |
fn test_from_rotation() { |
| 156 |
let rot_x1 = $mat4::from_rotation_x(deg(180.0)); |
| 157 |
let rot_x2 = $mat4::from_axis_angle($vec3::X, deg(180.0)); |
| 158 |
assert_approx_eq!(rot_x1, rot_x2); |
| 159 |
let rot_y1 = $mat4::from_rotation_y(deg(180.0)); |
| 160 |
let rot_y2 = $mat4::from_axis_angle($vec3::Y, deg(180.0)); |
| 161 |
assert_approx_eq!(rot_y1, rot_y2); |
| 162 |
let rot_z1 = $mat4::from_rotation_z(deg(180.0)); |
| 163 |
let rot_z2 = $mat4::from_axis_angle($vec3::Z, deg(180.0)); |
| 164 |
assert_approx_eq!(rot_z1, rot_z2); |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
fn test_mat4_mul() { |
| 169 |
let m = $mat4::from_axis_angle($vec3::Z, deg(90.0)); |
| 170 |
let result3 = m.transform_vector3($vec3::Y); |
| 171 |
assert_approx_eq!($newvec3(-1.0, 0.0, 0.0), result3); |
| 172 |
assert_approx_eq!(result3, (m * $vec3::Y.extend(0.0)).truncate().into()); |
| 173 |
let result4 = m * $vec4::Y; |
| 174 |
assert_approx_eq!($newvec4(-1.0, 0.0, 0.0, 0.0), result4); |
| 175 |
assert_approx_eq!(result4, m * $vec4::Y); |
| 176 |
|
| 177 |
let m = $mat4::from_scale_rotation_translation( |
| 178 |
$vec3::new(0.5, 1.5, 2.0), |
| 179 |
$quat::from_rotation_x(deg(90.0)), |
| 180 |
$vec3::new(1.0, 2.0, 3.0), |
| 181 |
); |
| 182 |
let result3 = m.transform_vector3($vec3::Y); |
| 183 |
assert_approx_eq!($newvec3(0.0, 0.0, 1.5), result3, 1.0e-6); |
| 184 |
assert_approx_eq!(result3, (m * $vec3::Y.extend(0.0)).truncate().into()); |
| 185 |
|
| 186 |
let result3 = m.transform_point3($vec3::Y); |
| 187 |
assert_approx_eq!($newvec3(1.0, 2.0, 4.5), result3, 1.0e-6); |
| 188 |
assert_approx_eq!(result3, (m * $vec3::Y.extend(1.0)).truncate().into()); |
| 189 |
|
| 190 |
let m = $mat4::from_cols( |
| 191 |
$newvec4(8.0, 0.0, 0.0, 0.0), |
| 192 |
$newvec4(0.0, 4.0, 0.0, 0.0), |
| 193 |
$newvec4(0.0, 0.0, 2.0, 2.0), |
| 194 |
$newvec4(0.0, 0.0, 0.0, 0.0), |
| 195 |
); |
| 196 |
assert_approx_eq!( |
| 197 |
$newvec3(4.0, 2.0, 1.0), |
| 198 |
m.project_point3($newvec3(2.0, 2.0, 2.0)) |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
#[test] |
| 203 |
fn test_from_ypr() { |
| 204 |
let zero = deg(0.0); |
| 205 |
let yaw = deg(30.0); |
| 206 |
let pitch = deg(60.0); |
| 207 |
let roll = deg(90.0); |
| 208 |
let y0 = $mat4::from_rotation_y(yaw); |
| 209 |
let y1 = $mat4::from_rotation_ypr(yaw, zero, zero); |
| 210 |
assert_approx_eq!(y0, y1); |
| 211 |
|
| 212 |
let x0 = $mat4::from_rotation_x(pitch); |
| 213 |
let x1 = $mat4::from_rotation_ypr(zero, pitch, zero); |
| 214 |
assert_approx_eq!(x0, x1); |
| 215 |
|
| 216 |
let z0 = $mat4::from_rotation_z(roll); |
| 217 |
let z1 = $mat4::from_rotation_ypr(zero, zero, roll); |
| 218 |
assert_approx_eq!(z0, z1); |
| 219 |
|
| 220 |
let yx0 = y0 * x0; |
| 221 |
let yx1 = $mat4::from_rotation_ypr(yaw, pitch, zero); |
| 222 |
assert_approx_eq!(yx0, yx1); |
| 223 |
|
| 224 |
let yxz0 = y0 * x0 * z0; |
| 225 |
let yxz1 = $mat4::from_rotation_ypr(yaw, pitch, roll); |
| 226 |
assert_approx_eq!(yxz0, yxz1, 1e-6); |
| 227 |
} |
| 228 |
|
| 229 |
#[test] |
| 230 |
fn test_from_scale() { |
| 231 |
let m = $mat4::from_scale($vec3::new(2.0, 4.0, 8.0)); |
| 232 |
assert_approx_eq!($vec4::X * 2.0, m.x_axis); |
| 233 |
assert_approx_eq!($vec4::Y * 4.0, m.y_axis); |
| 234 |
assert_approx_eq!($vec4::Z * 8.0, m.z_axis); |
| 235 |
assert_approx_eq!($vec4::W, m.w_axis); |
| 236 |
assert_approx_eq!( |
| 237 |
m.transform_point3($vec3::new(1.0, 1.0, 1.0)), |
| 238 |
$vec3::new(2.0, 4.0, 8.0) |
| 239 |
); |
| 240 |
} |
| 241 |
|
| 242 |
#[test] |
| 243 |
fn test_mat4_transpose() { |
| 244 |
let m = $newmat4( |
| 245 |
$newvec4(1.0, 2.0, 3.0, 4.0), |
| 246 |
$newvec4(5.0, 6.0, 7.0, 8.0), |
| 247 |
$newvec4(9.0, 10.0, 11.0, 12.0), |
| 248 |
$newvec4(13.0, 14.0, 15.0, 16.0), |
| 249 |
); |
| 250 |
let mt = m.transpose(); |
| 251 |
assert_eq!($newvec4(1.0, 5.0, 9.0, 13.0), mt.x_axis); |
| 252 |
assert_eq!($newvec4(2.0, 6.0, 10.0, 14.0), mt.y_axis); |
| 253 |
assert_eq!($newvec4(3.0, 7.0, 11.0, 15.0), mt.z_axis); |
| 254 |
assert_eq!($newvec4(4.0, 8.0, 12.0, 16.0), mt.w_axis); |
| 255 |
} |
| 256 |
|
| 257 |
#[test] |
| 258 |
fn test_mat4_det() { |
| 259 |
assert_eq!(0.0, $mat4::ZERO.determinant()); |
| 260 |
assert_eq!(1.0, $mat4::IDENTITY.determinant()); |
| 261 |
assert_eq!(1.0, $mat4::from_rotation_x(deg(90.0)).determinant()); |
| 262 |
assert_eq!(1.0, $mat4::from_rotation_y(deg(180.0)).determinant()); |
| 263 |
assert_eq!(1.0, $mat4::from_rotation_z(deg(270.0)).determinant()); |
| 264 |
assert_eq!( |
| 265 |
2.0 * 2.0 * 2.0, |
| 266 |
$mat4::from_scale($newvec3(2.0, 2.0, 2.0)).determinant() |
| 267 |
); |
| 268 |
} |
| 269 |
|
| 270 |
#[test] |
| 271 |
fn test_mat4_inverse() { |
| 272 |
// assert_eq!(None, $mat4::ZERO.inverse()); |
| 273 |
let inv = $mat4::IDENTITY.inverse(); |
| 274 |
// assert_ne!(None, inv); |
| 275 |
assert_approx_eq!($mat4::IDENTITY, inv); |
| 276 |
|
| 277 |
let rotz = $mat4::from_rotation_z(deg(90.0)); |
| 278 |
let rotz_inv = rotz.inverse(); |
| 279 |
// assert_ne!(None, rotz_inv); |
| 280 |
// let rotz_inv = rotz_inv.unwrap(); |
| 281 |
assert_approx_eq!($mat4::IDENTITY, rotz * rotz_inv); |
| 282 |
assert_approx_eq!($mat4::IDENTITY, rotz_inv * rotz); |
| 283 |
|
| 284 |
let trans = $mat4::from_translation($newvec3(1.0, 2.0, 3.0)); |
| 285 |
let trans_inv = trans.inverse(); |
| 286 |
// assert_ne!(None, trans_inv); |
| 287 |
// let trans_inv = trans_inv.unwrap(); |
| 288 |
assert_approx_eq!($mat4::IDENTITY, trans * trans_inv); |
| 289 |
assert_approx_eq!($mat4::IDENTITY, trans_inv * trans); |
| 290 |
|
| 291 |
let scale = $mat4::from_scale($newvec3(4.0, 5.0, 6.0)); |
| 292 |
let scale_inv = scale.inverse(); |
| 293 |
// assert_ne!(None, scale_inv); |
| 294 |
// let scale_inv = scale_inv.unwrap(); |
| 295 |
assert_approx_eq!($mat4::IDENTITY, scale * scale_inv); |
| 296 |
assert_approx_eq!($mat4::IDENTITY, scale_inv * scale); |
| 297 |
|
| 298 |
let m = scale * rotz * trans; |
| 299 |
let m_inv = m.inverse(); |
| 300 |
// assert_ne!(None, m_inv); |
| 301 |
// let m_inv = m_inv.unwrap(); |
| 302 |
assert_approx_eq!($mat4::IDENTITY, m * m_inv, 1.0e-5); |
| 303 |
assert_approx_eq!($mat4::IDENTITY, m_inv * m, 1.0e-5); |
| 304 |
assert_approx_eq!(m_inv, trans_inv * rotz_inv * scale_inv, 1.0e-6); |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
fn test_mat4_decompose() { |
| 309 |
// identity |
| 310 |
let (out_scale, out_rotation, out_translation) = |
| 311 |
$mat4::IDENTITY.to_scale_rotation_translation(); |
| 312 |
assert_approx_eq!($vec3::ONE, out_scale); |
| 313 |
assert!(out_rotation.is_near_identity()); |
| 314 |
assert_approx_eq!($vec3::ZERO, out_translation); |
| 315 |
|
| 316 |
// no scale |
| 317 |
let in_scale = $vec3::ONE; |
| 318 |
let in_translation = $vec3::new(-2.0, 4.0, -0.125); |
| 319 |
let in_rotation = $quat::from_rotation_ypr( |
| 320 |
$t::to_radians(-45.0), |
| 321 |
$t::to_radians(180.0), |
| 322 |
$t::to_radians(270.0), |
| 323 |
); |
| 324 |
let in_mat = |
| 325 |
$mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation); |
| 326 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation(); |
| 327 |
assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 328 |
// out_rotation is different but produces the same matrix |
| 329 |
// assert_approx_eq!(in_rotation, out_rotation); |
| 330 |
assert_approx_eq!(in_translation, out_translation); |
| 331 |
assert_approx_eq!( |
| 332 |
in_mat, |
| 333 |
$mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation), |
| 334 |
1e-6 |
| 335 |
); |
| 336 |
|
| 337 |
// positive scale |
| 338 |
let in_scale = $vec3::new(1.0, 2.0, 4.0); |
| 339 |
let in_mat = |
| 340 |
$mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation); |
| 341 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation(); |
| 342 |
assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 343 |
// out_rotation is different but produces the same matrix |
| 344 |
// assert_approx_eq!(in_rotation, out_rotation); |
| 345 |
assert_approx_eq!(in_translation, out_translation); |
| 346 |
assert_approx_eq!( |
| 347 |
in_mat, |
| 348 |
$mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation), |
| 349 |
1e-6 |
| 350 |
); |
| 351 |
|
| 352 |
// negative scale |
| 353 |
let in_scale = $vec3::new(-4.0, 1.0, 2.0); |
| 354 |
let in_mat = |
| 355 |
$mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation); |
| 356 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation(); |
| 357 |
assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 358 |
// out_rotation is different but produces the same matrix |
| 359 |
// assert_approx_eq!(in_rotation, out_rotation); |
| 360 |
assert_approx_eq!(in_translation, out_translation); |
| 361 |
assert_approx_eq!( |
| 362 |
in_mat, |
| 363 |
$mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation), |
| 364 |
1e-5 |
| 365 |
); |
| 366 |
|
| 367 |
// negative scale |
| 368 |
let in_scale = $vec3::new(4.0, -1.0, -2.0); |
| 369 |
let in_mat = |
| 370 |
$mat4::from_scale_rotation_translation(in_scale, in_rotation, in_translation); |
| 371 |
let (out_scale, out_rotation, out_translation) = in_mat.to_scale_rotation_translation(); |
| 372 |
// out_scale and out_rotation are different but they produce the same matrix |
| 373 |
// assert_approx_eq!(in_scale, out_scale, 1e-6); |
| 374 |
// assert_approx_eq!(in_rotation, out_rotation); |
| 375 |
assert_approx_eq!(in_translation, out_translation); |
| 376 |
assert_approx_eq!( |
| 377 |
in_mat, |
| 378 |
$mat4::from_scale_rotation_translation(out_scale, out_rotation, out_translation), |
| 379 |
1e-6 |
| 380 |
); |
| 381 |
} |
| 382 |
|
| 383 |
#[test] |
| 384 |
fn test_mat4_look_at() { |
| 385 |
let eye = $vec3::new(0.0, 0.0, -5.0); |
| 386 |
let center = $vec3::new(0.0, 0.0, 0.0); |
| 387 |
let up = $vec3::new(1.0, 0.0, 0.0); |
| 388 |
let lh = $mat4::look_at_lh(eye, center, up); |
| 389 |
let rh = $mat4::look_at_rh(eye, center, up); |
| 390 |
let point = $vec3::new(1.0, 0.0, 0.0); |
| 391 |
assert_approx_eq!(lh.transform_point3(point), $vec3::new(0.0, 1.0, 5.0)); |
| 392 |
assert_approx_eq!(rh.transform_point3(point), $vec3::new(0.0, 1.0, -5.0)); |
| 393 |
} |
| 394 |
|
| 395 |
#[test] |
| 396 |
fn test_mat4_perspective_gl_rh() { |
| 397 |
let projection = $mat4::perspective_rh_gl($t::to_radians(90.0), 2.0, 5.0, 15.0); |
| 398 |
|
| 399 |
let original = $vec3::new(5.0, 5.0, -15.0); |
| 400 |
let projected = projection * original.extend(1.0); |
| 401 |
assert_approx_eq!($vec4::new(2.5, 5.0, 15.0, 15.0), projected); |
| 402 |
|
| 403 |
let original = $vec3::new(5.0, 5.0, -5.0); |
| 404 |
let projected = projection * original.extend(1.0); |
| 405 |
assert_approx_eq!($vec4::new(2.5, 5.0, -5.0, 5.0), projected); |
| 406 |
} |
| 407 |
|
| 408 |
#[test] |
| 409 |
fn test_mat4_perspective_lh() { |
| 410 |
let projection = $mat4::perspective_lh($t::to_radians(90.0), 2.0, 5.0, 15.0); |
| 411 |
|
| 412 |
let original = $vec3::new(5.0, 5.0, 15.0); |
| 413 |
let projected = projection * original.extend(1.0); |
| 414 |
assert_approx_eq!($vec4::new(2.5, 5.0, 15.0, 15.0), projected); |
| 415 |
|
| 416 |
let original = $vec3::new(5.0, 5.0, 5.0); |
| 417 |
let projected = projection * original.extend(1.0); |
| 418 |
assert_approx_eq!($vec4::new(2.5, 5.0, 0.0, 5.0), projected); |
| 419 |
} |
| 420 |
|
| 421 |
#[test] |
| 422 |
fn test_mat4_perspective_infinite_lh() { |
| 423 |
let projection = $mat4::perspective_infinite_lh($t::to_radians(90.0), 2.0, 5.0); |
| 424 |
|
| 425 |
let original = $vec3::new(5.0, 5.0, 15.0); |
| 426 |
let projected = projection * original.extend(1.0); |
| 427 |
assert_approx_eq!($vec4::new(2.5, 5.0, 10.0, 15.0), projected); |
| 428 |
|
| 429 |
let original = $vec3::new(5.0, 5.0, 5.0); |
| 430 |
let projected = projection * original.extend(1.0); |
| 431 |
assert_approx_eq!($vec4::new(2.5, 5.0, 0.0, 5.0), projected); |
| 432 |
} |
| 433 |
|
| 434 |
#[test] |
| 435 |
fn test_mat4_perspective_infinite_reverse_lh() { |
| 436 |
let projection = $mat4::perspective_infinite_reverse_lh($t::to_radians(90.0), 2.0, 5.0); |
| 437 |
|
| 438 |
let original = $vec3::new(5.0, 5.0, 15.0); |
| 439 |
let projected = projection * original.extend(1.0); |
| 440 |
assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, 15.0), projected); |
| 441 |
|
| 442 |
let original = $vec3::new(5.0, 5.0, 5.0); |
| 443 |
let projected = projection * original.extend(1.0); |
| 444 |
assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, 5.0), projected); |
| 445 |
} |
| 446 |
|
| 447 |
#[test] |
| 448 |
fn test_mat4_perspective_rh() { |
| 449 |
let projection = $mat4::perspective_rh($t::to_radians(90.0), 2.0, 5.0, 15.0); |
| 450 |
|
| 451 |
let original = $vec3::new(5.0, 5.0, 15.0); |
| 452 |
let projected = projection * original.extend(1.0); |
| 453 |
assert_approx_eq!($vec4::new(2.5, 5.0, -30.0, -15.0), projected); |
| 454 |
|
| 455 |
let original = $vec3::new(5.0, 5.0, 5.0); |
| 456 |
let projected = projection * original.extend(1.0); |
| 457 |
assert_approx_eq!($vec4::new(2.5, 5.0, -15.0, -5.0), projected); |
| 458 |
} |
| 459 |
|
| 460 |
#[test] |
| 461 |
fn test_mat4_perspective_infinite_rh() { |
| 462 |
let projection = $mat4::perspective_infinite_rh($t::to_radians(90.0), 2.0, 5.0); |
| 463 |
|
| 464 |
let original = $vec3::new(5.0, 5.0, 15.0); |
| 465 |
let projected = projection * original.extend(1.0); |
| 466 |
assert_approx_eq!($vec4::new(2.5, 5.0, -20.0, -15.0), projected); |
| 467 |
|
| 468 |
let original = $vec3::new(5.0, 5.0, 5.0); |
| 469 |
let projected = projection * original.extend(1.0); |
| 470 |
assert_approx_eq!($vec4::new(2.5, 5.0, -10.0, -5.0), projected); |
| 471 |
} |
| 472 |
|
| 473 |
#[test] |
| 474 |
fn test_mat4_perspective_infinite_reverse_rh() { |
| 475 |
let projection = $mat4::perspective_infinite_reverse_rh($t::to_radians(90.0), 2.0, 5.0); |
| 476 |
|
| 477 |
let original = $vec3::new(5.0, 5.0, 15.0); |
| 478 |
let projected = projection * original.extend(1.0); |
| 479 |
assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, -15.0), projected); |
| 480 |
|
| 481 |
let original = $vec3::new(5.0, 5.0, 5.0); |
| 482 |
let projected = projection * original.extend(1.0); |
| 483 |
assert_approx_eq!($vec4::new(2.5, 5.0, 5.0, -5.0), projected); |
| 484 |
} |
| 485 |
|
| 486 |
#[test] |
| 487 |
fn test_mat4_orthographic_gl_rh() { |
| 488 |
let projection = $mat4::orthographic_rh_gl(-10.0, 10.0, -5.0, 5.0, 0.0, -10.0); |
| 489 |
let original = $vec4::new(5.0, 5.0, -5.0, 1.0); |
| 490 |
let projected = projection.mul_vec4(original); |
| 491 |
assert_approx_eq!(projected, $vec4::new(0.5, 1.0, -2.0, 1.0)); |
| 492 |
} |
| 493 |
|
| 494 |
#[test] |
| 495 |
fn test_mat4_orthographic_rh() { |
| 496 |
let projection = $mat4::orthographic_rh(-10.0, 10.0, -5.0, 5.0, -10.0, 10.0); |
| 497 |
let original = $vec4::new(5.0, 5.0, -5.0, 1.0); |
| 498 |
let projected = projection.mul_vec4(original); |
| 499 |
assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.75, 1.0)); |
| 500 |
|
| 501 |
let original = $vec4::new(5.0, 5.0, 5.0, 1.0); |
| 502 |
let projected = projection.mul_vec4(original); |
| 503 |
assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.25, 1.0)); |
| 504 |
} |
| 505 |
|
| 506 |
#[test] |
| 507 |
fn test_mat4_orthographic_lh() { |
| 508 |
let projection = $mat4::orthographic_lh(-10.0, 10.0, -5.0, 5.0, -10.0, 10.0); |
| 509 |
let original = $vec4::new(5.0, 5.0, -5.0, 1.0); |
| 510 |
let projected = projection.mul_vec4(original); |
| 511 |
assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.25, 1.0)); |
| 512 |
|
| 513 |
let original = $vec4::new(5.0, 5.0, 5.0, 1.0); |
| 514 |
let projected = projection.mul_vec4(original); |
| 515 |
assert_approx_eq!(projected, $vec4::new(0.5, 1.0, 0.75, 1.0)); |
| 516 |
} |
| 517 |
|
| 518 |
#[test] |
| 519 |
fn test_mat4_ops() { |
| 520 |
let m0 = $mat4::from_cols_array_2d(&MATRIX); |
| 521 |
let m0x2 = $mat4::from_cols_array_2d(&[ |
| 522 |
[2.0, 4.0, 6.0, 8.0], |
| 523 |
[10.0, 12.0, 14.0, 16.0], |
| 524 |
[18.0, 20.0, 22.0, 24.0], |
| 525 |
[26.0, 28.0, 30.0, 32.0], |
| 526 |
]); |
| 527 |
assert_eq!(m0x2, m0 * 2.0); |
| 528 |
assert_eq!(m0x2, 2.0 * m0); |
| 529 |
assert_eq!(m0x2, m0 + m0); |
| 530 |
assert_eq!($mat4::ZERO, m0 - m0); |
| 531 |
assert_approx_eq!(m0, m0 * $mat4::IDENTITY); |
| 532 |
assert_approx_eq!(m0, $mat4::IDENTITY * m0); |
| 533 |
} |
| 534 |
|
| 535 |
#[test] |
| 536 |
fn test_mat4_fmt() { |
| 537 |
let a = $mat4::from_cols_array_2d(&MATRIX); |
| 538 |
assert_eq!( |
| 539 |
format!("{}", a), |
| 540 |
"[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]" |
| 541 |
); |
| 542 |
} |
| 543 |
|
| 544 |
#[cfg(feature = "std")] |
| 545 |
#[test] |
| 546 |
fn test_sum() { |
| 547 |
let id = $mat4::IDENTITY; |
| 548 |
assert_eq!(vec![id, id].iter().sum::<$mat4>(), id + id); |
| 549 |
} |
| 550 |
|
| 551 |
#[cfg(feature = "std")] |
| 552 |
#[test] |
| 553 |
fn test_product() { |
| 554 |
let two = $mat4::IDENTITY + $mat4::IDENTITY; |
| 555 |
assert_eq!(vec![two, two].iter().product::<$mat4>(), two * two); |
| 556 |
} |
| 557 |
|
| 558 |
#[test] |
| 559 |
fn test_mat4_is_finite() { |
| 560 |
assert!($mat4::IDENTITY.is_finite()); |
| 561 |
assert!(!($mat4::IDENTITY * INFINITY).is_finite()); |
| 562 |
assert!(!($mat4::IDENTITY * NEG_INFINITY).is_finite()); |
| 563 |
assert!(!($mat4::IDENTITY * NAN).is_finite()); |
| 564 |
} |
| 565 |
}; |
| 566 |
} |
| 567 |
|
| 568 |
mod mat4 { |
| 569 |
use super::support::deg; |
| 570 |
use glam::{const_mat4, mat4, vec3, vec4, Mat4, Quat, Vec3, Vec4}; |
| 571 |
|
| 572 |
#[test] |
| 573 |
fn test_align() { |
| 574 |
use std::mem; |
| 575 |
assert_eq!(64, mem::size_of::<Mat4>()); |
| 576 |
if cfg!(feature = "scalar-math") { |
| 577 |
assert_eq!(4, mem::align_of::<Mat4>()); |
| 578 |
} else { |
| 579 |
assert_eq!(16, mem::align_of::<Mat4>()); |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
#[test] |
| 584 |
fn test_as() { |
| 585 |
use glam::DMat4; |
| 586 |
assert_eq!( |
| 587 |
DMat4::from_cols_array_2d(&[ |
| 588 |
[1.0, 2.0, 3.0, 4.0], |
| 589 |
[5.0, 6.0, 7.0, 8.0], |
| 590 |
[9.0, 10.0, 11.0, 12.0], |
| 591 |
[13.0, 14.0, 15.0, 16.0] |
| 592 |
]), |
| 593 |
Mat4::from_cols_array_2d(&[ |
| 594 |
[1.0, 2.0, 3.0, 4.0], |
| 595 |
[5.0, 6.0, 7.0, 8.0], |
| 596 |
[9.0, 10.0, 11.0, 12.0], |
| 597 |
[13.0, 14.0, 15.0, 16.0] |
| 598 |
]) |
| 599 |
.as_f64() |
| 600 |
); |
| 601 |
assert_eq!( |
| 602 |
Mat4::from_cols_array_2d(&[ |
| 603 |
[1.0, 2.0, 3.0, 4.0], |
| 604 |
[5.0, 6.0, 7.0, 8.0], |
| 605 |
[9.0, 10.0, 11.0, 12.0], |
| 606 |
[13.0, 14.0, 15.0, 16.0] |
| 607 |
]), |
| 608 |
DMat4::from_cols_array_2d(&[ |
| 609 |
[1.0, 2.0, 3.0, 4.0], |
| 610 |
[5.0, 6.0, 7.0, 8.0], |
| 611 |
[9.0, 10.0, 11.0, 12.0], |
| 612 |
[13.0, 14.0, 15.0, 16.0] |
| 613 |
]) |
| 614 |
.as_f32() |
| 615 |
); |
| 616 |
} |
| 617 |
|
| 618 |
impl_mat4_tests!(f32, const_mat4, mat4, vec4, vec3, Mat4, Quat, Vec4, Vec3); |
| 619 |
} |
| 620 |
|
| 621 |
mod dmat4 { |
| 622 |
use super::support::deg; |
| 623 |
use glam::{const_dmat4, dmat4, dvec3, dvec4, DMat4, DQuat, DVec3, DVec4}; |
| 624 |
|
| 625 |
#[test] |
| 626 |
fn test_align() { |
| 627 |
use std::mem; |
| 628 |
assert_eq!(128, mem::size_of::<DMat4>()); |
| 629 |
assert_eq!(8, mem::align_of::<DMat4>()); |
| 630 |
} |
| 631 |
|
| 632 |
impl_mat4_tests!( |
| 633 |
f64, |
| 634 |
const_dmat4, |
| 635 |
dmat4, |
| 636 |
dvec4, |
| 637 |
dvec3, |
| 638 |
DMat4, |
| 639 |
DQuat, |
| 640 |
DVec4, |
| 641 |
DVec3 |
| 642 |
); |
| 643 |
} |