]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Base/SkeletonWriter.hxx
...
[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   auto mIt = sk->BeginEdgesRows( );
73   auto rIt = mIt->second.begin( );
74   auto eIt = rIt->second.begin( );
75   auto path = *eIt;
76
77   // Write base information
78   std::stringstream out1, out2;
79   out1 << TSkeleton::Dimension << std::endl;
80   auto spa = path->GetSpacing( );
81   for( unsigned int d = 0; d < TSkeleton::Dimension; ++d )
82     out1 << spa[ d ] << " ";
83   out1 << std::endl;
84   auto 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   auto 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   auto end_points = sk->GetEndPoints( );
96   out1 << end_points.size( ) << std::endl;
97   for( auto point : end_points )
98   {
99     for( unsigned int d = 0; d < TSkeleton::Dimension; ++d )
100       out1 << point[ d ] << " ";
101     out1 << std::endl;
102
103   } // rof
104
105   // Bifurcations
106   auto bifurcations = sk->GetBifurcations( );
107   out1 << bifurcations.size( ) << std::endl;
108   for( auto point : bifurcations )
109   {
110     for( unsigned int d = 0; d < TSkeleton::Dimension; ++d )
111       out1 << point[ d ] << " ";
112     out1 << std::endl;
113
114   } // rof
115
116   // Write paths
117   unsigned long pathCount = 0;
118   mIt = sk->BeginEdgesRows( );
119   for( ; mIt != sk->EndEdgesRows( ); ++mIt )
120   {
121     auto rIt = mIt->second.begin( );
122     for( ; rIt != mIt->second.end( ); ++rIt )
123     {
124       auto eIt = rIt->second.begin( );
125       for( ; eIt != rIt->second.end( ); ++eIt )
126       {
127         auto path = *eIt;
128         pathCount++;
129         unsigned int size = path->GetSize( );
130         out2 << size << std::endl;
131         for( unsigned int i = 0; i < path->GetSize( ); ++i )
132         {
133           auto v = path->GetVertex( i );
134           for( unsigned int d = 0; d < TSkeleton::Dimension; ++d )
135             out2 << v[ d ] << " ";
136
137         } // rof
138         out2 << std::endl;
139
140       } // rof
141
142     } // rof
143
144   } // rof
145   out1 << pathCount << std::endl << out2.str( );
146
147   // Real write
148   std::ofstream file_stream( this->m_FileName.c_str( ), std::ofstream::binary );
149   if( !file_stream )
150     itkExceptionMacro(
151       << "Unable to write skeleton to \""
152       << this->m_FileName
153       << "\""
154       );
155   file_stream.write( out1.str( ).c_str( ), out1.str( ).size( ) );
156 }
157
158 #endif // __fpa__Base__SkeletonWriter__hxx__
159
160 // eof - $RCSfile$