4 #include <vtkPolyDataReader.h> 5 #include <vtkXMLPolyDataReader.h> 6 #include <vtksys/SystemTools.hxx> 9 #include <vtkDoubleArray.h> 10 #include <vtkPointData.h> 20 std::string extensionName = m_FileName.substr(m_FileName.find_last_of(
'.') + 1);
22 if (extensionName ==
"vtk")
24 else if ((extensionName ==
"vtp")||(extensionName ==
""))
26 if (extensionName ==
"")
30 else if (extensionName ==
"fds")
32 else if (extensionName ==
"csv")
35 throw itk::ExceptionObject(__FILE__, __LINE__,
"Unsupported shapes extension.",ITK_LOCATION);
40 vtkSmartPointer <vtkPolyDataReader> vtkReader = vtkPolyDataReader::New();
41 vtkReader->SetFileName(m_FileName.c_str());
44 m_OutputData = vtkSmartPointer <vtkPolyData>::New();
45 m_OutputData->ShallowCopy(vtkReader->GetOutput());
50 vtkSmartPointer <vtkXMLPolyDataReader> vtkReader = vtkXMLPolyDataReader::New();
51 vtkReader->SetFileName(m_FileName.c_str());
54 m_OutputData = vtkSmartPointer <vtkPolyData>::New();
55 m_OutputData->ShallowCopy(vtkReader->GetOutput());
60 std::replace(m_FileName.begin(),m_FileName.end(),
'\\',
'/');
63 std::size_t lastSlashPos = m_FileName.find_last_of(
'/');
64 if (lastSlashPos != std::string::npos)
65 baseName.append(m_FileName.begin(),m_FileName.begin() + lastSlashPos + 1);
67 tinyxml2::XMLDocument doc;
68 tinyxml2::XMLError loadOk = doc.LoadFile(m_FileName.c_str());
70 if (loadOk != tinyxml2::XML_SUCCESS)
71 throw itk::ExceptionObject(__FILE__, __LINE__,
"Error reading XML FDS file header",ITK_LOCATION);
73 tinyxml2::XMLElement *vtkFileNode = doc.FirstChildElement(
"VTKFile");
75 throw itk::ExceptionObject(__FILE__, __LINE__,
"Malformed FDS file",ITK_LOCATION);
77 tinyxml2::XMLElement *datasetNode = vtkFileNode->FirstChildElement(
"vtkFiberDataSet");
79 throw itk::ExceptionObject(__FILE__, __LINE__,
"Malformed FDS file",ITK_LOCATION);
81 tinyxml2::XMLElement *fibersNode = datasetNode->FirstChildElement(
"Fibers");
82 std::string vtkFileName = baseName + fibersNode->Attribute(
"file");
84 std::string extensionName = vtkFileName.substr(vtkFileName.find_last_of(
'.') + 1);
86 if (extensionName ==
"vtk")
88 vtkSmartPointer <vtkPolyDataReader> vtkReader = vtkPolyDataReader::New();
89 vtkReader->SetFileName(vtkFileName.c_str());
92 m_OutputData = vtkSmartPointer <vtkPolyData>::New();
93 m_OutputData->ShallowCopy(vtkReader->GetOutput());
95 else if (extensionName ==
"vtp")
97 vtkSmartPointer <vtkXMLPolyDataReader> vtkReader = vtkXMLPolyDataReader::New();
98 vtkReader->SetFileName(vtkFileName.c_str());
101 m_OutputData = vtkSmartPointer <vtkPolyData>::New();
102 m_OutputData->ShallowCopy(vtkReader->GetOutput());
105 throw itk::ExceptionObject(__FILE__, __LINE__,
"Unsupported fibers extension inside FDS files.",ITK_LOCATION);
110 std::string extension = vtksys::SystemTools::GetFilenameLastExtension(m_FileName);
111 std::ifstream file(m_FileName);
114 throw itk::ExceptionObject(__FILE__, __LINE__,
"The input file does not exist.", ITK_LOCATION);
117 std::vector<std::string> header;
118 std::string file_line;
119 std::getline(file, file_line);
120 std::stringstream iss(file_line);
121 unsigned int numberOfColumns = 0;
123 std::cout <<
"Header:" << std::endl;
127 std::getline(iss, val,
',');
128 std::stringstream convertor(val);
130 header.resize(numberOfColumns + 1);
131 convertor >> header[numberOfColumns];
132 std::cout <<
" " << header[numberOfColumns] << std::endl;
136 std::cout <<
"Number of columns: " << numberOfColumns << std::endl;
138 if (numberOfColumns < 5)
139 throw itk::ExceptionObject(__FILE__, __LINE__,
"The CSV should contain at least 5 columns.", ITK_LOCATION);
141 if (header[0] !=
"X" || header[1] !=
"Y" || header[2] !=
"Z" || header[3] !=
"PointId" || header[4] !=
"StreamlineId")
142 throw itk::ExceptionObject(__FILE__, __LINE__,
"The CSV should contain at least the following first 5 variables in order: X, Y, Z, PointId, StreamlineId.", ITK_LOCATION);
144 std::vector<unsigned int> numberOfComponents;
145 unsigned int pos = 5;
146 while (pos < numberOfColumns)
149 if (header[pos].find_last_of(
"#") == -1)
151 numberOfComponents.push_back(1);
156 unsigned int count = 0;
157 while (header[pos+count].find_last_of(
"#") != -1)
159 numberOfComponents.push_back(count);
165 std::vector< std::vector<double> > data;
166 unsigned int numberOfRows = 0;
170 data.resize(numberOfRows + 1);
171 data[numberOfRows].resize(numberOfColumns);
172 std::string file_line;
173 std::getline(file, file_line);
174 std::stringstream iss(file_line);
176 for (
unsigned int i = 0;i < numberOfColumns;++i)
179 std::getline(iss, val,
',');
180 std::stringstream convertor(val);
183 data[numberOfRows][i] = std::numeric_limits<double>::quiet_NaN();
185 convertor >> data[numberOfRows][i];
195 bool isoFormat =
true;
196 for (
unsigned int j = 0;j < numberOfColumns;++j)
197 if (!std::isnan(data[numberOfRows - 1][j]))
207 std::cout <<
"Number of data points: " << numberOfRows << std::endl;
210 m_OutputData = vtkSmartPointer<vtkPolyData>::New();
211 m_OutputData->Initialize();
212 m_OutputData->Allocate();
218 vtkSmartPointer<vtkPoints> myPoints = vtkSmartPointer<vtkPoints>::New();
221 unsigned int numberOfStreamlines = data[numberOfRows-1][4];
222 std::cout <<
"Number of streamlines: " << numberOfStreamlines << std::endl;
224 unsigned int initialPosition = 0;
225 for (
unsigned int i = 0;i < numberOfStreamlines;++i)
228 unsigned int npts = 0;
229 while (initialPosition + npts < numberOfRows && data[initialPosition+npts][4] == i+1)
232 vtkIdType* ids =
new vtkIdType[npts];
234 for (
unsigned int j = 0;j < npts;++j)
236 unsigned int tmpPos = initialPosition + j;
237 ids[j] = myPoints->InsertNextPoint(data[tmpPos][0], data[tmpPos][1], data[tmpPos][2]);
240 m_OutputData->InsertNextCell(VTK_POLY_LINE, npts, ids);
242 initialPosition += npts;
245 m_OutputData->SetPoints(myPoints);
248 std::cout <<
"Number of arrays: " << numberOfComponents.size() << std::endl;
250 unsigned int arrayPos = 0;
251 while (pos < numberOfColumns)
253 unsigned int nbComponents = numberOfComponents[arrayPos];
255 vtkSmartPointer<vtkDoubleArray> arrayData = vtkSmartPointer<vtkDoubleArray>::New();
258 if (nbComponents == 1)
259 tmpStr = header[pos];
261 tmpStr = header[pos].substr(0, header[pos].find_last_of(
"#"));
263 arrayData->SetName(tmpStr.c_str());
264 arrayData->SetNumberOfComponents(nbComponents);
266 for (
unsigned int i = 0;i < numberOfRows;++i)
267 for (
unsigned int j = 0;j < nbComponents;++j)
268 arrayData->InsertNextValue(data[i][pos+j]);
270 m_OutputData->GetPointData()->AddArray(arrayData);
void ReadFileAsMedinriaFibers()
void ReadFileAsVTKAscii()