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