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 itkExceptionStringMacro(
"Currently we dont support this cell type");
195 template <
typename T>
201 while (!inputFile.eof())
203 std::getline(inputFile, line,
'\n');
205 if (line.find(
"POINTS") != std::string::npos)
213 template <
typename T>
219 while (!inputFile.eof())
221 std::getline(inputFile, line,
'\n');
223 if (line.find(
"POINTS") != std::string::npos)
227 inputFile.read(
reinterpret_cast<char *
>(buffer), numberOfComponents *
sizeof(T));
242 template <
typename T>
248 while (!inputFile.eof())
250 std::getline(inputFile, line,
'\n');
251 if (line.find(
"POINT_DATA") != std::string::npos)
253 if (!inputFile.eof())
255 std::getline(inputFile, line,
'\n');
259 itkExceptionStringMacro(
"UnExpected end of line while trying to read POINT_DATA");
263 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
265 if (!inputFile.eof())
267 std::getline(inputFile, line,
'\n');
268 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
270 itkExceptionStringMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
275 itkExceptionStringMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
286 template <
typename T>
292 while (!inputFile.eof())
294 std::getline(inputFile, line,
'\n');
295 if (line.find(
"POINT_DATA") != std::string::npos)
297 if (!inputFile.eof())
299 std::getline(inputFile, line,
'\n');
303 itkExceptionStringMacro(
"UnExpected end of line while trying to read POINT_DATA");
307 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
309 if (!inputFile.eof())
311 std::getline(inputFile, line,
'\n');
312 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
314 itkExceptionStringMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
319 itkExceptionStringMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
325 inputFile.read(
reinterpret_cast<char *
>(buffer), numberOfComponents *
sizeof(T));
334 template <
typename T>
340 while (!inputFile.eof())
342 std::getline(inputFile, line,
'\n');
343 if (line.find(
"CELL_DATA") != std::string::npos)
345 if (!inputFile.eof())
347 std::getline(inputFile, line,
'\n');
351 itkExceptionStringMacro(
"UnExpected end of line while trying to read CELL_DATA");
355 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
357 if (!inputFile.eof())
359 std::getline(inputFile, line,
'\n');
360 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
362 itkExceptionStringMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
367 itkExceptionStringMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
378 template <
typename T>
384 while (!inputFile.eof())
386 std::getline(inputFile, line,
'\n');
387 if (line.find(
"POINT_DATA") != std::string::npos)
389 if (!inputFile.eof())
391 std::getline(inputFile, line,
'\n');
395 itkExceptionStringMacro(
"UnExpected end of line while trying to read POINT_DATA");
399 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
401 if (!inputFile.eof())
403 std::getline(inputFile, line,
'\n');
404 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
406 itkExceptionStringMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
411 itkExceptionStringMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
417 inputFile.read(
reinterpret_cast<char *
>(buffer), numberOfComponents *
sizeof(T));
426 template <
typename T>
433 outputFile << pointComponentType <<
'\n';
434 for (
SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
441 outputFile <<
ConvertNumberToString(buffer[ii * this->m_PointDimension + this->m_PointDimension - 1]) <<
'\n';
445 template <
typename T>
450 outputFile <<
"POINTS " << this->
m_NumberOfPoints << pointComponentType <<
'\n';
456 template <
typename T>
461 unsigned int numberOfVertices = 0;
462 unsigned int numberOfVertexIndices = 0;
463 unsigned int numberOfLines = 0;
464 unsigned int numberOfLineIndices = 0;
465 unsigned int numberOfPolygons = 0;
466 unsigned int numberOfPolygonIndices = 0;
472 if (numberOfVertices)
475 outputFile <<
"VERTICES " << numberOfVertices <<
' ' << numberOfVertexIndices <<
'\n';
478 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
479 auto nn =
static_cast<unsigned int>(buffer[index++]);
480 if (cellType == CellGeometryEnum::VERTEX_CELL)
483 for (
unsigned int jj = 0; jj < nn; ++jj)
485 outputFile <<
' ' << buffer[index++];
501 numberOfLineIndices = 0;
507 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
508 auto nn =
static_cast<unsigned int>(buffer[index++]);
511 if (cellType == CellGeometryEnum::LINE_CELL)
513 pointIds.push_back(
static_cast<SizeValueType>(buffer[index]));
514 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + 1]));
516 else if (cellType == CellGeometryEnum::POLYLINE_CELL)
518 for (
unsigned int jj = 0; jj < nn; ++jj)
520 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + jj]));
524 polylines->InsertElement(numberOfPolylines++, pointIds);
525 numberOfLineIndices += pointIds.size();
529 numberOfLines = polylines->Size();
530 numberOfLineIndices += numberOfLines;
533 outputFile <<
"LINES " << numberOfLines <<
' ' << numberOfLineIndices <<
'\n';
536 auto nn =
static_cast<unsigned int>(polylines->ElementAt(ii).size());
538 for (
unsigned int jj = 0; jj < nn; ++jj)
540 outputFile <<
' ' << polylines->ElementAt(ii)[jj];
549 if (numberOfPolygons)
552 outputFile <<
"POLYGONS " << numberOfPolygons <<
' ' << numberOfPolygonIndices <<
'\n';
555 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
556 auto nn =
static_cast<unsigned int>(buffer[index++]);
557 if (cellType == CellGeometryEnum::POLYGON_CELL || cellType == CellGeometryEnum::TRIANGLE_CELL ||
558 cellType == CellGeometryEnum::QUADRILATERAL_CELL)
561 for (
unsigned int jj = 0; jj < nn; ++jj)
563 outputFile <<
' ' << buffer[index++];
575 template <
typename T>
580 unsigned int numberOfVertices = 0;
581 unsigned int numberOfVertexIndices = 0;
582 unsigned int numberOfLines = 0;
583 unsigned int numberOfLineIndices = 0;
584 unsigned int numberOfPolygons = 0;
585 unsigned int numberOfPolygonIndices = 0;
591 if (numberOfVertices)
594 outputFile <<
"VERTICES " << numberOfVertices <<
' ' << numberOfVertexIndices <<
'\n';
598 data.get(), numberOfVertexIndices, &outputFile);
607 numberOfLineIndices = 0;
613 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
614 auto nn =
static_cast<unsigned int>(buffer[index++]);
617 if (cellType == CellGeometryEnum::LINE_CELL)
619 pointIds.push_back(
static_cast<SizeValueType>(buffer[index]));
620 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + 1]));
622 else if (cellType == CellGeometryEnum::POLYLINE_CELL)
624 for (
unsigned int jj = 0; jj < nn; ++jj)
626 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + jj]));
629 polylines->InsertElement(numberOfPolylines++, pointIds);
630 numberOfLineIndices += pointIds.size();
634 numberOfLines = polylines->Size();
635 numberOfLineIndices += numberOfLines;
639 outputFile <<
"LINES " << numberOfLines <<
' ' << numberOfLineIndices <<
'\n';
641 unsigned long outputIndex = 0;
644 auto nn =
static_cast<unsigned int>(polylines->ElementAt(ii).size());
645 data[outputIndex++] = nn;
646 for (
unsigned int jj = 0; jj < nn; ++jj)
648 data[outputIndex++] = polylines->ElementAt(ii)[jj];
659 if (numberOfPolygons)
662 outputFile <<
"POLYGONS " << numberOfPolygons <<
' ' << numberOfPolygonIndices <<
'\n';
666 data.get(), numberOfPolygonIndices, &outputFile);
671 template <
typename T>
681 case IOPixelEnum::SCALAR:
683 outputFile <<
"SCALARS ";
685 outputFile << dataName <<
" ";
688 case IOPixelEnum::OFFSET:
689 case IOPixelEnum::POINT:
690 case IOPixelEnum::COVARIANTVECTOR:
691 case IOPixelEnum::VECTOR:
693 outputFile <<
"VECTORS ";
695 outputFile << dataName <<
" ";
698 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
699 case IOPixelEnum::DIFFUSIONTENSOR3D:
701 outputFile <<
"TENSORS ";
703 outputFile << dataName <<
" ";
706 case IOPixelEnum::ARRAY:
707 case IOPixelEnum::VARIABLELENGTHVECTOR:
709 outputFile <<
"COLOR_SCALARS ";
711 outputFile << dataName <<
" ";
718 itkExceptionStringMacro(
"Unknown point pixel type");
722 outputFile << pointPixelComponentName <<
'\n';
726 outputFile <<
"LOOKUP_TABLE default" <<
'\n';
782 __FILE__, __LINE__,
"itk::ERROR: VTKImageIO2: Unsupported number of components in tensor.", ITK_LOCATION);
788 for (
SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ++ii)
793 outputFile <<
ConvertNumberToString(buffer[ii * this->m_NumberOfPointPixelComponents + jj]) << indent;
800 template <
typename T>
810 case IOPixelEnum::SCALAR:
812 outputFile <<
"SCALARS ";
814 outputFile << dataName <<
" ";
817 case IOPixelEnum::OFFSET:
818 case IOPixelEnum::POINT:
819 case IOPixelEnum::COVARIANTVECTOR:
820 case IOPixelEnum::VECTOR:
822 outputFile <<
"VECTORS ";
824 outputFile << dataName <<
" ";
827 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
828 case IOPixelEnum::DIFFUSIONTENSOR3D:
830 outputFile <<
"TENSORS ";
832 outputFile << dataName <<
" ";
835 case IOPixelEnum::ARRAY:
836 case IOPixelEnum::VARIABLELENGTHVECTOR:
838 outputFile <<
"COLOR_SCALARS ";
840 outputFile << dataName <<
" ";
847 itkExceptionStringMacro(
"Unknown point pixel type");
851 outputFile << pointPixelComponentName <<
'\n';
854 outputFile <<
"LOOKUP_TABLE default\n";
862 template <
typename T>
872 case IOPixelEnum::SCALAR:
874 outputFile <<
"SCALARS ";
876 outputFile << dataName <<
" ";
879 case IOPixelEnum::OFFSET:
880 case IOPixelEnum::POINT:
881 case IOPixelEnum::COVARIANTVECTOR:
882 case IOPixelEnum::VECTOR:
884 outputFile <<
"VECTORS ";
886 outputFile << dataName <<
" ";
889 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
890 case IOPixelEnum::DIFFUSIONTENSOR3D:
892 outputFile <<
"TENSORS ";
894 outputFile << dataName <<
" ";
897 case IOPixelEnum::ARRAY:
898 case IOPixelEnum::VARIABLELENGTHVECTOR:
900 outputFile <<
"COLOR_SCALARS ";
902 outputFile << dataName <<
" ";
909 itkExceptionStringMacro(
"Unknown cell pixel type");
913 outputFile << cellPixelComponentName <<
'\n';
916 outputFile <<
"LOOKUP_TABLE default" <<
'\n';
932 outputFile << *ptr++ << indent;
934 outputFile << e12 << indent << zero <<
'\n';
936 outputFile << e12 << indent << *ptr++ << indent << zero <<
'\n';
938 outputFile << zero << indent << zero << indent << zero <<
"\n\n";
950 outputFile << *ptr++ << indent;
952 outputFile << e12 << indent;
954 outputFile << e13 <<
'\n';
956 outputFile << e12 << indent << *ptr++ << indent;
958 outputFile << e23 <<
'\n';
960 outputFile << e13 << indent << e23 << indent << *ptr++ <<
"\n\n";
968 "itk::ERROR: VTKPolyDataMeshIO: Unsupported number of components in tensor.",
975 for (
SizeValueType ii = 0; ii < this->m_NumberOfCellPixels; ++ii)
980 outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj] << indent;
982 outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj] <<
'\n';
987 template <
typename T>
997 case IOPixelEnum::SCALAR:
999 outputFile <<
"SCALARS ";
1001 outputFile << dataName <<
" ";
1004 case IOPixelEnum::OFFSET:
1005 case IOPixelEnum::POINT:
1006 case IOPixelEnum::COVARIANTVECTOR:
1007 case IOPixelEnum::VECTOR:
1009 outputFile <<
"VECTORS ";
1011 outputFile << dataName <<
" ";
1014 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
1015 case IOPixelEnum::DIFFUSIONTENSOR3D:
1017 outputFile <<
"TENSORS ";
1019 outputFile << dataName <<
" ";
1022 case IOPixelEnum::ARRAY:
1023 case IOPixelEnum::VARIABLELENGTHVECTOR:
1025 outputFile <<
"COLOR_SCALARS ";
1027 outputFile << dataName <<
" ";
1034 itkExceptionStringMacro(
"Unknown cell pixel type");
1038 outputFile << cellPixelComponentName <<
'\n';
1041 outputFile <<
"LOOKUP_TABLE default\n";
1049 template <
typename T>
1053 unsigned int numberOfPixelComponents,
1056 outputFile << numberOfPixelComponents <<
'\n';
1060 for (
unsigned int jj = 0; jj < numberOfPixelComponents; ++jj)
1062 outputFile << ConvertNumberToString(static_cast<float>(buffer[ii * numberOfPixelComponents + jj])) << indent;
1069 template <
typename T>
1073 unsigned int numberOfPixelComponents,
1076 outputFile << numberOfPixelComponents <<
'\n';
1077 const SizeValueType numberOfElements = numberOfPixelComponents * numberOfPixels;
1081 data[ii] =
static_cast<unsigned char>(buffer[ii]);
1084 outputFile.write(
reinterpret_cast<char *
>(data.get()), numberOfElements);
1090 template <
typename TInput,
typename TOutput>
1097 if (input && output)
1102 auto nn =
static_cast<unsigned int>(input[inputIndex++]);
1103 output[outputIndex++] = nn;
1104 for (
unsigned int jj = 0; jj < nn; ++jj)
1106 output[outputIndex++] =
static_cast<TOutput
>(input[inputIndex++]);
1121 template <
typename T>
1127 if (!(inputFile >> buffer[i]))
1129 itkGenericExceptionMacro(
"Failed to read a component from the specified ASCII input file!");
1140 template <
typename TOffset>
1144 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)