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