diff --git a/Sources/Voxelotl/CMakeLists.txt b/Sources/Voxelotl/CMakeLists.txt index b664b7a..c19f2dd 100644 --- a/Sources/Voxelotl/CMakeLists.txt +++ b/Sources/Voxelotl/CMakeLists.txt @@ -11,7 +11,10 @@ add_executable(Voxelotl MACOSX_BUNDLE Math/FloatExtensions.swift Math/VectorExtensions.swift Math/Matrix4x4.swift + Math/Point.swift + Math/Size.swift Math/Rectangle.swift + Math/Extent.swift Math/AABB.swift # Random number generator subsystem diff --git a/Sources/Voxelotl/Math/Extent.swift b/Sources/Voxelotl/Math/Extent.swift new file mode 100644 index 0000000..12c2145 --- /dev/null +++ b/Sources/Voxelotl/Math/Extent.swift @@ -0,0 +1,13 @@ +struct Extent: Equatable { + var top: T, bottom: T, left: T, right: T + + @inline(__always) 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 { + var size: Size { .init( + right > left ? right - left : left - right, + bottom > top ? bottom - top : top - bottom) } +} diff --git a/Sources/Voxelotl/Math/Point.swift b/Sources/Voxelotl/Math/Point.swift new file mode 100644 index 0000000..3fc44c5 --- /dev/null +++ b/Sources/Voxelotl/Math/Point.swift @@ -0,0 +1,27 @@ +struct Point: Equatable { + var x: T, y: T + + static var zero: Self { .init(.zero, .zero) } + + 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 } +} + +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) } + + @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: Self) { lhs.x -= rhs.x; lhs.y -= rhs.y } +} + +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 19b3acc..ba5caa8 100644 --- a/Sources/Voxelotl/Math/Rectangle.swift +++ b/Sources/Voxelotl/Math/Rectangle.swift @@ -1,73 +1,3 @@ -struct Point: Equatable { - var x: T, y: T - - static var zero: Self { .init(.zero, .zero) } - - 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 } -} - -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) } - - @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: Self) { lhs.x -= rhs.x; lhs.y -= rhs.y } -} - -extension SIMD2 where Scalar: AdditiveArithmetic { - init(_ point: Point) { - self.init(point.x, point.y) - } -} - -public struct Size: Equatable { - var w: T, h: T - - static var zero: Self { .init(.zero, .zero) } - - init(_ w: T, _ h: T) { - self.w = w - self.h = h - } - - @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 } -} - -extension Size where T: BinaryInteger { - static var one: Self { .init(T(1), T(1)) } - - init(_ other: Size) { - self.init(T(other.w), T(other.h)) - } - init(_ other: Size) { - self.init(T(other.w), T(other.h)) - } -} - -extension Size where T: BinaryFloatingPoint { - init(_ other: Size) { - self.init(T(other.w), T(other.h)) - } - 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 { - init(_ size: Size) { - self.init(size.w, size.h) - } -} - public struct Rect: Equatable { var x: T, y: T, w: T, h: T @@ -107,17 +37,3 @@ public extension Rect where T: AdditiveArithmetic { var up: T { y } var down: T { y + h } } - -struct Extent: Equatable { - var top: T, bottom: T, left: T, right: T - - @inline(__always) 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 { - var size: Size { .init( - right > left ? right - left : left - right, - bottom > top ? bottom - top : top - bottom) } -} diff --git a/Sources/Voxelotl/Math/Size.swift b/Sources/Voxelotl/Math/Size.swift new file mode 100644 index 0000000..1d8d099 --- /dev/null +++ b/Sources/Voxelotl/Math/Size.swift @@ -0,0 +1,41 @@ +public struct Size: Equatable { + var w: T, h: T + + static var zero: Self { .init(.zero, .zero) } + + init(_ w: T, _ h: T) { + self.w = w + self.h = h + } + + @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 } +} + +extension Size where T: BinaryInteger { + static var one: Self { .init(T(1), T(1)) } + + init(_ other: Size) { + self.init(T(other.w), T(other.h)) + } + init(_ other: Size) { + self.init(T(other.w), T(other.h)) + } +} + +extension Size where T: BinaryFloatingPoint { + init(_ other: Size) { + self.init(T(other.w), T(other.h)) + } + 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 { + init(_ size: Size) { + self.init(size.w, size.h) + } +}