diff --git a/code/io/itk/rttbITKIOHelper.cpp b/code/io/itk/rttbITKIOHelper.cpp index 155a7c9..f9f56b6 100644 --- a/code/io/itk/rttbITKIOHelper.cpp +++ b/code/io/itk/rttbITKIOHelper.cpp @@ -1,171 +1,167 @@ // ----------------------------------------------------------------------- // 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. // //------------------------------------------------------------------------ #include "rttbITKIOHelper.h" #include "rttbException.h" #include "rttbInvalidDoseException.h" #include "rttbInvalidParameterException.h" namespace rttb { namespace io { namespace itk { - ITKImageType::Pointer readITKDoubleImage(FileNameType aITKImageFile) { - return readITKDoubleImage(aITKImageFile, false); - } - ITKImageType::Pointer readITKDoubleImage(FileNameType aITKImageFile, bool isDicom) { ITKImageType::Pointer itkDoubleImage; GenericImageReader::Pointer spReader = GenericImageReader::New(); if (isDicom) { spReader->setSeriesReadStyle(ImageSeriesReadStyle::Type::Dicom); } spReader->setFileName(aITKImageFile); GenericImageReader::GenericOutputImageType::Pointer itkGenericImage; ITKImageType::ConstPointer itkDoubleImageConst; try { unsigned int loadedDimensions; GenericImageReader::LoadedPixelType loadedPixelType; GenericImageReader::LoadedComponentType loadedComponentType; itkGenericImage = spReader->GetOutput(loadedDimensions, loadedPixelType, loadedComponentType); if (loadedDimensions != 3) { throw core::InvalidParameterException("image dimensions != 3. Only dim = 3 supported."); } if (loadedPixelType != ::itk::ImageIOBase::SCALAR) { throw core::InvalidParameterException("image component type != SCALAR. Only SCALAR supported."); } if (loadedComponentType == ::itk::ImageIOBase::DOUBLE) { itkDoubleImage = dynamic_cast<ITKImageType*>(itkGenericImage.GetPointer()); } else { itkDoubleImage = handleGenericImage(itkGenericImage, loadedComponentType); } if (itkDoubleImage.IsNull()) { throw core::InvalidDoseException("Error!!! unable to load input image. File is not existing or has an unsupported format."); } } catch (::itk::ExceptionObject& e) { std::cerr << "Error!!!" << std::endl; std::cerr << e << std::endl; throw rttb::core::InvalidDoseException(e.GetDescription()); } return itkDoubleImage; } ITKImageType::Pointer handleGenericImage( GenericImageReader::GenericOutputImageType* itkGenericImage, ::itk::ImageIOBase::IOComponentType& loadedComponentType) { ITKImageType::Pointer itkDoubleImage; switch (loadedComponentType) { case ::itk::ImageIOBase::UCHAR: { itkDoubleImage = doCasting<unsigned char>(itkGenericImage); break; } case ::itk::ImageIOBase::CHAR: { itkDoubleImage = doCasting<char>(itkGenericImage); break; } case ::itk::ImageIOBase::USHORT: { itkDoubleImage = doCasting<unsigned short>(itkGenericImage); break; } case ::itk::ImageIOBase::SHORT: { itkDoubleImage = doCasting<short>(itkGenericImage); break; } case ::itk::ImageIOBase::UINT: { itkDoubleImage = doCasting<unsigned int>(itkGenericImage); break; } case ::itk::ImageIOBase::INT: { itkDoubleImage = doCasting<int>(itkGenericImage); break; } case ::itk::ImageIOBase::ULONG: { itkDoubleImage = doCasting<unsigned long>(itkGenericImage); break; } case ::itk::ImageIOBase::LONG: { itkDoubleImage = doCasting<long>(itkGenericImage); break; } case ::itk::ImageIOBase::FLOAT: { itkDoubleImage = doCasting<float>(itkGenericImage); break; } case ::itk::ImageIOBase::DOUBLE: { itkDoubleImage = doCasting<double>(itkGenericImage); break; } default: { throw core::InvalidParameterException("image type unknown"); } } return itkDoubleImage; } }//end namespace itk }//end namespace io }//end namespace rttb diff --git a/code/io/itk/rttbITKIOHelper.h b/code/io/itk/rttbITKIOHelper.h index 81ccdc1..eb5598b 100644 --- a/code/io/itk/rttbITKIOHelper.h +++ b/code/io/itk/rttbITKIOHelper.h @@ -1,62 +1,60 @@ // ----------------------------------------------------------------------- // 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. // //------------------------------------------------------------------------ #ifndef __ITK_IO_HELPER_H #define __ITK_IO_HELPER_H #include "rttbBaseType.h" #include "rttbGenericImageReader.h" #include "itkImage.h" namespace rttb { namespace io { namespace itk { typedef ::itk::Image<GenericValueType, 3> ITKImageType; /*! @brief Read a itk image file into itkImage<DoseTypeGy,3> */ - ITKImageType::Pointer readITKDoubleImage(FileNameType aITKImageFile); - - ITKImageType::Pointer readITKDoubleImage(FileNameType aITKImageFile, bool isDicom); + ITKImageType::Pointer readITKDoubleImage(FileNameType aITKImageFile, bool isDicom = false); /*! @brief Converts a generic image to itkImage<DoseTypeGy,3> @param itkGenericImage the image read by GenericImageReader @param loadedComponentType the component type (used for casting later on) @exception InvalidParameterException if component type is not supported @sa GenericImageReader */ ITKImageType::Pointer handleGenericImage(GenericImageReader::GenericOutputImageType* itkGenericImage, ::itk::ImageIOBase::IOComponentType& loadedComponentType); /*! @brief Casts into itkImage<DoseTypeGy,3> */ template <typename TPixelType> ITKImageType::Pointer doCasting( GenericImageReader::GenericOutputImageType* genericImage); }//end namespace itk }//end namespace io }//end namespace rttb #include "rttbITKIOHelper.tpp" #endif diff --git a/code/io/itk/rttbITKImageFileAccessorGenerator.cpp b/code/io/itk/rttbITKImageFileAccessorGenerator.cpp index aba58d1..0e11d04 100644 --- a/code/io/itk/rttbITKImageFileAccessorGenerator.cpp +++ b/code/io/itk/rttbITKImageFileAccessorGenerator.cpp @@ -1,52 +1,49 @@ // ----------------------------------------------------------------------- // 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. // //------------------------------------------------------------------------ #include <boost/make_shared.hpp> #include <boost/shared_ptr.hpp> #include "rttbITKImageFileAccessorGenerator.h" #include "rttbITKImageAccessorConverter.h" #include "rttbITKIOHelper.h" namespace rttb { namespace io { namespace itk { ITKImageFileAccessorGenerator::~ITKImageFileAccessorGenerator() = default; - ITKImageFileAccessorGenerator::ITKImageFileAccessorGenerator(const FileNameType& fileName) + ITKImageFileAccessorGenerator::ITKImageFileAccessorGenerator(const FileNameType& fileName, const bool& useDicom) { _fileName = fileName; + _useDicom = useDicom; } rttb::core::DoseAccessorGeneratorBase::DoseAccessorPointer ITKImageFileAccessorGenerator::generateDoseAccessor() { - return ITKImageFileAccessorGenerator::generateDoseAccessor(false); - } - - rttb::core::DoseAccessorGeneratorBase::DoseAccessorPointer ITKImageFileAccessorGenerator::generateDoseAccessor(bool isDicom) { - _itkDoubleImage = readITKDoubleImage(_fileName, isDicom); + _itkDoubleImage = readITKDoubleImage(_fileName, _useDicom); _doseAccessor = boost::make_shared<ITKImageAccessor>(_itkDoubleImage.GetPointer()); return _doseAccessor; } }//end namespace itk }//end namespace io }//end namespace rttb diff --git a/code/io/itk/rttbITKImageFileAccessorGenerator.h b/code/io/itk/rttbITKImageFileAccessorGenerator.h index d408200..3425aa9 100644 --- a/code/io/itk/rttbITKImageFileAccessorGenerator.h +++ b/code/io/itk/rttbITKImageFileAccessorGenerator.h @@ -1,73 +1,73 @@ // ----------------------------------------------------------------------- // 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. // //------------------------------------------------------------------------ #ifndef __ITK_IMAGE_FILE_ACCESSOR_GENERATOR_H #define __ITK_IMAGE_FILE_ACCESSOR_GENERATOR_H #include "rttbDoseAccessorGeneratorBase.h" #include "rttbBaseType.h" #include "itkImage.h" namespace rttb { namespace io { namespace itk { /*! @class ITKImageFileAccessorGenerator @brief Load image data using the itk loading methods and wraps the resulting itk image in a ITKImageAccessor. * this can be used if dose distributions are stored in formats like meta image, nrrd... * @note it implies that the dose information is stored in absolute Gy values. */ class ITKImageFileAccessorGenerator: public core::DoseAccessorGeneratorBase { public: typedef ::itk::Image<GenericValueType, 3> ITKImageType; using DoseAccessorPointer = DoseAccessorGeneratorBase::DoseAccessorPointer; private: FileNameType _fileName; /** @brief The dose as itkImage */ ITKImageType::Pointer _itkDoubleImage; + + bool _useDicom; ITKImageFileAccessorGenerator() = delete; public: ~ITKImageFileAccessorGenerator() override; - ITKImageFileAccessorGenerator(const FileNameType& fileName); + ITKImageFileAccessorGenerator(const FileNameType& fileName, const bool& useDicom = false); /*! @brief Generate DoseAccessor @return Return shared pointer of DoseAccessor. @exception InvalidDoseException Thrown if file could not be read @exception InvalidParameterException Thrown if file has imageDimension !=3 or image component type != SCALAR @details is always converted into a itkImage<DoseTypeGy,3> by using a CastImageFilter @sa doCasting, handleGenericImage */ DoseAccessorPointer generateDoseAccessor() override; - DoseAccessorPointer generateDoseAccessor(bool isDicom); - }; }//end namespace itk }//end namespace io }//end namespace rttb #endif diff --git a/code/io/utils/files.cmake b/code/io/utils/files.cmake index df811f2..6fe84d2 100644 --- a/code/io/utils/files.cmake +++ b/code/io/utils/files.cmake @@ -1,7 +1,9 @@ SET(CPP_FILES rttbDoseLoader.cpp rttbStructLoader.cpp ) SET(H_FILES + rttbDoseLoader.h + rttbStructLoader.h ) diff --git a/code/io/utils/rttbDoseLoader.cpp b/code/io/utils/rttbDoseLoader.cpp index 3f515bc..6c03757 100644 --- a/code/io/utils/rttbDoseLoader.cpp +++ b/code/io/utils/rttbDoseLoader.cpp @@ -1,107 +1,87 @@ // ----------------------------------------------------------------------- // 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. // //------------------------------------------------------------------------ -#ifndef __RTTB_DOSE_LOADER_H -#define __RTTB_DOSE_LOADER_H +#include "rttbDoseLoader.h" -#include "rttbDoseIteratorInterface.h" #include "rttbExceptionMacros.h" #include "rttbDicomFileDoseAccessorGenerator.h" #include "rttbDicomHelaxFileDoseAccessorGenerator.h" #include "rttbITKImageFileAccessorGenerator.h" namespace rttb { namespace io { namespace utils { - /*! @brief loads a dicom dose from a file. - @exception Throws an rttb::Exception if loading fails - @sa DicomFileDoseAccessorGenerator - */ rttb::core::DoseAccessorInterface::DoseAccessorPointer loadDicomDose(const std::string& fileName) { rttb::io::dicom::DicomFileDoseAccessorGenerator generator(fileName); return generator.generateDoseAccessor(); } - /*! @brief loads a helax dose from a file. - @exception Throws an rttb::Exception if loading fails - @sa DicomHelaxFileDoseAccessorGenerator - */ rttb::core::DoseAccessorInterface::DoseAccessorPointer loadHelaxDose(const std::string& path) { rttb::io::helax::DicomHelaxFileDoseAccessorGenerator generator(path); return generator.generateDoseAccessor(); } - /*! @brief loads an itk dose from a file. - @exception Throws an rttb::Exception if loading fails. - @details Might be of all formats that ITK know (*.mhd, *.nrrd, ...). The absolute image values are taken as dose. - @sa ITKImageFileAccessorGenerator - */ rttb::core::DoseAccessorInterface::DoseAccessorPointer loadITKDose(const std::string& fileName) { rttb::io::itk::ITKImageFileAccessorGenerator generator(fileName); return generator.generateDoseAccessor(); } rttb::core::DoseAccessorInterface::DoseAccessorPointer loadITKDicomDose(const std::string& fileName) { - rttb::io::itk::ITKImageFileAccessorGenerator generator(fileName); - return generator.generateDoseAccessor(true); + rttb::io::itk::ITKImageFileAccessorGenerator generator(fileName, true); + return generator.generateDoseAccessor(); } - /*! @brief loads a dose from a file based on the loadingStyle. - @params args[0]: determines the loadingStyle - @exception Throws an rttb::Exception if loading fails - */ rttb::core::DoseAccessorInterface::DoseAccessorPointer loadDose(const std::string& fileName, const std::string& loadStyle) { rttb::core::DoseAccessorInterface::DoseAccessorPointer result; if (loadStyle == "" || loadStyle == "dicom") { result = loadDicomDose(fileName); } else if (loadStyle == "helax") { result = loadHelaxDose(fileName); } else if (loadStyle == "itk") { result = loadITKDose(fileName); } else if (loadStyle == "itkDicom") { result = loadITKDicomDose(fileName); } else { rttbDefaultExceptionStaticMacro(<< "Unknown io style selected. Cannot load data. Selected style: " << loadStyle); } return result; } } } } -#endif diff --git a/code/io/utils/rttbDoseLoader.h b/code/io/utils/rttbDoseLoader.h new file mode 100644 index 0000000..57ce35c --- /dev/null +++ b/code/io/utils/rttbDoseLoader.h @@ -0,0 +1,56 @@ +// ----------------------------------------------------------------------- +// 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. +// +//------------------------------------------------------------------------ + +#ifndef __RTTB_DOSE_LOADER_H +#define __RTTB_DOSE_LOADER_H + +#include "rttbDoseIteratorInterface.h" + +namespace rttb +{ + namespace io + { + namespace utils + { + /*! @brief loads a dicom dose from a file. + @exception Throws an rttb::Exception if loading fails + @sa DicomFileDoseAccessorGenerator + */ + rttb::core::DoseAccessorInterface::DoseAccessorPointer loadDicomDose(const std::string& fileName); + + /*! @brief loads a helax dose from a file. + @exception Throws an rttb::Exception if loading fails + @sa DicomHelaxFileDoseAccessorGenerator + */ + rttb::core::DoseAccessorInterface::DoseAccessorPointer loadHelaxDose(const std::string& path); + + /*! @brief loads an itk dose from a file. + @exception Throws an rttb::Exception if loading fails. + @details Might be of all formats that ITK know (*.mhd, *.nrrd, ...). The absolute image values are taken as dose. + @sa ITKImageFileAccessorGenerator + */ + rttb::core::DoseAccessorInterface::DoseAccessorPointer loadITKDose(const std::string& fileName); + + rttb::core::DoseAccessorInterface::DoseAccessorPointer loadITKDicomDose(const std::string& fileName); + + /*! @brief loads a dose from a file based on the loadingStyle. + @params args[0]: determines the loadingStyle + @exception Throws an rttb::Exception if loading fails + */ + rttb::core::DoseAccessorInterface::DoseAccessorPointer loadDose(const std::string& fileName, const std::string& loadStyle = "dicom"); + } + } +} +#endif diff --git a/code/io/utils/rttbStructLoader.cpp b/code/io/utils/rttbStructLoader.cpp index e93409b..b7f71dd 100644 --- a/code/io/utils/rttbStructLoader.cpp +++ b/code/io/utils/rttbStructLoader.cpp @@ -1,77 +1,59 @@ // ----------------------------------------------------------------------- // 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. // //------------------------------------------------------------------------ -#ifndef __RTTB_STRUCT_LOADER_H -#define __RTTB_STRUCT_LOADER_H - -#include "rttbDicomFileStructureSetGenerator.h" +#include "rttbStructLoader.h" #include "rttbExceptionMacros.h" namespace rttb { namespace io { namespace utils { - /*! @brief loads a dicom struct from a file. - You may pass a structure name regex. If is not empty, it will be used to filter structure in the - loading process. Only structures with a name matching the reg ex will be loaded. This speeds up the - loading process significantly if you need only one structure out of a structure set. - @exception Throws an rttb::Exception if loading fails - @sa DicomFileStructureSetGenerator - */ rttb::core::StructureSetGeneratorInterface::StructureSetPointer loadDicomStruct( - const std::string& fileName, const std::string& structNameRegex = "") + const std::string& fileName, const std::string& structNameRegex) { rttb::io::dicom::DicomFileStructureSetGenerator generator(fileName); if (!structNameRegex.empty()) { generator.setStructureLabelFilterActive(true); generator.setFilterRegEx(structNameRegex); } return generator.generateStructureSet(); } - /*! @brief loads a struct from a file based on the loadingStyle. - You may pass a structure name regex. If is not empty, it will be used to filter structure in the - loading process. Only structures with a name matching the reg ex will be loaded. This speeds up the - loading process significantly if you need only one structure out of a structure set. - @exception Throws an rttb::Exception if loading fails - @details voxelized itk images are read in generateMask() directly - */ rttb::core::StructureSetGeneratorInterface::StructureSetPointer loadStruct( - const std::string& fileName, const std::string& loadStyle, const std::string& structNameRegex = "") + const std::string& fileName, const std::string& loadStyle, const std::string& structNameRegex) { rttb::core::StructureSetGeneratorInterface::StructureSetPointer result; if (loadStyle == "" || loadStyle == "dicom") { result = rttb::io::utils::loadDicomStruct(fileName, structNameRegex); } else { rttbDefaultExceptionStaticMacro(<< "Unknown io style selected. Cannot load data. Selected style: " << loadStyle); } return result; } } } } -#endif diff --git a/code/io/utils/rttbStructLoader.h b/code/io/utils/rttbStructLoader.h new file mode 100644 index 0000000..f9da50e --- /dev/null +++ b/code/io/utils/rttbStructLoader.h @@ -0,0 +1,52 @@ +// ----------------------------------------------------------------------- +// 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. +// +//------------------------------------------------------------------------ + +#ifndef __RTTB_STRUCT_LOADER_H +#define __RTTB_STRUCT_LOADER_H + +#include "rttbDicomFileStructureSetGenerator.h" + +namespace rttb { + + namespace io { + + namespace utils { + + /*! @brief loads a dicom struct from a file. + You may pass a structure name regex. If is not empty, it will be used to filter structure in the + loading process. Only structures with a name matching the reg ex will be loaded. This speeds up the + loading process significantly if you need only one structure out of a structure set. + @exception Throws an rttb::Exception if loading fails + @sa DicomFileStructureSetGenerator + */ + rttb::core::StructureSetGeneratorInterface::StructureSetPointer loadDicomStruct(const std::string& fileName, const std::string& structNameRegex = ""); + + + /*! @brief loads a struct from a file based on the loadingStyle. + You may pass a structure name regex. If is not empty, it will be used to filter structure in the + loading process. Only structures with a name matching the reg ex will be loaded. This speeds up the + loading process significantly if you need only one structure out of a structure set. + @exception Throws an rttb::Exception if loading fails + @details voxelized itk images are read in generateMask() directly + */ + rttb::core::StructureSetGeneratorInterface::StructureSetPointer loadStruct( + const std::string& fileName, + const std::string& loadStyle = "dicom", + const std::string& structNameRegex = "" + ); + } + } +} +#endif