diff --git a/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkDiffusionImage.txx b/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkDiffusionImage.txx index 6d8c981890..cdce6d9559 100644 --- a/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkDiffusionImage.txx +++ b/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkDiffusionImage.txx @@ -1,346 +1,353 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2008-02-08 11:19:03 +0100 (Fr, 08 Feb 2008) $ Version: $Revision: 11989 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. 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 "itkImageRegionIterator.h" #include "itkImageRegionConstIterator.h" #include "mitkImageCast.h" template mitk::DiffusionImage::DiffusionImage() : m_VectorImage(0), m_Directions(0), m_OriginalDirections(0), m_B_Value(-1.0), m_VectorImageAdaptor(0) { + MeasurementFrameType mf; + for(int i=0; i<3; i++) + for(int j=0; j<3; j++) + mf[i][j] = 0; + for(int i=0; i<3; i++) + mf[i][i] = 1; + m_MeasurementFrame = mf; } template mitk::DiffusionImage::~DiffusionImage() { } template void mitk::DiffusionImage ::InitializeFromVectorImage() { if(!m_VectorImage || !m_Directions || m_B_Value==-1.0) { MITK_INFO << "DiffusionImage could not be initialized. Set all members first!" << std::endl; return; } // find bzero index int firstZeroIndex = -1; for(GradientDirectionContainerType::ConstIterator it = m_Directions->Begin(); it != m_Directions->End(); ++it) { firstZeroIndex++; GradientDirectionType g = it.Value(); if(g[0] == 0 && g[1] == 0 && g[2] == 0 ) break; } typedef itk::Image ImgType; typename ImgType::Pointer img = ImgType::New(); img->SetSpacing( m_VectorImage->GetSpacing() ); // Set the image spacing img->SetOrigin( m_VectorImage->GetOrigin() ); // Set the image origin img->SetDirection( m_VectorImage->GetDirection() ); // Set the image direction img->SetLargestPossibleRegion( m_VectorImage->GetLargestPossibleRegion()); img->SetBufferedRegion( m_VectorImage->GetLargestPossibleRegion() ); img->Allocate(); int vecLength = m_VectorImage->GetVectorLength(); InitializeByItk( img.GetPointer(), 1, vecLength ); //for(int i=0; i itw (img, img->GetLargestPossibleRegion() ); itw = itw.Begin(); itk::ImageRegionConstIterator itr (m_VectorImage, m_VectorImage->GetLargestPossibleRegion() ); itr = itr.Begin(); while(!itr.IsAtEnd()) { itw.Set(itr.Get().GetElement(firstZeroIndex)); ++itr; ++itw; } // init SetImportVolume(img->GetBufferPointer());//, 0, 0, CopyMemory); //SetVolume( img->GetBufferPointer(), i ); //} m_DisplayIndex = firstZeroIndex; MITK_INFO << "Diffusion-Image successfully initialized."; } template void mitk::DiffusionImage ::SetDisplayIndexForRendering(int displayIndex) { int index = displayIndex; int vecLength = m_VectorImage->GetVectorLength(); index = index > vecLength-1 ? vecLength-1 : index; if( m_DisplayIndex != index ) { typedef itk::Image ImgType; typename ImgType::Pointer img = ImgType::New(); CastToItkImage(this, img); itk::ImageRegionIterator itw (img, img->GetLargestPossibleRegion() ); itw = itw.Begin(); itk::ImageRegionConstIterator itr (m_VectorImage, m_VectorImage->GetLargestPossibleRegion() ); itr = itr.Begin(); while(!itr.IsAtEnd()) { itw.Set(itr.Get().GetElement(index)); ++itr; ++itw; } } m_DisplayIndex = index; } //template //bool mitk::DiffusionImage::RequestedRegionIsOutsideOfTheBufferedRegion() //{ // return false; //} // //template //void mitk::DiffusionImage::SetRequestedRegion(itk::DataObject * /*data*/) //{ //} // //template //void mitk::DiffusionImage::SetRequestedRegionToLargestPossibleRegion() //{ //} // //template //bool mitk::DiffusionImage::VerifyRequestedRegion() //{ // return true; //} //template //void mitk::DiffusionImage::DuplicateIfSingleSlice() //{ // // new image // typename ImageType::Pointer oldImage = m_Image; // m_Image = ImageType::New(); // m_Image->SetSpacing( oldImage->GetSpacing() ); // Set the image spacing // m_Image->SetOrigin( oldImage->GetOrigin() ); // Set the image origin // m_Image->SetDirection( oldImage->GetDirection() ); // Set the image direction // typename ImageType::RegionType region = oldImage->GetLargestPossibleRegion(); // if(region.GetSize(0) == 1) // region.SetSize(0,3); // if(region.GetSize(1) == 1) // region.SetSize(1,3); // if(region.GetSize(2) == 1) // region.SetSize(2,3); // m_Image->SetLargestPossibleRegion( region ); // m_Image->SetVectorLength( m_Directions->size() ); // m_Image->SetBufferedRegion( region ); // m_Image->Allocate(); // // // average image data that corresponds to identical directions // itk::ImageRegionIterator< ImageType > newIt(m_Image, region); // newIt.GoToBegin(); // itk::ImageRegionIterator< ImageType > oldIt(oldImage, oldImage->GetLargestPossibleRegion()); // oldIt.GoToBegin(); // // while(!newIt.IsAtEnd()) // { // newIt.Set(oldIt.Get()); // ++newIt; // ++oldIt; // if(oldIt.IsAtEnd()) // oldIt.GoToBegin(); // } // //} template bool mitk::DiffusionImage::AreAlike(GradientDirectionType g1, GradientDirectionType g2, double precision) { GradientDirectionType diff = g1 - g2; return diff.two_norm() < precision; } template void mitk::DiffusionImage::CorrectDKFZBrokenGradientScheme(double precision) { GradientDirectionContainerType::Pointer directionSet = CalcAveragedDirectionSet(precision, m_Directions); if(directionSet->size() < 7) { MITK_INFO << "Too few directions, assuming and correcting DKFZ-bogus sequence details."; double v [7][3] = {{ 0, 0, 0 }, {-0.707057, 0, 0.707057 }, { 0.707057, 0, 0.707057 }, { 0, 0.707057, 0.707057 }, { 0, 0.707057, -0.707057 }, {-0.707057, 0.707057, 0 }, { 0.707057, 0.707057, 0 } }; int i=0; for(GradientDirectionContainerType::Iterator it = m_Directions->Begin(); it != m_Directions->End(); ++it) { it.Value().set(v[i++%7]); } for(GradientDirectionContainerType::Iterator it = m_OriginalDirections->Begin(); it != m_OriginalDirections->End(); ++it) { it.Value().set(v[i++%7]); } } } template mitk::DiffusionImage::GradientDirectionContainerType::Pointer mitk::DiffusionImage::CalcAveragedDirectionSet(double precision, GradientDirectionContainerType::Pointer directions) { // save old and construct new direction container GradientDirectionContainerType::Pointer newDirections = GradientDirectionContainerType::New(); // fill new direction container for(GradientDirectionContainerType::ConstIterator gdcitOld = directions->Begin(); gdcitOld != directions->End(); ++gdcitOld) { // already exists? bool found = false; for(GradientDirectionContainerType::ConstIterator gdcitNew = newDirections->Begin(); gdcitNew != newDirections->End(); ++gdcitNew) { if(AreAlike(gdcitNew.Value(), gdcitOld.Value(), precision)) { found = true; break; } } // if not found, add it to new container if(!found) { newDirections->push_back(gdcitOld.Value()); } } return newDirections; } template void mitk::DiffusionImage::AverageRedundantGradients(double precision) { GradientDirectionContainerType::Pointer newDirs = CalcAveragedDirectionSet(precision, m_Directions); GradientDirectionContainerType::Pointer newOriginalDirs = CalcAveragedDirectionSet(precision, m_OriginalDirections); // if sizes equal, we do not need to do anything in this function if(m_Directions->size() == newDirs->size() || m_OriginalDirections->size() == newOriginalDirs->size()) return; GradientDirectionContainerType::Pointer oldDirections = m_Directions; GradientDirectionContainerType::Pointer oldOriginalDirections = m_OriginalDirections; m_Directions = newDirs; m_OriginalDirections = newOriginalDirs; // new image typename ImageType::Pointer oldImage = m_VectorImage; m_VectorImage = ImageType::New(); m_VectorImage->SetSpacing( oldImage->GetSpacing() ); // Set the image spacing m_VectorImage->SetOrigin( oldImage->GetOrigin() ); // Set the image origin m_VectorImage->SetDirection( oldImage->GetDirection() ); // Set the image direction m_VectorImage->SetLargestPossibleRegion( oldImage->GetLargestPossibleRegion() ); m_VectorImage->SetVectorLength( m_Directions->size() ); m_VectorImage->SetBufferedRegion( oldImage->GetLargestPossibleRegion() ); m_VectorImage->Allocate(); // average image data that corresponds to identical directions itk::ImageRegionIterator< ImageType > newIt(m_VectorImage, m_VectorImage->GetLargestPossibleRegion()); newIt.GoToBegin(); itk::ImageRegionIterator< ImageType > oldIt(oldImage, oldImage->GetLargestPossibleRegion()); oldIt.GoToBegin(); // initial new value of voxel typename ImageType::PixelType newVec; newVec.SetSize(m_Directions->size()); newVec.AllocateElements(m_Directions->size()); std::vector > dirIndices; for(GradientDirectionContainerType::ConstIterator gdcitNew = m_Directions->Begin(); gdcitNew != m_Directions->End(); ++gdcitNew) { dirIndices.push_back(std::vector(0)); for(GradientDirectionContainerType::ConstIterator gdcitOld = oldDirections->Begin(); gdcitOld != oldDirections->End(); ++gdcitOld) { if(AreAlike(gdcitNew.Value(), gdcitOld.Value(), precision)) { dirIndices[gdcitNew.Index()].push_back(gdcitOld.Index()); } } } int ind1 = -1; while(!newIt.IsAtEnd()) { // progress typename ImageType::IndexType ind = newIt.GetIndex(); ind1 = ind.m_Index[2]; // init new vector with zeros newVec.Fill(0.0); // the old voxel value with duplicates typename ImageType::PixelType oldVec = oldIt.Get(); for(unsigned int i=0; i +#include + +#include "itksys/SystemTools.hxx" namespace mitk { template - void NrrdDiffusionImageReader - ::GenerateData() + void NrrdDiffusionImageReader + ::GenerateData() { // Since everything is completely read in GenerateOutputInformation() it is stored // in a cache variable. A timestamp is associated. // If the timestamp of the cache variable is newer than the MTime, we only need to // assign the cache variable to the DataObject. // Otherwise, the tree must be read again from the file and OuputInformation must // be updated! if ( ( ! m_OutputCache ) || ( this->GetMTime( ) > m_CacheTime.GetMTime( ) ) ) { this->GenerateOutputInformation(); itkWarningMacro("Cache regenerated!"); } if (!m_OutputCache) { itkWarningMacro("Tree cache is empty!"); } static_cast(this->GetOutput()) - ->SetVectorImage(m_OutputCache->GetVectorImage()); + ->SetVectorImage(m_OutputCache->GetVectorImage()); static_cast(this->GetOutput()) - ->SetB_Value(m_OutputCache->GetB_Value()); + ->SetB_Value(m_OutputCache->GetB_Value()); static_cast(this->GetOutput()) - ->SetDirections(m_OutputCache->GetDirections()); + ->SetDirections(m_OutputCache->GetDirections()); static_cast(this->GetOutput()) - ->SetOriginalDirections(m_OutputCache->GetOriginalDirections()); + ->SetOriginalDirections(m_OutputCache->GetOriginalDirections()); static_cast(this->GetOutput()) - ->SetMeasurementFrame(m_OutputCache->GetMeasurementFrame()); + ->SetMeasurementFrame(m_OutputCache->GetMeasurementFrame()); static_cast(this->GetOutput()) - ->InitializeFromVectorImage(); + ->InitializeFromVectorImage(); } template - void NrrdDiffusionImageReader::GenerateOutputInformation() + void NrrdDiffusionImageReader::GenerateOutputInformation() { typename OutputType::Pointer outputForCache = OutputType::New(); if ( m_FileName == "") { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, the filename to be read is empty!"); } else { try { const std::string& locale = "C"; const std::string& currLocale = setlocale( LC_ALL, NULL ); if ( locale.compare(currLocale)!=0 ) { try { MITK_INFO << " ** Changing locale from " << setlocale(LC_ALL, NULL) << " to '" << locale << "'"; setlocale(LC_ALL, locale.c_str()); } catch(...) { MITK_INFO << "Could not set locale " << locale; } } - itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); - typedef itk::ImageFileReader FileReaderType; - typename FileReaderType::Pointer reader = FileReaderType::New(); - reader->SetImageIO(io); - reader->SetFileName(this->m_FileName); - reader->Update(); - - typename ImageType::Pointer img = reader->GetOutput(); - itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary(); - std::vector imgMetaKeys = imgMetaDictionary.GetKeys(); - std::vector::const_iterator itKey = imgMetaKeys.begin(); - std::string metaString; + MITK_INFO << "NrrdDiffusionImageReader READING IMAGE INFORMATION"; + typename ImageType::Pointer img; - GradientDirectionType vect3d; + std::string ext = itksys::SystemTools::GetFilenameLastExtension(m_FileName); + ext = itksys::SystemTools::LowerCase(ext); + if (ext == ".hdwi" || ext == ".dwi") + { + typedef itk::ImageFileReader FileReaderType; + typename FileReaderType::Pointer reader = FileReaderType::New(); + reader->SetFileName(this->m_FileName); + itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); + reader->SetImageIO(io); + reader->Update(); + img = reader->GetOutput(); + } + else if(ext == ".fsl" || ext == ".fslgz") + { + // create temporary file with correct ending for nifti-io + std::string fname3 = m_FileName; + fname3 += ext == ".fsl" ? ".nii" : ".nii.gz"; + itksys::SystemTools::CopyAFile(m_FileName.c_str(), fname3.c_str()); + + // create reader and read file + typedef itk::Image ImageType4D; + itk::NiftiImageIO::Pointer io2 = itk::NiftiImageIO::New(); + typedef itk::ImageFileReader FileReaderType; + typename FileReaderType::Pointer reader = FileReaderType::New(); + reader->SetFileName(fname3); + reader->SetImageIO(io2); + reader->Update(); + typename ImageType4D::Pointer img4 = reader->GetOutput(); + + // delete temporary file + itksys::SystemTools::RemoveFile(fname3.c_str()); + + // convert 4D file to vector image + img = ImageType::New(); + + typename ImageType::SpacingType spacing; + typename ImageType4D::SpacingType spacing4 = img4->GetSpacing(); + for(int i=0; i<3; i++) + spacing[i] = spacing4[i]; + img->SetSpacing( spacing ); // Set the image spacing + + typename ImageType::PointType origin; + typename ImageType4D::PointType origin4 = img4->GetOrigin(); + for(int i=0; i<3; i++) + origin[i] = origin4[i]; + img->SetOrigin( origin ); // Set the image origin + + typename ImageType::DirectionType direction; + typename ImageType4D::DirectionType direction4 = img4->GetDirection(); + for(int i=0; i<3; i++) + for(int j=0; j<3; j++) + direction[i][j] = direction4[i][j]; + img->SetDirection( direction ); // Set the image direction + + typename ImageType::RegionType region; + typename ImageType4D::RegionType region4 = img4->GetLargestPossibleRegion(); + + typename ImageType::RegionType::SizeType size; + typename ImageType4D::RegionType::SizeType size4 = region4.GetSize(); + + for(int i=0; i<3; i++) + size[i] = size4[i]; + + typename ImageType::RegionType::IndexType index; + typename ImageType4D::RegionType::IndexType index4 = region4.GetIndex(); + for(int i=0; i<3; i++) + index[i] = index4[i]; + + region.SetSize(size); + region.SetIndex(index); + img->SetRegions( region ); + + img->SetVectorLength(size4[3]); + img->Allocate(); + + itk::ImageRegionIterator it (img, img->GetLargestPossibleRegion() ); + typedef typename ImageType::PixelType VecPixType; + for (it = it.Begin(); !it.IsAtEnd(); ++it) + { + VecPixType vec = it.Get(); + typename ImageType::IndexType currentIndex = it.GetIndex(); + for(int i=0; i<3; i++) + index4[i] = currentIndex[i]; + for(unsigned int ind=0; indGetPixel(index4); + } + it.Set(vec); + } + } + MITK_INFO << "NrrdDiffusionImageReader READING HEADER INFORMATION"; m_DiffusionVectors = GradientDirectionContainerType::New(); m_OriginalDiffusionVectors = GradientDirectionContainerType::New(); + if (ext == ".hdwi" || ext == ".dwi") + { - int numberOfImages = 0; - int numberOfGradientImages = 0; - bool readb0 = false; - bool readFrame = false; - double xx, xy, xz, yx, yy, yz, zx, zy, zz; + itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary(); + std::vector imgMetaKeys = imgMetaDictionary.GetKeys(); + std::vector::const_iterator itKey = imgMetaKeys.begin(); + std::string metaString; - for (; itKey != imgMetaKeys.end(); itKey ++) - { - double x,y,z; - - itk::ExposeMetaData (imgMetaDictionary, *itKey, metaString); - if (itKey->find("DWMRI_gradient") != std::string::npos) - { - MITK_INFO << *itKey << " ---> " << metaString; - sscanf(metaString.c_str(), "%lf %lf %lf\n", &x, &y, &z); - vect3d[0] = x; vect3d[1] = y; vect3d[2] = z; - m_DiffusionVectors->InsertElement( numberOfImages, vect3d ); - m_OriginalDiffusionVectors->InsertElement( numberOfImages, vect3d ); - ++numberOfImages; - // If the direction is 0.0, this is a reference image - if (vect3d[0] == 0.0 && - vect3d[1] == 0.0 && - vect3d[2] == 0.0) + GradientDirectionType vect3d; + + int numberOfImages = 0; + int numberOfGradientImages = 0; + bool readb0 = false; + bool readFrame = false; + double xx, xy, xz, yx, yy, yz, zx, zy, zz; + + for (; itKey != imgMetaKeys.end(); itKey ++) + { + double x,y,z; + + itk::ExposeMetaData (imgMetaDictionary, *itKey, metaString); + if (itKey->find("DWMRI_gradient") != std::string::npos) + { + MITK_INFO << *itKey << " ---> " << metaString; + sscanf(metaString.c_str(), "%lf %lf %lf\n", &x, &y, &z); + vect3d[0] = x; vect3d[1] = y; vect3d[2] = z; + m_DiffusionVectors->InsertElement( numberOfImages, vect3d ); + m_OriginalDiffusionVectors->InsertElement( numberOfImages, vect3d ); + ++numberOfImages; + // If the direction is 0.0, this is a reference image + if (vect3d[0] == 0.0 && + vect3d[1] == 0.0 && + vect3d[2] == 0.0) + { + continue; + } + ++numberOfGradientImages;; + } + else if (itKey->find("DWMRI_b-value") != std::string::npos) + { + MITK_INFO << *itKey << " ---> " << metaString; + readb0 = true; + m_B_Value = atof(metaString.c_str()); + } + else if (itKey->find("measurement frame") != std::string::npos) { - continue; + MITK_INFO << *itKey << " ---> " << metaString; + sscanf(metaString.c_str(), " ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) \n", &xx, &xy, &xz, &yx, &yy, &yz, &zx, &zy, &zz); + + readFrame = true; + if (xx>10e-10 || xy>10e-10 || xz>10e-10 || + yx>10e-10 || yy>10e-10 || yz>10e-10 || + zx>10e-10 || zy>10e-10 || zz>10e-10 ) + { + m_MeasurementFrame(0,0) = xx; + m_MeasurementFrame(0,1) = xy; + m_MeasurementFrame(0,2) = xz; + m_MeasurementFrame(1,0) = yx; + m_MeasurementFrame(1,1) = yy; + m_MeasurementFrame(1,2) = yz; + m_MeasurementFrame(2,0) = zx; + m_MeasurementFrame(2,1) = zy; + m_MeasurementFrame(2,2) = zz; + } + else + { + m_MeasurementFrame(0,0) = 1; + m_MeasurementFrame(0,1) = 0; + m_MeasurementFrame(0,2) = 0; + m_MeasurementFrame(1,0) = 0; + m_MeasurementFrame(1,1) = 1; + m_MeasurementFrame(1,2) = 0; + m_MeasurementFrame(2,0) = 0; + m_MeasurementFrame(2,1) = 0; + m_MeasurementFrame(2,2) = 1; + } } - ++numberOfGradientImages;; } - else if (itKey->find("DWMRI_b-value") != std::string::npos) + + if(readFrame) { - MITK_INFO << *itKey << " ---> " << metaString; - readb0 = true; - m_B_Value = atof(metaString.c_str()); + MITK_INFO << "Applying Measurement Frame: (" << xx << "," << xy << "," << xz + << ") (" << yx << "," << yy << "," << yz << ") (" << zx << "," << zy << "," << zz << ")"; + + for(int i=0; i vec(3); + vec.copy_in(m_DiffusionVectors->ElementAt(i).data_block()); + vec = vec.pre_multiply(m_MeasurementFrame); + m_DiffusionVectors->ElementAt(i).copy_in(vec.data_block()); + } } - else if (itKey->find("measurement frame") != std::string::npos) + + MITK_INFO << "Number of gradient images: " + << numberOfGradientImages + << " and Number of reference images: " + << numberOfImages - numberOfGradientImages; + + if(!readb0) { - MITK_INFO << *itKey << " ---> " << metaString; - sscanf(metaString.c_str(), " ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) \n", &xx, &xy, &xz, &yx, &yy, &yz, &zx, &zy, &zz); + MITK_INFO << "BValue not specified in header file"; + } + + } + else if(ext == ".fsl" || ext == ".fslgz") + { - readFrame = true; - if (xx>10e-10 || xy>10e-10 || xz>10e-10 || - yx>10e-10 || yy>10e-10 || yz>10e-10 || - zx>10e-10 || zy>10e-10 || zz>10e-10 ) + std::string line; + std::vector bvec_entries; + std::string fname = m_FileName; + fname += ".bvecs"; + std::ifstream myfile (fname.c_str()); + if (myfile.is_open()) + { + while ( myfile.good() ) { - m_MeasurementFrame(0,0) = xx; - m_MeasurementFrame(0,1) = xy; - m_MeasurementFrame(0,2) = xz; - m_MeasurementFrame(1,0) = yx; - m_MeasurementFrame(1,1) = yy; - m_MeasurementFrame(1,2) = yz; - m_MeasurementFrame(2,0) = zx; - m_MeasurementFrame(2,1) = zy; - m_MeasurementFrame(2,2) = zz; + getline (myfile,line); + char* pch = strtok (const_cast(line.c_str())," "); + while (pch != NULL) + { + bvec_entries.push_back(strtof(pch, NULL)); + pch = strtok (NULL, " "); + } } - else + myfile.close(); + } + else + { + MITK_INFO << "Unable to open bvecs file"; + } + + std::vector bval_entries; + std::string fname2 = m_FileName; + fname2 += ".bvals"; + std::ifstream myfile2 (fname2.c_str()); + if (myfile2.is_open()) + { + while ( myfile2.good() ) { - m_MeasurementFrame(0,0) = 1; - m_MeasurementFrame(0,1) = 0; - m_MeasurementFrame(0,2) = 0; - m_MeasurementFrame(1,0) = 0; - m_MeasurementFrame(1,1) = 1; - m_MeasurementFrame(1,2) = 0; - m_MeasurementFrame(2,0) = 0; - m_MeasurementFrame(2,1) = 0; - m_MeasurementFrame(2,2) = 1; + getline (myfile2,line); + char* pch = strtok (const_cast(line.c_str())," "); + while (pch != NULL) + { + bval_entries.push_back(strtof(pch, NULL)); + pch = strtok (NULL, " "); + } } + myfile2.close(); } - } - - if(readFrame) - { - MITK_INFO << "Applying Measurement Frame: (" << xx << "," << xy << "," << xz - << ") (" << yx << "," << yy << "," << yz << ") (" << zx << "," << zy << "," << zz << ")"; - - for(int i=0; i vec(3); - vec.copy_in(m_DiffusionVectors->ElementAt(i).data_block()); - vec = vec.pre_multiply(m_MeasurementFrame); - m_DiffusionVectors->ElementAt(i).copy_in(vec.data_block()); + MITK_INFO << "Unable to open bvals file"; } - } - MITK_INFO << "Number of gradient images: " - << numberOfGradientImages - << " and Number of reference images: " - << numberOfImages - numberOfGradientImages; + MITK_INFO << "Found " << bval_entries.size() << " b-values and " << bvec_entries.size() << " bvec-entries"; + m_B_Value = -1; + unsigned int numb = bval_entries.size(); + for(unsigned int i=0; i vec; + vec[0] = bvec_entries.at(i); + vec[1] = bvec_entries.at(i+numb); + vec[2] = bvec_entries.at(i+2*numb); - if(!readb0) - { - MITK_INFO << "BValue not specified in header file"; + m_DiffusionVectors->InsertElement(i,vec); + m_OriginalDiffusionVectors->InsertElement(i,vec); + + } + + for(int i=0; i<3; i++) + for(int j=0; j<3; j++) + m_MeasurementFrame[i][j] = i==j ? 1 : 0; } // This call updates the output information of the associated VesselTreeData outputForCache->SetVectorImage(img); outputForCache->SetB_Value(m_B_Value); outputForCache->SetDirections(m_DiffusionVectors); outputForCache->SetOriginalDirections(m_OriginalDiffusionVectors); outputForCache->SetMeasurementFrame(m_MeasurementFrame); // Since we have already read the tree, we can store it in a cache variable // so that it can be assigned to the DataObject in GenerateData(); m_OutputCache = outputForCache; m_CacheTime.Modified(); try { MITK_INFO << " ** Changing locale back from " << setlocale(LC_ALL, NULL) << " to '" << currLocale << "'"; setlocale(LC_ALL, currLocale.c_str()); } catch(...) { MITK_INFO << "Could not reset locale " << currLocale; } } catch(std::exception& e) { + MITK_INFO << "Std::Exception while reading file!!"; + MITK_INFO << e.what(); throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what()); } catch(...) { + MITK_INFO << "Exception while reading file!!"; throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, an error occurred while reading the requested vessel tree file!"); } } } template - const char* NrrdDiffusionImageReader - ::GetFileName() const + const char* NrrdDiffusionImageReader + ::GetFileName() const { return m_FileName.c_str(); } template - void NrrdDiffusionImageReader - ::SetFileName(const char* aFileName) + void NrrdDiffusionImageReader + ::SetFileName(const char* aFileName) { m_FileName = aFileName; } template - const char* NrrdDiffusionImageReader - ::GetFilePrefix() const + const char* NrrdDiffusionImageReader + ::GetFilePrefix() const { return m_FilePrefix.c_str(); } template - void NrrdDiffusionImageReader - ::SetFilePrefix(const char* aFilePrefix) + void NrrdDiffusionImageReader + ::SetFilePrefix(const char* aFilePrefix) { m_FilePrefix = aFilePrefix; } template - const char* NrrdDiffusionImageReader - ::GetFilePattern() const + const char* NrrdDiffusionImageReader + ::GetFilePattern() const { return m_FilePattern.c_str(); } template - void NrrdDiffusionImageReader - ::SetFilePattern(const char* aFilePattern) + void NrrdDiffusionImageReader + ::SetFilePattern(const char* aFilePattern) { m_FilePattern = aFilePattern; } template - bool NrrdDiffusionImageReader - ::CanReadFile(const std::string filename, const std::string /*filePrefix*/, const std::string /*filePattern*/) + bool NrrdDiffusionImageReader + ::CanReadFile(const std::string filename, const std::string /*filePrefix*/, const std::string /*filePattern*/) { // First check the extension if( filename == "" ) { return false; } std::string ext = itksys::SystemTools::GetFilenameLastExtension(filename); ext = itksys::SystemTools::LowerCase(ext); if (ext == ".hdwi" || ext == ".dwi") { itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); typedef itk::ImageFileReader FileReaderType; typename FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(filename); try { reader->Update(); } catch(itk::ExceptionObject e) { MITK_INFO << e.GetDescription(); } typename ImageType::Pointer img = reader->GetOutput(); itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary(); std::vector imgMetaKeys = imgMetaDictionary.GetKeys(); std::vector::const_iterator itKey = imgMetaKeys.begin(); std::string metaString; for (; itKey != imgMetaKeys.end(); itKey ++) { itk::ExposeMetaData (imgMetaDictionary, *itKey, metaString); if (itKey->find("modality") != std::string::npos) { if (metaString.find("DWMRI") != std::string::npos) { return true; } } } } + + if (ext == ".fsl" || ext == ".fslgz") + { + // itk::NiftiImageIO::Pointer io2 = itk::NiftiImageIO::New(); + + // typedef itk::ImageFileReader FileReaderType; + // typename FileReaderType::Pointer reader = FileReaderType::New(); + // reader->SetImageIO(io2); + // reader->SetFileName(filename); + + // try + // { + // reader->Update(); + // } + // catch(itk::ExceptionObject e) + // { + // MITK_INFO << e.GetDescription(); + // } + + std::string fname = filename; + fname += ".bvecs"; + + std::string fname2 = filename; + fname2 += ".bvals"; + + if( itksys::SystemTools::FileExists(fname.c_str()) + && itksys::SystemTools::FileExists(fname2.c_str()) ) + { + return true; + } + else + { + MITK_INFO << ".bvals and .bvals files do not exist properly"; + } + + } + return false; } } //namespace MITK #endif diff --git a/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkNrrdDiffusionImageWriter.cpp b/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkNrrdDiffusionImageWriter.cpp index abc4e9ad20..071dd10dc1 100644 --- a/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkNrrdDiffusionImageWriter.cpp +++ b/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkNrrdDiffusionImageWriter.cpp @@ -1,149 +1,303 @@ /*========================================================================= - + Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2008-12-10 18:05:13 +0100 (Mi, 10 Dez 2008) $ Version: $Revision: 15922 $ - + Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. - + 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 __mitkNrrdDiffusionImageWriter__cpp #define __mitkNrrdDiffusionImageWriter__cpp #include "mitkNrrdDiffusionImageWriter.h" #include "itkMetaDataDictionary.h" #include "itkMetaDataObject.h" #include "itkNrrdImageIO.h" +#include "itkNiftiImageIO.h" #include "itkImageFileWriter.h" +#include "itksys/SystemTools.hxx" + +#include +#include template mitk::NrrdDiffusionImageWriter::NrrdDiffusionImageWriter() - : m_FileName(""), m_FilePrefix(""), m_FilePattern(""), m_Success(false) + : m_FileName(""), m_FilePrefix(""), m_FilePattern(""), m_Success(false) { - this->SetNumberOfRequiredInputs( 1 ); + this->SetNumberOfRequiredInputs( 1 ); } template mitk::NrrdDiffusionImageWriter::~NrrdDiffusionImageWriter() {} template void mitk::NrrdDiffusionImageWriter::GenerateData() { - m_Success = false; - InputType* input = this->GetInput(); - if (input == NULL) - { - itkWarningMacro(<<"Sorry, input to NrrdDiffusionImageWriter is NULL!"); - return; - } - if ( m_FileName == "" ) - { - itkWarningMacro( << "Sorry, filename has not been set!" ); - return ; - } - - char keybuffer[512]; - char valbuffer[512]; - std::string tmp; + m_Success = false; + InputType* input = this->GetInput(); + if (input == NULL) + { + itkWarningMacro(<<"Sorry, input to NrrdDiffusionImageWriter is NULL!"); + return; + } + if ( m_FileName == "" ) + { + itkWarningMacro( << "Sorry, filename has not been set!" ); + return ; + } - itk::VectorImage::Pointer img = input->GetVectorImage(); - img->GetMetaDataDictionary(); + char keybuffer[512]; + char valbuffer[512]; + std::string tmp; - //itk::MetaDataDictionary dic = input->GetImage()->GetMetaDataDictionary(); + itk::VectorImage::Pointer img = input->GetVectorImage(); + img->GetMetaDataDictionary(); - vnl_matrix_fixed measurementFrame = input->GetMeasurementFrame(); - if (measurementFrame(0,0) || measurementFrame(0,1) || measurementFrame(0,2) || - measurementFrame(1,0) || measurementFrame(1,1) || measurementFrame(1,2) || - measurementFrame(2,0) || measurementFrame(2,1) || measurementFrame(2,2)) - { - sprintf( valbuffer, " ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) \n", measurementFrame(0,0), measurementFrame(0,1), measurementFrame(0,2), measurementFrame(1,0), measurementFrame(1,1), measurementFrame(1,2), measurementFrame(2,0), measurementFrame(2,1), measurementFrame(2,2)); - itk::EncapsulateMetaData(input->GetVectorImage()->GetMetaDataDictionary(),std::string("measurement frame"),std::string(valbuffer)); - } + //itk::MetaDataDictionary dic = input->GetImage()->GetMetaDataDictionary(); - sprintf( valbuffer, "DWMRI"); - itk::EncapsulateMetaData(input->GetVectorImage()->GetMetaDataDictionary(),std::string("modality"),std::string(valbuffer)); + vnl_matrix_fixed measurementFrame = input->GetMeasurementFrame(); + if (measurementFrame(0,0) || measurementFrame(0,1) || measurementFrame(0,2) || + measurementFrame(1,0) || measurementFrame(1,1) || measurementFrame(1,2) || + measurementFrame(2,0) || measurementFrame(2,1) || measurementFrame(2,2)) + { + sprintf( valbuffer, " ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) ( %lf , %lf , %lf ) \n", measurementFrame(0,0), measurementFrame(0,1), measurementFrame(0,2), measurementFrame(1,0), measurementFrame(1,1), measurementFrame(1,2), measurementFrame(2,0), measurementFrame(2,1), measurementFrame(2,2)); + itk::EncapsulateMetaData(input->GetVectorImage()->GetMetaDataDictionary(),std::string("measurement frame"),std::string(valbuffer)); + } - if(input->GetOriginalDirections()->Size()) - { - sprintf( valbuffer, "%1f", input->GetB_Value() ); - itk::EncapsulateMetaData(input->GetVectorImage()->GetMetaDataDictionary(),std::string("DWMRI_b-value"),std::string(valbuffer)); - } + sprintf( valbuffer, "DWMRI"); + itk::EncapsulateMetaData(input->GetVectorImage()->GetMetaDataDictionary(),std::string("modality"),std::string(valbuffer)); - for(unsigned int i=0; iGetOriginalDirections()->Size(); i++) - { - sprintf( keybuffer, "DWMRI_gradient_%04d", i ); + if(input->GetOriginalDirections()->Size()) + { + sprintf( valbuffer, "%1f", input->GetB_Value() ); + itk::EncapsulateMetaData(input->GetVectorImage()->GetMetaDataDictionary(),std::string("DWMRI_b-value"),std::string(valbuffer)); + } + + for(unsigned int i=0; iGetOriginalDirections()->Size(); i++) + { + sprintf( keybuffer, "DWMRI_gradient_%04d", i ); - /*if(itk::ExposeMetaData(input->GetMetaDataDictionary(), + /*if(itk::ExposeMetaData(input->GetMetaDataDictionary(), std::string(keybuffer),tmp)) continue;*/ - sprintf( valbuffer, "%1f %1f %1f", input->GetOriginalDirections()->ElementAt(i).get(0), - input->GetOriginalDirections()->ElementAt(i).get(1), input->GetOriginalDirections()->ElementAt(i).get(2)); + sprintf( valbuffer, "%1f %1f %1f", input->GetOriginalDirections()->ElementAt(i).get(0), + input->GetOriginalDirections()->ElementAt(i).get(1), input->GetOriginalDirections()->ElementAt(i).get(2)); - itk::EncapsulateMetaData(input->GetVectorImage()->GetMetaDataDictionary(),std::string(keybuffer),std::string(valbuffer)); - } + itk::EncapsulateMetaData(input->GetVectorImage()->GetMetaDataDictionary(),std::string(keybuffer),std::string(valbuffer)); + } + + typedef itk::VectorImage ImageType; + std::string ext = itksys::SystemTools::GetFilenameLastExtension(m_FileName); + ext = itksys::SystemTools::LowerCase(ext); + if (ext == ".hdwi" || ext == ".dwi") + { itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); //io->SetNrrdVectorType( nrrdKindList ); io->SetFileType( itk::ImageIOBase::Binary ); io->UseCompressionOn(); - typedef itk::VectorImage ImageType; typedef itk::ImageFileWriter WriterType; typename WriterType::Pointer nrrdWriter = WriterType::New(); nrrdWriter->UseInputMetaDataDictionaryOn(); nrrdWriter->SetInput( input->GetVectorImage() ); nrrdWriter->SetImageIO(io); nrrdWriter->SetFileName(m_FileName); nrrdWriter->UseCompressionOn(); - + nrrdWriter->SetImageIO(io); try { nrrdWriter->Update(); } catch (itk::ExceptionObject e) { std::cout << e << std::endl; } - m_Success = true; + } + else if (ext == ".fsl" || ext == ".fslgz") + { + MITK_INFO << "Writing Nifti-Image for FSL"; + typename ImageType::Pointer vecimg = input->GetVectorImage(); + + typedef itk::Image ImageType4D; + typename ImageType4D::Pointer img4 = ImageType4D::New(); + + typename ImageType::SpacingType spacing = vecimg->GetSpacing(); + typename ImageType4D::SpacingType spacing4; + for(int i=0; i<3; i++) + spacing4[i] = spacing[i]; + spacing4[3] = 1; + img4->SetSpacing( spacing4 ); // Set the image spacing + + typename ImageType::PointType origin = vecimg->GetOrigin(); + typename ImageType4D::PointType origin4; + for(int i=0; i<3; i++) + origin4[i] = origin[i]; + origin4[3] = 0; + img4->SetOrigin( origin4 ); // Set the image origin + + typename ImageType::DirectionType direction = vecimg->GetDirection(); + typename ImageType4D::DirectionType direction4; + for(int i=0; i<3; i++) + for(int j=0; j<3; j++) + direction4[i][j] = direction[i][j]; + for(int i=0; i<4; i++) + direction4[i][3] = 0; + for(int i=0; i<4; i++) + direction4[3][i] = 0; + direction4[3][3] = 1; + img4->SetDirection( direction4 ); // Set the image direction + + typename ImageType::RegionType region = vecimg->GetLargestPossibleRegion(); + typename ImageType4D::RegionType region4; + + typename ImageType::RegionType::SizeType size = region.GetSize(); + typename ImageType4D::RegionType::SizeType size4; + + for(int i=0; i<3; i++) + size4[i] = size[i]; + size4[3] = vecimg->GetVectorLength(); + + typename ImageType::RegionType::IndexType index = region.GetIndex(); + typename ImageType4D::RegionType::IndexType index4; + for(int i=0; i<3; i++) + index4[i] = index[i]; + index4[3] = 0; + + region4.SetSize(size4); + region4.SetIndex(index4); + img4->SetRegions( region4 ); + + img4->Allocate(); + + itk::ImageRegionIterator it (vecimg, vecimg->GetLargestPossibleRegion() ); + typedef typename ImageType::PixelType VecPixType; + + for (it = it.Begin(); !it.IsAtEnd(); ++it) + { + VecPixType vec = it.Get(); + typename ImageType::IndexType currentIndex = it.GetIndex(); + for(unsigned int ind=0; indSetPixel(index4, vec[ind]); + } + } + + // create copy of file with correct ending for mitk + std::string fname3 = m_FileName; + std::string newext = ext == ".fsl" ? "nii" : "nii.gz"; + std::string::iterator itend = fname3.end(); + fname3.replace( itend-3, itend, newext); + + itk::NiftiImageIO::Pointer io4 = itk::NiftiImageIO::New(); + + typedef itk::VectorImage ImageType; + typedef itk::ImageFileWriter WriterType4; + typename WriterType4::Pointer nrrdWriter4 = WriterType4::New(); + nrrdWriter4->UseInputMetaDataDictionaryOn(); + nrrdWriter4->SetInput( img4 ); + nrrdWriter4->SetFileName(fname3); + nrrdWriter4->UseCompressionOn(); + nrrdWriter4->SetImageIO(io4); + try + { + nrrdWriter4->Update(); + } + catch (itk::ExceptionObject e) + { + std::cout << e << std::endl; + } + + itksys::SystemTools::CopyAFile(fname3.c_str(), m_FileName.c_str()); + + if(input->GetDirections()->Size()) + { + std::ofstream myfile; + std::string fname = m_FileName; + fname += ".bvals"; + myfile.open (fname.c_str()); + for(unsigned int i=0; iGetDirections()->Size(); i++) + { + double twonorm = input->GetDirections()->ElementAt(i).two_norm(); + myfile << input->GetB_Value()*twonorm*twonorm << " "; + } + myfile.close(); + + std::ofstream myfile2; + std::string fname2 = m_FileName; + fname2 += ".bvecs"; + myfile2.open (fname2.c_str()); + for(int j=0; j<3; j++) + { + for(unsigned int i=0; iGetDirections()->Size(); i++) + { + myfile2 << input->GetDirections()->ElementAt(i).get(j) << " "; + } + myfile2 << std::endl; + } + + std::ofstream myfile3; + std::string fname4 = m_FileName; + fname4 += ".ttk"; + myfile3.open (fname4.c_str()); + for(unsigned int i=0; iGetDirections()->Size(); i++) + { + for(int j=0; j<3; j++) + { + myfile3 << input->GetDirections()->ElementAt(i).get(j) << " "; + } + myfile3 << std::endl; + } + } + } + + m_Success = true; } template void mitk::NrrdDiffusionImageWriter::SetInput( InputType* diffVolumes ) { - this->ProcessObject::SetNthInput( 0, diffVolumes ); + this->ProcessObject::SetNthInput( 0, diffVolumes ); } template mitk::DiffusionImage* mitk::NrrdDiffusionImageWriter::GetInput() { - if ( this->GetNumberOfInputs() < 1 ) - { - return NULL; - } - else - { - return dynamic_cast ( this->ProcessObject::GetInput( 0 ) ); - } + if ( this->GetNumberOfInputs() < 1 ) + { + return NULL; + } + else + { + return dynamic_cast ( this->ProcessObject::GetInput( 0 ) ); + } } template std::vector mitk::NrrdDiffusionImageWriter::GetPossibleFileExtensions() { std::vector possibleFileExtensions; possibleFileExtensions.push_back(".dwi"); possibleFileExtensions.push_back(".hdwi"); + possibleFileExtensions.push_back(".fsl"); + possibleFileExtensions.push_back(".fslgz"); return possibleFileExtensions; } #endif //__mitkNrrdDiffusionImageWriter__cpp diff --git a/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkNrrdDiffusionImageWriter.h b/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkNrrdDiffusionImageWriter.h index bec5ed8ff0..fb14341f75 100644 --- a/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkNrrdDiffusionImageWriter.h +++ b/Modules/DiffusionImaging/IODataStructures/DiffusionWeightedImages/mitkNrrdDiffusionImageWriter.h @@ -1,133 +1,133 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2008-08-27 17:18:46 +0200 (Mi, 27 Aug 2008) $ Version: $Revision: 15096 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. 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 _MITK_NRRDDIFFVOL_WRITER__H_ #define _MITK_NRRDDIFFVOL_WRITER__H_ #include #include #include namespace mitk { /** * Writes diffusion volumes to a file * @ingroup Process */ template < class TPixelType > class NrrdDiffusionImageWriter : public mitk::FileWriterWithInformation { public: mitkClassMacro( NrrdDiffusionImageWriter, mitk::FileWriterWithInformation ); mitkWriterMacro; itkNewMacro( Self ); typedef mitk::DiffusionImage InputType; /** * Sets the filename of the file to write. * @param FileName the name of the file to write. */ itkSetStringMacro( FileName ); /** * @returns the name of the file to be written to disk. */ itkGetStringMacro( FileName ); /** * @warning multiple write not (yet) supported */ itkSetStringMacro( FilePrefix ); /** * @warning multiple write not (yet) supported */ itkGetStringMacro( FilePrefix ); /** * @warning multiple write not (yet) supported */ itkSetStringMacro( FilePattern ); /** * @warning multiple write not (yet) supported */ itkGetStringMacro( FilePattern ); /** * Sets the input object for the filter. * @param input the diffusion volumes to write to file. */ void SetInput( InputType* input ); /** * @returns the 0'th input object of the filter. */ InputType* GetInput(); /** * Returns false if an error happened during writing */ itkGetMacro( Success, bool ); /** * @return possible file extensions for the data type associated with the writer */ virtual std::vector GetPossibleFileExtensions(); // FileWriterWithInformation methods virtual const char * GetDefaultFilename() { return "DiffusionWeightedImages.dwi"; } - virtual const char * GetFileDialogPattern() { return "Diffusion Weighted Images (*.dwi *.hdwi)"; } + virtual const char * GetFileDialogPattern() { return "Diffusion Weighted Images (*.dwi *.hdwi *.fsl)"; } virtual const char * GetDefaultExtension() { return ".dwi"; } virtual bool CanWriteDataType(BaseData::Pointer data) { return (dynamic_cast*>(data.GetPointer()) != NULL); }; virtual void DoWrite(BaseData::Pointer data) { if (CanWriteDataType(data)) { this->SetInput(dynamic_cast*>(data.GetPointer())); this->Update(); } }; protected: NrrdDiffusionImageWriter(); virtual ~NrrdDiffusionImageWriter(); virtual void GenerateData(); std::string m_FileName; std::string m_FilePrefix; std::string m_FilePattern; bool m_Success; }; } // end of namespace mitk #include "mitkNrrdDiffusionImageWriter.cpp" #endif diff --git a/Modules/DiffusionImaging/IODataStructures/mitkDiffusionImagingObjectFactory.cpp b/Modules/DiffusionImaging/IODataStructures/mitkDiffusionImagingObjectFactory.cpp index a53c4d090b..57a93e78ea 100644 --- a/Modules/DiffusionImaging/IODataStructures/mitkDiffusionImagingObjectFactory.cpp +++ b/Modules/DiffusionImaging/IODataStructures/mitkDiffusionImagingObjectFactory.cpp @@ -1,207 +1,213 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2009-06-18 15:59:04 +0200 (Do, 18 Jun 2009) $ Version: $Revision: 16916 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. 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 "mitkDiffusionImagingObjectFactory.h" #include "mitkProperties.h" #include "mitkBaseRenderer.h" #include "mitkDataNode.h" #include "mitkNrrdDiffusionImageIOFactory.h" #include "mitkNrrdDiffusionImageWriterFactory.h" #include "mitkNrrdDiffusionImageWriter.h" #include "mitkDiffusionImage.h" #include "mitkNrrdQBallImageIOFactory.h" #include "mitkNrrdQBallImageWriterFactory.h" #include "mitkNrrdQBallImageWriter.h" #include "mitkNrrdTensorImageIOFactory.h" #include "mitkNrrdTensorImageWriterFactory.h" #include "mitkNrrdTensorImageWriter.h" #include "mitkCompositeMapper.h" #include "mitkDiffusionImageMapper.h" #include "mitkGPUVolumeMapper3D.h" typedef short DiffusionPixelType; typedef mitk::DiffusionImage DiffusionImageShort; typedef std::multimap MultimapType; mitk::DiffusionImagingObjectFactory::DiffusionImagingObjectFactory(bool /*registerSelf*/) :CoreObjectFactoryBase() { static bool alreadyDone = false; if (!alreadyDone) { MITK_INFO << "DiffusionImagingObjectFactory c'tor" << std::endl; RegisterIOFactories(); mitk::NrrdDiffusionImageIOFactory::RegisterOneFactory(); mitk::NrrdQBallImageIOFactory::RegisterOneFactory(); mitk::NrrdTensorImageIOFactory::RegisterOneFactory(); mitk::NrrdDiffusionImageWriterFactory::RegisterOneFactory(); mitk::NrrdQBallImageWriterFactory::RegisterOneFactory(); mitk::NrrdTensorImageWriterFactory::RegisterOneFactory(); m_FileWriters.push_back( NrrdDiffusionImageWriter::New().GetPointer() ); m_FileWriters.push_back( NrrdQBallImageWriter::New().GetPointer() ); m_FileWriters.push_back( NrrdTensorImageWriter::New().GetPointer() ); mitk::CoreObjectFactory::GetInstance()->RegisterExtraFactory(this); CreateFileExtensionsMap(); alreadyDone = true; } } mitk::Mapper::Pointer mitk::DiffusionImagingObjectFactory::CreateMapper(mitk::DataNode* node, MapperSlotId id) { mitk::Mapper::Pointer newMapper=NULL; if ( id == mitk::BaseRenderer::Standard2D ) { std::string classname("QBallImage"); if(node->GetData() && classname.compare(node->GetData()->GetNameOfClass())==0) { newMapper = mitk::CompositeMapper::New(); newMapper->SetDataNode(node); node->SetMapper(3, ((CompositeMapper*)newMapper.GetPointer())->GetImageMapper()); } classname = "TensorImage"; if(node->GetData() && classname.compare(node->GetData()->GetNameOfClass())==0) { newMapper = mitk::CompositeMapper::New(); newMapper->SetDataNode(node); node->SetMapper(3, ((CompositeMapper*)newMapper.GetPointer())->GetImageMapper()); } classname = "DiffusionImage"; if(node->GetData() && classname.compare(node->GetData()->GetNameOfClass())==0) { newMapper = mitk::DiffusionImageMapper::New(); newMapper->SetDataNode(node); } } else if ( id == mitk::BaseRenderer::Standard3D ) { std::string classname("QBallImage"); if(node->GetData() && classname.compare(node->GetData()->GetNameOfClass())==0) { newMapper = mitk::GPUVolumeMapper3D::New(); newMapper->SetDataNode(node); } classname = "TensorImage"; if(node->GetData() && classname.compare(node->GetData()->GetNameOfClass())==0) { newMapper = mitk::GPUVolumeMapper3D::New(); newMapper->SetDataNode(node); } classname = "DiffusionImage"; if(node->GetData() && classname.compare(node->GetData()->GetNameOfClass())==0) { newMapper = mitk::GPUVolumeMapper3D::New(); newMapper->SetDataNode(node); } } return newMapper; } void mitk::DiffusionImagingObjectFactory::SetDefaultProperties(mitk::DataNode* node) { std::string classname = "QBallImage"; if(node->GetData() && classname.compare(node->GetData()->GetNameOfClass())==0) { mitk::CompositeMapper::SetDefaultProperties(node); mitk::GPUVolumeMapper3D::SetDefaultProperties(node); } classname = "TensorImage"; if(node->GetData() && classname.compare(node->GetData()->GetNameOfClass())==0) { mitk::CompositeMapper::SetDefaultProperties(node); mitk::GPUVolumeMapper3D::SetDefaultProperties(node); } classname = "DiffusionImage"; if(node->GetData() && classname.compare(node->GetData()->GetNameOfClass())==0) { mitk::DiffusionImageMapper::SetDefaultProperties(node); mitk::GPUVolumeMapper3D::SetDefaultProperties(node); } } const char* mitk::DiffusionImagingObjectFactory::GetFileExtensions() { std::string fileExtension; this->CreateFileExtensions(m_FileExtensionsMap, fileExtension); return fileExtension.c_str(); }; mitk::CoreObjectFactoryBase::MultimapType mitk::DiffusionImagingObjectFactory::GetFileExtensionsMap() { return m_FileExtensionsMap; } const char* mitk::DiffusionImagingObjectFactory::GetSaveFileExtensions() { std::string fileExtension; this->CreateFileExtensions(m_SaveFileExtensionsMap, fileExtension); return fileExtension.c_str(); }; mitk::CoreObjectFactoryBase::MultimapType mitk::DiffusionImagingObjectFactory::GetSaveFileExtensionsMap() { return m_SaveFileExtensionsMap; } void mitk::DiffusionImagingObjectFactory::CreateFileExtensionsMap() { m_FileExtensionsMap.insert(std::pair("*.dwi", "Diffusion Weighted Images")); m_FileExtensionsMap.insert(std::pair("*.hdwi", "Diffusion Weighted Images")); + m_FileExtensionsMap.insert(std::pair("*.nii", "Diffusion Weighted Images for FSL")); + m_FileExtensionsMap.insert(std::pair("*.fsl", "Diffusion Weighted Images for FSL")); + m_FileExtensionsMap.insert(std::pair("*.fslgz", "Diffusion Weighted Images for FSL")); m_FileExtensionsMap.insert(std::pair("*.qbi", "Q-Ball Images")); m_FileExtensionsMap.insert(std::pair("*.hqbi", "Q-Ball Images")); m_FileExtensionsMap.insert(std::pair("*.dti", "Tensor Images")); m_FileExtensionsMap.insert(std::pair("*.hdti", "Tensor Images")); m_SaveFileExtensionsMap.insert(std::pair("*.dwi", "Diffusion Weighted Images")); m_SaveFileExtensionsMap.insert(std::pair("*.hdwi", "Diffusion Weighted Images")); + m_SaveFileExtensionsMap.insert(std::pair("*.nii", "Diffusion Weighted Images for FSL")); + m_SaveFileExtensionsMap.insert(std::pair("*.fsl", "Diffusion Weighted Images for FSL")); + m_SaveFileExtensionsMap.insert(std::pair("*.fslgz", "Diffusion Weighted Images for FSL")); m_SaveFileExtensionsMap.insert(std::pair("*.qbi", "Q-Ball Images")); m_SaveFileExtensionsMap.insert(std::pair("*.hqbi", "Q-Ball Images")); m_SaveFileExtensionsMap.insert(std::pair("*.dti", "Tensor Images")); m_SaveFileExtensionsMap.insert(std::pair("*.hdti", "Tensor Images")); } void mitk::DiffusionImagingObjectFactory::RegisterIOFactories() { } void RegisterDiffusionImagingObjectFactory() { static bool oneDiffusionImagingObjectFactoryRegistered = false; if ( ! oneDiffusionImagingObjectFactoryRegistered ) { MITK_INFO << "Registering DiffusionImagingObjectFactory..." << std::endl; mitk::CoreObjectFactory::GetInstance()->RegisterExtraFactory(mitk::DiffusionImagingObjectFactory::New()); oneDiffusionImagingObjectFactoryRegistered = true; } }