]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/PolyLineParametricPathWriter.hxx
yet another refactoring
[cpPlugins.git] / lib / cpExtensions / Algorithms / PolyLineParametricPathWriter.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __cpExtensions__Algorithms__PolyLineParametricPathWriter__hxx__
6 #define __cpExtensions__Algorithms__PolyLineParametricPathWriter__hxx__
7
8 #include <sstream>
9
10 // -------------------------------------------------------------------------
11 template< class _TPolyLine >
12 void cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
13 SetInput( const _TPolyLine* input )
14 {
15   this->itk::ProcessObject::SetNthInput(
16     0, const_cast< _TPolyLine* >( input )
17     );
18 }
19
20 // -------------------------------------------------------------------------
21 template< class _TPolyLine >
22 const _TPolyLine*
23 cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
24 GetInput( )
25 {
26   return(
27     dynamic_cast< const _TPolyLine* >(
28       this->itk::ProcessObject::GetInput( 0 )
29       )
30     );
31 }
32
33 // -------------------------------------------------------------------------
34 template< class _TPolyLine >
35 void cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
36 Update( )
37 {
38   _TPolyLine* input = const_cast< _TPolyLine* >( 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 _TPolyLine >
51 cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
52 PolyLineParametricPathWriter( )
53   : Superclass( ),
54     m_FileName( "" ),
55     m_NumberOfPoints( 100 )
56 {
57   this->SetNumberOfRequiredInputs( 1 );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class _TPolyLine >
62 cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
63 ~PolyLineParametricPathWriter( )
64 {
65 }
66
67 // -------------------------------------------------------------------------
68 template< class _TPolyLine >
69 void cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine >::
70 GenerateData( )
71 {
72   typedef typename _TPolyLine::TContinuousIndex _TContinuousIndex;
73
74   // "Serialize" data
75   const _TPolyLine* input = this->GetInput( );
76   unsigned int dim = _TPolyLine::PathDimension;
77   std::stringstream buffer;
78   long step = input->GetSize( ) / this->m_NumberOfPoints;
79
80   for( unsigned long i = 0; i < input->GetSize( ); i += step )
81   {
82     _TContinuousIndex idx;
83     idx.Fill( 0 );
84     int c = 0;
85     for( long j = -step; j <= step; ++j )
86     {
87       long k = i + j;
88       if( k >= 0 && k < input->GetSize( ) )
89       {
90         _TContinuousIndex kdx = input->GetContinuousVertex( k );
91         for( unsigned int d = 0; d < dim; ++d )
92           idx[ d ] += kdx[ d ];
93         c++;
94
95       } // fi
96
97     } // rof
98     if( c != 0 )
99       for( unsigned int d = 0; d < dim; ++d )
100         idx[ d ] /= c;
101
102     buffer << idx[ 0 ];
103     for( unsigned int d = 1; d < dim; ++d )
104       buffer << " " << idx[ d ];
105     buffer << std::endl;
106
107   } // rof
108
109   // Real write
110   std::ofstream file_stream(
111     this->m_FileName.c_str( ), std::ofstream::binary
112     );
113   if( !file_stream )
114   {
115     itkExceptionMacro(
116       << "Could not open file \"" << this->m_FileName << "\" to write a "
117       << "cpExtensions::DataStructures::PolyLineParametricPath< " << dim
118       << " > object."
119       );
120     return;
121
122   } // fi
123   file_stream.write( buffer.str( ).c_str( ), buffer.str( ).size( ) );
124 }
125
126 #endif // __cpExtensions__Algorithms__PolyLineParametricPathWriter__hxx__
127
128 // eof - $RCSfile$