Groundroom / app / Sources / RoomLayout.swift
| 1 | import Foundation |
| 2 | |
| 3 | struct RoomLayout: Equatable, Sendable { |
| 4 | enum LayoutError: Error, Equatable { case invalidRoom, invalidObject, outsideRoom, tooManyObjects } |
| 5 | struct Block: Equatable, Sendable { |
| 6 | var x: Double |
| 7 | var z: Double |
| 8 | var width: Double |
| 9 | var depth: Double |
| 10 | var height: Double |
| 11 | } |
| 12 | enum Viewpoint: String, CaseIterable, Identifiable { |
| 13 | case entrance = "Entrance" |
| 14 | case left = "Left side" |
| 15 | case right = "Right side" |
| 16 | var id: Self { self } |
| 17 | } |
| 18 | var width: Double |
| 19 | var depth: Double |
| 20 | var height: Double |
| 21 | var blocks: [Block] |
| 22 | |
| 23 | func checked() throws -> RoomLayout { |
| 24 | guard (2...12).contains(width), (2...12).contains(depth), (2...5).contains(height) |
| 25 | else { throw LayoutError.invalidRoom } |
| 26 | guard blocks.count <= 24 else { throw LayoutError.tooManyObjects } |
| 27 | for b in blocks { |
| 28 | guard b.x.isFinite, b.z.isFinite, (0.1...4).contains(b.width), |
| 29 | (0.1...4).contains(b.depth), (0.1...3).contains(b.height) |
| 30 | else { throw LayoutError.invalidObject } |
| 31 | guard abs(b.x) + b.width / 2 <= width / 2, |
| 32 | abs(b.z) + b.depth / 2 <= depth / 2, b.height <= height |
| 33 | else { throw LayoutError.outsideRoom } |
| 34 | } |
| 35 | return self |
| 36 | } |
| 37 | |
| 38 | /// Offset the preview rather than moving the headset/user camera. |
| 39 | func translation(for viewpoint: Viewpoint) -> SIMD3<Float> { |
| 40 | let fraction: Double |
| 41 | switch viewpoint { |
| 42 | case .entrance: fraction = 0 |
| 43 | case .left: fraction = 0.20 |
| 44 | case .right: fraction = -0.20 |
| 45 | } |
| 46 | return SIMD3(Float(fraction * width), 0, Float(-0.8 - depth / 2)) |
| 47 | } |
| 48 | |
| 49 | static let example = RoomLayout(width: 4, depth: 4.6, height: 2.6, blocks: [ |
| 50 | Block(x: -1.05, z: 0.15, width: 1.5, depth: 0.8, height: 0.66), |
| 51 | Block(x: 0.8, z: -0.45, width: 0.9, depth: 0.52, height: 0.42), |
| 52 | Block(x: 1.25, z: 1.3, width: 0.55, depth: 0.65, height: 0.95) |
| 53 | ]) |
| 54 | } |