diff --git a/src/test/test_vec.rs b/src/test/test_vec.rs index 28abb7b..eed05ba 100644 --- a/src/test/test_vec.rs +++ b/src/test/test_vec.rs @@ -130,7 +130,7 @@ fn test_Vec3() { mut_a.swap(1, 2); assert mut_a[1] == a[2]; assert mut_a[2] == a[1]; - let mut mut_a = a; + mut_a = a; assert a.x == 1f; assert a.y == 2f; @@ -141,6 +141,10 @@ fn test_Vec3() { assert a.cross(&b) == Vec3::new(-3f, 6f, -3f); + mut_a.cross_self(&b); + assert mut_a == a.cross(&b); + mut_a = a; + assert -a == Vec3::new(-1f, -2f, -3f); assert a.neg() == Vec3::new(-1f, -2f, -3f); diff --git a/src/vec.rs b/src/vec.rs index 7220180..487cd3d 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -143,6 +143,16 @@ pub trait NumericVector3: NumericVector { pure fn cross(&self, other: &self) -> self; } +/** + * A mutable 3-dimensional vector with numeric components + */ +pub trait MutableNumericVector3: MutableNumericVector<&self/T> { + /** + * Set to the cross product of the vector and `other` + */ + fn cross_self(&mut self, other: &self); +} + /** * A 4-dimensional vector with numeric components */ @@ -525,15 +535,6 @@ pub impl Vec3: Neg> { } } -pub impl Vec3: NumericVector3 { - #[inline(always)] - pure fn cross(&self, other: &Vec3) -> Vec3 { - Vec3::new((self[1] * other[2]) - (self[2] * other[1]), - (self[2] * other[0]) - (self[0] * other[2]), - (self[0] * other[1]) - (self[1] * other[0])) - } -} - pub impl Vec3: MutableNumericVector<&self/T> { #[inline(always)] fn neg_self(&mut self) { @@ -571,6 +572,22 @@ pub impl Vec3: MutableNumericVector<&self/T> { } } +pub impl Vec3: NumericVector3 { + #[inline(always)] + pure fn cross(&self, other: &Vec3) -> Vec3 { + Vec3::new((self[1] * other[2]) - (self[2] * other[1]), + (self[2] * other[0]) - (self[0] * other[2]), + (self[0] * other[1]) - (self[1] * other[0])) + } +} + +pub impl Vec3: MutableNumericVector3<&self/T> { + #[inline(always)] + fn cross_self(&mut self, other: &Vec3) { + *self = self.cross(other); + } +} + pub impl Vec3: GeometricVector { #[inline(always)] pure fn length2(&self) -> T {