Integrate DICOM series and Metal volume viewer

This commit is contained in:
SantaSpeen committed 2026-11-22 01:14:03 +03:00
1 parent 48177fc7fb
commit c7f9af8025
7 files changed
+696 -6

No files matched your search

+33
View File
@@ -13,6 +13,7 @@ struct PatientImage: Codable, Identifiable {
let id: UUID
let name: String
let importedAt: Date
var dicomCount: Int? = nil
}
@MainActor
@@ -82,6 +83,38 @@ final class PatientLibrary: ObservableObject {
return image
}
func importDICOM(from sources: [URL], for patientID: UUID) throws -> PatientImage {
guard let position = patients.firstIndex(where: { $0.id == patientID }) else {
throw LibraryError.patientMissing
}
let accessed = sources.filter { $0.startAccessingSecurityScopedResource() }
defer { accessed.forEach { $0.stopAccessingSecurityScopedResource() } }
_ = try DICOMVolume(urls: sources)
try prepareDirectory()
let name = sources.count == 1 ? sources[0].lastPathComponent : "Серия DICOM (\(sources.count) срезов)"
let image = PatientImage(id: UUID(), name: name, importedAt: Date(), dicomCount: sources.count)
let folder = url(for: image)
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: false)
do {
for (source, destination) in zip(sources, dicomURLs(for: image)) {
try FileManager.default.copyItem(at: source, to: destination)
}
var updated = patients
updated[position].images.insert(image, at: 0)
try save(updated)
} catch {
try? FileManager.default.removeItem(at: folder)
throw error
}
return image
}
func dicomURLs(for image: PatientImage) -> [URL] {
(0..<(image.dicomCount ?? 0)).map {
url(for: image).appendingPathComponent("slice-\($0 + 1).dcm")
}
}
func url(for image: PatientImage) -> URL {
directory.appendingPathComponent(image.id.uuidString)
}