-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathConfiguration.swift
More file actions
171 lines (147 loc) · 5.17 KB
/
Copy pathConfiguration.swift
File metadata and controls
171 lines (147 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright © 2026 Apple Inc.
import Foundation
import MLX
public struct Configuration {
public var width: Int = 1280
public var height: Int = 1024
public var initialTemperature: MLXArray
public struct Wall: Sendable {
var x: Int
var y: Int
var width: Int
var height: Int
var minX: Int { x }
var maxX: Int { x + width }
var minY: Int { y }
var maxY: Int { y + height }
}
public var walls: [Wall]
public struct HeatSource: Sendable {
var x: Int
var y: Int
var radius: Int
var temperature: Float
}
public var heatSources: [HeatSource]
public init(
width: Int = 1280, height: Int = 1024, initialTemperature: MLXArray? = nil,
walls: [Wall] = [], heatSources: [HeatSource] = []
) {
self.width = width
self.height = height
self.initialTemperature = initialTemperature ?? zeros([height, width], dtype: .float16)
self.walls = walls
self.heatSources = heatSources
}
public mutating func addRandomWall() {
enum Kind: CaseIterable {
case horizontal
case vertical
case horizontalSplit
case verticalSplit
}
let s = min(width, height)
let length = Int.random(in: (s / 6) ... (3 * s / 4))
let thickness = Int.random(in: 4 ... 10)
let border = thickness * 3
switch Kind.allCases.randomElement()! {
case .horizontal:
walls.append(
.init(
x: Int.random(in: border ... (width - border - length)),
y: Int.random(in: border ... (height - border)),
width: length,
height: thickness)
)
case .vertical:
walls.append(
.init(
x: Int.random(in: border ... (width - border)),
y: Int.random(in: border ... (height - border - length)),
width: thickness,
height: length)
)
case .horizontalSplit:
let f = Float.random(in: 0.25 ... 0.75)
let wall = Wall(
x: Int.random(in: border ... (width - border - length)),
y: Int.random(in: border ... (height - border)),
width: Int(Float(length) * (f - 0.05)),
height: thickness)
let wall2 = Wall(
x: wall.x + wall.width + length / 10,
y: wall.y,
width: Int(Float(length) * (1 - f - 0.05)),
height: thickness)
walls.append(wall)
walls.append(wall2)
case .verticalSplit:
let f = Float.random(in: 0.25 ... 0.75)
let wall = Wall(
x: Int.random(in: border ... (width - border)),
y: Int.random(in: border ... (height - border - length)),
width: thickness,
height: Int(Float(length) * (f - 0.05)))
let wall2 = Wall(
x: wall.x,
y: wall.y + wall.height + length / 10,
width: thickness,
height: Int(Float(length) * (1 - f - 0.05)))
walls.append(wall)
walls.append(wall2)
}
}
public mutating func addRandomHeatSource() {
heatSources.append(
.init(
x: Int.random(in: width / 10 ... 9 * width / 10),
y: Int.random(in: height / 10 ... 9 * height / 10),
radius: Int.random(in: 20 ... 40), temperature: Float.random(in: 0.8 ... 1.1))
)
}
public func asRoom() -> Room {
let rows = arange(0, height)[.ellipsis, .newAxis]
let cols = arange(0, width)[.newAxis, .ellipsis]
var wallMask =
((rows .== 0) .|| (rows .== (height - 1)) .|| (cols .== 0) .|| (cols .== (width - 1)))
for wall in walls {
wallMask = wallMask
.|| ((rows .>= wall.minY) .&& (rows .< wall.maxY) .&& (cols .>= wall.minX)
.&& (cols .< wall.maxX))
}
var heatSources = zeros([height, width])
for heatSource in self.heatSources {
let dx = cols - heatSource.x
let dy = rows - heatSource.y
let mask = (dx * dx + dy * dy) .< (heatSource.radius * heatSource.radius)
heatSources = which(mask, heatSource.temperature, heatSources)
}
let staticMask = wallMask .|| (heatSources .> 0)
let redMask = ((rows + cols) % 2) .== 0
let blackMask = ((rows + cols) % 2) .== 1
return Room(
temperature: initialTemperature,
heatSources: heatSources,
staticMask: staticMask,
wallMask: wallMask,
redMask: redMask, blackMask: blackMask)
}
}
public struct Room {
// [H, W]
var temperature: MLXArray
var heatSources: MLXArray
var staticMask: MLXArray
// [H, W] -- for display
var wallMask: MLXArray
// [H, W] -- for SOR
var redMask: MLXArray
var blackMask: MLXArray
}
let lut: [UInt32] = [
0xFF00_0000,
0xFF80_0000,
0xFFFF_0000,
0xFFFF_FF00,
0xFFFF_FFFF,
]