ITK  5.2.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  * http://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_ASSIGN(OBJMeshIO);
40 
42  using Self = OBJMeshIO;
46 
48 
50  itkNewMacro(Self);
51 
53  itkTypeMacro(OBJMeshIO, MeshIOBase);
54 
55  /*-------- This part of the interfaces deals with reading data. ----- */
56 
62  bool
63  CanReadFile(const char * FileNameToRead) 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 * FileNameToWrite) 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  NumberToString<T> convert;
122 
123  for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++)
124  {
125  outputFile << "v ";
126  for (unsigned int jj = 0; jj < this->m_PointDimension; jj++)
127  {
128  outputFile << convert(buffer[index++]) << " ";
129  }
130  outputFile << '\n';
131  }
132  }
133 
134  template <typename T>
135  void
136  WriteCells(T * buffer, std::ofstream & outputFile)
137  {
139 
140  for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++)
141  {
142  outputFile << "f ";
143  index++;
144  auto numberOfCellPoints = static_cast<unsigned int>(buffer[index++]);
145 
146  for (unsigned int jj = 0; jj < numberOfCellPoints; jj++)
147  {
148  outputFile << buffer[index++] + 1 << " ";
149  }
150  outputFile << '\n';
151  }
152  }
153 
155  template <typename T>
156  void
157  WritePointData(T * buffer, std::ofstream & outputFile)
158  {
159  NumberToString<T> convert;
162 
163  for (SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++)
164  {
165  outputFile << "vn ";
166  for (unsigned int jj = 0; jj < this->m_PointDimension; jj++)
167  {
168  outputFile << convert(buffer[index++]) << " ";
169  }
170 
171  outputFile << '\n';
172  }
173  }
174 
175  static bool
176  SplitLine(const std::string & line, std::string & type, std::string & content);
177 
178 protected:
179  OBJMeshIO();
180  ~OBJMeshIO() override;
181 
182  void
183  PrintSelf(std::ostream & os, Indent indent) const override;
184 
185  void
186  OpenFile();
187 
188  void
189  CloseFile();
190 
191 private:
192  std::ifstream m_InputFile;
193  std::streampos m_PointsStartPosition; // file position for points rlative to
194  // std::ios::beg
195 };
196 } // end namespace itk
197 
198 #endif
itk::OBJMeshIO::WritePointData
void WritePointData(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:157
itk::OBJMeshIO::WriteCells
void WriteCells(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:136
itk::NumberToString
Convert floating and fixed point numbers to strings.
Definition: itkNumberToString.h:42
itk::MeshIOBase::SizeValueType
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:88
itk::OBJMeshIO::WritePoints
void WritePoints(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:117
itk::OBJMeshIO::m_PointsStartPosition
std::streampos m_PointsStartPosition
Definition: itkOBJMeshIO.h:193
itk::MeshIOBase
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:71
itk::SmartPointer< const Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::OBJMeshIO::m_InputFile
std::ifstream m_InputFile
Definition: itkOBJMeshIO.h:192
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:59
itkNumberToString.h
itkMeshIOBase.h
itk::NumericTraits::ZeroValue
static T ZeroValue()
Definition: itkNumericTraits.h:148
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:62
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83