From 5672a44b47a4028931f39bd2eae873be214a31a3 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sun, 14 Oct 2018 12:17:00 -0700 Subject: [PATCH] Add multiplication of matrices to 3x3 and 4x4 --- MetaballsKit/Math.swift | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/MetaballsKit/Math.swift b/MetaballsKit/Math.swift index c0741da..66c016e 100644 --- a/MetaballsKit/Math.swift +++ b/MetaballsKit/Math.swift @@ -29,6 +29,12 @@ extension Float2: CustomStringConvertible { } } +extension Float3 { + func dot(other: Float3) -> Float3 { + return Float3(x * other.x, y * other.y, z * other.z) + } +} + extension Float4 { public init(r: Float, g: Float, b: Float, a: Float) { self.init(r, g, b, a) @@ -67,6 +73,12 @@ extension Matrix3x3 { } } +extension Matrix3x3 { + static func *(left: Matrix3x3, right: Matrix3x3) -> Matrix3x3 { + return matrix_multiply(left, right) + } +} + extension Matrix4x4 { /// Create a 4x4 orthographic projection matrix with the provided 6-tuple. /// @see https://en.wikipedia.org/wiki/Orthographic_projection @@ -79,6 +91,28 @@ extension Matrix4x4 { ] return Matrix4x4(rows: rows) } + + static func translation(dx: Float, dy: Float, dz: Float) -> Matrix4x4 { + var mat = self.init(1.0) + mat.columns.3.x = dx + mat.columns.3.y = dy + mat.columns.3.z = dz + return mat + } + + static func scale(x: Float, y: Float, z: Float) -> Matrix4x4 { + var mat = self.init(1.0) + mat.columns.0.x = x + mat.columns.1.y = y + mat.columns.2.z = z + return mat + } +} + +extension Matrix4x4 { + static func *(left: Matrix4x4, right: Matrix4x4) -> Matrix4x4 { + return matrix_multiply(left, right) + } } extension CGSize {