]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/SkeletonToPolyDataFilter.hxx
9468c4baf5debe3b728708c34965f3aab22aacb2
[FrontAlgorithms.git] / lib / fpa / Image / SkeletonToPolyDataFilter.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #ifndef __fpa__Image__SkeletonToPolyDataFilter__hxx__
7 #define __fpa__Image__SkeletonToPolyDataFilter__hxx__
8
9 #include <vtkCellArray.h>
10 #include <vtkInformation.h>
11 #include <vtkInformationVector.h>
12 #include <vtkSmartPointer.h>
13
14 // -------------------------------------------------------------------------
15 template< class _TSkeleton >
16 typename fpa::Image::SkeletonToPolyDataFilter< _TSkeleton >::
17 Self* fpa::Image::SkeletonToPolyDataFilter< _TSkeleton >::
18 New( )
19 {
20   return( new Self( ) );
21 }
22
23 // -------------------------------------------------------------------------
24 template< class _TSkeleton >
25 const typename
26 fpa::Image::SkeletonToPolyDataFilter< _TSkeleton >::
27 TSkeleton* fpa::Image::SkeletonToPolyDataFilter< _TSkeleton >::
28 GetInput( ) const
29 {
30   return( this->m_Skeleton );
31 }
32
33 // -------------------------------------------------------------------------
34 template< class _TSkeleton >
35 void fpa::Image::SkeletonToPolyDataFilter< _TSkeleton >::
36 SetInput( const TSkeleton* sk )
37 {
38   if( this->m_Skeleton != sk )
39   {
40     this->m_Skeleton = sk;
41     this->Modified( );
42
43   } // fi
44 }
45
46 // -------------------------------------------------------------------------
47 template< class _TSkeleton >
48 fpa::Image::SkeletonToPolyDataFilter< _TSkeleton >::
49 SkeletonToPolyDataFilter( )
50   : vtkPolyDataAlgorithm( ),
51     m_Skeleton( NULL )
52 {
53   this->SetNumberOfInputPorts( 0 );
54 }
55
56 // -------------------------------------------------------------------------
57 template< class _TSkeleton >
58 fpa::Image::SkeletonToPolyDataFilter< _TSkeleton >::
59 ~SkeletonToPolyDataFilter( )
60 {
61 }
62
63 // -------------------------------------------------------------------------
64 template< class _TSkeleton >
65 int fpa::Image::SkeletonToPolyDataFilter< _TSkeleton >::
66 RequestData(
67   vtkInformation* information,
68   vtkInformationVector** input,
69   vtkInformationVector* output
70   )
71 {
72   typedef typename _TSkeleton::TPath _TPath;
73   static const unsigned int dim = _TPath::PathDimension;
74
75   if( this->m_Skeleton == NULL )
76     return( 0 );
77
78   // Get output
79   vtkInformation* info = output->GetInformationObject( 0 );
80   vtkPolyData* out = vtkPolyData::SafeDownCast(
81     info->Get( vtkDataObject::DATA_OBJECT( ) )
82     );
83
84   // Prepare data
85   out->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
86   out->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
87   out->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
88   out->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
89   out->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
90   vtkPoints* points = out->GetPoints( );
91   vtkCellArray* lines = out->GetLines( );
92
93   // Assign all data
94   typename TMatrix::const_iterator  mIt = this->m_Skeleton->BeginEdgesRows( );
95   for( ; mIt != this->m_Skeleton->EndEdgesRows( ); ++mIt )
96   {
97     // TODO: mIt->first; --> this is the row index. <--
98     typename TMatrixRow::const_iterator rIt = mIt->second.begin( );
99     for( ; rIt != mIt->second.end( ); ++rIt )
100     {
101       // TODO: rIt->first;  --> this is the column index.
102       typename TEdges::const_iterator eIt = rIt->second.begin( );
103       for( ; eIt != rIt->second.end( ); ++eIt )
104       {
105         _TPath* path = *eIt;
106         for( unsigned long i = 0; i < path->GetSize( ); ++i )
107         {
108           auto pnt = path->GetPoint( i );
109           if( dim == 1 )
110             points->InsertNextPoint( pnt[ 0 ], 0, 0 );
111           else if( dim == 2 )
112             points->InsertNextPoint( pnt[ 0 ], pnt[ 1 ], 0 );
113           else
114             points->InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
115           if( i > 0 )
116           {
117             lines->InsertNextCell( 2 );
118             lines->InsertCellPoint( points->GetNumberOfPoints( ) - 2 );
119             lines->InsertCellPoint( points->GetNumberOfPoints( ) - 1 );
120
121           } // fi
122
123         } // rof
124
125       } // rof
126
127     } // rof
128
129   } // rof
130   return( 1 );
131 }
132
133 // -------------------------------------------------------------------------
134 template< class _TSkeleton >
135 int fpa::Image::SkeletonToPolyDataFilter< _TSkeleton >::
136 RequestInformation(
137   vtkInformation* information,
138   vtkInformationVector** input,
139   vtkInformationVector* output
140   )
141 {
142   return( 1 );
143 }
144
145 #endif // __fpa__Image__SkeletonToPolyDataFilterFilter__hxx__
146
147 // eof - $RCSfile$