]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/PolyLineParametricPathWriter.hxx
d71c6e3aa640eb7d2d118b967883edac9ff6c4ac
[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, class _TImage >
12 void cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine, _TImage >::
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, class _TImage >
22 const _TPolyLine*
23 cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine, _TImage >::
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, class _TImage >
35 void cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine, _TImage >::
36 SetImage( const _TImage* image )
37 {
38   this->itk::ProcessObject::SetNthInput(
39     1, const_cast< _TImage* >( image )
40     );
41 }
42
43 // -------------------------------------------------------------------------
44 template< class _TPolyLine, class _TImage >
45 const _TImage* cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine, _TImage >::
46 GetImage( )
47 {
48   return(
49     dynamic_cast< const _TImage* >(
50       this->itk::ProcessObject::GetInput( 1 )
51       )
52     );
53 }
54
55 // -------------------------------------------------------------------------
56 template< class _TPolyLine, class _TImage >
57 void cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine, _TImage >::
58 Update( )
59 {
60   _TPolyLine* input = const_cast< _TPolyLine* >( this->GetInput( ) );
61   if( input != NULL )
62   {
63     input->UpdateOutputInformation( );
64     input->UpdateOutputData( );
65     this->GenerateData( );
66     this->ReleaseInputs( );
67
68   } // fi
69 }
70
71 // -------------------------------------------------------------------------
72 template< class _TPolyLine, class _TImage >
73 cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine, _TImage >::
74 PolyLineParametricPathWriter( )
75   : Superclass( ),
76     m_FileName( "" ),
77     m_NumberOfPoints( 100 )
78 {
79   this->SetNumberOfRequiredInputs( 1 );
80 }
81
82 // -------------------------------------------------------------------------
83 template< class _TPolyLine, class _TImage >
84 cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine, _TImage >::
85 ~PolyLineParametricPathWriter( )
86 {
87 }
88
89 // -------------------------------------------------------------------------
90 template< class _TPolyLine, class _TImage >
91 void cpExtensions::Algorithms::PolyLineParametricPathWriter< _TPolyLine, _TImage >::
92 GenerateData( )
93 {
94   typedef typename _TPolyLine::TContinuousIndex _TContinuousIndex;
95
96   // "Serialize" data
97   const _TPolyLine* input = this->GetInput( );
98   const _TImage* image = this->GetImage( );
99   unsigned int dim = _TPolyLine::PathDimension;
100   std::stringstream buffer;
101   long step = input->GetSize( ) / this->m_NumberOfPoints;
102
103   for( unsigned long i = 0; i < input->GetSize( ); i += step )
104   {
105     _TContinuousIndex idx;
106     idx.Fill( 0 );
107     int c = 0;
108     for( long j = -step; j <= step; ++j )
109     {
110       long k = i + j;
111       if( k >= 0 && k < input->GetSize( ) )
112       {
113         _TContinuousIndex kdx = input->GetContinuousVertex( k );
114         for( unsigned int d = 0; d < dim; ++d )
115           idx[ d ] += kdx[ d ];
116         c++;
117
118       } // fi
119
120     } // rof
121     if( c != 0 )
122       for( unsigned int d = 0; d < dim; ++d )
123         idx[ d ] /= c;
124
125     buffer << idx[ 0 ];
126     for( unsigned int d = 1; d < dim; ++d )
127       buffer << " " << idx[ d ];
128     if( image != NULL )
129     {
130       typename _TImage::PointType pidx;
131       typename _TImage::IndexType iidx;
132       image->TransformContinuousIndexToPhysicalPoint( idx, pidx );
133       image->TransformPhysicalPointToIndex( pidx, iidx );
134       buffer << " " << image->GetPixel( iidx );
135
136     } // fi
137     buffer << std::endl;
138
139   } // rof
140
141   // Real write
142   std::ofstream file_stream(
143     this->m_FileName.c_str( ), std::ofstream::binary
144     );
145   if( !file_stream )
146   {
147     itkExceptionMacro(
148       << "Could not open file \"" << this->m_FileName << "\" to write a "
149       << "cpExtensions::DataStructures::PolyLineParametricPath< " << dim
150       << " > object."
151       );
152     return;
153
154   } // fi
155   file_stream.write( buffer.str( ).c_str( ), buffer.str( ).size( ) );
156 }
157
158 #endif // __cpExtensions__Algorithms__PolyLineParametricPathWriter__hxx__
159
160 // eof - $RCSfile$