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