Implement by-ref negations for matrices
This commit is contained in:
parent
23c632acca
commit
8c05db962a
6 changed files with 89 additions and 100 deletions
12
src/angle.rs
12
src/angle.rs
|
@ -167,23 +167,23 @@ macro_rules! impl_angle {
|
|||
fn neg(self) -> $Angle<S> { $Angle::new(-self.s) }
|
||||
}
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Add<$Angle<S> > for $Angle<S> {
|
||||
impl_operator!(<S: BaseFloat> Add<$Angle<S> > for $Angle<S> {
|
||||
fn add(lhs, rhs) -> $Angle<S> { $Angle::new(lhs.s + rhs.s) }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseFloat> Sub<$Angle<S> > for $Angle<S> {
|
||||
impl_operator!(<S: BaseFloat> Sub<$Angle<S> > for $Angle<S> {
|
||||
fn sub(lhs, rhs) -> $Angle<S> { $Angle::new(lhs.s - rhs.s) }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseFloat> Div<$Angle<S> > for $Angle<S> {
|
||||
impl_operator!(<S: BaseFloat> Div<$Angle<S> > for $Angle<S> {
|
||||
fn div(lhs, rhs) -> S { lhs.s / rhs.s }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseFloat> Rem<$Angle<S> > for $Angle<S> {
|
||||
impl_operator!(<S: BaseFloat> Rem<$Angle<S> > for $Angle<S> {
|
||||
fn rem(lhs, rhs) -> $Angle<S> { $Angle::new(lhs.s % rhs.s) }
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Mul<S> for $Angle<S> {
|
||||
impl_operator!(<S: BaseFloat> Mul<S> for $Angle<S> {
|
||||
fn mul(lhs, scalar) -> $Angle<S> { $Angle::new(lhs.s * scalar) }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseFloat> Div<S> for $Angle<S> {
|
||||
impl_operator!(<S: BaseFloat> Div<S> for $Angle<S> {
|
||||
fn div(lhs, scalar) -> $Angle<S> { $Angle::new(lhs.s / scalar) }
|
||||
});
|
||||
|
||||
|
|
|
@ -18,59 +18,79 @@
|
|||
#![macro_use]
|
||||
|
||||
/// Generates a binary operator implementation for the permutations of by-ref and by-val
|
||||
macro_rules! impl_binary_operator {
|
||||
// When the right operand is a scalar
|
||||
(<$S:ident: $Constraint:ident> $Binop:ident<$Rhs:ident> for $Lhs:ty {
|
||||
fn $binop:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
|
||||
macro_rules! impl_operator {
|
||||
// When it is an unary operator
|
||||
(<$S:ident: $Constraint:ident> $Op:ident for $Lhs:ty {
|
||||
fn $op:ident($x:ident) -> $Output:ty { $body:expr }
|
||||
}) => {
|
||||
impl<$S: $Constraint> $Binop<$Rhs> for $Lhs {
|
||||
impl<$S: $Constraint> $Op for $Lhs {
|
||||
type Output = $Output;
|
||||
#[inline]
|
||||
fn $binop(self, other: $Rhs) -> $Output {
|
||||
fn $op(self) -> $Output {
|
||||
let $x = self; $body
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, $S: $Constraint> $Op for &'a $Lhs {
|
||||
type Output = $Output;
|
||||
#[inline]
|
||||
fn $op(self) -> $Output {
|
||||
let $x = self; $body
|
||||
}
|
||||
}
|
||||
};
|
||||
// When the right operand is a scalar
|
||||
(<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ident> for $Lhs:ty {
|
||||
fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
|
||||
}) => {
|
||||
impl<$S: $Constraint> $Op<$Rhs> for $Lhs {
|
||||
type Output = $Output;
|
||||
#[inline]
|
||||
fn $op(self, other: $Rhs) -> $Output {
|
||||
let ($lhs, $rhs) = (self, other); $body
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, $S: $Constraint> $Binop<$Rhs> for &'a $Lhs {
|
||||
impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs {
|
||||
type Output = $Output;
|
||||
#[inline]
|
||||
fn $binop(self, other: $Rhs) -> $Output {
|
||||
fn $op(self, other: $Rhs) -> $Output {
|
||||
let ($lhs, $rhs) = (self, other); $body
|
||||
}
|
||||
}
|
||||
};
|
||||
// When the right operand is a compound type
|
||||
(<$S:ident: $Constraint:ident> $Binop:ident<$Rhs:ty> for $Lhs:ty {
|
||||
fn $binop:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
|
||||
(<$S:ident: $Constraint:ident> $Op:ident<$Rhs:ty> for $Lhs:ty {
|
||||
fn $op:ident($lhs:ident, $rhs:ident) -> $Output:ty { $body:expr }
|
||||
}) => {
|
||||
impl<$S: $Constraint> $Binop<$Rhs> for $Lhs {
|
||||
impl<$S: $Constraint> $Op<$Rhs> for $Lhs {
|
||||
type Output = $Output;
|
||||
#[inline]
|
||||
fn $binop(self, other: $Rhs) -> $Output {
|
||||
fn $op(self, other: $Rhs) -> $Output {
|
||||
let ($lhs, $rhs) = (self, other); $body
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, $S: $Constraint> $Binop<&'a $Rhs> for $Lhs {
|
||||
impl<'a, $S: $Constraint> $Op<&'a $Rhs> for $Lhs {
|
||||
type Output = $Output;
|
||||
#[inline]
|
||||
fn $binop(self, other: &'a $Rhs) -> $Output {
|
||||
fn $op(self, other: &'a $Rhs) -> $Output {
|
||||
let ($lhs, $rhs) = (self, other); $body
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, $S: $Constraint> $Binop<$Rhs> for &'a $Lhs {
|
||||
impl<'a, $S: $Constraint> $Op<$Rhs> for &'a $Lhs {
|
||||
type Output = $Output;
|
||||
#[inline]
|
||||
fn $binop(self, other: $Rhs) -> $Output {
|
||||
fn $op(self, other: $Rhs) -> $Output {
|
||||
let ($lhs, $rhs) = (self, other); $body
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, $S: $Constraint> $Binop<&'a $Rhs> for &'b $Lhs {
|
||||
impl<'a, 'b, $S: $Constraint> $Op<&'a $Rhs> for &'b $Lhs {
|
||||
type Output = $Output;
|
||||
#[inline]
|
||||
fn $binop(self, other: &'a $Rhs) -> $Output {
|
||||
fn $op(self, other: &'a $Rhs) -> $Output {
|
||||
let ($lhs, $rhs) = (self, other); $body
|
||||
}
|
||||
}
|
||||
|
|
|
@ -815,70 +815,47 @@ impl<S: BaseFloat> ApproxEq for Matrix4<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat> Neg for Matrix2<S> {
|
||||
type Output = Matrix2<S>;
|
||||
|
||||
#[inline]
|
||||
fn neg(self) -> Matrix2<S> {
|
||||
Matrix2::from_cols(-self.x, -self.y)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat> Neg for Matrix3<S> {
|
||||
type Output = Matrix3<S>;
|
||||
|
||||
#[inline]
|
||||
fn neg(self) -> Matrix3<S> {
|
||||
Matrix3::from_cols(-self.x, -self.y, -self.z)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat> Neg for Matrix4<S> {
|
||||
type Output = Matrix4<S>;
|
||||
|
||||
#[inline]
|
||||
fn neg(self) -> Matrix4<S> {
|
||||
Matrix4::from_cols(-self.x, -self.y, -self.z, -self.w)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_binary_operators {
|
||||
macro_rules! impl_operators {
|
||||
($MatrixN:ident, $VectorN:ident { $($field:ident : $row_index:expr),+ }) => {
|
||||
impl_binary_operator!(<S: BaseFloat> Mul<S> for $MatrixN<S> {
|
||||
impl_operator!(<S: BaseFloat> Neg for $MatrixN<S> {
|
||||
fn neg(matrix) -> $MatrixN<S> { $MatrixN { $($field: -matrix.$field),+ } }
|
||||
});
|
||||
|
||||
impl_operator!(<S: BaseFloat> Mul<S> for $MatrixN<S> {
|
||||
fn mul(matrix, scalar) -> $MatrixN<S> { $MatrixN { $($field: matrix.$field * scalar),+ } }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseFloat> Div<S> for $MatrixN<S> {
|
||||
impl_operator!(<S: BaseFloat> Div<S> for $MatrixN<S> {
|
||||
fn div(matrix, scalar) -> $MatrixN<S> { $MatrixN { $($field: matrix.$field / scalar),+ } }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseFloat> Rem<S> for $MatrixN<S> {
|
||||
impl_operator!(<S: BaseFloat> Rem<S> for $MatrixN<S> {
|
||||
fn rem(matrix, scalar) -> $MatrixN<S> { $MatrixN { $($field: matrix.$field % scalar),+ } }
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Add<$MatrixN<S> > for $MatrixN<S> {
|
||||
fn add(lhs, rhs) -> $MatrixN<S> { $MatrixN { $($field: lhs.$field + rhs.$field ),+ } }
|
||||
impl_operator!(<S: BaseFloat> Add<$MatrixN<S> > for $MatrixN<S> {
|
||||
fn add(lhs, rhs) -> $MatrixN<S> { $MatrixN { $($field: lhs.$field + rhs.$field),+ } }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseFloat> Sub<$MatrixN<S> > for $MatrixN<S> {
|
||||
fn sub(lhs, rhs) -> $MatrixN<S> { $MatrixN { $($field: lhs.$field - rhs.$field ),+ } }
|
||||
impl_operator!(<S: BaseFloat> Sub<$MatrixN<S> > for $MatrixN<S> {
|
||||
fn sub(lhs, rhs) -> $MatrixN<S> { $MatrixN { $($field: lhs.$field - rhs.$field),+ } }
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Mul<$VectorN<S> > for $MatrixN<S> {
|
||||
impl_operator!(<S: BaseFloat> Mul<$VectorN<S> > for $MatrixN<S> {
|
||||
fn mul(matrix, vector) -> $VectorN<S> { $VectorN::new($(matrix.row($row_index).dot(vector.clone())),+) }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl_binary_operators!(Matrix2, Vector2 { x: 0, y: 1 });
|
||||
impl_binary_operators!(Matrix3, Vector3 { x: 0, y: 1, z: 2 });
|
||||
impl_binary_operators!(Matrix4, Vector4 { x: 0, y: 1, z: 2, w: 3 });
|
||||
impl_operators!(Matrix2, Vector2 { x: 0, y: 1 });
|
||||
impl_operators!(Matrix3, Vector3 { x: 0, y: 1, z: 2 });
|
||||
impl_operators!(Matrix4, Vector4 { x: 0, y: 1, z: 2, w: 3 });
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Mul<Matrix2<S> > for Matrix2<S> {
|
||||
impl_operator!(<S: BaseFloat> Mul<Matrix2<S> > for Matrix2<S> {
|
||||
fn mul(lhs, rhs) -> Matrix2<S> {
|
||||
Matrix2::new(lhs.row(0).dot(rhs[0]), lhs.row(1).dot(rhs[0]),
|
||||
lhs.row(0).dot(rhs[1]), lhs.row(1).dot(rhs[1]))
|
||||
}
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Mul<Matrix3<S> > for Matrix3<S> {
|
||||
impl_operator!(<S: BaseFloat> Mul<Matrix3<S> > for Matrix3<S> {
|
||||
fn mul(lhs, rhs) -> Matrix3<S> {
|
||||
Matrix3::new(lhs.row(0).dot(rhs[0]), lhs.row(1).dot(rhs[0]), lhs.row(2).dot(rhs[0]),
|
||||
lhs.row(0).dot(rhs[1]), lhs.row(1).dot(rhs[1]), lhs.row(2).dot(rhs[1]),
|
||||
|
@ -899,7 +876,7 @@ macro_rules! dot_matrix4 {
|
|||
};
|
||||
}
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Mul<Matrix4<S> > for Matrix4<S> {
|
||||
impl_operator!(<S: BaseFloat> Mul<Matrix4<S> > for Matrix4<S> {
|
||||
fn mul(lhs, rhs) -> Matrix4<S> {
|
||||
Matrix4::new(dot_matrix4!(lhs, rhs, 0, 0), dot_matrix4!(lhs, rhs, 1, 0), dot_matrix4!(lhs, rhs, 2, 0), dot_matrix4!(lhs, rhs, 3, 0),
|
||||
dot_matrix4!(lhs, rhs, 0, 1), dot_matrix4!(lhs, rhs, 1, 1), dot_matrix4!(lhs, rhs, 2, 1), dot_matrix4!(lhs, rhs, 3, 1),
|
||||
|
|
10
src/point.rs
10
src/point.rs
|
@ -142,23 +142,23 @@ macro_rules! impl_point {
|
|||
}
|
||||
}
|
||||
|
||||
impl_binary_operator!(<S: BaseNum> Add<$VectorN<S> > for $PointN<S> {
|
||||
impl_operator!(<S: BaseNum> Add<$VectorN<S> > for $PointN<S> {
|
||||
fn add(lhs, rhs) -> $PointN<S> { $PointN::new($(lhs.$field + rhs.$field),+) }
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseNum> Sub<$PointN<S> > for $PointN<S> {
|
||||
impl_operator!(<S: BaseNum> Sub<$PointN<S> > for $PointN<S> {
|
||||
fn sub(lhs, rhs) -> $VectorN<S> { $VectorN::new($(lhs.$field - rhs.$field),+) }
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseNum> Mul<S> for $PointN<S> {
|
||||
impl_operator!(<S: BaseNum> Mul<S> for $PointN<S> {
|
||||
fn mul(point, scalar) -> $PointN<S> { $PointN::new($(point.$field * scalar),+) }
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseNum> Div<S> for $PointN<S> {
|
||||
impl_operator!(<S: BaseNum> Div<S> for $PointN<S> {
|
||||
fn div(point, scalar) -> $PointN<S> { $PointN::new($(point.$field / scalar),+) }
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseNum> Rem<S> for $PointN<S> {
|
||||
impl_operator!(<S: BaseNum> Rem<S> for $PointN<S> {
|
||||
fn rem(point, scalar) -> $PointN<S> { $PointN::new($(point.$field % scalar),+) }
|
||||
});
|
||||
|
||||
|
|
|
@ -108,33 +108,25 @@ impl<S: BaseFloat> Quaternion<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat> Neg for Quaternion<S> {
|
||||
type Output = Quaternion<S>;
|
||||
impl_operator!(<S: BaseFloat> Neg for Quaternion<S> {
|
||||
fn neg(quat) -> Quaternion<S> {
|
||||
Quaternion::from_sv(-quat.s, -quat.v)
|
||||
}
|
||||
});
|
||||
|
||||
#[inline]
|
||||
fn neg(self) -> Quaternion<S> { Quaternion::from_sv(-self.s, -self.v) }
|
||||
}
|
||||
|
||||
impl<'a, S: BaseFloat> Neg for &'a Quaternion<S> {
|
||||
type Output = Quaternion<S>;
|
||||
|
||||
#[inline]
|
||||
fn neg(self) -> Quaternion<S> { Quaternion::from_sv(-self.s, -self.v) }
|
||||
}
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Mul<S> for Quaternion<S> {
|
||||
impl_operator!(<S: BaseFloat> Mul<S> for Quaternion<S> {
|
||||
fn mul(lhs, rhs) -> Quaternion<S> {
|
||||
Quaternion::from_sv(lhs.s * rhs, lhs.v * rhs)
|
||||
}
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Div<S> for Quaternion<S> {
|
||||
impl_operator!(<S: BaseFloat> Div<S> for Quaternion<S> {
|
||||
fn div(lhs, rhs) -> Quaternion<S> {
|
||||
Quaternion::from_sv(lhs.s / rhs, lhs.v / rhs)
|
||||
}
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Mul<Vector3<S> > for Quaternion<S> {
|
||||
impl_operator!(<S: BaseFloat> Mul<Vector3<S> > for Quaternion<S> {
|
||||
fn mul(lhs, rhs) -> Vector3<S> {{
|
||||
let rhs = rhs.clone();
|
||||
let two: S = cast(2i8).unwrap();
|
||||
|
@ -143,19 +135,19 @@ impl_binary_operator!(<S: BaseFloat> Mul<Vector3<S> > for Quaternion<S> {
|
|||
}}
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Add<Quaternion<S> > for Quaternion<S> {
|
||||
impl_operator!(<S: BaseFloat> Add<Quaternion<S> > for Quaternion<S> {
|
||||
fn add(lhs, rhs) -> Quaternion<S> {
|
||||
Quaternion::from_sv(lhs.s + rhs.s, lhs.v + rhs.v)
|
||||
}
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Sub<Quaternion<S> > for Quaternion<S> {
|
||||
impl_operator!(<S: BaseFloat> Sub<Quaternion<S> > for Quaternion<S> {
|
||||
fn sub(lhs, rhs) -> Quaternion<S> {
|
||||
Quaternion::from_sv(lhs.s - rhs.s, lhs.v - rhs.v)
|
||||
}
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseFloat> Mul<Quaternion<S> > for Quaternion<S> {
|
||||
impl_operator!(<S: BaseFloat> Mul<Quaternion<S> > for Quaternion<S> {
|
||||
fn mul(lhs, rhs) -> Quaternion<S> {
|
||||
Quaternion::new(lhs.s * rhs.s - lhs.v.x * rhs.v.x - lhs.v.y * rhs.v.y - lhs.v.z * rhs.v.z,
|
||||
lhs.s * rhs.v.x + lhs.v.x * rhs.s + lhs.v.y * rhs.v.z - lhs.v.z * rhs.v.y,
|
||||
|
|
|
@ -212,38 +212,38 @@ macro_rules! impl_vector {
|
|||
}
|
||||
}
|
||||
|
||||
impl_binary_operator!(<S: BaseNum> Add<S> for $VectorN<S> {
|
||||
impl_operator!(<S: BaseNum> Add<S> for $VectorN<S> {
|
||||
fn add(vector, scalar) -> $VectorN<S> { $VectorN::new($(vector.$field + scalar),+) }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseNum> Add<$VectorN<S> > for $VectorN<S> {
|
||||
impl_operator!(<S: BaseNum> Add<$VectorN<S> > for $VectorN<S> {
|
||||
fn add(lhs, rhs) -> $VectorN<S> { $VectorN::new($(lhs.$field + rhs.$field),+) }
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseNum> Sub<S> for $VectorN<S> {
|
||||
impl_operator!(<S: BaseNum> Sub<S> for $VectorN<S> {
|
||||
fn sub(vector, scalar) -> $VectorN<S> { $VectorN::new($(vector.$field - scalar),+) }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseNum> Sub<$VectorN<S> > for $VectorN<S> {
|
||||
impl_operator!(<S: BaseNum> Sub<$VectorN<S> > for $VectorN<S> {
|
||||
fn sub(lhs, rhs) -> $VectorN<S> { $VectorN::new($(lhs.$field - rhs.$field),+) }
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseNum> Mul<S> for $VectorN<S> {
|
||||
impl_operator!(<S: BaseNum> Mul<S> for $VectorN<S> {
|
||||
fn mul(vector, scalar) -> $VectorN<S> { $VectorN::new($(vector.$field * scalar),+) }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseNum> Mul<$VectorN<S> > for $VectorN<S> {
|
||||
impl_operator!(<S: BaseNum> Mul<$VectorN<S> > for $VectorN<S> {
|
||||
fn mul(lhs, rhs) -> $VectorN<S> { $VectorN::new($(lhs.$field * rhs.$field),+) }
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseNum> Div<S> for $VectorN<S> {
|
||||
impl_operator!(<S: BaseNum> Div<S> for $VectorN<S> {
|
||||
fn div(vector, scalar) -> $VectorN<S> { $VectorN::new($(vector.$field / scalar),+) }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseNum> Div<$VectorN<S> > for $VectorN<S> {
|
||||
impl_operator!(<S: BaseNum> Div<$VectorN<S> > for $VectorN<S> {
|
||||
fn div(lhs, rhs) -> $VectorN<S> { $VectorN::new($(lhs.$field / rhs.$field),+) }
|
||||
});
|
||||
|
||||
impl_binary_operator!(<S: BaseNum> Rem<S> for $VectorN<S> {
|
||||
impl_operator!(<S: BaseNum> Rem<S> for $VectorN<S> {
|
||||
fn rem(vector, scalar) -> $VectorN<S> { $VectorN::new($(vector.$field % scalar),+) }
|
||||
});
|
||||
impl_binary_operator!(<S: BaseNum> Rem<$VectorN<S> > for $VectorN<S> {
|
||||
impl_operator!(<S: BaseNum> Rem<$VectorN<S> > for $VectorN<S> {
|
||||
fn rem(lhs, rhs) -> $VectorN<S> { $VectorN::new($(lhs.$field % rhs.$field),+) }
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in a new issue