ITK  4.4.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 #include "itkNumberToString.h"
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  {
101  NumberToString<T> convert;
103 
104  for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
105  {
106  outputFile << "v ";
107  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
108  {
109  outputFile << convert(buffer[index++]) << " ";
110  }
111  outputFile << '\n';
112  }
113  }
114 
115  template< typename T >
116  void WriteCells(T *buffer, std::ofstream & outputFile)
117  {
119 
120  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
121  {
122  outputFile << "f ";
123  index++;
124  unsigned int numberOfCellPoints = static_cast< unsigned int >( buffer[index++] );
125 
126  for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
127  {
128  outputFile << buffer[index++] + 1 << " ";
129  }
130  outputFile << '\n';
131  }
132  }
133 
135  template< typename T >
136  void WritePointData(T *buffer, std::ofstream & outputFile)
137  {
138  NumberToString<T> convert;
140 
141  for ( SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++ )
142  {
143  outputFile << "vn ";
144  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
145  {
146  outputFile << convert(buffer[index++]) << " ";
147  }
148 
149  outputFile << '\n';
150  }
151  }
152 
153 protected:
154  OBJMeshIO();
155  virtual ~OBJMeshIO(){}
156 
157  void PrintSelf(std::ostream & os, Indent indent) const;
158 
159  void OpenFile();
160 
161  void CloseFile();
162 
163 private:
164  OBJMeshIO(const Self &); // purposely not implemented
165  void operator=(const Self &); // purposely not implemented
166 
167  std::ifstream m_InputFile;
168  std::streampos m_PointsStartPosition; // file position for points rlative to
169  // std::ios::beg
170 };
171 } // end namespace itk
172 
173 #endif
174