18#ifndef itkVTKPolyDataMeshIO_h
19#define itkVTKPolyDataMeshIO_h
20#include "ITKIOMeshVTKExport.h"
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++]);
155 case CellGeometryEnum::VERTEX_CELL:
157 numberOfVertexIndices += nn + 1;
159 case CellGeometryEnum::LINE_CELL:
161 numberOfLineIndices += nn + 1;
163 case CellGeometryEnum::POLYLINE_CELL:
165 numberOfLineIndices += nn + 1;
167 case CellGeometryEnum::TRIANGLE_CELL:
169 numberOfPolygonIndices += nn + 1;
171 case CellGeometryEnum::POLYGON_CELL:
173 numberOfPolygonIndices += nn + 1;
175 case CellGeometryEnum::QUADRILATERAL_CELL:
177 numberOfPolygonIndices += nn + 1;
180 itkExceptionMacro(
"Currently we dont support this cell type");
196 template <
typename T>
202 while (!inputFile.eof())
204 std::getline(inputFile, line,
'\n');
206 if (line.find(
"POINTS") != std::string::npos)
214 template <
typename T>
220 while (!inputFile.eof())
222 std::getline(inputFile, line,
'\n');
224 if (line.find(
"POINTS") != std::string::npos)
228 inputFile.read(
reinterpret_cast<char *
>(buffer), numberOfComponents *
sizeof(T));
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");
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");
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");
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");
423 inputFile.read(
reinterpret_cast<char *
>(buffer), numberOfComponents *
sizeof(T));
433 template <
typename T>
440 outputFile << pointComponentType <<
'\n';
441 for (
SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
448 outputFile <<
ConvertNumberToString(buffer[ii * this->m_PointDimension + this->m_PointDimension - 1]) <<
'\n';
454 template <
typename T>
459 outputFile <<
"POINTS " << this->
m_NumberOfPoints << pointComponentType <<
'\n';
467 template <
typename T>
472 unsigned int numberOfVertices = 0;
473 unsigned int numberOfVertexIndices = 0;
474 unsigned int numberOfLines = 0;
475 unsigned int numberOfLineIndices = 0;
476 unsigned int numberOfPolygons = 0;
477 unsigned int numberOfPolygonIndices = 0;
483 if (numberOfVertices)
486 outputFile <<
"VERTICES " << numberOfVertices <<
' ' << numberOfVertexIndices <<
'\n';
489 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
490 auto nn =
static_cast<unsigned int>(buffer[index++]);
491 if (cellType == CellGeometryEnum::VERTEX_CELL)
494 for (
unsigned int jj = 0; jj < nn; ++jj)
496 outputFile <<
' ' << buffer[index++];
512 numberOfLineIndices = 0;
518 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
519 auto nn =
static_cast<unsigned int>(buffer[index++]);
523 if (cellType == CellGeometryEnum::LINE_CELL)
525 pointIds.push_back(
static_cast<SizeValueType>(buffer[index]));
526 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + 1]));
528 else if (cellType == CellGeometryEnum::POLYLINE_CELL)
530 for (
unsigned int jj = 0; jj < nn; ++jj)
532 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + jj]));
536 polylines->InsertElement(numberOfPolylines++, pointIds);
537 numberOfLineIndices += pointIds.size();
541 numberOfLines = polylines->Size();
542 numberOfLineIndices += numberOfLines;
545 outputFile <<
"LINES " << numberOfLines <<
' ' << numberOfLineIndices <<
'\n';
548 auto nn =
static_cast<unsigned int>(polylines->ElementAt(ii).size());
550 for (
unsigned int jj = 0; jj < nn; ++jj)
552 outputFile <<
' ' << polylines->ElementAt(ii)[jj];
561 if (numberOfPolygons)
564 outputFile <<
"POLYGONS " << numberOfPolygons <<
' ' << numberOfPolygonIndices <<
'\n';
567 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
568 auto nn =
static_cast<unsigned int>(buffer[index++]);
569 if (cellType == CellGeometryEnum::POLYGON_CELL || cellType == CellGeometryEnum::TRIANGLE_CELL ||
570 cellType == CellGeometryEnum::QUADRILATERAL_CELL)
573 for (
unsigned int jj = 0; jj < nn; ++jj)
575 outputFile <<
' ' << buffer[index++];
588 template <
typename T>
593 unsigned int numberOfVertices = 0;
594 unsigned int numberOfVertexIndices = 0;
595 unsigned int numberOfLines = 0;
596 unsigned int numberOfLineIndices = 0;
597 unsigned int numberOfPolygons = 0;
598 unsigned int numberOfPolygonIndices = 0;
604 if (numberOfVertices)
607 outputFile <<
"VERTICES " << numberOfVertices <<
' ' << numberOfVertexIndices <<
'\n';
611 data.get(), numberOfVertexIndices, &outputFile);
620 numberOfLineIndices = 0;
626 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
627 auto nn =
static_cast<unsigned int>(buffer[index++]);
631 if (cellType == CellGeometryEnum::LINE_CELL)
633 pointIds.push_back(
static_cast<SizeValueType>(buffer[index]));
634 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + 1]));
636 else if (cellType == CellGeometryEnum::POLYLINE_CELL)
638 for (
unsigned int jj = 0; jj < nn; ++jj)
640 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + jj]));
643 polylines->InsertElement(numberOfPolylines++, pointIds);
644 numberOfLineIndices += pointIds.size();
648 numberOfLines = polylines->Size();
649 numberOfLineIndices += numberOfLines;
653 outputFile <<
"LINES " << numberOfLines <<
' ' << numberOfLineIndices <<
'\n';
655 unsigned long outputIndex = 0;
658 auto nn =
static_cast<unsigned int>(polylines->ElementAt(ii).size());
659 data[outputIndex++] = nn;
660 for (
unsigned int jj = 0; jj < nn; ++jj)
662 data[outputIndex++] = polylines->ElementAt(ii)[jj];
673 if (numberOfPolygons)
676 outputFile <<
"POLYGONS " << numberOfPolygons <<
' ' << numberOfPolygonIndices <<
'\n';
680 data.get(), numberOfPolygonIndices, &outputFile);
686 template <
typename T>
696 case IOPixelEnum::SCALAR:
698 outputFile <<
"SCALARS ";
700 outputFile << dataName <<
" ";
703 case IOPixelEnum::OFFSET:
704 case IOPixelEnum::POINT:
705 case IOPixelEnum::COVARIANTVECTOR:
706 case IOPixelEnum::VECTOR:
708 outputFile <<
"VECTORS ";
710 outputFile << dataName <<
" ";
713 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
714 case IOPixelEnum::DIFFUSIONTENSOR3D:
716 outputFile <<
"TENSORS ";
718 outputFile << dataName <<
" ";
721 case IOPixelEnum::ARRAY:
722 case IOPixelEnum::VARIABLELENGTHVECTOR:
724 outputFile <<
"COLOR_SCALARS ";
726 outputFile << dataName <<
" ";
733 itkExceptionMacro(
"Unknown point pixel type");
737 outputFile << pointPixelComponentName <<
'\n';
741 outputFile <<
"LOOKUP_TABLE default" <<
'\n';
797 __FILE__, __LINE__,
"itk::ERROR: VTKImageIO2: Unsupported number of components in tensor.", ITK_LOCATION);
803 for (
SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ++ii)
808 outputFile <<
ConvertNumberToString(buffer[ii * this->m_NumberOfPointPixelComponents + jj]) << indent;
817 template <
typename T>
827 case IOPixelEnum::SCALAR:
829 outputFile <<
"SCALARS ";
831 outputFile << dataName <<
" ";
834 case IOPixelEnum::OFFSET:
835 case IOPixelEnum::POINT:
836 case IOPixelEnum::COVARIANTVECTOR:
837 case IOPixelEnum::VECTOR:
839 outputFile <<
"VECTORS ";
841 outputFile << dataName <<
" ";
844 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
845 case IOPixelEnum::DIFFUSIONTENSOR3D:
847 outputFile <<
"TENSORS ";
849 outputFile << dataName <<
" ";
852 case IOPixelEnum::ARRAY:
853 case IOPixelEnum::VARIABLELENGTHVECTOR:
855 outputFile <<
"COLOR_SCALARS ";
857 outputFile << dataName <<
" ";
864 itkExceptionMacro(
"Unknown point pixel type");
868 outputFile << pointPixelComponentName <<
'\n';
871 outputFile <<
"LOOKUP_TABLE default\n";
880 template <
typename T>
890 case IOPixelEnum::SCALAR:
892 outputFile <<
"SCALARS ";
894 outputFile << dataName <<
" ";
897 case IOPixelEnum::OFFSET:
898 case IOPixelEnum::POINT:
899 case IOPixelEnum::COVARIANTVECTOR:
900 case IOPixelEnum::VECTOR:
902 outputFile <<
"VECTORS ";
904 outputFile << dataName <<
" ";
907 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
908 case IOPixelEnum::DIFFUSIONTENSOR3D:
910 outputFile <<
"TENSORS ";
912 outputFile << dataName <<
" ";
915 case IOPixelEnum::ARRAY:
916 case IOPixelEnum::VARIABLELENGTHVECTOR:
918 outputFile <<
"COLOR_SCALARS ";
920 outputFile << dataName <<
" ";
927 itkExceptionMacro(
"Unknown cell pixel type");
931 outputFile << cellPixelComponentName <<
'\n';
934 outputFile <<
"LOOKUP_TABLE default" <<
'\n';
950 outputFile << *ptr++ << indent;
952 outputFile << e12 << indent << zero <<
'\n';
954 outputFile << e12 << indent << *ptr++ << indent << zero <<
'\n';
956 outputFile << zero << indent << zero << indent << zero <<
"\n\n";
968 outputFile << *ptr++ << indent;
970 outputFile << e12 << indent;
972 outputFile << e13 <<
'\n';
974 outputFile << e12 << indent << *ptr++ << indent;
976 outputFile << e23 <<
'\n';
978 outputFile << e13 << indent << e23 << indent << *ptr++ <<
"\n\n";
986 "itk::ERROR: VTKPolyDataMeshIO: Unsupported number of components in tensor.",
993 for (
SizeValueType ii = 0; ii < this->m_NumberOfCellPixels; ++ii)
998 outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj] << indent;
1000 outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj] <<
'\n';
1007 template <
typename T>
1017 case IOPixelEnum::SCALAR:
1019 outputFile <<
"SCALARS ";
1021 outputFile << dataName <<
" ";
1024 case IOPixelEnum::OFFSET:
1025 case IOPixelEnum::POINT:
1026 case IOPixelEnum::COVARIANTVECTOR:
1027 case IOPixelEnum::VECTOR:
1029 outputFile <<
"VECTORS ";
1031 outputFile << dataName <<
" ";
1034 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
1035 case IOPixelEnum::DIFFUSIONTENSOR3D:
1037 outputFile <<
"TENSORS ";
1039 outputFile << dataName <<
" ";
1042 case IOPixelEnum::ARRAY:
1043 case IOPixelEnum::VARIABLELENGTHVECTOR:
1045 outputFile <<
"COLOR_SCALARS ";
1047 outputFile << dataName <<
" ";
1054 itkExceptionMacro(
"Unknown cell pixel type");
1058 outputFile << cellPixelComponentName <<
'\n';
1061 outputFile <<
"LOOKUP_TABLE default\n";
1070 template <
typename T>
1074 unsigned int numberOfPixelComponents,
1077 outputFile << numberOfPixelComponents <<
'\n';
1081 for (
unsigned int jj = 0; jj < numberOfPixelComponents; ++jj)
1083 outputFile << ConvertNumberToString(static_cast<float>(buffer[ii * numberOfPixelComponents + jj])) << indent;
1092 template <
typename T>
1096 unsigned int numberOfPixelComponents,
1099 outputFile << numberOfPixelComponents <<
'\n';
1100 const SizeValueType numberOfElements = numberOfPixelComponents * numberOfPixels;
1104 data[ii] =
static_cast<unsigned char>(buffer[ii]);
1107 outputFile.write(
reinterpret_cast<char *
>(data.get()), numberOfElements);
1114 template <
typename TInput,
typename TOutput>
1121 if (input && output)
1126 auto nn =
static_cast<unsigned int>(input[inputIndex++]);
1127 output[outputIndex++] = nn;
1128 for (
unsigned int jj = 0; jj < nn; ++jj)
1130 output[outputIndex++] =
static_cast<TOutput
>(input[inputIndex++]);
1145 template <
typename T>
1151 if (!(inputFile >> buffer[i]))
1153 itkGenericExceptionMacro(
"Failed to read a component from the specified ASCII input file!");
1165 template <
typename TOffset>
1169 template <
typename TOffset,
typename TConnectivity>
static constexpr bool SystemIsLittleEndian()
static void SwapWriteRangeFromSystemToBigEndian(const T *p, int num, std::ostream *fp)
static void SwapRangeFromSystemToBigEndian(T *p, BufferSizeType num)
Standard exception handling object.
Control indentation during Print() invocation.
IOPixelEnum m_CellPixelType
unsigned int m_NumberOfPointPixelComponents
SizeValueType m_NumberOfPoints
IdentifierType SizeValueType
unsigned int m_PointDimension
IOPixelEnum m_PointPixelType
SizeValueType m_NumberOfPointPixels
SizeValueType m_NumberOfCellPixels
unsigned int m_NumberOfCellPixelComponents
SizeValueType m_NumberOfCells
MetaDataDictionary & GetMetaDataDictionary()
Implements transparent reference counting.
void ReadCellsBufferAsBINARYConnectivityType(std::ifstream &inputFile, void *buffer)
static void ReadComponentsAsASCII(std::ifstream &inputFile, float *const buffer, const SizeValueType numberOfComponents)
void ReadPointDataBufferAsASCII(std::ifstream &inputFile, T *buffer)
static void ReadComponentsAsASCII(std::ifstream &inputFile, double *const buffer, const SizeValueType numberOfComponents)
void WriteCellDataBufferAsASCII(std::ofstream &outputFile, T *buffer, const StringType &cellPixelComponentName)
void WritePointDataBufferAsASCII(std::ofstream &outputFile, T *buffer, const StringType &pointPixelComponentName)
IOComponentEnum GetComponentTypeFromString(const std::string &pointType)
void WriteCells(void *buffer) override
void WritePointData(void *buffer) override
void ReadCellData(void *buffer) override
PolylinesContainerType::Pointer PolylinesContainerPointer
std::vector< SizeValueType > PointIdVector
SmartPointer< Self > Pointer
bool CanWriteFile(const char *fileName) override
void ReadPointData(void *buffer) override
void WriteMeshInformation() override
SmartPointer< const Self > ConstPointer
void WriteCellData(void *buffer) override
void ReadCellsBufferAsASCII(std::ifstream &inputFile, void *buffer)
void WritePointDataBufferAsBINARY(std::ofstream &outputFile, T *buffer, const StringType &pointPixelComponentName)
void ReadPointDataBufferAsBINARY(std::ifstream &inputFile, T *buffer)
void WriteCellsBufferAsBINARY(std::ofstream &outputFile, T *buffer)
void WriteCellDataBufferAsBINARY(std::ofstream &outputFile, T *buffer, const StringType &cellPixelComponentName)
void PrintSelf(std::ostream &os, Indent indent) const override
void WritePointsBufferAsBINARY(std::ofstream &outputFile, T *buffer, const StringType &pointComponentType)
void WriteColorScalarBufferAsBINARY(std::ofstream &outputFile, T *buffer, unsigned int numberOfPixelComponents, SizeValueType numberOfPixels)
void ReadCellsBufferAsBINARYOffsetType(std::ifstream &inputFile, void *buffer)
void ReadCellsBuffer(TInput *input, TOutput *output)
void ReadPointsBufferAsBINARY(std::ifstream &inputFile, T *buffer)
VectorContainer< PointIdVector > PolylinesContainerType
void ReadCellsBufferAsBINARY(std::ifstream &inputFile, void *buffer)
uint8_t m_ReadMeshVersionMajor
std::vector< StringType > StringVectorType
~VTKPolyDataMeshIO() override
void WritePoints(void *buffer) override
void WritePointsBufferAsASCII(std::ofstream &outputFile, T *buffer, const StringType &pointComponentType)
Superclass::SizeValueType SizeValueType
void ReadCellDataBufferAsBINARY(std::ifstream &inputFile, T *buffer)
void WriteColorScalarBufferAsASCII(std::ofstream &outputFile, T *buffer, unsigned int numberOfPixelComponents, SizeValueType numberOfPixels)
void UpdateCellInformation(T *buffer)
void ReadPointsBufferAsASCII(std::ifstream &inputFile, T *buffer)
void WriteCellsBufferAsASCII(std::ofstream &outputFile, T *buffer)
void ReadCells(void *buffer) override
bool CanReadFile(const char *fileName) override
void ReadMeshInformation() override
static void ReadComponentsAsASCII(std::ifstream &inputFile, T *const buffer, const SizeValueType numberOfComponents)
std::stringstream StringStreamType
int GetNextLine(std::ifstream &ifs, std::string &line, bool lowerCase=true, SizeValueType count=0)
void ReadPoints(void *buffer) override
void ReadCellDataBufferAsASCII(std::ifstream &inputFile, T *buffer)
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
void EncapsulateMetaData(MetaDataDictionary &Dictionary, const std::string &key, const T &invalue)
CommonEnums::IOComponent IOComponentEnum
detail::VectorContainer< std::conditional_t< std::is_void_v< T2 >, SizeValueType, T1 >, std::conditional_t< std::is_void_v< T2 >, T1, T2 > > VectorContainer
std::string ConvertNumberToString(const TValue val)
bool ExposeMetaData(const MetaDataDictionary &Dictionary, const std::string key, T &outval)
CommonEnums::CellGeometry CellGeometryEnum
auto make_unique_for_overwrite(const vcl_size_t numberOfElements)