From 12498701a149c3b564f91a00d63c21704f86c06f Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Fri, 2 Nov 2012 00:06:12 +1000 Subject: [PATCH] Convert vector arrays to separate fields in matrix types --- src/matrix.rs | 100 ++++++++++++++++++++++------------------ src/test/test_matrix.rs | 36 +++++++-------- 2 files changed, 74 insertions(+), 62 deletions(-) diff --git a/src/matrix.rs b/src/matrix.rs index 836a570..bf533b5 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1,6 +1,9 @@ -use std::cmp::FuzzyEq; +use cast::transmute; use cmp::Eq; use num::from_int; +use ptr::to_unsafe_ptr; +use vec::raw::buf_as_slice; +use std::cmp::FuzzyEq; use math::*; use num_util::*; @@ -15,8 +18,8 @@ pub trait Matrix { pure fn cols() -> uint; pure fn is_col_major() -> bool; - pure fn row(i: uint) -> V; pure fn col(i: uint) -> V; + pure fn row(i: uint) -> V; pure fn mul_t(value: T) -> self; pure fn mul_v(other: &V) -> V; @@ -57,7 +60,7 @@ pub trait Matrix4 { // // Mat2: A 2x2, column major matrix // -pub struct Mat2 { data:[Vec2 * 2] } +pub struct Mat2 { x: Vec2, y: Vec2 } pub mod Mat2 { @@ -70,19 +73,20 @@ pub mod Mat2 { #[inline(always)] pub pure fn from_cols(col0: &Vec2, col1: &Vec2) -> Mat2 { - Mat2 { data: [ *col0, *col1 ] } + Mat2 { x: *col0, + y: *col1 } } #[inline(always)] pub pure fn zero() -> Mat2 { - Mat2 { data: [ Vec2::zero(), - Vec2::zero() ] } + Mat2 { x: Vec2::zero(), + y: Vec2::zero() } } #[inline(always)] pub pure fn identity() -> Mat2 { - Mat2 { data: [ Vec2::unit_x(), - Vec2::unit_y() ] } + Mat2 { x: Vec2::unit_x(), + y: Vec2::unit_y() } } } @@ -96,17 +100,15 @@ pub impl Mat2: Matrix> { #[inline(always)] pure fn is_col_major() -> bool { true } + #[inline(always)] + pure fn col(i: uint) -> Vec2 { self[i] } + #[inline(always)] pure fn row(i: uint) -> Vec2 { Vec2::new(self[0][i], self[1][i]) } - #[inline(always)] - pure fn col(i: uint) -> Vec2 { - self.data[i] - } - #[inline(always)] pure fn mul_t(value: T) -> Mat2 { Mat2::from_cols(&self[0].mul_t(value), @@ -176,7 +178,10 @@ pub impl Mat2: Matrix> { pub impl Mat2: Index> { #[inline(always)] pure fn index(i: uint) -> Vec2 { - self.data[i] + unsafe { do buf_as_slice( + transmute::<*Mat2, *Vec2>( + to_unsafe_ptr(&self)), 2) |slice| { slice[i] } + } } } @@ -224,7 +229,7 @@ pub impl Mat2: FuzzyEq { // // Mat3: A 3x3, column major matrix // -pub struct Mat3 { data:[Vec3 * 3] } +pub struct Mat3 { x: Vec3, y: Vec3, z: Vec3 } pub mod Mat3 { @@ -239,21 +244,23 @@ pub mod Mat3 { #[inline(always)] pub pure fn from_cols(col0: &Vec3, col1: &Vec3, col2: &Vec3) -> Mat3 { - Mat3 { data: [ *col0, *col1, *col2 ] } + Mat3 { x: *col0, + y: *col1, + z: *col2 } } #[inline(always)] pub pure fn zero() -> Mat3 { - Mat3 { data: [ Vec3::zero(), - Vec3::zero(), - Vec3::zero() ] } + Mat3 { x: Vec3::zero(), + y: Vec3::zero(), + z: Vec3::zero() } } #[inline(always)] pub pure fn identity() -> Mat3 { - Mat3 { data: [ Vec3::unit_x(), - Vec3::unit_y(), - Vec3::unit_z() ] } + Mat3 { x: Vec3::unit_x(), + y: Vec3::unit_y(), + z: Vec3::unit_z() } } } @@ -267,6 +274,9 @@ pub impl Mat3: Matrix> { #[inline(always)] pure fn is_col_major() -> bool { true } + #[inline(always)] + pure fn col(i: uint) -> Vec3 { self[i] } + #[inline(always)] pure fn row(i: uint) -> Vec3 { Vec3::new(self[0][i], @@ -274,11 +284,6 @@ pub impl Mat3: Matrix> { self[2][i]) } - #[inline(always)] - pure fn col(i: uint) -> Vec3 { - self.data[i] - } - #[inline(always)] pure fn mul_t(value: T) -> Mat3 { Mat3::from_cols(&self[0].mul_t(value), @@ -431,7 +436,10 @@ pub impl Mat3: ToQuat { pub impl Mat3: Index> { #[inline(always)] pure fn index(i: uint) -> Vec3 { - self.data[i] + unsafe { do buf_as_slice( + transmute::<*Mat3, *Vec3>( + to_unsafe_ptr(&self)), 3) |slice| { slice[i] } + } } } @@ -488,7 +496,7 @@ pub impl Mat3: ToPtr { // // Mat4: A 4x4, column major matrix // -pub struct Mat4 { data:[Vec4 * 4] } +pub struct Mat4 { x: Vec4, y: Vec4, z: Vec4, w: Vec4 } pub mod Mat4 { @@ -505,23 +513,26 @@ pub mod Mat4 { #[inline(always)] pub pure fn from_cols(col0: &Vec4, col1: &Vec4, col2: &Vec4, col3: &Vec4) -> Mat4 { - Mat4 { data: [ *col0, *col1, *col2, *col3 ] } + Mat4 { x: *col0, + y: *col1, + z: *col2, + w: *col3 } } #[inline(always)] pub pure fn zero() -> Mat4 { - Mat4 { data: [ Vec4::zero(), - Vec4::zero(), - Vec4::zero(), - Vec4::zero() ] } + Mat4 { x: Vec4::zero(), + y: Vec4::zero(), + z: Vec4::zero(), + w: Vec4::zero() } } #[inline(always)] pub pure fn identity() -> Mat4 { - Mat4 { data: [ Vec4::unit_x(), - Vec4::unit_y(), - Vec4::unit_z(), - Vec4::unit_w() ] } + Mat4 { x: Vec4::unit_x(), + y: Vec4::unit_y(), + z: Vec4::unit_z(), + w: Vec4::unit_w() } } } @@ -535,6 +546,9 @@ pub impl Mat4: Matrix> { #[inline(always)] pure fn is_col_major() -> bool { true } + #[inline(always)] + pure fn col(i: uint) -> Vec4 { self[i] } + #[inline(always)] pure fn row(i: uint) -> Vec4 { Vec4::new(self[0][i], @@ -543,11 +557,6 @@ pub impl Mat4: Matrix> { self[3][i]) } - #[inline(always)] - pure fn col(i: uint) -> Vec4 { - self.data[i] - } - #[inline(always)] pure fn mul_t(value: T) -> Mat4 { Mat4::from_cols(&self[0].mul_t(value), @@ -688,7 +697,10 @@ pub impl Mat4: Matrix4 { pub impl Mat4: Index> { #[inline(always)] pure fn index(i: uint) -> Vec4 { - self.data[i] + unsafe { do buf_as_slice( + transmute::<*Mat4, *Vec4>( + to_unsafe_ptr(&self)), 4) |slice| { slice[i] } + } } } diff --git a/src/test/test_matrix.rs b/src/test/test_matrix.rs index 0856706..af82db5 100644 --- a/src/test/test_matrix.rs +++ b/src/test/test_matrix.rs @@ -5,10 +5,10 @@ use vector::*; #[test] fn test_Mat2() { - let a = Mat2 { data: [ Vec2 { x: 1f, y: 3f }, - Vec2 { x: 2f, y: 4f } ] }; - let b = Mat2 { data: [ Vec2 { x: 2f, y: 4f }, - Vec2 { x: 3f, y: 5f } ] }; + let a = Mat2 { x: Vec2 { x: 1f, y: 3f }, + y: Vec2 { x: 2f, y: 4f } }; + let b = Mat2 { x: Vec2 { x: 2f, y: 4f }, + y: Vec2 { x: 3f, y: 5f } }; let v1 = Vec2::new(1f, 2f); let f1 = 0.5f; @@ -69,12 +69,12 @@ fn test_Mat2() { #[test] fn test_Mat3() { - let a = Mat3 { data: [ Vec3 { x: 1f, y: 4f, z: 7f }, - Vec3 { x: 2f, y: 5f, z: 8f }, - Vec3 { x: 3f, y: 6f, z: 9f } ] }; - let b = Mat3 { data: [ Vec3 { x: 2f, y: 5f, z: 8f }, - Vec3 { x: 3f, y: 6f, z: 9f }, - Vec3 { x: 4f, y: 7f, z: 10f } ] }; + let a = Mat3 { x: Vec3 { x: 1f, y: 4f, z: 7f }, + y: Vec3 { x: 2f, y: 5f, z: 8f }, + z: Vec3 { x: 3f, y: 6f, z: 9f } }; + let b = Mat3 { x: Vec3 { x: 2f, y: 5f, z: 8f }, + y: Vec3 { x: 3f, y: 6f, z: 9f }, + z: Vec3 { x: 4f, y: 7f, z: 10f } }; let v1 = Vec3::new(1f, 2f, 3f); let f1 = 0.5f; @@ -154,14 +154,14 @@ fn test_Mat3() { #[test] fn test_Mat4() { - let a = Mat4 { data: [ Vec4 { x: 1f, y: 5f, z: 9f, w: 13f }, - Vec4 { x: 2f, y: 6f, z: 10f, w: 14f }, - Vec4 { x: 3f, y: 7f, z: 11f, w: 15f }, - Vec4 { x: 4f, y: 8f, z: 12f, w: 16f } ] }; - let b = Mat4 { data: [ Vec4 { x: 2f, y: 6f, z: 10f, w: 14f }, - Vec4 { x: 3f, y: 7f, z: 11f, w: 15f }, - Vec4 { x: 4f, y: 8f, z: 12f, w: 16f }, - Vec4 { x: 5f, y: 9f, z: 13f, w: 17f } ] }; + let a = Mat4 { x: Vec4 { x: 1f, y: 5f, z: 9f, w: 13f }, + y: Vec4 { x: 2f, y: 6f, z: 10f, w: 14f }, + z: Vec4 { x: 3f, y: 7f, z: 11f, w: 15f }, + w: Vec4 { x: 4f, y: 8f, z: 12f, w: 16f } }; + let b = Mat4 { x: Vec4 { x: 2f, y: 6f, z: 10f, w: 14f }, + y: Vec4 { x: 3f, y: 7f, z: 11f, w: 15f }, + z: Vec4 { x: 4f, y: 8f, z: 12f, w: 16f }, + w: Vec4 { x: 5f, y: 9f, z: 13f, w: 17f } }; let v1 = Vec4::new(1f, 2f, 3f, 4f); let f1 = 0.5f;