18 #ifndef itkVTKPolyDataMeshIO_h
19 #define itkVTKPolyDataMeshIO_h
20 #include "ITKIOMeshVTKExport.h"
76 CanReadFile(
const char * fileName)
override;
80 ReadMeshInformation()
override;
84 ReadPoints(
void * buffer)
override;
87 ReadCells(
void * buffer)
override;
90 ReadPointData(
void * buffer)
override;
93 ReadCellData(
void * buffer)
override;
102 CanWriteFile(
const char * fileName)
override;
106 WriteMeshInformation()
override;
111 WritePoints(
void * buffer)
override;
114 WriteCells(
void * buffer)
override;
117 WritePointData(
void * buffer)
override;
120 WriteCellData(
void * buffer)
override;
130 PrintSelf(std::ostream & os,
Indent indent)
const override;
134 GetNextLine(std::ifstream & ifs, std::string & line,
bool lowerCase =
true,
SizeValueType count = 0);
136 template <
typename T>
140 unsigned int numberOfVertices = 0;
141 unsigned int numberOfVertexIndices = 0;
142 unsigned int numberOfLines = 0;
144 unsigned int numberOfPolygons = 0;
145 unsigned int numberOfPolygonIndices = 0;
151 auto cellType = static_cast<CellGeometryEnum>(static_cast<int>(buffer[index++]));
152 auto nn = static_cast<unsigned int>(buffer[index++]);
157 numberOfVertexIndices += nn + 1;
161 numberOfLineIndices += nn + 1;
165 numberOfLineIndices += nn + 1;
169 numberOfPolygonIndices += nn + 1;
173 numberOfPolygonIndices += nn + 1;
177 numberOfPolygonIndices += nn + 1;
180 itkExceptionMacro(<<
"Currently we dont support this cell type");
187 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfVertices", numberOfVertices);
188 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfVertexIndices", numberOfVertexIndices);
189 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLines", numberOfLines);
190 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLineIndices", numberOfLineIndices);
191 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfPolygons", numberOfPolygons);
192 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfPolygonIndices", numberOfPolygonIndices);
196 template <
typename T>
202 while (!inputFile.eof())
204 std::getline(inputFile, line,
'\n');
206 if (line.find(
"POINTS") != std::string::npos)
209 Self::ReadComponentsAsASCII(inputFile, buffer, this->m_NumberOfPoints * this->m_PointDimension);
214 template <
typename T>
220 while (!inputFile.eof())
222 std::getline(inputFile, line,
'\n');
224 if (line.find(
"POINTS") != std::string::npos)
227 SizeValueType numberOfComponents = this->m_NumberOfPoints * this->m_PointDimension;
228 inputFile.read(reinterpret_cast<char *>(buffer), numberOfComponents *
sizeof(T));
239 ReadCellsBufferAsASCII(std::ifstream & inputFile,
void * buffer);
242 ReadCellsBufferAsBINARY(std::ifstream & inputFile,
void * buffer);
244 template <
typename T>
250 while (!inputFile.eof())
252 std::getline(inputFile, line,
'\n');
253 if (line.find(
"POINT_DATA") != std::string::npos)
255 if (!inputFile.eof())
257 std::getline(inputFile, line,
'\n');
261 itkExceptionMacro(
"UnExpected end of line while trying to read POINT_DATA");
265 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
267 if (!inputFile.eof())
269 std::getline(inputFile, line,
'\n');
270 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
272 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
277 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
283 Self::ReadComponentsAsASCII(
284 inputFile, buffer, this->m_NumberOfPointPixels * this->m_NumberOfPointPixelComponents);
289 template <
typename T>
295 while (!inputFile.eof())
297 std::getline(inputFile, line,
'\n');
298 if (line.find(
"POINT_DATA") != std::string::npos)
300 if (!inputFile.eof())
302 std::getline(inputFile, line,
'\n');
306 itkExceptionMacro(
"UnExpected end of line while trying to read POINT_DATA");
310 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
312 if (!inputFile.eof())
314 std::getline(inputFile, line,
'\n');
315 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
317 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
322 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
328 SizeValueType numberOfComponents = this->m_NumberOfPointPixels * this->m_NumberOfPointPixelComponents;
329 inputFile.read(reinterpret_cast<char *>(buffer), numberOfComponents *
sizeof(T));
339 template <
typename T>
345 while (!inputFile.eof())
347 std::getline(inputFile, line,
'\n');
348 if (line.find(
"CELL_DATA") != std::string::npos)
350 if (!inputFile.eof())
352 std::getline(inputFile, line,
'\n');
356 itkExceptionMacro(
"UnExpected end of line while trying to read CELL_DATA");
360 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
362 if (!inputFile.eof())
364 std::getline(inputFile, line,
'\n');
365 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
367 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
372 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
378 Self::ReadComponentsAsASCII(
379 inputFile, buffer, this->m_NumberOfCellPixels * this->m_NumberOfCellPixelComponents);
384 template <
typename T>
390 while (!inputFile.eof())
392 std::getline(inputFile, line,
'\n');
393 if (line.find(
"POINT_DATA") != std::string::npos)
395 if (!inputFile.eof())
397 std::getline(inputFile, line,
'\n');
401 itkExceptionMacro(
"UnExpected end of line while trying to read POINT_DATA");
405 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
407 if (!inputFile.eof())
409 std::getline(inputFile, line,
'\n');
410 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
412 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
417 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
422 SizeValueType numberOfComponents = this->m_NumberOfCellPixels * this->m_NumberOfCellPixelComponents;
423 inputFile.read(reinterpret_cast<char *>(buffer), numberOfComponents *
sizeof(T));
433 template <
typename T>
438 outputFile <<
"POINTS " << this->m_NumberOfPoints;
440 outputFile << pointComponentType <<
'\n';
441 for (
SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
443 for (
unsigned int jj = 0; jj < this->m_PointDimension - 1; ++jj)
448 outputFile <<
ConvertNumberToString(buffer[ii * this->m_PointDimension + this->m_PointDimension - 1]) <<
'\n';
454 template <
typename T>
459 outputFile <<
"POINTS " << this->m_NumberOfPoints;
460 outputFile << pointComponentType <<
"\n";
462 buffer, this->m_NumberOfPoints * this->m_PointDimension, &outputFile);
468 template <
typename T>
473 unsigned int numberOfVertices = 0;
474 unsigned int numberOfVertexIndices = 0;
475 unsigned int numberOfLines = 0;
476 unsigned int numberOfLineIndices = 0;
477 unsigned int numberOfPolygons = 0;
478 unsigned int numberOfPolygonIndices = 0;
483 ExposeMetaData<unsigned int>(metaDic,
"numberOfVertices", numberOfVertices);
484 if (numberOfVertices)
486 ExposeMetaData<unsigned int>(metaDic,
"numberOfVertexIndices", numberOfVertexIndices);
487 outputFile <<
"VERTICES " << numberOfVertices <<
" " << numberOfVertexIndices <<
'\n';
490 auto cellType = static_cast<CellGeometryEnum>(static_cast<int>(buffer[index++]));
491 auto nn = static_cast<unsigned int>(buffer[index++]);
495 for (
unsigned int jj = 0; jj < nn; ++jj)
497 outputFile <<
" " << buffer[index++];
510 ExposeMetaData<unsigned int>(metaDic,
"numberOfLines", numberOfLines);
513 numberOfLineIndices = 0;
519 auto cellType = static_cast<CellGeometryEnum>(static_cast<int>(buffer[index++]));
520 auto nn = static_cast<unsigned int>(buffer[index++]);
526 pointIds.push_back(static_cast<SizeValueType>(buffer[index]));
527 pointIds.push_back(static_cast<SizeValueType>(buffer[index + 1]));
531 for (
unsigned int jj = 0; jj < nn; ++jj)
533 pointIds.push_back(static_cast<SizeValueType>(buffer[index + jj]));
537 polylines->InsertElement(numberOfPolylines++, pointIds);
538 numberOfLineIndices += pointIds.size();
542 numberOfLines = polylines->Size();
543 numberOfLineIndices += numberOfLines;
544 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLines", numberOfLines);
545 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLineIndices", numberOfLineIndices);
546 outputFile <<
"LINES " << numberOfLines <<
" " << numberOfLineIndices <<
'\n';
549 auto nn = static_cast<unsigned int>(polylines->ElementAt(ii).size());
551 for (
unsigned int jj = 0; jj < nn; ++jj)
553 outputFile <<
" " << polylines->ElementAt(ii)[jj];
561 ExposeMetaData<unsigned int>(metaDic,
"numberOfPolygons", numberOfPolygons);
562 if (numberOfPolygons)
564 ExposeMetaData<unsigned int>(metaDic,
"numberOfPolygonIndices", numberOfPolygonIndices);
565 outputFile <<
"POLYGONS " << numberOfPolygons <<
" " << numberOfPolygonIndices <<
'\n';
568 auto cellType = static_cast<CellGeometryEnum>(static_cast<int>(buffer[index++]));
569 auto nn = static_cast<unsigned int>(buffer[index++]);
574 for (
unsigned int jj = 0; jj < nn; ++jj)
576 outputFile <<
" " << buffer[index++];
589 template <
typename T>
594 unsigned int numberOfVertices = 0;
595 unsigned int numberOfVertexIndices = 0;
596 unsigned int numberOfLines = 0;
597 unsigned int numberOfLineIndices = 0;
598 unsigned int numberOfPolygons = 0;
599 unsigned int numberOfPolygonIndices = 0;
604 ExposeMetaData<unsigned int>(metaDic,
"numberOfVertices", numberOfVertices);
605 if (numberOfVertices)
607 ExposeMetaData<unsigned int>(metaDic,
"numberOfVertexIndices", numberOfVertexIndices);
608 outputFile <<
"VERTICES " << numberOfVertices <<
" " << numberOfVertexIndices <<
'\n';
609 const auto data = make_unique_for_overwrite<unsigned int[]>(numberOfVertexIndices);
610 ReadCellsBuffer(buffer, data.get());
612 data.get(), numberOfVertexIndices, &outputFile);
618 ExposeMetaData<unsigned int>(metaDic,
"numberOfLines", numberOfLines);
621 numberOfLineIndices = 0;
627 auto cellType = static_cast<CellGeometryEnum>(static_cast<int>(buffer[index++]));
628 auto nn = static_cast<unsigned int>(buffer[index++]);
634 pointIds.push_back(static_cast<SizeValueType>(buffer[index]));
635 pointIds.push_back(static_cast<SizeValueType>(buffer[index + 1]));
639 for (
unsigned int jj = 0; jj < nn; ++jj)
641 pointIds.push_back(static_cast<SizeValueType>(buffer[index + jj]));
644 polylines->InsertElement(numberOfPolylines++, pointIds);
645 numberOfLineIndices += pointIds.size();
649 numberOfLines = polylines->Size();
650 numberOfLineIndices += numberOfLines;
651 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLines", numberOfLines);
652 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLineIndices", numberOfLineIndices);
654 outputFile <<
"LINES " << numberOfLines <<
" " << numberOfLineIndices <<
'\n';
655 const auto data = make_unique_for_overwrite<unsigned int[]>(numberOfLineIndices);
656 unsigned long outputIndex = 0;
659 auto nn = static_cast<unsigned int>(polylines->ElementAt(ii).size());
660 data[outputIndex++] = nn;
661 for (
unsigned int jj = 0; jj < nn; ++jj)
663 data[outputIndex++] = polylines->ElementAt(ii)[jj];
673 ExposeMetaData<unsigned int>(metaDic,
"numberOfPolygons", numberOfPolygons);
674 if (numberOfPolygons)
676 ExposeMetaData<unsigned int>(metaDic,
"numberOfPolygonIndices", numberOfPolygonIndices);
677 outputFile <<
"POLYGONS " << numberOfPolygons <<
" " << numberOfPolygonIndices <<
'\n';
678 const auto data = make_unique_for_overwrite<unsigned int[]>(numberOfPolygonIndices);
679 ReadCellsBuffer(buffer, data.get());
681 data.get(), numberOfPolygonIndices, &outputFile);
687 template <
typename T>
694 outputFile <<
"POINT_DATA " << this->m_NumberOfPointPixels <<
'\n';
695 switch (this->m_PointPixelType)
699 outputFile <<
"SCALARS ";
700 ExposeMetaData<StringType>(metaDic,
"pointScalarDataName", dataName);
701 outputFile << dataName <<
" ";
709 outputFile <<
"VECTORS ";
710 ExposeMetaData<StringType>(metaDic,
"pointVectorDataName", dataName);
711 outputFile << dataName <<
" ";
717 outputFile <<
"TENSORS ";
718 ExposeMetaData<StringType>(metaDic,
"pointTensorDataName", dataName);
719 outputFile << dataName <<
" ";
725 outputFile <<
"COLOR_SCALARS ";
726 ExposeMetaData<StringType>(metaDic,
"pointColorScalarDataName", dataName);
727 outputFile << dataName <<
" ";
728 WriteColorScalarBufferAsASCII(
729 outputFile, buffer, this->m_NumberOfPointPixelComponents, this->m_NumberOfPointPixels);
734 itkExceptionMacro(<<
"Unknown point pixel type");
738 outputFile << pointPixelComponentName <<
'\n';
742 outputFile <<
"LOOKUP_TABLE default" <<
'\n';
750 const SizeValueType num = this->m_NumberOfPointPixelComponents * this->m_NumberOfPointPixels;
753 if (this->m_NumberOfPointPixelComponents == 3)
774 else if (this->m_NumberOfPointPixelComponents == 6)
801 itk::ExceptionObject e_(
802 __FILE__, __LINE__,
"itk::ERROR: VTKImageIO2: Unsupported number of components in tensor.", ITK_LOCATION);
809 for (
SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ++ii)
811 for (jj = 0; jj < this->m_NumberOfPointPixelComponents - 1; ++jj)
813 outputFile <<
ConvertNumberToString(buffer[ii * this->m_NumberOfPointPixelComponents + jj]) << indent;
823 template <
typename T>
830 outputFile <<
"POINT_DATA " << this->m_NumberOfPointPixels <<
"\n";
831 switch (this->m_PointPixelType)
835 outputFile <<
"SCALARS ";
836 ExposeMetaData<StringType>(metaDic,
"pointScalarDataName", dataName);
837 outputFile << dataName <<
" ";
845 outputFile <<
"VECTORS ";
846 ExposeMetaData<StringType>(metaDic,
"pointVectorDataName", dataName);
847 outputFile << dataName <<
" ";
853 outputFile <<
"TENSORS ";
854 ExposeMetaData<StringType>(metaDic,
"pointTensorDataName", dataName);
855 outputFile << dataName <<
" ";
861 outputFile <<
"COLOR_SCALARS ";
862 ExposeMetaData<StringType>(metaDic,
"pointColorScalarDataName", dataName);
863 outputFile << dataName <<
" ";
864 WriteColorScalarBufferAsBINARY(
865 outputFile, buffer, this->m_NumberOfPointPixelComponents, this->m_NumberOfPointPixels);
870 itkExceptionMacro(<<
"Unknown point pixel type");
874 outputFile << pointPixelComponentName <<
"\n";
877 outputFile <<
"LOOKUP_TABLE default\n";
881 buffer, this->m_NumberOfPointPixels * this->m_NumberOfPointPixelComponents, &outputFile);
886 template <
typename T>
893 outputFile <<
"CELL_DATA " << this->m_NumberOfCellPixels <<
'\n';
894 switch (this->m_CellPixelType)
898 outputFile <<
"SCALARS ";
899 ExposeMetaData<StringType>(metaDic,
"cellScalarDataName", dataName);
900 outputFile << dataName <<
" ";
908 outputFile <<
"VECTORS ";
909 ExposeMetaData<StringType>(metaDic,
"cellVectorDataName", dataName);
910 outputFile << dataName <<
" ";
916 outputFile <<
"TENSORS ";
917 ExposeMetaData<StringType>(metaDic,
"cellTensorDataName", dataName);
918 outputFile << dataName <<
" ";
924 outputFile <<
"COLOR_SCALARS ";
925 ExposeMetaData<StringType>(metaDic,
"cellColorScalarDataName", dataName);
926 outputFile << dataName <<
" ";
927 WriteColorScalarBufferAsASCII(
928 outputFile, buffer, this->m_NumberOfCellPixelComponents, this->m_NumberOfCellPixels);
933 itkExceptionMacro(<<
"Unknown cell pixel type");
937 outputFile << cellPixelComponentName <<
'\n';
940 outputFile <<
"LOOKUP_TABLE default" <<
'\n';
948 const SizeValueType num = this->m_NumberOfCellPixelComponents * this->m_NumberOfCellPixels;
949 if (this->m_NumberOfCellPixelComponents == 2)
956 outputFile << *ptr++ << indent;
958 outputFile << e12 << indent;
959 outputFile << zero <<
'\n';
961 outputFile << e12 << indent;
962 outputFile << *ptr++ << indent;
963 outputFile << zero <<
'\n';
965 outputFile << zero << indent << zero << indent << zero <<
"\n\n";
969 else if (this->m_NumberOfCellPixelComponents == 3)
977 outputFile << *ptr++ << indent;
979 outputFile << e12 << indent;
981 outputFile << e13 <<
'\n';
983 outputFile << e12 << indent;
984 outputFile << *ptr++ << indent;
986 outputFile << e23 <<
'\n';
988 outputFile << e13 << indent;
989 outputFile << e23 << indent;
990 outputFile << *ptr++ <<
"\n\n";
996 ExceptionObject e_(__FILE__,
998 "itk::ERROR: VTKPolyDataMeshIO: Unsupported number of components in tensor.",
1006 for (
SizeValueType ii = 0; ii < this->m_NumberOfCellPixels; ++ii)
1008 for (jj = 0; jj < this->m_NumberOfCellPixelComponents - 1; ++jj)
1010 outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj] << indent;
1012 outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj];
1020 template <
typename T>
1027 outputFile <<
"CELL_DATA " << this->m_NumberOfCellPixels <<
"\n";
1028 switch (this->m_CellPixelType)
1032 outputFile <<
"SCALARS ";
1033 ExposeMetaData<StringType>(metaDic,
"cellScalarDataName", dataName);
1034 outputFile << dataName <<
" ";
1042 outputFile <<
"VECTORS ";
1043 ExposeMetaData<StringType>(metaDic,
"cellVectorDataName", dataName);
1044 outputFile << dataName <<
" ";
1050 outputFile <<
"TENSORS ";
1051 ExposeMetaData<StringType>(metaDic,
"cellTensorDataName", dataName);
1052 outputFile << dataName <<
" ";
1058 outputFile <<
"COLOR_SCALARS ";
1059 ExposeMetaData<StringType>(metaDic,
"cellColorScalarDataName", dataName);
1060 outputFile << dataName <<
" ";
1061 WriteColorScalarBufferAsBINARY(
1062 outputFile, buffer, this->m_NumberOfCellPixelComponents, this->m_NumberOfCellPixels);
1067 itkExceptionMacro(<<
"Unknown cell pixel type");
1071 outputFile << cellPixelComponentName <<
"\n";
1074 outputFile <<
"LOOKUP_TABLE default\n";
1078 buffer, this->m_NumberOfCells * this->m_NumberOfCellPixelComponents, &outputFile);
1083 template <
typename T>
1087 unsigned int numberOfPixelComponents,
1090 outputFile << numberOfPixelComponents <<
"\n";
1094 for (
unsigned int jj = 0; jj < numberOfPixelComponents; ++jj)
1096 outputFile << ConvertNumberToString(static_cast<float>(buffer[ii * numberOfPixelComponents + jj])) << indent;
1105 template <
typename T>
1109 unsigned int numberOfPixelComponents,
1112 outputFile << numberOfPixelComponents <<
"\n";
1113 SizeValueType numberOfElements = numberOfPixelComponents * numberOfPixels;
1114 const auto data = make_unique_for_overwrite<unsigned char[]>(numberOfElements);
1117 data[ii] = static_cast<unsigned char>(buffer[ii]);
1120 outputFile.write(reinterpret_cast<char *>(data.get()), numberOfElements);
1127 template <
typename TInput,
typename TOutput>
1134 if (input && output)
1136 for (
SizeValueType ii = 0; ii < this->m_NumberOfCells; ++ii)
1139 auto nn = static_cast<unsigned int>(input[inputIndex++]);
1140 output[outputIndex++] = nn;
1141 for (
unsigned int jj = 0; jj < nn; ++jj)
1143 output[outputIndex++] = static_cast<TOutput>(input[inputIndex++]);
1151 GetComponentTypeFromString(
const std::string & pointType);
1158 template <
typename T>
1164 if (!(inputFile >> buffer[i]))
1166 itkGenericExceptionMacro(
"Failed to read a component from the specified ASCII input file!");
1173 ReadComponentsAsASCII(std::ifstream & inputFile,
float *
const buffer,
const SizeValueType numberOfComponents);
1176 ReadComponentsAsASCII(std::ifstream & inputFile,
double *
const buffer,
const SizeValueType numberOfComponents);
1180 #endif // itkVTKPolyDataMeshIO_h