| 1 |
use na::{Matrix3, Matrix4, Point2, Point3, Vector2, Vector3}; |
| 2 |
|
| 3 |
/// See Example 3.4 of "Graphics and Visualization: Principles & Algorithms" |
| 4 |
/// by Theoharis, Papaioannou, Platis, Patrikalakis. |
| 5 |
#[test] |
| 6 |
fn test_scaling_wrt_point_1() { |
| 7 |
let a = Point2::new(0.0, 0.0); |
| 8 |
let b = Point2::new(1.0, 1.0); |
| 9 |
let c = Point2::new(5.0, 2.0); |
| 10 |
|
| 11 |
let scaling = Vector2::new(2.0, 2.0); |
| 12 |
let scale_about = Matrix3::new_nonuniform_scaling_wrt_point(&scaling, &c); |
| 13 |
|
| 14 |
let expected_a = Point2::new(-5.0, -2.0); |
| 15 |
let expected_b = Point2::new(-3.0, 0.0); |
| 16 |
let result_a = scale_about.transform_point(&a); |
| 17 |
let result_b = scale_about.transform_point(&b); |
| 18 |
let result_c = scale_about.transform_point(&c); |
| 19 |
|
| 20 |
assert!(expected_a == result_a); |
| 21 |
assert!(expected_b == result_b); |
| 22 |
assert!(c == result_c); |
| 23 |
} |
| 24 |
|
| 25 |
/// Based on the same example as the test above. |
| 26 |
#[test] |
| 27 |
fn test_scaling_wrt_point_2() { |
| 28 |
let a = Point3::new(0.0, 0.0, 1.0); |
| 29 |
let b = Point3::new(1.0, 1.0, 1.0); |
| 30 |
let c = Point3::new(5.0, 2.0, 1.0); |
| 31 |
|
| 32 |
let scaling = Vector3::new(2.0, 2.0, 1.0); |
| 33 |
let scale_about = Matrix4::new_nonuniform_scaling_wrt_point(&scaling, &c); |
| 34 |
|
| 35 |
let expected_a = Point3::new(-5.0, -2.0, 1.0); |
| 36 |
let expected_b = Point3::new(-3.0, 0.0, 1.0); |
| 37 |
|
| 38 |
let result_a = scale_about.transform_point(&a); |
| 39 |
let result_b = scale_about.transform_point(&b); |
| 40 |
let result_c = scale_about.transform_point(&c); |
| 41 |
|
| 42 |
assert!(expected_a == result_a); |
| 43 |
assert!(expected_b == result_b); |
| 44 |
assert!(c == result_c); |
| 45 |
} |
| 46 |
|
| 47 |
/// Based on https://github.com/emlowry/AiE/blob/50bae4068edb686cf8ffacdf6fab8e7cb22e7eb1/Year%201%20Classwork/MathTest/Matrix4x4TestGroup.cpp#L145 |
| 48 |
#[test] |
| 49 |
fn test_scaling_wrt_point_3() { |
| 50 |
let about = Point3::new(2.0, 1.0, -2.0); |
| 51 |
let scale = Vector3::new(2.0, 0.5, -1.0); |
| 52 |
let pt = Point3::new(1.0, 2.0, 3.0); |
| 53 |
let scale_about = Matrix4::new_nonuniform_scaling_wrt_point(&scale, &about); |
| 54 |
|
| 55 |
let expected = Point3::new(0.0, 1.5, -7.0); |
| 56 |
let result = scale_about.transform_point(&pt); |
| 57 |
|
| 58 |
assert!(result == expected); |
| 59 |
} |