]> Creatis software - clitk.git/blob - common/clitkDicomRT_StructureSet.cxx
d7c4e7932d6d2ef37a3c8e6b5e831493857fb237
[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
23 //--------------------------------------------------------------------
24 clitk::DicomRT_StructureSet::DicomRT_StructureSet() {
25   mStudyID = "NoStudyID";
26   mStudyTime = "NoStudyTime";
27   mStudyDate = "NoStudyDate";
28   mLabel = "NoLabel";
29   mName = "NoName";
30   mDate = "NoDate";
31   mTime = "NoTime";
32 }
33 //--------------------------------------------------------------------
34
35
36 //--------------------------------------------------------------------
37 clitk::DicomRT_StructureSet::~DicomRT_StructureSet() {
38   
39 }
40 //--------------------------------------------------------------------
41
42
43 //--------------------------------------------------------------------
44 const std::string & clitk::DicomRT_StructureSet::GetStudyID() const {
45   return mStudyID;
46 }
47 //--------------------------------------------------------------------
48
49
50 //--------------------------------------------------------------------
51 const std::string & clitk::DicomRT_StructureSet::GetStudyTime() const{
52   return mStudyTime;
53 }
54 //--------------------------------------------------------------------
55
56
57 //--------------------------------------------------------------------
58 const std::string & clitk::DicomRT_StructureSet::GetStudyDate() const {
59   return mStudyDate;
60 }
61 //--------------------------------------------------------------------
62
63
64 //--------------------------------------------------------------------
65 const std::string & clitk::DicomRT_StructureSet::GetLabel() const {
66   return mLabel;
67 }
68 //--------------------------------------------------------------------
69
70
71 //--------------------------------------------------------------------
72 const std::string & clitk::DicomRT_StructureSet::GetName() const {
73   return mName;
74 }
75 //--------------------------------------------------------------------
76
77
78 //--------------------------------------------------------------------
79 const std::string & clitk::DicomRT_StructureSet::GetDate() const {
80   return mDate;
81 }
82 //--------------------------------------------------------------------
83
84
85 //--------------------------------------------------------------------
86 const std::string & clitk::DicomRT_StructureSet::GetTime() const {
87   return mTime;
88 }
89 //--------------------------------------------------------------------
90     
91  
92 //--------------------------------------------------------------------
93 const std::vector<clitk::DicomRT_ROI*> & clitk::DicomRT_StructureSet::GetListOfROI() const {
94   return mListOfROI;
95 }
96 //--------------------------------------------------------------------
97     
98  
99 //--------------------------------------------------------------------
100 clitk::DicomRT_ROI* clitk::DicomRT_StructureSet::GetROI(int n) {
101   if (mMapOfROIIndex.find(n) == mMapOfROIIndex.end()) {
102     std::cerr << "No ROI number " << n << std::endl;
103     return NULL;
104   }
105   DD(mListOfROI[mMapOfROIIndex[n]]->GetName());
106   DD(mListOfROI[mMapOfROIIndex[n]]->GetROINumber());  
107   return mListOfROI[mMapOfROIIndex[n]];
108 }
109 //--------------------------------------------------------------------
110     
111  
112 //--------------------------------------------------------------------
113 void clitk::DicomRT_StructureSet::Print(std::ostream & os) const {
114   os << "Study ID      = " << mStudyID << std::endl
115      << "Study Date    = " << mStudyDate << std::endl
116      << "Study Time    = " << mStudyTime << std::endl
117      << "Struct Label  = " << mLabel << std::endl
118      << "Struct Name   = " << mName << std::endl
119      << "Struct Time   = " << mTime << std::endl
120      << "Number of ROI = " << mListOfROI.size() << std::endl;
121   for(unsigned int i=0; i<mListOfROI.size(); i++) {
122     mListOfROI[i]->Print(os);
123   }
124 }
125 //--------------------------------------------------------------------
126
127
128 //--------------------------------------------------------------------
129 void clitk::DicomRT_StructureSet::Read(const std::string & filename) {
130   // Open DICOM
131   gdcm::File reader;
132   reader.SetFileName(filename.c_str());
133   reader.SetMaxSizeLoadEntry(16384); // Needed ...
134   reader.SetLoadMode(gdcm::LD_NOSHADOW); // don't load shadow tags (in order to save memory)
135   reader.Load();
136
137   // Check file type
138   //Verify if the file is a RT-Structure-Set dicom file
139   if (!gdcm::Util::DicomStringEqual(reader.GetEntryValue(0x0008,0x0016),"1.2.840.10008.5.1.4.1.1.481.3")) {  //SOP clas UID
140     std::cerr << "Error. the file " << filename 
141               << " 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])"
142               << std::endl;
143     exit(0);
144   }    
145   if (!gdcm::Util::DicomStringEqual(reader.GetEntryValue(0x0008,0x0060),"RTSTRUCT")) {  //SOP clas UID
146     std::cerr << "Error. the file " << filename 
147               << " is not a Dicom Struct ? (must have 0x0008,0x0060 = RTSTRUCT [RT Structure Set Storage])"
148               << std::endl;
149     exit(0);
150   }    
151
152   // Read global info
153   mStudyID   = reader.GetValEntry(0x0020,0x0010)->GetValue();
154   mStudyTime = reader.GetValEntry(0x008,0x0020)->GetValue();
155   mStudyDate = reader.GetValEntry(0x008,0x0030)->GetValue();
156   mLabel     = reader.GetValEntry(0x3006,0x002)->GetValue();
157   mName      = reader.GetValEntry(0x3006,0x004)->GetValue();
158   mTime      = reader.GetValEntry(0x3006,0x009)->GetValue();
159   
160   //----------------------------------
161   // Read all ROI Names and number
162   // 0x3006,0x0020 = [ Structure Set ROI Sequence ]
163   gdcm::SeqEntry * roi_seq=reader.GetSeqEntry(0x3006,0x0020);
164   assert(roi_seq); // TODO error message
165   for (gdcm::SQItem* r=roi_seq->GetFirstSQItem();r!=0;r=roi_seq->GetNextSQItem()) {
166     std::string name = r->GetEntryValue(0x3006,0x0026);      // 0x3006,0x0026 = [ROI Name] 
167     int nb = atoi(r->GetEntryValue(0x3006,0x0022).c_str());  // 0x3006,0x0022 = [ROI Number]
168     // Change number if needed
169     
170     //TODO
171
172     // Check if such a number already exist
173     if (mMapOfROIName.find(nb) != mMapOfROIName.end()) {
174       std::cerr << "WARNING. A Roi already exist with the number " 
175                 << nb << ". I replace." << std::endl;
176     }
177     // Add in map
178     mMapOfROIName[nb] = name;
179   }
180   // DD(mMapOfROIName.size());
181   
182   //----------------------------------
183   // Read all ROI   
184   // 0x3006,0x0039 = [ ROI Contour Sequence ]
185   gdcm::SeqEntry * roi_contour_seq=reader.GetSeqEntry(0x3006,0x0039);
186   assert(roi_contour_seq); // TODO error message
187   int n=0;
188   for (gdcm::SQItem* r=roi_contour_seq->GetFirstSQItem();r!=0;r=roi_contour_seq->GetNextSQItem()) {
189     DicomRT_ROI * roi = new DicomRT_ROI;
190     roi->Read(mMapOfROIName, r);
191     mListOfROI.push_back(roi);
192     mMapOfROIIndex[roi->GetROINumber()] = n;
193     n++;
194   }
195
196 }
197 //--------------------------------------------------------------------
198
199
200 //--------------------------------------------------------------------
201 int clitk::DicomRT_StructureSet::AddBinaryImageAsNewROI(vvImage::Pointer im, std::string n) {
202   DD("AddBinaryImageAsNewROI");
203   // Search max ROI number
204   int max = -1;
205   for(unsigned int i=0; i<mListOfROI.size(); i++) {
206     if (mListOfROI[i]->GetROINumber() > max) 
207       max = mListOfROI[i]->GetROINumber();
208   }
209   DD(max);
210   ++max;
211   DD(max);
212
213   // Compute name
214   std::ostringstream oss;
215   oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(n))
216       << "_roi_" << max << vtksys::SystemTools::GetFilenameLastExtension(n);
217   DD(oss.str());
218   mMapOfROIName[max] = oss.str();
219
220   // Set color
221   std::vector<double> color;
222   color.push_back(1);
223   color.push_back(0);
224   color.push_back(0);
225   
226   // Create ROI
227   DicomRT_ROI * roi = new DicomRT_ROI;
228   roi->SetFromBinaryImage(im, 
229                           max, 
230                           oss.str(), 
231                           color);
232   mListOfROI.push_back(roi);
233   mMapOfROIIndex[mListOfROI.size()-1] = max;
234   DD(mMapOfROIIndex[mListOfROI.size()-1]);
235   return max;
236 }
237 //--------------------------------------------------------------------
238
239