From 0b7e0b6a41d4fad936575f564bcfb58078fefc53 Mon Sep 17 00:00:00 2001 From: Eugene Bokhan Date: Fri, 28 Feb 2020 23:23:10 +0300 Subject: [PATCH 1/4] add codable conformance to matrix3x3 --- Sources/Matrix3x3+Codable.swift | 28 ++++++++++++++++++++++++++++ Sources/float3x3+Codable.swift | 32 ++++++++++++++++++++++++++++++++ SwiftMath.podspec | 3 ++- 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 Sources/Matrix3x3+Codable.swift create mode 100644 Sources/float3x3+Codable.swift diff --git a/Sources/Matrix3x3+Codable.swift b/Sources/Matrix3x3+Codable.swift new file mode 100644 index 0000000..8e7b6ed --- /dev/null +++ b/Sources/Matrix3x3+Codable.swift @@ -0,0 +1,28 @@ +// +// Matrix3x3+Codable.swift +// org.SwiftGFX.SwiftMath +// +// Created by Eugene Bokhan on 28.02.20. +// +// + +import Foundation +import simd + +extension Matrix3x3f: Codable { + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + try self.init(container.decode(float3x3.self, + forKey: .float3x3)) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(float3x3(self), + forKey: .float3x3) + } + + private enum CodingKeys: String, CodingKey { + case float3x3 + } +} diff --git a/Sources/float3x3+Codable.swift b/Sources/float3x3+Codable.swift new file mode 100644 index 0000000..887a55e --- /dev/null +++ b/Sources/float3x3+Codable.swift @@ -0,0 +1,32 @@ +// +// float3x3+Codable.swift +// org.SwiftGFX.SwiftMath +// +// Created by Eugene Bokhan on 28.02.20. +// +// + +import Foundation +import simd + +extension float3x3: Codable { + public init(from decoder: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + let c1 = try values.decode(SIMD3.self, forKey: .column1) + let c2 = try values.decode(SIMD3.self, forKey: .column2) + let c3 = try values.decode(SIMD3.self, forKey: .column3) + + self.init(c1, c2, c3) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(self.columns.0, forKey: .column1) + try container.encode(self.columns.1, forKey: .column2) + try container.encode(self.columns.2, forKey: .column3) + } + + private enum CodingKeys: String, CodingKey { + case column1, column2, column3 + } +} diff --git a/SwiftMath.podspec b/SwiftMath.podspec index ccb8429..6e7647b 100644 --- a/SwiftMath.podspec +++ b/SwiftMath.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "SwiftMath" - s.version = "3.1.4" + s.version = "3.2.1" s.summary = "Floating point math library written using idiomatic Swift" s.description = <<-DESC @@ -15,6 +15,7 @@ Pod::Spec.new do |s| s.author = { "Andrey Volodin" => "siddok@gmail.com" } s.social_media_url = "http://twitter.com/s1ddok" + s.swift_versions = ['4.2', '5.0'] # When using multiple platforms s.ios.deployment_target = "9.0" From 347db1cf6142b57dbf387ffb4525624f4a6d4cf3 Mon Sep 17 00:00:00 2001 From: Eugene Bokhan Date: Fri, 28 Feb 2020 23:34:50 +0300 Subject: [PATCH 2/4] add equatable extension --- ...Matrix3x3+Codable.swift => Matrix3x3+Extensions.swift} | 8 +++++++- .../{float3x3+Codable.swift => float3x3+Extensions.swift} | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) rename Sources/{Matrix3x3+Codable.swift => Matrix3x3+Extensions.swift} (78%) rename Sources/{float3x3+Codable.swift => float3x3+Extensions.swift} (96%) diff --git a/Sources/Matrix3x3+Codable.swift b/Sources/Matrix3x3+Extensions.swift similarity index 78% rename from Sources/Matrix3x3+Codable.swift rename to Sources/Matrix3x3+Extensions.swift index 8e7b6ed..9967ca1 100644 --- a/Sources/Matrix3x3+Codable.swift +++ b/Sources/Matrix3x3+Extensions.swift @@ -1,5 +1,5 @@ // -// Matrix3x3+Codable.swift +// Matrix3x3+Extensions.swift // org.SwiftGFX.SwiftMath // // Created by Eugene Bokhan on 28.02.20. @@ -26,3 +26,9 @@ extension Matrix3x3f: Codable { case float3x3 } } + +extension Matrix3x3f: Equatable { + public static func == (lhs: Matrix3x3f, rhs: Matrix3x3f) -> Bool { + float3x3(lhs) == float3x3(rhs) + } +} diff --git a/Sources/float3x3+Codable.swift b/Sources/float3x3+Extensions.swift similarity index 96% rename from Sources/float3x3+Codable.swift rename to Sources/float3x3+Extensions.swift index 887a55e..7384f1e 100644 --- a/Sources/float3x3+Codable.swift +++ b/Sources/float3x3+Extensions.swift @@ -1,5 +1,5 @@ // -// float3x3+Codable.swift +// float3x3+Extensions.swift // org.SwiftGFX.SwiftMath // // Created by Eugene Bokhan on 28.02.20. From 9fde6ca286535c3cb5b0d3237a189229cff3e0cb Mon Sep 17 00:00:00 2001 From: Eugene Bokhan Date: Fri, 6 Mar 2020 15:16:40 +0300 Subject: [PATCH 3/4] add float4x4 extension --- Sources/Matrix3x3+Extensions.swift | 34 -------------------------- Sources/Matrix3x3+simd.swift | 12 ++++++---- Sources/Matrix4x4+simd.swift | 12 ++++++---- Sources/float3x3+Extensions.swift | 4 ++++ Sources/float4x4+Extensions.swift | 38 ++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 42 deletions(-) delete mode 100644 Sources/Matrix3x3+Extensions.swift create mode 100644 Sources/float4x4+Extensions.swift diff --git a/Sources/Matrix3x3+Extensions.swift b/Sources/Matrix3x3+Extensions.swift deleted file mode 100644 index 9967ca1..0000000 --- a/Sources/Matrix3x3+Extensions.swift +++ /dev/null @@ -1,34 +0,0 @@ -// -// Matrix3x3+Extensions.swift -// org.SwiftGFX.SwiftMath -// -// Created by Eugene Bokhan on 28.02.20. -// -// - -import Foundation -import simd - -extension Matrix3x3f: Codable { - public init(from decoder: Decoder) throws { - let container = try decoder.container(keyedBy: CodingKeys.self) - try self.init(container.decode(float3x3.self, - forKey: .float3x3)) - } - - public func encode(to encoder: Encoder) throws { - var container = encoder.container(keyedBy: CodingKeys.self) - try container.encode(float3x3(self), - forKey: .float3x3) - } - - private enum CodingKeys: String, CodingKey { - case float3x3 - } -} - -extension Matrix3x3f: Equatable { - public static func == (lhs: Matrix3x3f, rhs: Matrix3x3f) -> Bool { - float3x3(lhs) == float3x3(rhs) - } -} diff --git a/Sources/Matrix3x3+simd.swift b/Sources/Matrix3x3+simd.swift index 28e1560..163d923 100644 --- a/Sources/Matrix3x3+simd.swift +++ b/Sources/Matrix3x3+simd.swift @@ -9,10 +9,10 @@ import simd /// - remark: /// Matrices are stored in column-major order @frozen -public struct Matrix3x3f { +public struct Matrix3x3f: Codable, Equatable { internal var d: matrix_float3x3 - //MARK: - initializers + // MARK: - initializers /// Creates an instance initialized with either existing simd matrix or zeros public init(simdMatrix: matrix_float3x3 = .init()) { @@ -38,13 +38,13 @@ public struct Matrix3x3f { self.d = matrix_float3x3(columns: (c0.d, c1.d, c2.d)) } - //MARK:- properties + // MARK:- properties public var inversed: Matrix3x3f { return unsafeBitCast(d.inverse, to: Matrix3x3f.self) } - //MARK:- operators + // MARK:- operators public static prefix func -(lhs: Matrix3x3f) -> Matrix3x3f { return unsafeBitCast(-lhs.d, to: Matrix3x3f.self) @@ -57,6 +57,10 @@ public struct Matrix3x3f { public static func *(lhs: Matrix3x3f, rhs: Matrix3x3f) -> Matrix3x3f { return unsafeBitCast(lhs.d * rhs.d, to: Matrix3x3f.self) } + + public static func == (lhs: Matrix3x3f, rhs: Matrix3x3f) -> Bool { + float3x3(lhs) == float3x3(rhs) + } // MARK: - subscript operations diff --git a/Sources/Matrix4x4+simd.swift b/Sources/Matrix4x4+simd.swift index 617eea6..f44ab52 100644 --- a/Sources/Matrix4x4+simd.swift +++ b/Sources/Matrix4x4+simd.swift @@ -9,10 +9,10 @@ import simd /// - remark: /// Matrices are stored in column-major order @frozen -public struct Matrix4x4f { +public struct Matrix4x4f: Codable, Equatable { internal var d: matrix_float4x4 - //MARK: - initializers + // MARK: - initializers /// Creates an instance initialized with either existing simd matrix or zeros public init(simdMatrix: matrix_float4x4 = .init()) { @@ -39,7 +39,7 @@ public struct Matrix4x4f { self.d = matrix_float4x4(columns: (c0.d, c1.d, c2.d, c3.d)) } - //MARK:- properties + // MARK:- properties public var transposed: Matrix4x4f { return unsafeBitCast(d.transpose, to: Matrix4x4f.self) @@ -73,7 +73,7 @@ public struct Matrix4x4f { } } - //MARK:- operators + // MARK:- operators public static prefix func -(lhs: Matrix4x4f) -> Matrix4x4f { return unsafeBitCast(-lhs.d, to: Matrix4x4f.self) @@ -86,6 +86,10 @@ public struct Matrix4x4f { public static func *(lhs: Matrix4x4f, rhs: Matrix4x4f) -> Matrix4x4f { return unsafeBitCast(lhs.d * rhs.d, to: Matrix4x4f.self) } + + public static func == (lhs: Matrix4x4f, rhs: Matrix4x4f) -> Bool { + float4x4(lhs) == float4x4(rhs) + } } public extension matrix_float4x4 { diff --git a/Sources/float3x3+Extensions.swift b/Sources/float3x3+Extensions.swift index 7384f1e..d638907 100644 --- a/Sources/float3x3+Extensions.swift +++ b/Sources/float3x3+Extensions.swift @@ -6,6 +6,8 @@ // // +#if !NOSIMD + import Foundation import simd @@ -30,3 +32,5 @@ extension float3x3: Codable { case column1, column2, column3 } } + +#endif diff --git a/Sources/float4x4+Extensions.swift b/Sources/float4x4+Extensions.swift new file mode 100644 index 0000000..44bc904 --- /dev/null +++ b/Sources/float4x4+Extensions.swift @@ -0,0 +1,38 @@ +// +// float4x4+Extensions.swift +// org.SwiftGFX.SwiftMath +// +// Created by Eugene Bokhan on 28.02.20. +// +// + +#if !NOSIMD + +import Foundation +import simd + +extension float4x4: Codable { + public init(from decoder: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + let c1 = try values.decode(SIMD4.self, forKey: .column1) + let c2 = try values.decode(SIMD4.self, forKey: .column2) + let c3 = try values.decode(SIMD4.self, forKey: .column3) + let c4 = try values.decode(SIMD4.self, forKey: .column4) + + self.init(c1, c2, c3, c4) + } + + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(self.columns.0, forKey: .column1) + try container.encode(self.columns.1, forKey: .column2) + try container.encode(self.columns.2, forKey: .column3) + try container.encode(self.columns.3, forKey: .column4) + } + + private enum CodingKeys: String, CodingKey { + case column1, column2, column3, column4 + } +} + +#endif From aab73c842ce49219fd596bc978e164235fafea9c Mon Sep 17 00:00:00 2001 From: Eugene Bokhan Date: Fri, 6 Mar 2020 17:01:40 +0300 Subject: [PATCH 4/4] fix equatable --- Sources/Matrix3x3+simd.swift | 2 +- Sources/Matrix4x4+simd.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Matrix3x3+simd.swift b/Sources/Matrix3x3+simd.swift index 163d923..9fdc74d 100644 --- a/Sources/Matrix3x3+simd.swift +++ b/Sources/Matrix3x3+simd.swift @@ -59,7 +59,7 @@ public struct Matrix3x3f: Codable, Equatable { } public static func == (lhs: Matrix3x3f, rhs: Matrix3x3f) -> Bool { - float3x3(lhs) == float3x3(rhs) + lhs.d == rhs.d } // MARK: - subscript operations diff --git a/Sources/Matrix4x4+simd.swift b/Sources/Matrix4x4+simd.swift index f44ab52..b786e64 100644 --- a/Sources/Matrix4x4+simd.swift +++ b/Sources/Matrix4x4+simd.swift @@ -88,7 +88,7 @@ public struct Matrix4x4f: Codable, Equatable { } public static func == (lhs: Matrix4x4f, rhs: Matrix4x4f) -> Bool { - float4x4(lhs) == float4x4(rhs) + lhs.d == rhs.d } }