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