forked from InsightSoftwareConsortium/itk-wasm
/
itkWASMMeshIO.h
135 lines (109 loc) · 4.65 KB
/
itkWASMMeshIO.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*=========================================================================
*
* 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
*
* http://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 itkWASMMeshIO_h
#define itkWASMMeshIO_h
#include "WebAssemblyInterfaceExport.h"
#include "itkMeshIOBase.h"
#include <fstream>
namespace itk
{
/** \class WASMMeshIO
*
* \brief Read and write the an itk::Mesh in a format for interfacing in WebAssembly (WASM).
*
* This format is intended to facilitate data exchange in itk-wasm.
* It reads and writes an itk-wasm Mesh object where TypedArrays are
* replaced by binary files on the filesystem or in a ZIP file.
*
* The format is experimental and subject to change. We mean it.
*
* \ingroup IOFilters
* \ingroup WebAssemblyInterface
*/
class WebAssemblyInterface_EXPORT WASMMeshIO: public MeshIOBase
{
public:
/** Standard class typedefs. */
typedef WASMMeshIO Self;
typedef MeshIOBase Superclass;
typedef SmartPointer< Self > Pointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(WASMMeshIO, MeshIOBase);
bool CanReadFile(const char *) override;
/** Determine the required information and whether need to ReadPoints,
ReadCells, ReadPointData and ReadCellData */
void ReadMeshInformation() override;
/** Reads the data from disk into the memory buffer provided. */
void ReadPoints(void *buffer) override;
void ReadCells(void *buffer) override;
void ReadPointData(void *buffer) override;
void ReadCellData(void *buffer) override;
/*-------- 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. */
bool CanWriteFile(const char *) override;
void WriteMeshInformation() override;
void WritePoints(void *buffer) override;
void WriteCells(void *buffer) override;
void WritePointData(void *buffer) override;
void WriteCellData(void *buffer) override;
void Write() override;
protected:
WASMMeshIO();
~WASMMeshIO() override;
void PrintSelf(std::ostream & os, Indent indent) const override;
static size_t ITKComponentSize( const CommonEnums::IOComponent );
/** \brief Opens a file for reading and random access
*
* \param[out] inputStream is an istream presumed to be opened for reading
* \param[in] filename is the name of the file
* \param[in] ascii optional (default is false);
* if true than the file will be opened in ASCII mode,
* which generally only applies to Windows
*
* The stream is closed if it's already opened. If an error is
* encountered than an exception will be thrown.
*/
void OpenFileForReading(std::ifstream & inputStream, const std::string & filename,
bool ascii = false);
/** \brief Opens a file for writing and random access
*
* \param[out] outputStream is an ostream presumed to be opened for writing
* \param[in] filename is the name of the file
* \param[in] truncate optional (default is true);
* if true than the file's existing content is truncated,
* if false than the file is opened for reading and
* writing with existing content intact
* \param[in] ascii optional (default is false);
* if true than the file will be opened in ASCII mode,
* which generally only applies to Windows
*
* The stream is closed if it's already opened. If an error is
* encountered than an exception will be thrown.
*/
void OpenFileForWriting(std::ofstream & outputStream, const std::string & filename,
bool truncate = true, bool ascii = false);
/** Convenient method to read a buffer as binary. Return true on success. */
bool ReadBufferAsBinary(std::istream & os, void *buffer, SizeValueType numberOfBytesToBeRead);
private:
ITK_DISALLOW_COPY_AND_ASSIGN(WASMMeshIO);
};
} // end namespace itk
#endif // itkWASMMeshIO_h