14 #ifndef __itkVariableLengthVectorImageFileReader_hxx 15 #define __itkVariableLengthVectorImageFileReader_hxx 18 #include "itkImageRegionIterator.h" 19 #include "itksys/SystemTools.hxx" 24 template <
class TOutputImage>
29 m_UseStreaming =
true;
33 template <
class TOutputImage>
39 template <
class TOutputImage>
43 Superclass::PrintSelf(os, indent);
47 os << indent <<
"ImageIO: \n";
48 m_ImageIO->Print(os, indent.GetNextIndent());
52 os << indent <<
"ImageIO: (null)" <<
"\n";
55 os << indent <<
"m_FileName: " << m_FileName <<
"\n";
56 os << indent <<
"m_UseStreaming: " << m_UseStreaming <<
"\n";
59 template <
class TOutputImage>
63 itkDebugMacro ( <<
"VariableLengthVectorImageFileReader::GenerateData() \n" );
65 if (m_FileName.length()<4 || m_FileName.compare(m_FileName.length() - 4, 4,
".vlv")!=0)
67 itkExceptionMacro( <<
"the file " << m_FileName <<
" should be a text file ending with '.vlv'.");
70 std::string lengthFileName;
71 std::string valueFileName;
76 infile.open(m_FileName.c_str(), std::fstream::in);
78 if ( !infile.is_open() )
80 itkExceptionMacro( <<
"Cannot open file: " << m_FileName );
83 std::string fileName = itksys::SystemTools::GetFilenameName( m_FileName );
84 std::string pathName = itksys::SystemTools::GetFilenamePath( m_FileName );
86 std::string line, extractedLine;
95 unsigned int ndims = 0;
101 while (!infile.eof())
103 if (infile.getline(tempLine, 256))
105 line = std::string(tempLine);
107 if (line.find(
"NDims") != std::string::npos)
109 extractedLine = line.substr(line.find(
"=") + 1);
110 pch = strtok(const_cast<char*>(extractedLine.c_str()),
" ");
117 if (line.find(
"DimSize") != std::string::npos)
119 extractedLine = line.substr(line.find(
"=") + 1);
121 pch = strtok (const_cast<char*>(extractedLine.c_str()),
" ");
124 outputSize[idx] = atoi(pch);
126 pch = strtok (NULL,
" ");
130 if (line.find(
"ElementSpacing") != std::string::npos)
132 extractedLine = line.substr(line.find(
"=") + 1);
134 pch = strtok (const_cast<char*>(extractedLine.c_str()),
" ");
137 outputSpacing[idx] = atof(pch);
139 pch = strtok (NULL,
" ");
143 if (line.find(
"Offset") != std::string::npos)
145 extractedLine = line.substr(line.find(
"=") + 1);
147 pch = strtok (const_cast<char*>(extractedLine.c_str()),
" ");
150 outputOrigin[idx] = atof(pch);
152 pch = strtok (NULL,
" ");
156 if (line.find(
"TransformMatrix") != std::string::npos)
158 extractedLine = line.substr(line.find(
"=") + 1);
160 pch = strtok (const_cast<char*>(extractedLine.c_str()),
" ");
161 outputDirection.SetIdentity();
164 outputDirection( vcl_floor(idx / ImageDimension),
165 idx % ImageDimension ) = atof(pch);
167 pch = strtok (NULL,
" ");
171 if (line.find(
"LengthElementDataFile") != std::string::npos)
173 extractedLine = line.substr(line.find(
"=") + 1);
175 const size_t beginStr = extractedLine.find_first_not_of(
" \t");
176 if (beginStr == std::string::npos)
179 std::cerr <<
"No length file specified!";
181 const size_t endStr = extractedLine.find_last_not_of(
" \t");
182 const size_t range = endStr - beginStr + 1;
183 lengthFileName = extractedLine.substr(beginStr, range);
184 if ( pathName !=
"" )
186 lengthFileName = pathName +
"/" + lengthFileName;
190 if (line.find(
"ValueElementDataFile") != std::string::npos)
192 extractedLine = line.substr(line.find(
"=") + 1);
194 const size_t beginStr = extractedLine.find_first_not_of(
" \t");
195 if (beginStr == std::string::npos)
198 std::cerr <<
"No value file specified!";
200 const size_t endStr = extractedLine.find_last_not_of(
" \t");
201 const size_t range = endStr - beginStr + 1;
202 valueFileName = extractedLine.substr(beginStr, range);
203 if ( pathName !=
"" )
205 valueFileName = pathName +
"/" + valueFileName;
219 outputStartIndex.Fill(0);
220 outputRegion.SetIndex(outputStartIndex);
221 outputRegion.SetSize(outputSize);
222 output->SetRegions(outputRegion);
223 output->SetSpacing(outputSpacing);
225 output->SetOrigin(outputOrigin);
227 output->SetDirection(outputDirection);
235 m_LengthImageFileReader = LengthImageFileReaderType::New();
236 m_ValueImageFileReader = ValueImageFileReaderType::New();
238 m_LengthImageFileReader->SetFileName(lengthFileName);
239 m_ValueImageFileReader->SetFileName(valueFileName);
241 m_LengthImageFileReader->Update();
242 m_ValueImageFileReader->Update();
244 m_LengthImage = m_LengthImageFileReader->GetOutput();
245 m_ValueImage = m_ValueImageFileReader->GetOutput();
247 m_LengthImage->DisconnectPipeline();
248 m_ValueImage->DisconnectPipeline();
250 m_ImageIO = m_ValueImageFileReader->GetImageIO();
252 ImageRegionIterator<OutputImageType>
253 outputIt( output, output->GetLargestPossibleRegion() );
254 ImageRegionIterator<LengthImageType>
255 lengthIt( m_LengthImage, m_LengthImage->GetRequestedRegion() );
256 ImageRegionIterator<ValueImageType>
257 valueIt( m_ValueImage, m_ValueImage->GetRequestedRegion() );
260 outputIt.GoToBegin();
261 lengthIt.GoToBegin();
263 while ( !outputIt.IsAtEnd() )
267 outputPixel.SetSize(length);
270 outputPixel[k] = valueIt.Get();
273 outputIt.Set(outputPixel);
virtual void GenerateData() ITK_OVERRIDE
TOutputImage OutputImageType
OutputImageType::PixelType OutputImagePixelType
void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
OutputImageType::SizeType OutputImageSizeType
OutputImageType::RegionType OutputImageRegionType
OutputImageType::IndexType OutputImageIndexType
OutputImageType::DirectionType OutputImageDirectionType
OutputImageType::SpacingType OutputImageSpacingType
~VariableLengthVectorImageFileReader()
VariableLengthVectorImageFileReader()
OutputImageType::PointType OutputImagePointType