]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomNode.cpp
04879c8477346f0e74c1bbeedf6cd91361e05ff6
[creaImageIO.git] / src / creaImageIODicomNode.cpp
1 #include <creaMessageManager.h>
2 #include <creaImageIODicomNode.h>
3
4 #include <creaImageIODicomDatabase.h>
5 //#include <creaImageIOException.h>
6
7 #include <algorithm>
8
9 namespace creaImageIO
10 {
11   //=============================================================
12   const int DicomNode::Database = 0;
13   const int DicomNode::Patient = 1;
14   const int DicomNode::Study   = 2;
15   const int DicomNode::Series  = 3;
16   const int DicomNode::Image   = 4;
17   //=============================================================
18
19   const std::string DicomNodeTypeName[5] =
20     { "Database",
21       "Patient",
22       "Study",
23       "Series",
24       "Image"
25     };
26   const std::string DicomNodePluralTypeName[5] =
27     { "Databases",
28       "Patients",
29       "Studies",
30       "Series",
31       "Images"
32     };
33
34   //=============================================================
35   DicomNode::DicomNode(Type type, DicomDatabase* root, DicomNode* parent)
36     : mType(type),
37       mDicomDatabase(root),
38       mParent(parent),
39       mData(0),
40       mChildrenLoaded(false),
41       mNumberOfChildren(-1)
42   {
43     if (parent) 
44       {
45         parent->GetChildrenList().push_back(this);
46       }
47     if (root) 
48       {
49         DicomNodeTypeDescription::FieldDescriptionMapType::const_iterator i;
50         for (i  = GetTypeDescription().GetFieldDescriptionMap().begin();
51              i != GetTypeDescription().GetFieldDescriptionMap().end();
52              i++)
53           {
54             mFieldValueMap[i->first] = "----";
55           }
56       }
57   }
58   //=============================================================
59
60   //=============================================================
61   DicomNode::~DicomNode()
62   {
63     ChildrenListType::iterator i;
64     for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
65       {
66         delete *i;
67       }
68     if (mData) 
69       {
70         delete mData;
71         mData = 0;
72       }
73   }
74   //=============================================================
75
76   //=============================================================
77   const std::string& DicomNode::GetTypeName(const Type& t)
78   {
79     return DicomNodeTypeName[t];
80   }
81   //=============================================================
82   //=============================================================
83   const std::string& DicomNode::GetPluralTypeName(const Type& t)
84   {
85     return DicomNodePluralTypeName[t];
86   }
87   //=============================================================
88
89   //=============================================================
90   const std::string& DicomNode::GetTypeName() const
91   {
92     return DicomNodeTypeName[GetType()];
93   }
94   //=============================================================
95
96   //=============================================================
97   void DicomNode::RemoveChildrenFromList(DicomNode* node)
98   {
99     ChildrenListType::iterator i = find(GetChildrenList().begin(),
100                                         GetChildrenList().end(),
101                                         node);
102     if (i != GetChildrenList().end())
103       {
104         GetChildrenList().erase(i);
105       }
106   }
107   //=============================================================
108
109   //=============================================================
110   const DicomNodeTypeDescription& DicomNode::GetTypeDescription() const
111   {
112     return mDicomDatabase->GetDicomNodeTypeDescription(mType);
113   }
114   //=============================================================
115
116
117   //  Tree* DicomNode::GetTree() const { return mDicomDatabase->GetTree(); }
118   
119
120   //=============================================================
121   const Field::Value& DicomNode::GetFieldValue(const Field::Key& k) const
122   {
123     //    std::cout << "this = "<<(void*)this<<std::endl;
124     //    std::cout << "mFieldValueMap="<<(void*)(&mFieldValueMap)<<std::endl;
125     FieldValueMapType::const_iterator i = mFieldValueMap.find(k);
126     if (i==mFieldValueMap.end())
127       {
128         static Field::Value def("");
129         return def;
130         //      CREAIMAGEIO_ERROR("DicomNode::GetFieldValue : no field with key '"<<k<<"'");
131       }
132     return i->second;
133   }
134   //=============================================================
135
136   //=============================================================
137   const Field::Value& DicomNode::GetCleanFieldValue(const Field::Key& k) const
138   {
139     static std::string Clean("-");
140     FieldValueMapType::const_iterator i = mFieldValueMap.find(k);
141     if (i==mFieldValueMap.end())
142       {
143         creaError("DicomNode::GetFieldValue : no field with key '"<<k<<"'");
144       }
145     if (i->second == "GDCM::Unfound")
146       {
147         return Clean;
148       }
149     return i->second;
150   }
151   //=============================================================
152
153
154   //=============================================================
155   void DicomNode::SetFieldValue(const Field::Key& k, const Field::Value& v)
156   {
157     FieldValueMapType::iterator i = mFieldValueMap.find(k);
158     if (i==mFieldValueMap.end())
159       {
160         std::cout << "DicomNode::SetFieldValue : no field with key '"<<k
161                   <<"'"<<std::endl;
162         creaError("DicomNode::SetFieldValue : no field with key '"<<k<<"'");
163       }
164     i->second = v;
165   }
166   //=============================================================
167
168   //=============================================================
169   int DicomNode::ImageGetRows() const
170   {
171     return atoi(UnsafeGetFieldValue("A0028_0010").c_str());
172   }
173   //=============================================================
174
175   //=============================================================
176   int DicomNode::ImageGetColumns() const
177   {
178     return atoi(UnsafeGetFieldValue("A0028_0011").c_str());
179   }
180   //=============================================================
181
182   //=============================================================
183   int DicomNode::ImageGetFrames() const
184   {
185     return atoi(UnsafeGetFieldValue("A0028_0008").c_str());
186   }
187   //=============================================================
188
189   //=============================================================
190   void DicomNode::Print() const 
191   {
192     for (int ii=0;ii<GetType()+1;++ii) std::cout << " ";
193     std::cout << "-> "<<GetLabel() << std::endl;
194     ChildrenListType::const_iterator i;
195     for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
196       {
197         (*i)->Print();
198       }
199  
200   }
201   //=============================================================
202
203   //=============================================================
204   std::string DicomNode::GetLabel() const 
205   {
206     std::string l;
207     switch (GetType())
208       {
209       case Database :
210         l += GetFieldValue("Name");
211         break;
212         //        + " (" + GetFieldValue("Location") + ")";
213      case Patient :
214         l += GetCleanFieldValue("A0010_0010")  // Name 
215           + " | " + GetCleanFieldValue("A0010_0040") + " | "  // Sex
216           + GetCleanFieldValue("A0010_0030"); // Birthday
217         break;
218      case Study :
219        l += GetCleanFieldValue("A0008_0020")  // Study Date
220          + " | " +  GetCleanFieldValue("A0008_1030"); // Study Description
221        break;
222       case Series :
223         l += GetCleanFieldValue("A0008_0060")  // Modality
224           + " | " +  GetCleanFieldValue("A0008_103E");  // Description
225         break;
226       case Image :
227         l += GetCleanFieldValue("A0004_1500"); // File Name
228         break;
229       }
230     return l;
231   }
232   //=============================================================
233
234   //=============================================================
235   int DicomNode::GetNumberOfChildren()
236   {
237     //    std::cout << "$$$ DicomNode::GetNumberOfChildren"<<std::endl;
238     if ( GetType() == DicomNode::Image ) return 0;
239     if ( GetChildrenList().size() > 0 ) return GetChildrenList().size();
240     if ( mNumberOfChildren == -1 ) 
241       {
242         mNumberOfChildren = 0;
243         if (GetDicomDatabase()!=0)
244           {
245             mNumberOfChildren = 
246               GetDicomDatabase()->DBQueryNumberOfChildren(this);
247           }
248       }
249     return mNumberOfChildren;
250   }
251   //=============================================================
252
253   /*
254   const Field::Description& DicomNode::GetFieldDescription(const Field::Key& k) 
255     const
256   {
257     
258     return GetTypeDescription().GetFieldDescription(k);
259     
260   }
261   */
262
263
264
265  //=====================================================================
266   void DicomNode::SortChildren(const LexicographicalDicomNodeComparator& c)
267   {
268     // Check 
269     /*
270     ChildrenListType::const_iterator i;
271     for (i=GetChildrenList().begin(); i!=GetChildrenList().end(); i++)
272       {
273         std::cout << (*i)->GetLabel()<<std::endl;
274         if ((*i)==0) 
275           {
276             std::cout << "!!! SortChildren : child = "<<*i<<std::endl;
277             exit(0);
278           }
279       }
280     std::cout<<"=========================="<<std::endl;
281     */
282     std::sort(GetChildrenList().begin(),
283               GetChildrenList().end(),
284               c);
285   }
286  //=====================================================================
287
288
289
290
291
292 } // namespace creaImageIO