Added to the Mat3 the possibility to be constructed from a translation vector. Added tests.

This commit is contained in:
Andrea Catania 2019-11-05 07:15:33 +01:00
parent f69e781b8f
commit 04b21718f3
2 changed files with 36 additions and 0 deletions

View file

@ -159,6 +159,34 @@ impl<S> Matrix3<S> {
}
impl<S: BaseFloat> Matrix3<S> {
/// Create a homogeneous transformation matrix from a translation vector.
#[inline]
pub fn from_translation(v: Vector2<S>) -> Matrix3<S> {
#[cfg_attr(rustfmt, rustfmt_skip)]
Matrix3::new(
S::one(), S::zero(), S::zero(),
S::zero(), S::one(), S::zero(),
v.x, v.y, S::one(),
)
}
/// Create a homogeneous transformation matrix from a scale value.
#[inline]
pub fn from_scale(value: S) -> Matrix3<S> {
Matrix3::from_nonuniform_scale(value, value)
}
/// Create a homogeneous transformation matrix from a set of scale values.
#[inline]
pub fn from_nonuniform_scale(x: S, y: S) -> Matrix3<S> {
#[cfg_attr(rustfmt, rustfmt_skip)]
Matrix3::new(
x, S::zero(), S::zero(),
S::zero(), y, S::zero(),
S::zero(), S::zero(), S::one(),
)
}
/// Create a rotation matrix that will cause a vector to point at
/// `dir`, using `up` for orientation.
pub fn look_at(dir: Vector3<S>, up: Vector3<S>) -> Matrix3<S> {

View file

@ -358,6 +358,14 @@ pub mod matrix3 {
assert!(Matrix3::from_value(6.0f64).is_diagonal());
}
#[test]
fn test_from_translation() {
let mat = Matrix3::from_translation(Vector2::new(1.0f64, 2.0f64));
let vertex = Vector3::new(0.0f64, 0.0f64, 1.0f64);
let res = mat * vertex;
assert_eq!(res, Vector3::new(1., 2., 1.));
}
mod from_axis_x {
use cgmath::*;