Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator overloads for Graphene #1604

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions graphene/src/matrix.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -219,6 +219,64 @@ impl Default for Matrix {
}
}

// Scalar multiplication
impl ops::Mul<Matrix> 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<Matrix> for Matrix {
type Output = Matrix;

fn mul(self, rhs: Matrix) -> Self::Output {
(&self).multiply(&rhs)
}
}
impl ops::MulAssign<Matrix> for Matrix {
fn mul_assign(&mut self, rhs: Matrix) {
*self = *self * rhs;
}
}

impl ops::Mul<Vec4> for Matrix {
type Output = Vec4;

fn mul(self, rhs: Vec4) -> Self::Output {
(&self).transform_vec4(&rhs)
}
}

impl ops::Mul<Vec3> for Matrix {
type Output = Vec3;

fn mul(self, rhs: Vec3) -> Self::Output {
(&self).transform_vec3(&rhs)
}
}

impl ops::Mul<Point> for Matrix {
type Output = Point;

fn mul(self, rhs: Point) -> Self::Output {
(&self).transform_point(&rhs)
}
}


impl ops::Mul<Point3D> for Matrix {
type Output = Point3D;

fn mul(self, rhs: Point3D) -> Self::Output {
(&self).transform_point3d(&rhs)
}
}


#[cfg(test)]
mod tests {
use super::Matrix;
Expand Down
30 changes: 29 additions & 1 deletion graphene/src/quaternion.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down Expand Up @@ -145,3 +145,31 @@ impl fmt::Debug for Quaternion {
.finish()
}
}

impl ops::Add<Quaternion> for Quaternion {
type Output = Quaternion;

fn add(self, rhs: Quaternion) -> Self::Output {
(&self).add(&rhs)
}
}

impl ops::AddAssign<Quaternion> for Quaternion {
fn add_assign(&mut self, rhs: Quaternion) {
*self = *self + rhs;
}
}

impl ops::Mul<Quaternion> for Quaternion {
type Output = Quaternion;

fn mul(self, rhs: Quaternion) -> Self::Output {
(&self).multiply(&rhs)
}
}

impl ops::MulAssign<Quaternion> for Quaternion {
fn mul_assign(&mut self, rhs: Quaternion) {
*self = *self * rhs;
}
}
82 changes: 81 additions & 1 deletion graphene/src/vec2.rs
Original file line number Diff line number Diff line change
@@ -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};
jgcodes2020 marked this conversation as resolved.
Show resolved Hide resolved

use glib::translate::*;

Expand Down Expand Up @@ -52,3 +52,83 @@ impl Default for Vec2 {
Self::zero()
}
}

// addition/subtraction
impl ops::Add<Vec2> for Vec2 {
type Output = Vec2;

fn add(self, rhs: Vec2) -> Self::Output {
(&self).add(&rhs)
}
}
impl ops::AddAssign<Vec2> for Vec2 {
fn add_assign(&mut self, rhs: Vec2) {
*self = *self + rhs;
}
}
impl ops::Sub<Vec2> for Vec2 {
type Output = Vec2;

fn sub(self, rhs: Vec2) -> Self::Output {
(&self).subtract(&rhs)
}
}
impl ops::SubAssign<Vec2> 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<f32> for Vec2 {
type Output = Vec2;

fn mul(self, rhs: f32) -> Self::Output {
(&self).scale(rhs)
}
}
impl ops::MulAssign<f32> for Vec2 {
fn mul_assign(&mut self, rhs: f32) {
*self = *self * rhs;
}
}
impl ops::Mul<Vec2> for f32 {
type Output = Vec2;

fn mul(self, rhs: Vec2) -> Self::Output {
rhs * self
}
}

// Component-wise multiplication/division
impl ops::Mul<Vec2> for Vec2 {
type Output = Vec2;

fn mul(self, rhs: Vec2) -> Self::Output {
(&self).multiply(&rhs)
}
}
impl ops::MulAssign<Vec2> for Vec2 {
fn mul_assign(&mut self, rhs: Vec2) {
*self = *self * rhs;
}
}
impl ops::Div<Vec2> for Vec2 {
type Output = Vec2;

fn div(self, rhs: Vec2) -> Self::Output {
(&self).divide(&rhs)
}
}
impl ops::DivAssign<Vec2> for Vec2 {
fn div_assign(&mut self, rhs: Vec2) {
*self = *self / rhs;
}
}
82 changes: 81 additions & 1 deletion graphene/src/vec3.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down Expand Up @@ -53,3 +53,83 @@ impl Default for Vec3 {
Self::zero()
}
}

// addition/subtraction
impl ops::Add<Vec3> for Vec3 {
type Output = Vec3;

fn add(self, rhs: Vec3) -> Self::Output {
(&self).add(&rhs)
}
}
impl ops::AddAssign<Vec3> for Vec3 {
fn add_assign(&mut self, rhs: Vec3) {
*self = *self + rhs;
}
}
impl ops::Sub<Vec3> for Vec3 {
type Output = Vec3;

fn sub(self, rhs: Vec3) -> Self::Output {
(&self).subtract(&rhs)
}
}
impl ops::SubAssign<Vec3> 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<f32> for Vec3 {
type Output = Vec3;

fn mul(self, rhs: f32) -> Self::Output {
(&self).scale(rhs)
}
}
impl ops::MulAssign<f32> for Vec3 {
fn mul_assign(&mut self, rhs: f32) {
*self = *self * rhs;
}
}
impl ops::Mul<Vec3> for f32 {
type Output = Vec3;

fn mul(self, rhs: Vec3) -> Self::Output {
rhs * self
}
}

// Component-wise multiplication/division
impl ops::Mul<Vec3> for Vec3 {
type Output = Vec3;

fn mul(self, rhs: Vec3) -> Self::Output {
(&self).multiply(&rhs)
}
}
impl ops::MulAssign<Vec3> for Vec3 {
fn mul_assign(&mut self, rhs: Vec3) {
*self = *self * rhs;
}
}
impl ops::Div<Vec3> for Vec3 {
type Output = Vec3;

fn div(self, rhs: Vec3) -> Self::Output {
(&self).divide(&rhs)
}
}
impl ops::DivAssign<Vec3> for Vec3 {
fn div_assign(&mut self, rhs: Vec3) {
*self = *self / rhs;
}
}
82 changes: 81 additions & 1 deletion graphene/src/vec4.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand Down Expand Up @@ -76,3 +76,83 @@ impl Default for Vec4 {
Self::zero()
}
}

// addition/subtraction
impl ops::Add<Vec4> for Vec4 {
type Output = Vec4;

fn add(self, rhs: Vec4) -> Self::Output {
(&self).add(&rhs)
}
}
impl ops::AddAssign<Vec4> for Vec4 {
fn add_assign(&mut self, rhs: Vec4) {
*self = *self + rhs;
}
}
impl ops::Sub<Vec4> for Vec4 {
type Output = Vec4;

fn sub(self, rhs: Vec4) -> Self::Output {
(&self).subtract(&rhs)
}
}
impl ops::SubAssign<Vec4> 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<f32> for Vec4 {
type Output = Vec4;

fn mul(self, rhs: f32) -> Self::Output {
(&self).scale(rhs)
}
}
impl ops::MulAssign<f32> for Vec4 {
fn mul_assign(&mut self, rhs: f32) {
*self = *self * rhs;
}
}
impl ops::Mul<Vec4> for f32 {
type Output = Vec4;

fn mul(self, rhs: Vec4) -> Self::Output {
rhs * self
}
}

// Component-wise multiplication/division
impl ops::Mul<Vec4> for Vec4 {
type Output = Vec4;

fn mul(self, rhs: Vec4) -> Self::Output {
(&self).multiply(&rhs)
}
}
impl ops::MulAssign<Vec4> for Vec4 {
fn mul_assign(&mut self, rhs: Vec4) {
*self = *self * rhs;
}
}
impl ops::Div<Vec4> for Vec4 {
type Output = Vec4;

fn div(self, rhs: Vec4) -> Self::Output {
(&self).divide(&rhs)
}
}
impl ops::DivAssign<Vec4> for Vec4 {
fn div_assign(&mut self, rhs: Vec4) {
*self = *self / rhs;
}
}
Loading