18 #ifndef itkVTKPolyDataMeshIO_h
19 #define itkVTKPolyDataMeshIO_h
20 #include "ITKIOMeshVTKExport.h"
75 CanReadFile(
const char *
fileName)
override;
79 ReadMeshInformation()
override;
83 ReadPoints(
void * buffer)
override;
86 ReadCells(
void * buffer)
override;
89 ReadPointData(
void * buffer)
override;
92 ReadCellData(
void * buffer)
override;
101 CanWriteFile(
const char *
fileName)
override;
105 WriteMeshInformation()
override;
110 WritePoints(
void * buffer)
override;
113 WriteCells(
void * buffer)
override;
116 WritePointData(
void * buffer)
override;
119 WriteCellData(
void * buffer)
override;
129 PrintSelf(std::ostream & os,
Indent indent)
const override;
133 GetNextLine(std::ifstream & ifs, std::string & line,
bool lowerCase =
true,
SizeValueType count = 0);
135 template <
typename T>
139 unsigned int numberOfVertices = 0;
140 unsigned int numberOfVertexIndices = 0;
141 unsigned int numberOfLines = 0;
143 unsigned int numberOfPolygons = 0;
144 unsigned int numberOfPolygonIndices = 0;
150 auto cellType = static_cast<CellGeometryEnum>(static_cast<int>(buffer[index++]));
151 auto nn = static_cast<unsigned int>(buffer[index++]);
156 numberOfVertexIndices += nn + 1;
160 numberOfLineIndices += nn + 1;
164 numberOfPolygonIndices += nn + 1;
168 numberOfPolygonIndices += nn + 1;
172 numberOfPolygonIndices += nn + 1;
175 itkExceptionMacro(<<
"Currently we dont support this cell type");
182 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfVertices", numberOfVertices);
183 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfVertexIndices", numberOfVertexIndices);
184 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLines", numberOfLines);
185 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLineIndices", numberOfLineIndices);
186 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfPolygons", numberOfPolygons);
187 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfPolygonIndices", numberOfPolygonIndices);
191 template <
typename T>
197 while (!inputFile.eof())
199 std::getline(inputFile, line,
'\n');
201 if (line.find(
"POINTS") != std::string::npos)
204 SizeValueType numberOfComponents = this->m_NumberOfPoints * this->m_PointDimension;
207 inputFile >> buffer[ii];
213 template <
typename T>
219 while (!inputFile.eof())
221 std::getline(inputFile, line,
'\n');
223 if (line.find(
"POINTS") != std::string::npos)
226 SizeValueType numberOfComponents = this->m_NumberOfPoints * this->m_PointDimension;
227 inputFile.read(reinterpret_cast<char *>(buffer), numberOfComponents *
sizeof(T));
238 ReadCellsBufferAsASCII(std::ifstream & inputFile,
void * buffer);
241 ReadCellsBufferAsBINARY(std::ifstream & inputFile,
void * buffer);
243 template <
typename T>
249 while (!inputFile.eof())
251 std::getline(inputFile, line,
'\n');
252 if (line.find(
"POINT_DATA") != std::string::npos)
254 if (!inputFile.eof())
256 std::getline(inputFile, line,
'\n');
260 itkExceptionMacro(
"UnExpected end of line while trying to read POINT_DATA");
264 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
266 if (!inputFile.eof())
268 std::getline(inputFile, line,
'\n');
269 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
271 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
276 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
282 SizeValueType numberOfComponents = this->m_NumberOfPointPixels * this->m_NumberOfPointPixelComponents;
285 inputFile >> buffer[ii];
291 template <
typename T>
297 while (!inputFile.eof())
299 std::getline(inputFile, line,
'\n');
300 if (line.find(
"POINT_DATA") != std::string::npos)
302 if (!inputFile.eof())
304 std::getline(inputFile, line,
'\n');
308 itkExceptionMacro(
"UnExpected end of line while trying to read POINT_DATA");
312 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
314 if (!inputFile.eof())
316 std::getline(inputFile, line,
'\n');
317 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
319 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
324 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
330 SizeValueType numberOfComponents = this->m_NumberOfPointPixels * this->m_NumberOfPointPixelComponents;
331 inputFile.read(reinterpret_cast<char *>(buffer), numberOfComponents *
sizeof(T));
341 template <
typename T>
347 while (!inputFile.eof())
349 std::getline(inputFile, line,
'\n');
350 if (line.find(
"CELL_DATA") != std::string::npos)
352 if (!inputFile.eof())
354 std::getline(inputFile, line,
'\n');
358 itkExceptionMacro(
"UnExpected end of line while trying to read CELL_DATA");
362 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
364 if (!inputFile.eof())
366 std::getline(inputFile, line,
'\n');
367 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
369 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
374 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
380 SizeValueType numberOfComponents = this->m_NumberOfCellPixels * this->m_NumberOfCellPixelComponents;
383 inputFile >> buffer[ii];
389 template <
typename T>
395 while (!inputFile.eof())
397 std::getline(inputFile, line,
'\n');
398 if (line.find(
"POINT_DATA") != std::string::npos)
400 if (!inputFile.eof())
402 std::getline(inputFile, line,
'\n');
406 itkExceptionMacro(
"UnExpected end of line while trying to read POINT_DATA");
410 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
412 if (!inputFile.eof())
414 std::getline(inputFile, line,
'\n');
415 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
417 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
422 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
427 SizeValueType numberOfComponents = this->m_NumberOfCellPixels * this->m_NumberOfCellPixelComponents;
428 inputFile.read(reinterpret_cast<char *>(buffer), numberOfComponents *
sizeof(T));
438 template <
typename T>
444 outputFile <<
"POINTS " << this->m_NumberOfPoints;
446 outputFile << pointComponentType <<
'\n';
447 for (
SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++)
449 for (
unsigned int jj = 0; jj < this->m_PointDimension - 1; jj++)
451 outputFile << convert(buffer[ii * this->m_PointDimension + jj]) <<
" ";
454 outputFile << convert(buffer[ii * this->m_PointDimension + this->m_PointDimension - 1]) <<
'\n';
460 template <
typename T>
465 outputFile <<
"POINTS " << this->m_NumberOfPoints;
466 outputFile << pointComponentType <<
"\n";
468 buffer, this->m_NumberOfPoints * this->m_PointDimension, &outputFile);
474 template <
typename T>
479 unsigned int numberOfVertices = 0;
480 unsigned int numberOfVertexIndices = 0;
481 unsigned int numberOfLines = 0;
482 unsigned int numberOfLineIndices = 0;
483 unsigned int numberOfPolygons = 0;
484 unsigned int numberOfPolygonIndices = 0;
489 ExposeMetaData<unsigned int>(metaDic,
"numberOfVertices", numberOfVertices);
490 if (numberOfVertices)
492 ExposeMetaData<unsigned int>(metaDic,
"numberOfVertexIndices", numberOfVertexIndices);
493 outputFile <<
"VERTICES " << numberOfVertices <<
" " << numberOfVertexIndices <<
'\n';
496 auto cellType = static_cast<CellGeometryEnum>(static_cast<int>(buffer[index++]));
497 auto nn = static_cast<unsigned int>(buffer[index++]);
501 for (
unsigned int jj = 0; jj < nn; jj++)
503 outputFile <<
" " << buffer[index++];
516 ExposeMetaData<unsigned int>(metaDic,
"numberOfLines", numberOfLines);
519 numberOfLineIndices = 0;
525 auto cellType = static_cast<CellGeometryEnum>(static_cast<int>(buffer[index++]));
526 auto nn = static_cast<unsigned int>(buffer[index++]);
529 if (pointIds.size() >= nn)
532 if (
id == static_cast<SizeValueType>(buffer[index]))
534 pointIds.push_back(static_cast<SizeValueType>(buffer[index + 1]));
536 else if (
id == static_cast<SizeValueType>(buffer[index + 1]))
538 pointIds.push_back(static_cast<SizeValueType>(buffer[index]));
542 polylines->InsertElement(numberOfPolylines++, pointIds);
543 numberOfLineIndices += pointIds.size();
547 for (
unsigned int jj = 0; jj < nn; jj++)
549 pointIds.push_back(static_cast<SizeValueType>(buffer[index + jj]));
555 for (
unsigned int jj = 0; jj < nn; jj++)
557 pointIds.push_back(static_cast<SizeValueType>(buffer[index + jj]));
564 polylines->InsertElement(numberOfPolylines++, pointIds);
565 numberOfLineIndices += pointIds.size();
568 numberOfLines = polylines->Size();
569 numberOfLineIndices += numberOfLines;
570 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLines", numberOfLines);
571 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLineIndices", numberOfLineIndices);
572 outputFile <<
"LINES " << numberOfLines <<
" " << numberOfLineIndices <<
'\n';
575 auto nn = static_cast<unsigned int>(polylines->ElementAt(ii).size());
577 for (
unsigned int jj = 0; jj < nn; ++jj)
579 outputFile <<
" " << polylines->ElementAt(ii)[jj];
587 ExposeMetaData<unsigned int>(metaDic,
"numberOfPolygons", numberOfPolygons);
588 if (numberOfPolygons)
590 ExposeMetaData<unsigned int>(metaDic,
"numberOfPolygonIndices", numberOfPolygonIndices);
591 outputFile <<
"POLYGONS " << numberOfPolygons <<
" " << numberOfPolygonIndices <<
'\n';
594 auto cellType = static_cast<CellGeometryEnum>(static_cast<int>(buffer[index++]));
595 auto nn = static_cast<unsigned int>(buffer[index++]);
600 for (
unsigned int jj = 0; jj < nn; jj++)
602 outputFile <<
" " << buffer[index++];
615 template <
typename T>
620 unsigned int numberOfVertices = 0;
621 unsigned int numberOfVertexIndices = 0;
622 unsigned int numberOfLines = 0;
623 unsigned int numberOfLineIndices = 0;
624 unsigned int numberOfPolygons = 0;
625 unsigned int numberOfPolygonIndices = 0;
630 ExposeMetaData<unsigned int>(metaDic,
"numberOfVertices", numberOfVertices);
631 if (numberOfVertices)
633 ExposeMetaData<unsigned int>(metaDic,
"numberOfVertexIndices", numberOfVertexIndices);
634 outputFile <<
"VERTICES " << numberOfVertices <<
" " << numberOfVertexIndices <<
'\n';
635 auto * data =
new unsigned int[numberOfVertexIndices];
636 ReadCellsBuffer(buffer, data);
644 ExposeMetaData<unsigned int>(metaDic,
"numberOfLines", numberOfLines);
647 numberOfLineIndices = 0;
653 auto cellType = static_cast<CellGeometryEnum>(static_cast<int>(buffer[index++]));
654 auto nn = static_cast<unsigned int>(buffer[index++]);
657 if (pointIds.size() >= nn)
660 if (
id == static_cast<SizeValueType>(buffer[index]))
662 pointIds.push_back(static_cast<SizeValueType>(buffer[index + 1]));
664 else if (
id == static_cast<SizeValueType>(buffer[index + 1]))
666 pointIds.push_back(static_cast<SizeValueType>(buffer[index]));
670 polylines->InsertElement(numberOfPolylines++, pointIds);
671 numberOfLineIndices += pointIds.size();
675 for (
unsigned int jj = 0; jj < nn; jj++)
677 pointIds.push_back(static_cast<SizeValueType>(buffer[index + jj]));
683 for (
unsigned int jj = 0; jj < nn; jj++)
685 pointIds.push_back(static_cast<SizeValueType>(buffer[index + jj]));
692 polylines->InsertElement(numberOfPolylines++, pointIds);
693 numberOfLineIndices += pointIds.size();
696 numberOfLines = polylines->Size();
697 numberOfLineIndices += numberOfLines;
698 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLines", numberOfLines);
699 EncapsulateMetaData<unsigned int>(metaDic,
"numberOfLineIndices", numberOfLineIndices);
701 outputFile <<
"LINES " << numberOfLines <<
" " << numberOfLineIndices <<
'\n';
702 auto * data =
new unsigned int[numberOfLineIndices];
703 unsigned long outputIndex = 0;
706 auto nn = static_cast<unsigned int>(polylines->ElementAt(ii).size());
707 data[outputIndex++] = nn;
708 for (
unsigned int jj = 0; jj < nn; ++jj)
710 data[outputIndex++] = polylines->ElementAt(ii)[jj];
721 ExposeMetaData<unsigned int>(metaDic,
"numberOfPolygons", numberOfPolygons);
722 if (numberOfPolygons)
724 ExposeMetaData<unsigned int>(metaDic,
"numberOfPolygonIndices", numberOfPolygonIndices);
725 outputFile <<
"POLYGONS " << numberOfPolygons <<
" " << numberOfPolygonIndices <<
'\n';
726 auto * data =
new unsigned int[numberOfPolygonIndices];
727 ReadCellsBuffer(buffer, data);
735 template <
typename T>
743 outputFile <<
"POINT_DATA " << this->m_NumberOfPointPixels <<
'\n';
744 switch (this->m_PointPixelType)
748 outputFile <<
"SCALARS ";
749 ExposeMetaData<StringType>(metaDic,
"pointScalarDataName", dataName);
750 outputFile << dataName <<
" ";
758 outputFile <<
"VECTORS ";
759 ExposeMetaData<StringType>(metaDic,
"pointVectorDataName", dataName);
760 outputFile << dataName <<
" ";
766 outputFile <<
"TENSORS ";
767 ExposeMetaData<StringType>(metaDic,
"pointTensorDataName", dataName);
768 outputFile << dataName <<
" ";
774 outputFile <<
"COLOR_SCALARS ";
775 ExposeMetaData<StringType>(metaDic,
"pointColorScalarDataName", dataName);
776 outputFile << dataName <<
" ";
777 WriteColorScalarBufferAsASCII(
778 outputFile, buffer, this->m_NumberOfPointPixelComponents, this->m_NumberOfPointPixels);
783 itkExceptionMacro(<<
"Unknown point pixel type");
787 outputFile << pointPixelComponentName <<
'\n';
791 outputFile <<
"LOOKUP_TABLE default" <<
'\n';
799 const SizeValueType num = this->m_NumberOfPointPixelComponents * this->m_NumberOfPointPixels;
802 if (this->m_NumberOfPointPixelComponents == 3)
809 outputFile << convert(*ptr++) << indent;
811 outputFile << convert(e12) << indent;
812 outputFile << convert(zero) <<
'\n';
814 outputFile << convert(e12) << indent;
815 outputFile << convert(*ptr++) << indent;
816 outputFile << convert(zero) <<
'\n';
818 outputFile << convert(zero) << indent << convert(zero) << indent << convert(zero) <<
"\n\n";
822 else if (this->m_NumberOfPointPixelComponents == 6)
830 outputFile << convert(*ptr++) << indent;
832 outputFile << convert(e12) << indent;
834 outputFile << convert(e13) <<
'\n';
836 outputFile << convert(e12) << indent;
837 outputFile << convert(*ptr++) << indent;
839 outputFile << convert(e23) <<
'\n';
841 outputFile << convert(e13) << indent;
842 outputFile << convert(e23) << indent;
843 outputFile << convert(*ptr++) <<
"\n\n";
849 ::itk::ExceptionObject e_(
850 __FILE__, __LINE__,
"itk::ERROR: VTKImageIO2: Unsupported number of components in tensor.", ITK_LOCATION);
857 for (
SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++)
859 for (jj = 0; jj < this->m_NumberOfPointPixelComponents - 1; jj++)
861 outputFile << convert(buffer[ii * this->m_NumberOfPointPixelComponents + jj]) << indent;
863 outputFile << convert(buffer[ii * this->m_NumberOfPointPixelComponents + jj]);
871 template <
typename T>
878 outputFile <<
"POINT_DATA " << this->m_NumberOfPointPixels <<
"\n";
879 switch (this->m_PointPixelType)
883 outputFile <<
"SCALARS ";
884 ExposeMetaData<StringType>(metaDic,
"pointScalarDataName", dataName);
885 outputFile << dataName <<
" ";
893 outputFile <<
"VECTORS ";
894 ExposeMetaData<StringType>(metaDic,
"pointVectorDataName", dataName);
895 outputFile << dataName <<
" ";
901 outputFile <<
"TENSORS ";
902 ExposeMetaData<StringType>(metaDic,
"pointTensorDataName", dataName);
903 outputFile << dataName <<
" ";
909 outputFile <<
"COLOR_SCALARS ";
910 ExposeMetaData<StringType>(metaDic,
"pointColorScalarDataName", dataName);
911 outputFile << dataName <<
" ";
912 WriteColorScalarBufferAsBINARY(
913 outputFile, buffer, this->m_NumberOfPointPixelComponents, this->m_NumberOfPointPixels);
918 itkExceptionMacro(<<
"Unknown point pixel type");
922 outputFile << pointPixelComponentName <<
"\n";
925 outputFile <<
"LOOKUP_TABLE default\n";
929 buffer, this->m_NumberOfPointPixels * this->m_NumberOfPointPixelComponents, &outputFile);
934 template <
typename T>
941 outputFile <<
"CELL_DATA " << this->m_NumberOfCellPixels <<
'\n';
942 switch (this->m_CellPixelType)
946 outputFile <<
"SCALARS ";
947 ExposeMetaData<StringType>(metaDic,
"cellScalarDataName", dataName);
948 outputFile << dataName <<
" ";
956 outputFile <<
"VECTORS ";
957 ExposeMetaData<StringType>(metaDic,
"cellVectorDataName", dataName);
958 outputFile << dataName <<
" ";
964 outputFile <<
"TENSORS ";
965 ExposeMetaData<StringType>(metaDic,
"cellTensorDataName", dataName);
966 outputFile << dataName <<
" ";
972 outputFile <<
"COLOR_SCALARS ";
973 ExposeMetaData<StringType>(metaDic,
"cellColorScalarDataName", dataName);
974 outputFile << dataName <<
" ";
975 WriteColorScalarBufferAsASCII(
976 outputFile, buffer, this->m_NumberOfCellPixelComponents, this->m_NumberOfCellPixels);
981 itkExceptionMacro(<<
"Unknown cell pixel type");
985 outputFile << cellPixelComponentName <<
'\n';
988 outputFile <<
"LOOKUP_TABLE default" <<
'\n';
996 const SizeValueType num = this->m_NumberOfCellPixelComponents * this->m_NumberOfCellPixels;
997 if (this->m_NumberOfCellPixelComponents == 2)
1004 outputFile << *ptr++ << indent;
1006 outputFile << e12 << indent;
1007 outputFile << zero <<
'\n';
1009 outputFile << e12 << indent;
1010 outputFile << *ptr++ << indent;
1011 outputFile << zero <<
'\n';
1013 outputFile << zero << indent << zero << indent << zero <<
"\n\n";
1017 else if (this->m_NumberOfCellPixelComponents == 3)
1025 outputFile << *ptr++ << indent;
1027 outputFile << e12 << indent;
1029 outputFile << e13 <<
'\n';
1031 outputFile << e12 << indent;
1032 outputFile << *ptr++ << indent;
1034 outputFile << e23 <<
'\n';
1036 outputFile << e13 << indent;
1037 outputFile << e23 << indent;
1038 outputFile << *ptr++ <<
"\n\n";
1044 ExceptionObject e_(__FILE__,
1046 "itk::ERROR: VTKPolyDataMeshIO: Unsupported number of components in tensor.",
1054 for (
SizeValueType ii = 0; ii < this->m_NumberOfCellPixels; ii++)
1056 for (jj = 0; jj < this->m_NumberOfCellPixelComponents - 1; jj++)
1058 outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj] << indent;
1060 outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj];
1068 template <
typename T>
1075 outputFile <<
"CELL_DATA " << this->m_NumberOfCellPixels <<
"\n";
1076 switch (this->m_CellPixelType)
1080 outputFile <<
"SCALARS ";
1081 ExposeMetaData<StringType>(metaDic,
"cellScalarDataName", dataName);
1082 outputFile << dataName <<
" ";
1090 outputFile <<
"VECTORS ";
1091 ExposeMetaData<StringType>(metaDic,
"cellVectorDataName", dataName);
1092 outputFile << dataName <<
" ";
1098 outputFile <<
"TENSORS ";
1099 ExposeMetaData<StringType>(metaDic,
"cellTensorDataName", dataName);
1100 outputFile << dataName <<
" ";
1106 outputFile <<
"COLOR_SCALARS ";
1107 ExposeMetaData<StringType>(metaDic,
"cellColorScalarDataName", dataName);
1108 outputFile << dataName <<
" ";
1109 WriteColorScalarBufferAsBINARY(
1110 outputFile, buffer, this->m_NumberOfCellPixelComponents, this->m_NumberOfCellPixels);
1115 itkExceptionMacro(<<
"Unknown cell pixel type");
1119 outputFile << cellPixelComponentName <<
"\n";
1122 outputFile <<
"LOOKUP_TABLE default\n";
1126 buffer, this->m_NumberOfCells * this->m_NumberOfCellPixelComponents, &outputFile);
1131 template <
typename T>
1135 unsigned int numberOfPixelComponents,
1139 outputFile << numberOfPixelComponents <<
"\n";
1143 for (
unsigned int jj = 0; jj < numberOfPixelComponents; ++jj)
1145 outputFile << convert(static_cast<float>(buffer[ii * numberOfPixelComponents + jj])) << indent;
1154 template <
typename T>
1158 unsigned int numberOfPixelComponents,
1161 outputFile << numberOfPixelComponents <<
"\n";
1162 SizeValueType numberOfElements = numberOfPixelComponents * numberOfPixels;
1163 auto * data =
new unsigned char[numberOfElements];
1166 data[ii] = static_cast<unsigned char>(buffer[ii]);
1169 outputFile.write(reinterpret_cast<char *>(data), numberOfElements);
1178 template <
typename TInput,
typename TOutput>
1185 if (input && output)
1187 for (
SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++)
1190 auto nn = static_cast<unsigned int>(input[inputIndex++]);
1191 output[outputIndex++] = nn;
1192 for (
unsigned int jj = 0; jj < nn; jj++)
1194 output[outputIndex++] = static_cast<TOutput>(input[inputIndex++]);
1202 GetComponentTypeFromString(
const std::string & pointType);
1206 #endif // itkVTKPolyDataMeshIO_h