4 #include <itkImageRegionIterator.h> 5 #include <itkFileTools.h> 11 template <
class PixelType,
unsigned int ImageDimension>
12 MCMFileWriter <PixelType, ImageDimension>
18 template <
class PixelType,
unsigned int ImageDimension>
24 template <
class PixelType,
unsigned int ImageDimension>
27 ::SetFileName(std::string fileName)
29 if (fileName.find(
'.') != std::string::npos)
30 fileName.erase(fileName.find_first_of(
'.'));
31 m_FileName = fileName;
34 template <
class PixelType,
unsigned int ImageDimension>
40 throw itk::ExceptionObject(__FILE__, __LINE__,
"No filename specified for writing",ITK_LOCATION);
43 throw itk::ExceptionObject(__FILE__, __LINE__,
"No input file to write",ITK_LOCATION);
45 if (!m_InputImage->GetDescriptionModel())
46 throw itk::ExceptionObject(__FILE__, __LINE__,
"No reference model provided for writing MCM file",ITK_LOCATION);
48 std::replace(m_FileName.begin(),m_FileName.end(),
'\\',
'/');
49 std::string noPathName = m_FileName;
50 std::size_t lastSlashPos = m_FileName.find_last_of(
"/");
52 if (lastSlashPos != std::string::npos)
55 noPathName.append(m_FileName.begin() + lastSlashPos + 1,m_FileName.end());
58 itk::FileTools::CreateDirectory(m_FileName.c_str());
59 std::string headerName = m_FileName +
".mcm";
60 std::ofstream outputHeaderFile(headerName.c_str());
61 outputHeaderFile <<
"<?xml version=\"1.0\"?>" << std::endl;
62 outputHeaderFile <<
"<Model>" << std::endl;
65 ModelPointer descriptionModel = m_InputImage->GetDescriptionModel();
67 unsigned int numberOfCompartments = descriptionModel->GetNumberOfCompartments();
69 weightsImage->Initialize();
70 weightsImage->SetRegions(m_InputImage->GetLargestPossibleRegion());
71 weightsImage->SetSpacing (m_InputImage->GetSpacing());
72 weightsImage->SetOrigin (m_InputImage->GetOrigin());
73 weightsImage->SetDirection (m_InputImage->GetDirection());
74 weightsImage->SetVectorLength(numberOfCompartments);
75 weightsImage->Allocate();
77 typedef itk::ImageRegionIterator <InputImageType> InputImageIteratorType;
78 typedef itk::ImageRegionIterator <BaseOutputImageType> BaseOutputImageIteratorType;
79 typedef typename InputImageType::PixelType VectorType;
81 BaseOutputImageIteratorType weightsItr(weightsImage,m_InputImage->GetLargestPossibleRegion());
82 InputImageIteratorType inputItr(m_InputImage,m_InputImage->GetLargestPossibleRegion());
84 VectorType tmpWeights(numberOfCompartments);
85 VectorType workVector;
86 while (!weightsItr.IsAtEnd())
88 workVector = inputItr.Get();
90 for (
unsigned int i = 0;i < numberOfCompartments;++i)
91 tmpWeights[i] = workVector[i];
93 weightsItr.Set(tmpWeights);
99 std::string weightsName = m_FileName +
"/";
100 weightsName += noPathName;
101 weightsName +=
"_weights.nrrd";
103 anima::writeImage <BaseOutputImageType> (weightsName,weightsImage);
104 std::string xmlFileNameWeights = noPathName +
"_weights.nrrd";
106 outputHeaderFile <<
"<Weights>" << xmlFileNameWeights <<
"</Weights>" << std::endl;
109 unsigned int pos = numberOfCompartments;
111 for (
unsigned int i = 0;i < descriptionModel->GetNumberOfCompartments();++i)
113 outputHeaderFile <<
"<Compartment>" << std::endl;
114 switch(descriptionModel->GetCompartment(i)->GetCompartmentType())
117 outputHeaderFile <<
"<Type>Stick</Type>" << std::endl;
121 outputHeaderFile <<
"<Type>Zeppelin</Type>" << std::endl;
125 outputHeaderFile <<
"<Type>Tensor</Type>" << std::endl;
129 outputHeaderFile <<
"<Type>NODDI</Type>" << std::endl;
133 outputHeaderFile <<
"<Type>DDI</Type>" << std::endl;
137 outputHeaderFile <<
"<Type>FreeWater</Type>" << std::endl;
141 outputHeaderFile <<
"<Type>StationaryWater</Type>" << std::endl;
145 outputHeaderFile <<
"<Type>Stanisz</Type>" << std::endl;
150 outputHeaderFile <<
"<Type>IRWater</Type>" << std::endl;
155 unsigned int compartmentSize = descriptionModel->GetCompartment(i)->GetCompartmentSize();
158 compartmentImage->Initialize();
159 compartmentImage->SetRegions(m_InputImage->GetLargestPossibleRegion());
160 compartmentImage->SetSpacing (m_InputImage->GetSpacing());
161 compartmentImage->SetOrigin (m_InputImage->GetOrigin());
162 compartmentImage->SetDirection (m_InputImage->GetDirection());
163 compartmentImage->SetVectorLength(compartmentSize);
164 compartmentImage->Allocate();
166 inputItr.GoToBegin();
167 BaseOutputImageIteratorType compartmentItr(compartmentImage,m_InputImage->GetLargestPossibleRegion());
169 VectorType tmpCompartment(compartmentSize);
170 while (!compartmentItr.IsAtEnd())
172 workVector = inputItr.Get();
174 for (
unsigned int j = 0;j < compartmentSize;++j)
175 tmpCompartment[j] = workVector[pos + j];
177 compartmentItr.Set(tmpCompartment);
183 pos += compartmentSize;
185 std::string fullPathCompartmentName = m_FileName +
"/";
186 std::string compartmentName = noPathName +
"_";
189 sprintf(tmpStr,
"%d",i);
190 compartmentName += tmpStr;
191 compartmentName +=
".nrrd";
193 fullPathCompartmentName += compartmentName;
195 std::string xmlFileNameCompartment = compartmentName;
197 anima::writeImage <BaseOutputImageType> (fullPathCompartmentName,compartmentImage);
198 outputHeaderFile <<
"<FileName>" << xmlFileNameCompartment <<
"</FileName>" << std::endl;
200 outputHeaderFile <<
"</Compartment>" << std::endl;
203 outputHeaderFile <<
"</Model>" << std::endl;
204 outputHeaderFile.close();
ModelType::Pointer ModelPointer
BaseOutputImageType::Pointer BaseOutputImagePointer