From fb59c90b887a63c883bcec45018c2f03133f721e Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Fri, 7 Dec 2012 19:00:50 -0500 Subject: [PATCH 1/2] More explicit self. --- src/angle.rs | 10 +++++----- src/color/color.rs | 18 +++++++++--------- src/funs/common.rs | 8 ++++---- src/mat.rs | 18 +++++++++--------- src/quat.rs | 6 +++--- src/vec.rs | 14 +++++++------- 6 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/angle.rs b/src/angle.rs index 03764da..8c7b18e 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -70,8 +70,8 @@ pub impl Radians: Angle { pub impl Radians: Add, Radians> { #[inline(always)] - pure fn add(rhs: &Radians) -> Radians { - Radians(*self + **rhs) + pure fn add(&self, rhs: &Radians) -> Radians { + Radians(**self + **rhs) } } @@ -164,8 +164,8 @@ pub impl Degrees: Angle { pub impl Degrees: Add, Degrees> { #[inline(always)] - pure fn add(rhs: &Degrees) -> Degrees { - Degrees(*self + **rhs) + pure fn add(&self, rhs: &Degrees) -> Degrees { + Degrees(**self + **rhs) } } @@ -277,4 +277,4 @@ pub struct Euler { x: Radians, // pitch y: Radians, // yaw z: Radians, // roll -} \ No newline at end of file +} diff --git a/src/color/color.rs b/src/color/color.rs index 522556f..fa93f02 100644 --- a/src/color/color.rs +++ b/src/color/color.rs @@ -259,7 +259,7 @@ pub impl RGB: Dimensional { pub impl RGB: Index { #[inline(always)] - pure fn index(i: uint) -> T { + pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } } } } @@ -269,7 +269,7 @@ pub impl RGB: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*RGB, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -421,7 +421,7 @@ pub impl RGBA: Dimensional { pub impl RGBA: Index { #[inline(always)] - pure fn index(i: uint) -> T { + pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } } } @@ -431,7 +431,7 @@ pub impl RGBA: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*RGBA, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -579,7 +579,7 @@ pub impl HSV: Dimensional { pub impl HSV: Index { #[inline(always)] - pure fn index(i: uint) -> T { + pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } } } } @@ -589,7 +589,7 @@ pub impl HSV: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*HSV, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -717,7 +717,7 @@ pub impl HSVA: Dimensional { pub impl HSVA: Index { #[inline(always)] - pure fn index(i: uint) -> T { + pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } } } @@ -727,7 +727,7 @@ pub impl HSVA: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*HSVA, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -814,4 +814,4 @@ pub impl HSVA: Eq { pure fn ne(&self, other: &HSVA) -> bool { !(self == other) } -} \ No newline at end of file +} diff --git a/src/funs/common.rs b/src/funs/common.rs index 160726f..a5baff9 100644 --- a/src/funs/common.rs +++ b/src/funs/common.rs @@ -139,7 +139,7 @@ pub impl f32: Approx { #[inline(always)] pure fn round(&self) -> f32 { f32::round(*self) } // #[inline(always)] pure fn roundEven(&self) -> f32 {} #[inline(always)] pure fn ceil(&self) -> f32 { f32::ceil(*self) } - #[inline(always)] pure fn fract(&self) -> f32 { (*self) - floor(&*self) } + #[inline(always)] pure fn fract(&self) -> f32 { (*self) - floor(self) } } pub impl f64: Approx { @@ -148,7 +148,7 @@ pub impl f64: Approx { #[inline(always)] pure fn round(&self) -> f64 { f64::round(*self) } // #[inline(always)] pure fn roundEven(&self) -> f64 {} #[inline(always)] pure fn ceil(&self) -> f64 { f64::ceil(*self) } - #[inline(always)] pure fn fract(&self) -> f64 { (*self) - floor(&*self) } + #[inline(always)] pure fn fract(&self) -> f64 { (*self) - floor(self) } } pub impl float: Approx { @@ -157,7 +157,7 @@ pub impl float: Approx { #[inline(always)] pure fn round(&self) -> float { f64::round(*self as f64) as float } // #[inline(always)] pure fn roundEven(&self) -> float {} #[inline(always)] pure fn ceil(&self) -> float { f64::ceil(*self as f64) as float } - #[inline(always)] pure fn fract(&self) -> float { (*self) - floor(&*self) } + #[inline(always)] pure fn fract(&self) -> float { (*self) - floor(self) } } @@ -804,4 +804,4 @@ pub impl Vec4: Mix { self[2].step(&edge[2]), self[3].step(&edge[3])) } -} \ No newline at end of file +} diff --git a/src/mat.rs b/src/mat.rs index e9fd8de..3188d52 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -608,10 +608,10 @@ pub impl Mat2: Dimensional> { pub impl Mat2: Index> { #[inline(always)] - pure fn index(i: uint) -> Vec2 { + pure fn index(&self, i: uint) -> Vec2 { unsafe { do buf_as_slice( transmute::<*Mat2, *Vec2>( - to_unsafe_ptr(&self)), 2) |slice| { slice[i] } + to_unsafe_ptr(self)), 2) |slice| { slice[i] } } } } @@ -621,7 +621,7 @@ pub impl Mat2: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Mat2, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -1105,10 +1105,10 @@ pub impl Mat3: Dimensional> { pub impl Mat3: Index> { #[inline(always)] - pure fn index(i: uint) -> Vec3 { + pure fn index(&self, i: uint) -> Vec3 { unsafe { do buf_as_slice( transmute::<*Mat3, *Vec3>( - to_unsafe_ptr(&self)), 3) |slice| { slice[i] } + to_unsafe_ptr(self)), 3) |slice| { slice[i] } } } } @@ -1118,7 +1118,7 @@ pub impl Mat3: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Mat3, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -1679,10 +1679,10 @@ pub impl Mat4: Dimensional> { pub impl Mat4: Index> { #[inline(always)] - pure fn index(i: uint) -> Vec4 { + pure fn index(&self, i: uint) -> Vec4 { unsafe { do buf_as_slice( transmute::<*Mat4, *Vec4>( - to_unsafe_ptr(&self)), 4) |slice| { slice[i] } + to_unsafe_ptr(self)), 4) |slice| { slice[i] } } } } @@ -1692,7 +1692,7 @@ pub impl Mat4: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Mat4, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } diff --git a/src/quat.rs b/src/quat.rs index bb1f1d0..28d83df 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -241,7 +241,7 @@ pub impl Quat: Dimensional { pub impl Quat: Index { #[inline(always)] - pure fn index(i: uint) -> T { + pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } } } @@ -251,7 +251,7 @@ pub impl Quat: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Quat, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -454,4 +454,4 @@ pub impl Quat: FuzzyEq { self[2].fuzzy_eq(&other[2]) && self[3].fuzzy_eq(&other[3]) } -} \ No newline at end of file +} diff --git a/src/vec.rs b/src/vec.rs index 61d376d..84d0b2a 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -314,7 +314,7 @@ pub impl Vec2: Dimensional { pub impl Vec2: Index { #[inline(always)] - pure fn index(i: uint) -> T { + pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 2) |slice| { slice[i] } } } } @@ -324,7 +324,7 @@ pub impl Vec2: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Vec2, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -550,7 +550,7 @@ pub impl Vec3: Dimensional { pub impl Vec3: Index { #[inline(always)] - pure fn index(i: uint) -> T { + pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 3) |slice| { slice[i] } } } } @@ -560,7 +560,7 @@ pub impl Vec3: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Vec3, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -818,7 +818,7 @@ pub impl Vec4: Dimensional { pub impl Vec4: Index { #[inline(always)] - pure fn index(i: uint) -> T { + pure fn index(&self, i: uint) -> T { unsafe { do buf_as_slice(self.to_ptr(), 4) |slice| { slice[i] } } } } @@ -828,7 +828,7 @@ pub impl Vec4: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Vec4, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -1037,4 +1037,4 @@ pub impl Vec4: DefaultEq { self[2].default_eq(&other[2]) && self[3].default_eq(&other[3]) } -} \ No newline at end of file +} From d74cdb0dfa0e7e379e310e324175118bcd01c8de Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Fri, 7 Dec 2012 19:09:03 -0500 Subject: [PATCH 2/2] 4x4 matrix inversion now uses the mutable self operators. --- src/mat.rs | 52 +++++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/src/mat.rs b/src/mat.rs index 3188d52..c6794e2 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -1439,7 +1439,7 @@ pub impl Mat4: Matrix> { self[0][0] + self[1][1] + self[2][2] + self[3][3] } - pure fn inverse(&self) -> Option> { + pure fn inverse(&self) -> Option> unsafe { let d = self.determinant(); // let _0 = Number::from(0); // FIXME: Triggers ICE let _0 = cast(0); @@ -1448,55 +1448,41 @@ pub impl Mat4: Matrix> { } else { // Gauss Jordan Elimination with partial pivoting + // So take this matrix, A, augmented with the identity + // and essentially reduce [A|I] - // TODO: use column/row swapping methods. Check with Luqman to see - // if the column-major layout has been used correctly + let mut A = *self; + // let mut I: Mat4 = Matrix::identity(); // FIXME: there's something wrong with static functions here! + let mut I = Mat4::identity(); - let mut a = *self; - // let mut inv: Mat4 = Matrix::identity(); // FIXME: there's something wrong with static functions here! - let mut inv = Mat4::identity(); - - // Find largest pivot column j among rows j..3 for uint::range(0, 4) |j| { + // Find largest element in col j let mut i1 = j; for uint::range(j + 1, 4) |i| { - if abs(&a[i][j]) > abs(&a[i1][j]) { + if abs(&A[j][i]) > abs(&A[j][i1]) { i1 = i; } } - // Swap rows i1 and j in a and inv to + // Swap columns i1 and j in A and I to // put pivot on diagonal - let c = [mut a.x, a.y, a.z, a.w]; - c[i1] <-> c[j]; - a = Mat4::from_cols(c[0], c[1], c[2], c[3]); - let c = [mut inv.x, inv.y, inv.z, inv.w]; - c[i1] <-> c[j]; - inv = Mat4::from_cols(c[0], c[1], c[2], c[3]); + A.swap_cols(i1, j); + I.swap_cols(i1, j); - // Scale row j to have a unit diagonal - let c = [mut inv.x, inv.y, inv.z, inv.w]; - c[j] = c[j].div_t(a[j][j]); - inv = Mat4::from_cols(c[0], c[1], c[2], c[3]); - let c = [mut a.x, a.y, a.z, a.w]; - c[j] = c[j].div_t(a[j][j]); - a = Mat4::from_cols(c[0], c[1], c[2], c[3]); + // Scale col j to have a unit diagonal + I.col_mut(j).div_self_t(&A[j][j]); + A.col_mut(j).div_self_t(&A[j][j]); - // Eliminate off-diagonal elems in col j of a, - // doing identical ops to inv + // Eliminate off-diagonal elems in col j of A, + // doing identical ops to I for uint::range(0, 4) |i| { if i != j { - let c = [mut inv.x, inv.y, inv.z, inv.w]; - c[i] = c[i].sub_v(&c[j].mul_t(a[i][j])); - inv = Mat4::from_cols(c[0], c[1], c[2], c[3]); - - let c = [mut a.x, a.y, a.z, a.w]; - c[i] = c[i].sub_v(&c[j].mul_t(a[i][j])); - a = Mat4::from_cols(c[0], c[1], c[2], c[3]); + I.col_mut(i).sub_self_v(&I[j].mul_t(A[i][j])); + A.col_mut(i).sub_self_v(&A[j].mul_t(A[i][j])); } } } - Some(inv) + Some(I) } }