3
3
using System . Collections . Generic ;
4
4
using System . Threading . Tasks ;
5
5
using System . IO ;
6
+ using UnityEngine . Events ;
6
7
7
8
namespace UnityVolumeRendering
8
9
{
@@ -148,11 +149,25 @@ public override void OnInspectorGUI()
148
149
{
149
150
if ( GUILayout . Button ( "Load PET (NRRD, NIFTI)" ) )
150
151
{
151
- ImportPetScan ( volrendObj ) ;
152
+ ImportImageFileDataset ( volrendObj , ( VolumeDataset dataset ) =>
153
+ {
154
+ TransferFunction secondaryTransferFunction = ScriptableObject . CreateInstance < TransferFunction > ( ) ;
155
+ secondaryTransferFunction . colourControlPoints = new List < TFColourControlPoint > ( ) { new TFColourControlPoint ( 0.0f , Color . red ) , new TFColourControlPoint ( 1.0f , Color . red ) } ;
156
+ secondaryTransferFunction . GenerateTexture ( ) ;
157
+ volrendObj . SetOverlayDataset ( dataset ) ;
158
+ volrendObj . SetSecondaryTransferFunction ( secondaryTransferFunction ) ;
159
+ } ) ;
152
160
}
153
161
if ( GUILayout . Button ( "Load PET (DICOM)" ) )
154
162
{
155
- ImportPetScanDicom ( volrendObj ) ;
163
+ ImportDicomDataset ( volrendObj , ( VolumeDataset dataset ) =>
164
+ {
165
+ TransferFunction secondaryTransferFunction = ScriptableObject . CreateInstance < TransferFunction > ( ) ;
166
+ secondaryTransferFunction . colourControlPoints = new List < TFColourControlPoint > ( ) { new TFColourControlPoint ( 0.0f , Color . red ) , new TFColourControlPoint ( 1.0f , Color . red ) } ;
167
+ secondaryTransferFunction . GenerateTexture ( ) ;
168
+ volrendObj . SetOverlayDataset ( dataset ) ;
169
+ volrendObj . SetSecondaryTransferFunction ( secondaryTransferFunction ) ;
170
+ } ) ;
156
171
}
157
172
}
158
173
else
@@ -198,12 +213,18 @@ public override void OnInspectorGUI()
198
213
}
199
214
if ( GUILayout . Button ( "Add segmentation (NRRD, NIFTI)" ) )
200
215
{
201
- ImportSegmentation ( volrendObj ) ;
216
+ ImportImageFileDataset ( volrendObj , ( VolumeDataset dataset ) =>
217
+ {
218
+ volrendObj . AddSegmentation ( dataset ) ;
219
+ } ) ;
202
220
}
203
- /* if (GUILayout.Button("Add segmentation (DICOM)"))
221
+ if ( GUILayout . Button ( "Add segmentation (DICOM)" ) )
204
222
{
205
- ImportSegmentationDicom(volrendObj);
206
- }*/
223
+ ImportDicomDataset ( volrendObj , ( VolumeDataset dataset ) =>
224
+ {
225
+ volrendObj . AddSegmentation ( dataset ) ;
226
+ } ) ;
227
+ }
207
228
if ( GUILayout . Button ( "Clear segmentations" ) )
208
229
{
209
230
volrendObj . ClearSegmentations ( ) ;
@@ -224,7 +245,7 @@ public override void OnInspectorGUI()
224
245
volrendObj . SetSamplingRateMultiplier ( EditorGUILayout . Slider ( "Sampling rate multiplier" , volrendObj . GetSamplingRateMultiplier ( ) , 0.2f , 2.0f ) ) ;
225
246
}
226
247
}
227
- private static async void ImportPetScan ( VolumeRenderedObject targetObject )
248
+ private static async void ImportImageFileDataset ( VolumeRenderedObject targetObject , UnityAction < VolumeDataset > onLoad )
228
249
{
229
250
string filePath = EditorUtility . OpenFilePanel ( "Select a folder to load" , "" , "" ) ;
230
251
ImageFileFormat imageFileFormat = DatasetFormatUtilities . GetImageFileFormat ( filePath ) ;
@@ -241,67 +262,37 @@ private static async void ImportPetScan(VolumeRenderedObject targetObject)
241
262
242
263
using ( ProgressHandler progressHandler = new ProgressHandler ( new EditorProgressView ( ) ) )
243
264
{
244
- progressHandler . StartStage ( 1.0f , "Importing PET dataset" ) ;
265
+ progressHandler . StartStage ( 1.0f , "Importing dataset" ) ;
245
266
IImageFileImporter importer = ImporterFactory . CreateImageFileImporter ( imageFileFormat ) ;
246
267
Task < VolumeDataset > importTask = importer . ImportAsync ( filePath ) ;
247
268
await importTask ;
248
269
progressHandler . EndStage ( ) ;
249
270
250
- TransferFunction secondaryTransferFunction = ScriptableObject . CreateInstance < TransferFunction > ( ) ;
251
- secondaryTransferFunction . colourControlPoints = new List < TFColourControlPoint > ( ) { new TFColourControlPoint ( 0.0f , Color . red ) , new TFColourControlPoint ( 1.0f , Color . red ) } ;
252
- secondaryTransferFunction . GenerateTexture ( ) ;
253
- targetObject . SetOverlayDataset ( importTask . Result ) ;
254
- targetObject . SetSecondaryTransferFunction ( secondaryTransferFunction ) ;
271
+ if ( importTask . Result != null )
272
+ {
273
+ onLoad . Invoke ( importTask . Result ) ;
274
+ }
255
275
}
256
276
}
257
277
258
- private static async void ImportPetScanDicom ( VolumeRenderedObject targetObject )
278
+ private static async void ImportDicomDataset ( VolumeRenderedObject targetObject , UnityAction < VolumeDataset > onLoad )
259
279
{
260
280
string dir = EditorUtility . OpenFolderPanel ( "Select a folder to load" , "" , "" ) ;
261
281
if ( Directory . Exists ( dir ) )
262
282
{
263
283
using ( ProgressHandler progressHandler = new ProgressHandler ( new EditorProgressView ( ) ) )
264
284
{
265
- progressHandler . StartStage ( 1.0f , "Importing PET dataset" ) ;
285
+ progressHandler . StartStage ( 1.0f , "Importing dataset" ) ;
266
286
Task < VolumeDataset [ ] > importTask = EditorDatasetImportUtils . ImportDicomDirectoryAsync ( dir , progressHandler ) ;
267
287
await importTask ;
268
288
progressHandler . EndStage ( ) ;
269
289
270
- Debug . Assert ( importTask . Result . Length > 0 ) ;
271
- TransferFunction secondaryTransferFunction = ScriptableObject . CreateInstance < TransferFunction > ( ) ;
272
- secondaryTransferFunction . colourControlPoints = new List < TFColourControlPoint > ( ) { new TFColourControlPoint ( 0.0f , Color . red ) , new TFColourControlPoint ( 1.0f , Color . red ) } ;
273
- secondaryTransferFunction . GenerateTexture ( ) ;
274
- targetObject . SetOverlayDataset ( importTask . Result [ 0 ] ) ;
275
- targetObject . SetSecondaryTransferFunction ( secondaryTransferFunction ) ;
290
+ if ( importTask . Result . Length > 0 )
291
+ {
292
+ onLoad . Invoke ( importTask . Result [ 0 ] ) ;
293
+ }
276
294
}
277
295
}
278
296
}
279
-
280
- private static async void ImportSegmentation ( VolumeRenderedObject targetObject )
281
- {
282
- string filePath = EditorUtility . OpenFilePanel ( "Select a folder to load" , "" , "" ) ;
283
- ImageFileFormat imageFileFormat = DatasetFormatUtilities . GetImageFileFormat ( filePath ) ;
284
- if ( ! File . Exists ( filePath ) )
285
- {
286
- Debug . LogError ( $ "File doesn't exist: { filePath } ") ;
287
- return ;
288
- }
289
- if ( imageFileFormat == ImageFileFormat . Unknown )
290
- {
291
- Debug . LogError ( $ "Invalid file format: { Path . GetExtension ( filePath ) } ") ;
292
- return ;
293
- }
294
-
295
- using ( ProgressHandler progressHandler = new ProgressHandler ( new EditorProgressView ( ) ) )
296
- {
297
- progressHandler . StartStage ( 1.0f , "Importing segmentation dataset" ) ;
298
- IImageFileImporter importer = ImporterFactory . CreateImageFileImporter ( imageFileFormat ) ;
299
- Task < VolumeDataset > importTask = importer . ImportAsync ( filePath ) ;
300
- await importTask ;
301
- progressHandler . EndStage ( ) ;
302
-
303
- targetObject . AddSegmentation ( importTask . Result ) ;
304
- }
305
- }
306
297
}
307
298
}
0 commit comments