Skip to content

Commit d81238a

Browse files
authored
Matrix3x3+Codable (#6)
* add codable conformance to matrix3x3 * add equatable extension * add float4x4 extension * fix equatable
1 parent 5e5001c commit d81238a

5 files changed

+91
-9
lines changed

Sources/Matrix3x3+simd.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import simd
99
/// - remark:
1010
/// Matrices are stored in column-major order
1111
@frozen
12-
public struct Matrix3x3f {
12+
public struct Matrix3x3f: Codable, Equatable {
1313
internal var d: matrix_float3x3
1414

15-
//MARK: - initializers
15+
// MARK: - initializers
1616

1717
/// Creates an instance initialized with either existing simd matrix or zeros
1818
public init(simdMatrix: matrix_float3x3 = .init()) {
@@ -38,13 +38,13 @@ public struct Matrix3x3f {
3838
self.d = matrix_float3x3(columns: (c0.d, c1.d, c2.d))
3939
}
4040

41-
//MARK:- properties
41+
// MARK:- properties
4242

4343
public var inversed: Matrix3x3f {
4444
return unsafeBitCast(d.inverse, to: Matrix3x3f.self)
4545
}
4646

47-
//MARK:- operators
47+
// MARK:- operators
4848

4949
public static prefix func -(lhs: Matrix3x3f) -> Matrix3x3f {
5050
return unsafeBitCast(-lhs.d, to: Matrix3x3f.self)
@@ -57,6 +57,10 @@ public struct Matrix3x3f {
5757
public static func *(lhs: Matrix3x3f, rhs: Matrix3x3f) -> Matrix3x3f {
5858
return unsafeBitCast(lhs.d * rhs.d, to: Matrix3x3f.self)
5959
}
60+
61+
public static func == (lhs: Matrix3x3f, rhs: Matrix3x3f) -> Bool {
62+
lhs.d == rhs.d
63+
}
6064

6165
// MARK: - subscript operations
6266

Sources/Matrix4x4+simd.swift

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import simd
99
/// - remark:
1010
/// Matrices are stored in column-major order
1111
@frozen
12-
public struct Matrix4x4f {
12+
public struct Matrix4x4f: Codable, Equatable {
1313
internal var d: matrix_float4x4
1414

15-
//MARK: - initializers
15+
// MARK: - initializers
1616

1717
/// Creates an instance initialized with either existing simd matrix or zeros
1818
public init(simdMatrix: matrix_float4x4 = .init()) {
@@ -39,7 +39,7 @@ public struct Matrix4x4f {
3939
self.d = matrix_float4x4(columns: (c0.d, c1.d, c2.d, c3.d))
4040
}
4141

42-
//MARK:- properties
42+
// MARK:- properties
4343

4444
public var transposed: Matrix4x4f {
4545
return unsafeBitCast(d.transpose, to: Matrix4x4f.self)
@@ -73,7 +73,7 @@ public struct Matrix4x4f {
7373
}
7474
}
7575

76-
//MARK:- operators
76+
// MARK:- operators
7777

7878
public static prefix func -(lhs: Matrix4x4f) -> Matrix4x4f {
7979
return unsafeBitCast(-lhs.d, to: Matrix4x4f.self)
@@ -86,6 +86,10 @@ public struct Matrix4x4f {
8686
public static func *(lhs: Matrix4x4f, rhs: Matrix4x4f) -> Matrix4x4f {
8787
return unsafeBitCast(lhs.d * rhs.d, to: Matrix4x4f.self)
8888
}
89+
90+
public static func == (lhs: Matrix4x4f, rhs: Matrix4x4f) -> Bool {
91+
lhs.d == rhs.d
92+
}
8993
}
9094

9195
public extension matrix_float4x4 {

Sources/float3x3+Extensions.swift

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// float3x3+Extensions.swift
3+
// org.SwiftGFX.SwiftMath
4+
//
5+
// Created by Eugene Bokhan on 28.02.20.
6+
//
7+
//
8+
9+
#if !NOSIMD
10+
11+
import Foundation
12+
import simd
13+
14+
extension float3x3: Codable {
15+
public init(from decoder: Decoder) throws {
16+
let values = try decoder.container(keyedBy: CodingKeys.self)
17+
let c1 = try values.decode(SIMD3<Float>.self, forKey: .column1)
18+
let c2 = try values.decode(SIMD3<Float>.self, forKey: .column2)
19+
let c3 = try values.decode(SIMD3<Float>.self, forKey: .column3)
20+
21+
self.init(c1, c2, c3)
22+
}
23+
24+
public func encode(to encoder: Encoder) throws {
25+
var container = encoder.container(keyedBy: CodingKeys.self)
26+
try container.encode(self.columns.0, forKey: .column1)
27+
try container.encode(self.columns.1, forKey: .column2)
28+
try container.encode(self.columns.2, forKey: .column3)
29+
}
30+
31+
private enum CodingKeys: String, CodingKey {
32+
case column1, column2, column3
33+
}
34+
}
35+
36+
#endif

Sources/float4x4+Extensions.swift

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// float4x4+Extensions.swift
3+
// org.SwiftGFX.SwiftMath
4+
//
5+
// Created by Eugene Bokhan on 28.02.20.
6+
//
7+
//
8+
9+
#if !NOSIMD
10+
11+
import Foundation
12+
import simd
13+
14+
extension float4x4: Codable {
15+
public init(from decoder: Decoder) throws {
16+
let values = try decoder.container(keyedBy: CodingKeys.self)
17+
let c1 = try values.decode(SIMD4<Float>.self, forKey: .column1)
18+
let c2 = try values.decode(SIMD4<Float>.self, forKey: .column2)
19+
let c3 = try values.decode(SIMD4<Float>.self, forKey: .column3)
20+
let c4 = try values.decode(SIMD4<Float>.self, forKey: .column4)
21+
22+
self.init(c1, c2, c3, c4)
23+
}
24+
25+
public func encode(to encoder: Encoder) throws {
26+
var container = encoder.container(keyedBy: CodingKeys.self)
27+
try container.encode(self.columns.0, forKey: .column1)
28+
try container.encode(self.columns.1, forKey: .column2)
29+
try container.encode(self.columns.2, forKey: .column3)
30+
try container.encode(self.columns.3, forKey: .column4)
31+
}
32+
33+
private enum CodingKeys: String, CodingKey {
34+
case column1, column2, column3, column4
35+
}
36+
}
37+
38+
#endif

SwiftMath.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "SwiftMath"
4-
s.version = "3.2.0"
4+
s.version = "3.2.1"
55
s.summary = "Floating point math library written using idiomatic Swift"
66

77
s.description = <<-DESC

0 commit comments

Comments
 (0)