diff --git a/code/io/other/rttbDVHTxtFileReader.cpp b/code/io/other/rttbDVHTxtFileReader.cpp index 79b90c2..1ca0b75 100644 --- a/code/io/other/rttbDVHTxtFileReader.cpp +++ b/code/io/other/rttbDVHTxtFileReader.cpp @@ -1,219 +1,255 @@ // ----------------------------------------------------------------------- // 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 +#include +#include #include "rttbDVHTxtFileReader.h" #include "rttbInvalidParameterException.h" #include "rttbBaseType.h" namespace rttb { namespace io { namespace other { DVHTxtFileReader::DVHTxtFileReader(FileNameString aFileName) { _fileName = aFileName; _resetFile = true; } DVHTxtFileReader::~DVHTxtFileReader() {} void DVHTxtFileReader::resetFileName(FileNameString aFileName) { _fileName = aFileName; _resetFile = true; } void DVHTxtFileReader::createDVH() { std::ifstream dvh_ifstr(this->_fileName.c_str(), std::ios::in); std::string structureLabel; std::string dvhType; unsigned int numberOfBins; DoseTypeGy prescribedDose; double estimated_max_dose_prescribed_dose_ratio; std::deque dataDifferential; std::deque dataCumulative; DoseTypeGy deltaD = 0; DoseVoxelVolumeType deltaV = 0; IDType strID; - IDType doseID; + IDType doseID; + + enum dataTypes { _deltaV, _deltaD, _strID, _doseID, _numberOfBins, _dvhType, _prescribedDose, _estimated_max_dose_prescribed_dose_ratio}; + std::map mapTypes; + + mapTypes["DeltaV"] = _deltaV; + mapTypes["DeltaD"] = _deltaD; + mapTypes["StructureID"] = _strID; + mapTypes["DoseID"] = _doseID; + mapTypes["Number of bins"] = _numberOfBins; + mapTypes["DVH Type"] = _dvhType; + mapTypes["Prescribed Dose"] = _prescribedDose; + mapTypes["Estimated_max_dose_prescribed_dose_ratio"] = _estimated_max_dose_prescribed_dose_ratio; + + bool isDifferential = false; if (!dvh_ifstr.is_open()) { throw core::InvalidParameterException("DVH file name invalid: could not open the dvh file!"); } else { bool data_begin = false; while (!dvh_ifstr.eof()) { std::string line; std::getline(dvh_ifstr, line); - if (line.find("DVH Data:") != std::string::npos) + if (!data_begin) { - data_begin = true; - } + std::vector buffer; + boost::split(buffer, line, boost::is_any_of(":")); - if (data_begin && (line.find(",") != std::string::npos)) - { - std::stringstream ss(line.substr(line.find(",") + 1)); - DoseCalcType dvh_i; - ss >> dvh_i; + if (buffer.size() != 2) + { + throw core::InvalidParameterException("Error while splitting the line..."); + } - if (dvhType == "DIFFERENTIAL") + if (buffer.at(0) == "DVH Data") { - dataDifferential.push_back(dvh_i); + data_begin = true; } - else if (dvhType == "CUMULATIVE") + else { - dataCumulative.push_back(dvh_i); + std::string key = buffer.at(0); + std::string value = buffer.at(1); + + boost::trim(key); + boost::trim(value); + + switch (mapTypes.at(key)) + { + case _deltaD: + deltaD = boost::lexical_cast(value); + break; + case _deltaV: + deltaV = boost::lexical_cast(value); + break; + case _strID: + strID = value; + break; + case _doseID: + doseID = value; + break; + case _numberOfBins: + numberOfBins = boost::lexical_cast(value); + case _dvhType: + dvhType = value; + if (dvhType == "DIFFERENTIAL") + { + isDifferential = true; + } + break; + case _prescribedDose: + prescribedDose = boost::lexical_cast(value); + break; + case _estimated_max_dose_prescribed_dose_ratio: + estimated_max_dose_prescribed_dose_ratio = boost::lexical_cast(value); + break; + } } } - - if (line.find("DeltaD: ") != std::string::npos) - { - std::stringstream ss(line.substr(8)); - ss >> deltaD; - } - - if (line.find("DeltaV: ") != std::string::npos) - { - std::stringstream ss(line.substr(8)); - ss >> deltaV; - } - - if (line.find("StructureID: ") != std::string::npos) - { - std::stringstream ss(line.substr(13)); - ss >> strID; - } - - if (line.find("DoseID: ") != std::string::npos) - { - std::stringstream ss(line.substr(8)); - ss >> doseID; - } - - if (line.find("Number of bins: ") != std::string::npos) - { - std::stringstream ss(line.substr(16)); - ss >> numberOfBins; - } - - if (line.find("Structure Label: ") != std::string::npos) - { - std::stringstream ss(line.substr(17)); - ss >> structureLabel; - } - - if (line.find("DVH Type: ") != std::string::npos) - { - std::stringstream ss(line.substr(10)); - ss >> dvhType; - } - - if (line.find("Prescribed Dose: ") != std::string::npos) - { - std::stringstream ss(line.substr(17)); - ss >> prescribedDose; - } - - if (line.find("Estimated_max_dose_prescribed_dose_ratio: ") != std::string::npos) + else { - std::stringstream ss(line.substr(42)); - ss >> estimated_max_dose_prescribed_dose_ratio; - } + if (line.empty()) + { + break; + } + else + { + if (isDifferential) + { + loadData(line, dataDifferential); + } + else + { + loadData(line, dataCumulative); + } + } + } } } numberOfBins = static_cast(std::max(dataDifferential.size(), dataCumulative.size())); - if (dvhType == "CUMULATIVE") - { - DoseCalcType differentialDVHi = 0; - std::deque::iterator it; - - for (it = dataCumulative.begin(); it != dataCumulative.end(); ++it) - { - if (dataDifferential.size() == numberOfBins - 1) - { - differentialDVHi = *it; - } - else - { - differentialDVHi = *it - *(it + 1); - } - - dataDifferential.push_back(differentialDVHi); - } - } - if (numberOfBins == 0) { throw core::InvalidParameterException("Invalid dvh file: empty dvh data!"); } + if (!isDifferential) + { + calculateDataCumulative(dataCumulative, dataDifferential, numberOfBins); + } + if (deltaD == 0) { deltaD = prescribedDose * estimated_max_dose_prescribed_dose_ratio / numberOfBins; - } + if (deltaD == 0) + { + throw core::InvalidParameterException("Invalid dvh file: deltaD or deltaV must not be zero!"); + } + } + if (deltaV == 0) { deltaV = 0.027; } - if (deltaD == 0 || deltaV == 0) - { - throw core::InvalidParameterException("Invalid dvh file: deltaD or deltaV must not be zero!"); - } - _dvh = boost::make_shared(dataDifferential, deltaD, deltaV, strID, doseID); _resetFile = false; } - + DVHTxtFileReader::DVHPointer DVHTxtFileReader::generateDVH() { if (_resetFile) { this->createDVH(); } return _dvh; } + + void DVHTxtFileReader::calculateDataCumulative(std::deque& dataCumulative, + std::deque& dataDifferential, unsigned int numberOfBins) + { + DoseCalcType differentialDVHi = 0; + std::deque::iterator it; + + for (it = dataCumulative.begin(); it != dataCumulative.end(); ++it) + { + if (dataDifferential.size() == numberOfBins - 1) + { + differentialDVHi = *it; + } + else + { + differentialDVHi = *it - *(it + 1); + } + + dataDifferential.push_back(differentialDVHi); + } + } + + void DVHTxtFileReader::loadData(const std::string& line, std::deque& data) + { + std::vector dataBuffer; + boost::split(dataBuffer, line, boost::is_any_of(",")); + + if (dataBuffer.size() != 2) + { + throw core::InvalidParameterException("Error while splitting the line..."); + } + + boost::trim(dataBuffer.at(1)); + + DoseCalcType dvh_i = boost::lexical_cast(dataBuffer.at(1)); + + data.push_back(dvh_i); + } }//end namespace other }//end namespace io }//end namespace rttb diff --git a/code/io/other/rttbDVHTxtFileReader.h b/code/io/other/rttbDVHTxtFileReader.h index 28f2e65..049d4a7 100644 --- a/code/io/other/rttbDVHTxtFileReader.h +++ b/code/io/other/rttbDVHTxtFileReader.h @@ -1,72 +1,79 @@ // ----------------------------------------------------------------------- // 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 __DVH_TXT_FILE_READER_H #define __DVH_TXT_FILE_READER_H #include "rttbBaseType.h" #include "rttbDVH.h" #include "rttbDVHGeneratorInterface.h" namespace rttb { namespace io { namespace other { /*! @class DVHTxtFileReader @brief Reads DVH data from txt files. */ class DVHTxtFileReader: public core::DVHGeneratorInterface { private: FileNameString _fileName; bool _resetFile; /*! @brief Create new DVH object using the info from dvh txt file - @exception InvalidParameterException Thrown if _fileName invalid + @exception InvalidParameterException Thrown if _fileName invalid */ void createDVH(); + void calculateDataCumulative(std::deque& dataCumulative, std::deque& dataDifferential, unsigned int numberOfBins); + + /*! @brief Load the DVH data either in a vector for differential data or in one for cumulative data + @exception InvalidParameterException Thrown if splitting of a line in the file is invalid + */ + void loadData(const std::string& line, std::deque& data); + public: /*! @brief Constructor. */ DVHTxtFileReader(FileNameString aFileName); ~DVHTxtFileReader(); /*! @brief Change file name. */ void resetFileName(FileNameString aFileName); /*! @brief Generate DVH, createDVH() will be called @return Return new shared pointer of DVH. @exception InvalidParameterException Thrown if _fileName invalid */ DVHPointer generateDVH(); }; } } } #endif