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()
42 //--------------------------------------------------------------------
45 //--------------------------------------------------------------------
46 const std::string & clitk::DicomRT_StructureSet::GetStudyID() const
50 //--------------------------------------------------------------------
53 //--------------------------------------------------------------------
54 const std::string & clitk::DicomRT_StructureSet::GetStudyTime() const
58 //--------------------------------------------------------------------
61 //--------------------------------------------------------------------
62 const std::string & clitk::DicomRT_StructureSet::GetStudyDate() const
66 //--------------------------------------------------------------------
69 //--------------------------------------------------------------------
70 const std::string & clitk::DicomRT_StructureSet::GetLabel() const
74 //--------------------------------------------------------------------
77 //--------------------------------------------------------------------
78 const std::string & clitk::DicomRT_StructureSet::GetName() const
82 //--------------------------------------------------------------------
85 //--------------------------------------------------------------------
86 const std::string & clitk::DicomRT_StructureSet::GetDate() const
90 //--------------------------------------------------------------------
93 //--------------------------------------------------------------------
94 const std::string & clitk::DicomRT_StructureSet::GetTime() const
98 //--------------------------------------------------------------------
101 //--------------------------------------------------------------------
102 const std::vector<clitk::DicomRT_ROI*> & clitk::DicomRT_StructureSet::GetListOfROI() const
106 //--------------------------------------------------------------------
109 //--------------------------------------------------------------------
110 clitk::DicomRT_ROI* clitk::DicomRT_StructureSet::GetROI(int n)
112 if (mMapOfROIIndex.find(n) == mMapOfROIIndex.end()) {
113 std::cerr << "No ROI number " << n << std::endl;
116 // DD(mListOfROI[mMapOfROIIndex[n]]->GetName());
117 //DD(mListOfROI[mMapOfROIIndex[n]]->GetROINumber());
118 return mListOfROI[mMapOfROIIndex[n]];
120 //--------------------------------------------------------------------
123 //--------------------------------------------------------------------
124 void clitk::DicomRT_StructureSet::Print(std::ostream & os) const
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);
137 //--------------------------------------------------------------------
140 //--------------------------------------------------------------------
141 void clitk::DicomRT_StructureSet::Read(const std::string & filename)
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)
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])"
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])"
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();
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
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;
191 mMapOfROIName[nb] = name;
193 // DD(mMapOfROIName.size());
195 //----------------------------------
197 // 0x3006,0x0039 = [ ROI Contour Sequence ]
198 gdcm::SeqEntry * roi_contour_seq=reader.GetSeqEntry(0x3006,0x0039);
199 assert(roi_contour_seq); // TODO error message
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;
210 //--------------------------------------------------------------------
213 //--------------------------------------------------------------------
214 int clitk::DicomRT_StructureSet::AddBinaryImageAsNewROI(vvImage::Pointer im, std::string n)
216 //DD("AddBinaryImageAsNewROI");
217 // Search max ROI number
219 for(unsigned int i=0; i<mListOfROI.size(); i++) {
220 if (mListOfROI[i]->GetROINumber() > max)
221 max = mListOfROI[i]->GetROINumber();
228 std::ostringstream oss;
229 oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(n));
230 // << "_roi_" << max << vtksys::SystemTools::GetFilenameLastExtension(n);
232 mMapOfROIName[max] = oss.str();
235 std::vector<double> color;
241 DicomRT_ROI * roi = new DicomRT_ROI;
242 roi->SetFromBinaryImage(im, max, oss.str(), color, n);
243 mListOfROI.push_back(roi);
244 mMapOfROIIndex[mListOfROI.size()-1] = max;
245 //DD(mMapOfROIIndex[mListOfROI.size()-1]);
248 //--------------------------------------------------------------------