ITK  6.0.0
Insight Toolkit
itkOFFMeshIO.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19#ifndef itkOFFMeshIO_h
20#define itkOFFMeshIO_h
21#include "ITKIOMeshOFFExport.h"
22
23#include "itkMeshIOBase.h"
25
26#include <fstream>
27
28namespace itk
29{
37class ITKIOMeshOFF_EXPORT OFFMeshIO : public MeshIOBase
38{
39public:
40 ITK_DISALLOW_COPY_AND_MOVE(OFFMeshIO);
41
43 using Self = OFFMeshIO;
47
50
52 itkNewMacro(Self);
53
55 itkOverrideGetNameOfClassMacro(OFFMeshIO);
56
57 /*-------- This part of the interfaces deals with reading data. ----- */
58
64 bool
65 CanReadFile(const char * fileName) override;
66
68 void
70
72 void
73 ReadPoints(void * buffer) override;
74
75 void
76 ReadCells(void * buffer) override;
77
78 void
79 ReadPointData(void * buffer) override;
80
81 void
82 ReadCellData(void * buffer) override;
83
84 /*-------- This part of the interfaces deals with writing data. ----- */
85
91 bool
92 CanWriteFile(const char * fileName) override;
93
95 void
97
100 void
101 WritePoints(void * buffer) override;
102
103 void
104 WriteCells(void * buffer) override;
105
106 void
107 WritePointData(void * buffer) override;
108
109 void
110 WriteCellData(void * buffer) override;
111
112 void
113 Write() override;
114
115protected:
117 template <typename T>
118 void
119 ReadCellsBufferAsAscii(T * buffer, std::ifstream & inputFile)
120 {
121 SizeValueType index = 0;
122 unsigned int numberOfPoints = 0;
123 std::string line;
124
125 for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ++ii)
126 {
127 inputFile >> numberOfPoints;
128 buffer[index++] = static_cast<T>(numberOfPoints);
129 for (unsigned int jj = 0; jj < numberOfPoints; ++jj)
130 {
131 inputFile >> buffer[index++];
132 }
133 std::getline(inputFile, line, '\n');
134 }
135 }
136
140 template <typename TInput, typename TOutput>
141 void
142 ReadCellsBuffer(TInput * input, TOutput * output)
143 {
144 if (input && output)
145 {
146 SizeValueType indInput = 0;
147 SizeValueType indOutput = 0;
148 for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ++ii)
149 {
150 ++indInput; // ignore the cell type
151 auto numberOfPoints = static_cast<unsigned int>(input[indInput++]);
152 output[indOutput++] = static_cast<TOutput>(numberOfPoints);
153 for (unsigned int jj = 0; jj < numberOfPoints; ++jj)
154 {
155 output[indOutput++] = static_cast<TOutput>(input[indInput++]);
156 }
157 }
158 }
159 }
162 template <typename T>
163 void
164 WriteCellsAsAscii(T * buffer, std::ofstream & outputFile)
165 {
166 SizeValueType index = 0;
167
168 for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ++ii)
169 {
170 ++index;
171 auto numberOfCellPoints = static_cast<unsigned int>(buffer[index++]);
172 outputFile << numberOfCellPoints << " ";
173
174 for (unsigned int jj = 0; jj < numberOfCellPoints; ++jj)
175 {
176 outputFile << buffer[index++] << " ";
177 }
178
179 outputFile << '\n';
180 }
181 }
182
183 template <typename TOutput, typename TInput>
184 void
185 WriteCellsAsBinary(TInput * buffer, std::ofstream & outputFile)
186 {
187 const auto data = make_unique_for_overwrite<TOutput[]>(m_CellBufferSize - this->m_NumberOfCells);
188
189 ReadCellsBuffer(buffer, data.get());
190 WriteBufferAsBinary<TOutput>(data.get(), outputFile, m_CellBufferSize - this->m_NumberOfCells);
191 }
192
193protected:
195 ~OFFMeshIO() override;
196
197 void
198 PrintSelf(std::ostream & os, Indent indent) const override;
199
200 void
202
203 void
205
206private:
207 std::ifstream m_InputFile{};
208 StreamOffsetType m_PointsStartPosition{}; // file position for points relative to std::ios::beg
209 bool m_TriangleCellType{}; // if all cells are triangle it is true. otherwise, it is false.
210};
211} // end namespace itk
212
213#endif
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Light weight base class for most itk classes.
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:73
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:89
std::streamoff StreamOffsetType
Definition: itkMeshIOBase.h:87
this class defines how to read and write Object file format.
Definition: itkOFFMeshIO.h:38
void ReadPointData(void *buffer) override
bool CanWriteFile(const char *fileName) override
void Write() override
~OFFMeshIO() override
void ReadCellsBuffer(TInput *input, TOutput *output)
Definition: itkOFFMeshIO.h:142
void WriteCells(void *buffer) override
void WriteCellData(void *buffer) override
void ReadPoints(void *buffer) override
void WriteCellsAsAscii(T *buffer, std::ofstream &outputFile)
Definition: itkOFFMeshIO.h:164
void WriteCellsAsBinary(TInput *buffer, std::ofstream &outputFile)
Definition: itkOFFMeshIO.h:185
void WriteMeshInformation() override
void ReadCells(void *buffer) override
void WritePoints(void *buffer) override
void PrintSelf(std::ostream &os, Indent indent) const override
void ReadCellsBufferAsAscii(T *buffer, std::ifstream &inputFile)
Definition: itkOFFMeshIO.h:119
void WritePointData(void *buffer) override
bool CanReadFile(const char *fileName) override
void ReadCellData(void *buffer) override
void ReadMeshInformation() override
Base class for most ITK classes.
Definition: itkObject.h:62
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition: itkIntTypes.h:86