]> Creatis software - clitk.git/blob - common/clitkDicomRT_StructureSet.cxx
bc9992683cc31804324184223f412a3b50c9867c
[clitk.git] / common / clitkDicomRT_StructureSet.cxx
1 /*=========================================================================
2   Program:         vv http://www.creatis.insa-lyon.fr/rio/vv
3   Main authors :   XX XX XX
4
5   Authors belongs to:
6   - University of LYON           http://www.universite-lyon.fr/
7   - Léon Bérard cancer center    http://oncora1.lyon.fnclcc.fr
8   - CREATIS CNRS laboratory      http://www.creatis.insa-lyon.fr
9
10   This software is distributed WITHOUT ANY WARRANTY; without even
11   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12   PURPOSE.  See the copyright notices for more information.
13
14   It is distributed under dual licence
15   - BSD       http://www.opensource.org/licenses/bsd-license.php
16   - CeCILL-B  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17
18   =========================================================================*/
19
20 #include "clitkDicomRT_StructureSet.h"
21 #include <vtksys/SystemTools.hxx>
22 #include "gdcmFile.h"
23 #if GDCM_MAJOR_VERSION == 2
24 #include "gdcmReader.h"
25 #include "gdcmAttribute.h"
26 #endif
27
28 //--------------------------------------------------------------------
29 clitk::DicomRT_StructureSet::DicomRT_StructureSet()
30 {
31   mStudyID = "NoStudyID";
32   mStudyTime = "NoStudyTime";
33   mStudyDate = "NoStudyDate";
34   mLabel = "NoLabel";
35   mName = "NoName";
36   mDate = "NoDate";
37   mTime = "NoTime";
38 }
39 //--------------------------------------------------------------------
40
41
42 //--------------------------------------------------------------------
43 clitk::DicomRT_StructureSet::~DicomRT_StructureSet()
44 {
45 }
46 //--------------------------------------------------------------------
47
48
49 //--------------------------------------------------------------------
50 const std::string & clitk::DicomRT_StructureSet::GetStudyID() const
51 {
52   return mStudyID;
53 }
54 //--------------------------------------------------------------------
55
56
57 //--------------------------------------------------------------------
58 const std::string & clitk::DicomRT_StructureSet::GetStudyTime() const
59 {
60   return mStudyTime;
61 }
62 //--------------------------------------------------------------------
63
64
65 //--------------------------------------------------------------------
66 const std::string & clitk::DicomRT_StructureSet::GetStudyDate() const
67 {
68   return mStudyDate;
69 }
70 //--------------------------------------------------------------------
71
72
73 //--------------------------------------------------------------------
74 const std::string & clitk::DicomRT_StructureSet::GetLabel() const
75 {
76   return mLabel;
77 }
78 //--------------------------------------------------------------------
79
80
81 //--------------------------------------------------------------------
82 const std::string & clitk::DicomRT_StructureSet::GetName() const
83 {
84   return mName;
85 }
86 //--------------------------------------------------------------------
87
88
89 //--------------------------------------------------------------------
90 const std::string & clitk::DicomRT_StructureSet::GetDate() const
91 {
92   return mDate;
93 }
94 //--------------------------------------------------------------------
95
96
97 //--------------------------------------------------------------------
98 const std::string & clitk::DicomRT_StructureSet::GetTime() const
99 {
100   return mTime;
101 }
102 //--------------------------------------------------------------------
103
104
105 //--------------------------------------------------------------------
106 const std::vector<clitk::DicomRT_ROI::Pointer> & clitk::DicomRT_StructureSet::GetListOfROI() const
107 {
108   return mListOfROI;
109 }
110 //--------------------------------------------------------------------
111
112
113 //--------------------------------------------------------------------
114 clitk::DicomRT_ROI* clitk::DicomRT_StructureSet::GetROI(int n)
115 {
116   if (mMapOfROIIndex.find(n) == mMapOfROIIndex.end()) {
117     std::cerr << "No ROI number " << n << std::endl;
118     return NULL;
119   }
120   //  DD(mListOfROI[mMapOfROIIndex[n]]->GetName());
121   //DD(mListOfROI[mMapOfROIIndex[n]]->GetROINumber());
122   return mListOfROI[mMapOfROIIndex[n]];
123 }
124 //--------------------------------------------------------------------
125
126
127 //--------------------------------------------------------------------
128 void clitk::DicomRT_StructureSet::Print(std::ostream & os) const
129 {
130   os << "Study ID      = " << mStudyID << std::endl
131      << "Study Date    = " << mStudyDate << std::endl
132      << "Study Time    = " << mStudyTime << std::endl
133      << "Struct Label  = " << mLabel << std::endl
134      << "Struct Name   = " << mName << std::endl
135      << "Struct Time   = " << mTime << std::endl
136      << "Number of ROI = " << mListOfROI.size() << std::endl;
137   for(unsigned int i=0; i<mListOfROI.size(); i++) {
138     mListOfROI[i]->Print(os);
139   }
140 }
141 //--------------------------------------------------------------------
142
143
144 //--------------------------------------------------------------------
145 void clitk::DicomRT_StructureSet::Read(const std::string & filename)
146 {
147   // Open DICOM
148 #if GDCM_MAJOR_VERSION == 2
149   gdcm::Reader reader;
150   reader.SetFileName(filename.c_str());
151   reader.Read();
152
153   const gdcm::File & file = reader.GetFile();
154   const gdcm::DataSet & ds = file.GetDataSet();
155
156   // Check file type
157   //Verify if the file is a RT-Structure-Set dicom file
158   gdcm::MediaStorage ms;
159   ms.SetFromFile(file);
160   if( ms != gdcm::MediaStorage::RTStructureSetStorage )
161     {
162     std::cerr << "Error. the file " << filename
163               << " is not a Dicom Struct ? (must have a SOP Class UID [0008|0016] = 1.2.840.10008.5.1.4.1.1.481.3 ==> [RT Structure Set Storage])"
164               << std::endl;
165     exit(0);
166     }
167
168   gdcm::Attribute<0x8,0x60> modality;
169   modality.SetFromDataSet( ds );
170   if( modality.GetValue() != "RTSTRUCT" )
171     {
172     std::cerr << "Error. the file " << filename
173               << " is not a Dicom Struct ? (must have 0x0008,0x0060 = RTSTRUCT [RT Structure Set Storage])"
174               << std::endl;
175     exit(0);
176     }
177
178   // Read global info
179   gdcm::Attribute<0x20,0x10> studyid;
180   studyid.SetFromDataSet( ds );
181   gdcm::Attribute<0x8,0x20> studytime;
182   studytime.SetFromDataSet( ds );
183   gdcm::Attribute<0x8,0x30> studydate;
184   studydate.SetFromDataSet( ds );
185   gdcm::Attribute<0x3006,0x02> label;
186   label.SetFromDataSet( ds );
187   gdcm::Attribute<0x3006,0x04> atname;
188   atname.SetFromDataSet( ds );
189   gdcm::Attribute<0x3006,0x09> time;
190   time.SetFromDataSet( ds );
191
192   mStudyID   = studyid.GetValue();
193   mStudyTime = studytime.GetValue();
194   mStudyDate = studydate.GetValue();
195   mLabel     = label.GetValue();
196   mName      = atname.GetValue();
197   mTime      = time.GetValue();
198
199   //----------------------------------
200   // Read all ROI Names and number
201   // 0x3006,0x0020 = [ Structure Set ROI Sequence ]
202   gdcm::Tag tssroisq(0x3006,0x0020);
203   const gdcm::DataElement &ssroisq = ds.GetDataElement( tssroisq );
204   gdcm::SmartPointer<gdcm::SequenceOfItems> roi_seq = ssroisq.GetValueAsSQ();
205   assert(roi_seq); // TODO error message
206   for(unsigned int ridx = 0; ridx < roi_seq->GetNumberOfItems(); ++ridx)
207     {
208     gdcm::Item & item = roi_seq->GetItem( ridx + 1); // Item starts at 1
209     const gdcm::DataSet& nestedds = item.GetNestedDataSet();
210
211     gdcm::Attribute<0x3006,0x26> roiname;
212     roiname.SetFromDataSet( nestedds );
213     std::string name = roiname.GetValue();      // 0x3006,0x0026 = [ROI Name]
214     gdcm::Attribute<0x3006,0x0022> roinumber;
215     roinumber.SetFromDataSet( nestedds );
216     int nb = roinumber.GetValue();  // 0x3006,0x0022 = [ROI Number]
217     // Change number if needed
218
219     //TODO
220
221     // Check if such a number already exist
222     if (mMapOfROIName.find(nb) != mMapOfROIName.end()) {
223       std::cerr << "WARNING. A Roi already exist with the number "
224         << nb << ". I replace." << std::endl;
225     }
226     // Add in map
227     mMapOfROIName[nb] = name;
228     }
229   // DD(mMapOfROIName.size());
230
231   //----------------------------------
232   // Read all ROI
233   // 0x3006,0x0039 = [ ROI Contour Sequence ]
234   gdcm::Tag troicsq(0x3006,0x0039);
235   const gdcm::DataElement &roicsq = ds.GetDataElement( troicsq );
236   gdcm::SmartPointer<gdcm::SequenceOfItems> roi_contour_seq = roicsq.GetValueAsSQ();
237   assert(roi_contour_seq); // TODO error message
238   int n=0;
239   for(unsigned int ridx = 0; ridx < roi_contour_seq->GetNumberOfItems(); ++ridx)
240     {
241     gdcm::Item & item = roi_contour_seq->GetItem( ridx + 1); // Item starts at 1
242
243     DicomRT_ROI::Pointer roi = DicomRT_ROI::New();
244     roi->Read(mMapOfROIName, item);
245     mListOfROI.push_back(roi);
246     mMapOfROIIndex[roi->GetROINumber()] = n;
247     n++;
248     }
249
250 #else
251   gdcm::File reader;
252   reader.SetFileName(filename.c_str());
253   reader.SetMaxSizeLoadEntry(16384); // Needed ...
254   reader.SetLoadMode(gdcm::LD_NOSHADOW); // don't load shadow tags (in order to save memory)
255   reader.Load();
256
257   // Check file type
258   //Verify if the file is a RT-Structure-Set dicom file
259   if (!gdcm::Util::DicomStringEqual(reader.GetEntryValue(0x0008,0x0016),"1.2.840.10008.5.1.4.1.1.481.3")) {  //SOP clas UID
260     std::cerr << "Error. the file " << filename
261               << " is not a Dicom Struct ? (must have a SOP Class UID [0008|0016] = 1.2.840.10008.5.1.4.1.1.481.3 ==> [RT Structure Set Storage])"
262               << std::endl;
263     exit(0);
264   }
265   if (!gdcm::Util::DicomStringEqual(reader.GetEntryValue(0x0008,0x0060),"RTSTRUCT")) {  //SOP clas UID
266     std::cerr << "Error. the file " << filename
267               << " is not a Dicom Struct ? (must have 0x0008,0x0060 = RTSTRUCT [RT Structure Set Storage])"
268               << std::endl;
269     exit(0);
270   }
271
272   // Read global info
273   mStudyID   = reader.GetValEntry(0x0020,0x0010)->GetValue();
274   mStudyTime = reader.GetValEntry(0x008,0x0020)->GetValue();
275   mStudyDate = reader.GetValEntry(0x008,0x0030)->GetValue();
276   mLabel     = reader.GetValEntry(0x3006,0x002)->GetValue();
277   mName      = reader.GetValEntry(0x3006,0x004)->GetValue();
278   mTime      = reader.GetValEntry(0x3006,0x009)->GetValue();
279
280   //----------------------------------
281   // Read all ROI Names and number
282   // 0x3006,0x0020 = [ Structure Set ROI Sequence ]
283   gdcm::SeqEntry * roi_seq=reader.GetSeqEntry(0x3006,0x0020);
284   assert(roi_seq); // TODO error message
285   for (gdcm::SQItem* r=roi_seq->GetFirstSQItem(); r!=0; r=roi_seq->GetNextSQItem()) {
286     std::string name = r->GetEntryValue(0x3006,0x0026);      // 0x3006,0x0026 = [ROI Name]
287     int nb = atoi(r->GetEntryValue(0x3006,0x0022).c_str());  // 0x3006,0x0022 = [ROI Number]
288     // Change number if needed
289
290     //TODO
291
292     // Check if such a number already exist
293     if (mMapOfROIName.find(nb) != mMapOfROIName.end()) {
294       std::cerr << "WARNING. A Roi already exist with the number "
295                 << nb << ". I replace." << std::endl;
296     }
297     // Add in map
298     mMapOfROIName[nb] = name;
299   }
300   // DD(mMapOfROIName.size());
301
302   //----------------------------------
303   // Read all ROI
304   // 0x3006,0x0039 = [ ROI Contour Sequence ]
305   gdcm::SeqEntry * roi_contour_seq=reader.GetSeqEntry(0x3006,0x0039);
306   assert(roi_contour_seq); // TODO error message
307   int n=0;
308   for (gdcm::SQItem* r=roi_contour_seq->GetFirstSQItem(); r!=0; r=roi_contour_seq->GetNextSQItem()) {
309     DicomRT_ROI::Pointer roi = DicomRT_ROI::New();
310     roi->Read(mMapOfROIName, r);
311     mListOfROI.push_back(roi);
312     mMapOfROIIndex[roi->GetROINumber()] = n;
313     n++;
314   }
315
316 #endif
317 }
318 //--------------------------------------------------------------------
319
320
321 //--------------------------------------------------------------------
322 int clitk::DicomRT_StructureSet::AddBinaryImageAsNewROI(vvImage * im, std::string n)
323 {
324   //DD("AddBinaryImageAsNewROI");
325   // Search max ROI number
326   int max = -1;
327   for(unsigned int i=0; i<mListOfROI.size(); i++) {
328     if (mListOfROI[i]->GetROINumber() > max)
329       max = mListOfROI[i]->GetROINumber();
330   }
331   //  DD(max);
332   ++max;
333   //DD(max);
334
335   // Compute name
336   std::ostringstream oss;
337   oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(n));
338   //      << "_roi_" << max << vtksys::SystemTools::GetFilenameLastExtension(n);
339   //DD(oss.str());
340   mMapOfROIName[max] = oss.str();
341
342   // Set color
343   std::vector<double> color;
344   color.push_back(1);
345   color.push_back(0);
346   color.push_back(0);
347
348   // Create ROI
349   DicomRT_ROI::Pointer roi = DicomRT_ROI::New();
350   roi->SetFromBinaryImage(im, max, oss.str(), color, n);
351   mListOfROI.push_back(roi);
352   mMapOfROIIndex[mListOfROI.size()-1] = max;
353   //DD(mMapOfROIIndex[mListOfROI.size()-1]);
354   return max;
355 }
356 //--------------------------------------------------------------------
357
358