Skip to content

Commit 35e15b7

Browse files
authored
Merge pull request #1604 from jgcodes2020/operator-overloads
Operator overloads for Graphene
2 parents bb40a03 + 06538f9 commit 35e15b7

File tree

5 files changed

+336
-6
lines changed

5 files changed

+336
-6
lines changed

graphene/src/matrix.rs

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use std::fmt;
3+
use std::{fmt, ops};
44

55
use glib::translate::*;
66

7-
use crate::{ffi, Matrix, Point3D, Vec3, Vec4};
7+
use crate::{ffi, Matrix, Point, Point3D, Vec3, Vec4};
88

99
impl Matrix {
1010
#[doc(alias = "graphene_matrix_init_from_2d")]
@@ -219,6 +219,68 @@ impl Default for Matrix {
219219
}
220220
}
221221

222+
// Scalar multiplication
223+
impl ops::Mul<Matrix> for f32 {
224+
type Output = Matrix;
225+
226+
fn mul(self, mut rhs: Matrix) -> Self::Output {
227+
rhs.scale(self, self, self);
228+
rhs
229+
}
230+
}
231+
232+
// Matrix-matrix/-vector multiplication
233+
impl ops::Mul<Matrix> for Matrix {
234+
type Output = Matrix;
235+
236+
fn mul(self, rhs: Matrix) -> Self::Output {
237+
Matrix::multiply(&self, &rhs)
238+
}
239+
}
240+
impl ops::MulAssign<Matrix> for Matrix {
241+
fn mul_assign(&mut self, rhs: Matrix) {
242+
*self = *self * rhs;
243+
}
244+
}
245+
246+
impl ops::Mul<Vec4> for Matrix {
247+
type Output = Vec4;
248+
249+
/// Transforms this `Vec4` using the provided matrix.
250+
/// See [Matrix::transform_vec4].
251+
fn mul(self, rhs: Vec4) -> Self::Output {
252+
Matrix::transform_vec4(&self, &rhs)
253+
}
254+
}
255+
256+
impl ops::Mul<Vec3> for Matrix {
257+
type Output = Vec3;
258+
259+
/// Transforms this `Vec3` using the provided matrix.
260+
/// See [Matrix::transform_vec3].
261+
fn mul(self, rhs: Vec3) -> Self::Output {
262+
Matrix::transform_vec3(&self, &rhs)
263+
}
264+
}
265+
266+
impl ops::Mul<Point> for Matrix {
267+
type Output = Point;
268+
269+
fn mul(self, rhs: Point) -> Self::Output {
270+
Matrix::transform_point(&self, &rhs)
271+
}
272+
}
273+
274+
impl ops::Mul<Point3D> for Matrix {
275+
type Output = Point3D;
276+
277+
/// Transforms this point using the provided matrix.
278+
/// See [Matrix::transform_point3d].
279+
fn mul(self, rhs: Point3D) -> Self::Output {
280+
Matrix::transform_point3d(&self, &rhs)
281+
}
282+
}
283+
222284
#[cfg(test)]
223285
mod tests {
224286
use super::Matrix;

graphene/src/quaternion.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use std::fmt;
3+
use std::{fmt, ops};
44

55
use glib::translate::*;
66

@@ -145,3 +145,31 @@ impl fmt::Debug for Quaternion {
145145
.finish()
146146
}
147147
}
148+
149+
impl ops::Add<Quaternion> for Quaternion {
150+
type Output = Quaternion;
151+
152+
fn add(self, rhs: Quaternion) -> Self::Output {
153+
Quaternion::add(&self, &rhs)
154+
}
155+
}
156+
157+
impl ops::AddAssign<Quaternion> for Quaternion {
158+
fn add_assign(&mut self, rhs: Quaternion) {
159+
*self = *self + rhs;
160+
}
161+
}
162+
163+
impl ops::Mul<Quaternion> for Quaternion {
164+
type Output = Quaternion;
165+
166+
fn mul(self, rhs: Quaternion) -> Self::Output {
167+
Quaternion::multiply(&self, &rhs)
168+
}
169+
}
170+
171+
impl ops::MulAssign<Quaternion> for Quaternion {
172+
fn mul_assign(&mut self, rhs: Quaternion) {
173+
*self = *self * rhs;
174+
}
175+
}

graphene/src/vec2.rs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use std::fmt;
3+
use std::{fmt, ops};
44

55
use glib::translate::*;
66

@@ -52,3 +52,83 @@ impl Default for Vec2 {
5252
Self::zero()
5353
}
5454
}
55+
56+
// addition/subtraction
57+
impl ops::Add<Vec2> for Vec2 {
58+
type Output = Vec2;
59+
60+
fn add(self, rhs: Vec2) -> Self::Output {
61+
Vec2::add(&self, &rhs)
62+
}
63+
}
64+
impl ops::AddAssign<Vec2> for Vec2 {
65+
fn add_assign(&mut self, rhs: Vec2) {
66+
*self = *self + rhs;
67+
}
68+
}
69+
impl ops::Sub<Vec2> for Vec2 {
70+
type Output = Vec2;
71+
72+
fn sub(self, rhs: Vec2) -> Self::Output {
73+
Vec2::subtract(&self, &rhs)
74+
}
75+
}
76+
impl ops::SubAssign<Vec2> for Vec2 {
77+
fn sub_assign(&mut self, rhs: Vec2) {
78+
*self = *self - rhs;
79+
}
80+
}
81+
impl ops::Neg for Vec2 {
82+
type Output = Vec2;
83+
84+
fn neg(self) -> Self::Output {
85+
Vec2::negate(&self)
86+
}
87+
}
88+
89+
// scalar multiplication
90+
impl ops::Mul<f32> for Vec2 {
91+
type Output = Vec2;
92+
93+
fn mul(self, rhs: f32) -> Self::Output {
94+
Vec2::scale(&self, rhs)
95+
}
96+
}
97+
impl ops::MulAssign<f32> for Vec2 {
98+
fn mul_assign(&mut self, rhs: f32) {
99+
*self = *self * rhs;
100+
}
101+
}
102+
impl ops::Mul<Vec2> for f32 {
103+
type Output = Vec2;
104+
105+
fn mul(self, rhs: Vec2) -> Self::Output {
106+
rhs * self
107+
}
108+
}
109+
110+
// Component-wise multiplication/division
111+
impl ops::Mul<Vec2> for Vec2 {
112+
type Output = Vec2;
113+
114+
fn mul(self, rhs: Vec2) -> Self::Output {
115+
Vec2::multiply(&self, &rhs)
116+
}
117+
}
118+
impl ops::MulAssign<Vec2> for Vec2 {
119+
fn mul_assign(&mut self, rhs: Vec2) {
120+
*self = *self * rhs;
121+
}
122+
}
123+
impl ops::Div<Vec2> for Vec2 {
124+
type Output = Vec2;
125+
126+
fn div(self, rhs: Vec2) -> Self::Output {
127+
Vec2::divide(&self, &rhs)
128+
}
129+
}
130+
impl ops::DivAssign<Vec2> for Vec2 {
131+
fn div_assign(&mut self, rhs: Vec2) {
132+
*self = *self / rhs;
133+
}
134+
}

graphene/src/vec3.rs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use std::fmt;
3+
use std::{fmt, ops};
44

55
use glib::translate::*;
66

@@ -53,3 +53,83 @@ impl Default for Vec3 {
5353
Self::zero()
5454
}
5555
}
56+
57+
// addition/subtraction
58+
impl ops::Add<Vec3> for Vec3 {
59+
type Output = Vec3;
60+
61+
fn add(self, rhs: Vec3) -> Self::Output {
62+
Vec3::add(&self, &rhs)
63+
}
64+
}
65+
impl ops::AddAssign<Vec3> for Vec3 {
66+
fn add_assign(&mut self, rhs: Vec3) {
67+
*self = *self + rhs;
68+
}
69+
}
70+
impl ops::Sub<Vec3> for Vec3 {
71+
type Output = Vec3;
72+
73+
fn sub(self, rhs: Vec3) -> Self::Output {
74+
Vec3::subtract(&self, &rhs)
75+
}
76+
}
77+
impl ops::SubAssign<Vec3> for Vec3 {
78+
fn sub_assign(&mut self, rhs: Vec3) {
79+
*self = *self - rhs;
80+
}
81+
}
82+
impl ops::Neg for Vec3 {
83+
type Output = Vec3;
84+
85+
fn neg(self) -> Self::Output {
86+
Vec3::negate(&self)
87+
}
88+
}
89+
90+
// scalar multiplication
91+
impl ops::Mul<f32> for Vec3 {
92+
type Output = Vec3;
93+
94+
fn mul(self, rhs: f32) -> Self::Output {
95+
Vec3::scale(&self, rhs)
96+
}
97+
}
98+
impl ops::MulAssign<f32> for Vec3 {
99+
fn mul_assign(&mut self, rhs: f32) {
100+
*self = *self * rhs;
101+
}
102+
}
103+
impl ops::Mul<Vec3> for f32 {
104+
type Output = Vec3;
105+
106+
fn mul(self, rhs: Vec3) -> Self::Output {
107+
rhs * self
108+
}
109+
}
110+
111+
// Component-wise multiplication/division
112+
impl ops::Mul<Vec3> for Vec3 {
113+
type Output = Vec3;
114+
115+
fn mul(self, rhs: Vec3) -> Self::Output {
116+
Vec3::multiply(&self, &rhs)
117+
}
118+
}
119+
impl ops::MulAssign<Vec3> for Vec3 {
120+
fn mul_assign(&mut self, rhs: Vec3) {
121+
*self = *self * rhs;
122+
}
123+
}
124+
impl ops::Div<Vec3> for Vec3 {
125+
type Output = Vec3;
126+
127+
fn div(self, rhs: Vec3) -> Self::Output {
128+
Vec3::divide(&self, &rhs)
129+
}
130+
}
131+
impl ops::DivAssign<Vec3> for Vec3 {
132+
fn div_assign(&mut self, rhs: Vec3) {
133+
*self = *self / rhs;
134+
}
135+
}

graphene/src/vec4.rs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use std::fmt;
3+
use std::{fmt, ops};
44

55
use glib::translate::*;
66

@@ -76,3 +76,83 @@ impl Default for Vec4 {
7676
Self::zero()
7777
}
7878
}
79+
80+
// addition/subtraction
81+
impl ops::Add<Vec4> for Vec4 {
82+
type Output = Vec4;
83+
84+
fn add(self, rhs: Vec4) -> Self::Output {
85+
Vec4::add(&self, &rhs)
86+
}
87+
}
88+
impl ops::AddAssign<Vec4> for Vec4 {
89+
fn add_assign(&mut self, rhs: Vec4) {
90+
*self = *self + rhs;
91+
}
92+
}
93+
impl ops::Sub<Vec4> for Vec4 {
94+
type Output = Vec4;
95+
96+
fn sub(self, rhs: Vec4) -> Self::Output {
97+
Vec4::subtract(&self, &rhs)
98+
}
99+
}
100+
impl ops::SubAssign<Vec4> for Vec4 {
101+
fn sub_assign(&mut self, rhs: Vec4) {
102+
*self = *self - rhs;
103+
}
104+
}
105+
impl ops::Neg for Vec4 {
106+
type Output = Vec4;
107+
108+
fn neg(self) -> Self::Output {
109+
Vec4::negate(&self)
110+
}
111+
}
112+
113+
// scalar multiplication
114+
impl ops::Mul<f32> for Vec4 {
115+
type Output = Vec4;
116+
117+
fn mul(self, rhs: f32) -> Self::Output {
118+
Vec4::scale(&self, rhs)
119+
}
120+
}
121+
impl ops::MulAssign<f32> for Vec4 {
122+
fn mul_assign(&mut self, rhs: f32) {
123+
*self = *self * rhs;
124+
}
125+
}
126+
impl ops::Mul<Vec4> for f32 {
127+
type Output = Vec4;
128+
129+
fn mul(self, rhs: Vec4) -> Self::Output {
130+
rhs * self
131+
}
132+
}
133+
134+
// Component-wise multiplication/division
135+
impl ops::Mul<Vec4> for Vec4 {
136+
type Output = Vec4;
137+
138+
fn mul(self, rhs: Vec4) -> Self::Output {
139+
Vec4::multiply(&self, &rhs)
140+
}
141+
}
142+
impl ops::MulAssign<Vec4> for Vec4 {
143+
fn mul_assign(&mut self, rhs: Vec4) {
144+
*self = *self * rhs;
145+
}
146+
}
147+
impl ops::Div<Vec4> for Vec4 {
148+
type Output = Vec4;
149+
150+
fn div(self, rhs: Vec4) -> Self::Output {
151+
Vec4::divide(&self, &rhs)
152+
}
153+
}
154+
impl ops::DivAssign<Vec4> for Vec4 {
155+
fn div_assign(&mut self, rhs: Vec4) {
156+
*self = *self / rhs;
157+
}
158+
}

0 commit comments

Comments
 (0)