]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Common/Image/PathReader.hxx
...
[FrontAlgorithms.git] / lib / fpa / Common / Image / PathReader.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5 #ifndef __fpa__Common__Image__PathReader__hxx__
6 #define __fpa__Common__Image__PathReader__hxx__
7
8 // -------------------------------------------------------------------------
9 template< class _TPath >
10 _TPath* fpa::Common::Image::PathReader< _TPath >::
11 GetOutput( )
12 {
13   return(
14     itkDynamicCastInDebugMode< TPath* >( this->GetPrimaryOutput( ) )
15     );
16 }
17
18 // -------------------------------------------------------------------------
19 template< class _TPath >
20 _TPath* fpa::Common::Image::PathReader< _TPath >::
21 GetOutput( unsigned int i )
22 {
23   return(
24     itkDynamicCastInDebugMode< TPath* >(
25       this->itk::ProcessObject::GetOutput( i )
26       )
27     );
28 }
29
30 // -------------------------------------------------------------------------
31 template< class _TPath >
32 void fpa::Common::Image::PathReader< _TPath >::
33 GraftOutput( itk::DataObject* out )
34 {
35   this->GraftNthOutput( 0, out );
36 }
37
38 // -------------------------------------------------------------------------
39 template< class _TPath >
40 void fpa::Common::Image::PathReader< _TPath >::
41 GraftOutput(
42   const typename Superclass::DataObjectIdentifierType& key,
43   itk::DataObject* out
44   )
45 {
46   if( out == NULL )
47   {
48     itkExceptionMacro(
49       << "Requested to graft output that is a NULL pointer"
50       );
51
52   } // fi
53   itk::DataObject* output = this->itk::ProcessObject::GetOutput( key );
54   output->Graft( out );
55 }
56
57 // -------------------------------------------------------------------------
58 template< class _TPath >
59 void fpa::Common::Image::PathReader< _TPath >::
60 GraftNthOutput( unsigned int i, itk::DataObject* out )
61 {
62   if( i >= this->GetNumberOfIndexedOutputs( ) )
63   {
64     itkExceptionMacro(
65       << "Requested to graft output " << i
66       << " but this filter only has "
67       << this->GetNumberOfIndexedOutputs( )
68       << " indexed Outputs."
69       );
70
71   } // fi
72   this->GraftOutput( this->MakeNameFromOutputIndex( i ), out );
73 }
74
75 // -------------------------------------------------------------------------
76 template< class _TPath >
77 itk::DataObject::Pointer fpa::Common::Image::PathReader< _TPath >::
78 MakeOutput( itk::ProcessObject::DataObjectPointerArraySizeType i )
79 {
80   return( TPath::New( ).GetPointer( ) );
81 }
82
83 // -------------------------------------------------------------------------
84 template< class _TPath >
85 fpa::Common::Image::PathReader< _TPath >::
86 PathReader( )
87   : Superclass( )
88 {
89   typename TPath::Pointer out =
90     static_cast< TPath* >( this->MakeOutput( 0 ).GetPointer( ) );
91   this->itk::ProcessObject::SetNumberOfRequiredInputs( 0 );
92   this->itk::ProcessObject::SetNumberOfRequiredOutputs( 1 );
93   this->itk::ProcessObject::SetNthOutput( 0, out.GetPointer( ) );
94 }
95
96 // -------------------------------------------------------------------------
97 template< class _TPath >
98 fpa::Common::Image::PathReader< _TPath >::
99 ~PathReader( )
100 {
101 }
102
103 // -------------------------------------------------------------------------
104 template< class _TPath >
105 void fpa::Common::Image::PathReader< _TPath >::
106 GenerateData( )
107 {
108   std::string buffer;
109   std::ifstream file_stream( this->m_FileName.c_str( ) );
110   if( !file_stream )
111   {
112     itkExceptionMacro(
113       << "Error reading skeleton from \"" << this->m_FileName << "\""
114       );
115     return;
116
117   } // fi
118   file_stream.seekg( 0, std::ios::end );
119   buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
120   file_stream.seekg( 0, std::ios::beg );
121   buffer.assign(
122     ( std::istreambuf_iterator< char >( file_stream ) ),
123     std::istreambuf_iterator< char >( )
124     );
125   file_stream.close( );
126
127   std::istringstream in( buffer );
128   unsigned int dim;
129   in >> dim;
130   if( dim != TPath::Dimension )
131   {
132     itkExceptionMacro(
133       << "Mismatched path dimension: " << dim
134       << " != " << TPath::Dimension
135       );
136     return;
137
138   } // fi
139
140   // Read spatial parameters
141   typename TPath::TSpacing spa;
142   typename TPath::TDirection dir;
143   typename TPath::TPoint ori;
144   for( unsigned int d = 0; d < dim; ++d )
145     in >> spa[ d ];
146   for( unsigned int d = 0; d < dim; ++d )
147     for( unsigned int e = 0; e < dim; ++e )
148       in >> dir[ d ][ e ];
149   for( unsigned int d = 0; d < dim; ++d )
150     in >> ori[ d ];
151
152   // Read path
153   TPath* path = this->GetOutput( );
154   path->SetSpacing( spa );
155   path->SetOrigin( ori );
156   path->SetDirection( dir );
157
158   unsigned long pathSize;
159   in >> pathSize;
160   for( unsigned long id = 0; id < pathSize; ++id )
161   {
162     typename TPath::TIndex idx;
163     for( unsigned int d = 0; d < dim; ++d )
164       in >> idx[ d ];
165     path->AddVertex( idx );
166
167   } // rof
168 }
169
170 #endif // __fpa__Common__Image__PathReader__hxx__
171
172 // eof - $RCSfile$