1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
3 Main authors : XX XX XX
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
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.
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
18 =========================================================================*/
20 #include "clitkDicomRT_StructureSet.h"
21 #include <vtksys/SystemTools.hxx>
23 //--------------------------------------------------------------------
24 clitk::DicomRT_StructureSet::DicomRT_StructureSet()
26 mStudyID = "NoStudyID";
27 mStudyTime = "NoStudyTime";
28 mStudyDate = "NoStudyDate";
34 //--------------------------------------------------------------------
37 //--------------------------------------------------------------------
38 clitk::DicomRT_StructureSet::~DicomRT_StructureSet()
40 for(uint i=0; i<mListOfROI.size(); i++) {
44 //--------------------------------------------------------------------
47 //--------------------------------------------------------------------
48 const std::string & clitk::DicomRT_StructureSet::GetStudyID() const
52 //--------------------------------------------------------------------
55 //--------------------------------------------------------------------
56 const std::string & clitk::DicomRT_StructureSet::GetStudyTime() const
60 //--------------------------------------------------------------------
63 //--------------------------------------------------------------------
64 const std::string & clitk::DicomRT_StructureSet::GetStudyDate() const
68 //--------------------------------------------------------------------
71 //--------------------------------------------------------------------
72 const std::string & clitk::DicomRT_StructureSet::GetLabel() const
76 //--------------------------------------------------------------------
79 //--------------------------------------------------------------------
80 const std::string & clitk::DicomRT_StructureSet::GetName() const
84 //--------------------------------------------------------------------
87 //--------------------------------------------------------------------
88 const std::string & clitk::DicomRT_StructureSet::GetDate() const
92 //--------------------------------------------------------------------
95 //--------------------------------------------------------------------
96 const std::string & clitk::DicomRT_StructureSet::GetTime() const
100 //--------------------------------------------------------------------
103 //--------------------------------------------------------------------
104 const std::vector<clitk::DicomRT_ROI*> & clitk::DicomRT_StructureSet::GetListOfROI() const
108 //--------------------------------------------------------------------
111 //--------------------------------------------------------------------
112 clitk::DicomRT_ROI* clitk::DicomRT_StructureSet::GetROI(int n)
114 if (mMapOfROIIndex.find(n) == mMapOfROIIndex.end()) {
115 std::cerr << "No ROI number " << n << std::endl;
118 // DD(mListOfROI[mMapOfROIIndex[n]]->GetName());
119 //DD(mListOfROI[mMapOfROIIndex[n]]->GetROINumber());
120 return mListOfROI[mMapOfROIIndex[n]];
122 //--------------------------------------------------------------------
125 //--------------------------------------------------------------------
126 void clitk::DicomRT_StructureSet::Print(std::ostream & os) const
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);
139 //--------------------------------------------------------------------
142 //--------------------------------------------------------------------
143 void clitk::DicomRT_StructureSet::Read(const std::string & filename)
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)
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])"
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])"
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();
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
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;
193 mMapOfROIName[nb] = name;
195 // DD(mMapOfROIName.size());
197 //----------------------------------
199 // 0x3006,0x0039 = [ ROI Contour Sequence ]
200 gdcm::SeqEntry * roi_contour_seq=reader.GetSeqEntry(0x3006,0x0039);
201 assert(roi_contour_seq); // TODO error message
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;
212 //--------------------------------------------------------------------
215 //--------------------------------------------------------------------
216 int clitk::DicomRT_StructureSet::AddBinaryImageAsNewROI(vvImage * im, std::string n)
218 //DD("AddBinaryImageAsNewROI");
219 // Search max ROI number
221 for(unsigned int i=0; i<mListOfROI.size(); i++) {
222 if (mListOfROI[i]->GetROINumber() > max)
223 max = mListOfROI[i]->GetROINumber();
230 std::ostringstream oss;
231 oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(n));
232 // << "_roi_" << max << vtksys::SystemTools::GetFilenameLastExtension(n);
234 mMapOfROIName[max] = oss.str();
237 std::vector<double> color;
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]);
250 //--------------------------------------------------------------------