Skip to content

Commit 5235f6e

Browse files
committed
more matrix operations, quaternion operations
1 parent b071c05 commit 5235f6e

File tree

2 files changed

+67
-23
lines changed

2 files changed

+67
-23
lines changed

graphene/src/matrix.rs

Lines changed: 38 additions & 22 deletions
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

@@ -219,49 +219,47 @@ impl Default for Matrix {
219219
}
220220
}
221221

222-
#[cfg(test)]
223-
mod tests {
224-
use super::Matrix;
225-
#[test]
226-
fn test_matrix_values() {
227-
let matrix = Matrix::new_identity();
228-
assert_eq!(
229-
matrix.values(),
230-
&[
231-
[1.0, 0.0, 0.0, 0.0],
232-
[0.0, 1.0, 0.0, 0.0],
233-
[0.0, 0.0, 1.0, 0.0],
234-
[0.0, 0.0, 0.0, 1.0]
235-
],
236-
);
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
237229
}
238230
}
239231

240-
impl std::ops::Mul<Matrix> for Matrix {
232+
// Matrix-matrix/-vector multiplication
233+
impl ops::Mul<Matrix> for Matrix {
241234
type Output = Matrix;
242235

243236
fn mul(self, rhs: Matrix) -> Self::Output {
244237
(&self).multiply(&rhs)
245238
}
246239
}
240+
impl ops::MulAssign<Matrix> for Matrix {
241+
fn mul_assign(&mut self, rhs: Matrix) {
242+
*self = *self * rhs;
243+
}
244+
}
247245

248-
impl std::ops::Mul<Vec4> for Matrix {
246+
impl ops::Mul<Vec4> for Matrix {
249247
type Output = Vec4;
250248

251249
fn mul(self, rhs: Vec4) -> Self::Output {
252250
(&self).transform_vec4(&rhs)
253251
}
254252
}
255253

256-
impl std::ops::Mul<Vec3> for Matrix {
254+
impl ops::Mul<Vec3> for Matrix {
257255
type Output = Vec3;
258-
256+
259257
fn mul(self, rhs: Vec3) -> Self::Output {
260258
(&self).transform_vec3(&rhs)
261259
}
262260
}
263261

264-
impl std::ops::Mul<Point> for Matrix {
262+
impl ops::Mul<Point> for Matrix {
265263
type Output = Point;
266264

267265
fn mul(self, rhs: Point) -> Self::Output {
@@ -270,11 +268,29 @@ impl std::ops::Mul<Point> for Matrix {
270268
}
271269

272270

273-
impl std::ops::Mul<Point3D> for Matrix {
271+
impl ops::Mul<Point3D> for Matrix {
274272
type Output = Point3D;
275273

276274
fn mul(self, rhs: Point3D) -> Self::Output {
277275
(&self).transform_point3d(&rhs)
278276
}
279277
}
280278

279+
280+
#[cfg(test)]
281+
mod tests {
282+
use super::Matrix;
283+
#[test]
284+
fn test_matrix_values() {
285+
let matrix = Matrix::new_identity();
286+
assert_eq!(
287+
matrix.values(),
288+
&[
289+
[1.0, 0.0, 0.0, 0.0],
290+
[0.0, 1.0, 0.0, 0.0],
291+
[0.0, 0.0, 1.0, 0.0],
292+
[0.0, 0.0, 0.0, 1.0]
293+
],
294+
);
295+
}
296+
}

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+
(&self).add(&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+
(&self).multiply(&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+
}

0 commit comments

Comments
 (0)