]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/SkeletonReader.hxx
yet another refactoring
[cpPlugins.git] / lib / cpExtensions / Algorithms / SkeletonReader.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __cpExtensions__Algorithms__SkeletonReader__hxx__
6 #define __cpExtensions__Algorithms__SkeletonReader__hxx__
7
8 // -------------------------------------------------------------------------
9 template< class _TSkeleton >
10 _TSkeleton* cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
11 GetOutput( )
12 {
13   return(
14     itkDynamicCastInDebugMode< TSkeleton* >( this->GetPrimaryOutput( ) )
15     );
16 }
17
18 // -------------------------------------------------------------------------
19 template< class _TSkeleton >
20 _TSkeleton* cpExtensions::Algorithms::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 cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
33 GraftOutput( itk::DataObject* out )
34 {
35   this->GraftNthOutput( 0, out );
36 }
37
38 // -------------------------------------------------------------------------
39 template< class _TSkeleton >
40 void cpExtensions::Algorithms::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 cpExtensions::Algorithms::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 cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
79 MakeOutput( itk::ProcessObject::DataObjectPointerArraySizeType i )
80 {
81   return( TSkeleton::New( ).GetPointer( ) );
82 }
83
84 // -------------------------------------------------------------------------
85 template< class _TSkeleton >
86 cpExtensions::Algorithms::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 cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
100 ~SkeletonReader( )
101 {
102 }
103
104 // -------------------------------------------------------------------------
105 template< class _TSkeleton >
106 void cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
107 GenerateData( )
108 {
109   typedef typename TSkeleton::TPath         _TPath;
110   typedef typename _TPath::TSpacing         _TSpacing;
111   typedef typename _TPath::TPoint           _TPoint;
112   typedef typename _TPath::TDirection       _TDirection;
113   typedef typename _TPath::TContinuousIndex _TContinuousIndex;
114
115   std::string buffer;
116   /* TODO
117      if( !( cpExtensions::Read( buffer, this->m_FileName ) ) )
118      {
119      itkExceptionMacro(
120      << "Error reading skeleton from \"" << this->m_FileName << "\""
121      );
122      return;
123
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 // __cpExtensions__Algorithms__SkeletonReader__hxx__
175
176 // eof - $RCSfile$