Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mlavik1 committed Aug 28, 2024
1 parent 30dc9f7 commit 5d80d8f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 58 deletions.
87 changes: 39 additions & 48 deletions Assets/Editor/VolumeRenderedObjectCustomInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using UnityEngine.Events;

namespace UnityVolumeRendering
{
Expand Down Expand Up @@ -148,11 +149,25 @@ public override void OnInspectorGUI()
{
if (GUILayout.Button("Load PET (NRRD, NIFTI)"))
{
ImportPetScan(volrendObj);
ImportImageFileDataset(volrendObj, (VolumeDataset dataset) =>
{
TransferFunction secondaryTransferFunction = ScriptableObject.CreateInstance<TransferFunction>();
secondaryTransferFunction.colourControlPoints = new List<TFColourControlPoint>() { new TFColourControlPoint(0.0f, Color.red), new TFColourControlPoint(1.0f, Color.red) };
secondaryTransferFunction.GenerateTexture();
volrendObj.SetOverlayDataset(dataset);
volrendObj.SetSecondaryTransferFunction(secondaryTransferFunction);
});
}
if (GUILayout.Button("Load PET (DICOM)"))
{
ImportPetScanDicom(volrendObj);
ImportDicomDataset(volrendObj, (VolumeDataset dataset) =>
{
TransferFunction secondaryTransferFunction = ScriptableObject.CreateInstance<TransferFunction>();
secondaryTransferFunction.colourControlPoints = new List<TFColourControlPoint>() { new TFColourControlPoint(0.0f, Color.red), new TFColourControlPoint(1.0f, Color.red) };
secondaryTransferFunction.GenerateTexture();
volrendObj.SetOverlayDataset(dataset);
volrendObj.SetSecondaryTransferFunction(secondaryTransferFunction);
});
}
}
else
Expand Down Expand Up @@ -198,12 +213,18 @@ public override void OnInspectorGUI()
}
if (GUILayout.Button("Add segmentation (NRRD, NIFTI)"))
{
ImportSegmentation(volrendObj);
ImportImageFileDataset(volrendObj, (VolumeDataset dataset) =>
{
volrendObj.AddSegmentation(dataset);
});
}
/*if (GUILayout.Button("Add segmentation (DICOM)"))
if (GUILayout.Button("Add segmentation (DICOM)"))
{
ImportSegmentationDicom(volrendObj);
}*/
ImportDicomDataset(volrendObj, (VolumeDataset dataset) =>
{
volrendObj.AddSegmentation(dataset);
});
}
if (GUILayout.Button("Clear segmentations"))
{
volrendObj.ClearSegmentations();
Expand All @@ -224,7 +245,7 @@ public override void OnInspectorGUI()
volrendObj.SetSamplingRateMultiplier(EditorGUILayout.Slider("Sampling rate multiplier", volrendObj.GetSamplingRateMultiplier(), 0.2f, 2.0f));
}
}
private static async void ImportPetScan(VolumeRenderedObject targetObject)
private static async void ImportImageFileDataset(VolumeRenderedObject targetObject, UnityAction<VolumeDataset> onLoad)
{
string filePath = EditorUtility.OpenFilePanel("Select a folder to load", "", "");
ImageFileFormat imageFileFormat = DatasetFormatUtilities.GetImageFileFormat(filePath);
Expand All @@ -241,67 +262,37 @@ private static async void ImportPetScan(VolumeRenderedObject targetObject)

using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView()))
{
progressHandler.StartStage(1.0f, "Importing PET dataset");
progressHandler.StartStage(1.0f, "Importing dataset");
IImageFileImporter importer = ImporterFactory.CreateImageFileImporter(imageFileFormat);
Task<VolumeDataset> importTask = importer.ImportAsync(filePath);
await importTask;
progressHandler.EndStage();

TransferFunction secondaryTransferFunction = ScriptableObject.CreateInstance<TransferFunction>();
secondaryTransferFunction.colourControlPoints = new List<TFColourControlPoint>() { new TFColourControlPoint(0.0f, Color.red), new TFColourControlPoint(1.0f, Color.red) };
secondaryTransferFunction.GenerateTexture();
targetObject.SetOverlayDataset(importTask.Result);
targetObject.SetSecondaryTransferFunction(secondaryTransferFunction);
if (importTask.Result != null)
{
onLoad.Invoke(importTask.Result);
}
}
}

private static async void ImportPetScanDicom(VolumeRenderedObject targetObject)
private static async void ImportDicomDataset(VolumeRenderedObject targetObject, UnityAction<VolumeDataset> onLoad)
{
string dir = EditorUtility.OpenFolderPanel("Select a folder to load", "", "");
if (Directory.Exists(dir))
{
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView()))
{
progressHandler.StartStage(1.0f, "Importing PET dataset");
progressHandler.StartStage(1.0f, "Importing dataset");
Task<VolumeDataset[]> importTask = EditorDatasetImportUtils.ImportDicomDirectoryAsync(dir, progressHandler);
await importTask;
progressHandler.EndStage();

Debug.Assert(importTask.Result.Length > 0);
TransferFunction secondaryTransferFunction = ScriptableObject.CreateInstance<TransferFunction>();
secondaryTransferFunction.colourControlPoints = new List<TFColourControlPoint>() { new TFColourControlPoint(0.0f, Color.red), new TFColourControlPoint(1.0f, Color.red) };
secondaryTransferFunction.GenerateTexture();
targetObject.SetOverlayDataset(importTask.Result[0]);
targetObject.SetSecondaryTransferFunction(secondaryTransferFunction);
if (importTask.Result.Length > 0)
{
onLoad.Invoke(importTask.Result[0]);
}
}
}
}

private static async void ImportSegmentation(VolumeRenderedObject targetObject)
{
string filePath = EditorUtility.OpenFilePanel("Select a folder to load", "", "");
ImageFileFormat imageFileFormat = DatasetFormatUtilities.GetImageFileFormat(filePath);
if (!File.Exists(filePath))
{
Debug.LogError($"File doesn't exist: {filePath}");
return;
}
if (imageFileFormat == ImageFileFormat.Unknown)
{
Debug.LogError($"Invalid file format: {Path.GetExtension(filePath)}");
return;
}

using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView()))
{
progressHandler.StartStage(1.0f, "Importing segmentation dataset");
IImageFileImporter importer = ImporterFactory.CreateImageFileImporter(imageFileFormat);
Task<VolumeDataset> importTask = importer.ImportAsync(filePath);
await importTask;
progressHandler.EndStage();

targetObject.AddSegmentation(importTask.Result);
}
}
}
}
36 changes: 26 additions & 10 deletions Assets/Shaders/DirectVolumeRenderingShader.shader
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#pragma multi_compile __ USE_MAIN_LIGHT
#pragma multi_compile __ CUBIC_INTERPOLATION_ON
#pragma multi_compile __ SECONDARY_VOLUME_ON
#pragma multi_compile MULTIVOLUME_NONE MULTIVOLUME_OVERLAY MULTIVOLUME_ISOLATE
#pragma vertex vert
#pragma fragment frag

Expand Down Expand Up @@ -215,11 +216,7 @@
// Gets the density of the secondary volume at the specified position
float getSecondaryDensity(float3 pos)
{
#if CUBIC_INTERPOLATION_ON
return interpolateTricubicFast(_SecondaryDataTex, float3(pos.x, pos.y, pos.z), _TextureSize);
#else
return tex3Dlod(_SecondaryDataTex, float4(pos.x, pos.y, pos.z, 0.0f));
#endif
}

// Gets the density at the specified position, without tricubic interpolation
Expand Down Expand Up @@ -344,12 +341,14 @@
continue;
#endif

#if SECONDARY_VOLUME_ON
const float density2 = getSecondaryDensity(currPos);
float4 src2 = getSecondaryTF1DColour(density2);
//src.rgb = src.rgb * (1.0 - src2.a) + src2.rgb * src2.a;
src = src2.a > 0.0 ? src2 : src;
//src.a = src2.a > 0.0 ? src.a : 0.0;
#if defined(MULTIVOLUME_OVERLAY) || defined(MULTIVOLUME_ISOLATE)
const float secondaryDensity = getSecondaryDensity(currPos);
float4 secondaryColour = getSecondaryTF1DColour(secondaryDensity);
#if MULTIVOLUME_OVERLAY
src = secondaryColour.a > 0.0 ? secondaryColour : src;
#elif MULTIVOLUME_ISOLATE
src.a = secondaryColour.a > 0.0 ? src.a : 0.0;
#endif
#endif

// Calculate gradient (needed for lighting and 2D transfer functions)
Expand Down Expand Up @@ -469,6 +468,23 @@
#endif

const float density = getDensity(currPos);
#if MULTIVOLUME_ISOLATE
const float secondaryDensity = getSecondaryDensity(currPos);
if (secondaryDensity <= 0.0)
continue;
#elif MULTIVOLUME_OVERLAY
const float secondaryDensity = getSecondaryDensity(currPos);
if (secondaryDensity > 0.0)
{
col = getSecondaryTF1DColour(secondaryDensity);
float3 gradient = getGradient(currPos);
float gradMag = length(gradient);
float3 normal = gradient / gradMag;
col.rgb = calculateLighting(col.rgb, normal, getLightDirection(-ray.direction), -ray.direction, 0.15);
col.a = 1.0;
break;
}
#endif
if (density > _MinVal && density < _MaxVal)
{
float3 gradient = getGradient(currPos);
Expand Down

0 comments on commit 5d80d8f

Please sign in to comment.