]> Creatis software - cpMesh.git/blob - lib/cpm/IO/WaveFrontOBJReader.hxx
First commit
[cpMesh.git] / lib / cpm / IO / WaveFrontOBJReader.hxx
1 #ifndef __CPM__IO__WAVEFRONTOBJREADER__HXX__
2 #define __CPM__IO__WAVEFRONTOBJREADER__HXX__
3
4 #include <cstdlib>
5 #include <fstream>
6 #include <sstream>
7
8 // -------------------------------------------------------------------------
9 template< typename M >
10 cpm::IO::WaveFrontOBJReader< M >::
11 WaveFrontOBJReader( )
12   : Superclass( )
13 {
14 }
15
16 // -------------------------------------------------------------------------
17 template< typename M >
18 cpm::IO::WaveFrontOBJReader< M >::
19 ~WaveFrontOBJReader( )
20 {
21 }
22
23 // -------------------------------------------------------------------------
24 template< typename M >
25 void cpm::IO::WaveFrontOBJReader< M >::
26 GenerateData( )
27 {
28   typedef typename PointType::ValueType TScalar;
29   typename M::Pointer out = this->GetOutput( );
30
31   out->SetCellsAllocationMethod( M::CellsAllocatedDynamicallyCellByCell );
32   if( this->m_FileName == "" )
33   {
34     itkExceptionMacro( << "No input FileName" );
35
36   } // fi
37
38   std::ifstream in( this->m_FileName.c_str( ) );
39   if( !in.is_open( ) )
40   {
41     itkExceptionMacro(
42       << "Unable to open file" << std::endl
43       << "\"" << m_FileName << "\""
44       );
45
46   } // fi
47
48   in.imbue( std::locale::classic( ) );
49   std::string buffer;
50
51   while( !in.eof( ) )
52   {
53     std::getline( in, buffer, '\n' );
54
55     // Tokenize
56     std::stringstream ss( buffer );
57     std::istream_iterator< std::string > it( ss );
58     std::istream_iterator< std::string > end;
59     std::vector< std::string > line( it, end );
60     if( line.size( ) == 0 )
61       continue;
62
63     // Now, we are talking...
64     if( line[ 0 ] == "v" || line[ 0 ] == "V" )
65     {
66       // Add points
67       PointType pnt;
68       pnt.Fill( TScalar( 0 ) );
69
70       if( line.size( ) > 1 && M::PointDimension >= 1 )
71         pnt[ 0 ] = TScalar( std::atof( line[ 1 ].c_str( ) ) );
72       if( line.size( ) > 2 && M::PointDimension >= 2 )
73         pnt[ 1 ] = TScalar( std::atof( line[ 2 ].c_str( ) ) );
74       if( line.size( ) > 3 && M::PointDimension >= 3 )
75         pnt[ 2 ] = TScalar( std::atof( line[ 3 ].c_str( ) ) );
76       if( M::PointDimension >= 4 )
77       {
78         if( line.size( ) > 4 )
79           pnt[ 3 ] = TScalar( std::atof( line[ 4 ].c_str( ) ) );
80         else
81           pnt[ 4 ] = TScalar( 1 );
82             
83       } // fi
84       out->SetPoint( out->GetNumberOfPoints( ), pnt );
85     }
86     else if( line[ 0 ] == "f" || line[ 0 ] == "F" )
87     {
88       if( line.size( ) > 3 )
89       {
90         // Add a face
91         CellAutoPointer cell;
92         TPolygonCell* face = new TPolygonCell( );
93
94         for( unsigned int k = 1; k < line.size( ); ++k )
95         {
96           PointIdentifier pId =
97             PointIdentifier(
98               std::atoi(
99                 line[ k ].
100                 substr( 0, line[ k ].find_first_of( "/" ) ).c_str( )
101                 )
102               );
103
104           // In OBJ files, vertices are numbered from 1
105           face->AddPointId( pId - 1 );
106
107         } // rof
108         cell.TakeOwnership( face );
109         out->SetCell( out->GetNumberOfCells( ), cell );
110
111       } // fi
112
113     } // fi
114
115   } // elihw
116
117   // Finish the job
118   in.close( );
119 }
120
121 #endif // __CPM__IO__WAVEFRONTOBJREADER__HXX__
122
123 // eof - $RCSfile$