|
1 | 1 | // Take a look at the license at the top of the repository in the LICENSE file.
|
2 | 2 |
|
3 |
| -use std::fmt; |
| 3 | +use std::{fmt, ops}; |
4 | 4 |
|
5 | 5 | use glib::translate::*;
|
6 | 6 |
|
7 |
| -use crate::{ffi, Matrix, Point3D, Vec3, Vec4}; |
| 7 | +use crate::{ffi, Matrix, Point, Point3D, Vec3, Vec4}; |
8 | 8 |
|
9 | 9 | impl Matrix {
|
10 | 10 | #[doc(alias = "graphene_matrix_init_from_2d")]
|
@@ -219,6 +219,68 @@ impl Default for Matrix {
|
219 | 219 | }
|
220 | 220 | }
|
221 | 221 |
|
| 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 | + |
222 | 284 | #[cfg(test)]
|
223 | 285 | mod tests {
|
224 | 286 | use super::Matrix;
|
|
0 commit comments