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