ITK  4.6.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;
104 
105  for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
106  {
107  outputFile << "v ";
108  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
109  {
110  outputFile << convert(buffer[index++]) << " ";
111  }
112  outputFile << '\n';
113  }
114  }
115 
116  template< typename T >
117  void WriteCells(T *buffer, std::ofstream & outputFile)
118  {
120 
121  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
122  {
123  outputFile << "f ";
124  index++;
125  unsigned int numberOfCellPoints = static_cast< unsigned int >( buffer[index++] );
126 
127  for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
128  {
129  outputFile << buffer[index++] + 1 << " ";
130  }
131  outputFile << '\n';
132  }
133  }
134 
136  template< typename T >
137  void WritePointData(T *buffer, std::ofstream & outputFile)
138  {
139  NumberToString<T> convert;
141 
142  for ( SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++ )
143  {
144  outputFile << "vn ";
145  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
146  {
147  outputFile << convert(buffer[index++]) << " ";
148  }
149 
150  outputFile << '\n';
151  }
152  }
153 
154 protected:
155  OBJMeshIO();
156  virtual ~OBJMeshIO(){}
157 
158  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
159 
160  void OpenFile();
161 
162  void CloseFile();
163 
164 private:
165  OBJMeshIO(const Self &); // purposely not implemented
166  void operator=(const Self &); // purposely not implemented
167 
168  std::ifstream m_InputFile;
169  std::streampos m_PointsStartPosition; // file position for points rlative to
170  // std::ios::beg
171 };
172 } // end namespace itk
173 
174 #endif
void WritePointData(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:137
Light weight base class for most itk classes.
void WriteCells(T *buffer, std::ofstream &outputFile)
Definition: itkOBJMeshIO.h:117
This class defines how to read and write Object file format.
Definition: itkOBJMeshIO.h:35
virtual ~OBJMeshIO()
Definition: itkOBJMeshIO.h:156
SmartPointer< Self > Pointer
Definition: itkOBJMeshIO.h:42
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:84
std::streampos m_PointsStartPosition
Definition: itkOBJMeshIO.h:169
unsigned long SizeValueType
Definition: itkIntTypes.h:143
Superclass::SizeValueType SizeValueType
Definition: itkOBJMeshIO.h:44
std::ifstream m_InputFile
Definition: itkOBJMeshIO.h:168
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
Define additional traits for native types such as int or float.