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__
8 // -------------------------------------------------------------------------
9 template< class _TSkeleton >
10 _TSkeleton* fpa::Common::Image::SkeletonReader< _TSkeleton >::
14 itkDynamicCastInDebugMode< TSkeleton* >( this->GetPrimaryOutput( ) )
18 // -------------------------------------------------------------------------
19 template< class _TSkeleton >
20 _TSkeleton* fpa::Common::Image::SkeletonReader< _TSkeleton >::
21 GetOutput( unsigned int i )
24 itkDynamicCastInDebugMode< TSkeleton* >(
25 this->itk::ProcessObject::GetOutput( i )
30 // -------------------------------------------------------------------------
31 template< class _TSkeleton >
32 void fpa::Common::Image::SkeletonReader< _TSkeleton >::
33 GraftOutput( itk::DataObject* out )
35 this->GraftNthOutput( 0, out );
38 // -------------------------------------------------------------------------
39 template< class _TSkeleton >
40 void fpa::Common::Image::SkeletonReader< _TSkeleton >::
42 const typename Superclass::DataObjectIdentifierType& key,
49 << "Requested to graft output that is a NULL pointer"
53 itk::DataObject* output = this->itk::ProcessObject::GetOutput( key );
57 // -------------------------------------------------------------------------
58 template< class _TSkeleton >
59 void fpa::Common::Image::SkeletonReader< _TSkeleton >::
60 GraftNthOutput( unsigned int i, itk::DataObject* out )
62 if( i >= this->GetNumberOfIndexedOutputs( ) )
65 << "Requested to graft output " << i
66 << " but this filter only has "
67 << this->GetNumberOfIndexedOutputs( )
68 << " indexed Outputs."
72 this->GraftOutput( this->MakeNameFromOutputIndex( i ), out );
75 // -------------------------------------------------------------------------
76 template< class _TSkeleton >
77 itk::DataObject::Pointer
78 fpa::Common::Image::SkeletonReader< _TSkeleton >::
79 MakeOutput( itk::ProcessObject::DataObjectPointerArraySizeType i )
81 return( TSkeleton::New( ).GetPointer( ) );
84 // -------------------------------------------------------------------------
85 template< class _TSkeleton >
86 fpa::Common::Image::SkeletonReader< _TSkeleton >::
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( ) );
97 // -------------------------------------------------------------------------
98 template< class _TSkeleton >
99 fpa::Common::Image::SkeletonReader< _TSkeleton >::
104 // -------------------------------------------------------------------------
105 template< class _TSkeleton >
106 void fpa::Common::Image::SkeletonReader< _TSkeleton >::
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;
116 std::ifstream file_stream( this->m_FileName.c_str( ) );
120 << "Error reading skeleton from \"" << this->m_FileName << "\""
125 file_stream.seekg( 0, std::ios::end );
126 buffer.reserve( ( unsigned int )( file_stream.tellg( ) ) );
127 file_stream.seekg( 0, std::ios::beg );
129 ( std::istreambuf_iterator< char >( file_stream ) ),
130 std::istreambuf_iterator< char >( )
132 file_stream.close( );
134 std::istringstream in( buffer );
137 if( dim != TSkeleton::Dimension )
140 << "Mismatched skeletons dimension: " << dim
141 << " != " << TSkeleton::Dimension
147 // Read spatial parameters
151 for( unsigned int d = 0; d < dim; ++d )
153 for( unsigned int d = 0; d < dim; ++d )
154 for( unsigned int e = 0; e < dim; ++e )
156 for( unsigned int d = 0; d < dim; ++d )
159 // Read end-points, just to ignore
162 for( unsigned int i = 0; i < n; ++i )
165 for( unsigned int d = 0; d < dim; ++d )
170 // Read bifurcations, just to ignore
172 for( unsigned int i = 0; i < n; ++i )
175 for( unsigned int d = 0; d < dim; ++d )
181 TSkeleton* out = this->GetOutput( );
184 for( unsigned int p = 0; p < nPaths; ++p )
186 typename _TPath::Pointer path = _TPath::New( );
187 path->SetSpacing( spa );
188 path->SetOrigin( ori );
189 path->SetDirection( dir );
191 unsigned long pathSize;
193 for( unsigned long id = 0; id < pathSize; ++id )
196 for( unsigned int d = 0; d < dim; ++d )
198 path->AddVertex( idx );
201 out->AddBranch( path );
206 #endif // __fpa__Common__Image__SkeletonReader__hxx__