Skip to content

Commit

Permalink
more matrix operations, quaternion operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jgcodes2020 committed Dec 19, 2024
1 parent 2242c8f commit f87d480
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 23 deletions.
60 changes: 38 additions & 22 deletions graphene/src/matrix.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 @@ -219,49 +219,47 @@ impl Default for Matrix {
}
}

#[cfg(test)]
mod tests {
use super::Matrix;
#[test]
fn test_matrix_values() {
let matrix = Matrix::new_identity();
assert_eq!(
matrix.values(),
&[
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]
],
);
// 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
}
}

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

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

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

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

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

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


impl std::ops::Mul<Point3D> for Matrix {
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;
#[test]
fn test_matrix_values() {
let matrix = Matrix::new_identity();
assert_eq!(
matrix.values(),
&[
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]
],
);
}
}
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;
}
}

0 comments on commit f87d480

Please sign in to comment.