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() {
25 mStudyID = "NoStudyID";
26 mStudyTime = "NoStudyTime";
27 mStudyDate = "NoStudyDate";
33 //--------------------------------------------------------------------
36 //--------------------------------------------------------------------
37 clitk::DicomRT_StructureSet::~DicomRT_StructureSet() {
40 //--------------------------------------------------------------------
43 //--------------------------------------------------------------------
44 const std::string & clitk::DicomRT_StructureSet::GetStudyID() const {
47 //--------------------------------------------------------------------
50 //--------------------------------------------------------------------
51 const std::string & clitk::DicomRT_StructureSet::GetStudyTime() const{
54 //--------------------------------------------------------------------
57 //--------------------------------------------------------------------
58 const std::string & clitk::DicomRT_StructureSet::GetStudyDate() const {
61 //--------------------------------------------------------------------
64 //--------------------------------------------------------------------
65 const std::string & clitk::DicomRT_StructureSet::GetLabel() const {
68 //--------------------------------------------------------------------
71 //--------------------------------------------------------------------
72 const std::string & clitk::DicomRT_StructureSet::GetName() const {
75 //--------------------------------------------------------------------
78 //--------------------------------------------------------------------
79 const std::string & clitk::DicomRT_StructureSet::GetDate() const {
82 //--------------------------------------------------------------------
85 //--------------------------------------------------------------------
86 const std::string & clitk::DicomRT_StructureSet::GetTime() const {
89 //--------------------------------------------------------------------
92 //--------------------------------------------------------------------
93 const std::vector<clitk::DicomRT_ROI*> & clitk::DicomRT_StructureSet::GetListOfROI() const {
96 //--------------------------------------------------------------------
99 //--------------------------------------------------------------------
100 clitk::DicomRT_ROI* clitk::DicomRT_StructureSet::GetROI(int n) {
101 if (mMapOfROIIndex.find(n) == mMapOfROIIndex.end()) {
102 std::cerr << "No ROI number " << n << std::endl;
105 DD(mListOfROI[mMapOfROIIndex[n]]->GetName());
106 DD(mListOfROI[mMapOfROIIndex[n]]->GetROINumber());
107 return mListOfROI[mMapOfROIIndex[n]];
109 //--------------------------------------------------------------------
112 //--------------------------------------------------------------------
113 void clitk::DicomRT_StructureSet::Print(std::ostream & os) const {
114 os << "Study ID = " << mStudyID << std::endl
115 << "Study Date = " << mStudyDate << std::endl
116 << "Study Time = " << mStudyTime << std::endl
117 << "Struct Label = " << mLabel << std::endl
118 << "Struct Name = " << mName << std::endl
119 << "Struct Time = " << mTime << std::endl
120 << "Number of ROI = " << mListOfROI.size() << std::endl;
121 for(unsigned int i=0; i<mListOfROI.size(); i++) {
122 mListOfROI[i]->Print(os);
125 //--------------------------------------------------------------------
128 //--------------------------------------------------------------------
129 void clitk::DicomRT_StructureSet::Read(const std::string & filename) {
132 reader.SetFileName(filename.c_str());
133 reader.SetMaxSizeLoadEntry(16384); // Needed ...
134 reader.SetLoadMode(gdcm::LD_NOSHADOW); // don't load shadow tags (in order to save memory)
138 //Verify if the file is a RT-Structure-Set dicom file
139 if (!gdcm::Util::DicomStringEqual(reader.GetEntryValue(0x0008,0x0016),"1.2.840.10008.5.1.4.1.1.481.3")) { //SOP clas UID
140 std::cerr << "Error. the file " << filename
141 << " 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])"
145 if (!gdcm::Util::DicomStringEqual(reader.GetEntryValue(0x0008,0x0060),"RTSTRUCT")) { //SOP clas UID
146 std::cerr << "Error. the file " << filename
147 << " is not a Dicom Struct ? (must have 0x0008,0x0060 = RTSTRUCT [RT Structure Set Storage])"
153 mStudyID = reader.GetValEntry(0x0020,0x0010)->GetValue();
154 mStudyTime = reader.GetValEntry(0x008,0x0020)->GetValue();
155 mStudyDate = reader.GetValEntry(0x008,0x0030)->GetValue();
156 mLabel = reader.GetValEntry(0x3006,0x002)->GetValue();
157 mName = reader.GetValEntry(0x3006,0x004)->GetValue();
158 mTime = reader.GetValEntry(0x3006,0x009)->GetValue();
160 //----------------------------------
161 // Read all ROI Names and number
162 // 0x3006,0x0020 = [ Structure Set ROI Sequence ]
163 gdcm::SeqEntry * roi_seq=reader.GetSeqEntry(0x3006,0x0020);
164 assert(roi_seq); // TODO error message
165 for (gdcm::SQItem* r=roi_seq->GetFirstSQItem();r!=0;r=roi_seq->GetNextSQItem()) {
166 std::string name = r->GetEntryValue(0x3006,0x0026); // 0x3006,0x0026 = [ROI Name]
167 int nb = atoi(r->GetEntryValue(0x3006,0x0022).c_str()); // 0x3006,0x0022 = [ROI Number]
168 // Change number if needed
172 // Check if such a number already exist
173 if (mMapOfROIName.find(nb) != mMapOfROIName.end()) {
174 std::cerr << "WARNING. A Roi already exist with the number "
175 << nb << ". I replace." << std::endl;
178 mMapOfROIName[nb] = name;
180 // DD(mMapOfROIName.size());
182 //----------------------------------
184 // 0x3006,0x0039 = [ ROI Contour Sequence ]
185 gdcm::SeqEntry * roi_contour_seq=reader.GetSeqEntry(0x3006,0x0039);
186 assert(roi_contour_seq); // TODO error message
188 for (gdcm::SQItem* r=roi_contour_seq->GetFirstSQItem();r!=0;r=roi_contour_seq->GetNextSQItem()) {
189 DicomRT_ROI * roi = new DicomRT_ROI;
190 roi->Read(mMapOfROIName, r);
191 mListOfROI.push_back(roi);
192 mMapOfROIIndex[roi->GetROINumber()] = n;
197 //--------------------------------------------------------------------
200 //--------------------------------------------------------------------
201 int clitk::DicomRT_StructureSet::AddBinaryImageAsNewROI(vvImage::Pointer im, std::string n) {
202 DD("AddBinaryImageAsNewROI");
203 // Search max ROI number
205 for(unsigned int i=0; i<mListOfROI.size(); i++) {
206 if (mListOfROI[i]->GetROINumber() > max)
207 max = mListOfROI[i]->GetROINumber();
214 std::ostringstream oss;
215 oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(n))
216 << "_roi_" << max << vtksys::SystemTools::GetFilenameLastExtension(n);
218 mMapOfROIName[max] = oss.str();
221 std::vector<double> color;
227 DicomRT_ROI * roi = new DicomRT_ROI;
228 roi->SetFromBinaryImage(im,
232 mListOfROI.push_back(roi);
233 mMapOfROIIndex[mListOfROI.size()-1] = max;
234 DD(mMapOfROIIndex[mListOfROI.size()-1]);
237 //--------------------------------------------------------------------