IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
DICOMLoader.cs
1 using UnityEngine;
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.ComponentModel;
6 using System.Runtime.InteropServices;
7 using itk.simple;
8 
15 public class DICOMLoader : MonoBehaviour {
16 
18  public static DICOMLoader instance { private set; get; }
19 
21  public DICOM2D currentDICOM { private set; get; }
23  public DICOM3D currentDICOMVolume { private set; get; }
25  public DICOMSeries currentDICOMSeries { private set; get; }
26 
28  public bool isBusy { private set; get; }
29 
31  private string directoryToLoad;
32 
34  private DICOMSeries seriesToLoad;
36  private int sliceToLoad;
37 
38  private DICOM newlyLoadedDICOM;
39 
41  private string currentDirectory;
42 
43  public List<DICOMSeries> availableSeries { private set; get; }
44 
46  private bool newDirectoryParsed = false;
48  private bool newDICOMLoaded = false;
49 
50  public DICOMLoader()
51  {
52  if (instance != null)
53  throw( new System.Exception( "Error: Only one Instance of DICOMLoader may exist!" ) );
54 
55  availableSeries = new List<DICOMSeries>();
56 
57  instance = this;
58  isBusy = false;
59  }
60 
61  // ===============================================================
62  // Directory parsing:
63 
66  public bool setDirectory( string path )
67  {
68  if (!isBusy) {
69 
70  // Lock:
71  isBusy = true;
72 
73  directoryToLoad = path;
75  t.Run ();
76 
77  // Let event system know what we're currently doing:
78  PatientEventSystem.triggerEvent (PatientEventSystem.Event.LOADING_AddLoadingJob,
79  "DICOM directory parsing");
80 
81  return true;
82  } else {
83  return false;
84  }
85  }
86 
88  public void parseDirectory(object sender, DoWorkEventArgs e)
89  {
90  // Parse the directory and return the seriesUIDs of all the DICOM series:
91  try {
92  availableSeries.Clear();
93 
94  Debug.Log("[DICOM] Searching directory: " + directoryToLoad );
95  VectorString series = ImageSeriesReader.GetGDCMSeriesIDs (directoryToLoad);
96  if (series.Count > 0) {
97 
98  foreach (string s in series) {
99  DICOMSeries info = new DICOMSeries ( directoryToLoad, s );
100  availableSeries.Add(info);
101  }
102  }
103 
104  // Debug log:
105  string str = "[DICOM] Found " + series.Count + " series.";
106  Debug.Log( str );
107 
108  } catch( System.Exception err ) {
109  Debug.LogError( err.Message );
110  }
111  }
112 
114  public void parseDirectoryCallback(object sender, RunWorkerCompletedEventArgs e)
115  {
116  // Unlock:
117  isBusy = false;
118  newDirectoryParsed = true;
119  }
120 
121  // ===============================================================
122  // DICOM Loading:
123 
127  public bool startLoading (DICOMSeries toLoad, int slice = 0 )
128  {
129  if (!isBusy && toLoad != null) {
130 
131  // Lock:
132  isBusy = true;
133 
134  seriesToLoad = toLoad;
135  sliceToLoad = slice;
137  t.Run ();
138 
139  // Let event system know what we're currently doing:
140  PatientEventSystem.triggerEvent (PatientEventSystem.Event.LOADING_AddLoadingJob,
141  "Loading DICOM series " + toLoad.seriesUID);
142 
143  if( slice >= 0 )
144  PatientEventSystem.triggerEvent (PatientEventSystem.Event.DICOM_StartLoadingSlice);
145  else
146  PatientEventSystem.triggerEvent (PatientEventSystem.Event.DICOM_StartLoadingVolume);
147 
148  return true;
149  } else {
150  return false;
151  }
152  }
156  public bool startLoadingSlice ( int index, int slice = 0 )
157  {
158  if (index >= 0 && index < availableSeries.Count) {
159  return startLoading (availableSeries [index], slice);
160  } else {
161  return false;
162  }
163  }
167  public bool startLoadingSlice ( string seriesUID, int slice = 0 )
168  {
169  foreach (DICOMSeries i in availableSeries) {
170  if (i.seriesUID == seriesUID) {
171  return startLoading (i, slice);
172  }
173  }
174  return false;
175  }
176 
179  public bool startLoadingVolume( DICOMSeries toLoad )
180  {
181  return startLoading (toLoad, -1);
182  }
183 
187  public bool startLoadingVolume ( int index )
188  {
189  if (index >= 0 && index < availableSeries.Count) {
190  return startLoading (availableSeries [index], -1);
191  } else {
192  return false;
193  }
194  }
198  public bool startLoadingVolume ( string seriesUID )
199  {
200  foreach (DICOMSeries i in availableSeries) {
201  if (i.seriesUID == seriesUID) {
202  return startLoading (i, -1);
203  }
204  }
205  return false;
206  }
207 
208 
209 
212  public void load(object sender, DoWorkEventArgs e)
213  {
214  try {
215  Debug.Log("[DICOM] Loading: " + seriesToLoad.seriesUID + " " + sliceToLoad );
216  if( sliceToLoad >= 0 )
217  newlyLoadedDICOM = new DICOM2D( seriesToLoad, sliceToLoad );
218  else
219  newlyLoadedDICOM = new DICOM3D( seriesToLoad );
220  currentDICOMSeries = seriesToLoad;
221  } catch( System.Exception err ) {
222  Debug.LogError( "[DICOM] " + err.Message );
223  newlyLoadedDICOM = null;
224  }
225  }
227  public void loadCallback(object sender, RunWorkerCompletedEventArgs e)
228  {
229  // Unlock:
230  isBusy = false;
231  newDICOMLoaded = true;
232  Debug.Log ("[DICOM] Loading done. " + currentDICOMSeries.seriesUID);
233  }
234 
235 
236  // ===============================================================
237  // Update loop:
238  public void Update()
239  {
240  if (newDirectoryParsed) {
241  PatientEventSystem.triggerEvent(PatientEventSystem.Event.DICOM_NewList);
242  newDirectoryParsed = false;
243 
244  // Let loading screen know what we're currently doing:
245  PatientEventSystem.triggerEvent (PatientEventSystem.Event.LOADING_RemoveLoadingJob,
246  "DICOM directory parsing");
247  }
248  if (newDICOMLoaded) {
249  newDICOMLoaded = false;
250  if (newlyLoadedDICOM != null) {
251  if (newlyLoadedDICOM.dimensions == 2) {
252  currentDICOM = newlyLoadedDICOM as DICOM2D;
253  // Let Listeners know that we've loaded a new DICOM:
254  PatientEventSystem.triggerEvent (PatientEventSystem.Event.DICOM_NewLoadedSlice, currentDICOM);
255  } else {
256  currentDICOMVolume = newlyLoadedDICOM as DICOM3D;
257  // Let Listeners know that we've loaded a new DICOM:
258  PatientEventSystem.triggerEvent (PatientEventSystem.Event.DICOM_NewLoadedVolume, currentDICOMVolume);
259  }
260  }
261  }
262  }
263 
266  public void unloadVolume()
267  {
268  PatientEventSystem.triggerEvent (PatientEventSystem.Event.DICOM_CloseVolume);
269  currentDICOMVolume = null;
270  }
271 
272 }
static DICOMLoader instance
Definition: DICOMLoader.cs:18
bool startLoading(DICOMSeries toLoad, int slice=0)
Definition: DICOMLoader.cs:127
void load(object sender, DoWorkEventArgs e)
Definition: DICOMLoader.cs:212
bool startLoadingSlice(int index, int slice=0)
Definition: DICOMLoader.cs:156
void parseDirectory(object sender, DoWorkEventArgs e)
Definition: DICOMLoader.cs:88
bool startLoadingVolume(DICOMSeries toLoad)
Definition: DICOMLoader.cs:179
bool startLoadingSlice(string seriesUID, int slice=0)
Definition: DICOMLoader.cs:167
bool startLoadingVolume(int index)
Definition: DICOMLoader.cs:187
string seriesUID
Definition: DICOMSeries.cs:27
DICOM2D currentDICOM
Definition: DICOMLoader.cs:21
Definition: DICOM.cs:8
void loadCallback(object sender, RunWorkerCompletedEventArgs e)
Definition: DICOMLoader.cs:227
bool setDirectory(string path)
Definition: DICOMLoader.cs:66
DICOMSeries currentDICOMSeries
Definition: DICOMLoader.cs:25
bool startLoadingVolume(string seriesUID)
Definition: DICOMLoader.cs:198
void unloadVolume()
Definition: DICOMLoader.cs:266
DICOM3D currentDICOMVolume
Definition: DICOMLoader.cs:23
int dimensions
Definition: DICOM.cs:18
void parseDirectoryCallback(object sender, RunWorkerCompletedEventArgs e)
Definition: DICOMLoader.cs:114