22using UnityEditor ;
33using System . Collections . Generic ;
44using System . Threading . Tasks ;
5+ using System . IO ;
6+ using UnityEngine . Events ;
57
68namespace UnityVolumeRendering
79{
@@ -11,6 +13,8 @@ public class VolumeRenderedObjectCustomInspector : Editor, IProgressView
1113 private bool tfSettings = true ;
1214 private bool lightSettings = true ;
1315 private bool otherSettings = true ;
16+ private bool overlayVolumeSettings = false ;
17+ private bool segmentationSettings = false ;
1418 private float currentProgress = 1.0f ;
1519 private string currentProgressDescrition = "" ;
1620 private bool progressDirty = false ;
@@ -137,6 +141,101 @@ public override void OnInspectorGUI()
137141 }
138142 }
139143
144+ // Overlay volume
145+ overlayVolumeSettings = EditorGUILayout . Foldout ( overlayVolumeSettings , "PET/overlay volume" ) ;
146+ if ( overlayVolumeSettings )
147+ {
148+ OverlayType overlayType = volrendObj . GetOverlayType ( ) ;
149+ TransferFunction secondaryTransferFunction = volrendObj . GetSecondaryTransferFunction ( ) ;
150+ if ( overlayType != OverlayType . Overlay )
151+ {
152+ if ( GUILayout . Button ( "Load PET (NRRD, NIFTI)" ) )
153+ {
154+ ImportImageFileDataset ( volrendObj , ( VolumeDataset dataset ) =>
155+ {
156+ TransferFunction secondaryTransferFunction = ScriptableObject . CreateInstance < TransferFunction > ( ) ;
157+ secondaryTransferFunction . colourControlPoints = new List < TFColourControlPoint > ( ) { new TFColourControlPoint ( 0.0f , Color . red ) , new TFColourControlPoint ( 1.0f , Color . red ) } ;
158+ secondaryTransferFunction . GenerateTexture ( ) ;
159+ volrendObj . SetOverlayDataset ( dataset ) ;
160+ volrendObj . SetSecondaryTransferFunction ( secondaryTransferFunction ) ;
161+ } ) ;
162+ }
163+ if ( GUILayout . Button ( "Load PET (DICOM)" ) )
164+ {
165+ ImportDicomDataset ( volrendObj , ( VolumeDataset dataset ) =>
166+ {
167+ TransferFunction secondaryTransferFunction = ScriptableObject . CreateInstance < TransferFunction > ( ) ;
168+ secondaryTransferFunction . colourControlPoints = new List < TFColourControlPoint > ( ) { new TFColourControlPoint ( 0.0f , Color . red ) , new TFColourControlPoint ( 1.0f , Color . red ) } ;
169+ secondaryTransferFunction . GenerateTexture ( ) ;
170+ volrendObj . SetOverlayDataset ( dataset ) ;
171+ volrendObj . SetSecondaryTransferFunction ( secondaryTransferFunction ) ;
172+ } ) ;
173+ }
174+ }
175+ else
176+ {
177+ if ( GUILayout . Button ( "Edit overlay transfer function" ) )
178+ {
179+ TransferFunctionEditorWindow . ShowWindow ( volrendObj , secondaryTransferFunction ) ;
180+ }
181+
182+ if ( GUILayout . Button ( "Remove secondary volume" ) )
183+ {
184+ volrendObj . SetOverlayDataset ( null ) ;
185+ }
186+ }
187+ }
188+
189+ // Segmentations
190+ segmentationSettings = EditorGUILayout . Foldout ( segmentationSettings , "Segmentations" ) ;
191+ if ( segmentationSettings )
192+ {
193+ List < SegmentationLabel > segmentationLabels = volrendObj . GetSegmentationLabels ( ) ;
194+ if ( segmentationLabels != null && segmentationLabels . Count > 0 )
195+ {
196+ for ( int i = 0 ; i < segmentationLabels . Count ; i ++ )
197+ {
198+ EditorGUILayout . BeginHorizontal ( ) ;
199+ SegmentationLabel segmentationlabel = segmentationLabels [ i ] ;
200+ EditorGUI . BeginChangeCheck ( ) ;
201+ segmentationlabel . name = EditorGUILayout . TextField ( segmentationlabel . name ) ;
202+ segmentationlabel . colour = EditorGUILayout . ColorField ( segmentationlabel . colour ) ;
203+ bool changed = EditorGUI . EndChangeCheck ( ) ;
204+ segmentationLabels [ i ] = segmentationlabel ;
205+ if ( GUILayout . Button ( "delete" ) )
206+ {
207+ volrendObj . RemoveSegmentation ( segmentationlabel . id ) ;
208+ }
209+ EditorGUILayout . EndHorizontal ( ) ;
210+ if ( changed )
211+ {
212+ volrendObj . UpdateSegmentationLabels ( ) ;
213+ }
214+ }
215+
216+ SegmentationRenderMode segmentationRendreMode = ( SegmentationRenderMode ) EditorGUILayout . EnumPopup ( "Render mode" , volrendObj . GetSegmentationRenderMode ( ) ) ;
217+ volrendObj . SetSegmentationRenderMode ( segmentationRendreMode ) ;
218+ }
219+ if ( GUILayout . Button ( "Add segmentation (NRRD, NIFTI)" ) )
220+ {
221+ ImportImageFileDataset ( volrendObj , ( VolumeDataset dataset ) =>
222+ {
223+ volrendObj . AddSegmentation ( dataset ) ;
224+ } ) ;
225+ }
226+ if ( GUILayout . Button ( "Add segmentation (DICOM)" ) )
227+ {
228+ ImportDicomDataset ( volrendObj , ( VolumeDataset dataset ) =>
229+ {
230+ volrendObj . AddSegmentation ( dataset ) ;
231+ } ) ;
232+ }
233+ if ( GUILayout . Button ( "Clear segmentations" ) )
234+ {
235+ volrendObj . ClearSegmentations ( ) ;
236+ }
237+ }
238+
140239 // Other settings
141240 GUILayout . Space ( 10 ) ;
142241 otherSettings = EditorGUILayout . Foldout ( otherSettings , "Other Settings" ) ;
@@ -152,5 +251,54 @@ public override void OnInspectorGUI()
152251 volrendObj . SetSamplingRateMultiplier ( EditorGUILayout . Slider ( "Sampling rate multiplier" , volrendObj . GetSamplingRateMultiplier ( ) , 0.2f , 2.0f ) ) ;
153252 }
154253 }
254+ private static async void ImportImageFileDataset ( VolumeRenderedObject targetObject , UnityAction < VolumeDataset > onLoad )
255+ {
256+ string filePath = EditorUtility . OpenFilePanel ( "Select a folder to load" , "" , "" ) ;
257+ ImageFileFormat imageFileFormat = DatasetFormatUtilities . GetImageFileFormat ( filePath ) ;
258+ if ( ! File . Exists ( filePath ) )
259+ {
260+ Debug . LogError ( $ "File doesn't exist: { filePath } ") ;
261+ return ;
262+ }
263+ if ( imageFileFormat == ImageFileFormat . Unknown )
264+ {
265+ Debug . LogError ( $ "Invalid file format: { Path . GetExtension ( filePath ) } ") ;
266+ return ;
267+ }
268+
269+ using ( ProgressHandler progressHandler = new ProgressHandler ( new EditorProgressView ( ) ) )
270+ {
271+ progressHandler . StartStage ( 1.0f , "Importing dataset" ) ;
272+ IImageFileImporter importer = ImporterFactory . CreateImageFileImporter ( imageFileFormat ) ;
273+ Task < VolumeDataset > importTask = importer . ImportAsync ( filePath ) ;
274+ await importTask ;
275+ progressHandler . EndStage ( ) ;
276+
277+ if ( importTask . Result != null )
278+ {
279+ onLoad . Invoke ( importTask . Result ) ;
280+ }
281+ }
282+ }
283+
284+ private static async void ImportDicomDataset ( VolumeRenderedObject targetObject , UnityAction < VolumeDataset > onLoad )
285+ {
286+ string dir = EditorUtility . OpenFolderPanel ( "Select a folder to load" , "" , "" ) ;
287+ if ( Directory . Exists ( dir ) )
288+ {
289+ using ( ProgressHandler progressHandler = new ProgressHandler ( new EditorProgressView ( ) ) )
290+ {
291+ progressHandler . StartStage ( 1.0f , "Importing dataset" ) ;
292+ Task < VolumeDataset [ ] > importTask = EditorDatasetImportUtils . ImportDicomDirectoryAsync ( dir , progressHandler ) ;
293+ await importTask ;
294+ progressHandler . EndStage ( ) ;
295+
296+ if ( importTask . Result . Length > 0 )
297+ {
298+ onLoad . Invoke ( importTask . Result [ 0 ] ) ;
299+ }
300+ }
301+ }
302+ }
155303 }
156304}
0 commit comments