]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/SkeletonReader.hxx
744bd043a1d15f38e0aa40ee5a24bb3d3581161a
[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 #include <cpExtensions/Utility.h>
9
10 // -------------------------------------------------------------------------
11 template< class _TSkeleton >
12 _TSkeleton* cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
13 GetOutput( )
14 {
15   return(
16     itkDynamicCastInDebugMode< TSkeleton* >( this->GetPrimaryOutput( ) )
17     );
18 }
19
20 // -------------------------------------------------------------------------
21 template< class _TSkeleton >
22 _TSkeleton* cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
23 GetOutput( unsigned int i )
24 {
25   return(
26     itkDynamicCastInDebugMode< TSkeleton* >(
27       this->itk::ProcessObject::GetOutput( i )
28       )
29     );
30 }
31
32 // -------------------------------------------------------------------------
33 template< class _TSkeleton >
34 void cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
35 GraftOutput( itk::DataObject* out )
36 {
37   this->GraftNthOutput( 0, out );
38 }
39
40 // -------------------------------------------------------------------------
41 template< class _TSkeleton >
42 void cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
43 GraftOutput(
44   const typename Superclass::DataObjectIdentifierType& key,
45   itk::DataObject* out
46   )
47 {
48   if( out == NULL )
49   {
50     itkExceptionMacro(
51       << "Requested to graft output that is a NULL pointer"
52       );
53
54   } // fi
55   itk::DataObject* output = this->itk::ProcessObject::GetOutput( key );
56   output->Graft( out );
57 }
58
59 // -------------------------------------------------------------------------
60 template< class _TSkeleton >
61 void cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
62 GraftNthOutput( unsigned int i, itk::DataObject* out )
63 {
64   if( i >= this->GetNumberOfIndexedOutputs( ) )
65   {
66     itkExceptionMacro(
67       << "Requested to graft output " << i
68       << " but this filter only has "
69       << this->GetNumberOfIndexedOutputs( )
70       << " indexed Outputs."
71       );
72
73   } // fi
74   this->GraftOutput( this->MakeNameFromOutputIndex( i ), out );
75 }
76
77 // -------------------------------------------------------------------------
78 template< class _TSkeleton >
79 itk::DataObject::Pointer
80 cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
81 MakeOutput( itk::ProcessObject::DataObjectPointerArraySizeType i )
82 {
83   return( TSkeleton::New( ).GetPointer( ) );
84 }
85
86 // -------------------------------------------------------------------------
87 template< class _TSkeleton >
88 cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
89 SkeletonReader( )
90   : Superclass( )
91 {
92   typename TSkeleton::Pointer out =
93     static_cast< TSkeleton* >( this->MakeOutput( 0 ).GetPointer( ) );
94   this->itk::ProcessObject::SetNumberOfRequiredInputs( 0 );
95   this->itk::ProcessObject::SetNumberOfRequiredOutputs( 1 );
96   this->itk::ProcessObject::SetNthOutput( 0, out.GetPointer( ) );
97 }
98
99 // -------------------------------------------------------------------------
100 template< class _TSkeleton >
101 cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
102 ~SkeletonReader( )
103 {
104 }
105
106 // -------------------------------------------------------------------------
107 template< class _TSkeleton >
108 void cpExtensions::Algorithms::SkeletonReader< _TSkeleton >::
109 GenerateData( )
110 {
111   typedef typename TSkeleton::TPath         _TPath;
112   typedef typename _TPath::TSpacing         _TSpacing;
113   typedef typename _TPath::TPoint           _TPoint;
114   typedef typename _TPath::TDirection       _TDirection;
115   typedef typename _TPath::TContinuousIndex _TContinuousIndex;
116
117   std::string buffer;
118   if( !( cpExtensions::Read( buffer, this->m_FileName ) ) )
119   {
120     itkExceptionMacro(
121       << "Error reading skeleton from \"" << this->m_FileName << "\""
122       );
123     return;
124
125   } // fi
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$