]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/IO/MatrixValuesContainerReader.hxx
Skeletonization ready to use
[FrontAlgorithms.git] / lib / fpa / IO / MatrixValuesContainerReader.hxx
1 #ifndef __FPA__IO__MATRIXVALUESCONTAINERREADER__HXX__
2 #define __FPA__IO__MATRIXVALUESCONTAINERREADER__HXX__
3
4 #include <fstream>
5
6 // -------------------------------------------------------------------------
7 template< class T >
8 T* fpa::IO::MatrixValuesContainerReader< T >::
9 GetOutput( )
10 {
11   return( itkDynamicCastInDebugMode< T* >( this->GetPrimaryOutput( ) ) );
12 }
13
14 // -------------------------------------------------------------------------
15 template< class T >
16 void fpa::IO::MatrixValuesContainerReader< T >::
17 Update( )
18 {
19   this->GenerateData( );
20 }
21
22 // -------------------------------------------------------------------------
23 template< class T >
24 fpa::IO::MatrixValuesContainerReader< T >::
25 MatrixValuesContainerReader( )
26   : Superclass( ),
27     m_FileName( "" )
28 {
29   this->itk::ProcessObject::SetNumberOfRequiredOutputs( 1 );
30   this->itk::ProcessObject::SetNthOutput( 0, T::New( ) );
31 }
32
33 // -------------------------------------------------------------------------
34 template< class T >
35 fpa::IO::MatrixValuesContainerReader< T >::
36 ~MatrixValuesContainerReader( )
37 {
38 }
39
40 // -------------------------------------------------------------------------
41 template< class T >
42 void fpa::IO::MatrixValuesContainerReader< T >::
43 GenerateData( )
44 {
45   T* output = this->GetOutput( );
46   output->Clear( );
47
48   std::ifstream in( this->m_FileName.c_str( ) );
49   if( !in )
50   {
51     itkExceptionMacro(
52       << "Error opening file to read a minimum spanning tree: \""
53       << this->m_FileName
54       << "\""
55       );
56     return;
57
58   } // fi
59
60   unsigned int dim;
61   unsigned long nTuples;
62   in >> dim >> nTuples;
63   this->m_NumberOfLabels = 0;
64   for( unsigned long tId = 0; tId < nTuples; ++tId )
65   {
66     unsigned long nCouples;
67     in >> nCouples;
68     typename T::TIndex v0;
69     for( unsigned int d = 0; d < dim; ++d )
70     {
71       long val;
72       in >> val;
73       if( d < T::TIndex::Dimension )
74         v0[ d ] = val;
75     
76     } // rof
77
78     for( unsigned long cId = 0; cId < nCouples; ++cId )
79     {
80       typename T::TIndex v1;
81       for( unsigned int d = 0; d < dim; ++d )
82       {
83         long val;
84         in >> val;
85         if( d < T::TIndex::Dimension )
86           v1[ d ] = val;
87     
88       } // rof
89       typename T::TValue val;
90       in >> val;
91       output->SetValue( v0, v1, val );
92       if( val > this->m_NumberOfLabels )
93         this->m_NumberOfLabels = val;
94
95     } // rof
96
97   } // rof
98   in.close( );
99 }
100
101 #endif // __FPA__IO__MATRIXVALUESCONTAINERREADER__HXX__
102
103 // eof - $RCSfile$