ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkOBJMeshIO.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 {
35 class ITKIOMeshOBJ_EXPORT OBJMeshIO:public MeshIOBase
36 {
37 public:
38  ITK_DISALLOW_COPY_AND_ASSIGN(OBJMeshIO);
39 
41  using Self = OBJMeshIO;
45 
47 
49  itkNewMacro(Self);
50 
52  itkTypeMacro(OBJMeshIO, MeshIOBase);
53 
54  /*-------- This part of the interfaces deals with reading data. ----- */
55 
61  bool CanReadFile(const char *FileNameToRead) override;
62 
64  void ReadMeshInformation() override;
65 
67  void ReadPoints(void *buffer) override;
68 
69  void ReadCells(void *buffer) override;
70 
71  void ReadPointData(void *buffer) override;
72 
73  void ReadCellData(void *buffer) override;
74 
75  /*-------- This part of the interfaces deals with writing data. ----- */
76 
82  bool CanWriteFile(const char *FileNameToWrite) override;
83 
85  void WriteMeshInformation() override;
86 
89  void WritePoints(void *buffer) override;
90 
91  void WriteCells(void *buffer) override;
92 
93  void WritePointData(void *buffer) override;
94 
95  void WriteCellData(void *buffer) override;
96 
97  void Write() override;
98 
99 protected:
101  template< typename T >
102  void WritePoints(T *buffer, std::ofstream & outputFile)
103  {
104  NumberToString<T> convert;
105  SizeValueType index = itk::NumericTraits< SizeValueType >::ZeroValue();
107 
108  for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
109  {
110  outputFile << "v ";
111  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
112  {
113  outputFile << convert(buffer[index++]) << " ";
114  }
115  outputFile << '\n';
116  }
117  }
118 
119  template< typename T >
120  void WriteCells(T *buffer, std::ofstream & outputFile)
121  {
122  SizeValueType index = itk::NumericTraits< SizeValueType >::ZeroValue();
123 
124  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
125  {
126  outputFile << "f ";
127  index++;
128  auto numberOfCellPoints = static_cast< unsigned int >( buffer[index++] );
129 
130  for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
131  {
132  outputFile << buffer[index++] + 1 << " ";
133  }
134  outputFile << '\n';
135  }
136  }
137 
139  template< typename T >
140  void WritePointData(T *buffer, std::ofstream & outputFile)
141  {
142  NumberToString<T> convert;
143  SizeValueType index = itk::NumericTraits< SizeValueType >::ZeroValue();
145 
146  for ( SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++ )
147  {
148  outputFile << "vn ";
149  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
150  {
151  outputFile << convert(buffer[index++]) << " ";
152  }
153 
154  outputFile << '\n';
155  }
156  }
157 
158  static bool SplitLine(const std::string& line, std::string& type, std::string& content);
159 
160 protected:
161  OBJMeshIO();
162  ~OBJMeshIO() override;
163 
164  void PrintSelf(std::ostream & os, Indent indent) const override;
165 
166  void OpenFile();
167 
168  void CloseFile();
169 
170 private:
171  std::ifstream m_InputFile;
172  std::streampos m_PointsStartPosition; // file position for points rlative to
173  // std::ios::beg
174 };
175 } // end namespace itk
176 
177 #endif
void WritePointData(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:140
Light weight base class for most itk classes.
void WritePoints(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:102
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:86
unsigned long SizeValueType
Definition: itkIntTypes.h:83
void WriteCells(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:120
This class defines how to read and write Object file format.
Definition: itkOBJMeshIO.h:35
std::streampos m_PointsStartPosition
Definition: itkOBJMeshIO.h:172
std::ifstream m_InputFile
Definition: itkOBJMeshIO.h:171
Convert floating and fixed point numbers to strings.
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:69
Base class for most ITK classes.
Definition: itkObject.h:60