diff --git a/Cargo.toml b/Cargo.toml index a396ec5..0aa8c88 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "cgmath" -version = "0.1.2" +version = "0.1.3" authors = ["Brendan Zabarauskas ", "Brian Heylin", "Colin Sherratt", diff --git a/src/matrix.rs b/src/matrix.rs index 2d7f9e6..8720071 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -44,7 +44,7 @@ pub struct Matrix3 { pub x: Vector3, pub y: Vector3, pub z: Vector3 pub struct Matrix4 { pub x: Vector4, pub y: Vector4, pub z: Vector4, pub w: Vector4 } -impl Matrix2 { +impl Matrix2 { /// Create a new matrix, providing values for each index. #[inline] pub fn new(c0r0: S, c0r1: S, @@ -58,7 +58,9 @@ impl Matrix2 { pub fn from_cols(c0: Vector2, c1: Vector2) -> Matrix2 { Matrix2 { x: c0, y: c1 } } +} +impl Matrix2 { /// Create a new diagonal matrix, providing a single value to use for each /// non-zero index. #[inline] @@ -98,6 +100,15 @@ impl Matrix2 { } } +impl> Matrix2 { + /// Negate this `Matrix2` in-place. + #[inline] + pub fn neg_self(&mut self) { + (&mut self[0]).neg_self(); + (&mut self[1]).neg_self(); + } +} + impl Matrix3 { /// Create a new matrix, providing values for each index. #[inline] @@ -137,8 +148,7 @@ impl Matrix3 { } } -impl -Matrix3 { +impl Matrix3 { /// Create a transformation matrix that will cause a vector to point at /// `dir`, using `up` for orientation. pub fn look_at(dir: &Vector3, up: &Vector3) -> Matrix3 { @@ -220,6 +230,16 @@ Matrix3 { } } +impl> Matrix3 { + /// Negate this `Matrix3` in-place. + #[inline] + pub fn neg_self(&mut self) { + (&mut self[0]).neg_self(); + (&mut self[1]).neg_self(); + (&mut self[2]).neg_self(); + } +} + impl Matrix4 { /// Create a new matrix, providing values for each index. #[inline] @@ -271,8 +291,7 @@ impl Matrix4 { } } -impl -Matrix4 { +impl Matrix4 { /// Create a transformation matrix that will cause a vector to point at /// `dir`, using `up` for orientation. pub fn look_at(eye: &Point3, center: &Point3, up: &Vector3) -> Matrix4 { @@ -287,8 +306,18 @@ Matrix4 { } } +impl> Matrix4 { + /// Negate this `Matrix4` in-place. + #[inline] + pub fn neg_self(&mut self) { + (&mut self[0]).neg_self(); + (&mut self[1]).neg_self(); + (&mut self[2]).neg_self(); + (&mut self[3]).neg_self(); + } +} + pub trait Matrix>: Array2 - + Neg + Zero + One + ApproxEq + Sized { @@ -317,9 +346,6 @@ pub trait Matrix>: Array2 #[must_use] fn mul_m(&self, m: &Self) -> Self; - /// Negate this matrix in-place (multiply by scalar -1). - fn neg_self(&mut self); - /// Multiply this matrix by a scalar, in-place. fn mul_self_s(&mut self, s: S); /// Divide this matrix by a scalar, in-place. @@ -423,11 +449,13 @@ impl Sub for Matrix4 { fn sub(self, other: Matrix4) -> Matrix4 { self.sub_m(&other) } } -impl Neg for Matrix2 { - type Output = Matrix2; +impl Neg for Matrix2 { + type Output = Matrix2; #[inline] - fn neg(self) -> Matrix2 { Matrix2::from_cols(self[0].neg(), self[1].neg()) } + fn neg(self) -> Matrix2 { + Matrix2::from_cols(self.x.neg(), self.y.neg()) + } } impl Neg for Matrix3 { @@ -795,12 +823,6 @@ impl Matrix> for Matrix2 { self.row(0).dot(&other[1]), self.row(1).dot(&other[1])) } - #[inline] - fn neg_self(&mut self) { - (&mut self[0]).neg_self(); - (&mut self[1]).neg_self(); - } - #[inline] fn mul_self_s(&mut self, s: S) { (&mut self[0]).mul_self_s(s); @@ -926,13 +948,6 @@ impl Matrix> for Matrix3 { self.row(0).dot(&other[2]),self.row(1).dot(&other[2]),self.row(2).dot(&other[2])) } - #[inline] - fn neg_self(&mut self) { - (&mut self[0]).neg_self(); - (&mut self[1]).neg_self(); - (&mut self[2]).neg_self(); - } - #[inline] fn mul_self_s(&mut self, s: S) { (&mut self[0]).mul_self_s(s); @@ -1094,14 +1109,6 @@ impl Matrix> for Matrix4 { dot_matrix4!(self, other, 0, 3), dot_matrix4!(self, other, 1, 3), dot_matrix4!(self, other, 2, 3), dot_matrix4!(self, other, 3, 3)) } - #[inline] - fn neg_self(&mut self) { - (&mut self[0]).neg_self(); - (&mut self[1]).neg_self(); - (&mut self[2]).neg_self(); - (&mut self[3]).neg_self(); - } - #[inline] fn mul_self_s(&mut self, s: S) { (&mut self[0]).mul_self_s(s); diff --git a/src/num.rs b/src/num.rs index 7de7ebf..cbd5744 100644 --- a/src/num.rs +++ b/src/num.rs @@ -72,7 +72,7 @@ pub trait One { /// Base numeric types with partial ordering pub trait BaseNum: Copy + NumCast + Clone + Add + Sub + - Mul + Div + Rem + Neg + PartialEq + Mul + Div + Rem + PartialEq + PartialOrd + cmp::PartialOrd + fmt::Debug + Zero + One {} diff --git a/src/vector.rs b/src/vector.rs index 7f32b8e..4cc8581 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -111,7 +111,7 @@ use num::{BaseNum, BaseFloat, Zero, One, zero, one}; /// A trait that specifies a range of numeric operations for vectors. Not all /// of these make sense from a linear algebra point of view, but are included /// for pragmatic reasons. -pub trait Vector: Array1 + Zero + One + Neg { +pub trait Vector: Array1 + Zero + One { /// Construct a vector from a single value, replicating it. fn from_value(s: S) -> Self; /// Add a scalar to this vector, returning a new vector. @@ -146,9 +146,6 @@ pub trait Vector: Array1 + Zero + One + Neg { #[must_use] fn rem_v(&self, v: &Self) -> Self; - /// Negate this vector in-place. - fn neg_self(&mut self); - /// Add a scalar to this vector in-place. fn add_self_s(&mut self, s: S); /// Subtract a scalar from this vector, in-place. @@ -203,6 +200,14 @@ macro_rules! vec( } } + impl<$S: Copy + Neg> $Self_<$S> { + /// Negate this vector in-place (multiply by -1). + #[inline] + pub fn neg_self(&mut self) { + $(self.$field = -self.$field);+ + } + } + /// The short constructor. #[inline] pub fn $constructor($($field: S),+) -> $Self_ { @@ -300,8 +305,6 @@ macro_rules! vec( #[inline] fn div_v(&self, v: &$Self_) -> $Self_ { $Self_::new($(self.$field / v.$field),+) } #[inline] fn rem_v(&self, v: &$Self_) -> $Self_ { $Self_::new($(self.$field % v.$field),+) } - #[inline] fn neg_self(&mut self) { $(self.$field = -self.$field;)+ } - #[inline] fn add_self_s(&mut self, s: S) { $(self.$field = self.$field + s;)+ } #[inline] fn sub_self_s(&mut self, s: S) { $(self.$field = self.$field - s;)+ } #[inline] fn mul_self_s(&mut self, s: S) { $(self.$field = self.$field * s;)+ } @@ -334,11 +337,11 @@ macro_rules! vec( fn sub(self, v: $Self_) -> $Self_ { self.sub_v(&v) } } - impl Neg for $Self_ { - type Output = $Self_; + impl Neg for $Self_ { + type Output = $Self_; #[inline] - fn neg(self) -> $Self_ { $Self_::new($(-self.$field),+) } + fn neg(self) -> $Self_ { $Self_::new($(-self.$field),+) } } impl Mul for $Self_ {