| 1 |
#[macro_use] |
| 2 |
extern crate approx; |
| 3 |
extern crate nalgebra as na; |
| 4 |
|
| 5 |
use na::{Matrix4, Point3, Vector3}; |
| 6 |
use std::f32::consts; |
| 7 |
|
| 8 |
fn main() { |
| 9 |
// Create a uniform scaling matrix with scaling factor 2. |
| 10 |
let mut m = Matrix4::new_scaling(2.0); |
| 11 |
|
| 12 |
assert_eq!(m.transform_vector(&Vector3::x()), Vector3::x() * 2.0); |
| 13 |
assert_eq!(m.transform_vector(&Vector3::y()), Vector3::y() * 2.0); |
| 14 |
assert_eq!(m.transform_vector(&Vector3::z()), Vector3::z() * 2.0); |
| 15 |
|
| 16 |
// Append a nonuniform scaling in-place. |
| 17 |
m.append_nonuniform_scaling_mut(&Vector3::new(1.0, 2.0, 3.0)); |
| 18 |
|
| 19 |
assert_eq!(m.transform_vector(&Vector3::x()), Vector3::x() * 2.0); |
| 20 |
assert_eq!(m.transform_vector(&Vector3::y()), Vector3::y() * 4.0); |
| 21 |
assert_eq!(m.transform_vector(&Vector3::z()), Vector3::z() * 6.0); |
| 22 |
|
| 23 |
// Append a translation out-of-place. |
| 24 |
let m2 = m.append_translation(&Vector3::new(42.0, 0.0, 0.0)); |
| 25 |
|
| 26 |
assert_eq!( |
| 27 |
m2.transform_point(&Point3::new(1.0, 1.0, 1.0)), |
| 28 |
Point3::new(42.0 + 2.0, 4.0, 6.0) |
| 29 |
); |
| 30 |
|
| 31 |
// Create rotation. |
| 32 |
let rot = Matrix4::from_scaled_axis(Vector3::x() * consts::PI); |
| 33 |
let rot_then_m = m * rot; // Right-multiplication is equivalent to prepending `rot` to `m`. |
| 34 |
let m_then_rot = rot * m; // Left-multiplication is equivalent to appending `rot` to `m`. |
| 35 |
|
| 36 |
let pt = Point3::new(1.0, 2.0, 3.0); |
| 37 |
|
| 38 |
assert_relative_eq!( |
| 39 |
m.transform_point(&rot.transform_point(&pt)), |
| 40 |
rot_then_m.transform_point(&pt) |
| 41 |
); |
| 42 |
assert_relative_eq!( |
| 43 |
rot.transform_point(&m.transform_point(&pt)), |
| 44 |
m_then_rot.transform_point(&pt) |
| 45 |
); |
| 46 |
} |