17 #ifndef __mitkNrrdTbssRoiReader_cpp
18 #define __mitkNrrdTbssRoiReader_cpp
22 #include "itkImageFileReader.h"
23 #include "itkMetaDataObject.h"
24 #include "itkNrrdImageIO.h"
25 #include "itkNiftiImageIO.h"
27 #include <itkImageFileWriter.h>
32 #include "itksys/SystemTools.hxx"
46 const std::string& locale =
"C";
47 const std::string& currLocale = setlocale( LC_ALL,
nullptr );
49 if ( locale.compare(currLocale)!=0 )
53 MITK_INFO <<
" ** Changing locale from " << setlocale(LC_ALL,
nullptr) <<
" to '" << locale <<
"'";
54 setlocale(LC_ALL, locale.c_str());
58 MITK_INFO <<
"Could not set locale " << locale;
65 const unsigned int MINDIM = 3;
66 const unsigned int MAXDIM = 4;
68 MITK_INFO <<
"loading " << m_FileName <<
" via mitk::NrrdTbssImageReader... " << std::endl;
71 if ( m_FileName ==
"" )
73 itkWarningMacro( <<
"Filename is empty!" )
78 imageIO->SetFileName( m_FileName.c_str() );
79 imageIO->ReadImageInformation();
81 unsigned int ndim = imageIO->GetNumberOfDimensions();
83 if ( ndim < MINDIM || ndim > MAXDIM )
85 itkWarningMacro( <<
"Sorry, only dimensions 3 is supported. The given file has " << ndim <<
" dimensions!" )
90 itk::ImageIORegion ioRegion( ndim );
91 itk::ImageIORegion::SizeType ioSize = ioRegion.GetSize();
92 itk::ImageIORegion::IndexType ioStart = ioRegion.GetIndex();
94 unsigned int dimensions[ MAXDIM ];
111 for ( i = 0; i < ndim ; ++i )
114 ioSize[ i ] = imageIO->GetDimensions( i );
117 dimensions[ i ] = imageIO->GetDimensions( i );
118 spacing[ i ] = imageIO->GetSpacing( i );
119 if(spacing[ i ] <= 0)
124 origin[ i ] = imageIO->GetOrigin( i );
128 ioRegion.SetSize( ioSize );
129 ioRegion.SetIndex( ioStart );
131 MITK_INFO <<
"ioRegion: " << ioRegion << std::endl;
132 imageIO->SetIORegion( ioRegion );
133 void* buffer =
new unsigned char[imageIO->GetImageSizeInBytes()];
134 imageIO->Read( buffer );
136 if((ndim==4) && (dimensions[3]<=1))
138 if((ndim==3) && (dimensions[2]<=1))
146 matrix.SetIdentity();
147 unsigned int j, itkDimMax3 = (ndim >= 3? 3 : ndim);
148 for ( i=0; i < itkDimMax3; ++i)
149 for( j=0; j < itkDimMax3; ++j )
150 matrix[i][j] = imageIO->GetDirection(j)[i];
155 (this->GetPrimaryOutput())->GetSlicedGeometry(0)->GetPlaneGeometry(0));
161 slicedGeometry->
InitializeEvenlySpaced(planeGeometry, static_cast<OutputType*>(this->GetPrimaryOutput())->GetDimension(2));
165 dynamic_cast<ProportionalTimeGeometry *
>(
static_cast<OutputType*
>(this->GetPrimaryOutput())->GetTimeGeometry())->Initialize(slicedGeometry, static_cast<OutputType*>(this->GetOutput(0))->GetDimension(3));
175 std::string ext = itksys::SystemTools::GetFilenameLastExtension(m_FileName);
176 ext = itksys::SystemTools::LowerCase(ext);
179 typedef itk::ImageFileReader<ImageType> FileReaderType;
181 reader->SetFileName(this->m_FileName);
183 reader->SetImageIO(imageIO);
186 img = reader->GetOutput();
188 static_cast<OutputType*
>(this->GetPrimaryOutput())->SetImage(img);
190 itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary();
191 ReadRoiInfo(imgMetaDictionary);
201 MITK_INFO <<
" ** Changing locale back from " << setlocale(LC_ALL,
nullptr) <<
" to '" << currLocale <<
"'";
202 setlocale(LC_ALL, currLocale.c_str());
206 MITK_INFO <<
"Could not reset locale " << currLocale;
209 MITK_INFO <<
"...finished!" << std::endl;
212 catch(std::exception& e)
214 MITK_INFO <<
"Std::Exception while reading file!!";
216 throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what());
220 MITK_INFO <<
"Exception while reading file!!";
221 throw itk::ImageFileReaderException(__FILE__, __LINE__,
"Sorry, an error occurred while reading the requested vessel tree file!");
232 std::vector<std::string> imgMetaKeys = dict.GetKeys();
233 std::vector<std::string>::const_iterator itKey = imgMetaKeys.begin();
234 std::string metaString;
235 std::vector< itk::Index<3> > roi;
237 for (; itKey != imgMetaKeys.end(); itKey ++)
241 itk::ExposeMetaData<std::string> (dict, *itKey, metaString);
243 if (itKey->find(
"ROI_index") != std::string::npos)
245 MITK_INFO << *itKey <<
" ---> " << metaString;
246 sscanf(metaString.c_str(),
"%lf %lf %lf\n", &x, &y, &z);
247 ix[0] = x; ix[1] = y; ix[2] = z;
250 else if(itKey->find(
"preprocessed FA") != std::string::npos)
252 MITK_INFO << *itKey <<
" ---> " << metaString;
253 static_cast<OutputType*
>(this->GetPrimaryOutput())->SetPreprocessedFA(
true);
254 static_cast<OutputType*
>(this->GetPrimaryOutput())->SetPreprocessedFAFile(metaString);
258 if (itKey->find(
"structure") != std::string::npos)
260 MITK_INFO << *itKey <<
" ---> " << metaString;
261 static_cast<OutputType*
>(this->GetPrimaryOutput())->SetStructure(metaString);
264 static_cast<OutputType*
>(this->GetPrimaryOutput())->SetRoi(roi);
272 return m_FileName.c_str();
279 m_FileName = aFileName;
286 return m_FilePrefix.c_str();
293 m_FilePrefix = aFilePrefix;
300 return m_FilePattern.c_str();
307 m_FilePattern = aFilePattern;
320 if( filePattern !=
"" && filePrefix !=
"" )
324 std::string ext = itksys::SystemTools::GetFilenameLastExtension(filename);
325 ext = itksys::SystemTools::LowerCase(ext);
331 typedef itk::ImageFileReader<ImageType> FileReaderType;
333 reader->SetImageIO(io);
334 reader->SetFileName(filename);
340 catch(itk::ExceptionObject e)
itk::SmartPointer< Self > Pointer
void SetSpacing(const mitk::Vector3D &aSpacing, bool enforceSetSpacing=false)
Set the spacing (m_Spacing).
Base of all data objects.
void SetFileName(const char *aFileName) override
Specify the file to load.
DataCollection - Class to facilitate loading/accessing structured data.
void ReadRoiInfo(itk::MetaDataDictionary dict)
virtual void InitializeEvenlySpaced(mitk::PlaneGeometry *geometry2D, unsigned int slices)
Completely initialize this instance as evenly-spaced with slices parallel to the provided PlaneGeomet...
static bool CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
MITKCORE_EXPORT mitk::PixelType MakePixelType(vtkImageData *vtkimagedata)
deduct the PixelType for a given vtk image
const char * GetFileName() const override
static const std::string filename
void SetOrigin(const Point3D &origin)
Set the origin, i.e. the upper-left corner of the plane.
const char * GetFilePrefix() const override
Get the specified file prefix for the file(s) to load.
Describes the geometry of a data object consisting of slices.
void SetFilePrefix(const char *aFilePrefix) override
Specify file prefix for the file(s) to load.
Describes a two-dimensional, rectangular plane.
virtual void GenerateData() override
const char * GetFilePattern() const override
Get the specified file pattern for the file(s) to load. The sprintf format used to build filename fro...
void SetFilePattern(const char *aFilePattern) override
Specified file pattern for the file(s) to load. The sprintf format used to build filename from FilePr...
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.