X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FclitkDicomRT_StructureSet.cxx;h=2d5abd15ead731c71c343249dec98dd385c1ceb6;hb=667413db66459661701ccbb20c6dd89b15aa35bd;hp=fb23570f78156124d5cb5a0f33aa7786ab9d0736;hpb=1e034c70105f0926939acaaa27ddb46e904ae8bf;p=clitk.git diff --git a/common/clitkDicomRT_StructureSet.cxx b/common/clitkDicomRT_StructureSet.cxx index fb23570..2d5abd1 100644 --- a/common/clitkDicomRT_StructureSet.cxx +++ b/common/clitkDicomRT_StructureSet.cxx @@ -4,7 +4,7 @@ Authors belongs to: - University of LYON http://www.universite-lyon.fr/ - - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + - Léon Bérard cancer center http://www.centreleonberard.fr - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr This software is distributed WITHOUT ANY WARRANTY; without even @@ -19,6 +19,11 @@ #include "clitkDicomRT_StructureSet.h" #include +#include "gdcmFile.h" +#if GDCM_MAJOR_VERSION == 2 +#include "gdcmReader.h" +#include "gdcmAttribute.h" +#endif //-------------------------------------------------------------------- clitk::DicomRT_StructureSet::DicomRT_StructureSet() @@ -37,7 +42,6 @@ clitk::DicomRT_StructureSet::DicomRT_StructureSet() //-------------------------------------------------------------------- clitk::DicomRT_StructureSet::~DicomRT_StructureSet() { - } //-------------------------------------------------------------------- @@ -99,7 +103,7 @@ const std::string & clitk::DicomRT_StructureSet::GetTime() const //-------------------------------------------------------------------- -const std::vector & clitk::DicomRT_StructureSet::GetListOfROI() const +const std::vector & clitk::DicomRT_StructureSet::GetListOfROI() const { return mListOfROI; } @@ -113,8 +117,8 @@ clitk::DicomRT_ROI* clitk::DicomRT_StructureSet::GetROI(int n) std::cerr << "No ROI number " << n << std::endl; return NULL; } - DD(mListOfROI[mMapOfROIIndex[n]]->GetName()); - DD(mListOfROI[mMapOfROIIndex[n]]->GetROINumber()); + // DD(mListOfROI[mMapOfROIIndex[n]]->GetName()); + //DD(mListOfROI[mMapOfROIIndex[n]]->GetROINumber()); return mListOfROI[mMapOfROIIndex[n]]; } //-------------------------------------------------------------------- @@ -137,10 +141,125 @@ void clitk::DicomRT_StructureSet::Print(std::ostream & os) const //-------------------------------------------------------------------- +//-------------------------------------------------------------------- +void clitk::DicomRT_StructureSet::Write(const std::string & filename) +{ +#if GDCM_MAJOR_VERSION == 2 + DD("WRITE TODO"); +#else + FATAL("Sorry not compatible with GDCM1, use GDCM2"); +#endif +} +//-------------------------------------------------------------------- + + //-------------------------------------------------------------------- void clitk::DicomRT_StructureSet::Read(const std::string & filename) { // Open DICOM +#if GDCM_MAJOR_VERSION == 2 + gdcm::Reader reader; + reader.SetFileName(filename.c_str()); + reader.Read(); + + const gdcm::File & file = reader.GetFile(); + const gdcm::DataSet & ds = file.GetDataSet(); + + // Check file type + //Verify if the file is a RT-Structure-Set dicom file + gdcm::MediaStorage ms; + ms.SetFromFile(file); + if( ms != gdcm::MediaStorage::RTStructureSetStorage ) + { + std::cerr << "Error. the file " << filename + << " 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])" + << std::endl; + exit(0); + } + + gdcm::Attribute<0x8,0x60> modality; + modality.SetFromDataSet( ds ); + if( modality.GetValue() != "RTSTRUCT" ) + { + std::cerr << "Error. the file " << filename + << " is not a Dicom Struct ? (must have 0x0008,0x0060 = RTSTRUCT [RT Structure Set Storage])" + << std::endl; + exit(0); + } + + // Read global info + gdcm::Attribute<0x20,0x10> studyid; + studyid.SetFromDataSet( ds ); + gdcm::Attribute<0x8,0x20> studytime; + studytime.SetFromDataSet( ds ); + gdcm::Attribute<0x8,0x30> studydate; + studydate.SetFromDataSet( ds ); + gdcm::Attribute<0x3006,0x02> label; + label.SetFromDataSet( ds ); + gdcm::Attribute<0x3006,0x04> atname; + atname.SetFromDataSet( ds ); + gdcm::Attribute<0x3006,0x09> time; + time.SetFromDataSet( ds ); + + mStudyID = studyid.GetValue(); + mStudyTime = studytime.GetValue(); + mStudyDate = studydate.GetValue(); + mLabel = label.GetValue(); + mName = atname.GetValue(); + mTime = time.GetValue(); + + //---------------------------------- + // Read all ROI Names and number + // 0x3006,0x0020 = [ Structure Set ROI Sequence ] + gdcm::Tag tssroisq(0x3006,0x0020); + const gdcm::DataElement &ssroisq = ds.GetDataElement( tssroisq ); + gdcm::SmartPointer roi_seq = ssroisq.GetValueAsSQ(); + assert(roi_seq); // TODO error message + for(unsigned int ridx = 0; ridx < roi_seq->GetNumberOfItems(); ++ridx) + { + gdcm::Item & item = roi_seq->GetItem( ridx + 1); // Item starts at 1 + const gdcm::DataSet& nestedds = item.GetNestedDataSet(); + + gdcm::Attribute<0x3006,0x26> roiname; + roiname.SetFromDataSet( nestedds ); + std::string name = roiname.GetValue(); // 0x3006,0x0026 = [ROI Name] + gdcm::Attribute<0x3006,0x0022> roinumber; + roinumber.SetFromDataSet( nestedds ); + int nb = roinumber.GetValue(); // 0x3006,0x0022 = [ROI Number] + // Change number if needed + + //TODO + + // Check if such a number already exist + if (mMapOfROIName.find(nb) != mMapOfROIName.end()) { + std::cerr << "WARNING. A Roi already exist with the number " + << nb << ". I replace." << std::endl; + } + // Add in map + mMapOfROIName[nb] = name; + } + // DD(mMapOfROIName.size()); + + //---------------------------------- + // Read all ROI + // 0x3006,0x0039 = [ ROI Contour Sequence ] + gdcm::Tag troicsq(0x3006,0x0039); + const gdcm::DataElement &roicsq = ds.GetDataElement( troicsq ); + gdcm::SmartPointer roi_contour_seq = roicsq.GetValueAsSQ(); + assert(roi_contour_seq); // TODO error message + int n=0; + for(unsigned int ridx = 0; ridx < roi_contour_seq->GetNumberOfItems(); ++ridx) + { + gdcm::Item & item = roi_contour_seq->GetItem( ridx + 1); // Item starts at 1 + + DicomRT_ROI::Pointer roi = DicomRT_ROI::New(); + roi->Read(mMapOfROIName, item); + mListOfROI.push_back(roi); + mMapOfROIIndex[roi->GetROINumber()] = n; + n++; + } + +#else gdcm::File reader; reader.SetFileName(filename.c_str()); reader.SetMaxSizeLoadEntry(16384); // Needed ... @@ -167,7 +286,12 @@ void clitk::DicomRT_StructureSet::Read(const std::string & filename) mStudyTime = reader.GetValEntry(0x008,0x0020)->GetValue(); mStudyDate = reader.GetValEntry(0x008,0x0030)->GetValue(); mLabel = reader.GetValEntry(0x3006,0x002)->GetValue(); - mName = reader.GetValEntry(0x3006,0x004)->GetValue(); + if (!reader.GetValEntry(0x3006,0x004)) { + mName = "Anonymous"; + } + else { + mName = reader.GetValEntry(0x3006,0x004)->GetValue(); + } mTime = reader.GetValEntry(0x3006,0x009)->GetValue(); //---------------------------------- @@ -199,36 +323,37 @@ void clitk::DicomRT_StructureSet::Read(const std::string & filename) assert(roi_contour_seq); // TODO error message int n=0; for (gdcm::SQItem* r=roi_contour_seq->GetFirstSQItem(); r!=0; r=roi_contour_seq->GetNextSQItem()) { - DicomRT_ROI * roi = new DicomRT_ROI; + DicomRT_ROI::Pointer roi = DicomRT_ROI::New(); roi->Read(mMapOfROIName, r); mListOfROI.push_back(roi); mMapOfROIIndex[roi->GetROINumber()] = n; n++; } +#endif } //-------------------------------------------------------------------- //-------------------------------------------------------------------- -int clitk::DicomRT_StructureSet::AddBinaryImageAsNewROI(vvImage::Pointer im, std::string n) +int clitk::DicomRT_StructureSet::AddBinaryImageAsNewROI(vvImage * im, std::string n) { - DD("AddBinaryImageAsNewROI"); + //DD("AddBinaryImageAsNewROI"); // Search max ROI number int max = -1; for(unsigned int i=0; iGetROINumber() > max) max = mListOfROI[i]->GetROINumber(); } - DD(max); + // DD(max); ++max; - DD(max); + //DD(max); // Compute name std::ostringstream oss; - oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(n)) - << "_roi_" << max << vtksys::SystemTools::GetFilenameLastExtension(n); - DD(oss.str()); + oss << vtksys::SystemTools::GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(n)); + // << "_roi_" << max << vtksys::SystemTools::GetFilenameLastExtension(n); + //DD(oss.str()); mMapOfROIName[max] = oss.str(); // Set color @@ -238,14 +363,11 @@ int clitk::DicomRT_StructureSet::AddBinaryImageAsNewROI(vvImage::Pointer im, std color.push_back(0); // Create ROI - DicomRT_ROI * roi = new DicomRT_ROI; - roi->SetFromBinaryImage(im, - max, - oss.str(), - color); + DicomRT_ROI::Pointer roi = DicomRT_ROI::New(); + roi->SetFromBinaryImage(im, max, oss.str(), color, n); mListOfROI.push_back(roi); mMapOfROIIndex[mListOfROI.size()-1] = max; - DD(mMapOfROIIndex[mListOfROI.size()-1]); + //DD(mMapOfROIIndex[mListOfROI.size()-1]); return max; } //--------------------------------------------------------------------