ITK  4.9.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 "ITKIOMeshExport.h"
22 
23 #include "itkMeshIOBase.h"
24 #include "itkNumberToString.h"
25 #include <fstream>
26 
27 namespace itk
28 {
35 class ITKIOMesh_EXPORT OBJMeshIO:public MeshIOBase
36 {
37 public:
39  typedef OBJMeshIO Self;
43 
45 
47  itkNewMacro(Self);
48 
50  itkTypeMacro(OBJMeshIO, MeshIOBase);
51 
52  /*-------- This part of the interfaces deals with reading data. ----- */
53 
59  virtual bool CanReadFile(const char *FileNameToRead) ITK_OVERRIDE;
60 
62  virtual void ReadMeshInformation() ITK_OVERRIDE;
63 
65  virtual void ReadPoints(void *buffer) ITK_OVERRIDE;
66 
67  virtual void ReadCells(void *buffer) ITK_OVERRIDE;
68 
69  virtual void ReadPointData(void *buffer) ITK_OVERRIDE;
70 
71  virtual void ReadCellData(void *buffer) ITK_OVERRIDE;
72 
73  /*-------- This part of the interfaces deals with writing data. ----- */
74 
80  virtual bool CanWriteFile(const char *FileNameToWrite) ITK_OVERRIDE;
81 
83  virtual void WriteMeshInformation() ITK_OVERRIDE;
84 
87  virtual void WritePoints(void *buffer) ITK_OVERRIDE;
88 
89  virtual void WriteCells(void *buffer) ITK_OVERRIDE;
90 
91  virtual void WritePointData(void *buffer) ITK_OVERRIDE;
92 
93  virtual void WriteCellData(void *buffer) ITK_OVERRIDE;
94 
95  virtual void Write() ITK_OVERRIDE;
96 
97 protected:
99  template< typename T >
100  void WritePoints(T *buffer, std::ofstream & outputFile)
101  {
102  NumberToString<T> convert;
105 
106  for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
107  {
108  outputFile << "v ";
109  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
110  {
111  outputFile << convert(buffer[index++]) << " ";
112  }
113  outputFile << '\n';
114  }
115  }
116 
117  template< typename T >
118  void WriteCells(T *buffer, std::ofstream & outputFile)
119  {
121 
122  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
123  {
124  outputFile << "f ";
125  index++;
126  unsigned int numberOfCellPoints = static_cast< unsigned int >( buffer[index++] );
127 
128  for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
129  {
130  outputFile << buffer[index++] + 1 << " ";
131  }
132  outputFile << '\n';
133  }
134  }
135 
137  template< typename T >
138  void WritePointData(T *buffer, std::ofstream & outputFile)
139  {
140  NumberToString<T> convert;
143 
144  for ( SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++ )
145  {
146  outputFile << "vn ";
147  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
148  {
149  outputFile << convert(buffer[index++]) << " ";
150  }
151 
152  outputFile << '\n';
153  }
154  }
155 
156 protected:
157  OBJMeshIO();
158  virtual ~OBJMeshIO(){}
159 
160  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
161 
162  void OpenFile();
163 
164  void CloseFile();
165 
166 private:
167  OBJMeshIO(const Self &) ITK_DELETE_FUNCTION;
168  void operator=(const Self &) ITK_DELETE_FUNCTION;
169 
170  std::ifstream m_InputFile;
171  std::streampos m_PointsStartPosition; // file position for points rlative to
172  // std::ios::beg
173 };
174 } // end namespace itk
175 
176 #endif
void WritePointData(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:138
Light weight base class for most itk classes.
void WriteCells(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:118
This class defines how to read and write Object file format.
Definition: itkOBJMeshIO.h:35
virtual ~OBJMeshIO()
Definition: itkOBJMeshIO.h:158
SmartPointer< Self > Pointer
Definition: itkOBJMeshIO.h:42
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:84
unsigned long SizeValueType
Definition: itkIntTypes.h:143
Superclass::SizeValueType SizeValueType
Definition: itkOBJMeshIO.h:44
Convert floating and fixed point numbers to strings.
SmartPointer< const Self > ConstPointer
Definition: itkOBJMeshIO.h:41
OBJMeshIO Self
Definition: itkOBJMeshIO.h:39
MeshIOBase Superclass
Definition: itkOBJMeshIO.h:40
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:69