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