Fixed the Neg disaster
This commit is contained in:
parent
da2df8e10b
commit
94d428d23e
4 changed files with 54 additions and 44 deletions
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
|
||||
name = "cgmath"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
authors = ["Brendan Zabarauskas <bjzaba@yahoo.com.au>",
|
||||
"Brian Heylin",
|
||||
"Colin Sherratt",
|
||||
|
|
|
@ -44,7 +44,7 @@ pub struct Matrix3<S> { pub x: Vector3<S>, pub y: Vector3<S>, pub z: Vector3<S>
|
|||
pub struct Matrix4<S> { pub x: Vector4<S>, pub y: Vector4<S>, pub z: Vector4<S>, pub w: Vector4<S> }
|
||||
|
||||
|
||||
impl<S: BaseNum> Matrix2<S> {
|
||||
impl<S> Matrix2<S> {
|
||||
/// Create a new matrix, providing values for each index.
|
||||
#[inline]
|
||||
pub fn new(c0r0: S, c0r1: S,
|
||||
|
@ -58,7 +58,9 @@ impl<S: BaseNum> Matrix2<S> {
|
|||
pub fn from_cols(c0: Vector2<S>, c1: Vector2<S>) -> Matrix2<S> {
|
||||
Matrix2 { x: c0, y: c1 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseNum> Matrix2<S> {
|
||||
/// Create a new diagonal matrix, providing a single value to use for each
|
||||
/// non-zero index.
|
||||
#[inline]
|
||||
|
@ -98,6 +100,15 @@ impl<S: BaseFloat + 'static> Matrix2<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: Copy + Neg<Output = S>> Matrix2<S> {
|
||||
/// Negate this `Matrix2` in-place.
|
||||
#[inline]
|
||||
pub fn neg_self(&mut self) {
|
||||
(&mut self[0]).neg_self();
|
||||
(&mut self[1]).neg_self();
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseNum> Matrix3<S> {
|
||||
/// Create a new matrix, providing values for each index.
|
||||
#[inline]
|
||||
|
@ -137,8 +148,7 @@ impl<S: BaseNum> Matrix3<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat + 'static>
|
||||
Matrix3<S> {
|
||||
impl<S: BaseFloat + 'static> Matrix3<S> {
|
||||
/// Create a transformation matrix that will cause a vector to point at
|
||||
/// `dir`, using `up` for orientation.
|
||||
pub fn look_at(dir: &Vector3<S>, up: &Vector3<S>) -> Matrix3<S> {
|
||||
|
@ -220,6 +230,16 @@ Matrix3<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: Copy + Neg<Output = S>> Matrix3<S> {
|
||||
/// 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<S: BaseNum> Matrix4<S> {
|
||||
/// Create a new matrix, providing values for each index.
|
||||
#[inline]
|
||||
|
@ -271,8 +291,7 @@ impl<S: BaseNum> Matrix4<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat>
|
||||
Matrix4<S> {
|
||||
impl<S: BaseFloat> Matrix4<S> {
|
||||
/// Create a transformation matrix that will cause a vector to point at
|
||||
/// `dir`, using `up` for orientation.
|
||||
pub fn look_at(eye: &Point3<S>, center: &Point3<S>, up: &Vector3<S>) -> Matrix4<S> {
|
||||
|
@ -287,8 +306,18 @@ Matrix4<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: Copy + Neg<Output = S>> Matrix4<S> {
|
||||
/// 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<S: BaseFloat, V: Clone + Vector<S>>: Array2<V, V, S>
|
||||
+ Neg
|
||||
+ Zero + One
|
||||
+ ApproxEq<S>
|
||||
+ Sized {
|
||||
|
@ -317,9 +346,6 @@ pub trait Matrix<S: BaseFloat, V: Clone + Vector<S>>: Array2<V, V, S>
|
|||
#[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<S: BaseFloat + 'static> Sub for Matrix4<S> {
|
|||
fn sub(self, other: Matrix4<S>) -> Matrix4<S> { self.sub_m(&other) }
|
||||
}
|
||||
|
||||
impl<S: BaseFloat> Neg for Matrix2<S> {
|
||||
type Output = Matrix2<S>;
|
||||
impl<S: Neg> Neg for Matrix2<S> {
|
||||
type Output = Matrix2<S::Output>;
|
||||
|
||||
#[inline]
|
||||
fn neg(self) -> Matrix2<S> { Matrix2::from_cols(self[0].neg(), self[1].neg()) }
|
||||
fn neg(self) -> Matrix2<S::Output> {
|
||||
Matrix2::from_cols(self.x.neg(), self.y.neg())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat> Neg for Matrix3<S> {
|
||||
|
@ -795,12 +823,6 @@ impl<S: BaseFloat + 'static> Matrix<S, Vector2<S>> for Matrix2<S> {
|
|||
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<S: BaseFloat + 'static> Matrix<S, Vector3<S>> for Matrix3<S> {
|
|||
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<S: BaseFloat + 'static> Matrix<S, Vector4<S>> for Matrix4<S> {
|
|||
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);
|
||||
|
|
|
@ -72,7 +72,7 @@ pub trait One {
|
|||
/// Base numeric types with partial ordering
|
||||
pub trait BaseNum:
|
||||
Copy + NumCast + Clone + Add<Output=Self> + Sub<Output=Self> +
|
||||
Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> + Neg<Output=Self> + PartialEq
|
||||
Mul<Output=Self> + Div<Output=Self> + Rem<Output=Self> + PartialEq
|
||||
+ PartialOrd + cmp::PartialOrd + fmt::Debug + Zero + One
|
||||
{}
|
||||
|
||||
|
|
|
@ -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<S: BaseNum>: Array1<S> + Zero + One + Neg<Output=Self> {
|
||||
pub trait Vector<S: BaseNum>: Array1<S> + 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<S: BaseNum>: Array1<S> + Zero + One + Neg<Output=Self> {
|
|||
#[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<Output = $S>> $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<S>($($field: S),+) -> $Self_<S> {
|
||||
|
@ -300,8 +305,6 @@ macro_rules! vec(
|
|||
#[inline] fn div_v(&self, v: &$Self_<S>) -> $Self_<S> { $Self_::new($(self.$field / v.$field),+) }
|
||||
#[inline] fn rem_v(&self, v: &$Self_<S>) -> $Self_<S> { $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_<S>) -> $Self_<S> { self.sub_v(&v) }
|
||||
}
|
||||
|
||||
impl<S: BaseNum> Neg for $Self_<S> {
|
||||
type Output = $Self_<S>;
|
||||
impl<S: Neg> Neg for $Self_<S> {
|
||||
type Output = $Self_<S::Output>;
|
||||
|
||||
#[inline]
|
||||
fn neg(self) -> $Self_<S> { $Self_::new($(-self.$field),+) }
|
||||
fn neg(self) -> $Self_<S::Output> { $Self_::new($(-self.$field),+) }
|
||||
}
|
||||
|
||||
impl<S: BaseNum> Mul for $Self_<S> {
|
||||
|
|
Loading…
Reference in a new issue