Files
CavesOfSwift/Sources/Maths/Vector4.swift

57 lines
1.1 KiB
Swift

#if USE_SIMD
import simd
#endif
#if USE_SIMD
public typealias Vector4 = SIMD4
#else
public struct Vector4<T: FloatingPoint>: Equatable
{
public typealias Scalar = T
public var x, y, z, w: T
public init() { self.x = 0; self.y = 0; self.z = 0; self.w = 0 }
public init(_ x: T, _ y: T, _ z: T, _ w: T) { self.x = x; self.y = y; self.z = z; self.w = w }
}
public extension Vector4
{
// constants
@inline(__always) static var zero: Self { Self(0, 0, 0, 0) }
@inline(__always) static var one: Self { Self(1, 1, 1, 1) }
subscript(index: Int) -> T
{
get
{
switch index
{
case 0: x
case 1: y
case 2: z
case 3: w
default: fatalError()
}
}
}
init(_ xyz: Vector3<T>, _ w: T) { self.x = xyz.x; self.y = xyz.y; self.z = xyz.z; self.w = w }
}
#endif
public extension Vector4 where Scalar: FloatingPoint
{
@inline(__always) static var X: Self { Self(1, 0, 0, 0) }
@inline(__always) static var Y: Self { Self(0, 1, 0, 0) }
@inline(__always) static var Z: Self { Self(0, 0, 1, 0) }
@inline(__always) static var W: Self { Self(0, 0, 0, 1) }
}
public typealias Vec4f = Vector4<Float>
public typealias Vec4d = Vector4<Double>