| 1 |
#[macro_use] |
| 2 |
mod support; |
| 3 |
|
| 4 |
macro_rules! impl_quat_tests { |
| 5 |
($t:ident, $const_new:ident, $new:ident, $mat3:ident, $mat4:ident, $quat:ident, $vec3:ident, $vec4:ident) => { |
| 6 |
use core::$t::INFINITY; |
| 7 |
use core::$t::NAN; |
| 8 |
use core::$t::NEG_INFINITY; |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn test_const() { |
| 12 |
const Q: $quat = $const_new!([1.0, 2.0, 3.0, 4.0]); |
| 13 |
assert_eq!($quat::from_xyzw(1.0, 2.0, 3.0, 4.0), Q); |
| 14 |
} |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn test_new() { |
| 18 |
let ytheta = deg(45.0); |
| 19 |
let q0 = $quat::from_rotation_y(ytheta); |
| 20 |
|
| 21 |
let t1 = (0.0, (ytheta * 0.5).sin(), 0.0, (ytheta * 0.5).cos()); |
| 22 |
assert_eq!(q0, t1.into()); |
| 23 |
let q1 = $quat::from(t1); |
| 24 |
assert_eq!(t1, q1.into()); |
| 25 |
|
| 26 |
assert_eq!(q0, $new(t1.0, t1.1, t1.2, t1.3)); |
| 27 |
|
| 28 |
let a1 = [0.0, (ytheta * 0.5).sin(), 0.0, (ytheta * 0.5).cos()]; |
| 29 |
assert_eq!(q0, a1.into()); |
| 30 |
let q1 = $quat::from(a1); |
| 31 |
let a2: [$t; 4] = q1.into(); |
| 32 |
assert_eq!(a1, a2); |
| 33 |
} |
| 34 |
|
| 35 |
#[test] |
| 36 |
fn test_funcs() { |
| 37 |
let q0 = $quat::from_rotation_ypr(deg(45.0), deg(180.0), deg(90.0)); |
| 38 |
assert!(q0.is_normalized()); |
| 39 |
assert_approx_eq!(q0.length_squared(), 1.0); |
| 40 |
assert_approx_eq!(q0.length(), 1.0); |
| 41 |
assert_approx_eq!(q0.length_recip(), 1.0); |
| 42 |
assert_approx_eq!(q0, q0.normalize()); |
| 43 |
|
| 44 |
assert_approx_eq!(q0.dot(q0), 1.0); |
| 45 |
assert_approx_eq!(q0.dot(q0), 1.0); |
| 46 |
|
| 47 |
let q1 = $quat::from($vec4::from(q0) * 2.0); |
| 48 |
assert!(!q1.is_normalized()); |
| 49 |
assert_approx_eq!(q1.length_squared(), 4.0, 1.0e-6); |
| 50 |
assert_approx_eq!(q1.length(), 2.0); |
| 51 |
assert_approx_eq!(q1.length_recip(), 0.5); |
| 52 |
assert_approx_eq!(q0, q1.normalize()); |
| 53 |
assert_approx_eq!(q0.dot(q1), 2.0, 1.0e-6); |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn test_rotation() { |
| 58 |
let zero = deg(0.0); |
| 59 |
let yaw = deg(30.0); |
| 60 |
let pitch = deg(60.0); |
| 61 |
let roll = deg(90.0); |
| 62 |
let y0 = $quat::from_rotation_y(yaw); |
| 63 |
assert!(y0.is_normalized()); |
| 64 |
let (axis, angle) = y0.to_axis_angle(); |
| 65 |
assert_approx_eq!(axis, $vec3::Y, 1.0e-6); |
| 66 |
assert_approx_eq!(angle, yaw); |
| 67 |
let y1 = $quat::from_rotation_ypr(yaw, zero, zero); |
| 68 |
assert_approx_eq!(y0, y1); |
| 69 |
let y2 = $quat::from_axis_angle($vec3::Y, yaw); |
| 70 |
assert_approx_eq!(y0, y2); |
| 71 |
let y3 = $quat::from_rotation_mat3(&$mat3::from_rotation_y(yaw)); |
| 72 |
assert_approx_eq!(y0, y3); |
| 73 |
let y4 = $quat::from_rotation_mat3(&$mat3::from_quat(y0)); |
| 74 |
assert_approx_eq!(y0, y4); |
| 75 |
|
| 76 |
let x0 = $quat::from_rotation_x(pitch); |
| 77 |
assert!(x0.is_normalized()); |
| 78 |
let (axis, angle) = x0.to_axis_angle(); |
| 79 |
assert_approx_eq!(axis, $vec3::X); |
| 80 |
assert_approx_eq!(angle, pitch); |
| 81 |
let x1 = $quat::from_rotation_ypr(zero, pitch, zero); |
| 82 |
assert_approx_eq!(x0, x1); |
| 83 |
let x2 = $quat::from_axis_angle($vec3::X, pitch); |
| 84 |
assert_approx_eq!(x0, x2); |
| 85 |
let x3 = $quat::from_rotation_mat4(&$mat4::from_rotation_x(deg(180.0))); |
| 86 |
assert!(x3.is_normalized()); |
| 87 |
assert_approx_eq!($quat::from_rotation_x(deg(180.0)), x3); |
| 88 |
|
| 89 |
let z0 = $quat::from_rotation_z(roll); |
| 90 |
assert!(z0.is_normalized()); |
| 91 |
let (axis, angle) = z0.to_axis_angle(); |
| 92 |
assert_approx_eq!(axis, $vec3::Z); |
| 93 |
assert_approx_eq!(angle, roll); |
| 94 |
let z1 = $quat::from_rotation_ypr(zero, zero, roll); |
| 95 |
assert_approx_eq!(z0, z1); |
| 96 |
let z2 = $quat::from_axis_angle($vec3::Z, roll); |
| 97 |
assert_approx_eq!(z0, z2); |
| 98 |
let z3 = $quat::from_rotation_mat4(&$mat4::from_rotation_z(roll)); |
| 99 |
assert_approx_eq!(z0, z3); |
| 100 |
|
| 101 |
let yx0 = y0 * x0; |
| 102 |
assert!(yx0.is_normalized()); |
| 103 |
let yx1 = $quat::from_rotation_ypr(yaw, pitch, zero); |
| 104 |
assert_approx_eq!(yx0, yx1); |
| 105 |
|
| 106 |
let yxz0 = y0 * x0 * z0; |
| 107 |
assert!(yxz0.is_normalized()); |
| 108 |
let yxz1 = $quat::from_rotation_ypr(yaw, pitch, roll); |
| 109 |
assert_approx_eq!(yxz0, yxz1); |
| 110 |
|
| 111 |
// use the conjugate of z0 to remove the rotation from yxz0 |
| 112 |
let yx2 = yxz0 * z0.conjugate(); |
| 113 |
assert_approx_eq!(yx0, yx2); |
| 114 |
assert!((yxz0 * yxz0.conjugate()).is_near_identity()); |
| 115 |
|
| 116 |
// test inverse does the same |
| 117 |
let yx2 = yxz0 * z0.inverse(); |
| 118 |
assert_approx_eq!(yx0, yx2); |
| 119 |
assert!((yxz0 * yxz0.inverse()).is_near_identity()); |
| 120 |
|
| 121 |
let yxz2 = $quat::from_rotation_mat4(&$mat4::from_quat(yxz0)); |
| 122 |
assert_approx_eq!(yxz0, yxz2); |
| 123 |
|
| 124 |
// if near identity, just returns x axis and 0 rotation |
| 125 |
let (axis, angle) = $quat::IDENTITY.to_axis_angle(); |
| 126 |
assert_eq!(axis, $vec3::X); |
| 127 |
assert_eq!(angle, rad(0.0)); |
| 128 |
} |
| 129 |
|
| 130 |
#[test] |
| 131 |
fn test_from_scaled_axis() { |
| 132 |
assert_eq!($quat::from_scaled_axis($vec3::ZERO), $quat::IDENTITY); |
| 133 |
assert_eq!( |
| 134 |
$quat::from_scaled_axis($vec3::Y * 1e-10), |
| 135 |
$quat::from_axis_angle($vec3::Y, 1e-10) |
| 136 |
); |
| 137 |
assert_eq!( |
| 138 |
$quat::from_scaled_axis($vec3::X * 1.0), |
| 139 |
$quat::from_axis_angle($vec3::X, 1.0) |
| 140 |
); |
| 141 |
assert_eq!( |
| 142 |
$quat::from_scaled_axis($vec3::Z * 2.0), |
| 143 |
$quat::from_axis_angle($vec3::Z, 2.0) |
| 144 |
); |
| 145 |
|
| 146 |
assert_eq!( |
| 147 |
$quat::from_scaled_axis($vec3::ZERO).to_scaled_axis(), |
| 148 |
$vec3::ZERO |
| 149 |
); |
| 150 |
for &v in &vec3_float_test_vectors!($vec3) { |
| 151 |
if v.length() < core::$t::consts::PI { |
| 152 |
assert!(($quat::from_scaled_axis(v).to_scaled_axis() - v).length() < 1e-6,); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
#[test] |
| 158 |
fn test_mul_vec3() { |
| 159 |
let qrz = $quat::from_rotation_z(deg(90.0)); |
| 160 |
assert_approx_eq!($vec3::Y, qrz * $vec3::X); |
| 161 |
assert_approx_eq!($vec3::Y, qrz.mul_vec3($vec3::X)); |
| 162 |
assert_approx_eq!($vec3::Y, -qrz * $vec3::X); |
| 163 |
assert_approx_eq!($vec3::Y, qrz.neg().mul_vec3($vec3::X)); |
| 164 |
assert_approx_eq!(-$vec3::X, qrz * $vec3::Y); |
| 165 |
assert_approx_eq!(-$vec3::X, qrz.mul_vec3($vec3::Y)); |
| 166 |
assert_approx_eq!(-$vec3::X, -qrz * $vec3::Y); |
| 167 |
assert_approx_eq!(-$vec3::X, qrz.neg().mul_vec3($vec3::Y)); |
| 168 |
|
| 169 |
// check vec3 * mat3 is the same |
| 170 |
let mrz = $mat3::from_quat(qrz); |
| 171 |
assert_approx_eq!($vec3::Y, mrz * $vec3::X); |
| 172 |
assert_approx_eq!($vec3::Y, mrz.mul_vec3($vec3::X)); |
| 173 |
// assert_approx_eq!($vec3::Y, -mrz * $vec3::X); |
| 174 |
assert_approx_eq!(-$vec3::X, mrz * $vec3::Y); |
| 175 |
assert_approx_eq!(-$vec3::X, mrz.mul_vec3($vec3::Y)); |
| 176 |
|
| 177 |
let qrx = $quat::from_rotation_x(deg(90.0)); |
| 178 |
assert_approx_eq!($vec3::X, qrx * $vec3::X); |
| 179 |
assert_approx_eq!($vec3::X, qrx.mul_vec3($vec3::X)); |
| 180 |
assert_approx_eq!($vec3::X, -qrx * $vec3::X); |
| 181 |
assert_approx_eq!($vec3::X, qrx.neg().mul_vec3($vec3::X)); |
| 182 |
assert_approx_eq!($vec3::Z, qrx * $vec3::Y); |
| 183 |
assert_approx_eq!($vec3::Z, qrx.mul_vec3($vec3::Y)); |
| 184 |
assert_approx_eq!($vec3::Z, -qrx * $vec3::Y); |
| 185 |
assert_approx_eq!($vec3::Z, qrx.neg().mul_vec3($vec3::Y)); |
| 186 |
|
| 187 |
// check vec3 * mat3 is the same |
| 188 |
let mrx = $mat3::from_quat(qrx); |
| 189 |
assert_approx_eq!($vec3::X, mrx * $vec3::X); |
| 190 |
assert_approx_eq!($vec3::X, mrx.mul_vec3($vec3::X)); |
| 191 |
assert_approx_eq!($vec3::Z, mrx * $vec3::Y); |
| 192 |
assert_approx_eq!($vec3::Z, mrx.mul_vec3($vec3::Y)); |
| 193 |
|
| 194 |
let qrxz = qrz * qrx; |
| 195 |
assert_approx_eq!($vec3::Y, qrxz * $vec3::X); |
| 196 |
assert_approx_eq!($vec3::Y, qrxz.mul_vec3($vec3::X)); |
| 197 |
assert_approx_eq!($vec3::Z, qrxz * $vec3::Y); |
| 198 |
assert_approx_eq!($vec3::Z, qrxz.mul_vec3($vec3::Y)); |
| 199 |
|
| 200 |
let mrxz = mrz * mrx; |
| 201 |
assert_approx_eq!($vec3::Y, mrxz * $vec3::X); |
| 202 |
assert_approx_eq!($vec3::Y, mrxz.mul_vec3($vec3::X)); |
| 203 |
assert_approx_eq!($vec3::Z, mrxz * $vec3::Y); |
| 204 |
assert_approx_eq!($vec3::Z, mrxz.mul_vec3($vec3::Y)); |
| 205 |
|
| 206 |
let qrzx = qrx * qrz; |
| 207 |
assert_approx_eq!($vec3::Z, qrzx * $vec3::X); |
| 208 |
assert_approx_eq!($vec3::Z, qrzx.mul_vec3($vec3::X)); |
| 209 |
assert_approx_eq!(-$vec3::X, qrzx * $vec3::Y); |
| 210 |
assert_approx_eq!(-$vec3::X, qrzx.mul_vec3($vec3::Y)); |
| 211 |
|
| 212 |
let mrzx = qrx * qrz; |
| 213 |
assert_approx_eq!($vec3::Z, mrzx * $vec3::X); |
| 214 |
assert_approx_eq!($vec3::Z, mrzx.mul_vec3($vec3::X)); |
| 215 |
assert_approx_eq!(-$vec3::X, mrzx * $vec3::Y); |
| 216 |
assert_approx_eq!(-$vec3::X, mrzx.mul_vec3($vec3::Y)); |
| 217 |
} |
| 218 |
|
| 219 |
#[test] |
| 220 |
fn test_angle_between() { |
| 221 |
const TAU: $t = 2.0 * core::$t::consts::PI; |
| 222 |
let eps = 10.0 * core::$t::EPSILON as f32; |
| 223 |
let q1 = $quat::from_rotation_ypr(0.0, 0.0, 0.0); |
| 224 |
let q2 = $quat::from_rotation_ypr(TAU * 0.25, 0.0, 0.0); |
| 225 |
let q3 = $quat::from_rotation_ypr(TAU * 0.5, 0.0, 0.0); |
| 226 |
let q4 = $quat::from_rotation_ypr(0.0, 0.0, TAU * 0.25); |
| 227 |
let q5 = $quat::from_axis_angle($vec3::new(1.0, 2.0, 3.0).normalize(), TAU * 0.3718); |
| 228 |
let q6 = $quat::from_axis_angle($vec3::new(-1.0, 5.0, 3.0).normalize(), TAU * 0.94); |
| 229 |
assert_approx_eq!(q1.angle_between(q2), TAU * 0.25, eps); |
| 230 |
assert_approx_eq!(q1.angle_between(q3), TAU * 0.5, eps); |
| 231 |
assert_approx_eq!(q3.angle_between(q3), 0.0, eps); |
| 232 |
assert_approx_eq!(q3.angle_between(-q3), 0.0, eps); |
| 233 |
assert_approx_eq!((q4 * q2 * q2).angle_between(q4 * q2), TAU * 0.25, eps); |
| 234 |
assert_approx_eq!(q1.angle_between(q5), TAU * 0.3718, eps); |
| 235 |
assert_approx_eq!( |
| 236 |
(q5 * q2 * q1).angle_between(q5 * q2 * q5), |
| 237 |
TAU * 0.3718, |
| 238 |
eps |
| 239 |
); |
| 240 |
assert_approx_eq!((q3 * q3).angle_between(q1), 0.0, eps); |
| 241 |
assert_approx_eq!((q3 * q3 * q3).angle_between(q3), 0.0, eps); |
| 242 |
assert_approx_eq!((q3 * q3 * q3 * q3).angle_between(q1), 0.0, eps); |
| 243 |
assert_approx_eq!(q1.angle_between(q6), TAU - TAU * 0.94, eps); |
| 244 |
assert_approx_eq!((q5 * q1).angle_between(q5 * q6), TAU - TAU * 0.94, eps); |
| 245 |
assert_approx_eq!((q1 * q5).angle_between(q6 * q5), TAU - TAU * 0.94, eps); |
| 246 |
} |
| 247 |
|
| 248 |
#[test] |
| 249 |
fn test_lerp() { |
| 250 |
let q0 = $quat::from_rotation_y(deg(0.0)); |
| 251 |
let q1 = $quat::from_rotation_y(deg(90.0)); |
| 252 |
assert_approx_eq!(q0, q0.lerp(q1, 0.0)); |
| 253 |
assert_approx_eq!(q1, q0.lerp(q1, 1.0)); |
| 254 |
assert_approx_eq!($quat::from_rotation_y(deg(45.0)), q0.lerp(q1, 0.5)); |
| 255 |
} |
| 256 |
|
| 257 |
#[test] |
| 258 |
fn test_slerp() { |
| 259 |
let q0 = $quat::from_rotation_y(deg(0.0)); |
| 260 |
let q1 = $quat::from_rotation_y(deg(90.0)); |
| 261 |
assert_approx_eq!(q0, q0.slerp(q1, 0.0), 1.0e-3); |
| 262 |
assert_approx_eq!(q1, q0.slerp(q1, 1.0), 1.0e-3); |
| 263 |
assert_approx_eq!($quat::from_rotation_y(deg(45.0)), q0.slerp(q1, 0.5), 1.0e-3); |
| 264 |
} |
| 265 |
|
| 266 |
#[test] |
| 267 |
fn test_slerp_constant_speed() { |
| 268 |
let step = 0.01; |
| 269 |
let mut s = 0.0; |
| 270 |
while s <= 1.0 { |
| 271 |
let q0 = $quat::from_rotation_y(deg(0.0)); |
| 272 |
let q1 = $quat::from_rotation_y(deg(90.0)); |
| 273 |
assert_approx_eq!( |
| 274 |
$quat::from_rotation_y(deg(s * 90.0)), |
| 275 |
q0.slerp(q1, s), |
| 276 |
1.0e-3 |
| 277 |
); |
| 278 |
s += step; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
#[test] |
| 283 |
fn test_fmt() { |
| 284 |
let a = $quat::IDENTITY; |
| 285 |
assert_eq!( |
| 286 |
format!("{:?}", a), |
| 287 |
format!("{}(0.0, 0.0, 0.0, 1.0)", stringify!($quat)) |
| 288 |
); |
| 289 |
// assert_eq!( |
| 290 |
// format!("{:#?}", a), |
| 291 |
// "$quat(\n 1.0,\n 2.0,\n 3.0,\n 4.0\n)" |
| 292 |
// ); |
| 293 |
assert_eq!(format!("{}", a), "[0, 0, 0, 1]"); |
| 294 |
} |
| 295 |
|
| 296 |
#[test] |
| 297 |
fn test_identity() { |
| 298 |
let identity = $quat::IDENTITY; |
| 299 |
assert!(identity.is_near_identity()); |
| 300 |
assert!(identity.is_normalized()); |
| 301 |
assert_eq!(identity, $quat::from_xyzw(0.0, 0.0, 0.0, 1.0)); |
| 302 |
assert_eq!(identity, identity * identity); |
| 303 |
let q = $quat::from_rotation_ypr(deg(10.0), deg(-10.0), deg(45.0)); |
| 304 |
assert_eq!(q, q * identity); |
| 305 |
assert_eq!(q, identity * q); |
| 306 |
assert_eq!(identity, $quat::default()); |
| 307 |
} |
| 308 |
|
| 309 |
#[test] |
| 310 |
fn test_slice() { |
| 311 |
let a: [$t; 4] = $quat::from_rotation_ypr(deg(30.0), deg(60.0), deg(90.0)).into(); |
| 312 |
let b = $quat::from_slice_unaligned(&a); |
| 313 |
let c: [$t; 4] = b.into(); |
| 314 |
assert_eq!(a, c); |
| 315 |
let mut d = [0.0, 0.0, 0.0, 0.0]; |
| 316 |
b.write_to_slice_unaligned(&mut d[..]); |
| 317 |
assert_eq!(a, d); |
| 318 |
} |
| 319 |
|
| 320 |
#[test] |
| 321 |
fn test_elements() { |
| 322 |
let x = 1.0; |
| 323 |
let y = 2.0; |
| 324 |
let z = 3.0; |
| 325 |
let w = 4.0; |
| 326 |
|
| 327 |
let a = $quat::from_xyzw(x, y, z, w); |
| 328 |
assert!(a.x == x); |
| 329 |
assert!(a.y == y); |
| 330 |
assert!(a.z == z); |
| 331 |
assert!(a.w == w); |
| 332 |
} |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn test_addition() { |
| 336 |
let a = $quat::from_xyzw(1.0, 2.0, 3.0, 4.0); |
| 337 |
let b = $quat::from_xyzw(5.0, 6.0, 7.0, -9.0); |
| 338 |
assert_eq!(a + b, $quat::from_xyzw(6.0, 8.0, 10.0, -5.0)); |
| 339 |
} |
| 340 |
|
| 341 |
#[test] |
| 342 |
fn test_subtraction() { |
| 343 |
let a = $quat::from_xyzw(6.0, 8.0, 10.0, -5.0); |
| 344 |
let b = $quat::from_xyzw(5.0, 6.0, 7.0, -9.0); |
| 345 |
assert_eq!(a - b, $quat::from_xyzw(1.0, 2.0, 3.0, 4.0)); |
| 346 |
} |
| 347 |
|
| 348 |
#[test] |
| 349 |
fn test_scalar_multiplication() { |
| 350 |
let a = $quat::from_xyzw(1.0, 2.0, 3.0, 4.0); |
| 351 |
assert_eq!(a * 2.0, $quat::from_xyzw(2.0, 4.0, 6.0, 8.0)); |
| 352 |
} |
| 353 |
|
| 354 |
#[test] |
| 355 |
fn test_scalar_division() { |
| 356 |
let a = $quat::from_xyzw(2.0, 4.0, 6.0, 8.0); |
| 357 |
assert_eq!(a / 2.0, $quat::from_xyzw(1.0, 2.0, 3.0, 4.0)); |
| 358 |
} |
| 359 |
|
| 360 |
#[cfg(feature = "std")] |
| 361 |
#[test] |
| 362 |
fn test_sum() { |
| 363 |
let two = $new(2.0, 2.0, 2.0, 2.0); |
| 364 |
assert_eq!(vec![two, two].iter().sum::<$quat>(), two + two); |
| 365 |
} |
| 366 |
|
| 367 |
#[cfg(feature = "std")] |
| 368 |
#[test] |
| 369 |
fn test_product() { |
| 370 |
let two = $new(2.0, 2.0, 2.0, 2.0).normalize(); |
| 371 |
assert_eq!(vec![two, two].iter().product::<$quat>(), two * two); |
| 372 |
} |
| 373 |
|
| 374 |
#[test] |
| 375 |
fn test_is_finite() { |
| 376 |
assert!($quat::from_xyzw(0.0, 0.0, 0.0, 0.0).is_finite()); |
| 377 |
assert!($quat::from_xyzw(-1e-10, 1.0, 1e10, 42.0).is_finite()); |
| 378 |
assert!(!$quat::from_xyzw(INFINITY, 0.0, 0.0, 0.0).is_finite()); |
| 379 |
assert!(!$quat::from_xyzw(0.0, NAN, 0.0, 0.0).is_finite()); |
| 380 |
assert!(!$quat::from_xyzw(0.0, 0.0, NEG_INFINITY, 0.0).is_finite()); |
| 381 |
assert!(!$quat::from_xyzw(0.0, 0.0, 0.0, NAN).is_finite()); |
| 382 |
} |
| 383 |
|
| 384 |
#[test] |
| 385 |
#[cfg(feature = "std")] |
| 386 |
fn test_rotation_arc() { |
| 387 |
let eps = 2.0 * core::$t::EPSILON.sqrt(); |
| 388 |
|
| 389 |
for &from in &vec3_float_test_vectors!($vec3) { |
| 390 |
let from = from.normalize(); |
| 391 |
|
| 392 |
{ |
| 393 |
let q = $quat::from_rotation_arc(from, from); |
| 394 |
assert!(q.is_near_identity(), "from: {}, q: {}", from, q); |
| 395 |
} |
| 396 |
|
| 397 |
{ |
| 398 |
let q = $quat::from_rotation_arc_colinear(from, from); |
| 399 |
assert!(q.is_near_identity(), "from: {}, q: {}", from, q); |
| 400 |
} |
| 401 |
|
| 402 |
{ |
| 403 |
let to = -from; |
| 404 |
let q = $quat::from_rotation_arc(from, to); |
| 405 |
assert!(q.is_normalized()); |
| 406 |
assert!((q * from - to).length() < eps); |
| 407 |
} |
| 408 |
|
| 409 |
{ |
| 410 |
let to = -from; |
| 411 |
let q = $quat::from_rotation_arc_colinear(from, to); |
| 412 |
assert!(q.is_near_identity(), "from: {}, q: {}", from, q); |
| 413 |
} |
| 414 |
|
| 415 |
for &to in &vec3_float_test_vectors!($vec3) { |
| 416 |
let to = to.normalize(); |
| 417 |
|
| 418 |
let q = $quat::from_rotation_arc(from, to); |
| 419 |
assert!(q.is_normalized()); |
| 420 |
assert!((q * from - to).length() < eps); |
| 421 |
|
| 422 |
let q = $quat::from_rotation_arc_colinear(from, to); |
| 423 |
assert!(q.is_normalized()); |
| 424 |
let transformed = q * from; |
| 425 |
assert!( |
| 426 |
(transformed - to).length() < eps || (-transformed - to).length() < eps |
| 427 |
); |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
}; |
| 432 |
} |
| 433 |
|
| 434 |
mod quat { |
| 435 |
use crate::support::{deg, rad}; |
| 436 |
use core::ops::Neg; |
| 437 |
use glam::{const_quat, quat, Mat3, Mat4, Quat, Vec3, Vec3A, Vec4}; |
| 438 |
|
| 439 |
#[test] |
| 440 |
fn test_align() { |
| 441 |
use std::mem; |
| 442 |
assert_eq!(16, mem::size_of::<Quat>()); |
| 443 |
if cfg!(feature = "scalar-math") { |
| 444 |
assert_eq!(4, mem::align_of::<Quat>()); |
| 445 |
} else { |
| 446 |
assert_eq!(16, mem::align_of::<Quat>()); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
#[test] |
| 451 |
fn test_mul_vec3a() { |
| 452 |
let qrz = Quat::from_rotation_z(deg(90.0)); |
| 453 |
assert_approx_eq!(Vec3A::Y, qrz * Vec3A::X); |
| 454 |
assert_approx_eq!(Vec3A::Y, qrz.mul_vec3a(Vec3A::X)); |
| 455 |
assert_approx_eq!(Vec3A::Y, -qrz * Vec3A::X); |
| 456 |
assert_approx_eq!(Vec3A::Y, qrz.neg().mul_vec3a(Vec3A::X)); |
| 457 |
assert_approx_eq!(-Vec3A::X, qrz * Vec3A::Y); |
| 458 |
assert_approx_eq!(-Vec3A::X, qrz.mul_vec3a(Vec3A::Y)); |
| 459 |
assert_approx_eq!(-Vec3A::X, -qrz * Vec3A::Y); |
| 460 |
assert_approx_eq!(-Vec3A::X, qrz.neg().mul_vec3a(Vec3A::Y)); |
| 461 |
|
| 462 |
// check vec3 * mat3 is the same |
| 463 |
let mrz = Mat3::from_quat(qrz); |
| 464 |
assert_approx_eq!(Vec3A::Y, mrz * Vec3A::X); |
| 465 |
assert_approx_eq!(Vec3A::Y, mrz.mul_vec3a(Vec3A::X)); |
| 466 |
// assert_approx_eq!(Vec3A::Y, -mrz * Vec3A::X); |
| 467 |
assert_approx_eq!(-Vec3A::X, mrz * Vec3A::Y); |
| 468 |
assert_approx_eq!(-Vec3A::X, mrz.mul_vec3a(Vec3A::Y)); |
| 469 |
|
| 470 |
let qrx = Quat::from_rotation_x(deg(90.0)); |
| 471 |
assert_approx_eq!(Vec3A::X, qrx * Vec3A::X); |
| 472 |
assert_approx_eq!(Vec3A::X, qrx.mul_vec3a(Vec3A::X)); |
| 473 |
assert_approx_eq!(Vec3A::X, -qrx * Vec3A::X); |
| 474 |
assert_approx_eq!(Vec3A::X, qrx.neg().mul_vec3a(Vec3A::X)); |
| 475 |
assert_approx_eq!(Vec3A::Z, qrx * Vec3A::Y); |
| 476 |
assert_approx_eq!(Vec3A::Z, qrx.mul_vec3a(Vec3A::Y)); |
| 477 |
assert_approx_eq!(Vec3A::Z, -qrx * Vec3A::Y); |
| 478 |
assert_approx_eq!(Vec3A::Z, qrx.neg().mul_vec3a(Vec3A::Y)); |
| 479 |
|
| 480 |
// check vec3 * mat3 is the same |
| 481 |
let mrx = Mat3::from_quat(qrx); |
| 482 |
assert_approx_eq!(Vec3A::X, mrx * Vec3A::X); |
| 483 |
assert_approx_eq!(Vec3A::X, mrx.mul_vec3a(Vec3A::X)); |
| 484 |
assert_approx_eq!(Vec3A::Z, mrx * Vec3A::Y); |
| 485 |
assert_approx_eq!(Vec3A::Z, mrx.mul_vec3a(Vec3A::Y)); |
| 486 |
|
| 487 |
let qrxz = qrz * qrx; |
| 488 |
assert_approx_eq!(Vec3A::Y, qrxz * Vec3A::X); |
| 489 |
assert_approx_eq!(Vec3A::Y, qrxz.mul_vec3a(Vec3A::X)); |
| 490 |
assert_approx_eq!(Vec3A::Z, qrxz * Vec3A::Y); |
| 491 |
assert_approx_eq!(Vec3A::Z, qrxz.mul_vec3a(Vec3A::Y)); |
| 492 |
|
| 493 |
let mrxz = mrz * mrx; |
| 494 |
assert_approx_eq!(Vec3A::Y, mrxz * Vec3A::X); |
| 495 |
assert_approx_eq!(Vec3A::Y, mrxz.mul_vec3a(Vec3A::X)); |
| 496 |
assert_approx_eq!(Vec3A::Z, mrxz * Vec3A::Y); |
| 497 |
assert_approx_eq!(Vec3A::Z, mrxz.mul_vec3a(Vec3A::Y)); |
| 498 |
|
| 499 |
let qrzx = qrx * qrz; |
| 500 |
assert_approx_eq!(Vec3A::Z, qrzx * Vec3A::X); |
| 501 |
assert_approx_eq!(Vec3A::Z, qrzx.mul_vec3a(Vec3A::X)); |
| 502 |
assert_approx_eq!(-Vec3A::X, qrzx * Vec3A::Y); |
| 503 |
assert_approx_eq!(-Vec3A::X, qrzx.mul_vec3a(Vec3A::Y)); |
| 504 |
|
| 505 |
let mrzx = qrx * qrz; |
| 506 |
assert_approx_eq!(Vec3A::Z, mrzx * Vec3A::X); |
| 507 |
assert_approx_eq!(Vec3A::Z, mrzx.mul_vec3a(Vec3A::X)); |
| 508 |
assert_approx_eq!(-Vec3A::X, mrzx * Vec3A::Y); |
| 509 |
assert_approx_eq!(-Vec3A::X, mrzx.mul_vec3a(Vec3A::Y)); |
| 510 |
} |
| 511 |
|
| 512 |
#[test] |
| 513 |
fn test_as() { |
| 514 |
use glam::DQuat; |
| 515 |
assert_approx_eq!( |
| 516 |
DQuat::from_rotation_ypr(1.0, 2.0, 3.0), |
| 517 |
Quat::from_rotation_ypr(1.0, 2.0, 3.0).as_f64() |
| 518 |
); |
| 519 |
assert_approx_eq!( |
| 520 |
Quat::from_rotation_ypr(1.0, 2.0, 3.0), |
| 521 |
DQuat::from_rotation_ypr(1.0, 2.0, 3.0).as_f32() |
| 522 |
); |
| 523 |
} |
| 524 |
|
| 525 |
impl_quat_tests!(f32, const_quat, quat, Mat3, Mat4, Quat, Vec3, Vec4); |
| 526 |
} |
| 527 |
|
| 528 |
mod dquat { |
| 529 |
use crate::support::{deg, rad}; |
| 530 |
use core::ops::Neg; |
| 531 |
use glam::{const_dquat, dquat, DMat3, DMat4, DQuat, DVec3, DVec4}; |
| 532 |
|
| 533 |
#[test] |
| 534 |
fn test_align() { |
| 535 |
use std::mem; |
| 536 |
assert_eq!(32, mem::size_of::<DQuat>()); |
| 537 |
assert_eq!(8, mem::align_of::<DQuat>()); |
| 538 |
} |
| 539 |
|
| 540 |
impl_quat_tests!(f64, const_dquat, dquat, DMat3, DMat4, DQuat, DVec3, DVec4); |
| 541 |
} |