]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/IO/MinimumSpanningTreeWriter.hxx
d4b1f120b47c2d1d397988af75f946f49273e5d5
[FrontAlgorithms.git] / lib / fpa / IO / MinimumSpanningTreeWriter.hxx
1 #ifndef __FPA__IO__MINIMUMSPANNINGTREEWRITER__HXX__
2 #define __FPA__IO__MINIMUMSPANNINGTREEWRITER__HXX__
3
4 #include <fstream>
5 #include <sstream>
6 #include <itkMacro.h>
7 #include <itkOffset.h>
8 #include <itkImageRegionConstIteratorWithIndex.h>
9
10 // -------------------------------------------------------------------------
11 template< class _TTree >
12 void fpa::IO::MinimumSpanningTreeWriter< _TTree >::
13 SetInput( const _TTree* input )
14 {
15   this->itk::ProcessObject::SetNthInput( 0, const_cast< _TTree* >( input ) );
16 }
17
18 // -------------------------------------------------------------------------
19 template< class _TTree >
20 _TTree* fpa::IO::MinimumSpanningTreeWriter< _TTree >::
21 GetInput( )
22 {
23   return(
24     dynamic_cast< _TTree* >( this->itk::ProcessObject::GetInput( 0 ) )
25     );
26 }
27
28 // -------------------------------------------------------------------------
29 template< class _TTree >
30 const _TTree* fpa::IO::MinimumSpanningTreeWriter< _TTree >::
31 GetInput( ) const
32 {
33   return(
34     dynamic_cast< const _TTree* >( this->itk::ProcessObject::GetInput( 0 ) )
35     );
36 }
37
38 // -------------------------------------------------------------------------
39 template< class _TTree >
40 void fpa::IO::MinimumSpanningTreeWriter< _TTree >::
41 Update( )
42 {
43   _TTree* input = this->GetInput( );
44   if( input != NULL )
45   {
46     input->UpdateOutputInformation( );
47     input->UpdateOutputData( );
48     this->GenerateData( );
49     this->ReleaseInputs( );
50
51   } // fi
52 }
53
54 // -------------------------------------------------------------------------
55 template< class _TTree >
56 fpa::IO::MinimumSpanningTreeWriter< _TTree >::
57 MinimumSpanningTreeWriter( )
58   : Superclass( ),
59     m_FileName( "" )
60 {
61   this->SetNumberOfRequiredInputs( 1 );
62 }
63
64 // -------------------------------------------------------------------------
65 template< class _TTree >
66 fpa::IO::MinimumSpanningTreeWriter< _TTree >::
67 ~MinimumSpanningTreeWriter( )
68 {
69 }
70
71 // -------------------------------------------------------------------------
72 template< class _TTree >
73 void fpa::IO::MinimumSpanningTreeWriter< _TTree >::
74 GenerateData( )
75 {
76   typedef itk::Offset< _TTree::ImageDimension > _TOffset;
77   _TOffset zero_off;
78   zero_off.Fill( 0 );
79   const _TTree* input = this->GetInput( );
80
81   // Create buffer
82   std::stringstream out;
83
84   // Get tree properties
85   unsigned int dim = _TTree::ImageDimension;
86   auto lr = input->GetLargestPossibleRegion( );
87   auto rr = input->GetRequestedRegion( );
88   auto br = input->GetBufferedRegion( );
89   auto ori = input->GetOrigin( );
90   auto spc = input->GetSpacing( );
91   auto dir = input->GetDirection( );
92
93   // Write tree dimension
94   out << dim << std::endl;
95   out << ( ( input->GetFillNodeQueue( ) )? 1: 0 ) << std::endl;
96
97   // Write tree regions
98   out << lr.GetIndex( )[ 0 ] << " " << lr.GetSize( )[ 0 ];
99   for( unsigned int d = 1; d < dim; ++d )
100     out << " " << lr.GetIndex( )[ d ] << " " << lr.GetSize( )[ d ];
101   out << std::endl;
102   out << rr.GetIndex( )[ 0 ] << " " << rr.GetSize( )[ 0 ];
103   for( unsigned int d = 1; d < dim; ++d )
104     out << " " << rr.GetIndex( )[ d ] << " " << rr.GetSize( )[ d ];
105   out << std::endl;
106   out << br.GetIndex( )[ 0 ] << " " << br.GetSize( )[ 0 ];
107   for( unsigned int d = 1; d < dim; ++d )
108     out << " " << br.GetIndex( )[ d ] << " " << br.GetSize( )[ d ];
109   out << std::endl;
110
111   // Write spatial properties
112   out << ori[ 0 ];
113   for( unsigned int d = 1; d < dim; ++d )
114     out << " " << ori[ d ];
115   out << std::endl;
116   out << spc[ 0 ];
117   for( unsigned int d = 1; d < dim; ++d )
118     out << " " << spc[ d ];
119   out << std::endl;
120   out << dir[ 0 ][ 0 ];
121   for( unsigned int d = 0; d < dim; ++d )
122     for( unsigned int e = 0; e < dim; ++e )
123       if( d != 0 || e != 0 )
124         out << " " << dir[ d ][ e ];
125   out << std::endl;
126
127   // Write collisions
128   auto& coll = input->GetCollisions( );
129   out << coll.size( ) << std::endl;
130   for( unsigned long i = 0; i < coll.size( ); ++i )
131   {
132     out << coll[ i ].size( );
133     for( unsigned long j = 0; j < coll[ i ].size( ); ++j )
134     {
135       for( unsigned int d = 0; d < dim; ++d )
136         out << " " << coll[ i ][ j ].first[ d ];
137       out << " " << coll[ i ][ j ].second << std::endl;
138
139     } // rof
140
141   } // rof
142
143   // Write vertices
144   itk::ImageRegionConstIteratorWithIndex< _TTree > vIt( input, rr );
145   for( vIt.GoToBegin( ); !vIt.IsAtEnd( ); ++vIt )
146   {
147     if(
148       vIt.Get( ).Parent != zero_off ||
149       vIt.Get( ).GlobalCost > double( 0 )
150       )
151     {
152       out << vIt.Get( ).FrontId << " " << vIt.Get( ).GlobalCost;
153       for( unsigned int d = 0; d < dim; ++d )
154         out << " " << vIt.GetIndex( )[ d ];
155       for( unsigned int d = 0; d < dim; ++d )
156         out << " " << vIt.Get( ).Parent[ d ];
157       out << std::endl;
158
159     } // fi
160
161   } // rof
162
163   // Real file write
164   std::ofstream file(
165     this->m_FileName.c_str( ), std::ios::binary | std::ios::trunc
166     );
167   if( !file )
168   {
169     itkExceptionMacro(
170       << "Error opening file to write a minimum spanning tree: \""
171       << this->m_FileName
172       << "\""
173       );
174     return;
175
176   } // fi
177   file.write( out.str( ).c_str( ), out.str( ).size( ) );
178   file.close( );
179 }
180
181 #endif // __FPA__IO__MINIMUMSPANNINGTREEWRITER__HXX__
182
183 // eof - $RCSfile$