Skip to content

Commit de5ea21

Browse files
committed
Simplified pi constant precision for better performance and readability
1 parent f08b8da commit de5ea21

File tree

1,823 files changed

+2218
-1771
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,823 files changed

+2218
-1771
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//
2+
// SimdExtensions.swift
3+
// Math
4+
//
5+
// SIMD matrix utilities for graphics rendering
6+
//
7+
8+
#if canImport(simd)
9+
import simd
10+
11+
// MARK: - Matrix Creation from Math
12+
13+
extension Math {
14+
/// Creates a perspective projection matrix
15+
/// - Parameters:
16+
/// - fov: Field of view in radians
17+
/// - aspect: Aspect ratio (width/height)
18+
/// - near: Near clipping plane
19+
/// - far: Far clipping plane
20+
public static func makePerspectiveMatrix(fov: Math, aspect: Math, near: Math, far: Math) -> simd_float4x4 {
21+
guard let fovF = fov.asDouble,
22+
let aspectF = aspect.asDouble,
23+
let nearF = near.asDouble,
24+
let farF = far.asDouble else {
25+
return matrix_identity_float4x4
26+
}
27+
28+
let fovRad = Float(fovF)
29+
let halfFov = fovRad * 0.5
30+
let tanHalfFov = tanf(halfFov)
31+
32+
let y = 1.0 / tanHalfFov
33+
let x = y / Float(aspectF)
34+
let z = Float(farF) / (Float(nearF) - Float(farF))
35+
36+
return simd_float4x4(
37+
simd_float4(x, 0, 0, 0),
38+
simd_float4(0, y, 0, 0),
39+
simd_float4(0, 0, z, -1),
40+
simd_float4(0, 0, z * Float(nearF), 0)
41+
)
42+
}
43+
44+
/// Creates a look-at view matrix
45+
/// - Parameters:
46+
/// - eye: Camera position
47+
/// - target: Look-at target position
48+
/// - up: Up vector
49+
public static func makeLookAtMatrix(eye: (Math, Math, Math),
50+
target: (Math, Math, Math),
51+
up: (Math, Math, Math)) -> simd_float4x4 {
52+
guard let eyeX = eye.0.asDouble, let eyeY = eye.1.asDouble, let eyeZ = eye.2.asDouble,
53+
let targetX = target.0.asDouble, let targetY = target.1.asDouble, let targetZ = target.2.asDouble,
54+
let upX = up.0.asDouble, let upY = up.1.asDouble, let upZ = up.2.asDouble else {
55+
return matrix_identity_float4x4
56+
}
57+
58+
let eyeVec = simd_float3(Float(eyeX), Float(eyeY), Float(eyeZ))
59+
let targetVec = simd_float3(Float(targetX), Float(targetY), Float(targetZ))
60+
let upVec = simd_float3(Float(upX), Float(upY), Float(upZ))
61+
62+
let z = simd_normalize(eyeVec - targetVec)
63+
let x = simd_normalize(simd_cross(upVec, z))
64+
let y = simd_cross(z, x)
65+
66+
return simd_float4x4(
67+
simd_float4(x.x, y.x, z.x, 0),
68+
simd_float4(x.y, y.y, z.y, 0),
69+
simd_float4(x.z, y.z, z.z, 0),
70+
simd_float4(-simd_dot(x, eyeVec), -simd_dot(y, eyeVec), -simd_dot(z, eyeVec), 1)
71+
)
72+
}
73+
74+
/// Creates a translation matrix
75+
public static func makeTranslationMatrix(_ x: Math, _ y: Math, _ z: Math) -> simd_float4x4 {
76+
guard let xF = x.asDouble, let yF = y.asDouble, let zF = z.asDouble else {
77+
return matrix_identity_float4x4
78+
}
79+
80+
var matrix = matrix_identity_float4x4
81+
matrix.columns.3 = simd_float4(Float(xF), Float(yF), Float(zF), 1)
82+
return matrix
83+
}
84+
85+
/// Creates a rotation matrix around X axis (pitch)
86+
public static func makeRotationX(_ angle: Math) -> simd_float4x4 {
87+
guard let angleF = angle.asDouble else {
88+
return matrix_identity_float4x4
89+
}
90+
91+
let a = Float(angleF)
92+
let c = cosf(a)
93+
let s = sinf(a)
94+
95+
return simd_float4x4(
96+
simd_float4(1, 0, 0, 0),
97+
simd_float4(0, c, s, 0),
98+
simd_float4(0, -s, c, 0),
99+
simd_float4(0, 0, 0, 1)
100+
)
101+
}
102+
103+
/// Creates a rotation matrix around Y axis (yaw)
104+
public static func makeRotationY(_ angle: Math) -> simd_float4x4 {
105+
guard let angleF = angle.asDouble else {
106+
return matrix_identity_float4x4
107+
}
108+
109+
let a = Float(angleF)
110+
let c = cosf(a)
111+
let s = sinf(a)
112+
113+
return simd_float4x4(
114+
simd_float4(c, 0, s, 0),
115+
simd_float4(0, 1, 0, 0),
116+
simd_float4(-s, 0, c, 0),
117+
simd_float4(0, 0, 0, 1)
118+
)
119+
}
120+
121+
/// Creates a rotation matrix around Z axis (roll)
122+
public static func makeRotationZ(_ angle: Math) -> simd_float4x4 {
123+
guard let angleF = angle.asDouble else {
124+
return matrix_identity_float4x4
125+
}
126+
127+
let a = Float(angleF)
128+
let c = cosf(a)
129+
let s = sinf(a)
130+
131+
return simd_float4x4(
132+
simd_float4(c, s, 0, 0),
133+
simd_float4(-s, c, 0, 0),
134+
simd_float4(0, 0, 1, 0),
135+
simd_float4(0, 0, 0, 1)
136+
)
137+
}
138+
139+
/// Creates a scale matrix
140+
public static func makeScaleMatrix(_ x: Math, _ y: Math, _ z: Math) -> simd_float4x4 {
141+
guard let xF = x.asDouble, let yF = y.asDouble, let zF = z.asDouble else {
142+
return matrix_identity_float4x4
143+
}
144+
145+
return simd_float4x4(
146+
simd_float4(Float(xF), 0, 0, 0),
147+
simd_float4(0, Float(yF), 0, 0),
148+
simd_float4(0, 0, Float(zF), 0),
149+
simd_float4(0, 0, 0, 1)
150+
)
151+
}
152+
}
153+
154+
// MARK: - Math Array Conversion
155+
156+
extension Array where Element == Math {
157+
/// Converts an array of Math values to Float array for GPU
158+
public var asFloatArray: [Float] {
159+
compactMap { $0.asDouble }.map { Float($0) }
160+
}
161+
}
162+
163+
#endif

0 commit comments

Comments
 (0)