Files
ezDent-MacOS/Sources/EzDent/DICOMVolume.swift
T

110 lines
4.6 KiB
Swift

import CoreGraphics
import Foundation
import simd
struct DICOMVolume {
enum Plane: String, CaseIterable, Identifiable {
case slices = "Срезы серии"
case coronal = "Поперёк рядов"
case sagittal = "Поперёк столбцов"
var id: Self { self }
}
let size: SIMD3<Int>
let spacing: SIMD3<Double>?
let pixels: [Float]
let window: SIMD2<Float>?
let inverted: Bool
let minimum: Float
let maximum: Float
init(urls: [URL]) throws {
guard !urls.isEmpty, urls.count <= 4096 else { throw DICOMError.geometry }
let first = try DICOMImage(url: urls[0])
let planeSize = first.width * first.height
guard planeSize <= 64_000_000 / urls.count else { throw DICOMError.unsupported }
size = SIMD3(first.width, first.height, urls.count == 1 ? first.frames : urls.count)
window = first.window
inverted = first.monochrome1
if urls.count == 1 {
spacing = nil
pixels = first.pixels
} else {
guard let series = first.series, !series.isEmpty, let basis = first.geometry,
abs(simd_length(basis.row) - 1) < 0.001,
abs(simd_length(basis.column) - 1) < 0.001,
abs(simd_dot(basis.row, basis.column)) < 0.001 else { throw DICOMError.geometry }
let normal = simd_normalize(simd_cross(basis.row, basis.column))
func position(_ image: DICOMImage) throws -> Double {
guard image.frames == 1, image.series == series, image.width == first.width,
image.height == first.height, image.monochrome1 == first.monochrome1,
let geometry = image.geometry,
simd_distance(geometry.row, basis.row) < 0.001,
simd_distance(geometry.column, basis.column) < 0.001,
simd_distance(geometry.spacing, basis.spacing) < 0.001 else { throw DICOMError.geometry }
let offset = geometry.origin - basis.origin
let depth = simd_dot(offset, normal)
guard depth.isFinite, simd_length(offset - depth * normal) < 0.1 else { throw DICOMError.geometry }
return depth
}
var slices = [(depth: try position(first), pixels: first.pixels)]
for url in urls.dropFirst() {
let image = try DICOMImage(url: url)
slices.append((try position(image), image.pixels))
}
slices.sort { $0.depth < $1.depth }
let step = (slices[slices.count - 1].depth - slices[0].depth) / Double(slices.count - 1)
guard step > 0.001, zip(slices, slices.dropFirst()).allSatisfy({
abs($1.depth - $0.depth - step) <= max(0.01, step * 0.02)
}) else { throw DICOMError.geometry }
spacing = SIMD3(basis.spacing.x, basis.spacing.y, step)
pixels = slices.flatMap(\.pixels)
}
minimum = pixels.min() ?? 0
maximum = pixels.max() ?? 1
}
func count(_ plane: Plane) -> Int {
switch plane {
case .slices: return size.z
case .coronal: return size.y
case .sagittal: return size.x
}
}
func image(_ plane: Plane, index: Int, center: Float, span: Float) -> (CGImage, Double)? {
let values: [Float]
let width: Int
let height: Int
let pitch = spacing ?? SIMD3(repeating: 1)
let aspect: Double
switch plane {
case .slices:
width = size.x
height = size.y
let start = index * width * height
values = Array(pixels[start..<(start + width * height)])
aspect = Double(width) * pitch.x / (Double(height) * pitch.y)
case .coronal:
width = size.x
height = size.z
values = (0..<height).reversed().flatMap { z in
let start = (z * size.y + index) * width
return pixels[start..<(start + width)]
}
aspect = Double(width) * pitch.x / (Double(height) * pitch.z)
case .sagittal:
width = size.y
height = size.z
values = (0..<height).reversed().flatMap { z in
(0..<width).map { y in pixels[(z * size.y + y) * size.x + index] }
}
aspect = Double(width) * pitch.y / (Double(height) * pitch.z)
}
return DICOMImage.grayscale(values, width: width, height: height, center: center,
span: span, invert: inverted).map { ($0, aspect) }
}
}