Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Progress reporting of async texture creation #184

Merged
merged 6 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Assets/Editor/EditorProgressView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public void FinishProgress(ProgressStatus status = ProgressStatus.Succeeded)
EditorApplication.update -= EditorUpdate;
}

public void UpdateProgress(float progress, string description)
public void UpdateProgress(float totalProgress, float currentStageProgress, string description)
{
this.cachedProgress = progress;
this.cachedProgress = totalProgress;
this.cachedDescription = description;
ShowProgress(progress, description);
ShowProgress(totalProgress, description);
}

private void EditorUpdate()
Expand All @@ -42,7 +42,7 @@ private void ShowProgress(float progress, string description)
{
if (progressId != -1)
{
Progress.Report(progressId, progress, description);
Progress.Report(progressId, progress, progress == 1.0f ? "Done" : description);
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions Assets/Editor/RAWDatasetImporterEditorWIndow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ public RAWDatasetImporterEditorWindow(string filePath)

private async Task ImportDatasetAsync()
{
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView()))
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView(), "RAW import"))
{
progressHandler.Start("RAW import", "Importing RAW...");
progressHandler.ReportProgress(0.0f, "Importing RAW dataset");

RawDatasetImporter importer = new RawDatasetImporter(fileToImport, dimX, dimY, dimZ, dataFormat, endianness, bytesToSkip);
Expand All @@ -73,8 +72,6 @@ private async Task ImportDatasetAsync()
Debug.LogError("Failed to import datset");
}

progressHandler.Finish();

this.Close();
}
}
Expand Down
41 changes: 37 additions & 4 deletions Assets/Editor/VolumeRenderedObjectCustomInspector.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace UnityVolumeRendering
{
[CustomEditor(typeof(VolumeRenderedObject))]
public class VolumeRenderedObjectCustomInspector : Editor
public class VolumeRenderedObjectCustomInspector : Editor, IProgressView
{
private bool tfSettings = true;
private bool lightSettings = true;
private bool otherSettings = true;
private float currentProgress = 1.0f;
private string currentProgressDescrition = "";

public void StartProgress(string title, string description)
{
}

public void FinishProgress(ProgressStatus status = ProgressStatus.Succeeded)
{
currentProgress = 1.0f;
}

public void UpdateProgress(float totalProgress, float currentStageProgress, string description)
{
currentProgressDescrition = description;
currentProgress = totalProgress;
}

public override void OnInspectorGUI()
{
VolumeRenderedObject volrendObj = (VolumeRenderedObject)target;

if (currentProgress < 1.0f)
{
Rect rect = EditorGUILayout.BeginVertical();
EditorGUI.ProgressBar(rect, currentProgress, currentProgressDescrition);
GUILayout.Space(18);
EditorGUILayout.EndVertical();
}

// Render mode
RenderMode oldRenderMode = volrendObj.GetRenderMode();
RenderMode newRenderMode = (RenderMode)EditorGUILayout.EnumPopup("Render mode", oldRenderMode);
if (newRenderMode != oldRenderMode)
volrendObj.SetRenderMode(newRenderMode);
{
Task task = volrendObj.SetRenderModeAsync(newRenderMode, new ProgressHandler(this));
}

// Visibility window
Vector2 visibilityWindow = volrendObj.GetVisibilityWindow();
Expand All @@ -33,7 +62,9 @@ public override void OnInspectorGUI()
// Transfer function type
TFRenderMode tfMode = (TFRenderMode)EditorGUILayout.EnumPopup("Transfer function type", volrendObj.GetTransferFunctionMode());
if (tfMode != volrendObj.GetTransferFunctionMode())
volrendObj.SetTransferFunctionMode(tfMode);
{
Task task = volrendObj.SetTransferFunctionModeAsync(tfMode, new ProgressHandler(this));
}

// Show TF button
if (GUILayout.Button("Edit transfer function"))
Expand All @@ -51,7 +82,9 @@ public override void OnInspectorGUI()
if (lightSettings)
{
if (volrendObj.GetRenderMode() == RenderMode.DirectVolumeRendering)
volrendObj.SetLightingEnabled(GUILayout.Toggle(volrendObj.GetLightingEnabled(), "Enable lighting"));
{
Task task = volrendObj.SetLightingEnabledAsync(GUILayout.Toggle(volrendObj.GetLightingEnabled(), "Enable lighting"), new ProgressHandler(this));
}
else
volrendObj.SetLightingEnabled(false);

Expand Down
29 changes: 12 additions & 17 deletions Assets/Editor/VolumeRendererEditorFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ static async void DicomImportAsync()
if (Directory.Exists(dir))
{
Debug.Log("Async dataset load. Hold on.");
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView()))
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView(), "DICOM import"))
{
await DicomImportDirectoryAsync(dir, progressHandler);
progressHandler.Finish();
}
}
else
Expand All @@ -56,7 +55,6 @@ static async void DicomImportAsync()
static async Task DicomImportDirectoryAsync(string dir, ProgressHandler progressHandler)
{
Debug.Log("Async dataset load. Hold on.");
progressHandler.Start("DICOM import", "Importing DICOM...");

bool recursive = true;

Expand All @@ -75,22 +73,23 @@ static async Task DicomImportDirectoryAsync(string dir, ProgressHandler progress

if (fileCandidates.Any())
{
progressHandler.StartStage(0.6f, "Loading DICOM series");
progressHandler.StartStage(0.2f, "Loading DICOM series");

IImageSequenceImporter importer = ImporterFactory.CreateImageSequenceImporter(ImageSequenceFormat.DICOM);
IEnumerable<IImageSequenceSeries> seriesList = await importer.LoadSeriesAsync(fileCandidates, new ImageSequenceImportSettings { progressHandler = progressHandler });
float numVolumesCreated = 0;

progressHandler.EndStage();
progressHandler.StartStage(0.4f);
progressHandler.StartStage(0.8f);

int seriesIndex = 0, numSeries = seriesList.Count();
foreach (IImageSequenceSeries series in seriesList)
{
progressHandler.ReportProgress(seriesIndex, numSeries, $"Importing series {seriesIndex} of {numSeries}");
progressHandler.StartStage(0.8f);
progressHandler.StartStage((seriesIndex + 1) / numSeries, $"Importing series {seriesIndex} of {numSeries}");
progressHandler.StartStage(0.7f, $"Importing series {seriesIndex} of {numSeries}");
VolumeDataset dataset = await importer.ImportSeriesAsync(series, new ImageSequenceImportSettings { progressHandler = progressHandler });
progressHandler.EndStage();
progressHandler.StartStage(0.3f, "Creating object");
if (dataset != null)
{
if (EditorPrefs.GetBool("DownscaleDatasetPrompt"))
Expand All @@ -103,10 +102,12 @@ static async Task DicomImportDirectoryAsync(string dir, ProgressHandler progress
}
}

VolumeRenderedObject obj = await VolumeObjectFactory.CreateObjectAsync(dataset);
VolumeRenderedObject obj = await VolumeObjectFactory.CreateObjectAsync(dataset, progressHandler);
obj.transform.position = new Vector3(numVolumesCreated, 0, 0);
numVolumesCreated++;
}
progressHandler.EndStage();
progressHandler.EndStage();
seriesIndex++;
}

Expand Down Expand Up @@ -138,9 +139,8 @@ static async void ImportNRRDDatasetAsync()
if (File.Exists(file))
{
Debug.Log("Async dataset load. Hold on.");
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView()))
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView(), "NRRD import"))
{
progressHandler.Start("NRRD import", "Importing NRRD...");
progressHandler.ReportProgress(0.0f, "Importing NRRD dataset");

IImageFileImporter importer = ImporterFactory.CreateImageFileImporter(ImageFileFormat.NRRD);
Expand All @@ -155,7 +155,6 @@ static async void ImportNRRDDatasetAsync()
{
Debug.LogError("Failed to import datset");
}
progressHandler.Finish();
}
}
else
Expand All @@ -176,9 +175,8 @@ static async void ImportNIFTIDatasetAsync()
if (File.Exists(file))
{
Debug.Log("Async dataset load. Hold on.");
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView()))
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView(), "NIFTI import"))
{
progressHandler.Start("NIfTI import", "Importing NIfTI...");
progressHandler.ReportProgress(0.0f, "Importing NIfTI dataset");

IImageFileImporter importer = ImporterFactory.CreateImageFileImporter(ImageFileFormat.NIFTI);
Expand All @@ -194,7 +192,6 @@ static async void ImportNIFTIDatasetAsync()
{
Debug.LogError("Failed to import datset");
}
progressHandler.Finish();
}
}
else
Expand All @@ -215,9 +212,8 @@ static async void ImportParDatasetAsync()
if (File.Exists(file))
{
Debug.Log("Async dataset load. Hold on.");
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView()))
using (ProgressHandler progressHandler = new ProgressHandler(new EditorProgressView(), "AVSP import"))
{
progressHandler.Start("VASP import", "Importing VASP...");
progressHandler.ReportProgress(0.0f, "Importing VASP dataset");

IImageFileImporter importer = ImporterFactory.CreateImageFileImporter(ImageFileFormat.VASP);
Expand All @@ -233,7 +229,6 @@ static async void ImportParDatasetAsync()
{
Debug.LogError("Failed to import datset");
}
progressHandler.Finish();
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public async Task<VolumeDataset> ImportAsync(string filePath)

return volumeDataset;
}
private void ImportInternal(VolumeDataset volumeDataset, float[] pixelData,VectorUInt32 size,VectorDouble spacing,string filePath)

private void ImportInternal(VolumeDataset volumeDataset, float[] pixelData, VectorUInt32 size, VectorDouble spacing,string filePath)
{
ImageFileReader reader = new ImageFileReader();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ private void CalculateSliceLocations(List<DICOMSliceFile> slices)
Vector3 yBase = new Vector3(cosines[3], cosines[4], cosines[5]);
Vector3 normal = Vector3.Cross(xBase, yBase);

for(int i = 1; i < slices.Count; i++)
for(int i = 0; i < slices.Count; i++)
{
Vector3 position = slices[i].position;
// Project p onto n. d = dot(p,n) / |n| = dot(p,n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ public async Task<VolumeDataset> ImportSeriesAsync(IImageSequenceSeries series,
if (sequenceSeries.files.Count == 0)
{
Debug.LogError("Empty series. No files to load.");
settings.progressHandler.Fail();
return null;
}

await Task.Run(() => ImportSeriesInternal(dicomNames,sequenceSeries,image,size,pixelData,volumeDataset));
await Task.Run(() => ImportSeriesInternal(dicomNames, sequenceSeries, image, size, pixelData, volumeDataset));

return volumeDataset;
}
Expand Down Expand Up @@ -169,7 +170,7 @@ private void ImportSeriesInternal(VectorString dicomNames, ImageSequenceSeries s
volumeDataset.dimX = (int)size[0];
volumeDataset.dimY = (int)size[1];
volumeDataset.dimZ = (int)size[2];
volumeDataset.datasetName = "test";
volumeDataset.datasetName = System.IO.Path.GetFileName(dicomNames[0]);
volumeDataset.filePath = dicomNames[0];
volumeDataset.scale = new Vector3(
(float)(spacing[0] * size[0]) / 1000.0f, // mm to m
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Progress/IProgressHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public interface IProgressHandler
void EndStage();
void ReportProgress(float progress, string description = "");
void ReportProgress(int currentStep, int totalSteps, string description = "");
void Fail();
}
}
6 changes: 6 additions & 0 deletions Assets/Scripts/Progress/NullProgressHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace UnityVolumeRendering
{
public class NullProgressHandler : IProgressHandler
{
public static readonly IProgressHandler instance = new NullProgressHandler();

public void StartStage(float weight, string description = "")
{
}
Expand All @@ -17,5 +19,9 @@ public void ReportProgress(float progress, string description = "")
public void ReportProgress(int currentStep, int totalSteps, string description = "")
{
}

public void Fail()
{
}
}
}
Loading