IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
PatientDirectoryLoader.cs
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Text;
6 using LitJson;
7 
8 public class PatientDirectoryLoader {
9 
10  private static string currentPath = "";
11  private static List<PatientMeta> mPatientEntries = new List<PatientMeta>();
12  private static bool currentlyLoading = false;
13 
14  //Variable used for thread
15  private static PatientMeta entry;
16  private static bool loadingLock = false;
17 
18 
19  static PatientDirectoryLoader()
20  {
21  PatientEventSystem.startListening(PatientEventSystem.Event.PATIENT_Loaded, loadPatientFinish);
22  }
23 
25  {
26  PatientEventSystem.stopListening(PatientEventSystem.Event.PATIENT_Loaded, loadPatientFinish);
27  }
28 
30  public static void setPath(string newPath)
31  {
32  //Debug.Log( "Current working directory:\n" + Directory.GetCurrentDirectory() );
33 
34  if (!Directory.Exists(newPath))
35  throw (new System.Exception("Invalid path given: " + newPath));
36 
37  // While parsing a directory, don't start parsing another one:
38  if (!currentlyLoading) {
39  currentlyLoading = true;
40 
41  // remove previously found patient entries:
42  mPatientEntries.Clear ();
43 
44  currentPath = newPath;
45 
46  Debug.Log ("Looking for Patients in:\n" + currentPath);
47 
48  string[] folders = Directory.GetDirectories (currentPath);
49 
50  foreach (string folder in folders) {
51  // Attempt to load the directorie's contents as a patient:
52  PatientMeta newPatient = PatientMeta.createFromFolder (folder);
53  if (newPatient != null) {
54  mPatientEntries.Add (newPatient);
55 
56  // Let listeners know there's a new patient entry by firing an event:
57  PatientEventSystem.triggerEvent (
58  PatientEventSystem.Event.PATIENT_NewPatientDirectoryFound
59  );
60  }
61  }
62 
63  // Done parsing, unlock:
64  currentlyLoading = false;
65  }
66  }
67 
68  public static int getCount()
69  {
70  return mPatientEntries.Count;
71  }
72 
73  public static PatientMeta getEntry( int index )
74  {
75  if (index >= 0 && index < mPatientEntries.Count)
76  return mPatientEntries[index];
77 
78  throw (new System.Exception("Could not find entry with index " + index.ToString()));
79  }
80 
81  public static void loadPatient( int index )
82  {
83  if (loadingLock)
84  {
85  Debug.LogWarning("[PatientDirectoryLoader.cs] Loading aborted. Still loading other patient");
86  return;
87  }
88 
89  loadingLock = true;
90  if (index >= 0 && index < mPatientEntries.Count)
91  {
92  entry = mPatientEntries[index];
93 
94  PatientEventSystem.triggerEvent (PatientEventSystem.Event.PATIENT_StartLoading, entry);
95 
96  new Patient (entry);
97  }
98  else
99  {
100  throw (new System.Exception("Could not find patient with index " + index.ToString()));
101  }
102  }
103 
104  /*
105  * loadPatient() starts the loading of a patient. The event Event.PATIENT_Loaded calls this methode and the loading will be finished
106  */
107  private static void loadPatientFinish(object obj = null)
108  {
109  // Start parsing the DICOM directory:
110  //PatientDICOMLoader mPatientDICOMLoader = GameObject.Find("GlobalScript").GetComponent<PatientDICOMLoader>();
111  //mPatientDICOMLoader.loadDirectory(entry.dicomPath);
112 
113  DICOMLoader.instance.setDirectory (entry.dicomPath);
114 
115  // Load model in the directory:
116  MeshLoader mModelLoader = GameObject.Find("GlobalScript").GetComponent<MeshLoader>();
117  mModelLoader.LoadFile(entry.meshPath);
118  loadingLock = false;
119  }
120 
121  private static PatientDirectoryLoader mInstance;
122 
123 }
static void setPath(string newPath)