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()
41 //--------------------------------------------------------------------
44 //--------------------------------------------------------------------
45 const std::string & clitk::DicomRT_StructureSet::GetStudyID() const
49 //--------------------------------------------------------------------
52 //--------------------------------------------------------------------
53 const std::string & clitk::DicomRT_StructureSet::GetStudyTime() const
57 //--------------------------------------------------------------------
60 //--------------------------------------------------------------------
61 const std::string & clitk::DicomRT_StructureSet::GetStudyDate() const
65 //--------------------------------------------------------------------
68 //--------------------------------------------------------------------
69 const std::string & clitk::DicomRT_StructureSet::GetLabel() const
73 //--------------------------------------------------------------------
76 //--------------------------------------------------------------------
77 const std::string & clitk::DicomRT_StructureSet::GetName() const
81 //--------------------------------------------------------------------
84 //--------------------------------------------------------------------
85 const std::string & clitk::DicomRT_StructureSet::GetDate() const
89 //--------------------------------------------------------------------
92 //--------------------------------------------------------------------
93 const std::string & clitk::DicomRT_StructureSet::GetTime() const
97 //--------------------------------------------------------------------
100 //--------------------------------------------------------------------
101 const std::vector<clitk::DicomRT_ROI::Pointer> & clitk::DicomRT_StructureSet::GetListOfROI() const
105 //--------------------------------------------------------------------
108 //--------------------------------------------------------------------
109 clitk::DicomRT_ROI* clitk::DicomRT_StructureSet::GetROI(int n)
111 if (mMapOfROIIndex.find(n) == mMapOfROIIndex.end()) {
112 std::cerr << "No ROI number " << n << std::endl;
115 // DD(mListOfROI[mMapOfROIIndex[n]]->GetName());
116 //DD(mListOfROI[mMapOfROIIndex[n]]->GetROINumber());
117 return mListOfROI[mMapOfROIIndex[n]];
119 //--------------------------------------------------------------------
122 //--------------------------------------------------------------------
123 void clitk::DicomRT_StructureSet::Print(std::ostream & os) const
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);
136 //--------------------------------------------------------------------
139 //--------------------------------------------------------------------
140 void clitk::DicomRT_StructureSet::Read(const std::string & filename)
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)
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])"
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])"
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();
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
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;
190 mMapOfROIName[nb] = name;
192 // DD(mMapOfROIName.size());
194 //----------------------------------
196 // 0x3006,0x0039 = [ ROI Contour Sequence ]
197 gdcm::SeqEntry * roi_contour_seq=reader.GetSeqEntry(0x3006,0x0039);
198 assert(roi_contour_seq); // TODO error message
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;
209 //--------------------------------------------------------------------
212 //--------------------------------------------------------------------
213 int clitk::DicomRT_StructureSet::AddBinaryImageAsNewROI(vvImage * im, std::string n)
215 //DD("AddBinaryImageAsNewROI");
216 // Search max ROI number
218 for(unsigned int i=0; i<mListOfROI.size(); i++) {
219 if (mListOfROI[i]->GetROINumber() > max)
220 max = mListOfROI[i]->GetROINumber();
227 std::ostringstream oss;
228 oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(n));
229 // << "_roi_" << max << vtksys::SystemTools::GetFilenameLastExtension(n);
231 mMapOfROIName[max] = oss.str();
234 std::vector<double> color;
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]);
247 //--------------------------------------------------------------------