diff --git a/code/core/rttbUtils.cpp b/code/core/rttbUtils.cpp index 853f5c6..28e9988 100644 --- a/code/core/rttbUtils.cpp +++ b/code/core/rttbUtils.cpp @@ -1,66 +1,72 @@ // ----------------------------------------------------------------------- // RTToolbox - DKFZ radiotherapy quantitative evaluation library // // Copyright (c) German Cancer Research Center (DKFZ), // Software development for Integrated Diagnostics and Therapy (SIDT). // ALL RIGHTS RESERVED. // See rttbCopyright.txt or // http://www.dkfz.de/en/sidt/projects/rttb/copyright.html // // This software is distributed WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the above copyright notices for more information. // //------------------------------------------------------------------------ -/*! -// @file -// @version $Revision: 1674 $ (last changed revision) -// @date $Date: 2017-01-27 10:34:46 +0100 (Fr, 27 Jan 2017) $ (last change date) -// @author $Author: hentsch $ (last changed by) -*/ #include #include namespace rttb { + namespace core { - bool isKey(const std::map& values, const double value) { - for (auto const& collectionElements : values) { - if (std::abs(collectionElements.first - value) <= errorConstant) - { - return true; - } + + bool isKey(const std::map& values, const double value) { + for (auto const& collectionElements : values) { + if (std::abs(collectionElements.first - value) <= errorConstant) { + return true; } - return false; } - bool isKey(const std::vector& values, double value) { - for (auto const& collectionElement : values) { - if (std::abs(collectionElement - value) <= errorConstant) - { - return true; - } + return false; + } + + bool isKey(const std::vector& values, double value) { + for (auto const& collectionElement : values) { + if (std::abs(collectionElement - value) <= errorConstant) { + return true; } - return false; } + return false; + } bool valueIsClose(double value1, double value2, double specificErrorConstant) { - return std::abs(value1 - value2) < specificErrorConstant; + return std::abs(value1 - value2) < specificErrorConstant; } + bool isFile(FileNameType aName) { + boost::filesystem::path path = boost::filesystem::path(aName); + + return (boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)); + } + + bool isDirectory(FileNameType aName) { + boost::filesystem::path path = boost::filesystem::path(aName); + + return (boost::filesystem::exists(path) && boost::filesystem::is_directory(path)); + } - std::string getFilenameWithoutEnding(const std::string& outfilename) - { + std::string getFilenameWithoutEnding(const std::string& outfilename) { boost::filesystem::path p(outfilename); + return p.replace_extension("").string(); } - std::string getFileEnding(const std::string& outfilename) - { + std::string getFileEnding(const std::string& outfilename) { boost::filesystem::path p(outfilename); + return p.extension().string(); } } } diff --git a/code/core/rttbUtils.h b/code/core/rttbUtils.h index 878b5f3..806d282 100644 --- a/code/core/rttbUtils.h +++ b/code/core/rttbUtils.h @@ -1,49 +1,47 @@ // ----------------------------------------------------------------------- // RTToolbox - DKFZ radiotherapy quantitative evaluation library // // Copyright (c) German Cancer Research Center (DKFZ), // Software development for Integrated Diagnostics and Therapy (SIDT). // ALL RIGHTS RESERVED. // See rttbCopyright.txt or // http://www.dkfz.de/en/sidt/projects/rttb/copyright.html // // This software is distributed WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the above copyright notices for more information. // //------------------------------------------------------------------------ -/*! -// @file -// @version $Revision: 1674 $ (last changed revision) -// @date $Date: 2017-01-27 10:34:46 +0100 (Fr, 27 Jan 2017) $ (last change date) -// @author $Author: hentsch $ (last changed by) -*/ #ifndef __RTTB_UTILS_H #define __RTTB_UTILS_H #include #include #include #include "RTTBCoreExports.h" namespace rttb { namespace core { - bool RTTBCore_EXPORT isKey(const std::map& values, double value); + bool RTTBCore_EXPORT isKey(const std::map& values, double value); bool RTTBCore_EXPORT isKey(const std::vector& values, double value); bool RTTBCore_EXPORT valueIsClose(double value1, double value2, double specificErrorConstant = 1e-5); + bool RTTBCore_EXPORT isFile(FileNameType aName); + + bool RTTBCore_EXPORT isDirectory(FileNameType aName); + std::string RTTBCore_EXPORT getFilenameWithoutEnding(const std::string& outfilename); std::string RTTBCore_EXPORT getFileEnding(const std::string& outfilename); } } #endif diff --git a/code/io/dicom/rttbDicomFileDoseAccessorGenerator.cpp b/code/io/dicom/rttbDicomFileDoseAccessorGenerator.cpp index d6a685c..422d923 100644 --- a/code/io/dicom/rttbDicomFileDoseAccessorGenerator.cpp +++ b/code/io/dicom/rttbDicomFileDoseAccessorGenerator.cpp @@ -1,108 +1,103 @@ // ----------------------------------------------------------------------- // RTToolbox - DKFZ radiotherapy quantitative evaluation library // // Copyright (c) German Cancer Research Center (DKFZ), // Software development for Integrated Diagnostics and Therapy (SIDT). // ALL RIGHTS RESERVED. // See rttbCopyright.txt or // http://www.dkfz.de/en/sidt/projects/rttb/copyright.html // // This software is distributed WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the above copyright notices for more information. // //------------------------------------------------------------------------ -/*! -// @file -// @version $Revision$ (last changed revision) -// @date $Date$ (last change date) -// @author $Author$ (last changed by) -*/ #include #include #include "rttbDicomFileDoseAccessorGenerator.h" #include "rttbDicomDoseAccessor.h" #include "rttbNullPointerException.h" #include "rttbInvalidDoseException.h" #include "rttbDcmrtException.h" #include "rttbIndexOutOfBoundsException.h" #include "rttbDicomFileReaderHelper.h" #include "rttbInvalidParameterException.h" +#include "rttbUtils.h" namespace rttb { namespace io { namespace dicom { DicomFileDoseAccessorGenerator::~DicomFileDoseAccessorGenerator() = default; DicomFileDoseAccessorGenerator::DicomFileDoseAccessorGenerator(FileNameType aDICOMRTDoseFileName) { _dicomDoseFileName = aDICOMRTDoseFileName; } core::DoseAccessorGeneratorInterface::DoseAccessorPointer DicomFileDoseAccessorGenerator::generateDoseAccessor() { std::vector fileVector; //if a file - if (isFile(_dicomDoseFileName)) + if (core::isFile(_dicomDoseFileName)) { fileVector.push_back(_dicomDoseFileName); } //if a directory - else if (isDirectory(_dicomDoseFileName)) + else if (core::isDirectory(_dicomDoseFileName)) { rttb::io::dicom::Modality doseModality = {rttb::io::dicom::Modality::RTDOSE}; fileVector = getFileNamesWithSameUID(_dicomDoseFileName, doseModality); } else { throw rttb::core::InvalidParameterException("Invalid file/directory name!"); } if (fileVector.size() < 1) { throw rttb::core::InvalidParameterException("There is no structure set files in the directory!"); } OFCondition status; DcmFileFormat fileformat; DRTDoseIODPtr dose = boost::make_shared(); status = fileformat.loadFile(fileVector.at(0).c_str()); if (!status.good()) { std::cerr << "Error: load rtdose loadFile(" << fileVector.at(0) << ") failed!" << std::endl; throw core::InvalidDoseException("Invalid dicom dose!"); } DcmItemPtr dataSet = boost::make_shared(*fileformat.getDataset()); status = dose->read(*dataSet); if (!status.good()) { std::cerr << "Error: read DRTDoseIOD failed!" << std::endl; throw core::InvalidDoseException("Invalid dicom dose!"); } _doseAccessor = boost::make_shared(dose, dataSet); return _doseAccessor; } } } } diff --git a/code/io/dicom/rttbDicomFileReaderHelper.cpp b/code/io/dicom/rttbDicomFileReaderHelper.cpp index 703d41d..cace20d 100644 --- a/code/io/dicom/rttbDicomFileReaderHelper.cpp +++ b/code/io/dicom/rttbDicomFileReaderHelper.cpp @@ -1,251 +1,219 @@ // ----------------------------------------------------------------------- // RTToolbox - DKFZ radiotherapy quantitative evaluation library // // Copyright (c) German Cancer Research Center (DKFZ), // Software development for Integrated Diagnostics and Therapy (SIDT). // ALL RIGHTS RESERVED. // See rttbCopyright.txt or // http://www.dkfz.de/en/sidt/projects/rttb/copyright.html // // This software is distributed WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the above copyright notices for more information. // //------------------------------------------------------------------------ -/*! -// @file -// @version $Revision$ (last changed revision) -// @date $Date$ (last change date) -// @author $Author$ (last changed by) -*/ #include "rttbDicomFileReaderHelper.h" #include #include #include #include "boost/filesystem/operations.hpp" #include "boost/filesystem/path.hpp" #include "rttbDcmrtException.h" #include "rttbInvalidParameterException.h" +#include "rttbUtils.h" namespace rttb { namespace io { namespace dicom { - bool isFile(FileNameType aName) - { - boost::filesystem::path path = boost::filesystem::path(aName); - - if (boost::filesystem::exists(path) && boost::filesystem::is_regular_file(path)) - { - return true; - } - else - { - return false; - } - } - - bool isDirectory(FileNameType aName) - { - boost::filesystem::path path = boost::filesystem::path(aName); - - if (boost::filesystem::exists(path) && boost::filesystem::is_directory(path)) - { - return true; - } - else - { - return false; - } - } OFString getModality(DcmDataSetPtr aDcmDataSet) { OFString modality; OFCondition status; status = aDcmDataSet->findAndGetOFString(DcmTagKey(0x0008, 0x0060), modality); if (!status.good()) { throw DcmrtException("Error: get modality failed!"); } return modality; } OFString getUID(DcmDataSetPtr aDcmDataSet) { OFString uid; OFCondition status; status = aDcmDataSet->findAndGetOFString(DcmTagKey(0x0020, 0x000e), uid); if (!status.good()) { throw DcmrtException("Error: get uid failed!"); } return uid; } std::vector getFileNamesWithSameUID(FileNameType aDirName, Modality aModality) { std::vector fileNameVector; std::string modalityStrArray[] = {"RTDOSE", "RTSTRUCT", "RTPLAN"}; boost::filesystem::path path = boost::filesystem::path(aDirName); OFCondition status; DcmFileFormat fileformat; IDType uid = ""; DcmDataSetPtr datasetPtr; if (aModality.Type != 1 && aModality.Type != 2 && aModality.Type != 3) { throw core::InvalidParameterException("Error: invalid modality! The modality should be RTDOSE(1)/RTSTRUCT(2)/RTPLAN(3)."); } //if a directory - if (isDirectory(aDirName)) + if (core::isDirectory(aDirName)) { boost::filesystem::directory_iterator end_iter; bool isFirst = true; for (boost::filesystem::directory_iterator dir_itr(path); dir_itr != end_iter; ++dir_itr) { if (boost::filesystem::is_regular_file(dir_itr->status())) { boost::filesystem::path filePath(dir_itr->path().filename().string()); filePath = boost::filesystem::system_complete(dir_itr->path()); status = fileformat.loadFile(filePath.string().c_str()); if (!status.good()) { throw DcmrtException("Error: load dose file " + filePath.string() + " failed!"); } datasetPtr = boost::make_shared(*fileformat.getDataset()); OFString modalityOFS = getModality(datasetPtr); for (unsigned int i = 0; i < 3; i++) { if (aModality.Type == (i + 1) && modalityOFS == modalityStrArray[i].c_str()) { OFString currentUID = getUID(datasetPtr); //get the first uid of the given modality if (isFirst) { uid = currentUID.c_str(); isFirst = false; } if (uid == currentUID.c_str()) { fileNameVector.emplace_back(filePath.string().c_str()); } break; } } } } } - else if (isFile(aDirName)) + else if (core::isFile(aDirName)) { std::cout << "Important: the given name " + aDirName + " is a file name, not a directory name. Given modality will be ignored, use the modality of the file." << std::endl; fileNameVector = getFileNames(aDirName); } else { throw core::InvalidParameterException("Error: file/directory does not exist!"); } return fileNameVector; } std::vector getFileNames(FileNameType aFileName) { - if (!isFile(aFileName)) + if (!core::isFile(aFileName)) { throw core::InvalidParameterException("Error: file does not exist!"); } std::vector fileNameVector; boost::filesystem::path path = boost::filesystem::path(aFileName); OFCondition status; DcmFileFormat fileformat; DcmDataSetPtr datasetPtr; OFString modality;//modality of the given file OFString uid;//uid of the given file status = fileformat.loadFile(aFileName.c_str()); if (!status.good()) { throw DcmrtException("Error: fileformat.loadFile failed!"); } datasetPtr = boost::make_shared(*fileformat.getDataset()); modality = getModality(datasetPtr); uid = getUID(datasetPtr); //get parent directory boost::filesystem::path parentDir = path.parent_path(); if (boost::filesystem::is_directory(parentDir)) { boost::filesystem::directory_iterator end_iter; for (boost::filesystem::directory_iterator dir_itr(parentDir); dir_itr != end_iter; ++dir_itr) { if (boost::filesystem::is_regular_file(dir_itr->status())) { boost::filesystem::path currentFilePath(dir_itr->path().filename().string()); currentFilePath = boost::filesystem::system_complete(dir_itr->path()); status = fileformat.loadFile(currentFilePath.string().c_str()); if (!status.good()) { throw DcmrtException("Error: load dose fileformat.loadFile failed!"); } datasetPtr = boost::make_shared(*fileformat.getDataset()); OFString currentModality = getModality(datasetPtr); OFString currentUID = getUID(datasetPtr); //if the same modality if (modality == currentModality && uid == currentUID) { fileNameVector.emplace_back(currentFilePath.string().c_str()); } } } } return fileNameVector; } } } } diff --git a/code/io/dicom/rttbDicomFileReaderHelper.h b/code/io/dicom/rttbDicomFileReaderHelper.h index ab55f3e..63e067a 100644 --- a/code/io/dicom/rttbDicomFileReaderHelper.h +++ b/code/io/dicom/rttbDicomFileReaderHelper.h @@ -1,88 +1,75 @@ // ----------------------------------------------------------------------- // RTToolbox - DKFZ radiotherapy quantitative evaluation library // // Copyright (c) German Cancer Research Center (DKFZ), // Software development for Integrated Diagnostics and Therapy (SIDT). // ALL RIGHTS RESERVED. // See rttbCopyright.txt or // http://www.dkfz.de/en/sidt/projects/rttb/copyright.html // // This software is distributed WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the above copyright notices for more information. // //------------------------------------------------------------------------ -/*! -// @file -// @version $Revision$ (last changed revision) -// @date $Date$ (last change date) -// @author $Author$ (last changed by) -*/ + #ifndef __DICOM_FILE_READER_HELPER_H #define __DICOM_FILE_READER_HELPER_H #include #include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */ #include "drtdose.h" #include "rttbBaseType.h" #include "boost/shared_ptr.hpp" namespace rttb { namespace io { namespace dicom { struct Modality { enum Type { RTDOSE = 1, RTSTRUCT = 2, RTPLAN = 3, UserDefined = 128 } Type; }; using DRTDoseIODPtr = boost::shared_ptr; using DcmDataSetPtr = boost::shared_ptr; /*! Return the vector of all files with the same UID in the given directory, the UID is defined by the first file with the modality. @exception InvalidParameterException thrown if the file/directory does not exist or the modality is invalid @exception DcmrtException thrown if load/read file failed */ std::vector getFileNamesWithSameUID(FileNameType aDirName, Modality aModality); /*! Return the vector of all files with the same UID in the directory of the given file @exception InvalidParameterException thrown if the file does not exist @exception DcmrtException thrown if load/read file failed */ std::vector getFileNames(FileNameType aFileName); - /*! Return if the given name is a file - */ - bool isFile(FileNameType aName); - - /*! Return if the given name is a directory - */ - bool isDirectory(FileNameType aName); - /*! Return modality DcmTagKey(0x0008, 0x0060) @exception DcmrtException thrown if reading modality failed*/ OFString getModality(DcmDataSetPtr aDcmDataSet); /*! Return uid DcmTagKey(0x0020, 0x000e) @exception DcmrtException thrown if reading uid failed*/ OFString getUID(DcmDataSetPtr aDcmDataSet); }; } } #endif diff --git a/code/io/dicom/rttbDicomFileStructureSetGenerator.cpp b/code/io/dicom/rttbDicomFileStructureSetGenerator.cpp index f7e31e3..05252d8 100644 --- a/code/io/dicom/rttbDicomFileStructureSetGenerator.cpp +++ b/code/io/dicom/rttbDicomFileStructureSetGenerator.cpp @@ -1,110 +1,104 @@ // ----------------------------------------------------------------------- // RTToolbox - DKFZ radiotherapy quantitative evaluation library // // Copyright (c) German Cancer Research Center (DKFZ), // Software development for Integrated Diagnostics and Therapy (SIDT). // ALL RIGHTS RESERVED. // See rttbCopyright.txt or // http://www.dkfz.de/en/sidt/projects/rttb/copyright.html // // This software is distributed WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the above copyright notices for more information. // //------------------------------------------------------------------------ -/*! -// @file -// @version $Revision$ (last changed revision) -// @date $Date$ (last change date) -// @author $Author$ (last changed by) -*/ #include #include #include #include #include "rttbInvalidParameterException.h" #include "rttbStructure.h" #include "rttbDicomFileStructureSetGenerator.h" #include "rttbDicomIODStructureSetGenerator.h" #include "rttbDcmrtException.h" #include "rttbDicomFileReaderHelper.h" +#include "rttbUtils.h" namespace rttb { namespace io { namespace dicom { DicomFileStructureSetGenerator::DicomFileStructureSetGenerator(DICOMRTFileNameString aDICOMRTStrSetFileName) { _fileName = aDICOMRTStrSetFileName; } - DicomFileStructureSetGenerator::~DicomFileStructureSetGenerator() - = default; + DicomFileStructureSetGenerator::~DicomFileStructureSetGenerator() = default; DicomFileStructureSetGenerator::StructureSetPointer DicomFileStructureSetGenerator::generateStructureSet() { std::vector fileVector; //if a file - if (isFile(_fileName)) + if (core::isFile(_fileName)) { fileVector.push_back(_fileName); } //if a directory - else if (isDirectory(_fileName)) + else if (core::isDirectory(_fileName)) { rttb::io::dicom::Modality strModality = {rttb::io::dicom::Modality::RTSTRUCT}; fileVector = getFileNamesWithSameUID(_fileName, strModality); } else { throw rttb::core::InvalidParameterException("Invalid file/directory name!"); } if (fileVector.size() < 1) { throw rttb::core::InvalidParameterException("There is no structure set files in the directory!"); } OFCondition status; DcmFileFormat fileformat; DRTStrSetIODPtr drtStrSetIODPtr = boost::make_shared(); //get the first structure set file status = fileformat.loadFile(fileVector.at(0).c_str()); if (!status.good()) { throw DcmrtException("Load rt structure set loadFile() failed!"); } status = drtStrSetIODPtr->read(*fileformat.getDataset()); if (!status.good()) { throw DcmrtException("Read DRTStructureSetIOD DRTStructureSetIOD.read() failed!"); } - io::dicom::DicomIODStructureSetGenerator iodGenerator(drtStrSetIODPtr); - iodGenerator.setStructureLabelFilterActive(this->getStructureLabelFilterActive()); - iodGenerator.setFilterRegEx(this->getFilterRegEx()); + io::dicom::DicomIODStructureSetGenerator iodGenerator(drtStrSetIODPtr); + iodGenerator.setStructureLabelFilterActive(this->getStructureLabelFilterActive()); + iodGenerator.setFilterRegEx(this->getFilterRegEx()); return iodGenerator.generateStructureSet(); } }//end namespace dicom }//end namespace io }//end namespace rttb