Skip to content

Commit 1510f7a

Browse files
committed
Add experimental GameMath type
1 parent 9f58616 commit 1510f7a

File tree

2 files changed

+189
-70
lines changed

2 files changed

+189
-70
lines changed

Sources/GameMath/3D Types (New)/Vector3SIMD.swift

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Copyright © 2025 Dustin Collins (Strega's Gate)
3+
* All Rights Reserved.
4+
*
5+
* http://stregasgate.com
6+
*/
7+
8+
public import GameMath
9+
10+
public protocol Vector3n<Scalar> {
11+
typealias ScalarType = Numeric & SIMDScalar
12+
associatedtype Scalar: ScalarType
13+
associatedtype Vector3Counterpart: GameMath.Vector3
14+
15+
var x: Scalar {get set}
16+
var y: Scalar {get set}
17+
var z: Scalar {get set}
18+
init(x: Scalar, y: Scalar, z: Scalar)
19+
}
20+
21+
public typealias Position3i = Position3n<Int32>
22+
public typealias Position3f = Position3n<Float32>
23+
public struct Position3n<Scalar: Vector3n.ScalarType>: Vector3n {
24+
public typealias Vector3Counterpart = Position3
25+
public var x: Scalar
26+
public var y: Scalar
27+
public var z: Scalar
28+
29+
public init(x: Scalar, y: Scalar, z: Scalar) {
30+
self.x = x
31+
self.y = y
32+
self.z = z
33+
}
34+
}
35+
extension Position3n: AdditiveArithmetic where Scalar: AdditiveArithmetic { }
36+
extension Position3n: Equatable where Scalar: Equatable { }
37+
extension Position3n: Hashable where Scalar: Hashable { }
38+
extension Position3n: Comparable where Scalar: Comparable { }
39+
extension Position3n: Sendable where Scalar: Sendable { }
40+
extension Position3n: Codable where Scalar: Codable { }
41+
extension Position3n: BinaryCodable { }
42+
43+
public typealias Size3i = Size3n<Int32>
44+
public typealias Size3f = Size3n<Float32>
45+
public struct Size3n<Scalar: Vector3n.ScalarType>: Vector3n {
46+
public typealias Vector3Counterpart = Size3
47+
public var x: Scalar
48+
public var y: Scalar
49+
public var z: Scalar
50+
51+
public init(x: Scalar, y: Scalar, z: Scalar) {
52+
self.x = x
53+
self.y = y
54+
self.z = z
55+
}
56+
}
57+
extension Size3n: AdditiveArithmetic where Scalar: AdditiveArithmetic { }
58+
extension Size3n: Equatable where Scalar: Equatable { }
59+
extension Size3n: Hashable where Scalar: Hashable { }
60+
extension Size3n: Comparable where Scalar: Comparable { }
61+
extension Size3n: Sendable where Scalar: Sendable { }
62+
extension Size3n: Codable where Scalar: Codable { }
63+
extension Size3n: BinaryCodable { }
64+
public extension Size3n {
65+
@inlinable var width: Scalar { get{self.x} set{self.x = newValue} }
66+
@inlinable var height: Scalar { get{self.y} set{self.y = newValue} }
67+
@inlinable var depth: Scalar { get{self.z} set{self.z = newValue} }
68+
69+
@inlinable init(width: Scalar, height: Scalar, depth: Scalar) {
70+
self.init(x: width, y: height, z: depth)
71+
}
72+
}
73+
74+
extension Vector3n where Scalar: BinaryInteger {
75+
@inlinable
76+
public init(_ vector3: Vector3Counterpart) {
77+
self.init(
78+
x: Scalar(vector3.x),
79+
y: Scalar(vector3.y),
80+
z: Scalar(vector3.z)
81+
)
82+
}
83+
84+
@inlinable
85+
public init(_ vector3: Vector3Counterpart, roundingRule: FloatingPointRoundingRule) where Scalar: BinaryFloatingPoint {
86+
self.init(
87+
x: Scalar(vector3.x.rounded(roundingRule)),
88+
y: Scalar(vector3.y.rounded(roundingRule)),
89+
z: Scalar(vector3.z.rounded(roundingRule))
90+
)
91+
}
92+
@inlinable
93+
public init(_ vector3: Vector3Counterpart, roundingRule: FloatingPointRoundingRule) where Scalar: BinaryInteger {
94+
self.init(
95+
x: Scalar(vector3.x.rounded(roundingRule)),
96+
y: Scalar(vector3.y.rounded(roundingRule)),
97+
z: Scalar(vector3.z.rounded(roundingRule))
98+
)
99+
}
100+
101+
@inlinable
102+
public var vector3: Vector3Counterpart {
103+
return Vector3Counterpart(Float(x), Float(y), Float(z))
104+
}
105+
}
106+
107+
extension Vector3n where Scalar: BinaryFloatingPoint {
108+
@inlinable
109+
public init(_ vector3: Vector3Counterpart) {
110+
self.init(
111+
x: Scalar(vector3.x),
112+
y: Scalar(vector3.y),
113+
z: Scalar(vector3.z)
114+
)
115+
}
116+
117+
@inlinable
118+
public init(_ vector3: Vector3Counterpart, roundingRule: FloatingPointRoundingRule) {
119+
self.init(
120+
x: Scalar(vector3.x.rounded(roundingRule)),
121+
y: Scalar(vector3.y.rounded(roundingRule)),
122+
z: Scalar(vector3.z.rounded(roundingRule))
123+
)
124+
}
125+
126+
@inlinable
127+
public var vector3: Vector3Counterpart {
128+
return Vector3Counterpart(Float(x), Float(y), Float(z))
129+
}
130+
}
131+
132+
public extension Vector3n where Scalar: AdditiveArithmetic {
133+
@inlinable
134+
static func - (lhs: Self, rhs: Self) -> Self {
135+
return Self(x: lhs.x - rhs.x, y: lhs.y - rhs.y, z: lhs.z - rhs.z)
136+
}
137+
138+
@inlinable
139+
static func + (lhs: Self, rhs: Self) -> Self {
140+
return Self(x: lhs.x + rhs.x, y: lhs.y + rhs.y, z: lhs.z + rhs.z)
141+
}
142+
143+
@inlinable
144+
static var zero: Self {Self(x: .zero, y: .zero, z: .zero)}
145+
}
146+
147+
extension Vector3n where Scalar: Comparable {
148+
public static func < (lhs: Self, rhs: Self) -> Bool {
149+
return lhs.x < rhs.x && lhs.y < rhs.y && lhs.z < rhs.z
150+
}
151+
}
152+
153+
extension Vector3n where Scalar: Equatable {
154+
public static func == (lhs: Self, rhs: Self) -> Bool {
155+
return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z
156+
}
157+
}
158+
159+
extension Vector3n where Scalar: Hashable {
160+
public func hash(into hasher: inout Hasher) {
161+
hasher.combine(x)
162+
hasher.combine(y)
163+
hasher.combine(z)
164+
}
165+
}
166+
167+
public extension Vector3n where Scalar: FloatingPoint {
168+
@inlinable
169+
static func / (lhs: Self, rhs: Self) -> Self {
170+
return Self(x: lhs.x / rhs.x, y: lhs.y / rhs.y, z: lhs.z / rhs.z)
171+
}
172+
173+
@inlinable
174+
static func /= (lhs: inout Self, rhs: Self) {
175+
lhs = lhs / rhs
176+
}
177+
}
178+
179+
public extension Vector3n where Scalar: FixedWidthInteger {
180+
@inlinable
181+
static func / (lhs: Self, rhs: Self) -> Self {
182+
return Self(x: lhs.x / rhs.x, y: lhs.y / rhs.y, z: lhs.z / rhs.z)
183+
}
184+
185+
@inlinable
186+
static func /= (lhs: inout Self, rhs: Self) {
187+
lhs = lhs / rhs
188+
}
189+
}

0 commit comments

Comments
 (0)