#![deny(missing_docs)]
extern crate float;
use std::ops::{ Add, Mul, Neg, Sub, Div };
pub mod traits;
pub type Vector2<T> = [T; 2];
pub type Vector3<T> = [T; 3];
pub type Vector4<T> = [T; 4];
pub type Matrix2x3<T> = [[T; 3]; 2];
pub type Matrix3x2<T> = [[T; 2]; 3];
pub type Matrix3<T> = [[T; 3]; 3];
pub type Matrix3x4<T> = [[T; 4]; 3];
pub type Matrix4x3<T> = [[T; 3]; 4];
pub type Matrix4<T> = [[T; 4]; 4];
#[inline(always)]
pub fn col_mat3x2_mul_col<T>(
a: Matrix3x2<T>,
b: Matrix3x2<T>,
i: usize
) -> Vector2<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot_vec2(col_mat3x2_row(a, 0), b[i]),
vec3_dot_vec2(col_mat3x2_row(a, 1), b[i])
]
}
#[inline(always)]
pub fn col_mat3_mul_col<T>(
a: Matrix3<T>,
b: Matrix3<T>,
i: usize
) -> Vector3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot(col_mat3_row(a, 0), b[i]),
vec3_dot(col_mat3_row(a, 1), b[i]),
vec3_dot(col_mat3_row(a, 2), b[i])
]
}
#[inline(always)]
pub fn col_mat4x3_mul_col<T>(
a: Matrix4x3<T>,
b: Matrix4x3<T>,
i: usize
) -> Vector3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec4_dot_vec3(col_mat4x3_row(a, 0), b[i]),
vec4_dot_vec3(col_mat4x3_row(a, 1), b[i]),
vec4_dot_vec3(col_mat4x3_row(a, 2), b[i])
]
}
#[inline(always)]
pub fn col_mat4_mul_col<T>(
a: Matrix4<T>,
b: Matrix4<T>,
i: usize
) -> Vector4<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec4_dot(col_mat4_row(a, 0), b[i]),
vec4_dot(col_mat4_row(a, 1), b[i]),
vec4_dot(col_mat4_row(a, 2), b[i]),
vec4_dot(col_mat4_row(a, 3), b[i])
]
}
#[inline(always)]
pub fn row_mat2x3_mul_row<T>(
a: Matrix2x3<T>,
b: Matrix2x3<T>,
i: usize
) -> Vector3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot_vec2(a[i], row_mat2x3_col(b, 0)),
vec3_dot_vec2(a[i], row_mat2x3_col(b, 1)),
vec3_dot_pos2(a[i], row_mat2x3_col(b, 2))
]
}
#[inline(always)]
pub fn row_mat3_mul_row<T>(
a: Matrix3<T>,
b: Matrix3<T>,
i: usize
) -> Vector3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot(a[i], row_mat3_col(b, 0)),
vec3_dot(a[i], row_mat3_col(b, 1)),
vec3_dot(a[i], row_mat3_col(b, 2)),
]
}
#[inline(always)]
pub fn row_mat3x4_mul_row<T>(
a: Matrix3x4<T>,
b: Matrix3x4<T>,
i: usize
) -> Vector4<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec4_dot_vec3(a[i], row_mat3x4_col(b, 0)),
vec4_dot_vec3(a[i], row_mat3x4_col(b, 1)),
vec4_dot_vec3(a[i], row_mat3x4_col(b, 2)),
vec4_dot_pos3(a[i], row_mat3x4_col(b, 3))
]
}
#[inline(always)]
pub fn row_mat4_mul_row<T>(
a: Matrix4<T>,
b: Matrix4<T>,
i: usize
) -> Vector4<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec4_dot(a[i], row_mat4_col(b, 0)),
vec4_dot(a[i], row_mat4_col(b, 1)),
vec4_dot(a[i], row_mat4_col(b, 2)),
vec4_dot(a[i], row_mat4_col(b, 3))
]
}
#[inline(always)]
pub fn col_mat3x2_mul<T>(
a: Matrix3x2<T>,
b: Matrix3x2<T>
) -> Matrix3x2<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
col_mat3x2_mul_col(a, b, 0),
col_mat3x2_mul_col(a, b, 1),
col_mat3x2_mul_col(a, b, 2)
]
}
#[inline(always)]
pub fn col_mat3_mul<T>(
a: Matrix3<T>,
b: Matrix3<T>
) -> Matrix3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
col_mat3_mul_col(a, b, 0),
col_mat3_mul_col(a, b, 1),
col_mat3_mul_col(a, b, 2)
]
}
#[inline(always)]
pub fn col_mat4x3_mul<T>(
a: Matrix4x3<T>,
b: Matrix4x3<T>
) -> Matrix4x3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
col_mat4x3_mul_col(a, b, 0),
col_mat4x3_mul_col(a, b, 1),
col_mat4x3_mul_col(a, b, 2),
col_mat4x3_mul_col(a, b, 3)
]
}
#[inline(always)]
pub fn col_mat4_mul<T>(
a: Matrix4<T>,
b: Matrix4<T>
) -> Matrix4<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
col_mat4_mul_col(a, b, 0),
col_mat4_mul_col(a, b, 1),
col_mat4_mul_col(a, b, 2),
col_mat4_mul_col(a, b, 3)
]
}
#[inline(always)]
pub fn row_mat2x3_mul<T>(
a: Matrix2x3<T>,
b: Matrix2x3<T>
) -> Matrix2x3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
row_mat2x3_mul_row(a, b, 0),
row_mat2x3_mul_row(a, b, 1),
]
}
#[inline(always)]
pub fn row_mat3_mul<T>(
a: Matrix3<T>,
b: Matrix3<T>
) -> Matrix3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
row_mat3_mul_row(a, b, 0),
row_mat3_mul_row(a, b, 1),
row_mat3_mul_row(a, b, 2)
]
}
#[inline(always)]
pub fn row_mat3x4_mul<T>(
a: Matrix3x4<T>,
b: Matrix3x4<T>
) -> Matrix3x4<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
row_mat3x4_mul_row(a, b, 0),
row_mat3x4_mul_row(a, b, 1),
row_mat3x4_mul_row(a, b, 2)
]
}
#[inline(always)]
pub fn row_mat4_mul<T>(
a: Matrix4<T>,
b: Matrix4<T>
) -> Matrix4<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
row_mat4_mul_row(a, b, 0),
row_mat4_mul_row(a, b, 1),
row_mat4_mul_row(a, b, 2),
row_mat4_mul_row(a, b, 3)
]
}
#[inline(always)]
pub fn mat2x3_id<T>() -> Matrix2x3<T>
where T: Copy + traits::One + traits::Zero
{
let one = T::one();
let zero = T::zero();
[
[one, zero, zero],
[zero, one, zero]
]
}
#[inline(always)]
pub fn mat3x2_id<T>() -> Matrix3x2<T>
where T: Copy + traits::One + traits::Zero
{
let one = T::one();
let zero = T::zero();
[
[one, zero],
[zero, one],
[zero, zero]
]
}
#[inline(always)]
pub fn mat3_id<T>() -> Matrix3<T>
where T: Copy + traits::One + traits::Zero
{
let one = T::one();
let zero = T::zero();
[
[one, zero, zero],
[zero, one, zero],
[zero, zero, one]
]
}
#[inline(always)]
pub fn mat3x4_id<T>() -> Matrix3x4<T>
where T: Copy + traits::One + traits::Zero
{
let one = T::one();
let zero = T::zero();
[
[one, zero, zero, zero],
[zero, one, zero, zero],
[zero, zero, one, zero]
]
}
#[inline(always)]
pub fn mat4x3_id<T>() -> Matrix4x3<T>
where T: Copy + traits::One + traits::Zero
{
let one = T::one();
let zero = T::zero();
[
[one, zero, zero],
[zero, one, zero],
[zero, zero, one],
[zero, zero, zero]
]
}
#[inline(always)]
pub fn mat4_id<T>() -> Matrix4<T>
where T: Copy + traits::One + traits::Zero
{
let one = T::one();
let zero = T::zero();
[
[one, zero, zero, zero],
[zero, one, zero, zero],
[zero, zero, one, zero],
[zero, zero, zero, one]
]
}
#[inline(always)]
pub fn vec2_cast<T, U>(a: Vector2<T>) -> Vector2<U>
where T: Copy + traits::Cast<U>
{
[
a[0].cast(),
a[1].cast()
]
}
#[inline(always)]
pub fn vec3_cast<T, U>(a: Vector3<T>) -> Vector3<U>
where T: Copy + traits::Cast<U>
{
[
a[0].cast(),
a[1].cast(),
a[2].cast()
]
}
#[inline(always)]
pub fn vec4_cast<T, U>(a: Vector4<T>) -> Vector4<U>
where T: Copy + traits::Cast<U>
{
[
a[0].cast(),
a[1].cast(),
a[2].cast(),
a[3].cast()
]
}
#[inline(always)]
pub fn mat2x3_cast<T, U>(mat: Matrix2x3<T>) -> Matrix2x3<U>
where T: Copy + traits::Cast<U>
{
[
vec3_cast(mat[0]),
vec3_cast(mat[1])
]
}
#[inline(always)]
pub fn mat3x2_cast<T, U>(mat: Matrix3x2<T>) -> Matrix3x2<U>
where T: Copy + traits::Cast<U>
{
[
vec2_cast(mat[0]),
vec2_cast(mat[1]),
vec2_cast(mat[2])
]
}
#[inline(always)]
pub fn mat3_cast<T, U>(mat: Matrix3<T>) -> Matrix3<U>
where T: Copy + traits::Cast<U>
{
[
vec3_cast(mat[0]),
vec3_cast(mat[1]),
vec3_cast(mat[2])
]
}
#[inline(always)]
pub fn mat3x4_cast<T, U>(m: Matrix3x4<T>) -> Matrix3x4<U>
where T: Copy + traits::Cast<U>
{
[
vec4_cast(m[0]),
vec4_cast(m[1]),
vec4_cast(m[2])
]
}
#[inline(always)]
pub fn mat4x3_cast<T, U>(m: Matrix4x3<T>) -> Matrix4x3<U>
where T: Copy + traits::Cast<U>
{
[
vec3_cast(m[0]),
vec3_cast(m[1]),
vec3_cast(m[2]),
vec3_cast(m[3])
]
}
#[inline(always)]
pub fn mat4_cast<T, U>(m: Matrix4<T>) -> Matrix4<U>
where T: Copy + traits::Cast<U>
{
[
vec4_cast(m[0]),
vec4_cast(m[1]),
vec4_cast(m[2]),
vec4_cast(m[3])
]
}
#[inline(always)]
pub fn vec2_sub<T>(a: Vector2<T>, b: Vector2<T>) -> Vector2<T>
where T: Copy + Sub<T, Output = T>
{
[
a[0] - b[0],
a[1] - b[1],
]
}
#[inline(always)]
pub fn vec3_sub<T>(a: Vector3<T>, b: Vector3<T>) -> Vector3<T>
where T: Copy + Sub<T, Output = T>
{
[
a[0] - b[0],
a[1] - b[1],
a[2] - b[2],
]
}
#[inline(always)]
pub fn vec4_sub<T>(a: Vector4<T>, b: Vector4<T>) -> Vector4<T>
where T: Copy + Sub<T, Output = T>
{
[
a[0] - b[0],
a[1] - b[1],
a[2] - b[2],
a[3] - b[3]
]
}
#[inline(always)]
pub fn mat2x3_sub<T>(a: Matrix2x3<T>, b: Matrix2x3<T>) -> Matrix2x3<T>
where T: Copy + Sub<T, Output = T>
{
[
vec3_sub(a[0], b[0]),
vec3_sub(a[1], b[1])
]
}
#[inline(always)]
pub fn mat3x2_sub<T>(a: Matrix3x2<T>, b: Matrix3x2<T>) -> Matrix3x2<T>
where T: Copy + Sub<T, Output = T>
{
[
vec2_sub(a[0], b[0]),
vec2_sub(a[1], b[1]),
vec2_sub(a[2], b[2])
]
}
#[inline(always)]
pub fn mat3_sub<T>(a: Matrix3<T>, b: Matrix3<T>) -> Matrix3<T>
where T: Copy + Sub<T, Output = T>
{
[
vec3_sub(a[0], b[0]),
vec3_sub(a[1], b[1]),
vec3_sub(a[2], b[2])
]
}
#[inline(always)]
pub fn mat3x4_sub<T>(a: Matrix3x4<T>, b: Matrix3x4<T>) -> Matrix3x4<T>
where T: Copy + Sub<T, Output = T>
{
[
vec4_sub(a[0], b[0]),
vec4_sub(a[1], b[1]),
vec4_sub(a[2], b[2])
]
}
#[inline(always)]
pub fn mat4x3_sub<T>(a: Matrix4x3<T>, b: Matrix4x3<T>) -> Matrix4x3<T>
where T: Copy + Sub<T, Output = T>
{
[
vec3_sub(a[0], b[0]),
vec3_sub(a[1], b[1]),
vec3_sub(a[2], b[2]),
vec3_sub(a[3], b[3])
]
}
#[inline(always)]
pub fn mat4_sub<T>(a: Matrix4<T>, b: Matrix4<T>) -> Matrix4<T>
where T: Copy + Sub<T, Output = T>
{
[
vec4_sub(a[0], b[0]),
vec4_sub(a[1], b[1]),
vec4_sub(a[2], b[2]),
vec4_sub(a[3], b[3])
]
}
#[inline(always)]
pub fn vec2_add<T>(a: Vector2<T>, b: Vector2<T>) -> Vector2<T>
where T: Copy + Add<T, Output = T>
{
[
a[0] + b[0],
a[1] + b[1],
]
}
#[inline(always)]
pub fn vec3_add<T>(a: Vector3<T>, b: Vector3<T>) -> Vector3<T>
where T: Copy + Add<T, Output = T>
{
[
a[0] + b[0],
a[1] + b[1],
a[2] + b[2]
]
}
#[inline(always)]
pub fn vec4_add<T>(a: Vector4<T>, b: Vector4<T>) -> Vector4<T>
where T: Copy + Add<T, Output = T>
{
[
a[0] + b[0],
a[1] + b[1],
a[2] + b[2],
a[3] + b[3]
]
}
#[inline(always)]
pub fn mat2x3_add<T>(a: Matrix2x3<T>, b: Matrix2x3<T>) -> Matrix2x3<T>
where T: Copy + Add<T, Output = T>
{
[
vec3_add(a[0], b[0]),
vec3_add(a[1], b[1])
]
}
#[inline(always)]
pub fn mat3x2_add<T>(a: Matrix3x2<T>, b: Matrix3x2<T>) -> Matrix3x2<T>
where T: Copy + Add<T, Output = T>
{
[
vec2_add(a[0], b[0]),
vec2_add(a[1], b[1]),
vec2_add(a[2], b[2])
]
}
#[inline(always)]
pub fn mat3_add<T>(a: Matrix3<T>, b: Matrix3<T>) -> Matrix3<T>
where T: Copy + Add<T, Output = T>
{
[
vec3_add(a[0], b[0]),
vec3_add(a[1], b[1]),
vec3_add(a[2], b[2])
]
}
#[inline(always)]
pub fn mat3x4_add<T>(a: Matrix3x4<T>, b: Matrix3x4<T>) -> Matrix3x4<T>
where T: Copy + Add<T, Output = T>
{
[
vec4_add(a[0], b[0]),
vec4_add(a[1], b[1]),
vec4_add(a[2], b[2])
]
}
#[inline(always)]
pub fn mat4x3_add<T>(a: Matrix4x3<T>, b: Matrix4x3<T>) -> Matrix4x3<T>
where T: Copy + Add<T, Output = T>
{
[
vec3_add(a[0], b[0]),
vec3_add(a[1], b[1]),
vec3_add(a[2], b[2]),
vec3_add(a[3], b[3])
]
}
#[inline(always)]
pub fn mat4_add<T>(a: Matrix4<T>, b: Matrix4<T>) -> Matrix4<T>
where T: Copy + Add<T, Output = T>
{
[
vec4_add(a[0], b[0]),
vec4_add(a[1], b[1]),
vec4_add(a[2], b[2]),
vec4_add(a[3], b[3])
]
}
#[inline(always)]
pub fn vec2_mul<T>(a: Vector2<T>, b: Vector2<T>) -> Vector2<T>
where T: Copy + Mul<T, Output = T>
{
[a[0] * b[0], a[1] * b[1]]
}
#[inline(always)]
pub fn vec3_mul<T>(a: Vector3<T>, b: Vector3<T>) -> Vector3<T>
where T: Copy + Mul<T, Output = T>
{
[a[0] * b[0], a[1] * b[1], a[2] * b[2]]
}
#[inline(always)]
pub fn vec4_mul<T>(a: Vector4<T>, b: Vector4<T>) -> Vector4<T>
where T: Copy + Mul<T, Output = T>
{
[a[0] * b[0], a[1] * b[1], a[2] * b[2], a[3] * b[3]]
}
#[inline(always)]
pub fn vec2_dot<T>(a: Vector2<T>, b: Vector2<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
a[0] * b[0] + a[1] * b[1]
}
#[inline(always)]
pub fn vec3_dot<T>(a: Vector3<T>, b: Vector3<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
#[inline(always)]
pub fn vec4_dot<T>(a: Vector4<T>, b: Vector4<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]
}
#[inline(always)]
pub fn vec2_square_len<T>(a: Vector2<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
a[0] * a[0] + a[1] * a[1]
}
#[inline(always)]
pub fn vec3_square_len<T>(a: Vector3<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
a[0] * a[0] + a[1] * a[1] + a[2] * a[2]
}
#[inline(always)]
pub fn vec4_square_len<T>(a: Vector4<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3]
}
#[inline(always)]
pub fn vec2_cross<T>(a: Vector2<T>, b: Vector2<T>) -> T
where T: Copy + Mul<T, Output = T> + Sub<T, Output = T>
{
a[0] * b[1] - a[1] * b[0]
}
#[inline(always)]
pub fn vec3_cross<T>(a: Vector3<T>, b: Vector3<T>) -> Vector3<T>
where T: Copy + Mul<T, Output = T> + Sub<T, Output = T>
{
[
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0]
]
}
#[inline(always)]
pub fn vec2_scale<T>(a: Vector2<T>, b: T) -> Vector2<T>
where T: Copy + Mul<T, Output = T>
{
[
a[0] * b,
a[1] * b
]
}
#[inline(always)]
pub fn vec3_scale<T>(a: Vector3<T>, b: T) -> Vector3<T>
where T: Copy + Mul<T, Output = T>
{
[
a[0] * b,
a[1] * b,
a[2] * b
]
}
#[inline(always)]
pub fn vec4_scale<T>(a: Vector4<T>, b: T) -> Vector4<T>
where T: Copy + Mul<T, Output = T>
{
[
a[0] * b,
a[1] * b,
a[2] * b,
a[3] * b
]
}
#[inline(always)]
pub fn vec2_neg<T>(a: Vector2<T>) -> Vector2<T>
where T: Copy + Neg<Output = T>
{
[-a[0], -a[1]]
}
#[inline(always)]
pub fn vec3_neg<T>(a: Vector3<T>) -> Vector3<T>
where T: Copy + Neg<Output = T>
{
[-a[0], -a[1], -a[2]]
}
#[inline(always)]
pub fn vec4_neg<T>(a: Vector4<T>) -> Vector4<T>
where T: Copy + Neg<Output = T>
{
[-a[0], -a[1], -a[2], -a[3]]
}
#[inline(always)]
pub fn vec2_len<T>(a: Vector2<T>) -> T
where T: Copy + traits::Sqrt + Add<T, Output = T> + Mul<T, Output = T>
{
vec2_square_len(a).sqrt()
}
#[inline(always)]
pub fn vec3_len<T>(a: Vector3<T>) -> T
where T: Copy + traits::Sqrt + Add<T, Output = T> + Mul<T, Output = T>
{
vec3_square_len(a).sqrt()
}
#[inline(always)]
pub fn vec4_len<T>(a: Vector4<T>) -> T
where T: Copy + traits::Sqrt + Add<T, Output = T> + Mul<T, Output = T>
{
vec4_square_len(a).sqrt()
}
#[inline(always)]
pub fn vec2_inv_len<T>(a: Vector2<T>) -> T
where T: Copy + traits::One + traits::Sqrt + Add<T, Output = T>
+ Mul<T, Output = T> + Div<T, Output = T>
{
let one = T::one();
one / vec2_len(a)
}
#[inline(always)]
pub fn vec3_inv_len<T>(a: Vector3<T>) -> T
where T: Copy + traits::One + traits::Sqrt + Add<T, Output = T>
+ Mul<T, Output = T> + Div<T, Output = T>
{
let one = T::one();
one / vec3_len(a)
}
#[inline(always)]
pub fn vec4_inv_len<T>(a: Vector4<T>) -> T
where T: Copy + traits::One + traits::Sqrt + Add<T, Output = T>
+ Mul<T, Output = T> + Div<T, Output = T>
{
let one = T::one();
one / vec4_len(a)
}
#[inline(always)]
pub fn vec2_normalized<T>(a: Vector2<T>) -> Vector2<T>
where T: Copy + traits::One + traits::Sqrt + Add<T, Output = T>
+ Mul<T, Output = T> + Div<T, Output = T>
{
vec2_scale(a, vec2_inv_len(a))
}
#[inline(always)]
pub fn vec3_normalized<T>(a: Vector3<T>) -> Vector3<T>
where T: Copy + traits::One + traits::Sqrt + Add<T, Output = T>
+ Mul<T, Output = T> + Div<T, Output = T>
{
vec3_scale(a, vec3_inv_len(a))
}
#[inline(always)]
pub fn vec4_normalized<T>(a: Vector4<T>) -> Vector4<T>
where T: Copy + traits::One + traits::Sqrt + Add<T, Output = T>
+ Mul<T, Output = T> + Div<T, Output = T>
{
vec4_scale(a, vec4_inv_len(a))
}
#[inline(always)]
pub fn vec2_normalized_sub<T>(
a: Vector2<T>,
b: Vector2<T>
) -> Vector2<T>
where T: Copy + traits::One + traits::Sqrt + Add<T, Output = T>
+ Mul<T, Output = T> + Div<T, Output = T> + Sub<T, Output = T>
{
vec2_normalized(vec2_sub(a, b))
}
#[inline(always)]
pub fn vec3_normalized_sub<T>(
a: Vector3<T>,
b: Vector3<T>
) -> Vector3<T>
where T: Copy + traits::One + traits::Sqrt + Add<T, Output = T>
+ Mul<T, Output = T> + Sub<T, Output = T> + Div<T, Output = T>
{
vec3_normalized(vec3_sub(a, b))
}
#[inline(always)]
pub fn vec4_normalized_sub<T>(
a: Vector4<T>,
b: Vector4<T>
) -> Vector4<T>
where T: Copy + traits::One + traits::Sqrt + Add<T, Output = T>
+ Mul<T, Output = T> + Sub<T, Output = T> + Div<T, Output = T>
{
vec4_normalized(vec4_sub(a, b))
}
#[inline(always)]
pub fn vec3_dot_vec2<T>(a: Vector3<T>, b: Vector2<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
a[0] * b[0] + a[1] * b[1]
}
#[inline(always)]
pub fn vec4_dot_vec3<T>(a: Vector4<T>, b: Vector3<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
#[inline(always)]
pub fn vec3_dot_pos2<T>(a: Vector3<T>, b: Vector2<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
vec3_dot_vec2(a, b) + a[2]
}
#[inline(always)]
pub fn vec4_dot_pos3<T>(a: Vector4<T>, b: Vector3<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
vec4_dot_vec3(a, b) + a[3]
}
#[inline(always)]
pub fn row_mat2x3_col<T: Copy>(mat: Matrix2x3<T>, i: usize) -> Vector2<T> {
[mat[0][i], mat[1][i]]
}
#[inline(always)]
pub fn col_mat2x3_row<T: Copy>(mat: Matrix2x3<T>, i: usize) -> Vector2<T> {
row_mat2x3_col(mat, i)
}
#[inline(always)]
pub fn row_mat3x2_col<T: Copy>(a: Matrix3x2<T>, i: usize) -> Vector3<T> {
[a[0][i], a[1][i], a[2][i]]
}
#[inline(always)]
pub fn col_mat3x2_row<T: Copy>(a: Matrix3x2<T>, i: usize) -> Vector3<T> {
row_mat3x2_col(a, i)
}
#[inline(always)]
pub fn row_mat3_col<T: Copy>(a: Matrix3<T>, i: usize) -> Vector3<T> {
[a[0][i], a[1][i], a[2][i]]
}
#[inline(always)]
pub fn col_mat3_row<T: Copy>(a: Matrix3<T>, i: usize) -> Vector3<T> {
row_mat3_col(a, i)
}
#[inline(always)]
pub fn row_mat3x4_col<T: Copy>(mat: Matrix3x4<T>, i: usize) -> Vector3<T> {
[mat[0][i], mat[1][i], mat[2][i]]
}
#[inline(always)]
pub fn col_mat3x4_row<T: Copy>(mat: Matrix3x4<T>, i: usize) -> Vector3<T> {
row_mat3x4_col(mat, i)
}
#[inline(always)]
pub fn row_mat4x3_col<T: Copy>(a: Matrix4x3<T>, i: usize) -> Vector4<T> {
[a[0][i], a[1][i], a[2][i], a[3][i]]
}
#[inline(always)]
pub fn col_mat4x3_row<T: Copy>(a: Matrix4x3<T>, i: usize) -> Vector4<T> {
row_mat4x3_col(a, i)
}
#[inline(always)]
pub fn row_mat4_col<T: Copy>(a: Matrix4<T>, i: usize) -> Vector4<T> {
[a[0][i], a[1][i], a[2][i], a[3][i]]
}
#[inline(always)]
pub fn col_mat4_row<T: Copy>(a: Matrix4<T>, i: usize) -> Vector4<T> {
row_mat4_col(a, i)
}
#[inline(always)]
pub fn mat2x3_transposed<T: Copy>(a: Matrix2x3<T>) -> Matrix3x2<T> {
[
row_mat2x3_col(a, 0),
row_mat2x3_col(a, 1),
row_mat2x3_col(a, 2)
]
}
#[inline(always)]
pub fn mat3x2_transposed<T: Copy>(a: Matrix3x2<T>) -> Matrix2x3<T> {
[
row_mat3x2_col(a, 0),
row_mat3x2_col(a, 1)
]
}
#[inline(always)]
pub fn mat3_transposed<T: Copy>(a: Matrix3<T>) -> Matrix3<T> {
[
row_mat3_col(a, 0),
row_mat3_col(a, 1),
row_mat3_col(a, 2)
]
}
#[inline(always)]
pub fn mat3x4_transposed<T: Copy>(a: Matrix3x4<T>) -> Matrix4x3<T> {
[
row_mat3x4_col(a, 0),
row_mat3x4_col(a, 1),
row_mat3x4_col(a, 2),
row_mat3x4_col(a, 3)
]
}
#[inline(always)]
pub fn mat4x3_transposed<T: Copy>(a: Matrix4x3<T>) -> Matrix3x4<T> {
[
row_mat4x3_col(a, 0),
row_mat4x3_col(a, 1),
row_mat4x3_col(a, 2)
]
}
#[inline(always)]
pub fn mat4_transposed<T: Copy>(a: Matrix4<T>) -> Matrix4<T> {
[
row_mat4_col(a, 0),
row_mat4_col(a, 1),
row_mat4_col(a, 2),
row_mat4_col(a, 3)
]
}
#[inline(always)]
pub fn col_mat3_transform<T>(
mat: Matrix3<T>,
a: Vector3<T>
) -> Vector3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot(col_mat3_row(mat, 0), a),
vec3_dot(col_mat3_row(mat, 1), a),
vec3_dot(col_mat3_row(mat, 2), a)
]
}
#[inline(always)]
pub fn col_mat4_transform<T>(
mat: Matrix4<T>,
a: Vector4<T>
) -> Vector4<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec4_dot(col_mat4_row(mat, 0), a),
vec4_dot(col_mat4_row(mat, 1), a),
vec4_dot(col_mat4_row(mat, 2), a),
vec4_dot(col_mat4_row(mat, 3), a)
]
}
#[inline(always)]
pub fn row_mat3_transform<T>(
mat: Matrix3<T>,
a: Vector3<T>
) -> Vector3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot(mat[0], a),
vec3_dot(mat[1], a),
vec3_dot(mat[2], a)
]
}
#[inline(always)]
pub fn row_mat4_transform<T>(
mat: Matrix4<T>,
a: Vector4<T>
) -> Vector4<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec4_dot(mat[0], a),
vec4_dot(mat[1], a),
vec4_dot(mat[2], a),
vec4_dot(mat[3], a)
]
}
#[inline(always)]
pub fn row_mat2x3_transform_pos2<T>(
mat: Matrix2x3<T>,
a: Vector2<T>
) -> Vector2<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot_pos2(mat[0], a),
vec3_dot_pos2(mat[1], a)
]
}
#[inline(always)]
pub fn col_mat3x2_transform_pos2<T>(
mat: Matrix3x2<T>,
a: Vector2<T>
) -> Vector2<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot_pos2(col_mat3x2_row(mat, 0), a),
vec3_dot_pos2(col_mat3x2_row(mat, 1), a)
]
}
#[inline(always)]
pub fn row_mat3_transform_pos2<T>(
mat: Matrix3<T>,
a: Vector2<T>
) -> Vector2<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot_pos2(mat[0], a),
vec3_dot_pos2(mat[1], a)
]
}
#[inline(always)]
pub fn col_mat3_transform_pos2<T>(
mat: Matrix3<T>,
a: Vector2<T>
) -> Vector2<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot_pos2(col_mat3_row(mat, 0), a),
vec3_dot_pos2(col_mat3_row(mat, 1), a)
]
}
#[inline(always)]
pub fn row_mat3x4_transform_pos3<T>(
mat: Matrix3x4<T>,
a: Vector3<T>
) -> Vector3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec4_dot_pos3(mat[0], a),
vec4_dot_pos3(mat[1], a),
vec4_dot_pos3(mat[2], a),
]
}
#[inline(always)]
pub fn col_mat4x3_transform_pos3<T>(
mat: Matrix4x3<T>,
a: Vector3<T>
) -> Vector3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec4_dot_pos3(col_mat4x3_row(mat, 0), a),
vec4_dot_pos3(col_mat4x3_row(mat, 1), a),
vec4_dot_pos3(col_mat4x3_row(mat, 2), a)
]
}
#[inline(always)]
pub fn row_mat2x3_transform_vec2<T>(
mat: Matrix2x3<T>,
a: Vector2<T>
) -> Vector2<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot_vec2(mat[0], a),
vec3_dot_vec2(mat[1], a)
]
}
#[inline(always)]
pub fn col_mat3x2_transform_vec2<T>(
mat: Matrix3x2<T>,
a: Vector2<T>
) -> Vector2<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot_vec2(col_mat3x2_row(mat, 0), a),
vec3_dot_vec2(col_mat3x2_row(mat, 1), a)
]
}
#[inline(always)]
pub fn row_mat3_transform_vec2<T>(
mat: Matrix3<T>,
a: Vector2<T>
) -> Vector2<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot_vec2(mat[0], a),
vec3_dot_vec2(mat[1], a)
]
}
#[inline(always)]
pub fn col_mat3_transform_vec2<T>(
mat: Matrix3<T>,
a: Vector2<T>
) -> Vector2<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec3_dot_vec2(col_mat3_row(mat, 0), a),
vec3_dot_vec2(col_mat3_row(mat, 1), a)
]
}
#[inline(always)]
pub fn row_mat3x4_transform_vec3<T>(
mat: Matrix3x4<T>,
a: Vector3<T>
) -> Vector3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec4_dot_vec3(mat[0], a),
vec4_dot_vec3(mat[1], a),
vec4_dot_vec3(mat[2], a)
]
}
#[inline(always)]
pub fn col_mat4x3_transform_vec3<T>(
mat: Matrix4x3<T>,
a: Vector3<T>
) -> Vector3<T>
where T: Copy + Add<T, Output = T> + Mul<T, Output = T>
{
[
vec4_dot_vec3(col_mat4x3_row(mat, 0), a),
vec4_dot_vec3(col_mat4x3_row(mat, 1), a),
vec4_dot_vec3(col_mat4x3_row(mat, 2), a)
]
}
pub fn mat2x3_det<T>(mat: Matrix2x3<T>) -> T
where T: Copy + Mul<T, Output = T> + Sub<T, Output = T>
{
mat[0][0] * mat[1][1]
- mat[0][1] * mat[1][0]
}
pub fn mat3x2_det<T>(mat: Matrix3x2<T>) -> T
where T: Copy + Mul<T, Output = T> + Sub<T, Output = T>
{
mat[0][0] * mat[1][1]
- mat[0][1] * mat[1][0]
}
pub fn mat3_det<T>(mat: Matrix3<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T> + Sub<T, Output = T>
{
mat[0][0] * mat[1][1] * mat[2][2]
+ mat[0][1] * mat[1][2] * mat[2][0]
+ mat[0][2] * mat[1][0] * mat[2][1]
- mat[0][0] * mat[1][2] * mat[2][1]
- mat[0][1] * mat[1][0] * mat[2][2]
- mat[0][2] * mat[1][1] * mat[2][0]
}
pub fn mat3x4_det<T>(mat: Matrix3x4<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T> + Sub<T, Output = T>
{
mat[0][0] * mat[1][1] * mat[2][2]
+ mat[0][1] * mat[1][2] * mat[2][0]
+ mat[0][2] * mat[1][0] * mat[2][1]
- mat[0][0] * mat[1][2] * mat[2][1]
- mat[0][1] * mat[1][0] * mat[2][2]
- mat[0][2] * mat[1][1] * mat[2][0]
}
pub fn mat4x3_det<T>(mat: Matrix4x3<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T> + Sub<T, Output = T>
{
mat[0][0] * mat[1][1] * mat[2][2]
+ mat[0][1] * mat[1][2] * mat[2][0]
+ mat[0][2] * mat[1][0] * mat[2][1]
- mat[0][0] * mat[1][2] * mat[2][1]
- mat[0][1] * mat[1][0] * mat[2][2]
- mat[0][2] * mat[1][1] * mat[2][0]
}
pub fn mat4_det<T>(mat: Matrix4<T>) -> T
where T: Copy + Add<T, Output = T> + Mul<T, Output = T> + Sub<T, Output = T>
{
mat[0][0] * mat[1][1] * mat[2][2] * mat[3][3]
+ mat[0][0] * mat[1][2] * mat[2][3] * mat[3][1]
+ mat[0][0] * mat[1][3] * mat[2][1] * mat[3][2]
+ mat[0][1] * mat[1][0] * mat[2][3] * mat[3][2]
+ mat[0][1] * mat[1][2] * mat[2][0] * mat[3][3]
+ mat[0][1] * mat[1][3] * mat[2][2] * mat[3][0]
+ mat[0][2] * mat[1][0] * mat[2][1] * mat[3][3]
+ mat[0][2] * mat[1][1] * mat[2][3] * mat[3][0]
+ mat[0][2] * mat[1][3] * mat[2][0] * mat[3][1]
+ mat[0][3] * mat[1][0] * mat[2][2] * mat[3][1]
+ mat[0][3] * mat[1][1] * mat[2][0] * mat[3][2]
+ mat[0][3] * mat[1][2] * mat[2][1] * mat[3][0]
- mat[0][0] * mat[1][1] * mat[2][3] * mat[3][2]
- mat[0][0] * mat[1][2] * mat[2][1] * mat[3][3]
- mat[0][0] * mat[1][3] * mat[2][2] * mat[3][1]
- mat[0][1] * mat[1][0] * mat[2][2] * mat[3][3]
- mat[0][1] * mat[1][2] * mat[2][3] * mat[3][0]
- mat[0][1] * mat[1][3] * mat[2][0] * mat[3][2]
- mat[0][2] * mat[1][0] * mat[2][3] * mat[3][1]
- mat[0][2] * mat[1][1] * mat[2][0] * mat[3][3]
- mat[0][2] * mat[1][3] * mat[2][1] * mat[3][0]
- mat[0][3] * mat[1][0] * mat[2][1] * mat[3][2]
- mat[0][3] * mat[1][1] * mat[2][2] * mat[3][0]
- mat[0][3] * mat[1][2] * mat[2][0] * mat[3][1]
}
#[inline(always)]
pub fn mat2x3_inv_det<T>(mat: Matrix2x3<T>) -> T
where T: Copy + traits::One + Add<T, Output = T> + Mul<T, Output = T>
+ Sub<T, Output = T> + Div<T, Output = T>
{
let one = T::one();
one / mat2x3_det(mat)
}
#[inline(always)]
pub fn mat3x2_inv_det<T>(mat: Matrix3x2<T>) -> T
where T: Copy + traits::One + Mul<T, Output = T> + Sub<T, Output = T>
+ Div<T, Output = T>
{
let one = T::one();
one / mat3x2_det(mat)
}
#[inline(always)]
pub fn mat3_inv_det<T>(mat: Matrix3<T>) -> T
where T: Copy + traits::One + Mul<T, Output = T> + Sub<T, Output = T>
+ Div<T, Output = T> + Add<T, Output = T>
{
let one = T::one();
one / mat3_det(mat)
}
#[inline(always)]
pub fn mat3x4_inv_det<T>(mat: Matrix3x4<T>) -> T
where T: Copy + traits::One + Add<T, Output = T> + Mul<T, Output = T>
+ Sub<T, Output = T> + Div<T, Output = T>
{
let one = T::one();
one / mat3x4_det(mat)
}
#[inline(always)]
pub fn mat4x3_inv_det<T>(mat: Matrix4x3<T>) -> T
where T: Copy + traits::One + Add<T, Output = T> + Mul<T, Output = T>
+ Sub<T, Output = T> + Div<T, Output = T>
{
let one = T::one();
one / mat4x3_det(mat)
}
#[inline(always)]
pub fn mat4_inv_det<T>(mat: Matrix4<T>) -> T
where T: Copy + traits::One + Add<T, Output = T> + Mul<T, Output = T>
+ Sub<T, Output = T> + Div<T, Output = T>
{
let one = T::one();
one / mat4_det(mat)
}
pub fn mat2x3_inv<T>(mat: Matrix2x3<T>) -> Matrix2x3<T>
where T: Copy + traits::One + Add<T, Output = T> + Mul<T, Output = T>
+ Sub<T, Output = T> + Div<T, Output = T> + Neg<Output = T>
{
let inv_det = mat2x3_inv_det(mat);
[
[
mat[1][1] * inv_det,
- mat[0][1] * inv_det,
(
mat[0][1] * mat[1][2]
- mat[0][2] * mat[1][1]
) * inv_det
],
[
- mat[1][0] * inv_det,
mat[0][0] * inv_det,
(
mat[0][2] * mat[1][0]
- mat[0][0] * mat[1][2]
) * inv_det,
]
]
}
pub fn mat3x2_inv<T>(mat: Matrix3x2<T>) -> Matrix3x2<T>
where T: Copy + traits::One + Mul<T, Output = T> + Sub<T, Output = T>
+ Div<T, Output = T> + Neg<Output = T>
{
let inv_det = mat3x2_inv_det(mat);
[
[
mat[1][1] * inv_det,
- mat[0][1] * inv_det
],
[
- mat[1][0] * inv_det,
mat[0][0] * inv_det
],
[
(
mat[1][0] * mat[2][1]
- mat[1][1] * mat[2][0]
) * inv_det,
(
mat[0][1] * mat[2][0]
- mat[0][0] * mat[2][1]
) * inv_det
]
]
}
pub fn mat3_inv<T>(mat: Matrix3<T>) -> Matrix3<T>
where T: Copy + traits::One + Mul<T, Output = T> + Sub<T, Output = T>
+ Div<T, Output = T> + Add<T, Output = T>
{
let inv_det = mat3_inv_det(mat);
[
[ (
mat[1][1] * mat[2][2]
- mat[1][2] * mat[2][1]
) * inv_det,
(
mat[0][2] * mat[2][1]
- mat[0][1] * mat[2][2]
) * inv_det,
(
mat[0][1] * mat[1][2]
- mat[0][2] * mat[1][1]
) * inv_det
],
[
(
mat[1][2] * mat[2][0]
- mat[1][0] * mat[2][2]
) * inv_det,
(
mat[0][0] * mat[2][2]
- mat[0][2] * mat[2][0]
) * inv_det,
(
mat[0][2] * mat[1][0]
- mat[0][0] * mat[1][2]
) * inv_det
],
[
(
mat[1][0] * mat[2][1]
- mat[1][1] * mat[2][0]
) * inv_det,
(
mat[0][1] * mat[2][0]
- mat[0][0] * mat[2][1]
) * inv_det,
(
mat[0][0] * mat[1][1]
- mat[0][1] * mat[1][0]
) * inv_det
]
]
}
pub fn mat3x4_inv<T>(mat: Matrix3x4<T>) -> Matrix3x4<T>
where T: Copy + traits::One + Add<T, Output = T> + Mul<T, Output = T>
+ Sub<T, Output = T> + Div<T, Output = T>
{
let inv_det = mat3x4_inv_det(mat);
[
[ (
mat[1][1] * mat[2][2]
- mat[1][2] * mat[2][1]
) * inv_det,
(
mat[0][2] * mat[2][1]
- mat[0][1] * mat[2][2]
) * inv_det,
(
mat[0][1] * mat[1][2]
- mat[0][2] * mat[1][1]
) * inv_det,
(
mat[0][1] * mat[1][3] * mat[2][2]
+ mat[0][2] * mat[1][1] * mat[2][3]
+ mat[0][3] * mat[1][2] * mat[2][1]
- mat[0][1] * mat[1][2] * mat[2][3]
- mat[0][2] * mat[1][3] * mat[2][1]
- mat[0][3] * mat[1][1] * mat[2][2]
) * inv_det
],
[
(
mat[1][2] * mat[2][0]
- mat[1][0] * mat[2][2]
) * inv_det,
(
mat[0][0] * mat[2][2]
- mat[0][2] * mat[2][0]
) * inv_det,
(
mat[0][2] * mat[1][0]
- mat[0][0] * mat[1][2]
) * inv_det,
(
mat[0][0] * mat[1][2] * mat[2][3]
+ mat[0][2] * mat[1][3] * mat[2][0]
+ mat[0][3] * mat[1][0] * mat[2][2]
- mat[0][0] * mat[1][3] * mat[2][2]
- mat[0][2] * mat[1][0] * mat[2][3]
- mat[0][3] * mat[1][2] * mat[2][0]
) * inv_det
],
[
(
mat[1][0] * mat[2][1]
- mat[1][1] * mat[2][0]
) * inv_det,
(
mat[0][1] * mat[2][0]
- mat[0][0] * mat[2][1]
) * inv_det,
(
mat[0][0] * mat[1][1]
- mat[0][1] * mat[1][0]
) * inv_det,
(
mat[0][0] * mat[1][3] * mat[2][1]
+ mat[0][1] * mat[1][0] * mat[2][3]
+ mat[0][3] * mat[1][1] * mat[2][0]
- mat[0][0] * mat[1][1] * mat[2][3]
- mat[0][1] * mat[1][3] * mat[2][0]
- mat[0][3] * mat[1][0] * mat[2][1]
) * inv_det
]
]
}
pub fn mat4x3_inv<T>(mat: Matrix4x3<T>) -> Matrix4x3<T>
where T: Copy + traits::One + Add<T, Output = T> + Mul<T, Output = T>
+ Sub<T, Output = T> + Div<T, Output = T>
{
let inv_det = mat4x3_inv_det(mat);
[
[ (
mat[1][1] * mat[2][2]
- mat[1][2] * mat[2][1]
) * inv_det,
(
mat[0][2] * mat[2][1]
- mat[0][1] * mat[2][2]
) * inv_det,
(
mat[0][1] * mat[1][2]
- mat[0][2] * mat[1][1]
) * inv_det
],
[
(
mat[1][2] * mat[2][0]
- mat[1][0] * mat[2][2]
) * inv_det,
(
mat[0][0] * mat[2][2]
- mat[0][2] * mat[2][0]
) * inv_det,
(
mat[0][2] * mat[1][0]
- mat[0][0] * mat[1][2]
) * inv_det
],
[
(
mat[1][0] * mat[2][1]
- mat[1][1] * mat[2][0]
) * inv_det,
(
mat[0][1] * mat[2][0]
- mat[0][0] * mat[2][1]
) * inv_det,
(
mat[0][0] * mat[1][1]
- mat[0][1] * mat[1][0]
) * inv_det
],
[
(
mat[1][0] * mat[2][2] * mat[3][1]
+ mat[1][1] * mat[2][0] * mat[3][2]
+ mat[1][2] * mat[2][1] * mat[3][0]
- mat[1][0] * mat[2][1] * mat[3][2]
- mat[1][1] * mat[2][2] * mat[3][0]
- mat[1][2] * mat[2][0] * mat[3][1]
) * inv_det,
(
mat[0][0] * mat[2][1] * mat[3][2]
+ mat[0][1] * mat[2][2] * mat[3][0]
+ mat[0][2] * mat[2][0] * mat[3][1]
- mat[0][0] * mat[2][2] * mat[3][1]
- mat[0][1] * mat[2][0] * mat[3][2]
- mat[0][2] * mat[2][1] * mat[3][0]
) * inv_det,
(
mat[0][0] * mat[1][2] * mat[3][1]
+ mat[0][1] * mat[1][0] * mat[3][2]
+ mat[0][2] * mat[1][1] * mat[3][0]
- mat[0][0] * mat[1][1] * mat[3][2]
- mat[0][1] * mat[1][2] * mat[3][0]
- mat[0][2] * mat[1][0] * mat[3][1]
) * inv_det
]
]
}
pub fn mat4_inv<T>(mat: Matrix4<T>) -> Matrix4<T>
where T: Copy + traits::One + Add<T, Output = T> + Mul<T, Output = T>
+ Sub<T, Output = T> + Div<T, Output = T>
{
let inv_det = mat4_inv_det(mat);
[
[ (
mat[1][1] * mat[2][2] * mat[3][3]
+ mat[1][2] * mat[2][3] * mat[3][1]
+ mat[1][3] * mat[2][1] * mat[3][2]
- mat[1][1] * mat[2][3] * mat[3][2]
- mat[1][2] * mat[2][1] * mat[3][3]
- mat[1][3] * mat[2][2] * mat[3][1]
) * inv_det,
(
mat[0][1] * mat[2][3] * mat[3][2]
+ mat[0][2] * mat[2][1] * mat[3][3]
+ mat[0][3] * mat[2][2] * mat[3][1]
- mat[0][1] * mat[2][2] * mat[3][3]
- mat[0][2] * mat[2][3] * mat[3][1]
- mat[0][3] * mat[2][1] * mat[3][2]
) * inv_det,
(
mat[0][1] * mat[1][2] * mat[3][3]
+ mat[0][2] * mat[1][3] * mat[3][1]
+ mat[0][3] * mat[1][1] * mat[3][2]
- mat[0][1] * mat[1][3] * mat[3][2]
- mat[0][2] * mat[1][1] * mat[3][3]
- mat[0][3] * mat[1][2] * mat[3][1]
) * inv_det,
(
mat[0][1] * mat[1][3] * mat[2][2]
+ mat[0][2] * mat[1][1] * mat[2][3]
+ mat[0][3] * mat[1][2] * mat[2][1]
- mat[0][1] * mat[1][2] * mat[2][3]
- mat[0][2] * mat[1][3] * mat[2][1]
- mat[0][3] * mat[1][1] * mat[2][2]
) * inv_det
],
[
(
mat[1][0] * mat[2][3] * mat[3][2]
+ mat[1][2] * mat[2][0] * mat[3][3]
+ mat[1][3] * mat[2][2] * mat[3][0]
- mat[1][0] * mat[2][2] * mat[3][3]
- mat[1][2] * mat[2][3] * mat[3][0]
- mat[1][3] * mat[2][0] * mat[3][2]
) * inv_det,
(
mat[0][0] * mat[2][2] * mat[3][3]
+ mat[0][2] * mat[2][3] * mat[3][0]
+ mat[0][3] * mat[2][0] * mat[3][2]
- mat[0][0] * mat[2][3] * mat[3][2]
- mat[0][2] * mat[2][0] * mat[3][3]
- mat[0][3] * mat[2][2] * mat[3][0]
) * inv_det,
(
mat[0][0] * mat[1][3] * mat[3][2]
+ mat[0][2] * mat[1][0] * mat[3][3]
+ mat[0][3] * mat[1][2] * mat[3][0]
- mat[0][0] * mat[1][2] * mat[3][3]
- mat[0][2] * mat[1][3] * mat[3][0]
- mat[0][3] * mat[1][0] * mat[3][2]
) * inv_det,
(
mat[0][0] * mat[1][2] * mat[2][3]
+ mat[0][2] * mat[1][3] * mat[2][0]
+ mat[0][3] * mat[1][0] * mat[2][2]
- mat[0][0] * mat[1][3] * mat[2][2]
- mat[0][2] * mat[1][0] * mat[2][3]
- mat[0][3] * mat[1][2] * mat[2][0]
) * inv_det
],
[
(
mat[1][0] * mat[2][1] * mat[3][3]
+ mat[1][1] * mat[2][3] * mat[3][0]
+ mat[1][3] * mat[2][0] * mat[3][1]
- mat[1][0] * mat[2][3] * mat[3][1]
- mat[1][1] * mat[2][0] * mat[3][3]
- mat[1][3] * mat[2][1] * mat[3][0]
) * inv_det,
(
mat[0][0] * mat[2][3] * mat[3][1]
+ mat[0][1] * mat[2][0] * mat[3][3]
+ mat[0][3] * mat[2][1] * mat[3][0]
- mat[0][0] * mat[2][1] * mat[3][3]
- mat[0][1] * mat[2][3] * mat[3][0]
- mat[0][3] * mat[2][0] * mat[3][1]
) * inv_det,
(
mat[0][0] * mat[1][1] * mat[3][3]
+ mat[0][1] * mat[1][3] * mat[3][0]
+ mat[0][3] * mat[1][0] * mat[3][1]
- mat[0][0] * mat[1][3] * mat[3][1]
- mat[0][1] * mat[1][0] * mat[3][3]
- mat[0][3] * mat[1][1] * mat[3][0]
) * inv_det,
(
mat[0][0] * mat[1][3] * mat[2][1]
+ mat[0][1] * mat[1][0] * mat[2][3]
+ mat[0][3] * mat[1][1] * mat[2][0]
- mat[0][0] * mat[1][1] * mat[2][3]
- mat[0][1] * mat[1][3] * mat[2][0]
- mat[0][3] * mat[1][0] * mat[2][1]
) * inv_det
],
[
(
mat[1][0] * mat[2][2] * mat[3][1]
+ mat[1][1] * mat[2][0] * mat[3][2]
+ mat[1][2] * mat[2][1] * mat[3][0]
- mat[1][0] * mat[2][1] * mat[3][2]
- mat[1][1] * mat[2][2] * mat[3][0]
- mat[1][2] * mat[2][0] * mat[3][1]
) * inv_det,
(
mat[0][0] * mat[2][1] * mat[3][2]
+ mat[0][1] * mat[2][2] * mat[3][0]
+ mat[0][2] * mat[2][0] * mat[3][1]
- mat[0][0] * mat[2][2] * mat[3][1]
- mat[0][1] * mat[2][0] * mat[3][2]
- mat[0][2] * mat[2][1] * mat[3][0]
) * inv_det,
(
mat[0][0] * mat[1][2] * mat[3][1]
+ mat[0][1] * mat[1][0] * mat[3][2]
+ mat[0][2] * mat[1][1] * mat[3][0]
- mat[0][0] * mat[1][1] * mat[3][2]
- mat[0][1] * mat[1][2] * mat[3][0]
- mat[0][2] * mat[1][0] * mat[3][1]
) * inv_det,
(
mat[0][0] * mat[1][1] * mat[2][2]
+ mat[0][1] * mat[1][2] * mat[2][0]
+ mat[0][2] * mat[1][0] * mat[2][1]
- mat[0][0] * mat[1][2] * mat[2][1]
- mat[0][1] * mat[1][0] * mat[2][2]
- mat[0][2] * mat[1][1] * mat[2][0]
) * inv_det
]
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_row_mat2x3_mul() {
let a: Matrix2x3<f64> = mat2x3_id();
let b = a;
let _ = row_mat2x3_mul(a, b);
}
#[test]
fn test_row_mat3x4_mul() {
let a: Matrix3x4<f64> = mat3x4_id();
let b = a;
let _ = row_mat3x4_mul(a, b);
}
}