diff --git a/src/angle.rs b/src/angle.rs index fb47410..753c5ea 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -69,8 +69,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) } } @@ -170,8 +170,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) } } diff --git a/src/color/color.rs b/src/color/color.rs index 8f804d8..b455a1e 100644 --- a/src/color/color.rs +++ b/src/color/color.rs @@ -267,7 +267,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] } } } } @@ -277,7 +277,7 @@ pub impl RGB: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*RGB, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -429,7 +429,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] } } } } @@ -439,7 +439,7 @@ pub impl RGBA: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*RGBA, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -587,7 +587,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] } } } } @@ -597,7 +597,7 @@ pub impl HSV: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*HSV, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -725,7 +725,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] } } } } @@ -735,7 +735,7 @@ pub impl HSVA: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*HSVA, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -822,4 +822,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 7d1014d..c1fce06 100644 --- a/src/mat.rs +++ b/src/mat.rs @@ -611,10 +611,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] } } } } @@ -624,7 +624,7 @@ pub impl Mat2: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Mat2, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -1127,10 +1127,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] } } } } @@ -1140,7 +1140,7 @@ pub impl Mat3: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Mat3, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -1461,7 +1461,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); @@ -1470,55 +1470,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) } } @@ -1701,10 +1687,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] } } } } @@ -1714,7 +1700,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 2e333b9..51e1ac6 100644 --- a/src/quat.rs +++ b/src/quat.rs @@ -244,7 +244,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] } } } } @@ -254,7 +254,7 @@ pub impl Quat: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Quat, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -467,4 +467,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 9ae671b..5c65212 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -313,7 +313,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] } } } } @@ -323,7 +323,7 @@ pub impl Vec2: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Vec2, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -542,7 +542,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] } } } } @@ -552,7 +552,7 @@ pub impl Vec3: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Vec3, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } } @@ -803,7 +803,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] } } } } @@ -813,7 +813,7 @@ pub impl Vec4: ToPtr { pure fn to_ptr(&self) -> *T { unsafe { transmute::<*Vec4, *T>( - to_unsafe_ptr(&*self) + to_unsafe_ptr(self) ) } }