]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/VTK/Image/PathToPolyDataFilter.hxx
Architecture revisited.
[FrontAlgorithms.git] / lib / fpa / VTK / Image / PathToPolyDataFilter.hxx
1 #ifndef __FPA__VTK__IMAGE__PATHTOPOLYDATAFILTER__HXX__
2 #define __FPA__VTK__IMAGE__PATHTOPOLYDATAFILTER__HXX__
3
4 #include <vtkCellArray.h>
5 #include <vtkInformation.h>
6 #include <vtkInformationVector.h>
7 #include <vtkPointData.h>
8 #include <vtkPoints.h>
9 #include <vtkSmartPointer.h>
10 #include <vtkStreamingDemandDrivenPipeline.h>
11
12 // -------------------------------------------------------------------------
13 template< class _TMinimumSpanningTree >
14 typename fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
15 Self* fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
16 New( )
17 {
18   return( new Self( ) );
19 }
20
21 // -------------------------------------------------------------------------
22 template< class _TMinimumSpanningTree >
23 const typename fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
24 TMinimumSpanningTree*
25 fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
26 GetMinimumSpanningTree( ) const
27 {
28   return( this->m_MST );
29 }
30
31 // -------------------------------------------------------------------------
32 template< class _TMinimumSpanningTree >
33 const typename fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
34 TImage* fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
35 GetImage( ) const
36 {
37   return( this->m_Image );
38 }
39
40 // -------------------------------------------------------------------------
41 template< class _TMinimumSpanningTree >
42 void fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
43 SetMinimumSpanningTree( const TMinimumSpanningTree* mst )
44 {
45   if( this->m_MST != mst )
46   {
47     this->m_MST = mst;
48     this->Modified( );
49
50   } // fi
51 }
52
53 // -------------------------------------------------------------------------
54 template< class _TMinimumSpanningTree >
55 void fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
56 SetImage( const TImage* image )
57 {
58   if( this->m_Image != image )
59   {
60     this->m_Image = image;
61     this->Modified( );
62
63   } // fi
64 }
65
66 // -------------------------------------------------------------------------
67 template< class _TMinimumSpanningTree >
68 fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
69 PathToPolyDataFilter( )
70   : vtkPolyDataAlgorithm( ),
71     m_MST( NULL ),
72     m_Image( NULL )
73 {
74   this->SetNumberOfInputPorts( 0 );
75 }
76         
77 // -------------------------------------------------------------------------
78 template< class _TMinimumSpanningTree >
79 fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
80 ~PathToPolyDataFilter( )
81 {
82 }
83
84 // -------------------------------------------------------------------------
85 template< class _TMinimumSpanningTree >
86 int fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
87 RequestData(
88   vtkInformation* information,
89   vtkInformationVector** input,
90   vtkInformationVector* output
91   )
92 {
93   if( this->m_MST == NULL || this->m_Image == NULL )
94     return( 0 );
95   auto path = this->m_MST->GetPath( this->Seed0, this->Seed1 );
96   if( path.size( ) == 0 )
97     return( 0 );
98
99   // Prepare output
100   vtkInformation* info = output->GetInformationObject( 0 );
101   vtkPolyData* out = vtkPolyData::SafeDownCast(
102     info->Get( vtkDataObject::DATA_OBJECT( ) )
103     );
104   this->_PrepareOutput( out );
105   vtkPoints* points = out->GetPoints( );
106   vtkCellArray* lines = out->GetLines( );
107
108   unsigned int i = 0;
109   for( auto pIt = path.begin( ); pIt != path.end( ); ++pIt, ++i )
110   {
111     typename TImage::PointType p;
112     this->m_Image->TransformIndexToPhysicalPoint( *pIt, p );
113     if( TImage::ImageDimension == 1 )
114       points->InsertNextPoint( double( p[ 0 ] ), double( 0 ), double( 0 ) );
115     else if( TImage::ImageDimension == 2 )
116       points->InsertNextPoint( double( p[ 0 ] ), double( p[ 1 ] ), double( 0 ) );
117     else if( TImage::ImageDimension > 2 )
118       points->InsertNextPoint( double( p[ 0 ] ), double( p[ 1 ] ), double( p[ 2 ] ) );
119
120     if( i > 0 )
121     {
122       lines->InsertNextCell( 2 );
123       lines->InsertCellPoint( i - 1 );
124       lines->InsertCellPoint( i );
125
126     } // fi
127
128   } // rof
129   out->Modified( );
130   return( 1 );
131 }
132
133 // -------------------------------------------------------------------------
134 template< class _TMinimumSpanningTree >
135 int fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
136 RequestInformation(
137   vtkInformation* information,
138   vtkInformationVector** input,
139   vtkInformationVector* output
140   )
141 {
142   vtkInformation* out = output->GetInformationObject( 0 );
143   out->Set( CAN_HANDLE_PIECE_REQUEST( ), 1 );
144   return( 1 );
145 }
146
147 // -------------------------------------------------------------------------
148 template< class _TMinimumSpanningTree >
149 void fpa::VTK::Image::PathToPolyDataFilter< _TMinimumSpanningTree >::
150 _PrepareOutput( vtkPolyData* out )
151 {
152   // Prepare points
153   vtkPoints* points = out->GetPoints( );
154   if( points == NULL )
155   {
156     points = vtkPoints::New( );
157     out->SetPoints( points );
158     points->Delete( );
159
160   } // fi
161
162   // Prepare points
163   vtkCellArray* lines = out->GetLines( );
164   if( lines == NULL )
165   {
166     lines = vtkCellArray::New( );
167     out->SetLines( lines );
168     out->SetVerts( vtkCellArray::New( ) );
169     out->SetPolys( vtkCellArray::New( ) );
170     out->SetStrips( vtkCellArray::New( ) );
171     lines->Delete( );
172
173   } // fi
174 }
175
176 #endif // __FPA__VTK__IMAGE__PATHTOPOLYDATAFILTER__HXX__
177
178 // eof - $RCSfile$