diff --git a/graphene/src/matrix.rs b/graphene/src/matrix.rs index 23677da58b67..872a32dbd76f 100644 --- a/graphene/src/matrix.rs +++ b/graphene/src/matrix.rs @@ -1,10 +1,10 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use std::fmt; +use std::{fmt, ops}; use glib::translate::*; -use crate::{ffi, Matrix, Point3D, Vec3, Vec4}; +use crate::{ffi, Matrix, Point, Point3D, Vec3, Vec4}; impl Matrix { #[doc(alias = "graphene_matrix_init_from_2d")] @@ -219,6 +219,62 @@ impl Default for Matrix { } } +// Scalar multiplication +impl ops::Mul for f32 { + type Output = Matrix; + + fn mul(self, mut rhs: Matrix) -> Self::Output { + rhs.scale(self, self, self); + rhs + } +} + +// Matrix-matrix/-vector multiplication +impl ops::Mul for Matrix { + type Output = Matrix; + + fn mul(self, rhs: Matrix) -> Self::Output { + (&self).multiply(&rhs) + } +} +impl ops::MulAssign for Matrix { + fn mul_assign(&mut self, rhs: Matrix) { + *self = *self * rhs; + } +} + +impl ops::Mul for Matrix { + type Output = Vec4; + + fn mul(self, rhs: Vec4) -> Self::Output { + (&self).transform_vec4(&rhs) + } +} + +impl ops::Mul for Matrix { + type Output = Vec3; + + fn mul(self, rhs: Vec3) -> Self::Output { + (&self).transform_vec3(&rhs) + } +} + +impl ops::Mul for Matrix { + type Output = Point; + + fn mul(self, rhs: Point) -> Self::Output { + (&self).transform_point(&rhs) + } +} + +impl ops::Mul for Matrix { + type Output = Point3D; + + fn mul(self, rhs: Point3D) -> Self::Output { + (&self).transform_point3d(&rhs) + } +} + #[cfg(test)] mod tests { use super::Matrix; diff --git a/graphene/src/quaternion.rs b/graphene/src/quaternion.rs index 837639c9b720..cf163853a5e2 100644 --- a/graphene/src/quaternion.rs +++ b/graphene/src/quaternion.rs @@ -1,6 +1,6 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use std::fmt; +use std::{fmt, ops}; use glib::translate::*; @@ -145,3 +145,31 @@ impl fmt::Debug for Quaternion { .finish() } } + +impl ops::Add for Quaternion { + type Output = Quaternion; + + fn add(self, rhs: Quaternion) -> Self::Output { + (&self).add(&rhs) + } +} + +impl ops::AddAssign for Quaternion { + fn add_assign(&mut self, rhs: Quaternion) { + *self = *self + rhs; + } +} + +impl ops::Mul for Quaternion { + type Output = Quaternion; + + fn mul(self, rhs: Quaternion) -> Self::Output { + (&self).multiply(&rhs) + } +} + +impl ops::MulAssign for Quaternion { + fn mul_assign(&mut self, rhs: Quaternion) { + *self = *self * rhs; + } +} diff --git a/graphene/src/vec2.rs b/graphene/src/vec2.rs index dbfd937c8dd5..3ee1a8a85988 100644 --- a/graphene/src/vec2.rs +++ b/graphene/src/vec2.rs @@ -1,6 +1,6 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use std::fmt; +use std::{fmt, ops}; use glib::translate::*; @@ -52,3 +52,83 @@ impl Default for Vec2 { Self::zero() } } + +// addition/subtraction +impl ops::Add for Vec2 { + type Output = Vec2; + + fn add(self, rhs: Vec2) -> Self::Output { + (&self).add(&rhs) + } +} +impl ops::AddAssign for Vec2 { + fn add_assign(&mut self, rhs: Vec2) { + *self = *self + rhs; + } +} +impl ops::Sub for Vec2 { + type Output = Vec2; + + fn sub(self, rhs: Vec2) -> Self::Output { + (&self).subtract(&rhs) + } +} +impl ops::SubAssign for Vec2 { + fn sub_assign(&mut self, rhs: Vec2) { + *self = *self - rhs; + } +} +impl ops::Neg for Vec2 { + type Output = Vec2; + + fn neg(self) -> Self::Output { + (&self).negate() + } +} + +// scalar multiplication +impl ops::Mul for Vec2 { + type Output = Vec2; + + fn mul(self, rhs: f32) -> Self::Output { + (&self).scale(rhs) + } +} +impl ops::MulAssign for Vec2 { + fn mul_assign(&mut self, rhs: f32) { + *self = *self * rhs; + } +} +impl ops::Mul for f32 { + type Output = Vec2; + + fn mul(self, rhs: Vec2) -> Self::Output { + rhs * self + } +} + +// Component-wise multiplication/division +impl ops::Mul for Vec2 { + type Output = Vec2; + + fn mul(self, rhs: Vec2) -> Self::Output { + (&self).multiply(&rhs) + } +} +impl ops::MulAssign for Vec2 { + fn mul_assign(&mut self, rhs: Vec2) { + *self = *self * rhs; + } +} +impl ops::Div for Vec2 { + type Output = Vec2; + + fn div(self, rhs: Vec2) -> Self::Output { + (&self).divide(&rhs) + } +} +impl ops::DivAssign for Vec2 { + fn div_assign(&mut self, rhs: Vec2) { + *self = *self / rhs; + } +} diff --git a/graphene/src/vec3.rs b/graphene/src/vec3.rs index fc73ab5b0f8f..a55988d8d975 100644 --- a/graphene/src/vec3.rs +++ b/graphene/src/vec3.rs @@ -1,6 +1,6 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use std::fmt; +use std::{fmt, ops}; use glib::translate::*; @@ -53,3 +53,83 @@ impl Default for Vec3 { Self::zero() } } + +// addition/subtraction +impl ops::Add for Vec3 { + type Output = Vec3; + + fn add(self, rhs: Vec3) -> Self::Output { + (&self).add(&rhs) + } +} +impl ops::AddAssign for Vec3 { + fn add_assign(&mut self, rhs: Vec3) { + *self = *self + rhs; + } +} +impl ops::Sub for Vec3 { + type Output = Vec3; + + fn sub(self, rhs: Vec3) -> Self::Output { + (&self).subtract(&rhs) + } +} +impl ops::SubAssign for Vec3 { + fn sub_assign(&mut self, rhs: Vec3) { + *self = *self - rhs; + } +} +impl ops::Neg for Vec3 { + type Output = Vec3; + + fn neg(self) -> Self::Output { + (&self).negate() + } +} + +// scalar multiplication +impl ops::Mul for Vec3 { + type Output = Vec3; + + fn mul(self, rhs: f32) -> Self::Output { + (&self).scale(rhs) + } +} +impl ops::MulAssign for Vec3 { + fn mul_assign(&mut self, rhs: f32) { + *self = *self * rhs; + } +} +impl ops::Mul for f32 { + type Output = Vec3; + + fn mul(self, rhs: Vec3) -> Self::Output { + rhs * self + } +} + +// Component-wise multiplication/division +impl ops::Mul for Vec3 { + type Output = Vec3; + + fn mul(self, rhs: Vec3) -> Self::Output { + (&self).multiply(&rhs) + } +} +impl ops::MulAssign for Vec3 { + fn mul_assign(&mut self, rhs: Vec3) { + *self = *self * rhs; + } +} +impl ops::Div for Vec3 { + type Output = Vec3; + + fn div(self, rhs: Vec3) -> Self::Output { + (&self).divide(&rhs) + } +} +impl ops::DivAssign for Vec3 { + fn div_assign(&mut self, rhs: Vec3) { + *self = *self / rhs; + } +} diff --git a/graphene/src/vec4.rs b/graphene/src/vec4.rs index 7fcaa481f775..aa9bf9859999 100644 --- a/graphene/src/vec4.rs +++ b/graphene/src/vec4.rs @@ -1,6 +1,6 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use std::fmt; +use std::{fmt, ops}; use glib::translate::*; @@ -76,3 +76,83 @@ impl Default for Vec4 { Self::zero() } } + +// addition/subtraction +impl ops::Add for Vec4 { + type Output = Vec4; + + fn add(self, rhs: Vec4) -> Self::Output { + (&self).add(&rhs) + } +} +impl ops::AddAssign for Vec4 { + fn add_assign(&mut self, rhs: Vec4) { + *self = *self + rhs; + } +} +impl ops::Sub for Vec4 { + type Output = Vec4; + + fn sub(self, rhs: Vec4) -> Self::Output { + (&self).subtract(&rhs) + } +} +impl ops::SubAssign for Vec4 { + fn sub_assign(&mut self, rhs: Vec4) { + *self = *self - rhs; + } +} +impl ops::Neg for Vec4 { + type Output = Vec4; + + fn neg(self) -> Self::Output { + (&self).negate() + } +} + +// scalar multiplication +impl ops::Mul for Vec4 { + type Output = Vec4; + + fn mul(self, rhs: f32) -> Self::Output { + (&self).scale(rhs) + } +} +impl ops::MulAssign for Vec4 { + fn mul_assign(&mut self, rhs: f32) { + *self = *self * rhs; + } +} +impl ops::Mul for f32 { + type Output = Vec4; + + fn mul(self, rhs: Vec4) -> Self::Output { + rhs * self + } +} + +// Component-wise multiplication/division +impl ops::Mul for Vec4 { + type Output = Vec4; + + fn mul(self, rhs: Vec4) -> Self::Output { + (&self).multiply(&rhs) + } +} +impl ops::MulAssign for Vec4 { + fn mul_assign(&mut self, rhs: Vec4) { + *self = *self * rhs; + } +} +impl ops::Div for Vec4 { + type Output = Vec4; + + fn div(self, rhs: Vec4) -> Self::Output { + (&self).divide(&rhs) + } +} +impl ops::DivAssign for Vec4 { + fn div_assign(&mut self, rhs: Vec4) { + *self = *self / rhs; + } +}