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