]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Common/Image/PolyLineParametricPathReader.hxx
...
[FrontAlgorithms.git] / lib / fpa / Common / Image / PolyLineParametricPathReader.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5 #ifndef __fpa__Common__Image__PolyLineParametricPathReader__hxx__
6 #define __fpa__Common__Image__PolyLineParametricPathReader__hxx__
7
8 // -------------------------------------------------------------------------
9 template< class _TPath >
10 _TPath* fpa::Common::Image::PolyLineParametricPathReader< _TPath >::
11 GetOutput( )
12 {
13   return(
14     itkDynamicCastInDebugMode< TPath* >( this->GetPrimaryOutput( ) )
15     );
16 }
17
18 // -------------------------------------------------------------------------
19 template< class _TPath >
20 _TPath* fpa::Common::Image::PolyLineParametricPathReader< _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::PolyLineParametricPathReader< _TPath >::
33 GraftOutput( itk::DataObject* out )
34 {
35   this->GraftNthOutput( 0, out );
36 }
37
38 // -------------------------------------------------------------------------
39 template< class _TPath >
40 void fpa::Common::Image::PolyLineParametricPathReader< _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::PolyLineParametricPathReader< _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
78 fpa::Common::Image::PolyLineParametricPathReader< _TPath >::
79 MakeOutput( itk::ProcessObject::DataObjectPointerArraySizeType i )
80 {
81   return( TPath::New( ).GetPointer( ) );
82 }
83
84 // -------------------------------------------------------------------------
85 template< class _TPath >
86 fpa::Common::Image::PolyLineParametricPathReader< _TPath >::
87 PolyLineParametricPathReader( )
88   : Superclass( )
89 {
90   typename TPath::Pointer out =
91     static_cast< TPath* >( this->MakeOutput( 0 ).GetPointer( ) );
92   this->itk::ProcessObject::SetNumberOfRequiredInputs( 0 );
93   this->itk::ProcessObject::SetNumberOfRequiredOutputs( 1 );
94   this->itk::ProcessObject::SetNthOutput( 0, out.GetPointer( ) );
95 }
96
97 // -------------------------------------------------------------------------
98 template< class _TPath >
99 fpa::Common::Image::PolyLineParametricPathReader< _TPath >::
100 ~PolyLineParametricPathReader( )
101 {
102 }
103
104 // -------------------------------------------------------------------------
105 template< class _TPath >
106 void fpa::Common::Image::PolyLineParametricPathReader< _TPath >::
107 GenerateData( )
108 {
109   std::string buffer;
110   std::ifstream file_stream( this->m_FileName.c_str( ) );
111   if( !file_stream )
112   {
113     itkExceptionMacro(
114       << "Error reading skeleton from \"" << this->m_FileName << "\""
115       );
116     return;
117
118   } // fi
119   file_stream.seekg( 0, std::ios::end );
120   buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
121   file_stream.seekg( 0, std::ios::beg );
122   buffer.assign(
123     ( std::istreambuf_iterator< char >( file_stream ) ),
124     std::istreambuf_iterator< char >( )
125     );
126   file_stream.close( );
127
128   std::istringstream in( buffer );
129   unsigned int dim;
130   in >> dim;
131   if( dim != TPath::Dimension )
132   {
133     itkExceptionMacro(
134       << "Mismatched path dimension: " << dim
135       << " != " << TPath::Dimension
136       );
137     return;
138
139   } // fi
140
141   // Read spatial parameters
142   typename TPath::TSpacing spa;
143   typename TPath::TDirection dir;
144   typename TPath::TPoint ori;
145   for( unsigned int d = 0; d < dim; ++d )
146     in >> spa[ d ];
147   for( unsigned int d = 0; d < dim; ++d )
148     for( unsigned int e = 0; e < dim; ++e )
149       in >> dir[ d ][ e ];
150   for( unsigned int d = 0; d < dim; ++d )
151     in >> ori[ d ];
152
153   // Read path
154   TPath* path = this->GetOutput( );
155   path->SetSpacing( spa );
156   path->SetOrigin( ori );
157   path->SetDirection( dir );
158
159   unsigned long pathSize;
160   in >> pathSize;
161   for( unsigned long id = 0; id < pathSize; ++id )
162   {
163     typename TPath::TIndex idx;
164     for( unsigned int d = 0; d < dim; ++d )
165       in >> idx[ d ];
166     path->AddVertex( idx );
167
168   } // rof
169 }
170
171 #endif // __fpa__Common__Image__PolyLineParametricPathReader__hxx__
172
173 // eof - $RCSfile$