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