]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/IO/MinimumSpanningTreeWriter.hxx
f9b8533b264691b3808dd598c7e848ea169513b2
[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 <itkMacro.h>
6
7 // -------------------------------------------------------------------------
8 template< class T >
9 void fpa::IO::MinimumSpanningTreeWriter< T >::
10 SetInput( const T* input )
11 {
12   this->itk::ProcessObject::SetNthInput( 0, const_cast< T* >( input ) );
13 }
14
15 // -------------------------------------------------------------------------
16 template< class T >
17 T* fpa::IO::MinimumSpanningTreeWriter< T >::
18 GetInput( )
19 {
20   return( dynamic_cast< T* >( this->itk::ProcessObject::GetInput( 0 ) ) );
21 }
22
23 // -------------------------------------------------------------------------
24 template< class T >
25 const T* fpa::IO::MinimumSpanningTreeWriter< T >::
26 GetInput( ) const
27 {
28   return(
29     dynamic_cast< const T* >( this->itk::ProcessObject::GetInput( 0 ) )
30     );
31 }
32
33 // -------------------------------------------------------------------------
34 template< class T >
35 void fpa::IO::MinimumSpanningTreeWriter< T >::
36 Update( )
37 {
38   T* input = 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 T >
51 fpa::IO::MinimumSpanningTreeWriter< T >::
52 MinimumSpanningTreeWriter( )
53   : Superclass( ),
54     m_FileName( "" )
55 {
56   this->SetNumberOfRequiredInputs( 1 );
57 }
58
59 // -------------------------------------------------------------------------
60 template< class T >
61 fpa::IO::MinimumSpanningTreeWriter< T >::
62 ~MinimumSpanningTreeWriter( )
63 {
64 }
65
66 // -------------------------------------------------------------------------
67 template< class T >
68 void fpa::IO::MinimumSpanningTreeWriter< T >::
69 GenerateData( )
70 {
71   const T* input = this->GetInput( );
72
73   std::ofstream out( this->m_FileName.c_str( ) );
74   if( !out )
75   {
76     itkExceptionMacro(
77       << "Error opening file to write a minimum spanning tree: \""
78       << this->m_FileName
79       << "\""
80       );
81     return;
82
83   } // fi
84
85   out << T::TVertex::Dimension << std::endl;
86
87   const typename T::TCollisions& collisions = input->GetCollisions( );
88   unsigned long nSeeds = collisions.size( );
89   out << nSeeds << std::endl;
90   for( unsigned long i = 0; i < nSeeds; ++i )
91   {
92     for( unsigned long j = 0; j < nSeeds; ++j )
93       out << collisions[ i ][ j ].second << " ";
94     out << std::endl;
95
96   } // rof
97
98   const typename T::TDecorated& real_input = input->Get( );
99   out << real_input.size( ) << std::endl;
100   for(
101     typename T::TDecorated::const_iterator iIt = real_input.begin( );
102     iIt != real_input.end( );
103     ++iIt
104     )
105   {
106     for( unsigned int d = 0; d < T::TVertex::Dimension; ++d )
107       out << iIt->first[ d ] << " ";
108     for( unsigned int d = 0; d < T::TVertex::Dimension; ++d )
109       out << iIt->second.first[ d ] << " ";
110     out << iIt->second.second << std::endl;
111
112   } // rof
113   out.close( );
114 }
115
116 #endif // __FPA__IO__MINIMUMSPANNINGTREEWRITER__HXX__
117
118 // eof - $RCSfile$