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