ITK  5.4.0
Insight Toolkit
itkOBJMeshIO.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 itkOBJMeshIO_h
20 #define itkOBJMeshIO_h
21 #include "ITKIOMeshOBJExport.h"
22 
23 #include "itkMeshIOBase.h"
24 #include "itkNumberToString.h"
25 #include <fstream>
26 
27 namespace itk
28 {
36 class ITKIOMeshOBJ_EXPORT OBJMeshIO : public MeshIOBase
37 {
38 public:
39  ITK_DISALLOW_COPY_AND_MOVE(OBJMeshIO);
40 
42  using Self = OBJMeshIO;
46 
48 
50  itkNewMacro(Self);
51 
53  itkOverrideGetNameOfClassMacro(OBJMeshIO);
54 
55  /*-------- This part of the interfaces deals with reading data. ----- */
56 
62  bool
63  CanReadFile(const char * fileName) override;
64 
66  void
67  ReadMeshInformation() override;
68 
70  void
71  ReadPoints(void * buffer) override;
72 
73  void
74  ReadCells(void * buffer) override;
75 
76  void
77  ReadPointData(void * buffer) override;
78 
79  void
80  ReadCellData(void * buffer) override;
81 
82  /*-------- This part of the interfaces deals with writing data. ----- */
83 
89  bool
90  CanWriteFile(const char * fileName) override;
91 
93  void
94  WriteMeshInformation() override;
95 
98  void
99  WritePoints(void * buffer) override;
100 
101  void
102  WriteCells(void * buffer) override;
103 
104  void
105  WritePointData(void * buffer) override;
106 
107  void
108  WriteCellData(void * buffer) override;
109 
110  void
111  Write() override;
112 
113 protected:
115  template <typename T>
116  void
117  WritePoints(T * buffer, std::ofstream & outputFile)
118  {
119  SizeValueType index{};
120 
121  for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
122  {
123  outputFile << "v ";
124  for (unsigned int jj = 0; jj < this->m_PointDimension; ++jj)
125  {
126  outputFile << ConvertNumberToString(buffer[index++]) << " ";
127  }
128  outputFile << '\n';
129  }
130  }
131 
132  template <typename T>
133  void
134  WriteCells(T * buffer, std::ofstream & outputFile)
135  {
136  SizeValueType index{};
137 
138  for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ++ii)
139  {
140  outputFile << "f ";
141  ++index;
142  auto numberOfCellPoints = static_cast<unsigned int>(buffer[index++]);
143 
144  for (unsigned int jj = 0; jj < numberOfCellPoints; ++jj)
145  {
146  outputFile << buffer[index++] + 1 << " ";
147  }
148  outputFile << '\n';
149  }
150  }
151 
153  template <typename T>
154  void
155  WritePointData(T * buffer, std::ofstream & outputFile)
156  {
157  SizeValueType index{};
158 
159  for (SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ++ii)
160  {
161  outputFile << "vn ";
162  for (unsigned int jj = 0; jj < this->m_PointDimension; ++jj)
163  {
164  outputFile << ConvertNumberToString(buffer[index++]) << " ";
165  }
166 
167  outputFile << '\n';
168  }
169  }
170 
171  static bool
172  SplitLine(const std::string & line, std::string & type, std::string & content);
173 
174 protected:
175  OBJMeshIO();
176  ~OBJMeshIO() override;
177 
178  void
179  PrintSelf(std::ostream & os, Indent indent) const override;
180 
181  void
182  OpenFile();
183 
184  void
185  CloseFile();
186 
187 private:
188  std::ifstream m_InputFile{};
189  std::streampos m_PointsStartPosition{}; // file position for points relative to
190  // std::ios::beg
191 };
192 } // end namespace itk
193 
194 #endif
itk::OBJMeshIO::WritePointData
void WritePointData(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:155
itk::OBJMeshIO::WriteCells
void WriteCells(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:134
itk::MeshIOBase::SizeValueType
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:89
itk::OBJMeshIO::WritePoints
void WritePoints(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:117
itk::MeshIOBase
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:72
itk::SmartPointer< const Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::OBJMeshIO
This class defines how to read and write Object file format.
Definition: itkOBJMeshIO.h:36
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itkNumberToString.h
itk::ConvertNumberToString
std::string ConvertNumberToString(const TValue val)
Definition: itkNumberToString.h:83
itkMeshIOBase.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83