| 1 |
#![allow(deprecated)] |
| 2 |
|
| 3 |
#[cfg(feature = "transform-types")] |
| 4 |
#[macro_use] |
| 5 |
mod support; |
| 6 |
|
| 7 |
#[cfg(feature = "transform-types")] |
| 8 |
mod transform { |
| 9 |
use crate::support::FloatCompare; |
| 10 |
use glam::*; |
| 11 |
|
| 12 |
impl FloatCompare for TransformSRT { |
| 13 |
#[inline] |
| 14 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 15 |
self.abs_diff_eq(*other, max_abs_diff) |
| 16 |
} |
| 17 |
|
| 18 |
#[inline] |
| 19 |
fn abs_diff(&self, other: &Self) -> Self { |
| 20 |
Self::from_scale_rotation_translation( |
| 21 |
self.scale.abs_diff(&other.scale), |
| 22 |
self.rotation.abs_diff(&other.rotation), |
| 23 |
self.translation.abs_diff(&other.translation), |
| 24 |
) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
impl FloatCompare for TransformRT { |
| 29 |
#[inline] |
| 30 |
fn approx_eq(&self, other: &Self, max_abs_diff: f32) -> bool { |
| 31 |
self.abs_diff_eq(*other, max_abs_diff) |
| 32 |
} |
| 33 |
|
| 34 |
#[inline] |
| 35 |
fn abs_diff(&self, other: &Self) -> Self { |
| 36 |
Self::from_rotation_translation( |
| 37 |
self.rotation.abs_diff(&other.rotation), |
| 38 |
self.translation.abs_diff(&other.translation), |
| 39 |
) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn test_identity() { |
| 45 |
let tr = TransformRT::IDENTITY; |
| 46 |
assert_eq!(tr.rotation, Quat::IDENTITY); |
| 47 |
assert_eq!(tr.translation, Vec3::ZERO); |
| 48 |
|
| 49 |
let srt = TransformSRT::IDENTITY; |
| 50 |
assert_eq!(srt.scale, Vec3::ONE); |
| 51 |
assert_eq!(srt.rotation, Quat::IDENTITY); |
| 52 |
assert_eq!(srt.translation, Vec3::ZERO); |
| 53 |
|
| 54 |
assert_eq!(srt, tr.into()); |
| 55 |
|
| 56 |
assert_eq!(TransformRT::IDENTITY, TransformRT::default()); |
| 57 |
assert_eq!(TransformSRT::IDENTITY, TransformSRT::default()); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn test_new() { |
| 62 |
let t = Vec3::new(1.0, 2.0, 3.0); |
| 63 |
let r = Quat::from_rotation_y(90.0_f32.to_radians()); |
| 64 |
let s = Vec3::new(-1.0, -2.0, -3.0); |
| 65 |
|
| 66 |
let tr = TransformRT::from_rotation_translation(r, t); |
| 67 |
assert_eq!(tr.rotation, r); |
| 68 |
assert_eq!(tr.translation, t); |
| 69 |
|
| 70 |
let srt = TransformSRT::from_scale_rotation_translation(s, r, t); |
| 71 |
assert_eq!(srt.scale, s); |
| 72 |
assert_eq!(srt.rotation, r); |
| 73 |
assert_eq!(srt.translation, t); |
| 74 |
|
| 75 |
assert_eq!(tr, tr); |
| 76 |
assert_eq!(srt, srt); |
| 77 |
} |
| 78 |
|
| 79 |
#[test] |
| 80 |
fn test_mul() { |
| 81 |
let tr = TransformRT::from_rotation_translation( |
| 82 |
Quat::from_rotation_z(-90.0_f32.to_radians()), |
| 83 |
Vec3::X, |
| 84 |
); |
| 85 |
let v0 = Vec3A::Y; |
| 86 |
let v1 = tr.transform_point3a(v0); |
| 87 |
assert_approx_eq!(v1, Vec3A::X * 2.0); |
| 88 |
assert_approx_eq!(v1, tr.transform_point3a(v0)); |
| 89 |
let inv_tr = tr.inverse(); |
| 90 |
let v2 = inv_tr.transform_point3a(v1); |
| 91 |
assert_approx_eq!(v0, v2); |
| 92 |
|
| 93 |
assert_eq!(tr * TransformRT::IDENTITY, tr); |
| 94 |
assert_approx_eq!(tr * inv_tr, TransformRT::IDENTITY); |
| 95 |
|
| 96 |
assert_eq!(tr * TransformSRT::IDENTITY, TransformSRT::from(tr)); |
| 97 |
assert_eq!(TransformSRT::IDENTITY * tr, TransformSRT::from(tr)); |
| 98 |
|
| 99 |
let s = Vec3::splat(2.0); |
| 100 |
let r = Quat::from_rotation_y(180.0_f32.to_radians()); |
| 101 |
let t = -Vec3::Y; |
| 102 |
let srt = TransformSRT::from_scale_rotation_translation(s, r, t); |
| 103 |
let v0 = Vec3A::X; |
| 104 |
let v1 = srt.transform_point3a(v0); |
| 105 |
assert_approx_eq!(v1, (r * (v0 * Vec3A::from(s))) + Vec3A::from(t)); |
| 106 |
assert_approx_eq!(v1, srt.transform_point3a(v0)); |
| 107 |
let inv_srt = srt.inverse(); |
| 108 |
let v2 = inv_srt.transform_point3a(v1); |
| 109 |
assert_approx_eq!(v0, v2); |
| 110 |
|
| 111 |
assert_eq!(srt * TransformSRT::IDENTITY, srt); |
| 112 |
assert_eq!(srt * inv_srt, TransformSRT::IDENTITY); |
| 113 |
|
| 114 |
// negative scale mul test |
| 115 |
let s = Vec3::splat(-2.0); |
| 116 |
let srt = TransformSRT::from_scale_rotation_translation(s, r, t); |
| 117 |
let inv_srt = srt.inverse(); |
| 118 |
assert_eq!(srt * inv_srt, TransformSRT::IDENTITY); |
| 119 |
} |
| 120 |
} |