/*========================================================================= * * Copyright NumFOCUS * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * *=========================================================================*/ #ifndef itkMeshIOBase_h #define itkMeshIOBase_h #include "ITKIOMeshBaseExport.h" #include "itkByteSwapper.h" #include "itkCellInterface.h" #include "itkCovariantVector.h" #include "itkDiffusionTensor3D.h" #include "itkIntTypes.h" #include "itkLightProcessObject.h" #include "itkMatrix.h" #include "itkRGBPixel.h" #include "itkRGBAPixel.h" #include "itkSymmetricSecondRankTensor.h" #include "itkVariableLengthVector.h" #include "itkVariableSizeMatrix.h" #include "itkVector.h" #include "itkNumberToString.h" #include "itkCommonEnums.h" #include "itkMakeUniqueForOverwrite.h" #include #include #include namespace itk { /** * \class MeshIOBase * \brief Abstract superclass defines mesh IO interface. * * MeshIOBase is a class that reads and/or writes Mesh / QuadEdgeMesh data * of a particular format (such as PNG or raw binary). The * MeshIOBase encapsulates both the reading and writing of data. The * MeshIOBase is used by the MeshFileReader class (to read data) * and the MeshFileWriter (to write data) into a single file. * Normally the user does not directly * manipulate this class other than to instantiate it, set the FileName, * and assign it to a MeshFileReader or MeshFileWriter. * * A Pluggable factory pattern is used this allows different kinds of readers * to be registered (even at run time) without having to modify the * code in this class. * * \author Wanlin Zhu. University of New South Wales, Australia. * * \sa MeshFileWriter * \sa MeshFileReader * * \ingroup IOFilters * \ingroup ITKIOMeshBase * */ class ITKIOMeshBase_EXPORT MeshIOBase : public LightProcessObject { public: ITK_DISALLOW_COPY_AND_MOVE(MeshIOBase); /** Standard class type aliases. */ using Self = MeshIOBase; using Superclass = LightProcessObject; using ConstPointer = SmartPointer; using Pointer = SmartPointer; /** Type for the list of strings to be used for extensions. */ using ArrayOfExtensionsType = std::vector; /** Type for representing size of bytes, and or positions along a file */ using StreamOffsetType = std::streamoff; using SizeValueType = IdentifierType; /** * \class UnknownType * Used to return information when types are unknown. * \ingroup ITKIOMeshBase */ class UnknownType {}; /** \see LightObject::GetNameOfClass() */ itkOverrideGetNameOfClassMacro(MeshIOBase); /** Set/Get the name of the file to be read. */ itkSetStringMacro(FileName); itkGetStringMacro(FileName); #if !defined(ITK_LEGACY_REMOVE) /**Exposes enums values for backwards compatibility*/ static constexpr IOPixelEnum UNKNOWNPIXELTYPE = IOPixelEnum::UNKNOWNPIXELTYPE; static constexpr IOPixelEnum SCALAR = IOPixelEnum::SCALAR; static constexpr IOPixelEnum RGB = IOPixelEnum::RGB; static constexpr IOPixelEnum RGBA = IOPixelEnum::RGBA; static constexpr IOPixelEnum OFFSET = IOPixelEnum::OFFSET; static constexpr IOPixelEnum VECTOR = IOPixelEnum::VECTOR; static constexpr IOPixelEnum POINT = IOPixelEnum::POINT; static constexpr IOPixelEnum COVARIANTVECTOR = IOPixelEnum::COVARIANTVECTOR; static constexpr IOPixelEnum SYMMETRICSECONDRANKTENSOR = IOPixelEnum::SYMMETRICSECONDRANKTENSOR; static constexpr IOPixelEnum DIFFUSIONTENSOR3D = IOPixelEnum::DIFFUSIONTENSOR3D; static constexpr IOPixelEnum COMPLEX = IOPixelEnum::COMPLEX; static constexpr IOPixelEnum FIXEDARRAY = IOPixelEnum::FIXEDARRAY; static constexpr IOPixelEnum ARRAY = IOPixelEnum::ARRAY; static constexpr IOPixelEnum MATRIX = IOPixelEnum::MATRIX; static constexpr IOPixelEnum VARIABLELENGTHVECTOR = IOPixelEnum::VARIABLELENGTHVECTOR; static constexpr IOPixelEnum VARIABLESIZEMATRIX = IOPixelEnum::VARIABLESIZEMATRIX; #endif #if !defined(ITK_LEGACY_REMOVE) /**Exposes enums values for backwards compatibility*/ static constexpr IOComponentEnum UNKNOWNCOMPONENTTYPE = IOComponentEnum::UNKNOWNCOMPONENTTYPE; static constexpr IOComponentEnum UCHAR = IOComponentEnum::UCHAR; static constexpr IOComponentEnum CHAR = IOComponentEnum::CHAR; static constexpr IOComponentEnum USHORT = IOComponentEnum::USHORT; static constexpr IOComponentEnum SHORT = IOComponentEnum::SHORT; static constexpr IOComponentEnum UINT = IOComponentEnum::UINT; static constexpr IOComponentEnum INT = IOComponentEnum::INT; static constexpr IOComponentEnum ULONG = IOComponentEnum::ULONG; static constexpr IOComponentEnum LONG = IOComponentEnum::LONG; static constexpr IOComponentEnum ULONGLONG = IOComponentEnum::ULONGLONG; static constexpr IOComponentEnum LONGLONG = IOComponentEnum::LONGLONG; static constexpr IOComponentEnum FLOAT = IOComponentEnum::FLOAT; static constexpr IOComponentEnum DOUBLE = IOComponentEnum::DOUBLE; static constexpr IOComponentEnum LDOUBLE = IOComponentEnum::LDOUBLE; #endif #if !defined(ITK_LEGACY_REMOVE) /**Exposes enums values for backwards compatibility*/ static constexpr IOFileEnum ASCII = IOFileEnum::ASCII; static constexpr IOFileEnum BINARY = IOFileEnum::BINARY; static constexpr IOFileEnum TYPENOTAPPLICABLE = IOFileEnum::TYPENOTAPPLICABLE; #endif #if !defined(ITK_LEGACY_REMOVE) /**Exposes enums values for backwards compatibility*/ static constexpr IOByteOrderEnum BigEndian = IOByteOrderEnum::BigEndian; static constexpr IOByteOrderEnum LittleEndian = IOByteOrderEnum::LittleEndian; static constexpr IOByteOrderEnum OrderNotApplicable = IOByteOrderEnum::OrderNotApplicable; #endif #if !defined(ITK_LEGACY_REMOVE) /**Exposes enums values for backwards compatibility*/ static constexpr IOFileModeEnum ReadMode = IOFileModeEnum::ReadMode; static constexpr IOFileModeEnum WriteMode = IOFileModeEnum::WriteMode; #endif #if !defined(ITK_LEGACY_REMOVE) /**Exposes enums values for backwards compatibility*/ static constexpr CellGeometryEnum VERTEX_CELL = CellGeometryEnum::VERTEX_CELL; static constexpr CellGeometryEnum LINE_CELL = CellGeometryEnum::LINE_CELL; static constexpr CellGeometryEnum TRIANGLE_CELL = CellGeometryEnum::TRIANGLE_CELL; static constexpr CellGeometryEnum QUADRILATERAL_CELL = CellGeometryEnum::QUADRILATERAL_CELL; static constexpr CellGeometryEnum POLYGON_CELL = CellGeometryEnum::POLYGON_CELL; static constexpr CellGeometryEnum TETRAHEDRON_CELL = CellGeometryEnum::TETRAHEDRON_CELL; static constexpr CellGeometryEnum HEXAHEDRON_CELL = CellGeometryEnum::HEXAHEDRON_CELL; static constexpr CellGeometryEnum QUADRATIC_EDGE_CELL = CellGeometryEnum::QUADRATIC_EDGE_CELL; static constexpr CellGeometryEnum QUADRATIC_TRIANGLE_CELL = CellGeometryEnum::QUADRATIC_TRIANGLE_CELL; static constexpr CellGeometryEnum LAST_ITK_CELL = CellGeometryEnum::LAST_ITK_CELL; static constexpr CellGeometryEnum MAX_ITK_CELLS = CellGeometryEnum::MAX_ITK_CELLS; #endif /** Set/Get the type of the point/cell pixel. The PixelTypes provides context * to the IO mechanisms for data conversions. PixelTypes can be * SCALAR, RGB, RGBA, VECTOR, COVARIANTVECTOR, POINT, INDEX. If * the PIXELTYPE is SCALAR, then the NumberOfComponents should be 1. * Any other of PIXELTYPE will have more than one component. */ itkSetEnumMacro(PointPixelType, itk::CommonEnums::IOPixel); itkGetEnumMacro(PointPixelType, itk::CommonEnums::IOPixel); itkSetEnumMacro(CellPixelType, itk::CommonEnums::IOPixel); itkGetEnumMacro(CellPixelType, itk::CommonEnums::IOPixel); /** Set/Get the component type of the point, cell, point data and cell data. This is always a native type. */ itkSetEnumMacro(PointComponentType, itk::CommonEnums::IOComponent); itkGetEnumMacro(PointComponentType, itk::CommonEnums::IOComponent); itkSetEnumMacro(CellComponentType, itk::CommonEnums::IOComponent); itkGetEnumMacro(CellComponentType, itk::CommonEnums::IOComponent); itkSetEnumMacro(PointPixelComponentType, itk::CommonEnums::IOComponent); itkGetEnumMacro(PointPixelComponentType, itk::CommonEnums::IOComponent); itkSetEnumMacro(CellPixelComponentType, itk::CommonEnums::IOComponent); itkGetEnumMacro(CellPixelComponentType, itk::CommonEnums::IOComponent); template struct MapComponentType { static constexpr IOComponentEnum CType = IOComponentEnum::UNKNOWNCOMPONENTTYPE; }; template void SetPixelType(const T & itkNotUsed(dummy), bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(1); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::SCALAR); } else { SetNumberOfCellPixelComponents(1); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::SCALAR); } } template void SetPixelType(const RGBPixel & itkNotUsed(dummy), bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(3); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::RGB); } else { SetNumberOfCellPixelComponents(3); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::RGB); } } template void SetPixelType(const RGBAPixel & itkNotUsed(dummy), bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(4); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::RGBA); } else { SetNumberOfCellPixelComponents(4); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::RGBA); } } template void SetPixelType(const Vector & itkNotUsed(dummy), bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(VLength); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::VECTOR); } else { SetNumberOfCellPixelComponents(VLength); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::VECTOR); } } template void SetPixelType(const CovariantVector & itkNotUsed(dummy), bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(VLength); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::COVARIANTVECTOR); } else { SetNumberOfCellPixelComponents(VLength); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::COVARIANTVECTOR); } } template void SetPixelType(const FixedArray & itkNotUsed(dummy), bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(VLength); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::FIXEDARRAY); } else { SetNumberOfCellPixelComponents(VLength); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::FIXEDARRAY); } } template void SetPixelType(const SymmetricSecondRankTensor itkNotUsed(dummy), bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(VLength * (VLength + 1) / 2); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::SYMMETRICSECONDRANKTENSOR); } else { SetNumberOfCellPixelComponents(VLength * (VLength + 1) / 2); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::SYMMETRICSECONDRANKTENSOR); } } template void SetPixelType(const DiffusionTensor3D & itkNotUsed(dummy), bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(6); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::DIFFUSIONTENSOR3D); } else { SetNumberOfCellPixelComponents(6); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::DIFFUSIONTENSOR3D); } } template void SetPixelType(const Matrix & itkNotUsed(dummy), bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(VRows * VColumns); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::MATRIX); } else { SetNumberOfCellPixelComponents(VRows * VColumns); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::MATRIX); } } template void SetPixelType(const std::complex & itkNotUsed(dummy), bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(2); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::COMPLEX); } else { SetNumberOfCellPixelComponents(2); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::COMPLEX); } } template void SetPixelType(const Array & array, bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(array.Size()); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::ARRAY); } else { SetNumberOfCellPixelComponents(array.Size()); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::ARRAY); } } template void SetPixelType(const VariableLengthVector & vector, bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(vector.Size()); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::VARIABLELENGTHVECTOR); } else { SetNumberOfCellPixelComponents(vector.Size()); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::VARIABLELENGTHVECTOR); } } template void SetPixelType(const VariableSizeMatrix & matrix, bool usePointPixel = true) { if (usePointPixel) { SetNumberOfPointPixelComponents(matrix.Rows() * matrix.Cols()); SetPointPixelComponentType(MapComponentType::CType); SetPointPixelType(IOPixelEnum::VARIABLESIZEMATRIX); } else { SetNumberOfCellPixelComponents(matrix.Rows() * matrix.Cols()); SetCellPixelComponentType(MapComponentType::CType); SetCellPixelType(IOPixelEnum::VARIABLESIZEMATRIX); } } /** Set/Get the number of components per pixel in the mesh. This may * be set by the reading process. For SCALAR pixel types, * NumberOfComponents will be 1. For other pixel types, * NumberOfComponents will be greater than or equal to one. */ itkSetMacro(NumberOfPointPixelComponents, unsigned int); itkGetConstMacro(NumberOfPointPixelComponents, unsigned int); itkSetMacro(NumberOfCellPixelComponents, unsigned int); itkGetConstMacro(NumberOfCellPixelComponents, unsigned int); itkSetMacro(PointDimension, unsigned int); itkGetConstMacro(PointDimension, unsigned int); itkSetMacro(NumberOfPoints, SizeValueType); itkGetConstMacro(NumberOfPoints, SizeValueType); itkSetMacro(NumberOfCells, SizeValueType); itkGetConstMacro(NumberOfCells, SizeValueType); itkSetMacro(NumberOfPointPixels, SizeValueType); itkGetConstMacro(NumberOfPointPixels, SizeValueType); itkSetMacro(NumberOfCellPixels, SizeValueType); itkGetConstMacro(NumberOfCellPixels, SizeValueType); itkSetMacro(CellBufferSize, SizeValueType); itkGetConstMacro(CellBufferSize, SizeValueType); itkSetMacro(UpdatePoints, bool); itkGetConstMacro(UpdatePoints, bool); itkBooleanMacro(UpdatePoints); itkSetMacro(UpdateCells, bool); itkGetConstMacro(UpdateCells, bool); itkBooleanMacro(UpdateCells); itkSetMacro(UpdatePointData, bool); itkGetConstMacro(UpdatePointData, bool); itkBooleanMacro(UpdatePointData); itkSetMacro(UpdateCellData, bool); itkGetConstMacro(UpdateCellData, bool); itkBooleanMacro(UpdateCellData); unsigned int GetComponentSize(IOComponentEnum componentType) const; /** Convenience method returns the IOComponentEnum as a string. This can be * used for writing output files. */ std::string GetComponentTypeAsString(IOComponentEnum) const; /** Convenience method returns the IOPixelEnum as a string. This can be * used for writing output files. */ std::string GetPixelTypeAsString(IOPixelEnum) const; /** These methods control whether the file is written binary or ASCII. * Many file formats (i.e., subclasses) ignore this flag. */ itkSetEnumMacro(FileType, IOFileEnum); itkGetEnumMacro(FileType, IOFileEnum); void SetFileTypeToASCII() { this->SetFileType(IOFileEnum::ASCII); } void SetFileTypeToBinary() { this->SetFileType(IOFileEnum::BINARY); } /** These methods indicate the byte ordering of the file you are * trying to read in. These methods will then either swap or not * swap the bytes depending on the byte ordering of the machine it * is being run on. For example, reading in a BigEndian file on a * BigEndian machine will result in no swapping. Trying to read the * same file on a LittleEndian machine will result in swapping. * Note: most UNIX machines are BigEndian while PC's and VAX's are * LittleEndian. So if the file you are reading in was generated on * a VAX or PC, SetByteOrderToLittleEndian() otherwise * SetByteOrderToBigEndian(). Some MeshIOBase subclasses * ignore these methods. */ itkSetEnumMacro(ByteOrder, IOByteOrderEnum); itkGetEnumMacro(ByteOrder, IOByteOrderEnum); void SetByteOrderToBigEndian() { this->SetByteOrder(IOByteOrderEnum::BigEndian); } void SetByteOrderToLittleEndian() { this->SetByteOrder(IOByteOrderEnum::LittleEndian); } /** Set/Get a boolean to use the compression or not. */ itkSetMacro(UseCompression, bool); itkGetConstMacro(UseCompression, bool); itkBooleanMacro(UseCompression); /** Convenience method returns the IOFileEnum as a string. This can be * used for writing output files. */ std::string GetFileTypeAsString(IOFileEnum) const; /** Convenience method returns the IOByteOrderEnum as a string. This can be * used for writing output files. */ std::string GetByteOrderAsString(IOByteOrderEnum) const; /*-------- This part of the interfaces deals with reading data ----- */ /** Determine the file type. Returns true if this MeshIO can read the * file specified. */ virtual bool CanReadFile(const char *) = 0; /** Determine the required information and whether need to ReadPoints, ReadCells, ReadPointData and ReadCellData */ virtual void ReadMeshInformation() = 0; /** Reads the data from disk into the memory buffer provided. */ virtual void ReadPoints(void * buffer) = 0; virtual void ReadCells(void * buffer) = 0; virtual void ReadPointData(void * buffer) = 0; virtual void ReadCellData(void * buffer) = 0; /*-------- This part of the interfaces deals with writing data ----- */ /** Writes the data to disk from the memory buffer provided. Make sure * that the IORegions has been set properly. */ virtual bool CanWriteFile(const char *) = 0; virtual void WriteMeshInformation() = 0; virtual void WritePoints(void * buffer) = 0; virtual void WriteCells(void * buffer) = 0; virtual void WritePointData(void * buffer) = 0; virtual void WriteCellData(void * buffer) = 0; virtual void Write() = 0; /** This method returns an array with the list of filename extensions * supported for reading by this MeshIO class. This is intended to * facilitate GUI and application level integration. */ const ArrayOfExtensionsType & GetSupportedReadExtensions() const; /** This method returns an array with the list of filename extensions * supported for writing by this MeshIO class. This is intended to * facilitate GUI and application level integration. */ const ArrayOfExtensionsType & GetSupportedWriteExtensions() const; protected: MeshIOBase(); ~MeshIOBase() override = default; void PrintSelf(std::ostream & os, Indent indent) const override; /** Insert an extension to the list of supported extensions for reading. */ void AddSupportedReadExtension(const char * extension); /** Insert an extension to the list of supported extensions for writing. */ void AddSupportedWriteExtension(const char * extension); /** Read data from input file stream to buffer with ascii style */ template void ReadBufferAsAscii(T * buffer, std::ifstream & inputFile, SizeValueType numberOfComponents) { for (SizeValueType i = 0; i < numberOfComponents; ++i) { inputFile >> buffer[i]; } } /** Read data from input file to buffer with binary style */ template void ReadBufferAsBinary(T * buffer, std::ifstream & inputFile, SizeValueType numberOfComponents) { inputFile.read(reinterpret_cast(buffer), numberOfComponents * sizeof(T)); if (m_ByteOrder == IOByteOrderEnum::BigEndian) { if (itk::ByteSwapper::SystemIsLittleEndian()) { itk::ByteSwapper::SwapRangeFromSystemToBigEndian(buffer, numberOfComponents); } } else if (m_ByteOrder == IOByteOrderEnum::LittleEndian) { if (itk::ByteSwapper::SystemIsBigEndian()) { itk::ByteSwapper::SwapRangeFromSystemToLittleEndian(buffer, numberOfComponents); } } } /** Write buffer to output file stream with ascii style */ template void WriteBufferAsAscii(T * buffer, std::ofstream & outputFile, SizeValueType numberOfLines, SizeValueType numberOfComponents) { for (SizeValueType ii = 0; ii < numberOfLines; ++ii) { for (SizeValueType jj = 0; jj < numberOfComponents; ++jj) { outputFile << ConvertNumberToString(buffer[ii * numberOfComponents + jj]) << " "; } outputFile << '\n'; } } /** Write buffer to output file stream with binary style */ template void WriteBufferAsBinary(TInput * buffer, std::ofstream & outputFile, SizeValueType numberOfComponents) { if (typeid(TInput) == typeid(TOutput)) { if (m_ByteOrder == IOByteOrderEnum::BigEndian && itk::ByteSwapper::SystemIsLittleEndian()) { itk::ByteSwapper::SwapRangeFromSystemToBigEndian(buffer, numberOfComponents); } else if (m_ByteOrder == IOByteOrderEnum::LittleEndian && itk::ByteSwapper::SystemIsBigEndian()) { itk::ByteSwapper::SwapRangeFromSystemToLittleEndian(buffer, numberOfComponents); } outputFile.write(reinterpret_cast(buffer), numberOfComponents); } else { const auto data = make_unique_for_overwrite(numberOfComponents); for (SizeValueType ii = 0; ii < numberOfComponents; ++ii) { data[ii] = static_cast(buffer[ii]); } if (m_ByteOrder == IOByteOrderEnum::BigEndian && itk::ByteSwapper::SystemIsLittleEndian()) { itk::ByteSwapper::SwapRangeFromSystemToBigEndian(data.get(), numberOfComponents); } else if (m_ByteOrder == IOByteOrderEnum::LittleEndian && itk::ByteSwapper::SystemIsBigEndian()) { itk::ByteSwapper::SwapRangeFromSystemToLittleEndian(data.get(), numberOfComponents); } outputFile.write(reinterpret_cast(data.get()), numberOfComponents); } } /** Read cells from a data buffer, used when writing cells. This function write all kind of cells as it is stored in cells container. It is used when cells container have only one kind of cells */ template void ReadCellsBuffer(TInput * input, TOutput * output) { if (input && output) { SizeValueType inputIndex{}; SizeValueType outputIndex{}; for (SizeValueType ii = 0; ii < m_NumberOfCells; ++ii) { ++inputIndex; // ignore the cell type auto numberOfPoints = static_cast(input[inputIndex++]); for (unsigned int jj = 0; jj < numberOfPoints; ++jj) { output[outputIndex++] = static_cast(input[inputIndex++]); } } } } /** Read cells from input buffer, used when Writing cells. This function only write specified type of cells(used when input cells container composes multiple type of cells and only want to write a specified cell type */ template void ReadCellsBuffer(TInput * input, TOutput * output, CellGeometryEnum type) { if (input && output) { SizeValueType inputIndex{}; SizeValueType outputIndex{}; for (SizeValueType ii = 0; ii < m_NumberOfCells; ++ii) { auto cellType = static_cast(input[inputIndex++]); auto nn = static_cast(input[inputIndex++]); if (cellType == type) { output[outputIndex++] = nn; for (unsigned int jj = 0; jj < nn; ++jj) { output[outputIndex++] = static_cast(input[inputIndex++]); } } else { inputIndex += nn; } } } } /** Write cells to a data buffer, used when reading a mesh. Used for cellType with constant number of points */ template void WriteCellsBuffer(TInput * input, TOutput * output, CellGeometryEnum cellType, unsigned int numberOfPoints, SizeValueType numberOfCells) { if (input && output) { SizeValueType inputIndex{}; SizeValueType outputIndex{}; for (SizeValueType ii = 0; ii < numberOfCells; ++ii) { output[outputIndex++] = static_cast(cellType); output[outputIndex++] = static_cast(numberOfPoints); for (unsigned int jj = 0; jj < numberOfPoints; ++jj) { output[outputIndex++] = static_cast(input[inputIndex++]); } } } } /** Write cells to a data buffer, used when reading mesh, used for cellType with non-constant number of points */ template void WriteCellsBuffer(TInput * input, TOutput * output, CellGeometryEnum cellType, SizeValueType numberOfCells) { if (input && output) { SizeValueType inputIndex{}; SizeValueType outputIndex{}; for (SizeValueType ii = 0; ii < numberOfCells; ++ii) { auto numberOfPoints = static_cast(input[inputIndex++]); if (numberOfPoints > 2 && cellType == CellGeometryEnum::LINE_CELL) { output[outputIndex++] = static_cast(CellGeometryEnum::POLYLINE_CELL); } else { output[outputIndex++] = static_cast(cellType); } output[outputIndex++] = static_cast(numberOfPoints); for (unsigned int jj = 0; jj < numberOfPoints; ++jj) { output[outputIndex++] = static_cast(input[inputIndex++]); } } } } protected: /** Big or Little Endian, and the type of the file. (May be ignored.) */ IOByteOrderEnum m_ByteOrder{ IOByteOrderEnum::OrderNotApplicable }; IOFileEnum m_FileType{ IOFileEnum::ASCII }; /** Filename to read */ std::string m_FileName{}; /** Should we compress the data? */ bool m_UseCompression{ false }; /** Used internally to keep track of the type of the component. */ IOComponentEnum m_PointComponentType{ IOComponentEnum::UNKNOWNCOMPONENTTYPE }; IOComponentEnum m_CellComponentType{ IOComponentEnum::UNKNOWNCOMPONENTTYPE }; IOComponentEnum m_PointPixelComponentType{ IOComponentEnum::UNKNOWNCOMPONENTTYPE }; IOComponentEnum m_CellPixelComponentType{ IOComponentEnum::UNKNOWNCOMPONENTTYPE }; /** Used internally to keep track of the type of the pixel. */ IOPixelEnum m_PointPixelType{ IOPixelEnum::SCALAR }; IOPixelEnum m_CellPixelType{ IOPixelEnum::SCALAR }; /** Stores the number of components per pixel. This will be 1 for * grayscale images, 3 for RGBPixel images, and 4 for RGBPixelA images. */ unsigned int m_NumberOfPointPixelComponents{ 0 }; unsigned int m_NumberOfCellPixelComponents{ 0 }; /** The number of independent dimensions in the point. */ unsigned int m_PointDimension{ 3 }; /** The number of points and cells */ SizeValueType m_NumberOfPoints{}; SizeValueType m_NumberOfCells{}; SizeValueType m_NumberOfPointPixels{}; SizeValueType m_NumberOfCellPixels{}; /** The buffer size of cells */ SizeValueType m_CellBufferSize{}; /** Flags indicate whether read or write points, cells, point data and cell data */ bool m_UpdatePoints{ false }; bool m_UpdateCells{ false }; bool m_UpdatePointData{ false }; bool m_UpdateCellData{ false }; private: ArrayOfExtensionsType m_SupportedReadExtensions{}; ArrayOfExtensionsType m_SupportedWriteExtensions{}; }; #define MESHIOBASE_TYPEMAP(type, ctype) \ template <> \ struct MeshIOBase::MapComponentType \ { \ static constexpr IOComponentEnum CType = ctype; \ } MESHIOBASE_TYPEMAP(unsigned char, IOComponentEnum::UCHAR); MESHIOBASE_TYPEMAP(char, IOComponentEnum::CHAR); MESHIOBASE_TYPEMAP(unsigned short, IOComponentEnum::USHORT); MESHIOBASE_TYPEMAP(short, IOComponentEnum::SHORT); MESHIOBASE_TYPEMAP(unsigned int, IOComponentEnum::UINT); MESHIOBASE_TYPEMAP(int, IOComponentEnum::INT); MESHIOBASE_TYPEMAP(unsigned long, IOComponentEnum::ULONG); MESHIOBASE_TYPEMAP(long, IOComponentEnum::LONG); MESHIOBASE_TYPEMAP(unsigned long long, IOComponentEnum::ULONGLONG); MESHIOBASE_TYPEMAP(long long, IOComponentEnum::LONGLONG); MESHIOBASE_TYPEMAP(float, IOComponentEnum::FLOAT); MESHIOBASE_TYPEMAP(double, IOComponentEnum::DOUBLE); MESHIOBASE_TYPEMAP(long double, IOComponentEnum::LDOUBLE); #undef MESHIOBASE_TYPEMAP } // end namespace itk #endif