import Foundation struct RoomLayout: Equatable, Sendable { enum LayoutError: Error, Equatable { case invalidRoom, invalidObject, outsideRoom, tooManyObjects } struct Block: Equatable, Sendable { var x: Double var z: Double var width: Double var depth: Double var height: Double } enum Viewpoint: String, CaseIterable, Identifiable { case entrance = "Entrance" case left = "Left side" case right = "Right side" var id: Self { self } } var width: Double var depth: Double var height: Double var blocks: [Block] func checked() throws -> RoomLayout { guard (2...12).contains(width), (2...12).contains(depth), (2...5).contains(height) else { throw LayoutError.invalidRoom } guard blocks.count <= 24 else { throw LayoutError.tooManyObjects } for b in blocks { guard b.x.isFinite, b.z.isFinite, (0.1...4).contains(b.width), (0.1...4).contains(b.depth), (0.1...3).contains(b.height) else { throw LayoutError.invalidObject } guard abs(b.x) + b.width / 2 <= width / 2, abs(b.z) + b.depth / 2 <= depth / 2, b.height <= height else { throw LayoutError.outsideRoom } } return self } /// Offset the preview rather than moving the headset/user camera. func translation(for viewpoint: Viewpoint) -> SIMD3 { let fraction: Double switch viewpoint { case .entrance: fraction = 0 case .left: fraction = 0.20 case .right: fraction = -0.20 } return SIMD3(Float(fraction * width), 0, Float(-0.8 - depth / 2)) } static let example = RoomLayout(width: 4, depth: 4.6, height: 2.6, blocks: [ Block(x: -1.05, z: 0.15, width: 1.5, depth: 0.8, height: 0.66), Block(x: 0.8, z: -0.45, width: 0.9, depth: 0.52, height: 0.42), Block(x: 1.25, z: 1.3, width: 0.55, depth: 0.65, height: 0.95) ]) }