15 #include <itkImageRegionIterator.h> 21 template <
class PixelType,
unsigned int ImageDimension>
22 MCMFileReader <PixelType, ImageDimension>
28 template <
class PixelType,
unsigned int ImageDimension>
34 template <
class PixelType,
unsigned int ImageDimension>
39 tinyxml2::XMLDocument doc;
40 tinyxml2::XMLError loadOk = doc.LoadFile(m_FileName.c_str());
42 std::replace(m_FileName.begin(),m_FileName.end(),
'\\',
'/');
43 std::string basePath, baseName;
44 std::size_t lastSlashPos = m_FileName.find_last_of(
"/");
46 if (lastSlashPos != std::string::npos)
48 basePath.append(m_FileName.begin(),m_FileName.begin() + lastSlashPos);
54 if (basePath.length() != 0)
55 basePath = basePath +
"/";
57 std::size_t lastDotPos = m_FileName.find_last_of(
".");
58 baseName.append(m_FileName.begin() + lastSlashPos, m_FileName.begin() + lastDotPos);
60 if (loadOk != tinyxml2::XML_SUCCESS)
62 std::string error(
"Unable to read input summary file: ");
64 throw itk::ExceptionObject(__FILE__, __LINE__,error,ITK_LOCATION);
68 tinyxml2::XMLElement *modelNode = doc.FirstChildElement(
"Model" );
71 std::string error(
"Model node missing in ");
73 throw itk::ExceptionObject(__FILE__, __LINE__,error,ITK_LOCATION);
76 tinyxml2::XMLElement *weightsNode = modelNode->FirstChildElement(
"Weights" );
80 std::string error(
"Weights file missing in ");
82 throw itk::ExceptionObject(__FILE__, __LINE__,error,ITK_LOCATION);
85 std::string weightsFileName = basePath + baseName +
"/";
86 weightsFileName += weightsNode->GetText();
89 unsigned int numCompartments = weightsImage->GetVectorLength();
91 tinyxml2::XMLElement *compartmentNode = modelNode->FirstChildElement(
"Compartment" );
94 std::vector <BaseInputImagePointer> compartmentImages;
95 while (compartmentNode)
97 tinyxml2::XMLElement *typeNode = compartmentNode->FirstChildElement(
"Type");
98 std::string compartmentType = typeNode->GetText();
102 referenceModel->AddCompartment(1.0 / numCompartments,additionalCompartment);
104 tinyxml2::XMLElement *fileNameNode = compartmentNode->FirstChildElement(
"FileName");
105 std::string imageFileName = basePath + baseName +
"/";
106 imageFileName += fileNameNode->GetText();
108 compartmentImages.push_back(anima::readImage <BaseInputImageType> (imageFileName));
110 compartmentNode = compartmentNode->NextSiblingElement(
"Compartment");
113 unsigned int vectorFinalSize = referenceModel->GetSize();
114 m_OutputImage = OutputImageType::New();
115 m_OutputImage->Initialize();
116 m_OutputImage->CopyInformation(compartmentImages[0]);
117 m_OutputImage->SetRegions(compartmentImages[0]->GetLargestPossibleRegion());
118 m_OutputImage->SetNumberOfComponentsPerPixel(vectorFinalSize);
119 m_OutputImage->Allocate();
120 m_OutputImage->SetDescriptionModel(referenceModel);
122 typedef itk::ImageRegionIterator <BaseInputImageType> InputImageIteratorType;
123 typedef itk::ImageRegionIterator <OutputImageType> OutputImageIteratorType;
125 std::vector <InputImageIteratorType> inputIterators(numCompartments);
126 for (
unsigned int i = 0;i < numCompartments;++i)
127 inputIterators[i] = InputImageIteratorType(compartmentImages[i],m_OutputImage->GetLargestPossibleRegion());
129 InputImageIteratorType weightsIterator(weightsImage,m_OutputImage->GetLargestPossibleRegion());
130 OutputImageIteratorType outItr(m_OutputImage,m_OutputImage->GetLargestPossibleRegion());
132 typedef typename OutputImageType::PixelType ImagePixelType;
133 ImagePixelType outValue(vectorFinalSize);
134 ImagePixelType weightsData(vectorFinalSize);
136 ImagePixelType inputCompartmentValue;
140 while (!outItr.IsAtEnd())
142 weightsData = weightsIterator.Get();
143 for (
unsigned int i = 0;i < numCompartments;++i)
144 weightsVector[i] = weightsData[i];
146 referenceModel->SetCompartmentWeights(weightsVector);
148 for (
unsigned int i = 0;i < numCompartments;++i)
150 inputCompartmentValue = inputIterators[i].Get();
151 unsigned int compartmentSize = inputCompartmentValue.GetSize();
153 if (inputMCMValue.GetSize() != compartmentSize)
154 inputMCMValue.SetSize(compartmentSize);
155 for (
unsigned int j = 0;j < compartmentSize;++j)
156 inputMCMValue[j] = inputCompartmentValue[j];
158 referenceModel->GetCompartment(i)->SetCompartmentVector(inputMCMValue);
161 outValue = referenceModel->GetModelVector();
162 outItr.Set(outValue);
166 for (
unsigned int i = 0;i < numCompartments;++i)
171 template <
class PixelType,
unsigned int ImageDimension>
174 ::CreateCompartmentForType(std::string &compartmentType)
178 if (compartmentType ==
"FreeWater")
180 else if (compartmentType ==
"StationaryWater")
182 else if (compartmentType ==
"IRWater")
184 else if (compartmentType ==
"Stanisz")
186 else if (compartmentType ==
"Stick")
188 else if (compartmentType ==
"Zeppelin")
190 else if (compartmentType ==
"Tensor")
192 else if (compartmentType ==
"NODDI")
196 std::string error(
"Unsupported compartment type: ");
197 error += compartmentType;
198 throw itk::ExceptionObject(__FILE__, __LINE__,error,ITK_LOCATION);
201 return additionalCompartment;
BaseCompartment::ListType ListType
itk::SmartPointer< Self > Pointer
BaseInputImageType::Pointer BaseInputImagePointer
BaseCompartment::ModelOutputVectorType ModelOutputVectorType
ModelType::Pointer ModelPointer