#if USE_SIMD
import simd
#endif


#if USE_SIMD
public typealias Vector3 = SIMD3
public extension Vector3
{
	typealias T = Scalar
}
#else

public struct Vector3<T: FloatingPoint>: Equatable
{
	public typealias Scalar = T

	public var x, y, z: T

	public init() { self.x = 0; self.y = 0; self.z = 0 }
	public init(_ x: T, _ y: T, _ z: T) { self.x = x; self.y = y; self.z = z }
}

public extension Vector3
{
	// constants
	@inline(__always) static var zero: Self { Self(0, 0, 0) }
	@inline(__always) static var one: Self { Self(1, 1, 1) }

	subscript(index: Int) -> T
	{
		get
		{
			switch index
			{
			case 0: x
			case 1: y
			case 2: z
			default: fatalError()
			}
		}
	}

	init(repeating v: T) { self.x = v; self.y = v; self.z = v }

	// relational
	@inline(__always) static func == (lhs: Self, rhs: Self) -> Bool { lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z }
	@inline(__always) static func != (lhs: Self, rhs: Self) -> Bool { lhs.x != rhs.x || lhs.y != rhs.y || lhs.z != rhs.z }

	// arithmetic
	@inline(__always) static func + (lhs: Self, rhs: Self) -> Self { Self(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z) }
	@inline(__always) static func - (lhs: Self, rhs: Self) -> Self { Self(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z) }
	@inline(__always) static func * (lhs: Self, rhs: Self) -> Self { Self(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z) }
	@inline(__always) static func / (lhs: Self, rhs: Self) -> Self { Self(lhs.x / rhs.x, lhs.y / rhs.y, lhs.z / rhs.z) }

	// scalar arithmetic
	@inline(__always) static func * (lhs: Self, rhs: T) -> Self { Self(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs) }
	@inline(__always) static func / (lhs: Self, rhs: T) -> Self { Self(lhs.x / rhs, lhs.y / rhs, lhs.z / rhs) }
	@inline(__always) static func * (lhs: T, rhs: Self) -> Self { Self(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z) }
	@inline(__always) static func / (lhs: T, rhs: Self) -> Self { Self(lhs / rhs.x, lhs / rhs.y, lhs / rhs.z) }

	// compound arithmetic
	@inline(__always) static func += (lhs: inout Self, rhs: Self) { lhs.x += rhs.x; lhs.y += rhs.y; lhs.z += rhs.z }
	@inline(__always) static func -= (lhs: inout Self, rhs: Self) { lhs.x -= rhs.x; lhs.y -= rhs.y; lhs.z -= rhs.z }
	@inline(__always) static func *= (lhs: inout Self, rhs: Self) { lhs.x *= rhs.x; lhs.y *= rhs.y; lhs.z *= rhs.z }
	@inline(__always) static func /= (lhs: inout Self, rhs: Self) { lhs.x /= rhs.x; lhs.y /= rhs.y; lhs.z /= rhs.z }

	// compound scalar arithmetic
	@inline(__always) static func *= (lhs: inout Self, rhs: T) { lhs.x *= rhs; lhs.y *= rhs; lhs.z *= rhs }
	@inline(__always) static func /= (lhs: inout Self, rhs: T) { lhs.x /= rhs; lhs.y /= rhs; lhs.z /= rhs }

	// unary
	@inline(__always) static prefix func + (vec: Self) -> Self { vec }
	@inline(__always) static prefix func - (vec: Self) -> Self { Self(-vec.x, -vec.y, -vec.z) }
}

#endif

public extension Vector3 where Scalar: FloatingPoint
{
	@inline(__always) static var X: Self { Self(1, 0, 0) }
	@inline(__always) static var Y: Self { Self(0, 1, 0) }
	@inline(__always) static var Z: Self { Self(0, 0, 1) }

	@inline(__always) static var up: Self      {  Y }
	@inline(__always) static var down: Self    { -Y }
	@inline(__always) static var left: Self    { -X }
	@inline(__always) static var right: Self   {  X }
	@inline(__always) static var forward: Self { -Z }
	@inline(__always) static var back: Self    {  Z }

	@inline(__always) var len2: Scalar { x * x + y * y + z * z }
	@inline(__always) var len: Scalar { len2.squareRoot() }
	@inline(__always) var normalised: Self { let invLen = 1 / len; return self * invLen }

	@inline(__always) mutating func normalise() { let invLen = 1 / len; self *= invLen }

	@inline(__always) func lerp(_ b: Self, _ x: Scalar) -> Self
	{
		let invX = 1 - x
		return Self(self.x * invX + b.x * x, self.y * invX + b.y * x, self.z * invX + b.z * x)
	}

	@inline(__always) func dot(_ b: Self) -> Scalar { x * b.x + y * b.y + z * b.z }
	@inline(__always) func cross(_ b: Self) -> Self { Self(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x) }

	@inline(__always) func project(_ n: Self) -> Self { n * self.dot(n) }
}


public typealias Vec3f = Vector3<Float>
public typealias Vec3d = Vector3<Double>
