diff --git a/Sources/Voxelotl/Math/Extent.swift b/Sources/Voxelotl/Math/Extent.swift index 12c2145..a3feba8 100644 --- a/Sources/Voxelotl/Math/Extent.swift +++ b/Sources/Voxelotl/Math/Extent.swift @@ -1,13 +1,46 @@ -struct Extent: Equatable { - var top: T, bottom: T, left: T, right: T +public struct Extent: Hashable { + public var left: T, top: T, right: T, bottom: T - @inline(__always) static func == (lhs: Self, rhs: Self) -> Bool { + @inline(__always) public var topLeft: Point { .init(left, top) } + @inline(__always) public var topRight: Point { .init(right, top) } + @inline(__always) public var bottomLeft: Point { .init(left, bottom) } + @inline(__always) public var bottomRight: Point { .init(right, bottom) } + + @inline(__always) public static func == (lhs: Self, rhs: Self) -> Bool { lhs.left == rhs.left && lhs.right == rhs.right && lhs.top == rhs.top && lhs.bottom == rhs.bottom } } -extension Extent where T: Comparable { +public extension Extent where T: Comparable { var size: Size { .init( right > left ? right - left : left - right, bottom > top ? bottom - top : top - bottom) } } + +public extension Extent where T: SIMDScalar { + init(from: SIMD2, to: SIMD2) { + self.left = from.x + self.top = from.y + self.right = to.x + self.bottom = to.y + } +} + +public extension Extent where T: AdditiveArithmetic { + init(_ rect: Rect) { + self.left = rect.x + self.top = rect.y + self.right = rect.x + rect.w + self.bottom = rect.y + rect.h + } +} + +public extension Extent where T: Numeric { + @inline(__always) static func * (lhs: Self, rhs: Size) -> Self { + .init( + left: lhs.left * rhs.w, + top: lhs.top * rhs.w, + right: lhs.right * rhs.h, + bottom: lhs.bottom * rhs.h) + } +} diff --git a/Sources/Voxelotl/Math/Point.swift b/Sources/Voxelotl/Math/Point.swift index 3fc44c5..b6690a9 100644 --- a/Sources/Voxelotl/Math/Point.swift +++ b/Sources/Voxelotl/Math/Point.swift @@ -1,18 +1,32 @@ -struct Point: Equatable { - var x: T, y: T +public struct Point: Hashable { + public var x: T, y: T - static var zero: Self { .init(.zero, .zero) } + public static var zero: Self { .init(.zero, .zero) } - init(_ x: T, _ y: T) { + public init(_ x: T, _ y: T) { self.x = x self.y = y } - @inline(__always) static func == (lhs: Self, rhs: Self) -> Bool { lhs.x == rhs.x && lhs.y == rhs.y } - @inline(__always) static func != (lhs: Self, rhs: Self) -> Bool { lhs.x != rhs.x || lhs.y != rhs.y } + public init(scalar value: T) { + self.x = value + self.y = value + } + + @inline(__always) public static func == (lhs: Self, rhs: Self) -> Bool { lhs.x == rhs.x && lhs.y == rhs.y } + @inline(__always) public static func != (lhs: Self, rhs: Self) -> Bool { lhs.x != rhs.x || lhs.y != rhs.y } } -extension Point where T: AdditiveArithmetic { +extension Point where T: BinaryInteger { + init(_ other: Point) { + self.init(T(other.x), T(other.y)) + } + init(_ other: Point) { + self.init(T(other.x), T(other.y)) + } +} + +public extension Point where T: AdditiveArithmetic { @inline(__always) static func + (lhs: Self, rhs: Self) -> Self { Self(lhs.x + rhs.x, lhs.y + rhs.y) } @inline(__always) static func - (lhs: Self, rhs: Self) -> Self { Self(lhs.x - rhs.x, lhs.y - rhs.y) } @@ -20,7 +34,34 @@ extension Point where T: AdditiveArithmetic { @inline(__always) static func -= (lhs: inout Self, rhs: Self) { lhs.x -= rhs.x; lhs.y -= rhs.y } } -extension SIMD2 where Scalar: AdditiveArithmetic { +public extension Point where T: Numeric { + @inline(__always) static func * (lhs: Self, rhs: Self) -> Self { .init(lhs.x * rhs.x, lhs.y * rhs.y) } + @inline(__always) static func * (lhs: Self, rhs: T) -> Self { .init(lhs.x * rhs, lhs.y * rhs) } + + @inline(__always) static func *= (lhs: inout Self, rhs: Self) { lhs.x *= rhs.x; lhs.y *= rhs.y } + @inline(__always) static func *= (lhs: inout Self, rhs: T) { lhs.x *= rhs; lhs.y *= rhs } +} + +extension Point where T: FloatingPoint { + @inline(__always) static func / (lhs: Self, rhs: Self) -> Self { .init(lhs.x / rhs.x, lhs.y / rhs.y) } + @inline(__always) static func / (lhs: Self, rhs: Size) -> Self { .init(lhs.x / rhs.w, lhs.y / rhs.h) } + @inline(__always) static func / (lhs: Self, rhs: T) -> Self { .init(lhs.x / rhs, lhs.y / rhs) } + + @inline(__always) static func /= (lhs: inout Self, rhs: Self) { lhs.x /= rhs.x; lhs.y /= rhs.y } + @inline(__always) static func /= (lhs: inout Self, rhs: Size) { lhs.x /= rhs.w; lhs.y /= rhs.h } + @inline(__always) static func /= (lhs: inout Self, rhs: T) { lhs.x /= rhs; lhs.y /= rhs } +} + +extension Point where T: BinaryFloatingPoint { + init(_ other: Point) { + self.init(T(other.x), T(other.y)) + } + init(_ other: Point) { + self.init(T(other.x), T(other.y)) + } +} + +public extension SIMD2 where Scalar: AdditiveArithmetic { init(_ point: Point) { self.init(point.x, point.y) } diff --git a/Sources/Voxelotl/Math/Rectangle.swift b/Sources/Voxelotl/Math/Rectangle.swift index ba5caa8..51ae232 100644 --- a/Sources/Voxelotl/Math/Rectangle.swift +++ b/Sources/Voxelotl/Math/Rectangle.swift @@ -1,16 +1,16 @@ -public struct Rect: Equatable { - var x: T, y: T, w: T, h: T +public struct Rect: Hashable { + public var x: T, y: T, w: T, h: T - var origin: Point { + public var origin: Point { get { .init(self.x, self.y) } set(point) { self.x = point.x; self.y = point.y } } - var size: Size { + public var size: Size { get { .init(self.w, self.h) } set(size) { self.w = size.w; self.h = size.h } } - static var zero: Self { .init(origin: .zero, size: .zero) } + public static var zero: Self { .init(origin: .zero, size: .zero) } init(x: T, y: T, width: T, height: T) { self.x = x @@ -19,7 +19,7 @@ public struct Rect: Equatable { self.h = height } - init(origin: Point, size: Size) { + public init(origin: Point, size: Size) { self.x = origin.x self.y = origin.y self.w = size.w @@ -37,3 +37,21 @@ public extension Rect where T: AdditiveArithmetic { var up: T { y } var down: T { y + h } } + +public extension Rect where T: BinaryInteger { + init(_ other: Rect) { + self.init(origin: Point(other.origin), size: Size(other.size)) + } + init(_ other: Rect) { + self.init(origin: Point(other.origin), size: Size(other.size)) + } +} + +public extension Rect where T: BinaryFloatingPoint { + init(_ other: Rect) { + self.init(origin: Point(other.origin), size: Size(other.size)) + } + init(_ other: Rect) { + self.init(origin: Point(other.origin), size: Size(other.size)) + } +} diff --git a/Sources/Voxelotl/Math/Size.swift b/Sources/Voxelotl/Math/Size.swift index 1d8d099..f129f7a 100644 --- a/Sources/Voxelotl/Math/Size.swift +++ b/Sources/Voxelotl/Math/Size.swift @@ -1,17 +1,47 @@ -public struct Size: Equatable { - var w: T, h: T +public struct Size: Hashable { + public var w: T, h: T - static var zero: Self { .init(.zero, .zero) } + public static var zero: Self { .init(.zero, .zero) } - init(_ w: T, _ h: T) { + public init(_ w: T, _ h: T) { self.w = w self.h = h } + public init(scalar value: T) { + self.w = value + self.h = value + } + @inline(__always) public static func == (lhs: Self, rhs: Self) -> Bool { lhs.w == rhs.w && lhs.h == rhs.h } @inline(__always) public static func != (lhs: Self, rhs: Self) -> Bool { lhs.w != rhs.w || lhs.h != rhs.h } } +public extension Size where T: AdditiveArithmetic { + @inline(__always) static func + (lhs: Self, rhs: Self) -> Self { .init(lhs.w + rhs.w, lhs.h + rhs.h) } + @inline(__always) static func - (lhs: Self, rhs: Self) -> Self { .init(lhs.w - rhs.w, lhs.h - rhs.h) } + + @inline(__always) static func += (lhs: inout Self, rhs: Self) { lhs.w += rhs.w; lhs.h += rhs.h } + @inline(__always) static func -= (lhs: inout Self, rhs: Self) { lhs.w -= rhs.w; lhs.h -= rhs.h } +} + +public extension Size where T: Numeric { + @inline(__always) static func * (lhs: Self, rhs: Self) -> Self { .init(lhs.w * rhs.w, lhs.h * rhs.h) } + @inline(__always) static func * (lhs: Self, rhs: T) -> Self { .init(lhs.w * rhs, lhs.h * rhs) } + + @inline(__always) static func *= (lhs: inout Self, rhs: Self) { lhs.w *= rhs.w; lhs.h *= rhs.h } + @inline(__always) static func *= (lhs: inout Self, rhs: T) { lhs.w *= rhs; lhs.h *= rhs } +} + +extension Size where T: FloatingPoint { + @inline(__always) static func / (lhs: Self, rhs: Self) -> Self { .init(lhs.w / rhs.w, lhs.h / rhs.h) } + @inline(__always) static func / (lhs: Self, rhs: T) -> Self { .init(lhs.w / rhs, lhs.h / rhs) } + @inline(__always) static func / (lhs: T, rhs: Self) -> Self { .init(lhs / rhs.w, lhs / rhs.h) } + + @inline(__always) static func /= (lhs: inout Self, rhs: Self) { lhs.w /= rhs.w; lhs.h /= rhs.h } + @inline(__always) static func /= (lhs: inout Self, rhs: T) { lhs.w /= rhs; lhs.h /= rhs } +} + extension Size where T: BinaryInteger { static var one: Self { .init(T(1), T(1)) } @@ -30,8 +60,6 @@ extension Size where T: BinaryFloatingPoint { init(_ other: Size) { self.init(T(other.w), T(other.h)) } - - @inline(__always) public static func / (lhs: Self, rhs: Self) -> Self { .init(lhs.w / rhs.w, lhs.h / rhs.h) } } extension SIMD2 where Scalar: AdditiveArithmetic { @@ -39,3 +67,7 @@ extension SIMD2 where Scalar: AdditiveArithmetic { self.init(size.w, size.h) } } + +extension Size where T: SIMDScalar & Numeric { + @inline(__always) public static func * (lhs: Self, rhs: SIMD2) -> Self { .init(lhs.w * rhs.x, lhs.h * rhs.y) } +}