-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanManager.swift
More file actions
283 lines (239 loc) · 9.34 KB
/
Copy pathScanManager.swift
File metadata and controls
283 lines (239 loc) · 9.34 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// ScanManager.swift
// RoomScanner
//
// Manages room scans and data persistence
//
import Foundation
import RoomPlan
import SwiftUI
import Combine
import simd
@MainActor
class ScanManager: ObservableObject {
@Published var scans: [RoomScan] = []
@Published var isScanning = false
private let scansKey = "savedScans"
private var documentsURL: URL {
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
}
init() {
loadScans()
}
// MARK: - Scan Management
func addScan(name: String, capturedRoom: CapturedRoom) async throws {
let fileName = "\(UUID().uuidString)"
let scan = RoomScan(name: name, fileName: fileName)
// Save in multiple formats
try await saveRoomData(capturedRoom: capturedRoom, fileName: fileName)
scans.append(scan)
saveScans()
}
func deleteScan(_ scan: RoomScan) {
// Delete files
let baseURL = documentsURL.appendingPathComponent(scan.fileName)
for ext in ["usd", "usdz", "json"] {
let fileURL = baseURL.appendingPathExtension(ext)
try? FileManager.default.removeItem(at: fileURL)
}
// Remove from list
scans.removeAll { $0.id == scan.id }
saveScans()
}
// MARK: - Export Functions
private func saveRoomData(capturedRoom: CapturedRoom, fileName: String) async throws {
let baseURL = documentsURL.appendingPathComponent(fileName)
// Export USD
let usdURL = baseURL.appendingPathExtension("usd")
try capturedRoom.export(to: usdURL, exportOptions: .model)
// Export USDZ (compressed)
let usdzURL = baseURL.appendingPathExtension("usdz")
try capturedRoom.export(to: usdzURL, exportOptions: .model)
// Export custom JSON format for Python
let jsonURL = baseURL.appendingPathExtension("json")
let jsonData = try exportToJSON(capturedRoom: capturedRoom)
try jsonData.write(to: jsonURL)
}
func exportToJSON(capturedRoom: CapturedRoom) throws -> Data {
var jsonData: [String: Any] = [:]
// Calculate room dimensions
let dims = calculateDimensions(from: capturedRoom.walls)
jsonData["dimensions"] = [
"width": dims.0,
"height": dims.1,
"length": dims.2
]
// Walls
var walls: [[String: Any]] = []
for wall in capturedRoom.walls {
walls.append(createWallDict(from: wall))
}
jsonData["walls"] = walls
// Doors
var doors: [[String: Any]] = []
for door in capturedRoom.doors {
doors.append(createDoorDict(from: door))
}
jsonData["doors"] = doors
// Windows
var windows: [[String: Any]] = []
for window in capturedRoom.windows {
windows.append(createWindowDict(from: window))
}
jsonData["windows"] = windows
// Objects
var objects: [[String: Any]] = []
for object in capturedRoom.objects {
objects.append(createObjectDict(from: object))
}
jsonData["objects"] = objects
return try JSONSerialization.data(withJSONObject: jsonData, options: .prettyPrinted)
}
// MARK: - Helper Functions
private func calculateDimensions(from walls: [CapturedRoom.Surface]) -> (Float, Float, Float) {
guard !walls.isEmpty else { return (0, 0, 0) }
var minX: Float = .infinity
var maxX: Float = -.infinity
var minY: Float = .infinity
var maxY: Float = -.infinity
var minZ: Float = .infinity
var maxZ: Float = -.infinity
for wall in walls {
let x = Float(wall.transform.columns.3.x)
let y = Float(wall.transform.columns.3.y)
let z = Float(wall.transform.columns.3.z)
let w = Float(wall.dimensions.x) / 2
let h = Float(wall.dimensions.y) / 2
let d = Float(wall.dimensions.z) / 2
minX = min(minX, x - w)
maxX = max(maxX, x + w)
minY = min(minY, y - h)
maxY = max(maxY, y + h)
minZ = min(minZ, z - d)
maxZ = max(maxZ, z + d)
}
return (maxX - minX, maxY - minY, maxZ - minZ)
}
private func createWallDict(from wall: CapturedRoom.Surface) -> [String: Any] {
let t = wall.transform
let posX = Float(t.columns.3.x)
let posY = Float(t.columns.3.y)
let posZ = Float(t.columns.3.z)
let row0 = [Float(t.columns.0.x), Float(t.columns.0.y), Float(t.columns.0.z), Float(t.columns.0.w)]
let row1 = [Float(t.columns.1.x), Float(t.columns.1.y), Float(t.columns.1.z), Float(t.columns.1.w)]
let row2 = [Float(t.columns.2.x), Float(t.columns.2.y), Float(t.columns.2.z), Float(t.columns.2.w)]
let row3 = [Float(t.columns.3.x), Float(t.columns.3.y), Float(t.columns.3.z), Float(t.columns.3.w)]
return [
"id": wall.identifier.uuidString,
"dimensions": [
"width": Float(wall.dimensions.x),
"height": Float(wall.dimensions.y),
"thickness": Float(wall.dimensions.z)
],
"transform": [
"position": ["x": posX, "y": posY, "z": posZ],
"matrix": [row0, row1, row2, row3]
]
]
}
private func createDoorDict(from door: CapturedRoom.Surface) -> [String: Any] {
let t = door.transform
// Extract rotation (same method as walls - using right vector in X-Z plane)
let rightX = Float(t.columns.0.x)
let rightZ = Float(t.columns.0.z)
let yawRadians = atan2(rightZ, rightX)
let yawDegrees = yawRadians * 180.0 / .pi
return [
"id": door.identifier.uuidString,
"dimensions": [
"width": Float(door.dimensions.x),
"height": Float(door.dimensions.y),
"depth": Float(door.dimensions.z)
],
"transform": [
"position": [
"x": Float(t.columns.3.x),
"y": Float(t.columns.3.y),
"z": Float(t.columns.3.z)
],
"rotation": [
"yaw_radians": yawRadians,
"yaw_degrees": yawDegrees
]
]
]
}
private func createWindowDict(from window: CapturedRoom.Surface) -> [String: Any] {
let t = window.transform
// Extract rotation (same method as walls - using right vector in X-Z plane)
let rightX = Float(t.columns.0.x)
let rightZ = Float(t.columns.0.z)
let yawRadians = atan2(rightZ, rightX)
let yawDegrees = yawRadians * 180.0 / .pi
return [
"id": window.identifier.uuidString,
"dimensions": [
"width": Float(window.dimensions.x),
"height": Float(window.dimensions.y),
"depth": Float(window.dimensions.z)
],
"transform": [
"position": [
"x": Float(t.columns.3.x),
"y": Float(t.columns.3.y),
"z": Float(t.columns.3.z)
],
"rotation": [
"yaw_radians": yawRadians,
"yaw_degrees": yawDegrees
]
]
]
}
private func createObjectDict(from object: CapturedRoom.Object) -> [String: Any] {
let t = object.transform
// Extract rotation (same method as walls - using right vector in X-Z plane)
let rightX = Float(t.columns.0.x)
let rightZ = Float(t.columns.0.z)
let yawRadians = atan2(rightZ, rightX)
let yawDegrees = yawRadians * 180.0 / .pi
return [
"id": object.identifier.uuidString,
"category": String(describing: object.category),
"dimensions": [
"width": Float(object.dimensions.x),
"height": Float(object.dimensions.y),
"depth": Float(object.dimensions.z)
],
"transform": [
"position": [
"x": Float(t.columns.3.x),
"y": Float(t.columns.3.y),
"z": Float(t.columns.3.z)
],
"rotation": [
"yaw_radians": yawRadians,
"yaw_degrees": yawDegrees
]
],
"confidence": String(describing: object.confidence)
]
}
func getFileURL(for scan: RoomScan, format: String) -> URL {
documentsURL
.appendingPathComponent(scan.fileName)
.appendingPathExtension(format)
}
// MARK: - Persistence
private func saveScans() {
if let encoded = try? JSONEncoder().encode(scans) {
UserDefaults.standard.set(encoded, forKey: scansKey)
}
}
private func loadScans() {
if let data = UserDefaults.standard.data(forKey: scansKey),
let decoded = try? JSONDecoder().decode([RoomScan].self, from: data) {
scans = decoded
}
}
}