Add more matrix4 and transform tests

This commit is contained in:
Aaron Loucks 2020-06-26 23:13:58 -04:00 committed by Dzmitry Malyshau
parent 213fc580a9
commit 24a9191189
2 changed files with 47 additions and 0 deletions

View file

@ -1127,6 +1127,48 @@ pub mod matrix4 {
);
}
#[test]
fn test_look_to_rh() {
let eye = Point3::new(10.0, 15.0, 20.0);
let dir = Vector3::new(1.0, 2.0, 3.0).normalize();
let up = Vector3::unit_y();
let m = Matrix4::look_to_rh(eye, dir, up);
#[allow(deprecated)]
assert_ulps_eq!(m, Matrix4::look_at_dir(eye, dir, up));
let expected = Matrix4::from([
[-0.9486833, -0.16903086, -0.26726127, 0.0],
[0.0, 0.84515435, -0.53452253, 0.0],
[0.31622776, -0.5070926, -0.8017838, 0.0],
[3.1622782, -0.84515476, 26.726126, 1.0_f32]
]);
assert_ulps_eq!(expected, m);
let m = Matrix4::look_at_rh(eye, eye + dir, up);
assert_abs_diff_eq!(expected, m, epsilon = 1.0e-4);
}
#[test]
fn test_look_to_lh() {
let eye = Point3::new(10.0, 15.0, 20.0);
let dir = Vector3::new(1.0, 2.0, 3.0).normalize();
let up = Vector3::unit_y();
let m = Matrix4::look_to_lh(eye, dir, up);
let expected = Matrix4::from([
[0.9486833, -0.16903086, 0.26726127, 0.0],
[0.0, 0.84515435, 0.53452253, 0.0],
[-0.31622776, -0.5070926, 0.8017838, 0.0],
[-3.1622782, -0.84515476, -26.726126, 1.0_f32]
]);
assert_ulps_eq!(expected, m);
let m = Matrix4::look_at_lh(eye, eye + dir, up);
assert_abs_diff_eq!(expected, m, epsilon = 1.0e-4);
}
mod from {
use cgmath::*;

View file

@ -94,6 +94,7 @@ fn test_look_at() {
let center = Point3::new(0.0f64, 0.0, 0.0);
let up = Vector3::new(1.0f64, 0.0, 0.0);
let t: Decomposed<Vector3<f64>, Quaternion<f64>> = Transform::look_at(eye, center, up);
assert_ulps_eq!(t, Decomposed::<Vector3<f64>, Quaternion<f64>>::look_at(eye, center, up));
let point = Point3::new(1.0f64, 0.0, 0.0);
let view_point = Point3::new(0.0f64, 1.0, 5.0);
assert_ulps_eq!(&t.transform_point(point), &view_point);
@ -105,12 +106,14 @@ fn test_look_at_lh() {
let center = Point3::new(0.0f64, 0.0, 0.0);
let up = Vector3::new(1.0f64, 0.0, 0.0);
let t: Decomposed<Vector3<f64>, Quaternion<f64>> = Transform::look_at_lh(eye, center, up);
assert_ulps_eq!(t, Decomposed::<Vector3<f64>, Quaternion<f64>>::look_at_lh(eye, center, up));
let point = Point3::new(1.0f64, 0.0, 0.0);
let view_point = Point3::new(0.0f64, 1.0, 5.0);
assert_ulps_eq!(&t.transform_point(point), &view_point);
// Decomposed::look_at_lh and Matrix4::look_at_lh should be consistent
let t: Matrix4<f64> = Transform::look_at_lh(eye, center, up);
assert_ulps_eq!(t, Matrix4::<f64>::look_at_lh(eye, center, up));
assert_ulps_eq!(&t.transform_point(point), &view_point);
// Decomposed::look_at is inconsistent and deprecated, but verify that the behvaior
@ -126,12 +129,14 @@ fn test_look_at_rh() {
let center = Point3::new(0.0f64, 0.0, 0.0);
let up = Vector3::new(1.0f64, 0.0, 0.0);
let t: Decomposed<Vector3<f64>, Quaternion<f64>> = Transform::look_at_rh(eye, center, up);
assert_ulps_eq!(t, Decomposed::<Vector3<f64>, Quaternion<f64>>::look_at_rh(eye, center, up));
let point = Point3::new(1.0f64, 0.0, 0.0);
let view_point = Point3::new(0.0f64, 1.0, -5.0);
assert_ulps_eq!(&t.transform_point(point), &view_point);
// Decomposed::look_at_rh and Matrix4::look_at_rh should be consistent
let t: Matrix4<f64> = Transform::look_at_rh(eye, center, up);
assert_ulps_eq!(t, Matrix4::<f64>::look_at_rh(eye, center, up));
assert_ulps_eq!(&t.transform_point(point), &view_point);
}