]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Base/SkeletonReader.hxx
...
[FrontAlgorithms.git] / lib / fpa / Base / SkeletonReader.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #ifndef __fpa__Base__SkeletonReader__hxx__
7 #define __fpa__Base__SkeletonReader__hxx__
8
9 // -------------------------------------------------------------------------
10 template< class _TSkeleton >
11 _TSkeleton* fpa::Base::SkeletonReader< _TSkeleton >::
12 GetOutput( )
13 {
14   return(
15     itkDynamicCastInDebugMode< TSkeleton* >( this->GetPrimaryOutput( ) )
16     );
17 }
18
19 // -------------------------------------------------------------------------
20 template< class _TSkeleton >
21 _TSkeleton* fpa::Base::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::Base::SkeletonReader< _TSkeleton >::
34 GraftOutput( itk::DataObject* out )
35 {
36   this->GraftNthOutput( 0, out );
37 }
38
39 // -------------------------------------------------------------------------
40 template< class _TSkeleton >
41 void fpa::Base::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::Base::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::Base::SkeletonReader< _TSkeleton >::
80 MakeOutput( itk::ProcessObject::DataObjectPointerArraySizeType i )
81 {
82   return( TSkeleton::New( ).GetPointer( ) );
83 }
84
85 // -------------------------------------------------------------------------
86 template< class _TSkeleton >
87 fpa::Base::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::Base::SkeletonReader< _TSkeleton >::
101 ~SkeletonReader( )
102 {
103 }
104
105 // -------------------------------------------------------------------------
106 template< class _TSkeleton >
107 void fpa::Base::SkeletonReader< _TSkeleton >::
108 GenerateData( )
109 {
110   typedef typename TSkeleton::TPath         _TPath;
111   typedef typename _TPath::TSpacing         _TSpacing;
112   typedef typename _TPath::TPoint           _TPoint;
113   typedef typename _TPath::TDirection       _TDirection;
114   typedef typename _TPath::TContinuousIndex _TContinuousIndex;
115
116   std::string buffer;
117   /* TODO
118      if( !( fpa::Read( buffer, this->m_FileName ) ) )
119      {
120      itkExceptionMacro(
121      << "Error reading skeleton from \"" << this->m_FileName << "\""
122      );
123      return;
124      } // fi
125   */
126
127   std::istringstream in( buffer );
128   unsigned int dim;
129   in >> dim;
130   if( dim != TSkeleton::Dimension )
131   {
132     itkExceptionMacro(
133       << "Mismatched skeletons dimension: " << dim
134       << " != " << TSkeleton::Dimension
135       );
136     return;
137
138   } // fi
139
140   TSkeleton* out = this->GetOutput( );
141   unsigned long size;
142   in >> size;
143   while( size > 0 )
144   {
145     _TSpacing spa;
146     _TPoint ori;
147     _TDirection dir;
148     for( unsigned int d = 0; d < dim; ++d )
149       in >> spa[ d ];
150     for( unsigned int d = 0; d < dim; ++d )
151       in >> ori[ d ];
152     for( unsigned int d = 0; d < dim; ++d )
153       for( unsigned int e = 0; e < dim; ++e )
154         in >> dir[ d ][ e ];
155
156     typename _TPath::Pointer path = _TPath::New( );
157     path->SetSpacing( spa );
158     path->SetOrigin( ori );
159     path->SetDirection( dir );
160     for( unsigned long s = 0; s < size; ++s )
161     {
162       _TContinuousIndex idx;
163       for( unsigned int d = 0; d < dim; ++d )
164         in >> idx[ d ];
165       path->AddVertex( idx );
166
167     } // rof
168     out->AddBranch( path );
169     in >> size;
170
171   } // elihw
172 }
173
174 #endif // __fpa__Base__SkeletonReader__hxx__
175
176 // eof - $RCSfile$