diff --git a/src/test/test_vec.rs b/src/test/test_vec.rs index eed05ba..e13245b 100644 --- a/src/test/test_vec.rs +++ b/src/test/test_vec.rs @@ -81,6 +81,8 @@ fn test_Vec2_geometric() { let b0 = Vec2::new(3f, 4f); // (3, 4, 5) Pythagorean triple let b = a.add_v(&b0); + // TODO: test normalize and normalize_self + assert a.length() == 13f; assert a.length2() == 13f * 13f; @@ -94,6 +96,10 @@ fn test_Vec2_geometric() { let d = Vec2::new( 1.0f, 0.0f); assert c.lerp(&d, 0.75f) == Vec2::new(0.250f, -0.250f); + + let mut mut_c = c; + mut_c.lerp_self(&d, &0.75f); + assert mut_c == c.lerp(&d, 0.75f); } #[test] @@ -189,6 +195,8 @@ fn test_Vec3_geometric() { let b0 = Vec3::new(1f, 4f, 8f); // (1, 4, 8, 9) Pythagorean quadruple let b = a.add_v(&b0); + // TODO: test normalize and normalize_self + assert a.length() == 7f; assert a.length2() == 7f * 7f; @@ -202,6 +210,10 @@ fn test_Vec3_geometric() { let d = Vec3::new( 1.0f, 0.0f, 0.5f); assert c.lerp(&d, 0.75f) == Vec3::new(0.250f, -0.250f, 0.625f); + + let mut mut_c = c; + mut_c.lerp_self(&d, &0.75f); + assert mut_c == c.lerp(&d, 0.75f); } #[test] @@ -297,6 +309,8 @@ fn test_Vec4_geometric() { let b0 = Vec4::new(1f, 2f, 8f, 10f); // (1, 2, 8, 10, 13) Pythagorean quintuple let b = a.add_v(&b0); + // TODO: test normalize and normalize_self + assert a.length() == 11f; assert a.length2() == 11f * 11f; @@ -310,4 +324,8 @@ fn test_Vec4_geometric() { let d = Vec4::new( 1.0f, 0.0f, 0.5f, 1.0f); assert c.lerp(&d, 0.75f) == Vec4::new(0.250f, -0.250f, 0.625f, 1.250f); + + let mut mut_c = c; + mut_c.lerp_self(&d, &0.75f); + assert mut_c == c.lerp(&d, 0.75f); } \ No newline at end of file diff --git a/src/vec.rs b/src/vec.rs index 487cd3d..1fc005e 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -77,7 +77,7 @@ pub trait NumericVector: Vector, Neg { pure fn div_t(&self, value: T) -> self; /** - * Returns the sum of the vector with `other` + * Returns the sum of the vector and `other` */ pure fn add_v(&self, other: &self) -> self; @@ -112,12 +112,12 @@ pub trait MutableNumericVector: MutableVector<&self/T>, NumericVector { fn div_self_t(&mut self, value: T); /** - * Add a vector + * Set to the sum of the vector and `other` */ fn add_self_v(&mut self, other: &self); /** - * Subtract a vector + * Set to the difference between the vector and `other` */ fn sub_self_v(&mut self, other: &self); } @@ -137,6 +137,7 @@ pub trait NumericVector3: NumericVector { // static pure fn unit_x() -> self; // static pure fn unit_y() -> self; // static pure fn unit_z() -> self; + /** * Returns the cross product of the vector and `other` */ @@ -198,6 +199,19 @@ pub trait GeometricVector: NumericVector { pure fn lerp(&self, other: &self, amount: T) -> self; } +pub trait MutableGeometricVector: MutableNumericVector<&self/T>, + GeometricVector { + /** + * Normalize the vector + */ + fn normalize_self(&mut self); + + /** + * Linearly intoperlate the vector towards `other` + */ + fn lerp_self(&mut self, other: &self, amount: T); +} + @@ -381,6 +395,19 @@ pub impl Vec2: GeometricVector { } } +pub impl Vec2: MutableGeometricVector<&self/T> { + #[inline(always)] + fn normalize_self(&mut self) { + let mut n: T = Number::from(1); + n /= self.length(); + self.mul_self_t(&n); + } + + fn lerp_self(&mut self, other: &Vec2, amount: &T) { + self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); + } +} + pub impl Vec2: Eq { #[inline(always)] pure fn eq(&self, other: &Vec2) -> bool { @@ -622,6 +649,19 @@ pub impl Vec3: GeometricVector { } } +pub impl Vec3: MutableGeometricVector<&self/T> { + #[inline(always)] + fn normalize_self(&mut self) { + let mut n: T = Number::from(1); + n /= self.length(); + self.mul_self_t(&n); + } + + fn lerp_self(&mut self, other: &Vec3, amount: &T) { + self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); + } +} + pub impl Vec3: Eq { #[inline(always)] pure fn eq(&self, other: &Vec3) -> bool { @@ -862,6 +902,19 @@ pub impl Vec4: GeometricVector { } } +pub impl Vec4: MutableGeometricVector<&self/T> { + #[inline(always)] + fn normalize_self(&mut self) { + let mut n: T = Number::from(1); + n /= self.length(); + self.mul_self_t(&n); + } + + fn lerp_self(&mut self, other: &Vec4, amount: &T) { + self.add_self_v(&other.sub_v(&*self).mul_t(*amount)); + } +} + pub impl Vec4: Eq { #[inline(always)] pure fn eq(&self, other: &Vec4) -> bool {