SimpleITK importers: Respect spacing. Added more comments.

This commit is contained in:
Matias Lavik 2022-04-07 12:05:52 +02:00
Родитель 9fb807b2aa
Коммит 5a0013f078
8 изменённых файлов: 70 добавлений и 7 удалений

Просмотреть файл

@ -9,6 +9,12 @@ using System.IO.Compression;
namespace UnityVolumeRendering
{
/// <summary>
/// Manager for the SimpleITK integration.
/// Since SimpleITK is a native library that requires binaries to be built for your target platform,
/// SimpleITK will be disabled by default and can be enabled through this class.
/// The binaries will be downloaded automatically.
/// </summary>
public class SimpleITKManager
{
private static string SimpleITKDefinition = "UVR_USE_SIMPLEITK";
@ -24,8 +30,14 @@ namespace UnityVolumeRendering
public static void EnableSITK(bool enable)
{
List<BuildTargetGroup> buildTargetGroups = new List<BuildTargetGroup> (){ BuildTargetGroup.Standalone };
if (!HasDownloadedBinaries())
{
EditorUtility.DisplayDialog("Missing SimpleITK binaries", "You need to download the SimpleITK binaries before you can enable SimpleITK.", "Ok");
return;
}
// Enable the UVR_USE_SIMPLEITK preprocessor definition for standalone target
List<BuildTargetGroup> buildTargetGroups = new List<BuildTargetGroup> (){ BuildTargetGroup.Standalone };
foreach (BuildTargetGroup group in buildTargetGroups)
{
List<string> defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';').ToList();

Просмотреть файл

@ -18,7 +18,6 @@ namespace UnityVolumeRendering
public class DatasetImporterUtility
{
public static DatasetType GetDatasetType(string filePath)
{
DatasetType datasetType;

Просмотреть файл

@ -9,6 +9,10 @@ namespace UnityVolumeRendering
NIFTI
}
/// <summary>
/// Interface for single file dataset importers (NRRD, NIFTI, etc.).
/// These datasets contain only one single file.
/// </summary>
public interface IImageFileImporter
{
VolumeDataset Import(String filePath);

Просмотреть файл

@ -8,9 +8,11 @@ using System.IO;
namespace UnityVolumeRendering
{
/// <summary>
/// SimpleITK-based DICOM importer.
/// </summary>
public class SimpleITKImageFileImporter : IImageFileImporter
{
public VolumeDataset Import(string filePath)
{
ImageFileReader reader = new ImageFileReader();
@ -23,7 +25,6 @@ namespace UnityVolumeRendering
image = SimpleITK.Cast(image, PixelIDValueEnum.sitkFloat32);
VectorUInt32 size = image.GetSize();
Debug.Log("Image size: " + size[0] + " " + size[1] + " " + size[2]);
int numPixels = 1;
for (int dim = 0; dim < image.GetDimension(); dim++)
@ -34,6 +35,8 @@ namespace UnityVolumeRendering
IntPtr imgBuffer = image.GetBufferAsFloat();
Marshal.Copy(imgBuffer, pixelData, 0, numPixels);
VectorDouble spacing = image.GetSpacing();
// Create dataset
VolumeDataset volumeDataset = new VolumeDataset();
volumeDataset.data = pixelData;
@ -42,6 +45,11 @@ namespace UnityVolumeRendering
volumeDataset.dimZ = (int)size[2];
volumeDataset.datasetName = "test";
volumeDataset.filePath = filePath;
volumeDataset.scaleX = (float)(spacing[0] * size[0]);
volumeDataset.scaleY = (float)(spacing[1] * size[1]);
volumeDataset.scaleZ = (float)(spacing[2] * size[2]);
volumeDataset.FixDimensions();
return volumeDataset;
}

Просмотреть файл

@ -18,9 +18,27 @@ namespace UnityVolumeRendering
IEnumerable<IImageSequenceFile> GetFiles();
}
/// <summary>
/// Importer for image sequence datasets, such as DICOM and image sequences.
/// These datasets usually contain one file per slice.
/// </summary>
public interface IImageSequenceImporter
{
/// <summary>
/// Read a list of files, and return all image sequence series.
/// Normally a directory will only contain a single series,
/// but if a folder contains multiple series/studies than this function will return all of them.
/// Each series should be imported separately, resulting in one dataset per series. (mostly relevant for DICOM)
/// </summary>
/// <param name="files">Files to load. Typically all the files stored in a specific (DICOM) directory.</param>
/// <returns>List of image sequence series.</returns>
IEnumerable<IImageSequenceSeries> LoadSeries(IEnumerable<string> files);
/// <summary>
/// Import a single image sequence series.
/// </summary>
/// <param name="series">The series to import</param>
/// <returns>Imported 3D volume dataset.</returns>
VolumeDataset ImportSeries(IImageSequenceSeries series);
}
}

Просмотреть файл

@ -14,8 +14,7 @@ namespace UnityVolumeRendering
{
/// <summary>
/// DICOM importer.
/// Reads a 3D DICOM dataset from a folder.
/// The folder needs to contain several .dcm/.dicom files, where each file is a slice of the same dataset.
/// Reads a 3D DICOM dataset from a list of DICOM files.
/// </summary>
public class DICOMImporter : IImageSequenceImporter
{

Просмотреть файл

@ -95,7 +95,6 @@ namespace UnityVolumeRendering
image = SimpleITK.Cast(image, PixelIDValueEnum.sitkFloat32);
VectorUInt32 size = image.GetSize();
Debug.Log("Image size: " + size[0] + " " + size[1] + " " + size[2]);
int numPixels = 1;
for (int dim = 0; dim < image.GetDimension(); dim++)
@ -109,6 +108,8 @@ namespace UnityVolumeRendering
for (int i = 0; i < pixelData.Length; i++)
pixelData[i] = Mathf.Clamp(pixelData[i], -1024, 3071);
VectorDouble spacing = image.GetSpacing();
// Create dataset
VolumeDataset volumeDataset = new VolumeDataset();
volumeDataset.data = pixelData;
@ -117,6 +118,11 @@ namespace UnityVolumeRendering
volumeDataset.dimZ = (int)size[2];
volumeDataset.datasetName = "test";
volumeDataset.filePath = dicomNames[0];
volumeDataset.scaleX = (float)(spacing[0] * size[0]);
volumeDataset.scaleY = (float)(spacing[1] * size[1]);
volumeDataset.scaleZ = (float)(spacing[2] * size[2]);
volumeDataset.FixDimensions();
return volumeDataset;
}

Просмотреть файл

@ -3,8 +3,19 @@ using UnityEngine;
namespace UnityVolumeRendering
{
/// <summary>
/// Factory for creating importers, for each format.
/// Use this if you only want to import a dataset, without deciding which importer to use.
/// Some dataset formats can be imported using several different importers, in which case this factory will return the best alternative.
/// </summary>
public class ImporterFactory
{
/// <summary>
/// Create an importer for an image sequence dataset (multiple files) of the specified format.
/// Use this for DICOM and image sequences.
/// </summary>
/// <param name="format">Format of the dataset.</param>
/// <returns></returns>
public static IImageSequenceImporter CreateImageSequenceImporter(ImageSequenceFormat format)
{
Type importerType = GetImageSequenceImporterType(format);
@ -19,6 +30,12 @@ namespace UnityVolumeRendering
}
}
/// <summary>
/// Create an importer for an image file dataset (single file) of the specified format.
/// Use this for NRRD, NIFTI and VASP/PARCHG.
/// </summary>
/// <param name="format">Format of the dataset.</param>
/// <returns></returns>
public static IImageFileImporter CreateImageFileImporter(ImageFileFormat format)
{
Type importerType = GetImageFileImporterType(format);