parent
d784e4e531
commit
7af4ebb28d
5 changed files with 167 additions and 37 deletions
|
@ -909,7 +909,7 @@ macro_rules! impl_binary_operator {
|
|||
|
||||
#[inline]
|
||||
fn $binop(self, other: &'a $MatrixN<S>) -> $MatrixN<S> {
|
||||
$MatrixN { $($field: self.$field.$binop(&other.$field)),+ }
|
||||
$MatrixN { $($field: self.$field.$binop(other.$field)),+ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -922,35 +922,29 @@ impl_binary_operator!(Sub::sub, Matrix2 { x, y });
|
|||
impl_binary_operator!(Sub::sub, Matrix3 { x, y, z });
|
||||
impl_binary_operator!(Sub::sub, Matrix4 { x, y, z, w });
|
||||
|
||||
impl<'a, 'b, S: BaseNum> Mul<&'a Vector2<S>> for &'b Matrix2<S> {
|
||||
type Output = Vector2<S>;
|
||||
macro_rules! impl_vector_mul_operators {
|
||||
($MatrixN:ident, $VectorN:ident { $($row_index:expr),+ }) => {
|
||||
impl<'a, S: BaseNum> Mul<$VectorN<S>> for &'a $MatrixN<S> {
|
||||
type Output = $VectorN<S>;
|
||||
|
||||
fn mul(self, v: &'a Vector2<S>) -> Vector2<S> {
|
||||
Vector2::new(self.row(0).dot(v),
|
||||
self.row(1).dot(v))
|
||||
fn mul(self, v: $VectorN<S>) -> $VectorN<S> {
|
||||
$VectorN::new($(self.row($row_index).dot(&v)),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, S: BaseNum> Mul<&'a $VectorN<S>> for &'b $MatrixN<S> {
|
||||
type Output = $VectorN<S>;
|
||||
|
||||
fn mul(self, v: &'a $VectorN<S>) -> $VectorN<S> {
|
||||
$VectorN::new($(self.row($row_index).dot(v)),+)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, S: BaseNum> Mul<&'a Vector3<S>> for &'b Matrix3<S> {
|
||||
type Output = Vector3<S>;
|
||||
|
||||
fn mul(self, v: &'a Vector3<S>) -> Vector3<S> {
|
||||
Vector3::new(self.row(0).dot(v),
|
||||
self.row(1).dot(v),
|
||||
self.row(2).dot(v))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, S: BaseNum> Mul<&'a Vector4<S>> for &'b Matrix4<S> {
|
||||
type Output = Vector4<S>;
|
||||
|
||||
fn mul(self, v: &'a Vector4<S>) -> Vector4<S> {
|
||||
Vector4::new(self.row(0).dot(v),
|
||||
self.row(1).dot(v),
|
||||
self.row(2).dot(v),
|
||||
self.row(3).dot(v))
|
||||
}
|
||||
}
|
||||
impl_vector_mul_operators!(Matrix2, Vector2 { 0, 1 });
|
||||
impl_vector_mul_operators!(Matrix3, Vector3 { 0, 1, 2 });
|
||||
impl_vector_mul_operators!(Matrix4, Vector4 { 0, 1, 2, 3 });
|
||||
|
||||
impl<'a, 'b, S: BaseNum> Mul<&'a Matrix2<S>> for &'b Matrix2<S> {
|
||||
type Output = Matrix2<S>;
|
||||
|
|
83
src/point.rs
83
src/point.rs
|
@ -55,7 +55,7 @@ impl<S: BaseNum> Point3<S> {
|
|||
impl<S: BaseNum> Point3<S> {
|
||||
#[inline]
|
||||
pub fn from_homogeneous(v: &Vector4<S>) -> Point3<S> {
|
||||
let e = v.truncate().mul_s(S::one() / v.w);
|
||||
let e = v.truncate() * (S::one() / v.w);
|
||||
Point3::new(e.x, e.y, e.z) //FIXME
|
||||
}
|
||||
|
||||
|
@ -299,6 +299,33 @@ impl<S: BaseFloat> ApproxEq for Point3<S> {
|
|||
|
||||
macro_rules! impl_operators {
|
||||
($PointN:ident { $($field:ident),+ }, $VectorN:ident) => {
|
||||
impl<S: BaseNum> Mul<S> for $PointN<S> {
|
||||
type Output = $PointN<S>;
|
||||
|
||||
#[inline]
|
||||
fn mul(self, scalar: S) -> $PointN<S> {
|
||||
$PointN::new($(self.$field * scalar),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseNum> Div<S> for $PointN<S> {
|
||||
type Output = $PointN<S>;
|
||||
|
||||
#[inline]
|
||||
fn div(self, scalar: S) -> $PointN<S> {
|
||||
$PointN::new($(self.$field / scalar),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseNum> Rem<S> for $PointN<S> {
|
||||
type Output = $PointN<S>;
|
||||
|
||||
#[inline]
|
||||
fn rem(self, scalar: S) -> $PointN<S> {
|
||||
$PointN::new($(self.$field % scalar),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: BaseNum> Mul<S> for &'a $PointN<S> {
|
||||
type Output = $PointN<S>;
|
||||
|
||||
|
@ -326,6 +353,60 @@ macro_rules! impl_operators {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: BaseNum> Add<$VectorN<S>> for $PointN<S> {
|
||||
type Output = $PointN<S>;
|
||||
|
||||
#[inline]
|
||||
fn add(self, v: $VectorN<S>) -> $PointN<S> {
|
||||
$PointN::new($(self.$field + v.$field),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseNum> Sub<$PointN<S>> for $PointN<S> {
|
||||
type Output = $VectorN<S>;
|
||||
|
||||
#[inline]
|
||||
fn sub(self, p: $PointN<S>) -> $VectorN<S> {
|
||||
$VectorN::new($(self.$field - p.$field),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: BaseNum> Add<&'a $VectorN<S>> for $PointN<S> {
|
||||
type Output = $PointN<S>;
|
||||
|
||||
#[inline]
|
||||
fn add(self, v: &'a $VectorN<S>) -> $PointN<S> {
|
||||
$PointN::new($(self.$field + v.$field),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: BaseNum> Sub<&'a $PointN<S>> for $PointN<S> {
|
||||
type Output = $VectorN<S>;
|
||||
|
||||
#[inline]
|
||||
fn sub(self, p: &'a $PointN<S>) -> $VectorN<S> {
|
||||
$VectorN::new($(self.$field - p.$field),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: BaseNum> Add<$VectorN<S>> for &'a $PointN<S> {
|
||||
type Output = $PointN<S>;
|
||||
|
||||
#[inline]
|
||||
fn add(self, v: $VectorN<S>) -> $PointN<S> {
|
||||
$PointN::new($(self.$field + v.$field),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: BaseNum> Sub<$PointN<S>> for &'a $PointN<S> {
|
||||
type Output = $VectorN<S>;
|
||||
|
||||
#[inline]
|
||||
fn sub(self, p: $PointN<S>) -> $VectorN<S> {
|
||||
$VectorN::new($(self.$field - p.$field),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, S: BaseNum> Add<&'a $VectorN<S>> for &'b $PointN<S> {
|
||||
type Output = $PointN<S>;
|
||||
|
||||
|
|
|
@ -114,12 +114,30 @@ impl<S: BaseFloat> Quaternion<S> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat> Mul<S> for Quaternion<S> {
|
||||
type Output = Quaternion<S>;
|
||||
|
||||
#[inline]
|
||||
fn mul(self, value: S) -> Quaternion<S> {
|
||||
Quaternion::from_sv(self.s * value, self.v * value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: BaseFloat> Mul<S> for &'a Quaternion<S> {
|
||||
type Output = Quaternion<S>;
|
||||
|
||||
#[inline]
|
||||
fn mul(self, value: S) -> Quaternion<S> {
|
||||
Quaternion::from_sv(self.s * value, &self.v * value)
|
||||
Quaternion::from_sv(self.s * value, self.v * value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseFloat> Div<S> for Quaternion<S> {
|
||||
type Output = Quaternion<S>;
|
||||
|
||||
#[inline]
|
||||
fn div(self, value: S) -> Quaternion<S> {
|
||||
Quaternion::from_sv(self.s / value, self.v / value)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -128,7 +146,7 @@ impl<'a, S: BaseFloat> Div<S> for &'a Quaternion<S> {
|
|||
|
||||
#[inline]
|
||||
fn div(self, value: S) -> Quaternion<S> {
|
||||
Quaternion::from_sv(self.s / value, &self.v / value)
|
||||
Quaternion::from_sv(self.s / value, self.v / value)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -137,8 +155,9 @@ impl<'a, 'b, S: BaseFloat> Mul<&'b Vector3<S>> for &'a Quaternion<S> {
|
|||
|
||||
#[inline]
|
||||
fn mul(self, vec: &'b Vector3<S>) -> Vector3<S> {
|
||||
let tmp = self.v.cross(vec).add_v(&vec.mul_s(self.s.clone()));
|
||||
self.v.cross(&tmp).mul_s(cast(2i8).unwrap()).add_v(vec)
|
||||
let two: S = cast(2i8).unwrap();
|
||||
let tmp = self.v.cross(vec) + (vec * self.s);
|
||||
(self.v.cross(&tmp) * two) + vec
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,7 +166,7 @@ impl<'a, 'b, S: BaseFloat> Add<&'b Quaternion<S>> for &'a Quaternion<S> {
|
|||
|
||||
#[inline]
|
||||
fn add(self, other: &'b Quaternion<S>) -> Quaternion<S> {
|
||||
Quaternion::from_sv(self.s + other.s, &self.v + &other.v)
|
||||
Quaternion::from_sv(self.s + other.s, self.v + &other.v)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -156,7 +175,7 @@ impl<'a, 'b, S: BaseFloat> Sub<&'b Quaternion<S>> for &'a Quaternion<S> {
|
|||
|
||||
#[inline]
|
||||
fn sub(self, other: &'b Quaternion<S>) -> Quaternion<S> {
|
||||
Quaternion::from_sv(self.s - other.s, &self.v - &other.v)
|
||||
Quaternion::from_sv(self.s - other.s, self.v - &other.v)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -142,7 +142,7 @@ pub trait Transform3<S: BaseNum>: Transform<Point3<S>> + Into<Matrix4<S>> {}
|
|||
impl<S: BaseFloat, R: Rotation2<S>> From<Decomposed<Vector2<S>, R>> for Matrix3<S> {
|
||||
fn from(dec: Decomposed<Vector2<S>, R>) -> Matrix3<S> {
|
||||
let m: Matrix2<_> = dec.rot.into();
|
||||
let mut m: Matrix3<_> = m.mul_s(dec.scale).into();
|
||||
let mut m: Matrix3<_> = (&m * dec.scale).into();
|
||||
m.z = dec.disp.extend(S::one());
|
||||
m
|
||||
}
|
||||
|
@ -151,7 +151,7 @@ impl<S: BaseFloat, R: Rotation2<S>> From<Decomposed<Vector2<S>, R>> for Matrix3<
|
|||
impl<S: BaseFloat, R: Rotation3<S>> From<Decomposed<Vector3<S>, R>> for Matrix4<S> {
|
||||
fn from(dec: Decomposed<Vector3<S>, R>) -> Matrix4<S> {
|
||||
let m: Matrix3<_> = dec.rot.into();
|
||||
let mut m: Matrix4<_> = m.mul_s(dec.scale).into();
|
||||
let mut m: Matrix4<_> = (&m * dec.scale).into();
|
||||
m.w = dec.disp.extend(S::one());
|
||||
m
|
||||
}
|
||||
|
|
|
@ -316,12 +316,48 @@ macro_rules! vec {
|
|||
|
||||
macro_rules! impl_binary_operator {
|
||||
($Binop:ident :: $binop:ident, $VectorN:ident { $($field:ident),+ }) => {
|
||||
impl<S: BaseNum> $Binop<S> for $VectorN<S> {
|
||||
type Output = $VectorN<S>;
|
||||
|
||||
#[inline]
|
||||
fn $binop(self, scalar: S) -> $VectorN<S> {
|
||||
$VectorN::new($(self.$field.$binop(scalar)),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: BaseNum> $Binop<S> for &'a $VectorN<S> {
|
||||
type Output = $VectorN<S>;
|
||||
|
||||
#[inline]
|
||||
fn $binop(self, s: S) -> $VectorN<S> {
|
||||
$VectorN::new($(self.$field.$binop(s)),+)
|
||||
fn $binop(self, scalar: S) -> $VectorN<S> {
|
||||
$VectorN::new($(self.$field.$binop(scalar)),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: BaseNum> $Binop<$VectorN<S>> for $VectorN<S> {
|
||||
type Output = $VectorN<S>;
|
||||
|
||||
#[inline]
|
||||
fn $binop(self, other: $VectorN<S>) -> $VectorN<S> {
|
||||
$VectorN::new($(self.$field.$binop(other.$field)),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: BaseNum> $Binop<&'a $VectorN<S>> for $VectorN<S> {
|
||||
type Output = $VectorN<S>;
|
||||
|
||||
#[inline]
|
||||
fn $binop(self, other: &'a $VectorN<S>) -> $VectorN<S> {
|
||||
$VectorN::new($(self.$field.$binop(other.$field)),+)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, S: BaseNum> $Binop<$VectorN<S>> for &'a $VectorN<S> {
|
||||
type Output = $VectorN<S>;
|
||||
|
||||
#[inline]
|
||||
fn $binop(self, other: $VectorN<S>) -> $VectorN<S> {
|
||||
$VectorN::new($(self.$field.$binop(other.$field)),+)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue