Skip to content

Commit

Permalink
Matrix3x3+Codable (#6)
Browse files Browse the repository at this point in the history
* add codable conformance to matrix3x3

* add equatable extension

* add float4x4 extension

* fix equatable
  • Loading branch information
eugenebokhan authored Mar 6, 2020
1 parent 5e5001c commit d81238a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
12 changes: 8 additions & 4 deletions Sources/Matrix3x3+simd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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)
Expand All @@ -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 {
lhs.d == rhs.d
}

// MARK: - subscript operations

Expand Down
12 changes: 8 additions & 4 deletions Sources/Matrix4x4+simd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
lhs.d == rhs.d
}
}

public extension matrix_float4x4 {
Expand Down
36 changes: 36 additions & 0 deletions Sources/float3x3+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// float3x3+Extensions.swift
// org.SwiftGFX.SwiftMath
//
// Created by Eugene Bokhan on 28.02.20.
//
//

#if !NOSIMD

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<Float>.self, forKey: .column1)
let c2 = try values.decode(SIMD3<Float>.self, forKey: .column2)
let c3 = try values.decode(SIMD3<Float>.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
}
}

#endif
38 changes: 38 additions & 0 deletions Sources/float4x4+Extensions.swift
Original file line number Diff line number Diff line change
@@ -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<Float>.self, forKey: .column1)
let c2 = try values.decode(SIMD4<Float>.self, forKey: .column2)
let c3 = try values.decode(SIMD4<Float>.self, forKey: .column3)
let c4 = try values.decode(SIMD4<Float>.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
2 changes: 1 addition & 1 deletion SwiftMath.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "SwiftMath"
s.version = "3.2.0"
s.version = "3.2.1"
s.summary = "Floating point math library written using idiomatic Swift"

s.description = <<-DESC
Expand Down

0 comments on commit d81238a

Please sign in to comment.