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://www.centreleonberard.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>
24 //--------------------------------------------------------------------
25 clitk::DicomRT_StructureSet::DicomRT_StructureSet()
27 mStudyID = "NoStudyID";
28 mStudyTime = "NoStudyTime";
29 mStudyDate = "NoStudyDate";
36 //--------------------------------------------------------------------
39 //--------------------------------------------------------------------
40 clitk::DicomRT_StructureSet::~DicomRT_StructureSet()
43 //--------------------------------------------------------------------
46 //--------------------------------------------------------------------
47 const std::string & clitk::DicomRT_StructureSet::GetStudyID() const
51 //--------------------------------------------------------------------
54 //--------------------------------------------------------------------
55 const std::string & clitk::DicomRT_StructureSet::GetStudyTime() const
59 //--------------------------------------------------------------------
62 //--------------------------------------------------------------------
63 const std::string & clitk::DicomRT_StructureSet::GetStudyDate() const
67 //--------------------------------------------------------------------
70 //--------------------------------------------------------------------
71 const std::string & clitk::DicomRT_StructureSet::GetLabel() const
75 //--------------------------------------------------------------------
78 //--------------------------------------------------------------------
79 const std::string & clitk::DicomRT_StructureSet::GetName() const
83 //--------------------------------------------------------------------
86 //--------------------------------------------------------------------
87 const std::string & clitk::DicomRT_StructureSet::GetDate() const
91 //--------------------------------------------------------------------
94 //--------------------------------------------------------------------
95 const std::string & clitk::DicomRT_StructureSet::GetTime() const
99 //--------------------------------------------------------------------
102 //--------------------------------------------------------------------
103 // const std::vector<clitk::DicomRT_ROI::Pointer> & clitk::DicomRT_StructureSet::GetListOfROI() const
105 // return mListOfROI;
107 //--------------------------------------------------------------------
110 //--------------------------------------------------------------------
111 clitk::DicomRT_ROI* clitk::DicomRT_StructureSet::GetROIFromROINumber(int n)
113 if (mROIs.find(n) == mROIs.end()) {
114 std::cerr << "No ROI number " << n << std::endl;
119 //--------------------------------------------------------------------
121 //--------------------------------------------------------------------
122 clitk::DicomRT_ROI* clitk::DicomRT_StructureSet::GetROIFromROIName(const std::string& name)
124 std::map<int, std::string>::iterator it = mMapOfROIName.begin();
126 while (it != mMapOfROIName.end() && number == -1) {
127 if (it->second == name)
134 std::cerr << "No ROI name " << name << std::endl;
138 return mROIs[number];
140 //--------------------------------------------------------------------
143 // RegEx version shall be available when C++x11 supports it propely
145 //--------------------------------------------------------------------
146 clitk::DicomRT_ROI* clitk::DicomRT_StructureSet::GetROIFromROINameRegEx(const std::string& regEx)
148 std::map<int, std::string>::iterator it = mMapOfROIName.begin();
151 while (it != mMapOfROIName.end() && number == -1) {
152 if (std::tr1::regex_match (it->second, std::tr1::regex(regEx)))
159 std::cerr << "No ROI name " << number << std::endl;
163 return mROIs[number];
165 //--------------------------------------------------------------------
167 //--------------------------------------------------------------------
168 clitk::DicomRT_ROI* clitk::DicomRT_StructureSet::GetROIFromROINameSubstr(const std::string& s)
170 std::map<int, std::string>::iterator it = mMapOfROIName.begin();
173 while (it != mMapOfROIName.end() && number == -1) {
174 if (it->second.find(s) != std::string::npos)
181 std::cerr << "No ROI name " << s << std::endl;
185 return mROIs[number];
187 //--------------------------------------------------------------------
189 //--------------------------------------------------------------------
190 clitk::DicomRT_StructureSet::ROIMapContainer *
191 clitk::DicomRT_StructureSet::GetROIsFromROINameSubstr(const std::string& s)
193 static ROIMapContainer rois;
196 ROIMapContainer::iterator it = mROIs.begin();
199 while (it != mROIs.end()) {
200 if (it->second->GetName().find(s) != std::string::npos) {
202 rois[number] = it->second;
208 std::cerr << "No ROI name " << s << std::endl;
215 //--------------------------------------------------------------------
217 //--------------------------------------------------------------------
218 void clitk::DicomRT_StructureSet::Print(std::ostream & os) const
220 os << "Study ID = " << mStudyID << std::endl
221 << "Study Date = " << mStudyDate << std::endl
222 << "Study Time = " << mStudyTime << std::endl
223 << "Struct Label = " << mLabel << std::endl
224 << "Struct Name = " << mName << std::endl
225 << "Struct Time = " << mTime << std::endl
226 << "Number of ROI = " << mROIs.size() << std::endl;
227 for(ROIConstIteratorType iter = mROIs.begin(); iter != mROIs.end(); iter++) {
228 iter->second->Print(os);
231 //--------------------------------------------------------------------
234 #if GDCM_MAJOR_VERSION == 2
235 //--------------------------------------------------------------------
236 int clitk::DicomRT_StructureSet::ReadROINumber(const gdcm::Item & item)
238 // 0x3006,0x0022 = [ROI Number]
239 const gdcm::DataSet & nestedds = item.GetNestedDataSet();
240 gdcm::Attribute<0x3006,0x0022> roinumber;
241 roinumber.SetFromDataSet( nestedds );
242 return roinumber.GetValue();
244 //--------------------------------------------------------------------
247 //--------------------------------------------------------------------
248 void clitk::DicomRT_StructureSet::Write(const std::string & filename)
250 #if GDCM_MAJOR_VERSION == 2
253 // Assert that the gdcm file is still open (we can write only if it was readed)
255 //assert(mFile != NULL);
256 FATAL("Sorry, I can write DICOM only if it was read first from a file with 'Read' function");
259 // Loop and update each ROI
260 for(ROIIteratorType iter = mROIs.begin(); iter != mROIs.end(); iter++) {
261 iter->second->UpdateDicomItem();
264 // Write [ Structure Set ROI Sequence ] = 0x3006,0x0020
265 gdcm::DataSet & ds = mFile->GetDataSet();
266 gdcm::Tag tssroisq(0x3006,0x0020);
267 const gdcm::DataElement &ssroisq = ds.GetDataElement( tssroisq );
268 gdcm::DataElement de(ssroisq);
269 de.SetValue(*mROIInfoSequenceOfItems);
272 // Write [ ROI Contour Sequence ] = 0x3006,0x0039
274 gdcm::Tag troicsq(0x3006,0x0039);
275 const gdcm::DataElement &roicsq = ds.GetDataElement( troicsq );
276 gdcm::DataElement de2(roicsq);
277 de2.SetValue(*mROIContoursSequenceOfItems);
281 gdcm::DataSet & a = mROIContoursSequenceOfItems->GetItem(1).GetNestedDataSet();
282 gdcm::Tag tcsq(0x3006,0x0040);
283 const gdcm::DataElement& csq = a.GetDataElement( tcsq );
284 gdcm::SmartPointer<gdcm::SequenceOfItems> sqi2 = csq.GetValueAsSQ();
285 gdcm::Item & j = sqi2->GetItem(1);
286 gdcm::DataSet & b = j.GetNestedDataSet();
287 gdcm::Attribute<0x3006,0x0050> at;
288 gdcm::Tag tcontourdata(0x3006,0x0050);
289 gdcm::DataElement contourdata = b.GetDataElement( tcontourdata );
290 at.SetFromDataElement( contourdata );
291 const double* points = at.GetValues();
297 //writer.CheckFileMetaInformationOff();
298 writer.SetFileName(filename.c_str());
299 writer.SetFile(*mFile);
304 FATAL("Sorry not compatible with GDCM1, use GDCM2");
307 //--------------------------------------------------------------------
310 //--------------------------------------------------------------------
311 void clitk::DicomRT_StructureSet::Read(const std::string & filename)
314 #if GDCM_MAJOR_VERSION == 2
316 mReader = new gdcm::Reader;
317 mReader->SetFileName(filename.c_str());
319 mFile = &(mReader->GetFile());
320 const gdcm::DataSet & ds = mFile->GetDataSet();
323 //Verify if the file is a RT-Structure-Set dicom file
324 gdcm::MediaStorage ms;
325 ms.SetFromFile(*mFile);
326 if( ms != gdcm::MediaStorage::RTStructureSetStorage )
328 std::cerr << "Error. the file " << filename
329 << " 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])"
334 gdcm::Attribute<0x8,0x60> modality;
335 modality.SetFromDataSet( ds );
336 if( modality.GetValue() != "RTSTRUCT" )
338 std::cerr << "Error. the file " << filename
339 << " is not a Dicom Struct ? (must have 0x0008,0x0060 = RTSTRUCT [RT Structure Set Storage])"
345 gdcm::Attribute<0x20,0x10> studyid;
346 studyid.SetFromDataSet( ds );
347 gdcm::Attribute<0x8,0x20> studytime;
348 studytime.SetFromDataSet( ds );
349 gdcm::Attribute<0x8,0x30> studydate;
350 studydate.SetFromDataSet( ds );
351 gdcm::Attribute<0x3006,0x02> label;
352 label.SetFromDataSet( ds );
353 gdcm::Attribute<0x3006,0x04> atname;
354 atname.SetFromDataSet( ds );
355 gdcm::Attribute<0x3006,0x09> time;
356 time.SetFromDataSet( ds );
358 mStudyID = studyid.GetValue();
359 mStudyTime = studytime.GetValue();
360 mStudyDate = studydate.GetValue();
361 mLabel = label.GetValue();
362 mName = atname.GetValue();
363 mTime = time.GetValue();
365 // Temporary store the list of items
366 std::map<int, gdcm::Item*> mMapOfROIInfo;
367 std::map<int, gdcm::Item*> mMapOfROIContours;
369 //----------------------------------
370 // Read all ROI Names and number
371 // 0x3006,0x0020 = [ Structure Set ROI Sequence ]
372 gdcm::Tag tssroisq(0x3006,0x0020);
373 const gdcm::DataElement &ssroisq = ds.GetDataElement( tssroisq );
374 mROIInfoSequenceOfItems = ssroisq.GetValueAsSQ();
375 gdcm::SmartPointer<gdcm::SequenceOfItems> & roi_seq = mROIInfoSequenceOfItems;
376 assert(roi_seq); // TODO error message
377 for(unsigned int ridx = 0; ridx < roi_seq->GetNumberOfItems(); ++ridx)
379 gdcm::Item & item = roi_seq->GetItem( ridx + 1); // Item starts at 1
380 const gdcm::DataSet& nestedds = item.GetNestedDataSet();
382 gdcm::Attribute<0x3006,0x26> roiname;
383 roiname.SetFromDataSet( nestedds );
384 std::string name = roiname.GetValue(); // 0x3006,0x0026 = [ROI Name]
386 // 0x3006,0x0022 = [ROI Number]
387 int nb = ReadROINumber(item);
390 mMapOfROIInfo[nb] = &item;
392 // Check if such a number already exist
393 if (mMapOfROIName.find(nb) != mMapOfROIName.end()) {
394 std::cerr << "WARNING. A Roi already exist with the number "
395 << nb << ". I replace." << std::endl;
398 mMapOfROIName[nb] = name;
401 //----------------------------------
403 // 0x3006,0x0039 = [ ROI Contour Sequence ]
404 gdcm::Tag troicsq(0x3006,0x0039);
405 const gdcm::DataElement &roicsq = ds.GetDataElement( troicsq );
406 gdcm::SmartPointer<gdcm::SequenceOfItems> roi_contour_seq = roicsq.GetValueAsSQ();
407 mROIContoursSequenceOfItems = roi_contour_seq;
408 assert(roi_contour_seq); // TODO error message
409 for(unsigned int ridx = 0; ridx < roi_contour_seq->GetNumberOfItems(); ++ridx) {
410 gdcm::Item & item = roi_contour_seq->GetItem( ridx + 1); // Item starts at 1
411 // ROI number [Referenced ROI Number]
412 const gdcm::DataSet& nestedds = item.GetNestedDataSet();
413 gdcm::Attribute<0x3006,0x0084> referencedroinumber;
414 referencedroinumber.SetFromDataSet( nestedds );
415 int nb = referencedroinumber.GetValue();
417 mMapOfROIContours[nb] = &item;
420 //----------------------------------
422 for(std::map<int, gdcm::Item*>::iterator i = mMapOfROIInfo.begin(); i != mMapOfROIInfo.end(); i++) {
423 int nb = i->first;//ReadROINumber(i);//mROIIndex[i];
425 DicomRT_ROI::Pointer roi = DicomRT_ROI::New();
426 roi->Read(mMapOfROIInfo[nb], mMapOfROIContours[nb]);
427 // mListOfROI.push_back(roi);
428 // mMapOfROIIndex[nb] = i;
432 //----------------------------------------------------------------------------------------
433 //----------------------------------------------------------------------------------------
434 //----------------------------------------------------------------------------------------
436 mFile = new gdcm::File;
437 mFile->SetFileName(filename.c_str());
438 mFile->SetMaxSizeLoadEntry(16384); // Needed ...
439 mFile->SetLoadMode(gdcm::LD_NOSHADOW); // don't load shadow tags (in order to save memory)
443 //Verify if the file is a RT-Structure-Set dicom file
444 if (!gdcm::Util::DicomStringEqual(mFile->GetEntryValue(0x0008,0x0016),"1.2.840.10008.5.1.4.1.1.481.3")) { //SOP clas UID
445 std::cerr << "Error. the file " << filename
446 << " 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])"
450 if (!gdcm::Util::DicomStringEqual(mFile->GetEntryValue(0x0008,0x0060),"RTSTRUCT")) { //SOP clas UID
451 std::cerr << "Error. the file " << filename
452 << " is not a Dicom Struct ? (must have 0x0008,0x0060 = RTSTRUCT [RT Structure Set Storage])"
458 mStudyID = mFile->GetValEntry(0x0020,0x0010)->GetValue();
459 mStudyTime = mFile->GetValEntry(0x008,0x0020)->GetValue();
460 mStudyDate = mFile->GetValEntry(0x008,0x0030)->GetValue();
461 mLabel = mFile->GetValEntry(0x3006,0x002)->GetValue();
462 if (!mFile->GetValEntry(0x3006,0x004)) {
466 mName = mFile->GetValEntry(0x3006,0x004)->GetValue();
468 mTime = mFile->GetValEntry(0x3006,0x009)->GetValue();
470 //----------------------------------
471 // Read all ROI Names and number
472 // 0x3006,0x0020 = [ Structure Set ROI Sequence ]
473 gdcm::SeqEntry * roi_seq=mFile->GetSeqEntry(0x3006,0x0020);
474 assert(roi_seq); // TODO error message
475 for (gdcm::SQItem* r=roi_seq->GetFirstSQItem(); r!=0; r=roi_seq->GetNextSQItem()) {
476 std::string name = r->GetEntryValue(0x3006,0x0026); // 0x3006,0x0026 = [ROI Name]
477 int nb = atoi(r->GetEntryValue(0x3006,0x0022).c_str()); // 0x3006,0x0022 = [ROI Number]
478 // Check if such a number already exist
479 if (mMapOfROIName.find(nb) != mMapOfROIName.end()) {
480 std::cerr << "WARNING. A Roi already exist with the number "
481 << nb << ". I replace." << std::endl;
484 mMapOfROIName[nb] = name;
487 //----------------------------------
489 // 0x3006,0x0039 = [ ROI Contour Sequence ]
490 gdcm::SeqEntry * roi_contour_seq=mFile->GetSeqEntry(0x3006,0x0039);
491 assert(roi_contour_seq); // TODO error message
493 for (gdcm::SQItem* r=roi_contour_seq->GetFirstSQItem(); r!=0; r=roi_contour_seq->GetNextSQItem()) {
494 DicomRT_ROI::Pointer roi = DicomRT_ROI::New();
495 roi->Read(mMapOfROIName, r);
496 mROIs[roi->GetROINumber()] = roi;
502 //--------------------------------------------------------------------
505 //--------------------------------------------------------------------
506 bool clitk::DicomRT_StructureSet::IsDicomRTStruct(const std::string & filename)
509 #if GDCM_MAJOR_VERSION == 2
511 mReader = new gdcm::Reader;
512 mReader->SetFileName(filename.c_str());
514 mFile = &(mReader->GetFile());
515 const gdcm::DataSet & ds = mFile->GetDataSet();
518 //Verify if the file is a RT-Structure-Set dicom file
519 gdcm::MediaStorage ms;
520 ms.SetFromFile(*mFile);
521 if( ms != gdcm::MediaStorage::RTStructureSetStorage ) return false;
523 gdcm::Attribute<0x8,0x60> modality;
524 modality.SetFromDataSet( ds );
525 if( modality.GetValue() != "RTSTRUCT" ) return false;
529 //----------------------------------------------------------------------------------------
531 mFile = new gdcm::File;
532 mFile->SetFileName(filename.c_str());
533 mFile->SetMaxSizeLoadEntry(16384); // Needed ...
534 mFile->SetLoadMode(gdcm::LD_NOSHADOW); // don't load shadow tags (in order to save memory)
538 //Verify if the file is a RT-Structure-Set dicom file
539 if (!gdcm::Util::DicomStringEqual(mFile->GetEntryValue(0x0008,0x0016),"1.2.840.10008.5.1.4.1.1.481.3"))
541 if (!gdcm::Util::DicomStringEqual(mFile->GetEntryValue(0x0008,0x0060),"RTSTRUCT"))
548 //--------------------------------------------------------------------
551 //--------------------------------------------------------------------
552 int clitk::DicomRT_StructureSet::AddBinaryImageAsNewROI(vvImage * im, std::string n)
554 // Search max ROI number
556 for(ROIConstIteratorType iter = mROIs.begin(); iter != mROIs.end(); iter++) {
557 // for(unsigned int i=0; i<mListOfROI.size(); i++) {
558 clitk::DicomRT_ROI::Pointer roi = iter->second;
559 if (roi->GetROINumber() > max)
560 max = roi->GetROINumber();
565 std::ostringstream oss;
566 oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(n));
567 mMapOfROIName[max] = oss.str();
570 std::vector<double> color;
576 DicomRT_ROI::Pointer roi = DicomRT_ROI::New();
577 roi->SetFromBinaryImage(im, max, oss.str(), color, n);
581 //--------------------------------------------------------------------