Skip to content

Commit

Permalink
Merge pull request #1604 from jgcodes2020/operator-overloads
Browse files Browse the repository at this point in the history
Operator overloads for Graphene
  • Loading branch information
sdroege authored Jan 27, 2025
2 parents bb40a03 + 06538f9 commit 35e15b7
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 6 deletions.
66 changes: 64 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,68 @@ 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 {
Matrix::multiply(&self, &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;

/// Transforms this `Vec4` using the provided matrix.
/// See [Matrix::transform_vec4].
fn mul(self, rhs: Vec4) -> Self::Output {
Matrix::transform_vec4(&self, &rhs)
}
}

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

/// Transforms this `Vec3` using the provided matrix.
/// See [Matrix::transform_vec3].
fn mul(self, rhs: Vec3) -> Self::Output {
Matrix::transform_vec3(&self, &rhs)
}
}

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

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

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

/// Transforms this point using the provided matrix.
/// See [Matrix::transform_point3d].
fn mul(self, rhs: Point3D) -> Self::Output {
Matrix::transform_point3d(&self, &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 {
Quaternion::add(&self, &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 {
Quaternion::multiply(&self, &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};

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 {
Vec2::add(&self, &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 {
Vec2::subtract(&self, &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 {
Vec2::negate(&self)
}
}

// scalar multiplication
impl ops::Mul<f32> for Vec2 {
type Output = Vec2;

fn mul(self, rhs: f32) -> Self::Output {
Vec2::scale(&self, 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 {
Vec2::multiply(&self, &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 {
Vec2::divide(&self, &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 {
Vec3::add(&self, &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 {
Vec3::subtract(&self, &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 {
Vec3::negate(&self)
}
}

// scalar multiplication
impl ops::Mul<f32> for Vec3 {
type Output = Vec3;

fn mul(self, rhs: f32) -> Self::Output {
Vec3::scale(&self, 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 {
Vec3::multiply(&self, &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 {
Vec3::divide(&self, &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 {
Vec4::add(&self, &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 {
Vec4::subtract(&self, &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 {
Vec4::negate(&self)
}
}

// scalar multiplication
impl ops::Mul<f32> for Vec4 {
type Output = Vec4;

fn mul(self, rhs: f32) -> Self::Output {
Vec4::scale(&self, 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 {
Vec4::multiply(&self, &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 {
Vec4::divide(&self, &rhs)
}
}
impl ops::DivAssign<Vec4> for Vec4 {
fn div_assign(&mut self, rhs: Vec4) {
*self = *self / rhs;
}
}

0 comments on commit 35e15b7

Please sign in to comment.