diff --git a/src/test/test_vec.rs b/src/test/test_vec.rs index 6c440b0..9a99103 100644 --- a/src/test/test_vec.rs +++ b/src/test/test_vec.rs @@ -49,8 +49,10 @@ fn test_vec2() { assert a.mul_t(f1) == Vec2::new( 1.5f, 3.0f); assert a.div_t(f2) == Vec2::new( 2.0f, 4.0f); - assert a.add_v(&b) == Vec2::new( 4f, 6f); - assert a.sub_v(&b) == Vec2::new(-2f, -2f); + assert a.add_v(&b) == Vec2::new( 4f, 6f); + assert a.sub_v(&b) == Vec2::new( -2f, -2f); + assert a.mul_v(&b) == Vec2::new( 3f, 8f); + assert a.div_v(&b) == Vec2::new(1f/3f, 2f/4f); mut_a.neg_self(); assert mut_a == -a; @@ -70,6 +72,14 @@ fn test_vec2() { mut_a.sub_self_v(&b); assert mut_a == a.sub_v(&b); + mut_a = a; + + mut_a.mul_self_v(&b); + assert mut_a == a.mul_v(&b); + mut_a = a; + + mut_a.div_self_v(&b); + assert mut_a == a.div_v(&b); // mut_a = a; // assert c.abs() == Vec2::new( 2.0f, 1.0f); @@ -192,8 +202,10 @@ fn test_vec3() { assert a.mul_t(f1) == Vec3::new( 1.5f, 3.0f, 4.5f); assert a.div_t(f2) == Vec3::new( 2.0f, 4.0f, 6.0f); - assert a.add_v(&b) == Vec3::new( 5f, 7f, 9f); - assert a.sub_v(&b) == Vec3::new(-3f, -3f, -3f); + assert a.add_v(&b) == Vec3::new( 5f, 7f, 9f); + assert a.sub_v(&b) == Vec3::new( -3f, -3f, -3f); + assert a.mul_v(&b) == Vec3::new( 4f, 10f, 18f); + assert a.div_v(&b) == Vec3::new(1f/4f, 2f/5f, 3f/6f); mut_a.neg_self(); assert mut_a == -a; @@ -213,6 +225,14 @@ fn test_vec3() { mut_a.sub_self_v(&b); assert mut_a == a.sub_v(&b); + mut_a = a; + + mut_a.mul_self_v(&b); + assert mut_a == a.mul_v(&b); + mut_a = a; + + mut_a.div_self_v(&b); + assert mut_a == a.div_v(&b); // mut_a = a; // exact_eq @@ -337,8 +357,10 @@ fn test_vec4() { assert a.mul_t(f1) == Vec4::new( 1.5f, 3.0f, 4.5f, 6.0f); assert a.div_t(f2) == Vec4::new( 2.0f, 4.0f, 6.0f, 8.0f); - assert a.add_v(&b) == Vec4::new( 6f, 8f, 10f, 12f); - assert a.sub_v(&b) == Vec4::new(-4f, -4f, -4f, -4f); + assert a.add_v(&b) == Vec4::new( 6f, 8f, 10f, 12f); + assert a.sub_v(&b) == Vec4::new( -4f, -4f, -4f, -4f); + assert a.mul_v(&b) == Vec4::new( 5f, 12f, 21f, 32f); + assert a.div_v(&b) == Vec4::new(1f/5f, 2f/6f, 3f/7f, 4f/8f); assert a.dot(&b) == 70f; @@ -360,6 +382,14 @@ fn test_vec4() { mut_a.sub_self_v(&b); assert mut_a == a.sub_v(&b); + mut_a = a; + + mut_a.mul_self_v(&b); + assert mut_a == a.mul_v(&b); + mut_a = a; + + mut_a.div_self_v(&b); + assert mut_a == a.div_v(&b); // mut_a = a; // assert c.abs() == Vec4::new( 2.0f, 1.0f, 1.0f, 2.0f); diff --git a/src/vec.rs b/src/vec.rs index e5cce18..925cce5 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -113,19 +113,25 @@ pub trait NumericVector: Vector Neg { pure fn div_t(&self, value: T) -> self; /** - * # Return value - * - * The sum of the vector and `other` + * Component-wise vector addition */ pure fn add_v(&self, other: &self) -> self; /** - * # Return value - * - * The difference between the vector and `other` + * Component-wise vector subtraction */ pure fn sub_v(&self, other: &self) -> self; + /** + * Component-wise vector multiplication + */ + pure fn mul_v(&self, other: &self) -> self; + + /** + * Component-wise vector division + */ + pure fn div_v(&self, other: &self) -> self; + /** * # Return value * @@ -155,14 +161,24 @@ pub trait MutableNumericVector: MutableVector<&self/T> fn div_self_t(&mut self, value: T); /** - * Set to the sum of the vector and `other` + * Set the vector to the component-wise vector sum */ fn add_self_v(&mut self, other: &self); /** - * Set to the difference between the vector and `other` + * Set the vector to the component-wise vector difference */ fn sub_self_v(&mut self, other: &self); + + /** + * Set the vector to the component-wise vector product + */ + fn mul_self_v(&mut self, other: &self); + + /** + * Set the vector to the component-wise vector quotient + */ + fn div_self_v(&mut self, other: &self); } /** diff --git a/src/vec2.rs b/src/vec2.rs index d712f8a..26280ef 100644 --- a/src/vec2.rs +++ b/src/vec2.rs @@ -115,6 +115,18 @@ pub impl Vec2: NumericVector { self[1] - other[1]) } + #[inline(always)] + pure fn mul_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(self[0] * other[0], + self[1] * other[1]) + } + + #[inline(always)] + pure fn div_v(&self, other: &Vec2) -> Vec2 { + Vec2::new(self[0] / other[0], + self[1] / other[1]) + } + #[inline(always)] pure fn dot(&self, other: &Vec2) -> T { self[0] * other[0] + @@ -159,6 +171,18 @@ pub impl Vec2: MutableNumericVector<&self/T> { *self.index_mut(0) -= other[0]; *self.index_mut(1) -= other[1]; } + + #[inline(always)] + fn mul_self_v(&mut self, other: &Vec2) { + *self.index_mut(0) *= other[0]; + *self.index_mut(1) *= other[1]; + } + + #[inline(always)] + fn div_self_v(&mut self, other: &Vec2) { + *self.index_mut(0) /= other[0]; + *self.index_mut(1) /= other[1]; + } } pub impl Vec2: NumericVector2 { diff --git a/src/vec3.rs b/src/vec3.rs index 2dc98e8..90593e4 100644 --- a/src/vec3.rs +++ b/src/vec3.rs @@ -124,6 +124,20 @@ pub impl Vec3: NumericVector { self[2] - other[2]) } + #[inline(always)] + pure fn mul_v(&self, other: &Vec3) -> Vec3{ + Vec3::new(self[0] * other[0], + self[1] * other[1], + self[2] * other[2]) + } + + #[inline(always)] + pure fn div_v(&self, other: &Vec3) -> Vec3{ + Vec3::new(self[0] / other[0], + self[1] / other[1], + self[2] / other[2]) + } + #[inline(always)] pure fn dot(&self, other: &Vec3) -> T { self[0] * other[0] + @@ -174,6 +188,20 @@ pub impl Vec3: MutableNumericVector<&self/T> { *self.index_mut(1) -= other[1]; *self.index_mut(2) -= other[2]; } + + #[inline(always)] + fn mul_self_v(&mut self, other: &Vec3) { + *self.index_mut(0) *= other[0]; + *self.index_mut(1) *= other[1]; + *self.index_mut(2) *= other[2]; + } + + #[inline(always)] + fn div_self_v(&mut self, other: &Vec3) { + *self.index_mut(0) /= other[0]; + *self.index_mut(1) /= other[1]; + *self.index_mut(2) /= other[2]; + } } pub impl Vec3: NumericVector3 { diff --git a/src/vec4.rs b/src/vec4.rs index 6586d7e..9dad13a 100644 --- a/src/vec4.rs +++ b/src/vec4.rs @@ -133,6 +133,22 @@ pub impl Vec4: NumericVector { self[3] - other[3]) } + #[inline(always)] + pure fn mul_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(self[0] * other[0], + self[1] * other[1], + self[2] * other[2], + self[3] * other[3]) + } + + #[inline(always)] + pure fn div_v(&self, other: &Vec4) -> Vec4 { + Vec4::new(self[0] / other[0], + self[1] / other[1], + self[2] / other[2], + self[3] / other[3]) + } + #[inline(always)] pure fn dot(&self, other: &Vec4) -> T { self[0] * other[0] + @@ -189,6 +205,22 @@ pub impl Vec4: MutableNumericVector<&self/T> { *self.index_mut(2) -= other[2]; *self.index_mut(3) -= other[3]; } + + #[inline(always)] + fn mul_self_v(&mut self, other: &Vec4) { + *self.index_mut(0) *= other[0]; + *self.index_mut(1) *= other[1]; + *self.index_mut(2) *= other[2]; + *self.index_mut(3) *= other[3]; + } + + #[inline(always)] + fn div_self_v(&mut self, other: &Vec4) { + *self.index_mut(0) /= other[0]; + *self.index_mut(1) /= other[1]; + *self.index_mut(2) /= other[2]; + *self.index_mut(3) /= other[3]; + } } pub impl Vec4: EuclideanVector {