Merge pull request #90 from csherratt/translate

Add a translation builder to Matrix4
This commit is contained in:
Brendan Zabarauskas 2014-06-28 18:32:33 -07:00
commit 091630cf56
2 changed files with 17 additions and 0 deletions

View file

@ -250,6 +250,15 @@ impl<S: BaseNum> Matrix4<S> {
pub fn identity() -> Matrix4<S> { pub fn identity() -> Matrix4<S> {
Matrix4::from_value(one()) Matrix4::from_value(one())
} }
/// Create a translation matrix from a Vector3
#[inline]
pub fn from_translation(v: &Vector3<S>) -> Matrix4<S> {
Matrix4::new(one(), zero(), zero(), zero(),
zero(), one(), zero(), zero(),
zero(), zero(), one(), zero(),
v.x, v.y, v.z, one())
}
} }
impl<S: BaseFloat> impl<S: BaseFloat>

View file

@ -328,6 +328,14 @@ fn test_invert() {
assert!(mat_f.invert().unwrap().mul_m(&mat_f).is_identity()); assert!(mat_f.invert().unwrap().mul_m(&mat_f).is_identity());
} }
#[test]
fn test_from_translation() {
let mat = Matrix4::from_translation(&Vector3::new(1.0f64, 2.0f64, 3.0f64));
let vertex = Vector4::new(0.0f64, 0.0f64, 0.0f64, 1.0f64);
let res = mat.mul_v(&vertex);
assert_eq!(res, Vector4::new(1., 2., 3., 1.));
}
#[test] #[test]
fn test_predicates() { fn test_predicates() {
// Matrix2 // Matrix2