]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Common/Image/SkeletonWriter.hxx
4f392ed3018601c9bdc84457e26f2180c5d4aa9c
[FrontAlgorithms.git] / lib / fpa / Common / Image / SkeletonWriter.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5 #ifndef __fpa__Common__Image__SkeletonWriter__hxx__
6 #define __fpa__Common__Image__SkeletonWriter__hxx__
7
8 #include <fstream>
9
10 // -------------------------------------------------------------------------
11 template< class _TSkeleton >
12 const _TSkeleton* fpa::Common::Image::SkeletonWriter< _TSkeleton >::
13 GetInput( ) const
14 {
15   return(
16     dynamic_cast< const TSkeleton* >(
17       this->itk::ProcessObject::GetInput( 0 )
18       )
19     );
20 }
21
22 // -------------------------------------------------------------------------
23 template< class _TSkeleton >
24 void fpa::Common::Image::SkeletonWriter< _TSkeleton >::
25 SetInput( const _TSkeleton* skeleton )
26 {
27   this->itk::ProcessObject::SetNthInput(
28     0, const_cast< TSkeleton* >( skeleton )
29     );
30 }
31
32 // -------------------------------------------------------------------------
33 template< class _TSkeleton >
34 void fpa::Common::Image::SkeletonWriter< _TSkeleton >::
35 Update( )
36 {
37   TSkeleton* input = const_cast< TSkeleton* >( this->GetInput( ) );
38   if( input != NULL )
39   {
40     input->UpdateOutputInformation( );
41     input->UpdateOutputData( );
42     this->GenerateData( );
43     this->ReleaseInputs( );
44
45   } // fi
46 }
47
48 // -------------------------------------------------------------------------
49 template< class _TSkeleton >
50 fpa::Common::Image::SkeletonWriter< _TSkeleton >::
51 SkeletonWriter( )
52   : Superclass( ),
53     m_FileName( "" )
54 {
55   this->SetNumberOfRequiredInputs( 1 );
56 }
57
58 // -------------------------------------------------------------------------
59 template< class _TSkeleton >
60 fpa::Common::Image::SkeletonWriter< _TSkeleton >::
61 ~SkeletonWriter( )
62 {
63 }
64
65 // -------------------------------------------------------------------------
66 template< class _TSkeleton >
67 void fpa::Common::Image::SkeletonWriter< _TSkeleton >::
68 GenerateData( )
69 {
70   const TSkeleton* sk = this->GetInput( );
71   typename TMatrix::const_iterator mIt = sk->BeginEdgesRows( );
72   typename TMatrixRow::const_iterator rIt = mIt->second.begin( );
73   typename TEdges::const_iterator eIt = rIt->second.begin( );
74   const TPath* path = *eIt;
75
76   // Write base information
77   std::stringstream out1, out2;
78   out1 << TSkeleton::Dimension << std::endl;
79   typename TPath::TSpacing spa = path->GetSpacing( );
80   for( unsigned int d = 0; d < TSkeleton::Dimension; ++d )
81     out1 << spa[ d ] << " ";
82   out1 << std::endl;
83   typename TPath::TDirection dir = path->GetDirection( );
84   for( unsigned int d = 0; d < TSkeleton::Dimension; ++d )
85     for( unsigned int e = 0; e < TSkeleton::Dimension; ++e )
86       out1 << dir[ d ][ e ] << " ";
87   out1 << std::endl;
88   typename TPath::TPoint ori = path->GetOrigin( );
89   for( unsigned int d = 0; d < TSkeleton::Dimension; ++d )
90     out1 << ori[ d ] << " ";
91   out1 << std::endl;
92
93   // End points
94   std::vector< TVertex > end_points = sk->GetEndPoints( );
95   out1 << end_points.size( ) << std::endl;
96   typename std::vector< TVertex >::const_iterator epIt = end_points.begin( );
97   for( ; epIt != end_points.end( ); ++epIt )
98   {
99     for( unsigned int d = 0; d < TSkeleton::Dimension; ++d )
100       out1 << ( *epIt )[ d ] << " ";
101     out1 << std::endl;
102
103   } // rof
104
105   // Bifurcations
106   std::vector< TVertex > bifurcations = sk->GetBifurcations( );
107   out1 << bifurcations.size( ) << std::endl;
108   typename std::vector< TVertex >::const_iterator bIt = bifurcations.begin( );
109   for( ; bIt != bifurcations.end( ); ++bIt )
110   {
111     for( unsigned int d = 0; d < TSkeleton::Dimension; ++d )
112       out1 << ( *bIt )[ d ] << " ";
113     out1 << std::endl;
114
115   } // rof
116
117   // Write paths
118   unsigned long pathCount = 0;
119   mIt = sk->BeginEdgesRows( );
120   for( ; mIt != sk->EndEdgesRows( ); ++mIt )
121   {
122     typename TMatrixRow::const_iterator rIt = mIt->second.begin( );
123     for( ; rIt != mIt->second.end( ); ++rIt )
124     {
125       typename TEdges::const_iterator eIt = rIt->second.begin( );
126       for( ; eIt != rIt->second.end( ); ++eIt )
127       {
128         TPath* path = *eIt;
129         pathCount++;
130         unsigned int size = path->GetSize( );
131         out2 << size << std::endl;
132         for( unsigned int i = 0; i < path->GetSize( ); ++i )
133         {
134           TVertex v = path->GetVertex( i );
135           for( unsigned int d = 0; d < TSkeleton::Dimension; ++d )
136             out2 << v[ d ] << " ";
137
138         } // rof
139         out2 << std::endl;
140
141       } // rof
142
143     } // rof
144
145   } // rof
146   out1 << pathCount << std::endl << out2.str( );
147
148   // Real write
149   std::ofstream file_stream( this->m_FileName.c_str( ), std::ofstream::binary );
150   if( !file_stream )
151     itkExceptionMacro(
152       << "Unable to write skeleton to \""
153       << this->m_FileName
154       << "\""
155       );
156   file_stream.write( out1.str( ).c_str( ), out1.str( ).size( ) );
157 }
158
159 #endif // __fpa__Common__Image__SkeletonWriter__hxx__
160
161 // eof - $RCSfile$