{Vec, Mat, Quat}->{Vector, Matrix, Quaternion}
The full names provide consistency with the other types. Also, Vec is now a type declared in libstd - this will reduce confusion.
This commit is contained in:
parent
ae370f3a1f
commit
64ae5fbd9a
23 changed files with 827 additions and 825 deletions
|
@ -20,462 +20,462 @@ use extra;
|
|||
|
||||
type float = f32;
|
||||
|
||||
pub mod mat2 {
|
||||
pub mod matrix2 {
|
||||
use cgmath::matrix::*;
|
||||
use cgmath::vector::*;
|
||||
type float = f32;
|
||||
|
||||
pub static A: Mat2<f32> = Mat2 { x: Vec2 { x: 1.0, y: 3.0 },
|
||||
y: Vec2 { x: 2.0, y: 4.0 } };
|
||||
pub static B: Mat2<f32> = Mat2 { x: Vec2 { x: 2.0, y: 4.0 },
|
||||
y: Vec2 { x: 3.0, y: 5.0 } };
|
||||
pub static A: Matrix2<f32> = Matrix2 { x: Vector2 { x: 1.0, y: 3.0 },
|
||||
y: Vector2 { x: 2.0, y: 4.0 } };
|
||||
pub static B: Matrix2<f32> = Matrix2 { x: Vector2 { x: 2.0, y: 4.0 },
|
||||
y: Vector2 { x: 3.0, y: 5.0 } };
|
||||
}
|
||||
|
||||
pub mod mat3 {
|
||||
pub mod matrix3 {
|
||||
use cgmath::matrix::*;
|
||||
use cgmath::vector::*;
|
||||
type float = f32;
|
||||
|
||||
pub static A: Mat3<f32> = Mat3 { x: Vec3 { x: 1.0, y: 4.0, z: 7.0 },
|
||||
y: Vec3 { x: 2.0, y: 5.0, z: 8.0 },
|
||||
z: Vec3 { x: 3.0, y: 6.0, z: 9.0 } };
|
||||
pub static B: Mat3<f32> = Mat3 { x: Vec3 { x: 2.0, y: 5.0, z: 8.0 },
|
||||
y: Vec3 { x: 3.0, y: 6.0, z: 9.0 },
|
||||
z: Vec3 { x: 4.0, y: 7.0, z: 10.0 } };
|
||||
pub static A: Matrix3<f32> = Matrix3 { x: Vector3 { x: 1.0, y: 4.0, z: 7.0 },
|
||||
y: Vector3 { x: 2.0, y: 5.0, z: 8.0 },
|
||||
z: Vector3 { x: 3.0, y: 6.0, z: 9.0 } };
|
||||
pub static B: Matrix3<f32> = Matrix3 { x: Vector3 { x: 2.0, y: 5.0, z: 8.0 },
|
||||
y: Vector3 { x: 3.0, y: 6.0, z: 9.0 },
|
||||
z: Vector3 { x: 4.0, y: 7.0, z: 10.0 } };
|
||||
}
|
||||
|
||||
pub mod mat4 {
|
||||
pub mod matrix4 {
|
||||
use cgmath::matrix::*;
|
||||
use cgmath::vector::*;
|
||||
type float = f32;
|
||||
|
||||
pub static A: Mat4<f32> = Mat4 { x: Vec4 { x: 1.0, y: 5.0, z: 9.0, w: 13.0 },
|
||||
y: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 },
|
||||
z: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 },
|
||||
w: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 } };
|
||||
pub static B: Mat4<f32> = Mat4 { x: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 },
|
||||
y: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 },
|
||||
z: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 },
|
||||
w: Vec4 { x: 5.0, y: 9.0, z: 13.0, w: 17.0 } };
|
||||
pub static A: Matrix4<f32> = Matrix4 { x: Vector4 { x: 1.0, y: 5.0, z: 9.0, w: 13.0 },
|
||||
y: Vector4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 },
|
||||
z: Vector4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 },
|
||||
w: Vector4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 } };
|
||||
pub static B: Matrix4<f32> = Matrix4 { x: Vector4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 },
|
||||
y: Vector4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 },
|
||||
z: Vector4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 },
|
||||
w: Vector4 { x: 5.0, y: 9.0, z: 13.0, w: 17.0 } };
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_mul_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_mul_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.mul_m(&mat2::B);
|
||||
matrix_a = matrix_a.mul_m(&matrix2::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_mul_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_mul_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.mul_m(&mat3::B);
|
||||
matrix_a = matrix_a.mul_m(&matrix3::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_mul_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_mul_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.mul_m(&mat4::B);
|
||||
matrix_a = matrix_a.mul_m(&matrix4::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_add_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_add_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.add_m(&mat2::B);
|
||||
matrix_a = matrix_a.add_m(&matrix2::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_add_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_add_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.add_m(&mat3::B);
|
||||
matrix_a = matrix_a.add_m(&matrix3::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_add_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_add_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.add_m(&mat4::B);
|
||||
matrix_a = matrix_a.add_m(&matrix4::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_sub_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_sub_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.sub_m(&mat2::B);
|
||||
matrix_a = matrix_a.sub_m(&matrix2::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_sub_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_sub_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.sub_m(&mat3::B);
|
||||
matrix_a = matrix_a.sub_m(&matrix3::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_sub_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_sub_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.sub_m(&mat4::B);
|
||||
matrix_a = matrix_a.sub_m(&matrix4::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_mul_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_mul_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.mul_s(2.0);
|
||||
matrix_a = matrix_a.mul_s(2.0);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_mul_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_mul_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.mul_s(2.0);
|
||||
matrix_a = matrix_a.mul_s(2.0);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_mul_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_mul_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.mul_s(2.0);
|
||||
matrix_a = matrix_a.mul_s(2.0);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_div_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_div_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.div_s(2.);
|
||||
matrix_a = matrix_a.div_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_div_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_div_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.div_s(2.);
|
||||
matrix_a = matrix_a.div_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_div_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_div_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.div_s(2.);
|
||||
matrix_a = matrix_a.div_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_rem_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_rem_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.rem_s(2.);
|
||||
matrix_a = matrix_a.rem_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_rem_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_rem_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.rem_s(2.);
|
||||
matrix_a = matrix_a.rem_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_rem_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_rem_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.rem_s(2.);
|
||||
matrix_a = matrix_a.rem_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_neg_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_neg_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.neg_self();
|
||||
matrix_a.neg_self();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_neg_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_neg_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.neg_self();
|
||||
matrix_a.neg_self();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_neg_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_neg_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.neg_self();
|
||||
matrix_a.neg_self();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_div_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_div_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.div_self_s(2.);
|
||||
matrix_a.div_self_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_div_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_div_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.div_self_s(2.);
|
||||
matrix_a.div_self_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_div_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_div_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.div_self_s(2.);
|
||||
matrix_a.div_self_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_rem_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_rem_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.rem_self_s(2.);
|
||||
matrix_a.rem_self_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_rem_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_rem_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.rem_self_s(2.);
|
||||
matrix_a.rem_self_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_rem_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_rem_self_s(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.rem_self_s(2.);
|
||||
matrix_a.rem_self_s(2.);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_mul_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_mul_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.mul_self_m(&mat2::B);
|
||||
matrix_a.mul_self_m(&matrix2::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_mul_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_mul_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.mul_self_m(&mat3::B);
|
||||
matrix_a.mul_self_m(&matrix3::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_mul_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_mul_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.mul_self_m(&mat4::B);
|
||||
matrix_a.mul_self_m(&matrix4::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_add_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_add_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.add_self_m(&mat2::B);
|
||||
matrix_a.add_self_m(&matrix2::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_add_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_add_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.add_self_m(&mat3::B);
|
||||
matrix_a.add_self_m(&matrix3::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_add_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_add_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.add_self_m(&mat4::B);
|
||||
matrix_a.add_self_m(&matrix4::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_sub_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_sub_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.sub_self_m(&mat2::B);
|
||||
matrix_a.sub_self_m(&matrix2::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_sub_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_sub_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.sub_self_m(&mat3::B);
|
||||
matrix_a.sub_self_m(&matrix3::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_sub_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_sub_self_m(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.add_self_m(&mat4::B);
|
||||
matrix_a.add_self_m(&matrix4::B);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_transpose(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_transpose(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.transpose();
|
||||
matrix_a = matrix_a.transpose();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_transpose(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_transpose(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.transpose();
|
||||
matrix_a = matrix_a.transpose();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_transpose(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_transpose(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a = mat_a.transpose();
|
||||
matrix_a = matrix_a.transpose();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat2_transpose_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat2::A.clone();
|
||||
fn bench_matrix2_transpose_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix2::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.transpose_self();
|
||||
matrix_a.transpose_self();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat3_transpose_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat3::A.clone();
|
||||
fn bench_matrix3_transpose_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix3::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.transpose_self();
|
||||
matrix_a.transpose_self();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mat4_transpose_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut mat_a = mat4::A.clone();
|
||||
fn bench_matrix4_transpose_self(b: &mut extra::test::BenchHarness) {
|
||||
let mut matrix_a = matrix4::A.clone();
|
||||
b.iter(|| {
|
||||
for _ in range(0, 1000) {
|
||||
mat_a.transpose_self();
|
||||
matrix_a.transpose_self();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
//! Axis-aligned bounding boxes
|
||||
|
||||
use point::{Point, Point2, Point3};
|
||||
use vector::{Vector, Vec2, Vec3};
|
||||
use vector::{Vector, Vector2, Vector3};
|
||||
use array::build;
|
||||
use partial_ord::PartOrdPrim;
|
||||
use std::fmt;
|
||||
|
@ -89,7 +89,7 @@ impl<S: PartOrdPrim> Aabb2<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdPrim> Aabb<S, Vec2<S>, Point2<S>, [S, ..2]> for Aabb2<S> {
|
||||
impl<S: PartOrdPrim> Aabb<S, Vector2<S>, Point2<S>, [S, ..2]> for Aabb2<S> {
|
||||
fn new(p1: Point2<S>, p2: Point2<S>) -> Aabb2<S> { Aabb2::new(p1, p2) }
|
||||
#[inline] fn min<'a>(&'a self) -> &'a Point2<S> { &self.min }
|
||||
#[inline] fn max<'a>(&'a self) -> &'a Point2<S> { &self.max }
|
||||
|
@ -122,7 +122,7 @@ impl<S: PartOrdPrim> Aabb3<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdPrim> Aabb<S, Vec3<S>, Point3<S>, [S, ..3]> for Aabb3<S> {
|
||||
impl<S: PartOrdPrim> Aabb<S, Vector3<S>, Point3<S>, [S, ..3]> for Aabb3<S> {
|
||||
fn new(p1: Point3<S>, p2: Point3<S>) -> Aabb3<S> { Aabb3::new(p1, p2) }
|
||||
#[inline] fn min<'a>(&'a self) -> &'a Point3<S> { &self.min }
|
||||
#[inline] fn max<'a>(&'a self) -> &'a Point3<S> { &self.max }
|
||||
|
|
|
@ -16,10 +16,10 @@
|
|||
use std::num;
|
||||
|
||||
use array::Array;
|
||||
use matrix::{Mat2, Mat3, Mat4};
|
||||
use matrix::{Matrix2, Matrix3, Matrix4};
|
||||
use point::{Point2, Point3};
|
||||
use quaternion::Quat;
|
||||
use vector::{Vec2, Vec3, Vec4};
|
||||
use quaternion::Quaternion;
|
||||
use vector::{Vector2, Vector3, Vector4};
|
||||
|
||||
pub trait ApproxEq<T: Float> {
|
||||
fn approx_epsilon(_hack: Option<Self>) -> T {
|
||||
|
@ -62,15 +62,15 @@ macro_rules! approx_array(
|
|||
)
|
||||
)
|
||||
|
||||
approx_array!(impl<S> Mat2<S>)
|
||||
approx_array!(impl<S> Mat3<S>)
|
||||
approx_array!(impl<S> Mat4<S>)
|
||||
approx_array!(impl<S> Matrix2<S>)
|
||||
approx_array!(impl<S> Matrix3<S>)
|
||||
approx_array!(impl<S> Matrix4<S>)
|
||||
|
||||
approx_array!(impl<S> Quat<S>)
|
||||
approx_array!(impl<S> Quaternion<S>)
|
||||
|
||||
approx_array!(impl<S> Vec2<S>)
|
||||
approx_array!(impl<S> Vec3<S>)
|
||||
approx_array!(impl<S> Vec4<S>)
|
||||
approx_array!(impl<S> Vector2<S>)
|
||||
approx_array!(impl<S> Vector3<S>)
|
||||
approx_array!(impl<S> Vector4<S>)
|
||||
|
||||
approx_array!(impl<S> Point2<S>)
|
||||
approx_array!(impl<S> Point3<S>)
|
||||
|
|
|
@ -16,11 +16,11 @@
|
|||
//! Oriented bounding cylinder
|
||||
|
||||
use point::Point3;
|
||||
use vector::Vec3;
|
||||
use vector::Vector3;
|
||||
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct Cylinder<S> {
|
||||
pub center: Point3<S>,
|
||||
pub axis: Vec3<S>,
|
||||
pub axis: Vector3<S>,
|
||||
pub radius: S,
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
//! View frustum for visibility determination
|
||||
|
||||
use matrix::{Matrix, Mat4};
|
||||
use matrix::{Matrix, Matrix4};
|
||||
use plane::Plane;
|
||||
use point::Point3;
|
||||
use vector::{Vector, EuclideanVector};
|
||||
|
@ -48,13 +48,13 @@ Frustum<S> {
|
|||
}
|
||||
|
||||
/// Extracts frustum planes from a projection matrix
|
||||
pub fn from_mat4(mat: Mat4<S>) -> Frustum<S> {
|
||||
Frustum::new(Plane::from_vec4(mat.r(3).add_v(&mat.r(0)).normalize()),
|
||||
Plane::from_vec4(mat.r(3).sub_v(&mat.r(0)).normalize()),
|
||||
Plane::from_vec4(mat.r(3).add_v(&mat.r(1)).normalize()),
|
||||
Plane::from_vec4(mat.r(3).sub_v(&mat.r(1)).normalize()),
|
||||
Plane::from_vec4(mat.r(3).add_v(&mat.r(2)).normalize()),
|
||||
Plane::from_vec4(mat.r(3).sub_v(&mat.r(2)).normalize()))
|
||||
pub fn from_matrix4(mat: Matrix4<S>) -> Frustum<S> {
|
||||
Frustum::new(Plane::from_vector4(mat.r(3).add_v(&mat.r(0)).normalize()),
|
||||
Plane::from_vector4(mat.r(3).sub_v(&mat.r(0)).normalize()),
|
||||
Plane::from_vector4(mat.r(3).add_v(&mat.r(1)).normalize()),
|
||||
Plane::from_vector4(mat.r(3).sub_v(&mat.r(1)).normalize()),
|
||||
Plane::from_vector4(mat.r(3).add_v(&mat.r(2)).normalize()),
|
||||
Plane::from_vector4(mat.r(3).sub_v(&mat.r(2)).normalize()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -22,136 +22,136 @@ use angle::{Rad, sin, cos, sin_cos};
|
|||
use approx::ApproxEq;
|
||||
use array::{Array, build};
|
||||
use point::{Point, Point3};
|
||||
use quaternion::{Quat, ToQuat};
|
||||
use quaternion::{Quaternion, ToQuaternion};
|
||||
use vector::{Vector, EuclideanVector};
|
||||
use vector::{Vec2, Vec3, Vec4};
|
||||
use vector::{Vector2, Vector3, Vector4};
|
||||
use partial_ord::PartOrdFloat;
|
||||
|
||||
/// A 2 x 2, column major matrix
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct Mat2<S> { pub x: Vec2<S>, pub y: Vec2<S> }
|
||||
pub struct Matrix2<S> { pub x: Vector2<S>, pub y: Vector2<S> }
|
||||
|
||||
/// A 3 x 3, column major matrix
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct Mat3<S> { pub x: Vec3<S>, pub y: Vec3<S>, pub z: Vec3<S> }
|
||||
pub struct Matrix3<S> { pub x: Vector3<S>, pub y: Vector3<S>, pub z: Vector3<S> }
|
||||
|
||||
/// A 4 x 4, column major matrix
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct Mat4<S> { pub x: Vec4<S>, pub y: Vec4<S>, pub z: Vec4<S>, pub w: Vec4<S> }
|
||||
pub struct Matrix4<S> { pub x: Vector4<S>, pub y: Vector4<S>, pub z: Vector4<S>, pub w: Vector4<S> }
|
||||
|
||||
|
||||
impl<S: Primitive> Mat2<S> {
|
||||
impl<S: Primitive> Matrix2<S> {
|
||||
#[inline]
|
||||
pub fn new(c0r0: S, c0r1: S,
|
||||
c1r0: S, c1r1: S) -> Mat2<S> {
|
||||
Mat2::from_cols(Vec2::new(c0r0, c0r1),
|
||||
Vec2::new(c1r0, c1r1))
|
||||
c1r0: S, c1r1: S) -> Matrix2<S> {
|
||||
Matrix2::from_cols(Vector2::new(c0r0, c0r1),
|
||||
Vector2::new(c1r0, c1r1))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_cols(c0: Vec2<S>, c1: Vec2<S>) -> Mat2<S> {
|
||||
Mat2 { x: c0, y: c1 }
|
||||
pub fn from_cols(c0: Vector2<S>, c1: Vector2<S>) -> Matrix2<S> {
|
||||
Matrix2 { x: c0, y: c1 }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_value(value: S) -> Mat2<S> {
|
||||
Mat2::new(value.clone(), zero(),
|
||||
pub fn from_value(value: S) -> Matrix2<S> {
|
||||
Matrix2::new(value.clone(), zero(),
|
||||
zero(), value.clone())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn zero() -> Mat2<S> {
|
||||
Mat2::from_value(zero())
|
||||
pub fn zero() -> Matrix2<S> {
|
||||
Matrix2::from_value(zero())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn identity() -> Mat2<S> {
|
||||
Mat2::from_value(one())
|
||||
pub fn identity() -> Matrix2<S> {
|
||||
Matrix2::from_value(one())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>> Mat2<S> {
|
||||
pub fn look_at(dir: &Vec2<S>, up: &Vec2<S>) -> Mat2<S> {
|
||||
impl<S: PartOrdFloat<S>> Matrix2<S> {
|
||||
pub fn look_at(dir: &Vector2<S>, up: &Vector2<S>) -> Matrix2<S> {
|
||||
//TODO: verify look_at 2D
|
||||
Mat2::from_cols(up.clone(), dir.clone()).transpose()
|
||||
Matrix2::from_cols(up.clone(), dir.clone()).transpose()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_angle(theta: Rad<S>) -> Mat2<S> {
|
||||
pub fn from_angle(theta: Rad<S>) -> Matrix2<S> {
|
||||
let cos_theta = cos(theta.clone());
|
||||
let sin_theta = sin(theta.clone());
|
||||
|
||||
Mat2::new(cos_theta.clone(), -sin_theta.clone(),
|
||||
Matrix2::new(cos_theta.clone(), -sin_theta.clone(),
|
||||
sin_theta.clone(), cos_theta.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Primitive> Mat3<S> {
|
||||
impl<S: Primitive> Matrix3<S> {
|
||||
#[inline]
|
||||
pub fn new(c0r0:S, c0r1:S, c0r2:S,
|
||||
c1r0:S, c1r1:S, c1r2:S,
|
||||
c2r0:S, c2r1:S, c2r2:S) -> Mat3<S> {
|
||||
Mat3::from_cols(Vec3::new(c0r0, c0r1, c0r2),
|
||||
Vec3::new(c1r0, c1r1, c1r2),
|
||||
Vec3::new(c2r0, c2r1, c2r2))
|
||||
c2r0:S, c2r1:S, c2r2:S) -> Matrix3<S> {
|
||||
Matrix3::from_cols(Vector3::new(c0r0, c0r1, c0r2),
|
||||
Vector3::new(c1r0, c1r1, c1r2),
|
||||
Vector3::new(c2r0, c2r1, c2r2))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_cols(c0: Vec3<S>, c1: Vec3<S>, c2: Vec3<S>) -> Mat3<S> {
|
||||
Mat3 { x: c0, y: c1, z: c2 }
|
||||
pub fn from_cols(c0: Vector3<S>, c1: Vector3<S>, c2: Vector3<S>) -> Matrix3<S> {
|
||||
Matrix3 { x: c0, y: c1, z: c2 }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_value(value: S) -> Mat3<S> {
|
||||
Mat3::new(value.clone(), zero(), zero(),
|
||||
pub fn from_value(value: S) -> Matrix3<S> {
|
||||
Matrix3::new(value.clone(), zero(), zero(),
|
||||
zero(), value.clone(), zero(),
|
||||
zero(), zero(), value.clone())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn zero() -> Mat3<S> {
|
||||
Mat3::from_value(zero())
|
||||
pub fn zero() -> Matrix3<S> {
|
||||
Matrix3::from_value(zero())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn identity() -> Mat3<S> {
|
||||
Mat3::from_value(one())
|
||||
pub fn identity() -> Matrix3<S> {
|
||||
Matrix3::from_value(one())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Mat3<S> {
|
||||
pub fn look_at(dir: &Vec3<S>, up: &Vec3<S>) -> Mat3<S> {
|
||||
Matrix3<S> {
|
||||
pub fn look_at(dir: &Vector3<S>, up: &Vector3<S>) -> Matrix3<S> {
|
||||
let dir = dir.normalize();
|
||||
let side = up.cross(&dir).normalize();
|
||||
let up = dir.cross(&side).normalize();
|
||||
|
||||
Mat3::from_cols(side, up, dir).transpose()
|
||||
Matrix3::from_cols(side, up, dir).transpose()
|
||||
}
|
||||
|
||||
/// Create a matrix from a rotation around the `x` axis (pitch).
|
||||
pub fn from_angle_x(theta: Rad<S>) -> Mat3<S> {
|
||||
pub fn from_angle_x(theta: Rad<S>) -> Matrix3<S> {
|
||||
// http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
|
||||
let (s, c) = sin_cos(theta);
|
||||
Mat3::new(one(), zero(), zero(),
|
||||
Matrix3::new(one(), zero(), zero(),
|
||||
zero(), c.clone(), s.clone(),
|
||||
zero(), -s.clone(), c.clone())
|
||||
}
|
||||
|
||||
/// Create a matrix from a rotation around the `y` axis (yaw).
|
||||
pub fn from_angle_y(theta: Rad<S>) -> Mat3<S> {
|
||||
pub fn from_angle_y(theta: Rad<S>) -> Matrix3<S> {
|
||||
// http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
|
||||
let (s, c) = sin_cos(theta);
|
||||
Mat3::new(c.clone(), zero(), -s.clone(),
|
||||
Matrix3::new(c.clone(), zero(), -s.clone(),
|
||||
zero(), one(), zero(),
|
||||
s.clone(), zero(), c.clone())
|
||||
}
|
||||
|
||||
/// Create a matrix from a rotation around the `z` axis (roll).
|
||||
pub fn from_angle_z(theta: Rad<S>) -> Mat3<S> {
|
||||
pub fn from_angle_z(theta: Rad<S>) -> Matrix3<S> {
|
||||
// http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
|
||||
let (s, c) = sin_cos(theta);
|
||||
Mat3::new(c.clone(), s.clone(), zero(),
|
||||
Matrix3::new(c.clone(), s.clone(), zero(),
|
||||
-s.clone(), c.clone(), zero(),
|
||||
zero(), zero(), one())
|
||||
}
|
||||
|
@ -163,23 +163,23 @@ Mat3<S> {
|
|||
/// - `x`: the angular rotation around the `x` axis (pitch).
|
||||
/// - `y`: the angular rotation around the `y` axis (yaw).
|
||||
/// - `z`: the angular rotation around the `z` axis (roll).
|
||||
pub fn from_euler(x: Rad<S>, y: Rad<S>, z: Rad<S>) -> Mat3<S> {
|
||||
pub fn from_euler(x: Rad<S>, y: Rad<S>, z: Rad<S>) -> Matrix3<S> {
|
||||
// http://en.wikipedia.org/wiki/Rotation_matrix#General_rotations
|
||||
let (sx, cx) = sin_cos(x);
|
||||
let (sy, cy) = sin_cos(y);
|
||||
let (sz, cz) = sin_cos(z);
|
||||
|
||||
Mat3::new(cy * cz, cy * sz, -sy,
|
||||
Matrix3::new(cy * cz, cy * sz, -sy,
|
||||
-cx * sz + sx * sy * cz, cx * cz + sx * sy * sz, sx * cy,
|
||||
sx * sz + cx * sy * cz, -sx * cz + cx * sy * sz, cx * cy)
|
||||
}
|
||||
|
||||
/// Create a matrix from a rotation around an arbitrary axis
|
||||
pub fn from_axis_angle(axis: &Vec3<S>, angle: Rad<S>) -> Mat3<S> {
|
||||
pub fn from_axis_angle(axis: &Vector3<S>, angle: Rad<S>) -> Matrix3<S> {
|
||||
let (s, c) = sin_cos(angle);
|
||||
let _1subc = one::<S>() - c;
|
||||
|
||||
Mat3::new(_1subc * axis.x * axis.x + c,
|
||||
Matrix3::new(_1subc * axis.x * axis.x + c,
|
||||
_1subc * axis.x * axis.y + s * axis.z,
|
||||
_1subc * axis.x * axis.z - s * axis.y,
|
||||
|
||||
|
@ -193,59 +193,59 @@ Mat3<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: Primitive> Mat4<S> {
|
||||
impl<S: Primitive> Matrix4<S> {
|
||||
#[inline]
|
||||
pub fn new(c0r0: S, c0r1: S, c0r2: S, c0r3: S,
|
||||
c1r0: S, c1r1: S, c1r2: S, c1r3: S,
|
||||
c2r0: S, c2r1: S, c2r2: S, c2r3: S,
|
||||
c3r0: S, c3r1: S, c3r2: S, c3r3: S) -> Mat4<S> {
|
||||
Mat4::from_cols(Vec4::new(c0r0, c0r1, c0r2, c0r3),
|
||||
Vec4::new(c1r0, c1r1, c1r2, c1r3),
|
||||
Vec4::new(c2r0, c2r1, c2r2, c2r3),
|
||||
Vec4::new(c3r0, c3r1, c3r2, c3r3))
|
||||
c3r0: S, c3r1: S, c3r2: S, c3r3: S) -> Matrix4<S> {
|
||||
Matrix4::from_cols(Vector4::new(c0r0, c0r1, c0r2, c0r3),
|
||||
Vector4::new(c1r0, c1r1, c1r2, c1r3),
|
||||
Vector4::new(c2r0, c2r1, c2r2, c2r3),
|
||||
Vector4::new(c3r0, c3r1, c3r2, c3r3))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_cols(c0: Vec4<S>, c1: Vec4<S>, c2: Vec4<S>, c3: Vec4<S>) -> Mat4<S> {
|
||||
Mat4 { x: c0, y: c1, z: c2, w: c3 }
|
||||
pub fn from_cols(c0: Vector4<S>, c1: Vector4<S>, c2: Vector4<S>, c3: Vector4<S>) -> Matrix4<S> {
|
||||
Matrix4 { x: c0, y: c1, z: c2, w: c3 }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_value(value: S) -> Mat4<S> {
|
||||
Mat4::new(value.clone(), zero(), zero(), zero(),
|
||||
pub fn from_value(value: S) -> Matrix4<S> {
|
||||
Matrix4::new(value.clone(), zero(), zero(), zero(),
|
||||
zero(), value.clone(), zero(), zero(),
|
||||
zero(), zero(), value.clone(), zero(),
|
||||
zero(), zero(), zero(), value.clone())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn zero() -> Mat4<S> {
|
||||
Mat4::from_value(zero())
|
||||
pub fn zero() -> Matrix4<S> {
|
||||
Matrix4::from_value(zero())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn identity() -> Mat4<S> {
|
||||
Mat4::from_value(one())
|
||||
pub fn identity() -> Matrix4<S> {
|
||||
Matrix4::from_value(one())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Mat4<S> {
|
||||
pub fn look_at(eye: &Point3<S>, center: &Point3<S>, up: &Vec3<S>) -> Mat4<S> {
|
||||
Matrix4<S> {
|
||||
pub fn look_at(eye: &Point3<S>, center: &Point3<S>, up: &Vector3<S>) -> Matrix4<S> {
|
||||
let f = center.sub_p(eye).normalize();
|
||||
let s = f.cross(up).normalize();
|
||||
let u = s.cross(&f);
|
||||
|
||||
Mat4::new(s.x.clone(), u.x.clone(), -f.x.clone(), zero(),
|
||||
Matrix4::new(s.x.clone(), u.x.clone(), -f.x.clone(), zero(),
|
||||
s.y.clone(), u.y.clone(), -f.y.clone(), zero(),
|
||||
s.z.clone(), u.z.clone(), -f.z.clone(), zero(),
|
||||
-eye.dot(&s), -eye.dot(&u), eye.dot(&f), one())
|
||||
}
|
||||
}
|
||||
|
||||
array!(impl<S> Mat2<S> -> [Vec2<S>, ..2] _2)
|
||||
array!(impl<S> Mat3<S> -> [Vec3<S>, ..3] _3)
|
||||
array!(impl<S> Mat4<S> -> [Vec4<S>, ..4] _4)
|
||||
array!(impl<S> Matrix2<S> -> [Vector2<S>, ..2] _2)
|
||||
array!(impl<S> Matrix3<S> -> [Vector3<S>, ..3] _3)
|
||||
array!(impl<S> Matrix4<S> -> [Vector4<S>, ..4] _4)
|
||||
|
||||
pub trait Matrix
|
||||
<
|
||||
|
@ -361,41 +361,41 @@ pub trait Matrix
|
|||
fn is_symmetric(&self) -> bool;
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>> Add<Mat2<S>, Mat2<S>> for Mat2<S> { #[inline] fn add(&self, other: &Mat2<S>) -> Mat2<S> { build(|i| self.i(i).add_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Add<Mat3<S>, Mat3<S>> for Mat3<S> { #[inline] fn add(&self, other: &Mat3<S>) -> Mat3<S> { build(|i| self.i(i).add_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Add<Mat4<S>, Mat4<S>> for Mat4<S> { #[inline] fn add(&self, other: &Mat4<S>) -> Mat4<S> { build(|i| self.i(i).add_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Add<Matrix2<S>, Matrix2<S>> for Matrix2<S> { #[inline] fn add(&self, other: &Matrix2<S>) -> Matrix2<S> { build(|i| self.i(i).add_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Add<Matrix3<S>, Matrix3<S>> for Matrix3<S> { #[inline] fn add(&self, other: &Matrix3<S>) -> Matrix3<S> { build(|i| self.i(i).add_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Add<Matrix4<S>, Matrix4<S>> for Matrix4<S> { #[inline] fn add(&self, other: &Matrix4<S>) -> Matrix4<S> { build(|i| self.i(i).add_v(other.i(i))) } }
|
||||
|
||||
impl<S: PartOrdFloat<S>> Sub<Mat2<S>, Mat2<S>> for Mat2<S> { #[inline] fn sub(&self, other: &Mat2<S>) -> Mat2<S> { build(|i| self.i(i).sub_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Sub<Mat3<S>, Mat3<S>> for Mat3<S> { #[inline] fn sub(&self, other: &Mat3<S>) -> Mat3<S> { build(|i| self.i(i).sub_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Sub<Mat4<S>, Mat4<S>> for Mat4<S> { #[inline] fn sub(&self, other: &Mat4<S>) -> Mat4<S> { build(|i| self.i(i).sub_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Sub<Matrix2<S>, Matrix2<S>> for Matrix2<S> { #[inline] fn sub(&self, other: &Matrix2<S>) -> Matrix2<S> { build(|i| self.i(i).sub_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Sub<Matrix3<S>, Matrix3<S>> for Matrix3<S> { #[inline] fn sub(&self, other: &Matrix3<S>) -> Matrix3<S> { build(|i| self.i(i).sub_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Sub<Matrix4<S>, Matrix4<S>> for Matrix4<S> { #[inline] fn sub(&self, other: &Matrix4<S>) -> Matrix4<S> { build(|i| self.i(i).sub_v(other.i(i))) } }
|
||||
|
||||
impl<S: PartOrdFloat<S>> Neg<Mat2<S>> for Mat2<S> { #[inline] fn neg(&self) -> Mat2<S> { build(|i| self.i(i).neg()) } }
|
||||
impl<S: PartOrdFloat<S>> Neg<Mat3<S>> for Mat3<S> { #[inline] fn neg(&self) -> Mat3<S> { build(|i| self.i(i).neg()) } }
|
||||
impl<S: PartOrdFloat<S>> Neg<Mat4<S>> for Mat4<S> { #[inline] fn neg(&self) -> Mat4<S> { build(|i| self.i(i).neg()) } }
|
||||
impl<S: PartOrdFloat<S>> Neg<Matrix2<S>> for Matrix2<S> { #[inline] fn neg(&self) -> Matrix2<S> { build(|i| self.i(i).neg()) } }
|
||||
impl<S: PartOrdFloat<S>> Neg<Matrix3<S>> for Matrix3<S> { #[inline] fn neg(&self) -> Matrix3<S> { build(|i| self.i(i).neg()) } }
|
||||
impl<S: PartOrdFloat<S>> Neg<Matrix4<S>> for Matrix4<S> { #[inline] fn neg(&self) -> Matrix4<S> { build(|i| self.i(i).neg()) } }
|
||||
|
||||
impl<S: PartOrdFloat<S>> Zero for Mat2<S> { #[inline] fn zero() -> Mat2<S> { Mat2::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } }
|
||||
impl<S: PartOrdFloat<S>> Zero for Mat3<S> { #[inline] fn zero() -> Mat3<S> { Mat3::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } }
|
||||
impl<S: PartOrdFloat<S>> Zero for Mat4<S> { #[inline] fn zero() -> Mat4<S> { Mat4::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } }
|
||||
impl<S: PartOrdFloat<S>> Zero for Matrix2<S> { #[inline] fn zero() -> Matrix2<S> { Matrix2::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } }
|
||||
impl<S: PartOrdFloat<S>> Zero for Matrix3<S> { #[inline] fn zero() -> Matrix3<S> { Matrix3::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } }
|
||||
impl<S: PartOrdFloat<S>> Zero for Matrix4<S> { #[inline] fn zero() -> Matrix4<S> { Matrix4::zero() } #[inline] fn is_zero(&self) -> bool { *self == zero() } }
|
||||
|
||||
impl<S: PartOrdFloat<S>> Mul<Mat2<S>, Mat2<S>> for Mat2<S> { #[inline] fn mul(&self, other: &Mat2<S>) -> Mat2<S> { build(|i| self.i(i).mul_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Mul<Mat3<S>, Mat3<S>> for Mat3<S> { #[inline] fn mul(&self, other: &Mat3<S>) -> Mat3<S> { build(|i| self.i(i).mul_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Mul<Mat4<S>, Mat4<S>> for Mat4<S> { #[inline] fn mul(&self, other: &Mat4<S>) -> Mat4<S> { build(|i| self.i(i).mul_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Mul<Matrix2<S>, Matrix2<S>> for Matrix2<S> { #[inline] fn mul(&self, other: &Matrix2<S>) -> Matrix2<S> { build(|i| self.i(i).mul_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Mul<Matrix3<S>, Matrix3<S>> for Matrix3<S> { #[inline] fn mul(&self, other: &Matrix3<S>) -> Matrix3<S> { build(|i| self.i(i).mul_v(other.i(i))) } }
|
||||
impl<S: PartOrdFloat<S>> Mul<Matrix4<S>, Matrix4<S>> for Matrix4<S> { #[inline] fn mul(&self, other: &Matrix4<S>) -> Matrix4<S> { build(|i| self.i(i).mul_v(other.i(i))) } }
|
||||
|
||||
impl<S: PartOrdFloat<S>> One for Mat2<S> { #[inline] fn one() -> Mat2<S> { Mat2::identity() } }
|
||||
impl<S: PartOrdFloat<S>> One for Mat3<S> { #[inline] fn one() -> Mat3<S> { Mat3::identity() } }
|
||||
impl<S: PartOrdFloat<S>> One for Mat4<S> { #[inline] fn one() -> Mat4<S> { Mat4::identity() } }
|
||||
impl<S: PartOrdFloat<S>> One for Matrix2<S> { #[inline] fn one() -> Matrix2<S> { Matrix2::identity() } }
|
||||
impl<S: PartOrdFloat<S>> One for Matrix3<S> { #[inline] fn one() -> Matrix3<S> { Matrix3::identity() } }
|
||||
impl<S: PartOrdFloat<S>> One for Matrix4<S> { #[inline] fn one() -> Matrix4<S> { Matrix4::identity() } }
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Matrix<S, [Vec2<S>, ..2], Vec2<S>, [S, ..2]>
|
||||
for Mat2<S>
|
||||
Matrix<S, [Vector2<S>, ..2], Vector2<S>, [S, ..2]>
|
||||
for Matrix2<S>
|
||||
{
|
||||
fn mul_m(&self, other: &Mat2<S>) -> Mat2<S> {
|
||||
Mat2::new(self.r(0).dot(other.c(0)), self.r(1).dot(other.c(0)),
|
||||
fn mul_m(&self, other: &Matrix2<S>) -> Matrix2<S> {
|
||||
Matrix2::new(self.r(0).dot(other.c(0)), self.r(1).dot(other.c(0)),
|
||||
self.r(0).dot(other.c(1)), self.r(1).dot(other.c(1)))
|
||||
}
|
||||
|
||||
fn transpose(&self) -> Mat2<S> {
|
||||
Mat2::new(self.cr(0, 0).clone(), self.cr(1, 0).clone(),
|
||||
fn transpose(&self) -> Matrix2<S> {
|
||||
Matrix2::new(self.cr(0, 0).clone(), self.cr(1, 0).clone(),
|
||||
self.cr(0, 1).clone(), self.cr(1, 1).clone())
|
||||
}
|
||||
|
||||
|
@ -410,12 +410,12 @@ for Mat2<S>
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn invert(&self) -> Option<Mat2<S>> {
|
||||
fn invert(&self) -> Option<Matrix2<S>> {
|
||||
let det = self.determinant();
|
||||
if det.approx_eq(&zero()) {
|
||||
None
|
||||
} else {
|
||||
Some(Mat2::new( *self.cr(1, 1) / det, -*self.cr(0, 1) / det,
|
||||
Some(Matrix2::new( *self.cr(1, 1) / det, -*self.cr(0, 1) / det,
|
||||
-*self.cr(1, 0) / det, *self.cr(0, 0) / det))
|
||||
}
|
||||
}
|
||||
|
@ -435,17 +435,17 @@ for Mat2<S>
|
|||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Matrix<S, [Vec3<S>, ..3], Vec3<S>, [S, ..3]>
|
||||
for Mat3<S>
|
||||
Matrix<S, [Vector3<S>, ..3], Vector3<S>, [S, ..3]>
|
||||
for Matrix3<S>
|
||||
{
|
||||
fn mul_m(&self, other: &Mat3<S>) -> Mat3<S> {
|
||||
Mat3::new(self.r(0).dot(other.c(0)),self.r(1).dot(other.c(0)),self.r(2).dot(other.c(0)),
|
||||
fn mul_m(&self, other: &Matrix3<S>) -> Matrix3<S> {
|
||||
Matrix3::new(self.r(0).dot(other.c(0)),self.r(1).dot(other.c(0)),self.r(2).dot(other.c(0)),
|
||||
self.r(0).dot(other.c(1)),self.r(1).dot(other.c(1)),self.r(2).dot(other.c(1)),
|
||||
self.r(0).dot(other.c(2)),self.r(1).dot(other.c(2)),self.r(2).dot(other.c(2)))
|
||||
}
|
||||
|
||||
fn transpose(&self) -> Mat3<S> {
|
||||
Mat3::new(self.cr(0, 0).clone(), self.cr(1, 0).clone(), self.cr(2, 0).clone(),
|
||||
fn transpose(&self) -> Matrix3<S> {
|
||||
Matrix3::new(self.cr(0, 0).clone(), self.cr(1, 0).clone(), self.cr(2, 0).clone(),
|
||||
self.cr(0, 1).clone(), self.cr(1, 1).clone(), self.cr(2, 1).clone(),
|
||||
self.cr(0, 2).clone(), self.cr(1, 2).clone(), self.cr(2, 2).clone())
|
||||
}
|
||||
|
@ -463,10 +463,10 @@ for Mat3<S>
|
|||
*self.cr(2, 0) * (*self.cr(0, 1) * *self.cr(1, 2) - *self.cr(1, 1) * *self.cr(0, 2))
|
||||
}
|
||||
|
||||
fn invert(&self) -> Option<Mat3<S>> {
|
||||
fn invert(&self) -> Option<Matrix3<S>> {
|
||||
let det = self.determinant();
|
||||
if det.approx_eq(&zero()) { None } else {
|
||||
Some(Mat3::from_cols(self.c(1).cross(self.c(2)).div_s(det.clone()),
|
||||
Some(Matrix3::from_cols(self.c(1).cross(self.c(2)).div_s(det.clone()),
|
||||
self.c(2).cross(self.c(0)).div_s(det.clone()),
|
||||
self.c(0).cross(self.c(1)).div_s(det.clone())).transpose())
|
||||
}
|
||||
|
@ -499,7 +499,7 @@ for Mat3<S>
|
|||
// causes the LLVM to miss identical loads and multiplies. This optimization
|
||||
// causes the code to be auto vectorized properly increasing the performance
|
||||
// around ~4 times.
|
||||
macro_rules! dot_mat4(
|
||||
macro_rules! dot_matrix4(
|
||||
($A:expr, $B:expr, $I:expr, $J:expr) => (
|
||||
(*$A.cr(0, $I)) * (*$B.cr($J, 0)) +
|
||||
(*$A.cr(1, $I)) * (*$B.cr($J, 1)) +
|
||||
|
@ -508,18 +508,18 @@ macro_rules! dot_mat4(
|
|||
))
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Matrix<S, [Vec4<S>, ..4], Vec4<S>, [S, ..4]>
|
||||
for Mat4<S>
|
||||
Matrix<S, [Vector4<S>, ..4], Vector4<S>, [S, ..4]>
|
||||
for Matrix4<S>
|
||||
{
|
||||
fn mul_m(&self, other: &Mat4<S>) -> Mat4<S> {
|
||||
Mat4::new(dot_mat4!(self, other, 0, 0), dot_mat4!(self, other, 1, 0), dot_mat4!(self, other, 2, 0), dot_mat4!(self, other, 3, 0),
|
||||
dot_mat4!(self, other, 0, 1), dot_mat4!(self, other, 1, 1), dot_mat4!(self, other, 2, 1), dot_mat4!(self, other, 3, 1),
|
||||
dot_mat4!(self, other, 0, 2), dot_mat4!(self, other, 1, 2), dot_mat4!(self, other, 2, 2), dot_mat4!(self, other, 3, 2),
|
||||
dot_mat4!(self, other, 0, 3), dot_mat4!(self, other, 1, 3), dot_mat4!(self, other, 2, 3), dot_mat4!(self, other, 3, 3))
|
||||
fn mul_m(&self, other: &Matrix4<S>) -> Matrix4<S> {
|
||||
Matrix4::new(dot_matrix4!(self, other, 0, 0), dot_matrix4!(self, other, 1, 0), dot_matrix4!(self, other, 2, 0), dot_matrix4!(self, other, 3, 0),
|
||||
dot_matrix4!(self, other, 0, 1), dot_matrix4!(self, other, 1, 1), dot_matrix4!(self, other, 2, 1), dot_matrix4!(self, other, 3, 1),
|
||||
dot_matrix4!(self, other, 0, 2), dot_matrix4!(self, other, 1, 2), dot_matrix4!(self, other, 2, 2), dot_matrix4!(self, other, 3, 2),
|
||||
dot_matrix4!(self, other, 0, 3), dot_matrix4!(self, other, 1, 3), dot_matrix4!(self, other, 2, 3), dot_matrix4!(self, other, 3, 3))
|
||||
}
|
||||
|
||||
fn transpose(&self) -> Mat4<S> {
|
||||
Mat4::new(self.cr(0, 0).clone(), self.cr(1, 0).clone(), self.cr(2, 0).clone(), self.cr(3, 0).clone(),
|
||||
fn transpose(&self) -> Matrix4<S> {
|
||||
Matrix4::new(self.cr(0, 0).clone(), self.cr(1, 0).clone(), self.cr(2, 0).clone(), self.cr(3, 0).clone(),
|
||||
self.cr(0, 1).clone(), self.cr(1, 1).clone(), self.cr(2, 1).clone(), self.cr(3, 1).clone(),
|
||||
self.cr(0, 2).clone(), self.cr(1, 2).clone(), self.cr(2, 2).clone(), self.cr(3, 2).clone(),
|
||||
self.cr(0, 3).clone(), self.cr(1, 3).clone(), self.cr(2, 3).clone(), self.cr(3, 3).clone())
|
||||
|
@ -535,16 +535,16 @@ for Mat4<S>
|
|||
}
|
||||
|
||||
fn determinant(&self) -> S {
|
||||
let m0 = Mat3::new(self.cr(1, 1).clone(), self.cr(2, 1).clone(), self.cr(3, 1).clone(),
|
||||
let m0 = Matrix3::new(self.cr(1, 1).clone(), self.cr(2, 1).clone(), self.cr(3, 1).clone(),
|
||||
self.cr(1, 2).clone(), self.cr(2, 2).clone(), self.cr(3, 2).clone(),
|
||||
self.cr(1, 3).clone(), self.cr(2, 3).clone(), self.cr(3, 3).clone());
|
||||
let m1 = Mat3::new(self.cr(0, 1).clone(), self.cr(2, 1).clone(), self.cr(3, 1).clone(),
|
||||
let m1 = Matrix3::new(self.cr(0, 1).clone(), self.cr(2, 1).clone(), self.cr(3, 1).clone(),
|
||||
self.cr(0, 2).clone(), self.cr(2, 2).clone(), self.cr(3, 2).clone(),
|
||||
self.cr(0, 3).clone(), self.cr(2, 3).clone(), self.cr(3, 3).clone());
|
||||
let m2 = Mat3::new(self.cr(0, 1).clone(), self.cr(1, 1).clone(), self.cr(3, 1).clone(),
|
||||
let m2 = Matrix3::new(self.cr(0, 1).clone(), self.cr(1, 1).clone(), self.cr(3, 1).clone(),
|
||||
self.cr(0, 2).clone(), self.cr(1, 2).clone(), self.cr(3, 2).clone(),
|
||||
self.cr(0, 3).clone(), self.cr(1, 3).clone(), self.cr(3, 3).clone());
|
||||
let m3 = Mat3::new(self.cr(0, 1).clone(), self.cr(1, 1).clone(), self.cr(2, 1).clone(),
|
||||
let m3 = Matrix3::new(self.cr(0, 1).clone(), self.cr(1, 1).clone(), self.cr(2, 1).clone(),
|
||||
self.cr(0, 2).clone(), self.cr(1, 2).clone(), self.cr(2, 2).clone(),
|
||||
self.cr(0, 3).clone(), self.cr(1, 3).clone(), self.cr(2, 3).clone());
|
||||
|
||||
|
@ -554,14 +554,14 @@ for Mat4<S>
|
|||
*self.cr(3, 0) * m3.determinant()
|
||||
}
|
||||
|
||||
fn invert(&self) -> Option<Mat4<S>> {
|
||||
fn invert(&self) -> Option<Matrix4<S>> {
|
||||
if self.is_invertible() {
|
||||
// Gauss Jordan Elimination with partial pivoting
|
||||
// So take this matrix ('mat') augmented with the identity ('ident'),
|
||||
// and essentially reduce [mat|ident]
|
||||
|
||||
let mut mat = self.clone();
|
||||
let mut ident = Mat4::identity();
|
||||
let mut ident = Matrix4::identity();
|
||||
|
||||
for j in range(0u, 4u) {
|
||||
// Find largest element in col j
|
||||
|
@ -636,27 +636,27 @@ for Mat4<S>
|
|||
}
|
||||
|
||||
// Conversion traits
|
||||
pub trait ToMat2<S: Primitive> { fn to_mat2(&self) -> Mat2<S>; }
|
||||
pub trait ToMat3<S: Primitive> { fn to_mat3(&self) -> Mat3<S>; }
|
||||
pub trait ToMat4<S: Primitive> { fn to_mat4(&self) -> Mat4<S>; }
|
||||
pub trait ToMatrix2<S: Primitive> { fn to_matrix2(&self) -> Matrix2<S>; }
|
||||
pub trait ToMatrix3<S: Primitive> { fn to_matrix3(&self) -> Matrix3<S>; }
|
||||
pub trait ToMatrix4<S: Primitive> { fn to_matrix4(&self) -> Matrix4<S>; }
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
ToMat3<S> for Mat2<S> {
|
||||
ToMatrix3<S> for Matrix2<S> {
|
||||
/// Clone the elements of a 2-dimensional matrix into the top corner of a
|
||||
/// 3-dimensional identity matrix.
|
||||
fn to_mat3(&self) -> Mat3<S> {
|
||||
Mat3::new(self.cr(0, 0).clone(), self.cr(0, 1).clone(), zero(),
|
||||
fn to_matrix3(&self) -> Matrix3<S> {
|
||||
Matrix3::new(self.cr(0, 0).clone(), self.cr(0, 1).clone(), zero(),
|
||||
self.cr(1, 0).clone(), self.cr(1, 1).clone(), zero(),
|
||||
zero(), zero(), one())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
ToMat4<S> for Mat2<S> {
|
||||
ToMatrix4<S> for Matrix2<S> {
|
||||
/// Clone the elements of a 2-dimensional matrix into the top corner of a
|
||||
/// 4-dimensional identity matrix.
|
||||
fn to_mat4(&self) -> Mat4<S> {
|
||||
Mat4::new(self.cr(0, 0).clone(), self.cr(0, 1).clone(), zero(), zero(),
|
||||
fn to_matrix4(&self) -> Matrix4<S> {
|
||||
Matrix4::new(self.cr(0, 0).clone(), self.cr(0, 1).clone(), zero(), zero(),
|
||||
self.cr(1, 0).clone(), self.cr(1, 1).clone(), zero(), zero(),
|
||||
zero(), zero(), one(), zero(),
|
||||
zero(), zero(), zero(), one())
|
||||
|
@ -664,11 +664,11 @@ ToMat4<S> for Mat2<S> {
|
|||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
ToMat4<S> for Mat3<S> {
|
||||
ToMatrix4<S> for Matrix3<S> {
|
||||
/// Clone the elements of a 3-dimensional matrix into the top corner of a
|
||||
/// 4-dimensional identity matrix.
|
||||
fn to_mat4(&self) -> Mat4<S> {
|
||||
Mat4::new(self.cr(0, 0).clone(), self.cr(0, 1).clone(), self.cr(0, 2).clone(), zero(),
|
||||
fn to_matrix4(&self) -> Matrix4<S> {
|
||||
Matrix4::new(self.cr(0, 0).clone(), self.cr(0, 1).clone(), self.cr(0, 2).clone(), zero(),
|
||||
self.cr(1, 0).clone(), self.cr(1, 1).clone(), self.cr(1, 2).clone(), zero(),
|
||||
self.cr(2, 0).clone(), self.cr(2, 1).clone(), self.cr(2, 2).clone(), zero(),
|
||||
zero(), zero(), zero(), one())
|
||||
|
@ -676,9 +676,9 @@ ToMat4<S> for Mat3<S> {
|
|||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
ToQuat<S> for Mat3<S> {
|
||||
ToQuaternion<S> for Matrix3<S> {
|
||||
/// Convert the matrix to a quaternion
|
||||
fn to_quat(&self) -> Quat<S> {
|
||||
fn to_quaternion(&self) -> Quaternion<S> {
|
||||
// http://www.cs.ucr.edu/~vbz/resources/quatut.pdf
|
||||
let trace = self.trace();
|
||||
let half: S = cast(0.5).unwrap();
|
||||
|
@ -690,7 +690,7 @@ ToQuat<S> for Mat3<S> {
|
|||
let x = (*self.cr(1, 2) - *self.cr(2, 1)) * s;
|
||||
let y = (*self.cr(2, 0) - *self.cr(0, 2)) * s;
|
||||
let z = (*self.cr(0, 1) - *self.cr(1, 0)) * s;
|
||||
Quat::new(w, x, y, z)
|
||||
Quaternion::new(w, x, y, z)
|
||||
}
|
||||
() if (*self.cr(0, 0) > *self.cr(1, 1)) && (*self.cr(0, 0) > *self.cr(2, 2)) => {
|
||||
let s = (half + (*self.cr(0, 0) - *self.cr(1, 1) - *self.cr(2, 2))).sqrt();
|
||||
|
@ -699,7 +699,7 @@ ToQuat<S> for Mat3<S> {
|
|||
let x = (*self.cr(0, 1) - *self.cr(1, 0)) * s;
|
||||
let y = (*self.cr(2, 0) - *self.cr(0, 2)) * s;
|
||||
let z = (*self.cr(1, 2) - *self.cr(2, 1)) * s;
|
||||
Quat::new(w, x, y, z)
|
||||
Quaternion::new(w, x, y, z)
|
||||
}
|
||||
() if *self.cr(1, 1) > *self.cr(2, 2) => {
|
||||
let s = (half + (*self.cr(1, 1) - *self.cr(0, 0) - *self.cr(2, 2))).sqrt();
|
||||
|
@ -708,7 +708,7 @@ ToQuat<S> for Mat3<S> {
|
|||
let x = (*self.cr(0, 1) - *self.cr(1, 0)) * s;
|
||||
let y = (*self.cr(1, 2) - *self.cr(2, 1)) * s;
|
||||
let z = (*self.cr(2, 0) - *self.cr(0, 2)) * s;
|
||||
Quat::new(w, x, y, z)
|
||||
Quaternion::new(w, x, y, z)
|
||||
}
|
||||
() => {
|
||||
let s = (half + (*self.cr(2, 2) - *self.cr(0, 0) - *self.cr(1, 1))).sqrt();
|
||||
|
@ -717,13 +717,13 @@ ToQuat<S> for Mat3<S> {
|
|||
let x = (*self.cr(2, 0) - *self.cr(0, 2)) * s;
|
||||
let y = (*self.cr(1, 2) - *self.cr(2, 1)) * s;
|
||||
let z = (*self.cr(0, 1) - *self.cr(1, 0)) * s;
|
||||
Quat::new(w, x, y, z)
|
||||
Quaternion::new(w, x, y, z)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S> + fmt::Show> fmt::Show for Mat2<S> {
|
||||
impl<S: PartOrdFloat<S> + fmt::Show> fmt::Show for Matrix2<S> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f.buf, "[[{}, {}], [{}, {}]]",
|
||||
self.cr(0, 0), self.cr(0, 1),
|
||||
|
@ -731,7 +731,7 @@ impl<S: PartOrdFloat<S> + fmt::Show> fmt::Show for Mat2<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S> + fmt::Show> fmt::Show for Mat3<S> {
|
||||
impl<S: PartOrdFloat<S> + fmt::Show> fmt::Show for Matrix3<S> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f.buf, "[[{}, {}, {}], [{}, {}, {}], [{}, {}, {}]]",
|
||||
self.cr(0, 0), self.cr(0, 1), self.cr(0, 2),
|
||||
|
@ -740,7 +740,7 @@ impl<S: PartOrdFloat<S> + fmt::Show> fmt::Show for Mat3<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S> + fmt::Show> fmt::Show for Mat4<S> {
|
||||
impl<S: PartOrdFloat<S> + fmt::Show> fmt::Show for Matrix4<S> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f.buf, "[[{}, {}, {}, {}], [{}, {}, {}, {}], [{}, {}, {}, {}], [{}, {}, {}, {}]]",
|
||||
self.cr(0, 0), self.cr(0, 1), self.cr(0, 2), self.cr(0, 3),
|
||||
|
|
|
@ -16,18 +16,18 @@
|
|||
//! Oriented bounding boxes
|
||||
|
||||
use point::{Point2, Point3};
|
||||
use vector::{Vec2, Vec3};
|
||||
use vector::{Vector2, Vector3};
|
||||
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct Obb2<S> {
|
||||
pub center: Point2<S>,
|
||||
pub axis: Vec2<S>,
|
||||
pub extents: Vec2<S>,
|
||||
pub axis: Vector2<S>,
|
||||
pub extents: Vector2<S>,
|
||||
}
|
||||
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct Obb3<S> {
|
||||
pub center: Point3<S>,
|
||||
pub axis: Vec3<S>,
|
||||
pub extents: Vec3<S>,
|
||||
pub axis: Vector3<S>,
|
||||
pub extents: Vector3<S>,
|
||||
}
|
||||
|
|
|
@ -21,12 +21,12 @@ use approx::ApproxEq;
|
|||
use intersect::Intersect;
|
||||
use point::{Point, Point3};
|
||||
use ray::Ray3;
|
||||
use vector::{Vec3, Vec4};
|
||||
use vector::{Vector3, Vector4};
|
||||
use vector::{Vector, EuclideanVector};
|
||||
use partial_ord::PartOrdFloat;
|
||||
|
||||
|
||||
/// A 3-dimendional plane formed from the equation: `a*x + b*y + c*z - d = 0`.
|
||||
/// A 3-dimensional plane formed from the equation: `a*x + b*y + c*z - d = 0`.
|
||||
///
|
||||
/// # Fields
|
||||
///
|
||||
|
@ -43,14 +43,14 @@ use partial_ord::PartOrdFloat;
|
|||
/// superfluous negations (see _Real Time Collision Detection_, p. 55).
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct Plane<S> {
|
||||
pub n: Vec3<S>,
|
||||
pub n: Vector3<S>,
|
||||
pub d: S,
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Plane<S> {
|
||||
/// Construct a plane from a normal vector and a scalar distance
|
||||
pub fn new(n: Vec3<S>, d: S) -> Plane<S> {
|
||||
pub fn new(n: Vector3<S>, d: S) -> Plane<S> {
|
||||
Plane { n: n, d: d }
|
||||
}
|
||||
|
||||
|
@ -61,11 +61,11 @@ Plane<S> {
|
|||
/// - `c`: the `z` component of the normal
|
||||
/// - `d`: the plane's distance value
|
||||
pub fn from_abcd(a: S, b: S, c: S, d: S) -> Plane<S> {
|
||||
Plane { n: Vec3::new(a, b, c), d: d }
|
||||
Plane { n: Vector3::new(a, b, c), d: d }
|
||||
}
|
||||
|
||||
/// Construct a plane from the components of a four-dimensional vector
|
||||
pub fn from_vec4(v: Vec4<S>) -> Plane<S> {
|
||||
pub fn from_vector4(v: Vector4<S>) -> Plane<S> {
|
||||
unsafe { transmute(v) }
|
||||
}
|
||||
|
||||
|
@ -78,7 +78,7 @@ Plane<S> {
|
|||
// find the normal vector that is perpendicular to v1 and v2
|
||||
let mut n = v0.cross(&v1);
|
||||
|
||||
if n.approx_eq(&Vec3::zero()) { None }
|
||||
if n.approx_eq(&Vector3::zero()) { None }
|
||||
else {
|
||||
// compute the normal and the distance to the plane
|
||||
n.normalize_self();
|
||||
|
|
|
@ -49,14 +49,14 @@ impl<S: Num> Point3<S> {
|
|||
|
||||
impl<S: PartOrdPrim> Point3<S> {
|
||||
#[inline]
|
||||
pub fn from_homogeneous(v: &Vec4<S>) -> Point3<S> {
|
||||
pub fn from_homogeneous(v: &Vector4<S>) -> Point3<S> {
|
||||
let e = v.truncate().mul_s(one::<S>() / v.w);
|
||||
Point3::new(e.x.clone(), e.y.clone(), e.z.clone()) //FIXME
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn to_homogeneous(&self) -> Vec4<S> {
|
||||
Vec4::new(self.x.clone(), self.y.clone(), self.z.clone(), one())
|
||||
pub fn to_homogeneous(&self) -> Vector4<S> {
|
||||
Vector4::new(self.x.clone(), self.y.clone(), self.z.clone(), one())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,8 +97,8 @@ pub trait Point
|
|||
array!(impl<S> Point2<S> -> [S, ..2] _2)
|
||||
array!(impl<S> Point3<S> -> [S, ..3] _3)
|
||||
|
||||
impl<S: PartOrdPrim> Point<S, Vec2<S>, [S, ..2]> for Point2<S> {}
|
||||
impl<S: PartOrdPrim> Point<S, Vec3<S>, [S, ..3]> for Point3<S> {}
|
||||
impl<S: PartOrdPrim> Point<S, Vector2<S>, [S, ..2]> for Point2<S> {}
|
||||
impl<S: PartOrdPrim> Point<S, Vector3<S>, [S, ..3]> for Point3<S> {}
|
||||
|
||||
impl<S: fmt::Show> fmt::Show for Point2<S> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
|
|
|
@ -17,7 +17,7 @@ use std::num::{zero, one, cast};
|
|||
|
||||
use angle::{Angle, tan, cot};
|
||||
use frustum::Frustum;
|
||||
use matrix::{Mat4, ToMat4};
|
||||
use matrix::{Matrix4, ToMatrix4};
|
||||
use plane::Plane;
|
||||
use partial_ord::PartOrdFloat;
|
||||
|
||||
|
@ -25,20 +25,20 @@ use partial_ord::PartOrdFloat;
|
|||
///
|
||||
/// This is the equivalent to the [gluPerspective]
|
||||
/// (http://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml) function.
|
||||
pub fn perspective<S: Float, A: Angle<S>>(fovy: A, aspect: S, near: S, far: S) -> Mat4<S> {
|
||||
pub fn perspective<S: Float, A: Angle<S>>(fovy: A, aspect: S, near: S, far: S) -> Matrix4<S> {
|
||||
PerspectiveFov {
|
||||
fovy: fovy,
|
||||
aspect: aspect,
|
||||
near: near,
|
||||
far: far,
|
||||
}.to_mat4()
|
||||
}.to_matrix4()
|
||||
}
|
||||
|
||||
/// Create a perspective matrix from a view frustrum.
|
||||
///
|
||||
/// This is the equivalent of the now deprecated [glFrustrum]
|
||||
/// (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml) function.
|
||||
pub fn frustum<S: Float>(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Mat4<S> {
|
||||
pub fn frustum<S: Float>(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Matrix4<S> {
|
||||
Perspective {
|
||||
left: left,
|
||||
right: right,
|
||||
|
@ -46,14 +46,14 @@ pub fn frustum<S: Float>(left: S, right: S, bottom: S, top: S, near: S, far: S)
|
|||
top: top,
|
||||
near: near,
|
||||
far: far,
|
||||
}.to_mat4()
|
||||
}.to_matrix4()
|
||||
}
|
||||
|
||||
/// Create an orthographic projection matrix.
|
||||
///
|
||||
/// This is the equivalent of the now deprecated [glOrtho]
|
||||
/// (http://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml) function.
|
||||
pub fn ortho<S: Float>(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Mat4<S> {
|
||||
pub fn ortho<S: Float>(left: S, right: S, bottom: S, top: S, near: S, far: S) -> Matrix4<S> {
|
||||
Ortho {
|
||||
left: left,
|
||||
right: right,
|
||||
|
@ -61,10 +61,10 @@ pub fn ortho<S: Float>(left: S, right: S, bottom: S, top: S, near: S, far: S) ->
|
|||
top: top,
|
||||
near: near,
|
||||
far: far,
|
||||
}.to_mat4()
|
||||
}.to_matrix4()
|
||||
}
|
||||
|
||||
pub trait Projection<S>: ToMat4<S> {
|
||||
pub trait Projection<S>: ToMatrix4<S> {
|
||||
fn to_frustum(&self) -> Frustum<S>;
|
||||
}
|
||||
|
||||
|
@ -98,12 +98,12 @@ impl<S: PartOrdFloat<S>, A: Angle<S>>
|
|||
Projection<S> for PerspectiveFov<S, A> {
|
||||
fn to_frustum(&self) -> Frustum<S> {
|
||||
// TODO: Could this be faster?
|
||||
Frustum::from_mat4(self.to_mat4())
|
||||
Frustum::from_matrix4(self.to_matrix4())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Float, A: Angle<S>> ToMat4<S> for PerspectiveFov<S, A> {
|
||||
fn to_mat4(&self) -> Mat4<S> {
|
||||
impl<S: Float, A: Angle<S>> ToMatrix4<S> for PerspectiveFov<S, A> {
|
||||
fn to_matrix4(&self) -> Matrix4<S> {
|
||||
let half_turn: A = Angle::turn_div_2();
|
||||
|
||||
assert!(self.fovy > zero(), "The vertical field of view cannot be below zero, found: {:?}", self.fovy);
|
||||
|
@ -136,7 +136,7 @@ impl<S: Float, A: Angle<S>> ToMat4<S> for PerspectiveFov<S, A> {
|
|||
let c3r2 = (two * self.far * self.near) / (self.near - self.far);
|
||||
let c3r3 = zero();
|
||||
|
||||
Mat4::new(c0r0, c0r1, c0r2, c0r3,
|
||||
Matrix4::new(c0r0, c0r1, c0r2, c0r3,
|
||||
c1r0, c1r1, c1r2, c1r3,
|
||||
c2r0, c2r1, c2r2, c2r3,
|
||||
c3r0, c3r1, c3r2, c3r3)
|
||||
|
@ -155,12 +155,12 @@ impl<S: PartOrdFloat<S>>
|
|||
Projection<S> for Perspective<S> {
|
||||
fn to_frustum(&self) -> Frustum<S> {
|
||||
// TODO: Could this be faster?
|
||||
Frustum::from_mat4(self.to_mat4())
|
||||
Frustum::from_matrix4(self.to_matrix4())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Float> ToMat4<S> for Perspective<S> {
|
||||
fn to_mat4(&self) -> Mat4<S> {
|
||||
impl<S: Float> ToMatrix4<S> for Perspective<S> {
|
||||
fn to_matrix4(&self) -> Matrix4<S> {
|
||||
assert!(self.left > self.right, "`left` cannot be greater than `right`, found: left: {:?} right: {:?}", self.left, self.right);
|
||||
assert!(self.bottom > self.top, "`bottom` cannot be greater than `top`, found: bottom: {:?} top: {:?}", self.bottom, self.top);
|
||||
assert!(self.near > self.far, "`near` cannot be greater than `far`, found: near: {:?} far: {:?}", self.near, self.far);
|
||||
|
@ -187,7 +187,7 @@ impl<S: Float> ToMat4<S> for Perspective<S> {
|
|||
let c3r2 = -(two * self.far * self.near) / (self.far - self.near);
|
||||
let c3r3 = zero();
|
||||
|
||||
Mat4::new(c0r0, c0r1, c0r2, c0r3,
|
||||
Matrix4::new(c0r0, c0r1, c0r2, c0r3,
|
||||
c1r0, c1r1, c1r2, c1r3,
|
||||
c2r0, c2r1, c2r2, c2r3,
|
||||
c3r0, c3r1, c3r2, c3r3)
|
||||
|
@ -216,8 +216,8 @@ Projection<S> for Ortho<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: Float> ToMat4<S> for Ortho<S> {
|
||||
fn to_mat4(&self) -> Mat4<S> {
|
||||
impl<S: Float> ToMatrix4<S> for Ortho<S> {
|
||||
fn to_matrix4(&self) -> Matrix4<S> {
|
||||
assert!(self.left < self.right, "`left` cannot be greater than `right`, found: left: {:?} right: {:?}", self.left, self.right);
|
||||
assert!(self.bottom < self.top, "`bottom` cannot be greater than `top`, found: bottom: {:?} top: {:?}", self.bottom, self.top);
|
||||
assert!(self.near < self.far, "`near` cannot be greater than `far`, found: near: {:?} far: {:?}", self.near, self.far);
|
||||
|
@ -244,7 +244,7 @@ impl<S: Float> ToMat4<S> for Ortho<S> {
|
|||
let c3r2 = -(self.far + self.near) / (self.far - self.near);
|
||||
let c3r3 = one::<S>();
|
||||
|
||||
Mat4::new(c0r0, c0r1, c0r2, c0r3,
|
||||
Matrix4::new(c0r0, c0r1, c0r2, c0r3,
|
||||
c1r0, c1r1, c1r2, c1r3,
|
||||
c2r0, c2r1, c2r2, c2r3,
|
||||
c3r0, c3r1, c3r2, c3r3)
|
||||
|
|
|
@ -14,24 +14,24 @@
|
|||
// limitations under the License.
|
||||
|
||||
use array::Array;
|
||||
use matrix::{Mat2, Mat3, Mat4};
|
||||
use matrix::{Matrix2, Matrix3, Matrix4};
|
||||
use point::{Point2, Point3};
|
||||
use vector::{Vec2, Vec3, Vec4};
|
||||
use vector::{Vector2, Vector3, Vector4};
|
||||
|
||||
pub trait Ptr<T> {
|
||||
fn ptr<'a>(&'a self) -> &'a T;
|
||||
}
|
||||
|
||||
impl<S: Clone> Ptr<S> for Vec2<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } }
|
||||
impl<S: Clone> Ptr<S> for Vec3<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } }
|
||||
impl<S: Clone> Ptr<S> for Vec4<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } }
|
||||
impl<S: Clone> Ptr<S> for Vector2<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } }
|
||||
impl<S: Clone> Ptr<S> for Vector3<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } }
|
||||
impl<S: Clone> Ptr<S> for Vector4<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } }
|
||||
|
||||
impl<S: Clone> Ptr<S> for Point2<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } }
|
||||
impl<S: Clone> Ptr<S> for Point3<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0) } }
|
||||
|
||||
impl<S: Clone> Ptr<S> for Mat2<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0).i(0) } }
|
||||
impl<S: Clone> Ptr<S> for Mat3<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0).i(0) } }
|
||||
impl<S: Clone> Ptr<S> for Mat4<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0).i(0) } }
|
||||
impl<S: Clone> Ptr<S> for Matrix2<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0).i(0) } }
|
||||
impl<S: Clone> Ptr<S> for Matrix3<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0).i(0) } }
|
||||
impl<S: Clone> Ptr<S> for Matrix4<S> { #[inline] fn ptr<'a>(&'a self) -> &'a S { self.i(0).i(0) } }
|
||||
|
||||
impl<'a, T, P: Ptr<T>> Ptr<T> for &'a [P] {
|
||||
#[inline] fn ptr<'a>(&'a self) -> &'a T { self[0].ptr() }
|
||||
|
|
|
@ -19,83 +19,83 @@ use std::num::{zero, one, cast};
|
|||
use angle::{Angle, Rad, acos, sin, sin_cos};
|
||||
use approx::ApproxEq;
|
||||
use array::{Array, build};
|
||||
use matrix::{Mat3, ToMat3, ToMat4, Mat4};
|
||||
use matrix::{Matrix3, ToMatrix3, ToMatrix4, Matrix4};
|
||||
use point::{Point3};
|
||||
use rotation::{Rotation, Rotation3, Basis3, ToBasis3};
|
||||
use vector::{Vec3, Vector, EuclideanVector};
|
||||
use vector::{Vector3, Vector, EuclideanVector};
|
||||
use partial_ord::PartOrdFloat;
|
||||
|
||||
/// A quaternion in scalar/vector form
|
||||
#[deriving(Clone, Eq)]
|
||||
pub struct Quat<S> { pub s: S, pub v: Vec3<S> }
|
||||
pub struct Quaternion<S> { pub s: S, pub v: Vector3<S> }
|
||||
|
||||
array!(impl<S> Quat<S> -> [S, ..4] _4)
|
||||
array!(impl<S> Quaternion<S> -> [S, ..4] _4)
|
||||
|
||||
pub trait ToQuat<S: Float> {
|
||||
fn to_quat(&self) -> Quat<S>;
|
||||
pub trait ToQuaternion<S: Float> {
|
||||
fn to_quaternion(&self) -> Quaternion<S>;
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Quat<S> {
|
||||
Quaternion<S> {
|
||||
/// Construct a new quaternion from one scalar component and three
|
||||
/// imaginary components
|
||||
#[inline]
|
||||
pub fn new(w: S, xi: S, yj: S, zk: S) -> Quat<S> {
|
||||
Quat::from_sv(w, Vec3::new(xi, yj, zk))
|
||||
pub fn new(w: S, xi: S, yj: S, zk: S) -> Quaternion<S> {
|
||||
Quaternion::from_sv(w, Vector3::new(xi, yj, zk))
|
||||
}
|
||||
|
||||
/// Construct a new quaternion from a scalar and a vector
|
||||
#[inline]
|
||||
pub fn from_sv(s: S, v: Vec3<S>) -> Quat<S> {
|
||||
Quat { s: s, v: v }
|
||||
pub fn from_sv(s: S, v: Vector3<S>) -> Quaternion<S> {
|
||||
Quaternion { s: s, v: v }
|
||||
}
|
||||
|
||||
/// The additive identity, ie: `q = 0 + 0i + 0j + 0i`
|
||||
#[inline]
|
||||
pub fn zero() -> Quat<S> {
|
||||
Quat::new(zero(), zero(), zero(), zero())
|
||||
pub fn zero() -> Quaternion<S> {
|
||||
Quaternion::new(zero(), zero(), zero(), zero())
|
||||
}
|
||||
|
||||
/// The multiplicative identity, ie: `q = 1 + 0i + 0j + 0i`
|
||||
#[inline]
|
||||
pub fn identity() -> Quat<S> {
|
||||
Quat::from_sv(one::<S>(), Vec3::zero())
|
||||
pub fn identity() -> Quaternion<S> {
|
||||
Quaternion::from_sv(one::<S>(), Vector3::zero())
|
||||
}
|
||||
|
||||
/// The result of multiplying the quaternion a scalar
|
||||
#[inline]
|
||||
pub fn mul_s(&self, value: S) -> Quat<S> {
|
||||
Quat::from_sv(self.s * value, self.v.mul_s(value))
|
||||
pub fn mul_s(&self, value: S) -> Quaternion<S> {
|
||||
Quaternion::from_sv(self.s * value, self.v.mul_s(value))
|
||||
}
|
||||
|
||||
/// The result of dividing the quaternion a scalar
|
||||
#[inline]
|
||||
pub fn div_s(&self, value: S) -> Quat<S> {
|
||||
Quat::from_sv(self.s / value, self.v.div_s(value))
|
||||
pub fn div_s(&self, value: S) -> Quaternion<S> {
|
||||
Quaternion::from_sv(self.s / value, self.v.div_s(value))
|
||||
}
|
||||
|
||||
/// The result of multiplying the quaternion by a vector
|
||||
#[inline]
|
||||
pub fn mul_v(&self, vec: &Vec3<S>) -> Vec3<S> {
|
||||
pub fn mul_v(&self, vec: &Vector3<S>) -> Vector3<S> {
|
||||
let tmp = self.v.cross(vec).add_v(&vec.mul_s(self.s.clone()));
|
||||
self.v.cross(&tmp).mul_s(cast(2).unwrap()).add_v(vec)
|
||||
}
|
||||
|
||||
/// The sum of this quaternion and `other`
|
||||
#[inline]
|
||||
pub fn add_q(&self, other: &Quat<S>) -> Quat<S> {
|
||||
pub fn add_q(&self, other: &Quaternion<S>) -> Quaternion<S> {
|
||||
build(|i| self.i(i).add(other.i(i)))
|
||||
}
|
||||
|
||||
/// The difference between this quaternion and `other`
|
||||
#[inline]
|
||||
pub fn sub_q(&self, other: &Quat<S>) -> Quat<S> {
|
||||
pub fn sub_q(&self, other: &Quaternion<S>) -> Quaternion<S> {
|
||||
build(|i| self.i(i).add(other.i(i)))
|
||||
}
|
||||
|
||||
/// The the result of multipliplying the quaternion by `other`
|
||||
pub fn mul_q(&self, other: &Quat<S>) -> Quat<S> {
|
||||
Quat::new(self.s * other.s - self.v.x * other.v.x - self.v.y * other.v.y - self.v.z * other.v.z,
|
||||
pub fn mul_q(&self, other: &Quaternion<S>) -> Quaternion<S> {
|
||||
Quaternion::new(self.s * other.s - self.v.x * other.v.x - self.v.y * other.v.y - self.v.z * other.v.z,
|
||||
self.s * other.v.x + self.v.x * other.s + self.v.y * other.v.z - self.v.z * other.v.y,
|
||||
self.s * other.v.y + self.v.y * other.s + self.v.z * other.v.x - self.v.x * other.v.z,
|
||||
self.s * other.v.z + self.v.z * other.s + self.v.x * other.v.y - self.v.y * other.v.x)
|
||||
|
@ -112,30 +112,30 @@ Quat<S> {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn add_self_q(&mut self, other: &Quat<S>) {
|
||||
pub fn add_self_q(&mut self, other: &Quaternion<S>) {
|
||||
self.each_mut(|i, x| *x = x.add(other.i(i)));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn sub_self_q(&mut self, other: &Quat<S>) {
|
||||
pub fn sub_self_q(&mut self, other: &Quaternion<S>) {
|
||||
self.each_mut(|i, x| *x = x.sub(other.i(i)));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn mul_self_q(&mut self, other: &Quat<S>) {
|
||||
pub fn mul_self_q(&mut self, other: &Quaternion<S>) {
|
||||
*self = self.mul_q(other);
|
||||
}
|
||||
|
||||
/// The dot product of the quaternion and `other`
|
||||
#[inline]
|
||||
pub fn dot(&self, other: &Quat<S>) -> S {
|
||||
pub fn dot(&self, other: &Quaternion<S>) -> S {
|
||||
self.s * other.s + self.v.dot(&other.v)
|
||||
}
|
||||
|
||||
/// The conjugate of the quaternion
|
||||
#[inline]
|
||||
pub fn conjugate(&self) -> Quat<S> {
|
||||
Quat::from_sv(self.s.clone(), -self.v.clone())
|
||||
pub fn conjugate(&self) -> Quaternion<S> {
|
||||
Quaternion::from_sv(self.s.clone(), -self.v.clone())
|
||||
}
|
||||
|
||||
/// The squared magnitude of the quaternion. This is useful for
|
||||
|
@ -160,7 +160,7 @@ Quat<S> {
|
|||
|
||||
/// The normalized quaternion
|
||||
#[inline]
|
||||
pub fn normalize(&self) -> Quat<S> {
|
||||
pub fn normalize(&self) -> Quaternion<S> {
|
||||
self.mul_s(one::<S>() / self.magnitude())
|
||||
}
|
||||
|
||||
|
@ -169,13 +169,13 @@ Quat<S> {
|
|||
/// # Return value
|
||||
///
|
||||
/// The intoperlated quaternion
|
||||
pub fn nlerp(&self, other: &Quat<S>, amount: S) -> Quat<S> {
|
||||
pub fn nlerp(&self, other: &Quaternion<S>, amount: S) -> Quaternion<S> {
|
||||
self.mul_s(one::<S>() - amount).add_q(&other.mul_s(amount)).normalize()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Quat<S> {
|
||||
Quaternion<S> {
|
||||
/// Spherical Linear Intoperlation
|
||||
///
|
||||
/// Perform a spherical linear interpolation between the quaternion and
|
||||
|
@ -195,7 +195,7 @@ Quat<S> {
|
|||
/// (http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/)
|
||||
/// - [Arcsynthesis OpenGL tutorial]
|
||||
/// (http://www.arcsynthesis.org/gltut/Positioning/Tut08%20Interpolation.html)
|
||||
pub fn slerp(&self, other: &Quat<S>, amount: S) -> Quat<S> {
|
||||
pub fn slerp(&self, other: &Quaternion<S>, amount: S) -> Quaternion<S> {
|
||||
use std::num::cast;
|
||||
|
||||
let dot = self.dot(other);
|
||||
|
@ -228,9 +228,9 @@ Quat<S> {
|
|||
}
|
||||
|
||||
impl<S: Float + ApproxEq<S>>
|
||||
ToMat3<S> for Quat<S> {
|
||||
ToMatrix3<S> for Quaternion<S> {
|
||||
/// Convert the quaternion to a 3 x 3 rotation matrix
|
||||
fn to_mat3(&self) -> Mat3<S> {
|
||||
fn to_matrix3(&self) -> Matrix3<S> {
|
||||
let x2 = self.v.x + self.v.x;
|
||||
let y2 = self.v.y + self.v.y;
|
||||
let z2 = self.v.z + self.v.z;
|
||||
|
@ -247,16 +247,16 @@ ToMat3<S> for Quat<S> {
|
|||
let sz2 = z2 * self.s;
|
||||
let sx2 = x2 * self.s;
|
||||
|
||||
Mat3::new(one::<S>() - yy2 - zz2, xy2 + sz2, xz2 - sy2,
|
||||
Matrix3::new(one::<S>() - yy2 - zz2, xy2 + sz2, xz2 - sy2,
|
||||
xy2 - sz2, one::<S>() - xx2 - zz2, yz2 + sx2,
|
||||
xz2 + sy2, yz2 - sx2, one::<S>() - xx2 - yy2)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Float + ApproxEq<S>>
|
||||
ToMat4<S> for Quat<S> {
|
||||
ToMatrix4<S> for Quaternion<S> {
|
||||
/// Convert the quaternion to a 4 x 4 rotation matrix
|
||||
fn to_mat4(&self) -> Mat4<S> {
|
||||
fn to_matrix4(&self) -> Matrix4<S> {
|
||||
let x2 = self.v.x + self.v.x;
|
||||
let y2 = self.v.y + self.v.y;
|
||||
let z2 = self.v.z + self.v.z;
|
||||
|
@ -273,7 +273,7 @@ ToMat4<S> for Quat<S> {
|
|||
let sz2 = z2 * self.s;
|
||||
let sx2 = x2 * self.s;
|
||||
|
||||
Mat4::new(one::<S>() - yy2 - zz2, xy2 + sz2, xz2 - sy2, zero::<S>(),
|
||||
Matrix4::new(one::<S>() - yy2 - zz2, xy2 + sz2, xz2 - sy2, zero::<S>(),
|
||||
xy2 - sz2, one::<S>() - xx2 - zz2, yz2 + sx2, zero::<S>(),
|
||||
xz2 + sy2, yz2 - sx2, one::<S>() - xx2 - yy2, zero::<S>(),
|
||||
zero::<S>(), zero::<S>(), zero::<S>(), one::<S>())
|
||||
|
@ -281,14 +281,14 @@ ToMat4<S> for Quat<S> {
|
|||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Neg<Quat<S>> for Quat<S> {
|
||||
Neg<Quaternion<S>> for Quaternion<S> {
|
||||
#[inline]
|
||||
fn neg(&self) -> Quat<S> {
|
||||
Quat::from_sv(-self.s, -self.v)
|
||||
fn neg(&self) -> Quaternion<S> {
|
||||
Quaternion::from_sv(-self.s, -self.v)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: fmt::Show> fmt::Show for Quat<S> {
|
||||
impl<S: fmt::Show> fmt::Show for Quaternion<S> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f.buf, "{} + {}i + {}j + {}k",
|
||||
self.s,
|
||||
|
@ -301,65 +301,65 @@ impl<S: fmt::Show> fmt::Show for Quat<S> {
|
|||
// Quaternion Rotation impls
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
ToBasis3<S> for Quat<S> {
|
||||
ToBasis3<S> for Quaternion<S> {
|
||||
#[inline]
|
||||
fn to_rot3(&self) -> Basis3<S> { Basis3::from_quat(self) }
|
||||
fn to_rot3(&self) -> Basis3<S> { Basis3::from_quaternion(self) }
|
||||
}
|
||||
|
||||
impl<S: Float> ToQuat<S> for Quat<S> {
|
||||
impl<S: Float> ToQuaternion<S> for Quaternion<S> {
|
||||
#[inline]
|
||||
fn to_quat(&self) -> Quat<S> { self.clone() }
|
||||
fn to_quaternion(&self) -> Quaternion<S> { self.clone() }
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Quat<S> {
|
||||
Rotation<S, [S, ..3], Vector3<S>, Point3<S>> for Quaternion<S> {
|
||||
#[inline]
|
||||
fn identity() -> Quat<S> { Quat::identity() }
|
||||
fn identity() -> Quaternion<S> { Quaternion::identity() }
|
||||
|
||||
#[inline]
|
||||
fn look_at(dir: &Vec3<S>, up: &Vec3<S>) -> Quat<S> {
|
||||
Mat3::look_at(dir, up).to_quat()
|
||||
fn look_at(dir: &Vector3<S>, up: &Vector3<S>) -> Quaternion<S> {
|
||||
Matrix3::look_at(dir, up).to_quaternion()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn between_vecs(a: &Vec3<S>, b: &Vec3<S>) -> Quat<S> {
|
||||
fn between_vectors(a: &Vector3<S>, b: &Vector3<S>) -> Quaternion<S> {
|
||||
//http://stackoverflow.com/questions/1171849/
|
||||
//finding-quaternion-representing-the-rotation-from-one-vector-to-another
|
||||
Quat::from_sv(one::<S>() + a.dot(b), a.cross(b)).normalize()
|
||||
Quaternion::from_sv(one::<S>() + a.dot(b), a.cross(b)).normalize()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rotate_vec(&self, vec: &Vec3<S>) -> Vec3<S> { self.mul_v(vec) }
|
||||
fn rotate_vector(&self, vec: &Vector3<S>) -> Vector3<S> { self.mul_v(vec) }
|
||||
|
||||
#[inline]
|
||||
fn concat(&self, other: &Quat<S>) -> Quat<S> { self.mul_q(other) }
|
||||
fn concat(&self, other: &Quaternion<S>) -> Quaternion<S> { self.mul_q(other) }
|
||||
|
||||
#[inline]
|
||||
fn concat_self(&mut self, other: &Quat<S>) { self.mul_self_q(other); }
|
||||
fn concat_self(&mut self, other: &Quaternion<S>) { self.mul_self_q(other); }
|
||||
|
||||
#[inline]
|
||||
fn invert(&self) -> Quat<S> { self.conjugate().div_s(self.magnitude2()) }
|
||||
fn invert(&self) -> Quaternion<S> { self.conjugate().div_s(self.magnitude2()) }
|
||||
|
||||
#[inline]
|
||||
fn invert_self(&mut self) { *self = self.invert() }
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Rotation3<S> for Quat<S>
|
||||
Rotation3<S> for Quaternion<S>
|
||||
{
|
||||
#[inline]
|
||||
fn from_axis_angle(axis: &Vec3<S>, angle: Rad<S>) -> Quat<S> {
|
||||
fn from_axis_angle(axis: &Vector3<S>, angle: Rad<S>) -> Quaternion<S> {
|
||||
let (s, c) = sin_cos(angle.mul_s(cast(0.5).unwrap()));
|
||||
Quat::from_sv(c, axis.mul_s(s))
|
||||
Quaternion::from_sv(c, axis.mul_s(s))
|
||||
}
|
||||
|
||||
fn from_euler(x: Rad<S>, y: Rad<S>, z: Rad<S>) -> Quat<S> {
|
||||
fn from_euler(x: Rad<S>, y: Rad<S>, z: Rad<S>) -> Quaternion<S> {
|
||||
// http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Conversion
|
||||
let (sx2, cx2) = sin_cos(x.mul_s(cast(0.5).unwrap()));
|
||||
let (sy2, cy2) = sin_cos(y.mul_s(cast(0.5).unwrap()));
|
||||
let (sz2, cz2) = sin_cos(z.mul_s(cast(0.5).unwrap()));
|
||||
|
||||
Quat::new(cz2 * cx2 * cy2 + sz2 * sx2 * sy2,
|
||||
Quaternion::new(cz2 * cx2 * cy2 + sz2 * sx2 * sy2,
|
||||
sz2 * cx2 * cy2 - cz2 * sx2 * sy2,
|
||||
cz2 * sx2 * cy2 + sz2 * cx2 * sy2,
|
||||
cz2 * cx2 * sy2 - sz2 * sx2 * cy2)
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
// limitations under the License.
|
||||
|
||||
use point::{Point, Point2, Point3};
|
||||
use vector::{Vector, Vec2, Vec3};
|
||||
use vector::{Vector, Vector2, Vector3};
|
||||
|
||||
/// A generic ray
|
||||
#[deriving(Clone, Eq)]
|
||||
|
@ -37,5 +37,5 @@ impl
|
|||
}
|
||||
}
|
||||
|
||||
pub type Ray2<S> = Ray<Point2<S>,Vec2<S>>;
|
||||
pub type Ray3<S> = Ray<Point3<S>,Vec3<S>>;
|
||||
pub type Ray2<S> = Ray<Point2<S>,Vector2<S>>;
|
||||
pub type Ray3<S> = Ray<Point3<S>,Vector3<S>>;
|
||||
|
|
|
@ -17,12 +17,12 @@ use angle::{Rad, acos};
|
|||
use approx::ApproxEq;
|
||||
use array::Array;
|
||||
use matrix::Matrix;
|
||||
use matrix::{Mat2, ToMat2};
|
||||
use matrix::{Mat3, ToMat3};
|
||||
use matrix::{Matrix2, ToMatrix2};
|
||||
use matrix::{Matrix3, ToMatrix3};
|
||||
use point::{Point, Point2, Point3};
|
||||
use quaternion::{Quat, ToQuat};
|
||||
use quaternion::{Quaternion, ToQuaternion};
|
||||
use ray::Ray;
|
||||
use vector::{Vector, Vec2, Vec3};
|
||||
use vector::{Vector, Vector2, Vector3};
|
||||
use partial_ord::{PartOrdPrim, PartOrdFloat};
|
||||
|
||||
/// A trait for generic rotation
|
||||
|
@ -43,20 +43,20 @@ pub trait Rotation
|
|||
|
||||
/// Create a shortest rotation to transform vector 'a' into 'b'.
|
||||
/// Both given vectors are assumed to have unit length.
|
||||
fn between_vecs(a: &V, b: &V) -> Self;
|
||||
fn between_vectors(a: &V, b: &V) -> Self;
|
||||
|
||||
fn rotate_vec(&self, vec: &V) -> V;
|
||||
fn rotate_vector(&self, vec: &V) -> V;
|
||||
|
||||
#[inline]
|
||||
fn rotate_point(&self, point: &P) -> P {
|
||||
Point::from_vec( &self.rotate_vec( &point.to_vec() ) )
|
||||
Point::from_vec( &self.rotate_vector( &point.to_vec() ) )
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rotate_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> {
|
||||
Ray::new( //FIXME: use clone derived from Array
|
||||
Array::build(|i| ray.origin.i(i).clone()),
|
||||
self.rotate_vec(&ray.direction) )
|
||||
self.rotate_vector(&ray.direction) )
|
||||
}
|
||||
|
||||
fn concat(&self, other: &Self) -> Self;
|
||||
|
@ -78,8 +78,8 @@ pub trait Rotation2
|
|||
<
|
||||
S
|
||||
>
|
||||
: Rotation<S, [S, ..2], Vec2<S>, Point2<S>>
|
||||
+ ToMat2<S>
|
||||
: Rotation<S, [S, ..2], Vector2<S>, Point2<S>>
|
||||
+ ToMatrix2<S>
|
||||
+ ToBasis2<S>
|
||||
{
|
||||
// Create a rotation by a given angle. Thus is a redundant case of both
|
||||
|
@ -92,13 +92,13 @@ pub trait Rotation3
|
|||
<
|
||||
S: Primitive
|
||||
>
|
||||
: Rotation<S, [S, ..3], Vec3<S>, Point3<S>>
|
||||
+ ToMat3<S>
|
||||
: Rotation<S, [S, ..3], Vector3<S>, Point3<S>>
|
||||
+ ToMatrix3<S>
|
||||
+ ToBasis3<S>
|
||||
+ ToQuat<S>
|
||||
+ ToQuaternion<S>
|
||||
{
|
||||
/// Create a rotation around a given axis.
|
||||
fn from_axis_angle(axis: &Vec3<S>, angle: Rad<S>) -> Self;
|
||||
fn from_axis_angle(axis: &Vector3<S>, angle: Rad<S>) -> Self;
|
||||
|
||||
/// Create a rotation from a set of euler angles.
|
||||
///
|
||||
|
@ -112,17 +112,17 @@ pub trait Rotation3
|
|||
/// Create a rotation matrix from a rotation around the `x` axis (pitch).
|
||||
#[inline]
|
||||
fn from_angle_x(theta: Rad<S>) -> Self {
|
||||
Rotation3::from_axis_angle( &Vec3::unit_x(), theta )
|
||||
Rotation3::from_axis_angle( &Vector3::unit_x(), theta )
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_angle_y(theta: Rad<S>) -> Self {
|
||||
Rotation3::from_axis_angle( &Vec3::unit_y(), theta )
|
||||
Rotation3::from_axis_angle( &Vector3::unit_y(), theta )
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_angle_z(theta: Rad<S>) -> Self {
|
||||
Rotation3::from_axis_angle( &Vec3::unit_z(), theta )
|
||||
Rotation3::from_axis_angle( &Vector3::unit_z(), theta )
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,17 +130,17 @@ pub trait Rotation3
|
|||
/// A two-dimensional rotation matrix.
|
||||
///
|
||||
/// The matrix is guaranteed to be orthogonal, so some operations can be
|
||||
/// implemented more efficiently than the implementations for `math::Mat2`. To
|
||||
/// implemented more efficiently than the implementations for `math::Matrix2`. To
|
||||
/// enforce orthogonality at the type level the operations have been restricted
|
||||
/// to a subset of those implemented on `Mat2`.
|
||||
/// to a subset of those implemented on `Matrix2`.
|
||||
#[deriving(Eq, Clone)]
|
||||
pub struct Basis2<S> {
|
||||
mat: Mat2<S>
|
||||
mat: Matrix2<S>
|
||||
}
|
||||
|
||||
impl<S: Float> Basis2<S> {
|
||||
#[inline]
|
||||
pub fn as_mat2<'a>(&'a self) -> &'a Mat2<S> { &'a self.mat }
|
||||
pub fn as_matrix2<'a>(&'a self) -> &'a Matrix2<S> { &'a self.mat }
|
||||
}
|
||||
|
||||
pub trait ToBasis2<S: Float> {
|
||||
|
@ -152,28 +152,28 @@ impl<S: Float> ToBasis2<S> for Basis2<S> {
|
|||
fn to_rot2(&self) -> Basis2<S> { self.clone() }
|
||||
}
|
||||
|
||||
impl<S: Float> ToMat2<S> for Basis2<S> {
|
||||
impl<S: Float> ToMatrix2<S> for Basis2<S> {
|
||||
#[inline]
|
||||
fn to_mat2(&self) -> Mat2<S> { self.mat.clone() }
|
||||
fn to_matrix2(&self) -> Matrix2<S> { self.mat.clone() }
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Rotation<S, [S, ..2], Vec2<S>, Point2<S>> for Basis2<S> {
|
||||
Rotation<S, [S, ..2], Vector2<S>, Point2<S>> for Basis2<S> {
|
||||
#[inline]
|
||||
fn identity() -> Basis2<S> { Basis2{ mat: Mat2::identity() } }
|
||||
fn identity() -> Basis2<S> { Basis2{ mat: Matrix2::identity() } }
|
||||
|
||||
#[inline]
|
||||
fn look_at(dir: &Vec2<S>, up: &Vec2<S>) -> Basis2<S> {
|
||||
Basis2 { mat: Mat2::look_at(dir, up) }
|
||||
fn look_at(dir: &Vector2<S>, up: &Vector2<S>) -> Basis2<S> {
|
||||
Basis2 { mat: Matrix2::look_at(dir, up) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn between_vecs(a: &Vec2<S>, b: &Vec2<S>) -> Basis2<S> {
|
||||
fn between_vectors(a: &Vector2<S>, b: &Vector2<S>) -> Basis2<S> {
|
||||
Rotation2::from_angle( acos(a.dot(b)) )
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rotate_vec(&self, vec: &Vec2<S>) -> Vec2<S> { self.mat.mul_v(vec) }
|
||||
fn rotate_vector(&self, vec: &Vector2<S>) -> Vector2<S> { self.mat.mul_v(vec) }
|
||||
|
||||
#[inline]
|
||||
fn concat(&self, other: &Basis2<S>) -> Basis2<S> { Basis2 { mat: self.mat.mul_m(&other.mat) } }
|
||||
|
@ -202,27 +202,29 @@ ApproxEq<S> for Basis2<S> {
|
|||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Rotation2<S> for Basis2<S> {
|
||||
fn from_angle(theta: Rad<S>) -> Basis2<S> { Basis2 { mat: Mat2::from_angle(theta) } }
|
||||
fn from_angle(theta: Rad<S>) -> Basis2<S> { Basis2 { mat: Matrix2::from_angle(theta) } }
|
||||
}
|
||||
|
||||
/// A three-dimensional rotation matrix.
|
||||
///
|
||||
/// The matrix is guaranteed to be orthogonal, so some operations, specifically
|
||||
/// inversion, can be implemented more efficiently than the implementations for
|
||||
/// `math::Mat3`. To ensure orthogonality is maintained, the operations have
|
||||
/// been restricted to a subeset of those implemented on `Mat3`.
|
||||
/// `math::Matrix3`. To ensure orthogonality is maintained, the operations have
|
||||
/// been restricted to a subeset of those implemented on `Matrix3`.
|
||||
#[deriving(Eq, Clone)]
|
||||
pub struct Basis3<S> {
|
||||
mat: Mat3<S>
|
||||
mat: Matrix3<S>
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Basis3<S> {
|
||||
#[inline]
|
||||
pub fn from_quat(quat: &Quat<S>) -> Basis3<S> { Basis3 { mat: quat.to_mat3() } }
|
||||
pub fn from_quaternion(quaternion: &Quaternion<S>) -> Basis3<S> {
|
||||
Basis3 { mat: quaternion.to_matrix3() }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn as_mat3<'a>(&'a self) -> &'a Mat3<S> { &'a self.mat }
|
||||
pub fn as_matrix3<'a>(&'a self) -> &'a Matrix3<S> { &'a self.mat }
|
||||
}
|
||||
|
||||
pub trait ToBasis3<S: Float> {
|
||||
|
@ -234,35 +236,35 @@ impl<S: Float> ToBasis3<S> for Basis3<S> {
|
|||
fn to_rot3(&self) -> Basis3<S> { self.clone() }
|
||||
}
|
||||
|
||||
impl<S: Float> ToMat3<S> for Basis3<S> {
|
||||
impl<S: Float> ToMatrix3<S> for Basis3<S> {
|
||||
#[inline]
|
||||
fn to_mat3(&self) -> Mat3<S> { self.mat.clone() }
|
||||
fn to_matrix3(&self) -> Matrix3<S> { self.mat.clone() }
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
ToQuat<S> for Basis3<S> {
|
||||
ToQuaternion<S> for Basis3<S> {
|
||||
#[inline]
|
||||
fn to_quat(&self) -> Quat<S> { self.mat.to_quat() }
|
||||
fn to_quaternion(&self) -> Quaternion<S> { self.mat.to_quaternion() }
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Rotation<S, [S, ..3], Vec3<S>, Point3<S>> for Basis3<S> {
|
||||
Rotation<S, [S, ..3], Vector3<S>, Point3<S>> for Basis3<S> {
|
||||
#[inline]
|
||||
fn identity() -> Basis3<S> { Basis3{ mat: Mat3::identity() } }
|
||||
fn identity() -> Basis3<S> { Basis3{ mat: Matrix3::identity() } }
|
||||
|
||||
#[inline]
|
||||
fn look_at(dir: &Vec3<S>, up: &Vec3<S>) -> Basis3<S> {
|
||||
Basis3 { mat: Mat3::look_at(dir, up) }
|
||||
fn look_at(dir: &Vector3<S>, up: &Vector3<S>) -> Basis3<S> {
|
||||
Basis3 { mat: Matrix3::look_at(dir, up) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn between_vecs(a: &Vec3<S>, b: &Vec3<S>) -> Basis3<S> {
|
||||
let q: Quat<S> = Rotation::between_vecs(a, b);
|
||||
fn between_vectors(a: &Vector3<S>, b: &Vector3<S>) -> Basis3<S> {
|
||||
let q: Quaternion<S> = Rotation::between_vectors(a, b);
|
||||
q.to_rot3()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn rotate_vec(&self, vec: &Vec3<S>) -> Vec3<S> { self.mat.mul_v(vec) }
|
||||
fn rotate_vector(&self, vec: &Vector3<S>) -> Vector3<S> { self.mat.mul_v(vec) }
|
||||
|
||||
#[inline]
|
||||
fn concat(&self, other: &Basis3<S>) -> Basis3<S> { Basis3 { mat: self.mat.mul_m(&other.mat) } }
|
||||
|
@ -291,23 +293,23 @@ ApproxEq<S> for Basis3<S> {
|
|||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
Rotation3<S> for Basis3<S> {
|
||||
fn from_axis_angle(axis: &Vec3<S>, angle: Rad<S>) -> Basis3<S> {
|
||||
Basis3 { mat: Mat3::from_axis_angle(axis, angle) }
|
||||
fn from_axis_angle(axis: &Vector3<S>, angle: Rad<S>) -> Basis3<S> {
|
||||
Basis3 { mat: Matrix3::from_axis_angle(axis, angle) }
|
||||
}
|
||||
|
||||
fn from_euler(x: Rad<S>, y: Rad<S>, z: Rad<S>) -> Basis3<S> {
|
||||
Basis3 { mat: Mat3::from_euler(x, y ,z) }
|
||||
Basis3 { mat: Matrix3::from_euler(x, y ,z) }
|
||||
}
|
||||
|
||||
fn from_angle_x(theta: Rad<S>) -> Basis3<S> {
|
||||
Basis3 { mat: Mat3::from_angle_x(theta) }
|
||||
Basis3 { mat: Matrix3::from_angle_x(theta) }
|
||||
}
|
||||
|
||||
fn from_angle_y(theta: Rad<S>) -> Basis3<S> {
|
||||
Basis3 { mat: Mat3::from_angle_y(theta) }
|
||||
Basis3 { mat: Matrix3::from_angle_y(theta) }
|
||||
}
|
||||
|
||||
fn from_angle_z(theta: Rad<S>) -> Basis3<S> {
|
||||
Basis3 { mat: Mat3::from_angle_z(theta) }
|
||||
Basis3 { mat: Matrix3::from_angle_z(theta) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,12 +18,12 @@ use std::{fmt,num};
|
|||
use std::num::one;
|
||||
|
||||
use approx::ApproxEq;
|
||||
use matrix::{Matrix, Mat4, ToMat4};
|
||||
use matrix::{Matrix, Matrix4, ToMatrix4};
|
||||
use point::{Point, Point3};
|
||||
use ray::Ray;
|
||||
use rotation::{Rotation, Rotation3};
|
||||
use quaternion::Quat;
|
||||
use vector::{Vector, Vec3};
|
||||
use quaternion::Quaternion;
|
||||
use vector::{Vector, Vector3};
|
||||
use partial_ord::{PartOrdPrim, PartOrdFloat};
|
||||
|
||||
/// A trait of affine transformation, that can be applied to points or vectors
|
||||
|
@ -38,12 +38,12 @@ pub trait Transform
|
|||
fn identity() -> Self;
|
||||
fn look_at(eye: &P, center: &P, up: &V) -> Self;
|
||||
|
||||
fn transform_vec(&self, vec: &V) -> V;
|
||||
fn transform_vector(&self, vec: &V) -> V;
|
||||
fn transform_point(&self, point: &P) -> P;
|
||||
|
||||
#[inline]
|
||||
fn transform_ray(&self, ray: &Ray<P,V>) -> Ray<P,V> {
|
||||
Ray::new( self.transform_point(&ray.origin), self.transform_vec(&ray.direction) )
|
||||
Ray::new( self.transform_point(&ray.origin), self.transform_vector(&ray.direction) )
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -98,7 +98,7 @@ Transform<S, Slice, V, P> for Decomposed<S,V,R> {
|
|||
fn look_at(eye: &P, center: &P, up: &V) -> Decomposed<S,V,R> {
|
||||
let origin :P = Point::origin();
|
||||
let rot :R = Rotation::look_at( ¢er.sub_p(eye), up );
|
||||
let disp :V = rot.rotate_vec( &origin.sub_p(eye) );
|
||||
let disp :V = rot.rotate_vector( &origin.sub_p(eye) );
|
||||
Decomposed {
|
||||
scale: num::one(),
|
||||
rot: rot,
|
||||
|
@ -107,8 +107,8 @@ Transform<S, Slice, V, P> for Decomposed<S,V,R> {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
fn transform_vec(&self, vec: &V) -> V {
|
||||
self.rot.rotate_vec( &vec.mul_s( self.scale.clone() ))
|
||||
fn transform_vector(&self, vec: &V) -> V {
|
||||
self.rot.rotate_vector( &vec.mul_s( self.scale.clone() ))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -131,7 +131,7 @@ Transform<S, Slice, V, P> for Decomposed<S,V,R> {
|
|||
let _1 : S = num::one();
|
||||
let s = _1 / self.scale;
|
||||
let r = self.rot.invert();
|
||||
let d = r.rotate_vec( &self.disp ).mul_s( -s );
|
||||
let d = r.rotate_vector( &self.disp ).mul_s( -s );
|
||||
Some( Decomposed {
|
||||
scale: s,
|
||||
rot: r,
|
||||
|
@ -142,24 +142,24 @@ Transform<S, Slice, V, P> for Decomposed<S,V,R> {
|
|||
}
|
||||
|
||||
pub trait Transform3<S>
|
||||
: Transform<S, [S, ..3], Vec3<S>, Point3<S>>
|
||||
+ ToMat4<S>
|
||||
: Transform<S, [S, ..3], Vector3<S>, Point3<S>>
|
||||
+ ToMatrix4<S>
|
||||
{}
|
||||
|
||||
impl<S: PartOrdFloat<S>, R: Rotation3<S>>
|
||||
ToMat4<S> for Decomposed<S, Vec3<S>, R> {
|
||||
fn to_mat4(&self) -> Mat4<S> {
|
||||
let mut m = self.rot.to_mat3().mul_s( self.scale.clone() ).to_mat4();
|
||||
ToMatrix4<S> for Decomposed<S, Vector3<S>, R> {
|
||||
fn to_matrix4(&self) -> Matrix4<S> {
|
||||
let mut m = self.rot.to_matrix3().mul_s( self.scale.clone() ).to_matrix4();
|
||||
m.w = self.disp.extend( num::one() );
|
||||
m
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>, R: Rotation3<S>>
|
||||
Transform3<S> for Decomposed<S,Vec3<S>,R> {}
|
||||
Transform3<S> for Decomposed<S,Vector3<S>,R> {}
|
||||
|
||||
impl<S: fmt::Show + Float, R: fmt::Show + Rotation3<S>>
|
||||
fmt::Show for Decomposed<S,Vec3<S>,R> {
|
||||
fmt::Show for Decomposed<S,Vector3<S>,R> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f.buf, "(scale({}), rot({}), disp{})",
|
||||
self.scale, self.rot, self.disp)
|
||||
|
@ -169,23 +169,23 @@ fmt::Show for Decomposed<S,Vec3<S>,R> {
|
|||
|
||||
/// A homogeneous transformation matrix.
|
||||
pub struct AffineMatrix3<S> {
|
||||
pub mat: Mat4<S>,
|
||||
pub mat: Matrix4<S>,
|
||||
}
|
||||
|
||||
impl<S : PartOrdFloat<S>>
|
||||
Transform<S, [S, ..3], Vec3<S>, Point3<S>> for AffineMatrix3<S> {
|
||||
Transform<S, [S, ..3], Vector3<S>, Point3<S>> for AffineMatrix3<S> {
|
||||
#[inline]
|
||||
fn identity() -> AffineMatrix3<S> {
|
||||
AffineMatrix3 { mat: Mat4::identity() }
|
||||
AffineMatrix3 { mat: Matrix4::identity() }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn look_at(eye: &Point3<S>, center: &Point3<S>, up: &Vec3<S>) -> AffineMatrix3<S> {
|
||||
AffineMatrix3 { mat: Mat4::look_at(eye, center, up) }
|
||||
fn look_at(eye: &Point3<S>, center: &Point3<S>, up: &Vector3<S>) -> AffineMatrix3<S> {
|
||||
AffineMatrix3 { mat: Matrix4::look_at(eye, center, up) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn transform_vec(&self, vec: &Vec3<S>) -> Vec3<S> {
|
||||
fn transform_vector(&self, vec: &Vector3<S>) -> Vector3<S> {
|
||||
self.mat.mul_v( &vec.extend(num::zero()) ).truncate()
|
||||
}
|
||||
|
||||
|
@ -206,8 +206,8 @@ Transform<S, [S, ..3], Vec3<S>, Point3<S>> for AffineMatrix3<S> {
|
|||
}
|
||||
|
||||
impl<S: PartOrdPrim>
|
||||
ToMat4<S> for AffineMatrix3<S> {
|
||||
#[inline] fn to_mat4(&self) -> Mat4<S> { self.mat.clone() }
|
||||
ToMatrix4<S> for AffineMatrix3<S> {
|
||||
#[inline] fn to_matrix4(&self) -> Matrix4<S> { self.mat.clone() }
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
|
@ -216,26 +216,26 @@ Transform3<S> for AffineMatrix3<S> {}
|
|||
|
||||
/// A transformation in three dimensions consisting of a rotation,
|
||||
/// displacement vector and scale amount.
|
||||
pub struct Transform3D<S>( Decomposed<S,Vec3<S>,Quat<S>> );
|
||||
pub struct Transform3D<S>( Decomposed<S,Vector3<S>,Quaternion<S>> );
|
||||
|
||||
impl<S: PartOrdFloat<S>> Transform3D<S> {
|
||||
#[inline]
|
||||
pub fn new(scale: S, rot: Quat<S>, disp: Vec3<S>) -> Transform3D<S> {
|
||||
pub fn new(scale: S, rot: Quaternion<S>, disp: Vector3<S>) -> Transform3D<S> {
|
||||
Transform3D( Decomposed { scale: scale, rot: rot, disp: disp })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn translate(x: S, y: S, z: S) -> Transform3D<S> {
|
||||
Transform3D( Decomposed { scale: one(), rot: Quat::zero(), disp: Vec3::new(x, y, z) })
|
||||
Transform3D( Decomposed { scale: one(), rot: Quaternion::zero(), disp: Vector3::new(x, y, z) })
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get<'a>(&'a self) -> &'a Decomposed<S,Vec3<S>,Quat<S>> {
|
||||
pub fn get<'a>(&'a self) -> &'a Decomposed<S,Vector3<S>,Quaternion<S>> {
|
||||
let &Transform3D(ref d) = self;
|
||||
d
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>> ToMat4<S> for Transform3D<S> {
|
||||
fn to_mat4(&self) -> Mat4<S> { self.get().to_mat4() }
|
||||
impl<S: PartOrdFloat<S>> ToMatrix4<S> for Transform3D<S> {
|
||||
fn to_matrix4(&self) -> Matrix4<S> { self.get().to_matrix4() }
|
||||
}
|
||||
|
|
|
@ -133,41 +133,41 @@ macro_rules! vec(
|
|||
)
|
||||
)
|
||||
|
||||
vec!(Vec2<S> { x, y }, 2)
|
||||
vec!(Vec3<S> { x, y, z }, 3)
|
||||
vec!(Vec4<S> { x, y, z, w }, 4)
|
||||
vec!(Vector2<S> { x, y }, 2)
|
||||
vec!(Vector3<S> { x, y, z }, 3)
|
||||
vec!(Vector4<S> { x, y, z, w }, 4)
|
||||
|
||||
array!(impl<S> Vec2<S> -> [S, ..2] _2)
|
||||
array!(impl<S> Vec3<S> -> [S, ..3] _3)
|
||||
array!(impl<S> Vec4<S> -> [S, ..4] _4)
|
||||
array!(impl<S> Vector2<S> -> [S, ..2] _2)
|
||||
array!(impl<S> Vector3<S> -> [S, ..3] _3)
|
||||
array!(impl<S> Vector4<S> -> [S, ..4] _4)
|
||||
|
||||
/// Operations specific to numeric two-dimensional vectors.
|
||||
impl<S: Primitive> Vec2<S> {
|
||||
#[inline] pub fn unit_x() -> Vec2<S> { Vec2::new(one(), zero()) }
|
||||
#[inline] pub fn unit_y() -> Vec2<S> { Vec2::new(zero(), one()) }
|
||||
impl<S: Primitive> Vector2<S> {
|
||||
#[inline] pub fn unit_x() -> Vector2<S> { Vector2::new(one(), zero()) }
|
||||
#[inline] pub fn unit_y() -> Vector2<S> { Vector2::new(zero(), one()) }
|
||||
|
||||
/// The perpendicular dot product of the vector and `other`.
|
||||
#[inline]
|
||||
pub fn perp_dot(&self, other: &Vec2<S>) -> S {
|
||||
pub fn perp_dot(&self, other: &Vector2<S>) -> S {
|
||||
(self.x * other.y) - (self.y * other.x)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn extend(&self, z: S)-> Vec3<S> {
|
||||
Vec3::new(self.x.clone(), self.y.clone(), z)
|
||||
pub fn extend(&self, z: S)-> Vector3<S> {
|
||||
Vector3::new(self.x.clone(), self.y.clone(), z)
|
||||
}
|
||||
}
|
||||
|
||||
/// Operations specific to numeric three-dimensional vectors.
|
||||
impl<S: Primitive> Vec3<S> {
|
||||
#[inline] pub fn unit_x() -> Vec3<S> { Vec3::new(one(), zero(), zero()) }
|
||||
#[inline] pub fn unit_y() -> Vec3<S> { Vec3::new(zero(), one(), zero()) }
|
||||
#[inline] pub fn unit_z() -> Vec3<S> { Vec3::new(zero(), zero(), one()) }
|
||||
impl<S: Primitive> Vector3<S> {
|
||||
#[inline] pub fn unit_x() -> Vector3<S> { Vector3::new(one(), zero(), zero()) }
|
||||
#[inline] pub fn unit_y() -> Vector3<S> { Vector3::new(zero(), one(), zero()) }
|
||||
#[inline] pub fn unit_z() -> Vector3<S> { Vector3::new(zero(), zero(), one()) }
|
||||
|
||||
/// Returns the cross product of the vector and `other`.
|
||||
#[inline]
|
||||
pub fn cross(&self, other: &Vec3<S>) -> Vec3<S> {
|
||||
Vec3::new((self.y * other.z) - (self.z * other.y),
|
||||
pub fn cross(&self, other: &Vector3<S>) -> Vector3<S> {
|
||||
Vector3::new((self.y * other.z) - (self.z * other.y),
|
||||
(self.z * other.x) - (self.x * other.z),
|
||||
(self.x * other.y) - (self.y * other.x))
|
||||
}
|
||||
|
@ -175,31 +175,31 @@ impl<S: Primitive> Vec3<S> {
|
|||
/// Calculates the cross product of the vector and `other`, then stores the
|
||||
/// result in `self`.
|
||||
#[inline]
|
||||
pub fn cross_self(&mut self, other: &Vec3<S>) {
|
||||
pub fn cross_self(&mut self, other: &Vector3<S>) {
|
||||
*self = self.cross(other)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn extend(&self, w: S)-> Vec4<S> {
|
||||
Vec4::new(self.x.clone(), self.y.clone(), self.z.clone(), w)
|
||||
pub fn extend(&self, w: S)-> Vector4<S> {
|
||||
Vector4::new(self.x.clone(), self.y.clone(), self.z.clone(), w)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn truncate(&self)-> Vec2<S> {
|
||||
Vec2::new(self.x.clone(), self.y.clone()) //ignore Z
|
||||
pub fn truncate(&self)-> Vector2<S> {
|
||||
Vector2::new(self.x.clone(), self.y.clone()) //ignore Z
|
||||
}
|
||||
}
|
||||
|
||||
/// Operations specific to numeric four-dimensional vectors.
|
||||
impl<S: Primitive> Vec4<S> {
|
||||
#[inline] pub fn unit_x() -> Vec4<S> { Vec4::new(one(), zero(), zero(), zero()) }
|
||||
#[inline] pub fn unit_y() -> Vec4<S> { Vec4::new(zero(), one(), zero(), zero()) }
|
||||
#[inline] pub fn unit_z() -> Vec4<S> { Vec4::new(zero(), zero(), one(), zero()) }
|
||||
#[inline] pub fn unit_w() -> Vec4<S> { Vec4::new(zero(), zero(), zero(), one()) }
|
||||
impl<S: Primitive> Vector4<S> {
|
||||
#[inline] pub fn unit_x() -> Vector4<S> { Vector4::new(one(), zero(), zero(), zero()) }
|
||||
#[inline] pub fn unit_y() -> Vector4<S> { Vector4::new(zero(), one(), zero(), zero()) }
|
||||
#[inline] pub fn unit_z() -> Vector4<S> { Vector4::new(zero(), zero(), one(), zero()) }
|
||||
#[inline] pub fn unit_w() -> Vector4<S> { Vector4::new(zero(), zero(), zero(), one()) }
|
||||
|
||||
#[inline]
|
||||
pub fn truncate(&self)-> Vec3<S> {
|
||||
Vec3::new(self.x.clone(), self.y.clone(), self.z.clone()) //ignore W
|
||||
pub fn truncate(&self)-> Vector3<S> {
|
||||
Vector3::new(self.x.clone(), self.y.clone(), self.z.clone()) //ignore W
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -279,42 +279,42 @@ pub trait EuclideanVector
|
|||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
EuclideanVector<S, [S, ..2]> for Vec2<S> {
|
||||
EuclideanVector<S, [S, ..2]> for Vector2<S> {
|
||||
#[inline]
|
||||
fn angle(&self, other: &Vec2<S>) -> Rad<S> {
|
||||
fn angle(&self, other: &Vector2<S>) -> Rad<S> {
|
||||
atan2(self.perp_dot(other), self.dot(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
EuclideanVector<S, [S, ..3]> for Vec3<S> {
|
||||
EuclideanVector<S, [S, ..3]> for Vector3<S> {
|
||||
#[inline]
|
||||
fn angle(&self, other: &Vec3<S>) -> Rad<S> {
|
||||
fn angle(&self, other: &Vector3<S>) -> Rad<S> {
|
||||
atan2(self.cross(other).length(), self.dot(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: PartOrdFloat<S>>
|
||||
EuclideanVector<S, [S, ..4]> for Vec4<S> {
|
||||
EuclideanVector<S, [S, ..4]> for Vector4<S> {
|
||||
#[inline]
|
||||
fn angle(&self, other: &Vec4<S>) -> Rad<S> {
|
||||
fn angle(&self, other: &Vector4<S>) -> Rad<S> {
|
||||
acos(self.dot(other) / (self.length() * other.length()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: fmt::Show> fmt::Show for Vec2<S> {
|
||||
impl<S: fmt::Show> fmt::Show for Vector2<S> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f.buf, "[{}, {}]", self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: fmt::Show> fmt::Show for Vec3<S> {
|
||||
impl<S: fmt::Show> fmt::Show for Vector3<S> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f.buf, "[{}, {}, {}]", self.x, self.y, self.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: fmt::Show> fmt::Show for Vec4<S> {
|
||||
impl<S: fmt::Show> fmt::Show for Vector4<S> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f.buf, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
use cgmath::aabb::*;
|
||||
use cgmath::point::{Point2, Point3};
|
||||
use cgmath::vector::{Vec2, Vec3};
|
||||
use cgmath::vector::{Vector2, Vector3};
|
||||
|
||||
#[test]
|
||||
fn test_aabb() {
|
||||
let aabb = Aabb2::new(Point2::new(-20, 30), Point2::new(10, -10));
|
||||
assert_eq!(aabb.min(), &Point2::new(-20, -10));
|
||||
assert_eq!(aabb.max(), &Point2::new(10, 30));
|
||||
assert_eq!(aabb.dim(), Vec2::new(30, 40));
|
||||
assert_eq!(aabb.dim(), Vector2::new(30, 40));
|
||||
assert_eq!(aabb.volume(), 30 * 40);
|
||||
assert_eq!(aabb.center(), Point2::new(-5, 10));
|
||||
|
||||
|
@ -24,7 +24,7 @@ fn test_aabb() {
|
|||
let aabb = Aabb3::new(Point3::new(-20, 30, 5), Point3::new(10, -10, -5));
|
||||
assert_eq!(aabb.min(), &Point3::new(-20, -10, -5));
|
||||
assert_eq!(aabb.max(), &Point3::new(10, 30, 5));
|
||||
assert_eq!(aabb.dim(), Vec3::new(30, 40, 10));
|
||||
assert_eq!(aabb.dim(), Vector3::new(30, 40, 10));
|
||||
assert_eq!(aabb.volume(), 30 * 40 * 10);
|
||||
assert_eq!(aabb.center(), Point3::new(-5, 10, 0));
|
||||
|
||||
|
@ -36,12 +36,12 @@ fn test_aabb() {
|
|||
assert!(aabb.contains(&Point3::new(-20, -10, -5)));
|
||||
assert!(!aabb.contains(&Point3::new(-21, -11, -6)));
|
||||
|
||||
assert_eq!(aabb.add_v(&Vec3::new(1, 2, 3)),
|
||||
assert_eq!(aabb.add_v(&Vector3::new(1, 2, 3)),
|
||||
Aabb3::new(Point3::new(-19, 32, 8), Point3::new(11, -8, -2)));
|
||||
|
||||
assert_eq!(aabb.mul_s(2),
|
||||
Aabb3::new(Point3::new(-40, -20, -10), Point3::new(20, 60, 10)));
|
||||
|
||||
assert_eq!(aabb.mul_v(&Vec3::new(1, 2, 3)),
|
||||
assert_eq!(aabb.mul_v(&Vector3::new(1, 2, 3)),
|
||||
Aabb3::new(Point3::new(-20, -20, -15), Point3::new(10, 60, 15)));
|
||||
}
|
||||
|
|
|
@ -17,81 +17,81 @@ use cgmath::matrix::*;
|
|||
use cgmath::vector::*;
|
||||
use cgmath::approx::ApproxEq;
|
||||
|
||||
pub mod mat2 {
|
||||
pub mod matrix2 {
|
||||
use cgmath::matrix::*;
|
||||
use cgmath::vector::*;
|
||||
|
||||
pub static A: Mat2<f64> = Mat2 { x: Vec2 { x: 1.0, y: 3.0 },
|
||||
y: Vec2 { x: 2.0, y: 4.0 } };
|
||||
pub static B: Mat2<f64> = Mat2 { x: Vec2 { x: 2.0, y: 4.0 },
|
||||
y: Vec2 { x: 3.0, y: 5.0 } };
|
||||
pub static C: Mat2<f64> = Mat2 { x: Vec2 { x: 2.0, y: 1.0 },
|
||||
y: Vec2 { x: 1.0, y: 2.0 } };
|
||||
pub static A: Matrix2<f64> = Matrix2 { x: Vector2 { x: 1.0, y: 3.0 },
|
||||
y: Vector2 { x: 2.0, y: 4.0 } };
|
||||
pub static B: Matrix2<f64> = Matrix2 { x: Vector2 { x: 2.0, y: 4.0 },
|
||||
y: Vector2 { x: 3.0, y: 5.0 } };
|
||||
pub static C: Matrix2<f64> = Matrix2 { x: Vector2 { x: 2.0, y: 1.0 },
|
||||
y: Vector2 { x: 1.0, y: 2.0 } };
|
||||
|
||||
pub static V: Vec2<f64> = Vec2 { x: 1.0, y: 2.0 };
|
||||
pub static V: Vector2<f64> = Vector2 { x: 1.0, y: 2.0 };
|
||||
pub static F: f64 = 0.5;
|
||||
}
|
||||
|
||||
pub mod mat3 {
|
||||
pub mod matrix3 {
|
||||
use cgmath::matrix::*;
|
||||
use cgmath::vector::*;
|
||||
|
||||
pub static A: Mat3<f64> = Mat3 { x: Vec3 { x: 1.0, y: 4.0, z: 7.0 },
|
||||
y: Vec3 { x: 2.0, y: 5.0, z: 8.0 },
|
||||
z: Vec3 { x: 3.0, y: 6.0, z: 9.0 } };
|
||||
pub static B: Mat3<f64> = Mat3 { x: Vec3 { x: 2.0, y: 5.0, z: 8.0 },
|
||||
y: Vec3 { x: 3.0, y: 6.0, z: 9.0 },
|
||||
z: Vec3 { x: 4.0, y: 7.0, z: 10.0 } };
|
||||
pub static C: Mat3<f64> = Mat3 { x: Vec3 { x: 2.0, y: 4.0, z: 6.0 },
|
||||
y: Vec3 { x: 0.0, y: 2.0, z: 4.0 },
|
||||
z: Vec3 { x: 0.0, y: 0.0, z: 1.0 } };
|
||||
pub static D: Mat3<f64> = Mat3 { x: Vec3 { x: 3.0, y: 2.0, z: 1.0 },
|
||||
y: Vec3 { x: 2.0, y: 3.0, z: 2.0 },
|
||||
z: Vec3 { x: 1.0, y: 2.0, z: 3.0 } };
|
||||
pub static A: Matrix3<f64> = Matrix3 { x: Vector3 { x: 1.0, y: 4.0, z: 7.0 },
|
||||
y: Vector3 { x: 2.0, y: 5.0, z: 8.0 },
|
||||
z: Vector3 { x: 3.0, y: 6.0, z: 9.0 } };
|
||||
pub static B: Matrix3<f64> = Matrix3 { x: Vector3 { x: 2.0, y: 5.0, z: 8.0 },
|
||||
y: Vector3 { x: 3.0, y: 6.0, z: 9.0 },
|
||||
z: Vector3 { x: 4.0, y: 7.0, z: 10.0 } };
|
||||
pub static C: Matrix3<f64> = Matrix3 { x: Vector3 { x: 2.0, y: 4.0, z: 6.0 },
|
||||
y: Vector3 { x: 0.0, y: 2.0, z: 4.0 },
|
||||
z: Vector3 { x: 0.0, y: 0.0, z: 1.0 } };
|
||||
pub static D: Matrix3<f64> = Matrix3 { x: Vector3 { x: 3.0, y: 2.0, z: 1.0 },
|
||||
y: Vector3 { x: 2.0, y: 3.0, z: 2.0 },
|
||||
z: Vector3 { x: 1.0, y: 2.0, z: 3.0 } };
|
||||
|
||||
pub static V: Vec3<f64> = Vec3 { x: 1.0, y: 2.0, z: 3.0 };
|
||||
pub static V: Vector3<f64> = Vector3 { x: 1.0, y: 2.0, z: 3.0 };
|
||||
pub static F: f64 = 0.5;
|
||||
}
|
||||
|
||||
pub mod mat4 {
|
||||
pub mod matrix4 {
|
||||
use cgmath::matrix::*;
|
||||
use cgmath::vector::*;
|
||||
|
||||
pub static A: Mat4<f64> = Mat4 { x: Vec4 { x: 1.0, y: 5.0, z: 9.0, w: 13.0 },
|
||||
y: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 },
|
||||
z: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 },
|
||||
w: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 } };
|
||||
pub static B: Mat4<f64> = Mat4 { x: Vec4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 },
|
||||
y: Vec4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 },
|
||||
z: Vec4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 },
|
||||
w: Vec4 { x: 5.0, y: 9.0, z: 13.0, w: 17.0 } };
|
||||
pub static C: Mat4<f64> = Mat4 { x: Vec4 { x: 3.0, y: 2.0, z: 1.0, w: 1.0 },
|
||||
y: Vec4 { x: 2.0, y: 3.0, z: 2.0, w: 2.0 },
|
||||
z: Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 3.0 },
|
||||
w: Vec4 { x: 0.0, y: 1.0, z: 1.0, w: 0.0 } };
|
||||
pub static D: Mat4<f64> = Mat4 { x: Vec4 { x: 4.0, y: 3.0, z: 2.0, w: 1.0 },
|
||||
y: Vec4 { x: 3.0, y: 4.0, z: 3.0, w: 2.0 },
|
||||
z: Vec4 { x: 2.0, y: 3.0, z: 4.0, w: 3.0 },
|
||||
w: Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 4.0 } };
|
||||
pub static A: Matrix4<f64> = Matrix4 { x: Vector4 { x: 1.0, y: 5.0, z: 9.0, w: 13.0 },
|
||||
y: Vector4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 },
|
||||
z: Vector4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 },
|
||||
w: Vector4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 } };
|
||||
pub static B: Matrix4<f64> = Matrix4 { x: Vector4 { x: 2.0, y: 6.0, z: 10.0, w: 14.0 },
|
||||
y: Vector4 { x: 3.0, y: 7.0, z: 11.0, w: 15.0 },
|
||||
z: Vector4 { x: 4.0, y: 8.0, z: 12.0, w: 16.0 },
|
||||
w: Vector4 { x: 5.0, y: 9.0, z: 13.0, w: 17.0 } };
|
||||
pub static C: Matrix4<f64> = Matrix4 { x: Vector4 { x: 3.0, y: 2.0, z: 1.0, w: 1.0 },
|
||||
y: Vector4 { x: 2.0, y: 3.0, z: 2.0, w: 2.0 },
|
||||
z: Vector4 { x: 1.0, y: 2.0, z: 3.0, w: 3.0 },
|
||||
w: Vector4 { x: 0.0, y: 1.0, z: 1.0, w: 0.0 } };
|
||||
pub static D: Matrix4<f64> = Matrix4 { x: Vector4 { x: 4.0, y: 3.0, z: 2.0, w: 1.0 },
|
||||
y: Vector4 { x: 3.0, y: 4.0, z: 3.0, w: 2.0 },
|
||||
z: Vector4 { x: 2.0, y: 3.0, z: 4.0, w: 3.0 },
|
||||
w: Vector4 { x: 1.0, y: 2.0, z: 3.0, w: 4.0 } };
|
||||
|
||||
pub static V: Vec4<f64> = Vec4 { x: 1.0, y: 2.0, z: 3.0, w: 4.0 };
|
||||
pub static V: Vector4<f64> = Vector4 { x: 1.0, y: 2.0, z: 3.0, w: 4.0 };
|
||||
pub static F: f64 = 0.5;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_neg() {
|
||||
// Mat2
|
||||
assert_eq!(-mat2::A,
|
||||
Mat2::new(-1.0, -3.0,
|
||||
// Matrix2
|
||||
assert_eq!(-matrix2::A,
|
||||
Matrix2::new(-1.0, -3.0,
|
||||
-2.0, -4.0));
|
||||
// Mat3
|
||||
assert_eq!(-mat3::A,
|
||||
Mat3::new(-1.0, -4.0, -7.0,
|
||||
// Matrix3
|
||||
assert_eq!(-matrix3::A,
|
||||
Matrix3::new(-1.0, -4.0, -7.0,
|
||||
-2.0, -5.0, -8.0,
|
||||
-3.0, -6.0, -9.0));
|
||||
// Mat4
|
||||
assert_eq!(-mat4::A,
|
||||
Mat4::new(-1.0, -5.0, -9.0, -13.0,
|
||||
// Matrix4
|
||||
assert_eq!(-matrix4::A,
|
||||
Matrix4::new(-1.0, -5.0, -9.0, -13.0,
|
||||
-2.0, -6.0, -10.0, -14.0,
|
||||
-3.0, -7.0, -11.0, -15.0,
|
||||
-4.0, -8.0, -12.0, -16.0));
|
||||
|
@ -99,112 +99,112 @@ fn test_neg() {
|
|||
|
||||
#[test]
|
||||
fn test_mul_s() {
|
||||
// Mat2
|
||||
assert_eq!(mat2::A.mul_s(mat2::F),
|
||||
Mat2::new(0.5, 1.5,
|
||||
// Matrix2
|
||||
assert_eq!(matrix2::A.mul_s(matrix2::F),
|
||||
Matrix2::new(0.5, 1.5,
|
||||
1.0, 2.0));
|
||||
let mut mut_a = mat2::A;
|
||||
mut_a.mul_self_s(mat2::F);
|
||||
assert_eq!(mut_a, mat2::A.mul_s(mat2::F));
|
||||
let mut mut_a = matrix2::A;
|
||||
mut_a.mul_self_s(matrix2::F);
|
||||
assert_eq!(mut_a, matrix2::A.mul_s(matrix2::F));
|
||||
|
||||
// Mat3
|
||||
assert_eq!(mat3::A.mul_s(mat3::F),
|
||||
Mat3::new(0.5, 2.0, 3.5,
|
||||
// Matrix3
|
||||
assert_eq!(matrix3::A.mul_s(matrix3::F),
|
||||
Matrix3::new(0.5, 2.0, 3.5,
|
||||
1.0, 2.5, 4.0,
|
||||
1.5, 3.0, 4.5));
|
||||
let mut mut_a = mat3::A;
|
||||
mut_a.mul_self_s(mat3::F);
|
||||
assert_eq!(mut_a, mat3::A.mul_s(mat3::F));
|
||||
let mut mut_a = matrix3::A;
|
||||
mut_a.mul_self_s(matrix3::F);
|
||||
assert_eq!(mut_a, matrix3::A.mul_s(matrix3::F));
|
||||
|
||||
// Mat4
|
||||
assert_eq!(mat4::A.mul_s(mat4::F),
|
||||
Mat4::new(0.5, 2.5, 4.5, 6.5,
|
||||
// Matrix4
|
||||
assert_eq!(matrix4::A.mul_s(matrix4::F),
|
||||
Matrix4::new(0.5, 2.5, 4.5, 6.5,
|
||||
1.0, 3.0, 5.0, 7.0,
|
||||
1.5, 3.5, 5.5, 7.5,
|
||||
2.0, 4.0, 6.0, 8.0));
|
||||
let mut mut_a = mat4::A;
|
||||
mut_a.mul_self_s(mat4::F);
|
||||
assert_eq!(mut_a, mat4::A.mul_s(mat4::F));
|
||||
let mut mut_a = matrix4::A;
|
||||
mut_a.mul_self_s(matrix4::F);
|
||||
assert_eq!(mut_a, matrix4::A.mul_s(matrix4::F));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_m() {
|
||||
// Mat2
|
||||
assert_eq!(mat2::A.add_m(&mat2::B),
|
||||
Mat2::new(3.0, 7.0,
|
||||
// Matrix2
|
||||
assert_eq!(matrix2::A.add_m(&matrix2::B),
|
||||
Matrix2::new(3.0, 7.0,
|
||||
5.0, 9.0));
|
||||
let mut mut_a = mat2::A;
|
||||
mut_a.add_self_m(&mat2::B);
|
||||
assert_eq!(mut_a, mat2::A.add_m(&mat2::B));
|
||||
let mut mut_a = matrix2::A;
|
||||
mut_a.add_self_m(&matrix2::B);
|
||||
assert_eq!(mut_a, matrix2::A.add_m(&matrix2::B));
|
||||
|
||||
// Mat3
|
||||
assert_eq!(mat3::A.add_m(&mat3::B),
|
||||
Mat3::new(3.0, 9.0, 15.0,
|
||||
// Matrix3
|
||||
assert_eq!(matrix3::A.add_m(&matrix3::B),
|
||||
Matrix3::new(3.0, 9.0, 15.0,
|
||||
5.0, 11.0, 17.0,
|
||||
7.0, 13.0, 19.0));
|
||||
let mut mut_a = mat3::A;
|
||||
mut_a.add_self_m(&mat3::B);
|
||||
assert_eq!(mut_a, mat3::A.add_m(&mat3::B));
|
||||
let mut mut_a = matrix3::A;
|
||||
mut_a.add_self_m(&matrix3::B);
|
||||
assert_eq!(mut_a, matrix3::A.add_m(&matrix3::B));
|
||||
|
||||
// Mat4
|
||||
assert_eq!(mat4::A.add_m(&mat4::B),
|
||||
Mat4::new(3.0, 11.0, 19.0, 27.0,
|
||||
// Matrix4
|
||||
assert_eq!(matrix4::A.add_m(&matrix4::B),
|
||||
Matrix4::new(3.0, 11.0, 19.0, 27.0,
|
||||
5.0, 13.0, 21.0, 29.0,
|
||||
7.0, 15.0, 23.0, 31.0,
|
||||
9.0, 17.0, 25.0, 33.0));
|
||||
let mut mut_a = mat4::A;
|
||||
mut_a.add_self_m(&mat4::B);
|
||||
assert_eq!(mut_a, mat4::A.add_m(&mat4::B));
|
||||
let mut mut_a = matrix4::A;
|
||||
mut_a.add_self_m(&matrix4::B);
|
||||
assert_eq!(mut_a, matrix4::A.add_m(&matrix4::B));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sub_m() {
|
||||
// Mat2
|
||||
assert_eq!(mat2::A.sub_m(&mat2::B),
|
||||
Mat2::new(-1.0, -1.0,
|
||||
// Matrix2
|
||||
assert_eq!(matrix2::A.sub_m(&matrix2::B),
|
||||
Matrix2::new(-1.0, -1.0,
|
||||
-1.0, -1.0));
|
||||
let mut mut_a = mat2::A;
|
||||
mut_a.sub_self_m(&mat2::B);
|
||||
assert_eq!(mut_a, mat2::A.sub_m(&mat2::B));
|
||||
let mut mut_a = matrix2::A;
|
||||
mut_a.sub_self_m(&matrix2::B);
|
||||
assert_eq!(mut_a, matrix2::A.sub_m(&matrix2::B));
|
||||
|
||||
// Mat3
|
||||
assert_eq!(mat3::A.sub_m(&mat3::B),
|
||||
Mat3::new(-1.0, -1.0, -1.0,
|
||||
// Matrix3
|
||||
assert_eq!(matrix3::A.sub_m(&matrix3::B),
|
||||
Matrix3::new(-1.0, -1.0, -1.0,
|
||||
-1.0, -1.0, -1.0,
|
||||
-1.0, -1.0, -1.0));
|
||||
let mut mut_a = mat3::A;
|
||||
mut_a.sub_self_m(&mat3::B);
|
||||
assert_eq!(mut_a, mat3::A.sub_m(&mat3::B));
|
||||
let mut mut_a = matrix3::A;
|
||||
mut_a.sub_self_m(&matrix3::B);
|
||||
assert_eq!(mut_a, matrix3::A.sub_m(&matrix3::B));
|
||||
|
||||
// Mat4
|
||||
assert_eq!(mat4::A.sub_m(&mat4::B),
|
||||
Mat4::new(-1.0, -1.0, -1.0, -1.0,
|
||||
// Matrix4
|
||||
assert_eq!(matrix4::A.sub_m(&matrix4::B),
|
||||
Matrix4::new(-1.0, -1.0, -1.0, -1.0,
|
||||
-1.0, -1.0, -1.0, -1.0,
|
||||
-1.0, -1.0, -1.0, -1.0,
|
||||
-1.0, -1.0, -1.0, -1.0));
|
||||
let mut mut_a = mat4::A;
|
||||
mut_a.sub_self_m(&mat4::B);
|
||||
assert_eq!(mut_a, mat4::A.sub_m(&mat4::B));
|
||||
let mut mut_a = matrix4::A;
|
||||
mut_a.sub_self_m(&matrix4::B);
|
||||
assert_eq!(mut_a, matrix4::A.sub_m(&matrix4::B));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mul_v() {
|
||||
assert_eq!(mat2::A.mul_v(&mat2::V), Vec2::new(5.0, 11.0));
|
||||
assert_eq!(mat3::A.mul_v(&mat3::V), Vec3::new(14.0, 32.0, 50.0));
|
||||
assert_eq!(mat4::A.mul_v(&mat4::V), Vec4::new(30.0, 70.0, 110.0, 150.0));
|
||||
assert_eq!(matrix2::A.mul_v(&matrix2::V), Vector2::new(5.0, 11.0));
|
||||
assert_eq!(matrix3::A.mul_v(&matrix3::V), Vector3::new(14.0, 32.0, 50.0));
|
||||
assert_eq!(matrix4::A.mul_v(&matrix4::V), Vector4::new(30.0, 70.0, 110.0, 150.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mul_m() {
|
||||
assert_eq!(mat2::A.mul_m(&mat2::B),
|
||||
Mat2::new(10.0, 22.0,
|
||||
assert_eq!(matrix2::A.mul_m(&matrix2::B),
|
||||
Matrix2::new(10.0, 22.0,
|
||||
13.0, 29.0));
|
||||
assert_eq!(mat3::A.mul_m(&mat3::B),
|
||||
Mat3::new(36.0, 81.0, 126.0,
|
||||
assert_eq!(matrix3::A.mul_m(&matrix3::B),
|
||||
Matrix3::new(36.0, 81.0, 126.0,
|
||||
42.0, 96.0, 150.0,
|
||||
48.0, 111.0, 174.0));
|
||||
assert_eq!(mat4::A.mul_m(&mat4::B),
|
||||
Mat4::new(100.0, 228.0, 356.0, 484.0,
|
||||
assert_eq!(matrix4::A.mul_m(&matrix4::B),
|
||||
Matrix4::new(100.0, 228.0, 356.0, 484.0,
|
||||
110.0, 254.0, 398.0, 542.0,
|
||||
120.0, 280.0, 440.0, 600.0,
|
||||
130.0, 306.0, 482.0, 658.0));
|
||||
|
@ -212,153 +212,153 @@ fn test_mul_m() {
|
|||
|
||||
#[test]
|
||||
fn test_determinant() {
|
||||
assert_eq!(mat2::A.determinant(), -2.0);
|
||||
assert_eq!(mat3::A.determinant(), 0.0);
|
||||
assert_eq!(mat4::A.determinant(), 0.0);
|
||||
assert_eq!(matrix2::A.determinant(), -2.0);
|
||||
assert_eq!(matrix3::A.determinant(), 0.0);
|
||||
assert_eq!(matrix4::A.determinant(), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trace() {
|
||||
assert_eq!(mat2::A.trace(), 5.0);
|
||||
assert_eq!(mat3::A.trace(), 15.0);
|
||||
assert_eq!(mat4::A.trace(), 34.0);
|
||||
assert_eq!(matrix2::A.trace(), 5.0);
|
||||
assert_eq!(matrix3::A.trace(), 15.0);
|
||||
assert_eq!(matrix4::A.trace(), 34.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transpose() {
|
||||
// Mat2
|
||||
assert_eq!(mat2::A.transpose(),
|
||||
Mat2::<f64>::new(1.0, 2.0,
|
||||
// Matrix2
|
||||
assert_eq!(matrix2::A.transpose(),
|
||||
Matrix2::<f64>::new(1.0, 2.0,
|
||||
3.0, 4.0));
|
||||
let mut mut_a = mat2::A;
|
||||
let mut mut_a = matrix2::A;
|
||||
mut_a.transpose_self();
|
||||
assert_eq!(mut_a, mat2::A.transpose());
|
||||
assert_eq!(mut_a, matrix2::A.transpose());
|
||||
|
||||
// Mat3
|
||||
assert_eq!(mat3::A.transpose(),
|
||||
Mat3::<f64>::new(1.0, 2.0, 3.0,
|
||||
// Matrix3
|
||||
assert_eq!(matrix3::A.transpose(),
|
||||
Matrix3::<f64>::new(1.0, 2.0, 3.0,
|
||||
4.0, 5.0, 6.0,
|
||||
7.0, 8.0, 9.0));
|
||||
let mut mut_a = mat3::A;
|
||||
let mut mut_a = matrix3::A;
|
||||
mut_a.transpose_self();
|
||||
assert_eq!(mut_a, mat3::A.transpose());
|
||||
assert_eq!(mut_a, matrix3::A.transpose());
|
||||
|
||||
// Mat4
|
||||
assert_eq!(mat4::A.transpose(),
|
||||
Mat4::<f64>::new( 1.0, 2.0, 3.0, 4.0,
|
||||
// Matrix4
|
||||
assert_eq!(matrix4::A.transpose(),
|
||||
Matrix4::<f64>::new( 1.0, 2.0, 3.0, 4.0,
|
||||
5.0, 6.0, 7.0, 8.0,
|
||||
9.0, 10.0, 11.0, 12.0,
|
||||
13.0, 14.0, 15.0, 16.0));
|
||||
let mut mut_a = mat4::A;
|
||||
let mut mut_a = matrix4::A;
|
||||
mut_a.transpose_self();
|
||||
assert_eq!(mut_a, mat4::A.transpose());
|
||||
assert_eq!(mut_a, matrix4::A.transpose());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invert() {
|
||||
// Mat2
|
||||
assert!(Mat2::<f64>::identity().invert().unwrap().is_identity());
|
||||
// Matrix2
|
||||
assert!(Matrix2::<f64>::identity().invert().unwrap().is_identity());
|
||||
|
||||
assert_eq!(mat2::A.invert().unwrap(),
|
||||
Mat2::new(-2.0, 1.5,
|
||||
assert_eq!(matrix2::A.invert().unwrap(),
|
||||
Matrix2::new(-2.0, 1.5,
|
||||
1.0, -0.5));
|
||||
assert!(Mat2::new(0.0, 2.0,
|
||||
assert!(Matrix2::new(0.0, 2.0,
|
||||
0.0, 5.0).invert().is_none());
|
||||
let mut mut_a = mat2::A;
|
||||
let mut mut_a = matrix2::A;
|
||||
mut_a.invert_self();
|
||||
assert_eq!(mut_a, mat2::A.invert().unwrap());
|
||||
assert_eq!(mut_a, matrix2::A.invert().unwrap());
|
||||
|
||||
// Mat3
|
||||
assert!(Mat3::<f64>::identity().invert().unwrap().is_identity());
|
||||
// Matrix3
|
||||
assert!(Matrix3::<f64>::identity().invert().unwrap().is_identity());
|
||||
|
||||
assert_eq!(mat3::A.invert(), None);
|
||||
assert_eq!(matrix3::A.invert(), None);
|
||||
|
||||
assert_eq!(mat3::C.invert().unwrap(),
|
||||
Mat3::new(0.5, -1.0, 1.0,
|
||||
assert_eq!(matrix3::C.invert().unwrap(),
|
||||
Matrix3::new(0.5, -1.0, 1.0,
|
||||
0.0, 0.5, -2.0,
|
||||
0.0, 0.0, 1.0));
|
||||
let mut mut_c = mat3::C;
|
||||
let mut mut_c = matrix3::C;
|
||||
mut_c.invert_self();
|
||||
assert_eq!(mut_c, mat3::C.invert().unwrap());
|
||||
assert_eq!(mut_c, matrix3::C.invert().unwrap());
|
||||
|
||||
// Mat4
|
||||
assert!(Mat4::<f64>::identity().invert().unwrap().is_identity());
|
||||
// Matrix4
|
||||
assert!(Matrix4::<f64>::identity().invert().unwrap().is_identity());
|
||||
|
||||
assert!(mat4::C.invert().unwrap().approx_eq(&
|
||||
Mat4::new( 5.0, -4.0, 1.0, 0.0,
|
||||
assert!(matrix4::C.invert().unwrap().approx_eq(&
|
||||
Matrix4::new( 5.0, -4.0, 1.0, 0.0,
|
||||
-4.0, 8.0, -4.0, 0.0,
|
||||
4.0, -8.0, 4.0, 8.0,
|
||||
-3.0, 4.0, 1.0, -8.0).mul_s(0.125)));
|
||||
let mut mut_c = mat4::C;
|
||||
let mut mut_c = matrix4::C;
|
||||
mut_c.invert_self();
|
||||
assert_eq!(mut_c, mat4::C.invert().unwrap());
|
||||
assert_eq!(mut_c, matrix4::C.invert().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_predicates() {
|
||||
// Mat2
|
||||
// Matrix2
|
||||
|
||||
assert!(Mat2::<f64>::identity().is_identity());
|
||||
assert!(Mat2::<f64>::identity().is_symmetric());
|
||||
assert!(Mat2::<f64>::identity().is_diagonal());
|
||||
assert!(!Mat2::<f64>::identity().is_rotated());
|
||||
assert!(Mat2::<f64>::identity().is_invertible());
|
||||
assert!(Matrix2::<f64>::identity().is_identity());
|
||||
assert!(Matrix2::<f64>::identity().is_symmetric());
|
||||
assert!(Matrix2::<f64>::identity().is_diagonal());
|
||||
assert!(!Matrix2::<f64>::identity().is_rotated());
|
||||
assert!(Matrix2::<f64>::identity().is_invertible());
|
||||
|
||||
assert!(!mat2::A.is_identity());
|
||||
assert!(!mat2::A.is_symmetric());
|
||||
assert!(!mat2::A.is_diagonal());
|
||||
assert!(mat2::A.is_rotated());
|
||||
assert!(mat2::A.is_invertible());
|
||||
assert!(!matrix2::A.is_identity());
|
||||
assert!(!matrix2::A.is_symmetric());
|
||||
assert!(!matrix2::A.is_diagonal());
|
||||
assert!(matrix2::A.is_rotated());
|
||||
assert!(matrix2::A.is_invertible());
|
||||
|
||||
assert!(!mat2::C.is_identity());
|
||||
assert!(mat2::C.is_symmetric());
|
||||
assert!(!mat2::C.is_diagonal());
|
||||
assert!(mat2::C.is_rotated());
|
||||
assert!(mat2::C.is_invertible());
|
||||
assert!(!matrix2::C.is_identity());
|
||||
assert!(matrix2::C.is_symmetric());
|
||||
assert!(!matrix2::C.is_diagonal());
|
||||
assert!(matrix2::C.is_rotated());
|
||||
assert!(matrix2::C.is_invertible());
|
||||
|
||||
assert!(Mat2::from_value(6.0).is_diagonal());
|
||||
assert!(Matrix2::from_value(6.0).is_diagonal());
|
||||
|
||||
// Mat3
|
||||
// Matrix3
|
||||
|
||||
assert!(Mat3::<f64>::identity().is_identity());
|
||||
assert!(Mat3::<f64>::identity().is_symmetric());
|
||||
assert!(Mat3::<f64>::identity().is_diagonal());
|
||||
assert!(!Mat3::<f64>::identity().is_rotated());
|
||||
assert!(Mat3::<f64>::identity().is_invertible());
|
||||
assert!(Matrix3::<f64>::identity().is_identity());
|
||||
assert!(Matrix3::<f64>::identity().is_symmetric());
|
||||
assert!(Matrix3::<f64>::identity().is_diagonal());
|
||||
assert!(!Matrix3::<f64>::identity().is_rotated());
|
||||
assert!(Matrix3::<f64>::identity().is_invertible());
|
||||
|
||||
assert!(!mat3::A.is_identity());
|
||||
assert!(!mat3::A.is_symmetric());
|
||||
assert!(!mat3::A.is_diagonal());
|
||||
assert!(mat3::A.is_rotated());
|
||||
assert!(!mat3::A.is_invertible());
|
||||
assert!(!matrix3::A.is_identity());
|
||||
assert!(!matrix3::A.is_symmetric());
|
||||
assert!(!matrix3::A.is_diagonal());
|
||||
assert!(matrix3::A.is_rotated());
|
||||
assert!(!matrix3::A.is_invertible());
|
||||
|
||||
assert!(!mat3::D.is_identity());
|
||||
assert!(mat3::D.is_symmetric());
|
||||
assert!(!mat3::D.is_diagonal());
|
||||
assert!(mat3::D.is_rotated());
|
||||
assert!(mat3::D.is_invertible());
|
||||
assert!(!matrix3::D.is_identity());
|
||||
assert!(matrix3::D.is_symmetric());
|
||||
assert!(!matrix3::D.is_diagonal());
|
||||
assert!(matrix3::D.is_rotated());
|
||||
assert!(matrix3::D.is_invertible());
|
||||
|
||||
assert!(Mat3::from_value(6.0).is_diagonal());
|
||||
assert!(Matrix3::from_value(6.0).is_diagonal());
|
||||
|
||||
// Mat4
|
||||
// Matrix4
|
||||
|
||||
assert!(Mat4::<f64>::identity().is_identity());
|
||||
assert!(Mat4::<f64>::identity().is_symmetric());
|
||||
assert!(Mat4::<f64>::identity().is_diagonal());
|
||||
assert!(!Mat4::<f64>::identity().is_rotated());
|
||||
assert!(Mat4::<f64>::identity().is_invertible());
|
||||
assert!(Matrix4::<f64>::identity().is_identity());
|
||||
assert!(Matrix4::<f64>::identity().is_symmetric());
|
||||
assert!(Matrix4::<f64>::identity().is_diagonal());
|
||||
assert!(!Matrix4::<f64>::identity().is_rotated());
|
||||
assert!(Matrix4::<f64>::identity().is_invertible());
|
||||
|
||||
assert!(!mat4::A.is_identity());
|
||||
assert!(!mat4::A.is_symmetric());
|
||||
assert!(!mat4::A.is_diagonal());
|
||||
assert!(mat4::A.is_rotated());
|
||||
assert!(!mat4::A.is_invertible());
|
||||
assert!(!matrix4::A.is_identity());
|
||||
assert!(!matrix4::A.is_symmetric());
|
||||
assert!(!matrix4::A.is_diagonal());
|
||||
assert!(matrix4::A.is_rotated());
|
||||
assert!(!matrix4::A.is_invertible());
|
||||
|
||||
assert!(!mat4::D.is_identity());
|
||||
assert!(mat4::D.is_symmetric());
|
||||
assert!(!mat4::D.is_diagonal());
|
||||
assert!(mat4::D.is_rotated());
|
||||
assert!(mat4::D.is_invertible());
|
||||
assert!(!matrix4::D.is_identity());
|
||||
assert!(matrix4::D.is_symmetric());
|
||||
assert!(!matrix4::D.is_diagonal());
|
||||
assert!(matrix4::D.is_rotated());
|
||||
assert!(matrix4::D.is_invertible());
|
||||
|
||||
assert!(Mat4::from_value(6.0).is_diagonal());
|
||||
assert!(Matrix4::from_value(6.0).is_diagonal());
|
||||
}
|
||||
|
|
|
@ -35,12 +35,12 @@ fn test_from_points() {
|
|||
#[test]
|
||||
fn test_ray_intersection() {
|
||||
let p0 = Plane::from_abcd(1f64, 0f64, 0f64, -7f64);
|
||||
let r0: Ray3<f64> = Ray::new(Point3::new(2f64, 3f64, 4f64), Vec3::new(1f64, 1f64, 1f64).normalize());
|
||||
let r0: Ray3<f64> = Ray::new(Point3::new(2f64, 3f64, 4f64), Vector3::new(1f64, 1f64, 1f64).normalize());
|
||||
assert_eq!((p0, r0).intersection(), Some(Point3::new(7f64, 8f64, 9f64)));
|
||||
|
||||
let p1 = Plane::from_points(Point3::new(5f64, 0f64, 5f64),
|
||||
Point3::new(5f64, 5f64, 5f64),
|
||||
Point3::new(5f64, 0f64, -1f64)).unwrap();
|
||||
let r1: Ray3<f64> = Ray::new(Point3::new(0f64, 0f64, 0f64), Vec3::new(-1f64, 0f64, 0f64).normalize());
|
||||
let r1: Ray3<f64> = Ray::new(Point3::new(0f64, 0f64, 0f64), Vector3::new(-1f64, 0f64, 0f64).normalize());
|
||||
assert_eq!((p1, r1).intersection(), None); // r1 points away from p1
|
||||
}
|
||||
|
|
|
@ -13,16 +13,16 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use cgmath::matrix::{ToMat4, ToMat3};
|
||||
use cgmath::quaternion::Quat;
|
||||
use cgmath::matrix::{ToMatrix4, ToMatrix3};
|
||||
use cgmath::quaternion::Quaternion;
|
||||
|
||||
#[test]
|
||||
fn to_mat4()
|
||||
fn to_matrix4()
|
||||
{
|
||||
let quat = Quat::new(2f32, 3f32, 4f32, 5f32);
|
||||
let quaternion = Quaternion::new(2f32, 3f32, 4f32, 5f32);
|
||||
|
||||
let mat_short = quat.to_mat4();
|
||||
let mat_long = quat.to_mat3().to_mat4();
|
||||
let matrix_short = quaternion.to_matrix4();
|
||||
let matrix_long = quaternion.to_matrix3().to_matrix4();
|
||||
|
||||
assert!(mat_short == mat_long);
|
||||
assert!(matrix_short == matrix_long);
|
||||
}
|
||||
|
|
|
@ -8,10 +8,10 @@ use cgmath::intersect::Intersect;
|
|||
#[test]
|
||||
fn test_intersection() {
|
||||
let sphere = Sphere {center: Point3::new(0f64,0f64,0f64), radius: 1f64};
|
||||
let r0 = Ray::new(Point3::new(0f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
|
||||
let r1 = Ray::new(Point3::new(1f64.cos(), 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
|
||||
let r2 = Ray::new(Point3::new(1f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
|
||||
let r3 = Ray::new(Point3::new(2f64, 0f64, 5f64), Vec3::new(0f64, 0f64, -5f64).normalize());
|
||||
let r0 = Ray::new(Point3::new(0f64, 0f64, 5f64), Vector3::new(0f64, 0f64, -5f64).normalize());
|
||||
let r1 = Ray::new(Point3::new(1f64.cos(), 0f64, 5f64), Vector3::new(0f64, 0f64, -5f64).normalize());
|
||||
let r2 = Ray::new(Point3::new(1f64, 0f64, 5f64), Vector3::new(0f64, 0f64, -5f64).normalize());
|
||||
let r3 = Ray::new(Point3::new(2f64, 0f64, 5f64), Vector3::new(0f64, 0f64, -5f64).normalize());
|
||||
assert_eq!((sphere,r0).intersection(), Some(Point3::new(0f64, 0f64, 1f64)));
|
||||
assert!((sphere,r1).intersection().unwrap().approx_eq( &Point3::new(1f64.cos(), 0f64, 1f64.sin()) ));
|
||||
assert_eq!((sphere,r2).intersection(), Some(Point3::new(1f64, 0f64, 0f64)));
|
||||
|
|
|
@ -21,19 +21,19 @@ use cgmath::approx::ApproxEq;
|
|||
|
||||
#[test]
|
||||
fn test_invert() {
|
||||
let v = Vec3::new(1.0, 2.0, 3.0);
|
||||
let t = Transform3D::new(1.5, Quat::new(0.5,0.5,0.5,0.5), Vec3::new(6.0,-7.0,8.0));
|
||||
let v = Vector3::new(1.0, 2.0, 3.0);
|
||||
let t = Transform3D::new(1.5, Quaternion::new(0.5,0.5,0.5,0.5), Vector3::new(6.0,-7.0,8.0));
|
||||
let ti = t.get().invert().expect("Expected successful inversion");
|
||||
let vt = t.get().transform_vec( &v );
|
||||
assert!(v.approx_eq( &ti.transform_vec( &vt ) ));
|
||||
let vt = t.get().transform_vector( &v );
|
||||
assert!(v.approx_eq( &ti.transform_vector( &vt ) ));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_look_at() {
|
||||
let eye = Point3::new(0.0, 0.0, -5.0);
|
||||
let center = Point3::new(0.0, 0.0, 0.0);
|
||||
let up = Vec3::new(1.0, 0.0, 0.0);
|
||||
let t: Decomposed<f64,Vec3<f64>,Quat<f64>> = Transform::look_at(&eye, ¢er, &up);
|
||||
let up = Vector3::new(1.0, 0.0, 0.0);
|
||||
let t: Decomposed<f64,Vector3<f64>,Quaternion<f64>> = Transform::look_at(&eye, ¢er, &up);
|
||||
let point = Point3::new(1.0, 0.0, 0.0);
|
||||
let view_point = Point3::new(0.0, 1.0, 5.0);
|
||||
assert!( t.transform_point(&point).approx_eq(&view_point) );
|
||||
|
|
|
@ -19,67 +19,67 @@ use cgmath::approx::ApproxEq;
|
|||
|
||||
#[test]
|
||||
fn test_from_value() {
|
||||
assert_eq!(Vec2::from_value(102), Vec2::new(102, 102));
|
||||
assert_eq!(Vec3::from_value(22), Vec3::new(22, 22, 22));
|
||||
assert_eq!(Vec4::from_value(76.5), Vec4::new(76.5, 76.5, 76.5, 76.5));
|
||||
assert_eq!(Vector2::from_value(102), Vector2::new(102, 102));
|
||||
assert_eq!(Vector3::from_value(22), Vector3::new(22, 22, 22));
|
||||
assert_eq!(Vector4::from_value(76.5), Vector4::new(76.5, 76.5, 76.5, 76.5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_dot() {
|
||||
assert_eq!(Vec2::new(1, 2).dot(&Vec2::new(3, 4)), 11);
|
||||
assert_eq!(Vec3::new(1, 2, 3).dot(&Vec3::new(4, 5, 6)), 32);
|
||||
assert_eq!(Vec4::new(1, 2, 3, 4).dot(&Vec4::new(5, 6, 7, 8)), 70);
|
||||
assert_eq!(Vector2::new(1, 2).dot(&Vector2::new(3, 4)), 11);
|
||||
assert_eq!(Vector3::new(1, 2, 3).dot(&Vector3::new(4, 5, 6)), 32);
|
||||
assert_eq!(Vector4::new(1, 2, 3, 4).dot(&Vector4::new(5, 6, 7, 8)), 70);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_comp_add() {
|
||||
assert_eq!(Vec2::new(1, 2).comp_add(), 3);
|
||||
assert_eq!(Vec3::new(1, 2, 3).comp_add(), 6);
|
||||
assert_eq!(Vec4::new(1, 2, 3, 4).comp_add(), 10);
|
||||
assert_eq!(Vector2::new(1, 2).comp_add(), 3);
|
||||
assert_eq!(Vector3::new(1, 2, 3).comp_add(), 6);
|
||||
assert_eq!(Vector4::new(1, 2, 3, 4).comp_add(), 10);
|
||||
|
||||
assert_eq!(Vec2::new(3.0, 4.0).comp_add(), 7.0);
|
||||
assert_eq!(Vec3::new(4.0, 5.0, 6.0).comp_add(), 15.0);
|
||||
assert_eq!(Vec4::new(5.0, 6.0, 7.0, 8.0).comp_add(), 26.0);
|
||||
assert_eq!(Vector2::new(3.0, 4.0).comp_add(), 7.0);
|
||||
assert_eq!(Vector3::new(4.0, 5.0, 6.0).comp_add(), 15.0);
|
||||
assert_eq!(Vector4::new(5.0, 6.0, 7.0, 8.0).comp_add(), 26.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_comp_mul() {
|
||||
assert_eq!(Vec2::new(1, 2).comp_mul(), 2);
|
||||
assert_eq!(Vec3::new(1, 2, 3).comp_mul(), 6);
|
||||
assert_eq!(Vec4::new(1, 2, 3, 4).comp_mul(), 24);
|
||||
assert_eq!(Vector2::new(1, 2).comp_mul(), 2);
|
||||
assert_eq!(Vector3::new(1, 2, 3).comp_mul(), 6);
|
||||
assert_eq!(Vector4::new(1, 2, 3, 4).comp_mul(), 24);
|
||||
|
||||
assert_eq!(Vec2::new(3.0, 4.0).comp_mul(), 12.0);
|
||||
assert_eq!(Vec3::new(4.0, 5.0, 6.0).comp_mul(), 120.0);
|
||||
assert_eq!(Vec4::new(5.0, 6.0, 7.0, 8.0).comp_mul(), 1680.0);
|
||||
assert_eq!(Vector2::new(3.0, 4.0).comp_mul(), 12.0);
|
||||
assert_eq!(Vector3::new(4.0, 5.0, 6.0).comp_mul(), 120.0);
|
||||
assert_eq!(Vector4::new(5.0, 6.0, 7.0, 8.0).comp_mul(), 1680.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_comp_min() {
|
||||
assert_eq!(Vec2::new(1, 2).comp_min(), 1);
|
||||
assert_eq!(Vec3::new(1, 2, 3).comp_min(), 1);
|
||||
assert_eq!(Vec4::new(1, 2, 3, 4).comp_min(), 1);
|
||||
assert_eq!(Vector2::new(1, 2).comp_min(), 1);
|
||||
assert_eq!(Vector3::new(1, 2, 3).comp_min(), 1);
|
||||
assert_eq!(Vector4::new(1, 2, 3, 4).comp_min(), 1);
|
||||
|
||||
assert_eq!(Vec2::new(3.0, 4.0).comp_min(), 3.0);
|
||||
assert_eq!(Vec3::new(4.0, 5.0, 6.0).comp_min(), 4.0);
|
||||
assert_eq!(Vec4::new(5.0, 6.0, 7.0, 8.0).comp_min(), 5.0);
|
||||
assert_eq!(Vector2::new(3.0, 4.0).comp_min(), 3.0);
|
||||
assert_eq!(Vector3::new(4.0, 5.0, 6.0).comp_min(), 4.0);
|
||||
assert_eq!(Vector4::new(5.0, 6.0, 7.0, 8.0).comp_min(), 5.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_comp_max() {
|
||||
assert_eq!(Vec2::new(1, 2).comp_max(), 2);
|
||||
assert_eq!(Vec3::new(1, 2, 3).comp_max(), 3);
|
||||
assert_eq!(Vec4::new(1, 2, 3, 4).comp_max(), 4);
|
||||
assert_eq!(Vector2::new(1, 2).comp_max(), 2);
|
||||
assert_eq!(Vector3::new(1, 2, 3).comp_max(), 3);
|
||||
assert_eq!(Vector4::new(1, 2, 3, 4).comp_max(), 4);
|
||||
|
||||
assert_eq!(Vec2::new(3.0, 4.0).comp_max(), 4.0);
|
||||
assert_eq!(Vec3::new(4.0, 5.0, 6.0).comp_max(), 6.0);
|
||||
assert_eq!(Vec4::new(5.0, 6.0, 7.0, 8.0).comp_max(), 8.0);
|
||||
assert_eq!(Vector2::new(3.0, 4.0).comp_max(), 4.0);
|
||||
assert_eq!(Vector3::new(4.0, 5.0, 6.0).comp_max(), 6.0);
|
||||
assert_eq!(Vector4::new(5.0, 6.0, 7.0, 8.0).comp_max(), 8.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_cross() {
|
||||
let a = Vec3::new(1, 2, 3);
|
||||
let b = Vec3::new(4, 5, 6);
|
||||
let r = Vec3::new(-3, 6, -3);
|
||||
let a = Vector3::new(1, 2, 3);
|
||||
let b = Vector3::new(4, 5, 6);
|
||||
let r = Vector3::new(-3, 6, -3);
|
||||
assert_eq!(a.cross(&b), r);
|
||||
|
||||
let mut a = a;
|
||||
|
@ -89,9 +89,9 @@ fn test_cross() {
|
|||
|
||||
#[test]
|
||||
fn test_is_perpendicular() {
|
||||
assert!(Vec2::new(1.0, 0.0).is_perpendicular(&Vec2::new(0.0, 1.0)));
|
||||
assert!(Vec3::new(0.0, 1.0, 0.0).is_perpendicular(&Vec3::new(0.0, 0.0, 1.0)));
|
||||
assert!(Vec4::new(1.0, 0.0, 0.0, 0.0).is_perpendicular(&Vec4::new(0.0, 0.0, 0.0, 1.0)));
|
||||
assert!(Vector2::new(1.0, 0.0).is_perpendicular(&Vector2::new(0.0, 1.0)));
|
||||
assert!(Vector3::new(0.0, 1.0, 0.0).is_perpendicular(&Vector3::new(0.0, 0.0, 1.0)));
|
||||
assert!(Vector4::new(1.0, 0.0, 0.0, 0.0).is_perpendicular(&Vector4::new(0.0, 0.0, 0.0, 1.0)));
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -99,9 +99,9 @@ mod test_length {
|
|||
use cgmath::vector::*;
|
||||
|
||||
#[test]
|
||||
fn test_vec2(){
|
||||
let (a, a_res) = (Vec2::new(3.0, 4.0), 5.0); // (3, 4, 5) Pythagorean triple
|
||||
let (b, b_res) = (Vec2::new(5.0, 12.0), 13.0); // (5, 12, 13) Pythagorean triple
|
||||
fn test_vector2(){
|
||||
let (a, a_res) = (Vector2::new(3.0, 4.0), 5.0); // (3, 4, 5) Pythagorean triple
|
||||
let (b, b_res) = (Vector2::new(5.0, 12.0), 13.0); // (5, 12, 13) Pythagorean triple
|
||||
|
||||
assert_eq!(a.length2(), a_res * a_res);
|
||||
assert_eq!(b.length2(), b_res * b_res);
|
||||
|
@ -111,9 +111,9 @@ mod test_length {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec3(){
|
||||
let (a, a_res) = (Vec3::new(2.0, 3.0, 6.0), 7.0); // (2, 3, 6, 7) Pythagorean quadruple
|
||||
let (b, b_res) = (Vec3::new(1.0, 4.0, 8.0), 9.0); // (1, 4, 8, 9) Pythagorean quadruple
|
||||
fn test_vector3(){
|
||||
let (a, a_res) = (Vector3::new(2.0, 3.0, 6.0), 7.0); // (2, 3, 6, 7) Pythagorean quadruple
|
||||
let (b, b_res) = (Vector3::new(1.0, 4.0, 8.0), 9.0); // (1, 4, 8, 9) Pythagorean quadruple
|
||||
|
||||
assert_eq!(a.length2(), a_res * a_res);
|
||||
assert_eq!(b.length2(), b_res * b_res);
|
||||
|
@ -123,9 +123,9 @@ mod test_length {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec4(){
|
||||
let (a, a_res) = (Vec4::new(1.0, 2.0, 4.0, 10.0), 11.0); // (1, 2, 4, 10, 11) Pythagorean quintuple
|
||||
let (b, b_res) = (Vec4::new(1.0, 2.0, 8.0, 10.0), 13.0); // (1, 2, 8, 10, 13) Pythagorean quintuple
|
||||
fn test_vector4(){
|
||||
let (a, a_res) = (Vector4::new(1.0, 2.0, 4.0, 10.0), 11.0); // (1, 2, 4, 10, 11) Pythagorean quintuple
|
||||
let (b, b_res) = (Vector4::new(1.0, 2.0, 8.0, 10.0), 13.0); // (1, 2, 8, 10, 13) Pythagorean quintuple
|
||||
|
||||
assert_eq!(a.length2(), a_res * a_res);
|
||||
assert_eq!(b.length2(), b_res * b_res);
|
||||
|
@ -137,23 +137,23 @@ mod test_length {
|
|||
|
||||
#[test]
|
||||
fn test_angle() {
|
||||
assert!(Vec2::new(1.0, 0.0).angle(&Vec2::new(0.0, 1.0)).approx_eq( &rad(Float::frac_pi_2()) ));
|
||||
assert!(Vec2::new(10.0, 0.0).angle(&Vec2::new(0.0, 5.0)).approx_eq( &rad(Float::frac_pi_2()) ));
|
||||
assert!(Vec2::new(-1.0, 0.0).angle(&Vec2::new(0.0, 1.0)).approx_eq( &-rad(Float::frac_pi_2()) ));
|
||||
assert!(Vector2::new(1.0, 0.0).angle(&Vector2::new(0.0, 1.0)).approx_eq( &rad(Float::frac_pi_2()) ));
|
||||
assert!(Vector2::new(10.0, 0.0).angle(&Vector2::new(0.0, 5.0)).approx_eq( &rad(Float::frac_pi_2()) ));
|
||||
assert!(Vector2::new(-1.0, 0.0).angle(&Vector2::new(0.0, 1.0)).approx_eq( &-rad(Float::frac_pi_2()) ));
|
||||
|
||||
assert!(Vec3::new(1.0, 0.0, 1.0).angle(&Vec3::new(1.0, 1.0, 0.0)).approx_eq( &rad(Float::frac_pi_3()) ));
|
||||
assert!(Vec3::new(10.0, 0.0, 10.0).angle(&Vec3::new(5.0, 5.0, 0.0)).approx_eq( &rad(Float::frac_pi_3()) ));
|
||||
assert!(Vec3::new(-1.0, 0.0, -1.0).angle(&Vec3::new(1.0, -1.0, 0.0)).approx_eq( &rad(2.0 * Float::frac_pi_3()) ));
|
||||
assert!(Vector3::new(1.0, 0.0, 1.0).angle(&Vector3::new(1.0, 1.0, 0.0)).approx_eq( &rad(Float::frac_pi_3()) ));
|
||||
assert!(Vector3::new(10.0, 0.0, 10.0).angle(&Vector3::new(5.0, 5.0, 0.0)).approx_eq( &rad(Float::frac_pi_3()) ));
|
||||
assert!(Vector3::new(-1.0, 0.0, -1.0).angle(&Vector3::new(1.0, -1.0, 0.0)).approx_eq( &rad(2.0 * Float::frac_pi_3()) ));
|
||||
|
||||
assert!(Vec4::new(1.0, 0.0, 1.0, 0.0).angle(&Vec4::new(0.0, 1.0, 0.0, 1.0)).approx_eq( &rad(Float::frac_pi_2()) ));
|
||||
assert!(Vec4::new(10.0, 0.0, 10.0, 0.0).angle(&Vec4::new(0.0, 5.0, 0.0, 5.0)).approx_eq( &rad(Float::frac_pi_2()) ));
|
||||
assert!(Vec4::new(-1.0, 0.0, -1.0, 0.0).angle(&Vec4::new(0.0, 1.0, 0.0, 1.0)).approx_eq( &rad(Float::frac_pi_2()) ));
|
||||
assert!(Vector4::new(1.0, 0.0, 1.0, 0.0).angle(&Vector4::new(0.0, 1.0, 0.0, 1.0)).approx_eq( &rad(Float::frac_pi_2()) ));
|
||||
assert!(Vector4::new(10.0, 0.0, 10.0, 0.0).angle(&Vector4::new(0.0, 5.0, 0.0, 5.0)).approx_eq( &rad(Float::frac_pi_2()) ));
|
||||
assert!(Vector4::new(-1.0, 0.0, -1.0, 0.0).angle(&Vector4::new(0.0, 1.0, 0.0, 1.0)).approx_eq( &rad(Float::frac_pi_2()) ));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_normalize() {
|
||||
// TODO: test normalize_to, normalize_sel.0, and normalize_self_to
|
||||
assert!(Vec2::new(3.0, 4.0).normalize().approx_eq( &Vec2::new(3.0/5.0, 4.0/5.0) ));
|
||||
assert!(Vec3::new(2.0, 3.0, 6.0).normalize().approx_eq( &Vec3::new(2.0/7.0, 3.0/7.0, 6.0/7.0) ));
|
||||
assert!(Vec4::new(1.0, 2.0, 4.0, 10.0).normalize().approx_eq( &Vec4::new(1.0/11.0, 2.0/11.0, 4.0/11.0, 10.0/11.0) ));
|
||||
assert!(Vector2::new(3.0, 4.0).normalize().approx_eq( &Vector2::new(3.0/5.0, 4.0/5.0) ));
|
||||
assert!(Vector3::new(2.0, 3.0, 6.0).normalize().approx_eq( &Vector3::new(2.0/7.0, 3.0/7.0, 6.0/7.0) ));
|
||||
assert!(Vector4::new(1.0, 2.0, 4.0, 10.0).normalize().approx_eq( &Vector4::new(1.0/11.0, 2.0/11.0, 4.0/11.0, 10.0/11.0) ));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue