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));
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");
287 template <
typename T>
293 while (!inputFile.eof())
295 std::getline(inputFile, line,
'\n');
296 if (line.find(
"POINT_DATA") != std::string::npos)
298 if (!inputFile.eof())
300 std::getline(inputFile, line,
'\n');
304 itkExceptionMacro(
"UnExpected end of line while trying to read POINT_DATA");
308 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
310 if (!inputFile.eof())
312 std::getline(inputFile, line,
'\n');
313 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
315 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
320 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
326 inputFile.read(
reinterpret_cast<char *
>(buffer), numberOfComponents *
sizeof(T));
335 template <
typename T>
341 while (!inputFile.eof())
343 std::getline(inputFile, line,
'\n');
344 if (line.find(
"CELL_DATA") != std::string::npos)
346 if (!inputFile.eof())
348 std::getline(inputFile, line,
'\n');
352 itkExceptionMacro(
"UnExpected end of line while trying to read CELL_DATA");
356 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
358 if (!inputFile.eof())
360 std::getline(inputFile, line,
'\n');
361 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
363 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
368 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
379 template <
typename T>
385 while (!inputFile.eof())
387 std::getline(inputFile, line,
'\n');
388 if (line.find(
"POINT_DATA") != std::string::npos)
390 if (!inputFile.eof())
392 std::getline(inputFile, line,
'\n');
396 itkExceptionMacro(
"UnExpected end of line while trying to read POINT_DATA");
400 if (line.find(
"SCALARS") != std::string::npos && line.find(
"COLOR_SCALARS") == std::string::npos)
402 if (!inputFile.eof())
404 std::getline(inputFile, line,
'\n');
405 if (line.find(
"LOOKUP_TABLE") == std::string::npos)
407 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
412 itkExceptionMacro(
"UnExpected end of line while trying to read LOOKUP_TABLE");
418 inputFile.read(
reinterpret_cast<char *
>(buffer), numberOfComponents *
sizeof(T));
427 template <
typename T>
434 outputFile << pointComponentType <<
'\n';
435 for (
SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
442 outputFile <<
ConvertNumberToString(buffer[ii * this->m_PointDimension + this->m_PointDimension - 1]) <<
'\n';
448 template <
typename T>
453 outputFile <<
"POINTS " << this->
m_NumberOfPoints << pointComponentType <<
'\n';
461 template <
typename T>
466 unsigned int numberOfVertices = 0;
467 unsigned int numberOfVertexIndices = 0;
468 unsigned int numberOfLines = 0;
469 unsigned int numberOfLineIndices = 0;
470 unsigned int numberOfPolygons = 0;
471 unsigned int numberOfPolygonIndices = 0;
477 if (numberOfVertices)
480 outputFile <<
"VERTICES " << numberOfVertices <<
' ' << numberOfVertexIndices <<
'\n';
483 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
484 auto nn =
static_cast<unsigned int>(buffer[index++]);
485 if (cellType == CellGeometryEnum::VERTEX_CELL)
488 for (
unsigned int jj = 0; jj < nn; ++jj)
490 outputFile <<
' ' << buffer[index++];
506 numberOfLineIndices = 0;
512 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
513 auto nn =
static_cast<unsigned int>(buffer[index++]);
516 if (cellType == CellGeometryEnum::LINE_CELL)
518 pointIds.push_back(
static_cast<SizeValueType>(buffer[index]));
519 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + 1]));
521 else if (cellType == CellGeometryEnum::POLYLINE_CELL)
523 for (
unsigned int jj = 0; jj < nn; ++jj)
525 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + jj]));
529 polylines->InsertElement(numberOfPolylines++, pointIds);
530 numberOfLineIndices += pointIds.size();
534 numberOfLines = polylines->Size();
535 numberOfLineIndices += numberOfLines;
538 outputFile <<
"LINES " << numberOfLines <<
' ' << numberOfLineIndices <<
'\n';
541 auto nn =
static_cast<unsigned int>(polylines->ElementAt(ii).size());
543 for (
unsigned int jj = 0; jj < nn; ++jj)
545 outputFile <<
' ' << polylines->ElementAt(ii)[jj];
554 if (numberOfPolygons)
557 outputFile <<
"POLYGONS " << numberOfPolygons <<
' ' << numberOfPolygonIndices <<
'\n';
560 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
561 auto nn =
static_cast<unsigned int>(buffer[index++]);
562 if (cellType == CellGeometryEnum::POLYGON_CELL || cellType == CellGeometryEnum::TRIANGLE_CELL ||
563 cellType == CellGeometryEnum::QUADRILATERAL_CELL)
566 for (
unsigned int jj = 0; jj < nn; ++jj)
568 outputFile <<
' ' << buffer[index++];
580 template <
typename T>
585 unsigned int numberOfVertices = 0;
586 unsigned int numberOfVertexIndices = 0;
587 unsigned int numberOfLines = 0;
588 unsigned int numberOfLineIndices = 0;
589 unsigned int numberOfPolygons = 0;
590 unsigned int numberOfPolygonIndices = 0;
596 if (numberOfVertices)
599 outputFile <<
"VERTICES " << numberOfVertices <<
' ' << numberOfVertexIndices <<
'\n';
603 data.get(), numberOfVertexIndices, &outputFile);
612 numberOfLineIndices = 0;
618 auto cellType =
static_cast<CellGeometryEnum>(
static_cast<int>(buffer[index++]));
619 auto nn =
static_cast<unsigned int>(buffer[index++]);
622 if (cellType == CellGeometryEnum::LINE_CELL)
624 pointIds.push_back(
static_cast<SizeValueType>(buffer[index]));
625 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + 1]));
627 else if (cellType == CellGeometryEnum::POLYLINE_CELL)
629 for (
unsigned int jj = 0; jj < nn; ++jj)
631 pointIds.push_back(
static_cast<SizeValueType>(buffer[index + jj]));
634 polylines->InsertElement(numberOfPolylines++, pointIds);
635 numberOfLineIndices += pointIds.size();
639 numberOfLines = polylines->Size();
640 numberOfLineIndices += numberOfLines;
644 outputFile <<
"LINES " << numberOfLines <<
' ' << numberOfLineIndices <<
'\n';
646 unsigned long outputIndex = 0;
649 auto nn =
static_cast<unsigned int>(polylines->ElementAt(ii).size());
650 data[outputIndex++] = nn;
651 for (
unsigned int jj = 0; jj < nn; ++jj)
653 data[outputIndex++] = polylines->ElementAt(ii)[jj];
664 if (numberOfPolygons)
667 outputFile <<
"POLYGONS " << numberOfPolygons <<
' ' << numberOfPolygonIndices <<
'\n';
671 data.get(), numberOfPolygonIndices, &outputFile);
676 template <
typename T>
686 case IOPixelEnum::SCALAR:
688 outputFile <<
"SCALARS ";
690 outputFile << dataName <<
" ";
693 case IOPixelEnum::OFFSET:
694 case IOPixelEnum::POINT:
695 case IOPixelEnum::COVARIANTVECTOR:
696 case IOPixelEnum::VECTOR:
698 outputFile <<
"VECTORS ";
700 outputFile << dataName <<
" ";
703 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
704 case IOPixelEnum::DIFFUSIONTENSOR3D:
706 outputFile <<
"TENSORS ";
708 outputFile << dataName <<
" ";
711 case IOPixelEnum::ARRAY:
712 case IOPixelEnum::VARIABLELENGTHVECTOR:
714 outputFile <<
"COLOR_SCALARS ";
716 outputFile << dataName <<
" ";
723 itkExceptionMacro(
"Unknown point pixel type");
727 outputFile << pointPixelComponentName <<
'\n';
731 outputFile <<
"LOOKUP_TABLE default" <<
'\n';
787 __FILE__, __LINE__,
"itk::ERROR: VTKImageIO2: Unsupported number of components in tensor.", ITK_LOCATION);
793 for (
SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ++ii)
798 outputFile <<
ConvertNumberToString(buffer[ii * this->m_NumberOfPointPixelComponents + jj]) << indent;
807 template <
typename T>
817 case IOPixelEnum::SCALAR:
819 outputFile <<
"SCALARS ";
821 outputFile << dataName <<
" ";
824 case IOPixelEnum::OFFSET:
825 case IOPixelEnum::POINT:
826 case IOPixelEnum::COVARIANTVECTOR:
827 case IOPixelEnum::VECTOR:
829 outputFile <<
"VECTORS ";
831 outputFile << dataName <<
" ";
834 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
835 case IOPixelEnum::DIFFUSIONTENSOR3D:
837 outputFile <<
"TENSORS ";
839 outputFile << dataName <<
" ";
842 case IOPixelEnum::ARRAY:
843 case IOPixelEnum::VARIABLELENGTHVECTOR:
845 outputFile <<
"COLOR_SCALARS ";
847 outputFile << dataName <<
" ";
854 itkExceptionMacro(
"Unknown point pixel type");
858 outputFile << pointPixelComponentName <<
'\n';
861 outputFile <<
"LOOKUP_TABLE default\n";
870 template <
typename T>
880 case IOPixelEnum::SCALAR:
882 outputFile <<
"SCALARS ";
884 outputFile << dataName <<
" ";
887 case IOPixelEnum::OFFSET:
888 case IOPixelEnum::POINT:
889 case IOPixelEnum::COVARIANTVECTOR:
890 case IOPixelEnum::VECTOR:
892 outputFile <<
"VECTORS ";
894 outputFile << dataName <<
" ";
897 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
898 case IOPixelEnum::DIFFUSIONTENSOR3D:
900 outputFile <<
"TENSORS ";
902 outputFile << dataName <<
" ";
905 case IOPixelEnum::ARRAY:
906 case IOPixelEnum::VARIABLELENGTHVECTOR:
908 outputFile <<
"COLOR_SCALARS ";
910 outputFile << dataName <<
" ";
917 itkExceptionMacro(
"Unknown cell pixel type");
921 outputFile << cellPixelComponentName <<
'\n';
924 outputFile <<
"LOOKUP_TABLE default" <<
'\n';
940 outputFile << *ptr++ << indent;
942 outputFile << e12 << indent << zero <<
'\n';
944 outputFile << e12 << indent << *ptr++ << indent << zero <<
'\n';
946 outputFile << zero << indent << zero << indent << zero <<
"\n\n";
958 outputFile << *ptr++ << indent;
960 outputFile << e12 << indent;
962 outputFile << e13 <<
'\n';
964 outputFile << e12 << indent << *ptr++ << indent;
966 outputFile << e23 <<
'\n';
968 outputFile << e13 << indent << e23 << indent << *ptr++ <<
"\n\n";
976 "itk::ERROR: VTKPolyDataMeshIO: Unsupported number of components in tensor.",
983 for (
SizeValueType ii = 0; ii < this->m_NumberOfCellPixels; ++ii)
988 outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj] << indent;
990 outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj] <<
'\n';
997 template <
typename T>
1007 case IOPixelEnum::SCALAR:
1009 outputFile <<
"SCALARS ";
1011 outputFile << dataName <<
" ";
1014 case IOPixelEnum::OFFSET:
1015 case IOPixelEnum::POINT:
1016 case IOPixelEnum::COVARIANTVECTOR:
1017 case IOPixelEnum::VECTOR:
1019 outputFile <<
"VECTORS ";
1021 outputFile << dataName <<
" ";
1024 case IOPixelEnum::SYMMETRICSECONDRANKTENSOR:
1025 case IOPixelEnum::DIFFUSIONTENSOR3D:
1027 outputFile <<
"TENSORS ";
1029 outputFile << dataName <<
" ";
1032 case IOPixelEnum::ARRAY:
1033 case IOPixelEnum::VARIABLELENGTHVECTOR:
1035 outputFile <<
"COLOR_SCALARS ";
1037 outputFile << dataName <<
" ";
1044 itkExceptionMacro(
"Unknown cell pixel type");
1048 outputFile << cellPixelComponentName <<
'\n';
1051 outputFile <<
"LOOKUP_TABLE default\n";
1060 template <
typename T>
1064 unsigned int numberOfPixelComponents,
1067 outputFile << numberOfPixelComponents <<
'\n';
1071 for (
unsigned int jj = 0; jj < numberOfPixelComponents; ++jj)
1073 outputFile << ConvertNumberToString(static_cast<float>(buffer[ii * numberOfPixelComponents + jj])) << indent;
1082 template <
typename T>
1086 unsigned int numberOfPixelComponents,
1089 outputFile << numberOfPixelComponents <<
'\n';
1090 const SizeValueType numberOfElements = numberOfPixelComponents * numberOfPixels;
1094 data[ii] =
static_cast<unsigned char>(buffer[ii]);
1097 outputFile.write(
reinterpret_cast<char *
>(data.get()), numberOfElements);
1104 template <
typename TInput,
typename TOutput>
1111 if (input && output)
1116 auto nn =
static_cast<unsigned int>(input[inputIndex++]);
1117 output[outputIndex++] = nn;
1118 for (
unsigned int jj = 0; jj < nn; ++jj)
1120 output[outputIndex++] =
static_cast<TOutput
>(input[inputIndex++]);
1135 template <
typename T>
1141 if (!(inputFile >> buffer[i]))
1143 itkGenericExceptionMacro(
"Failed to read a component from the specified ASCII input file!");
1154 template <
typename TOffset>
1158 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)