ITK  4.2.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 
22 #include "itkMeshIOBase.h"
23 
24 #include <fstream>
25 
26 namespace itk
27 {
34 class ITK_EXPORT OBJMeshIO:public MeshIOBase
35 {
36 public:
38  typedef OBJMeshIO Self;
42 
44 
46  itkNewMacro(Self);
47 
49  itkTypeMacro(OBJMeshIO, MeshIOBase);
50 
51  /*-------- This part of the interfaces deals with reading data. ----- */
52 
58  virtual bool CanReadFile(const char *FileNameToRead);
59 
61  virtual void ReadMeshInformation();
62 
64  virtual void ReadPoints(void *buffer);
65 
66  virtual void ReadCells(void *buffer);
67 
68  virtual void ReadPointData(void *buffer);
69 
70  virtual void ReadCellData(void *buffer);
71 
72  /*-------- This part of the interfaces deals with writing data. ----- */
73 
79  virtual bool CanWriteFile(const char *FileNameToWrite);
80 
82  virtual void WriteMeshInformation();
83 
86  virtual void WritePoints(void *buffer);
87 
88  virtual void WriteCells(void *buffer);
89 
90  virtual void WritePointData(void *buffer);
91 
92  virtual void WriteCellData(void *buffer);
93 
94  virtual void Write();
95 
96 protected:
98  template< typename T >
99  void WritePoints(T *buffer, std::ofstream & outputFile)
100  {
102 
103  for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
104  {
105  outputFile << "v ";
106  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
107  {
108  outputFile << buffer[index++] << " ";
109  }
110  outputFile << '\n';
111  }
112  }
113 
114  template< typename T >
115  void WriteCells(T *buffer, std::ofstream & outputFile)
116  {
118 
119  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
120  {
121  outputFile << "f ";
122  index++;
123  unsigned int numberOfCellPoints = static_cast< unsigned int >( buffer[index++] );
124 
125  for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
126  {
127  outputFile << buffer[index++] + 1 << " ";
128  }
129  outputFile << '\n';
130  }
131  }
132 
134  template< typename T >
135  void WritePointData(T *buffer, std::ofstream & outputFile)
136  {
138 
139  for ( SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++ )
140  {
141  outputFile << "vn ";
142  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
143  {
144  outputFile << buffer[index++] << " ";
145  }
146 
147  outputFile << '\n';
148  }
149  }
150 
151 protected:
152  OBJMeshIO();
153  virtual ~OBJMeshIO(){}
154 
155  void PrintSelf(std::ostream & os, Indent indent) const;
156 
157  void OpenFile();
158 
159  void CloseFile();
160 
161 private:
162  OBJMeshIO(const Self &); // purposely not implemented
163  void operator=(const Self &); // purposely not implemented
164 
165  std::ifstream m_InputFile;
166  std::streampos m_PointsStartPosition; // file position for points rlative to
167  // std::ios::beg
168 };
169 } // end namespace itk
170 
171 #endif
172