Skip to content

Commit

Permalink
vector operator overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
jgcodes2020 committed Dec 16, 2024
1 parent 83f2e55 commit bda9a99
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 3 deletions.
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 {
(&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;
}
}

0 comments on commit bda9a99

Please sign in to comment.