]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/VTK/Image/ImageSkeletonToPolyData.hxx
5e413580e8d8df75aca80643ce5de7249b98eb8b
[FrontAlgorithms.git] / lib / fpa / VTK / Image / ImageSkeletonToPolyData.hxx
1 #ifndef __FPA__VTK__IMAGESKELETONTOPOLYDATA__HXX__
2 #define __FPA__VTK__IMAGESKELETONTOPOLYDATA__HXX__
3
4 #include <vtkInformation.h>
5 #include <vtkInformationVector.h>
6 #include <vtkSmartPointer.h>
7
8 // -------------------------------------------------------------------------
9 template< class _TSkeleton >
10 typename fpa::VTK::Image::ImageSkeletonToPolyData< _TSkeleton >::
11 Self* fpa::VTK::Image::ImageSkeletonToPolyData< _TSkeleton >::
12 New( )
13 {
14   return( new Self( ) );
15 }
16
17 // -------------------------------------------------------------------------
18 template< class _TSkeleton >
19 const _TSkeleton* fpa::VTK::Image::ImageSkeletonToPolyData< _TSkeleton >::
20 GetInput( ) const
21 {
22   return( this->m_Skeleton );
23 }
24
25 // -------------------------------------------------------------------------
26 template< class _TSkeleton >
27 void fpa::VTK::Image::ImageSkeletonToPolyData< _TSkeleton >::
28 SetInput( const _TSkeleton* sk )
29 {
30   if( this->m_Skeleton != sk )
31   {
32     this->m_Skeleton = sk;
33     this->Modified( );
34
35   } // fi
36 }
37
38 // -------------------------------------------------------------------------
39 template< class _TSkeleton >
40 fpa::VTK::Image::ImageSkeletonToPolyData< _TSkeleton >::
41 ImageSkeletonToPolyData( )
42   : vtkPolyDataAlgorithm( ),
43     m_Skeleton( NULL )
44 {
45   this->SetNumberOfInputPorts( 0 );
46 }
47
48 // -------------------------------------------------------------------------
49 template< class _TSkeleton >
50 fpa::VTK::Image::ImageSkeletonToPolyData< _TSkeleton >::
51 ~ImageSkeletonToPolyData( )
52 {
53 }
54
55 // -------------------------------------------------------------------------
56 template< class _TSkeleton >
57 int fpa::VTK::Image::ImageSkeletonToPolyData< _TSkeleton >::
58 RequestData(
59   vtkInformation* information,
60   vtkInformationVector** input,
61   vtkInformationVector* output
62   )
63 {
64   typedef typename _TSkeleton::TVertex              _TVertex;
65   typedef typename _TSkeleton::TVertexCmp           _TCmp;
66   typedef itk::Point< double, _TVertex::Dimension > _TPoint;
67   typedef
68     std::map< _TVertex, std::map< _TVertex, bool, _TCmp >, _TCmp >
69     _TMarks;
70   static const unsigned int dim = _TVertex::Dimension;
71
72   if( this->m_Skeleton == NULL )
73     return( 0 );
74
75   // Get output
76   vtkInformation* info = output->GetInformationObject( 0 );
77   vtkPolyData* out = vtkPolyData::SafeDownCast(
78     info->Get( vtkDataObject::DATA_OBJECT( ) )
79     );
80
81   // Prepare points and cells
82   vtkSmartPointer< vtkPoints > points =
83     vtkSmartPointer< vtkPoints >::New( );
84   vtkSmartPointer< vtkCellArray > lines =
85     vtkSmartPointer< vtkCellArray >::New( );
86
87   // Iterator over input data
88   auto& sk = this->m_Skeleton->Get( );
89   auto mst = this->m_Skeleton->GetMinimumSpanningTree( );
90   _TMarks marks;
91   for( auto i = sk.begin( ); i != sk.end( ); ++i )
92   {
93     for( auto j = i->second.begin( ); j != i->second.end( ); ++j )
94     {
95       if( !marks[ i->first ][ j->first ] )
96       {
97         auto path = j->second.GetPointer( );
98         if( path != NULL )
99         {
100           auto vertices = path->GetVertexList( );
101           for( unsigned int v = 0; v < vertices->Size( ); ++v )
102           {
103             auto idx = vertices->GetElement( v );
104             _TPoint pnt;
105             mst->TransformContinuousIndexToPhysicalPoint( idx, pnt );
106             if( dim == 1 )
107               points->InsertNextPoint( pnt[ 0 ], 0, 0 );
108             else if( dim == 2 )
109               points->InsertNextPoint( pnt[ 0 ], pnt[ 1 ], 0 );
110             else
111               points->InsertNextPoint( pnt[ 0 ], pnt[ 1 ], pnt[ 2 ] );
112             if( v > 0 )
113             {
114               lines->InsertNextCell( 2 );
115               lines->InsertCellPoint( points->GetNumberOfPoints( ) - 2 );
116               lines->InsertCellPoint( points->GetNumberOfPoints( ) - 1 );
117
118             } // fi
119
120           } // rof
121
122         } // fi
123
124         // Mark path and its symmetric as visited
125         marks[ i->first ][ j->first ] = true;
126         marks[ j->first ][ i->first ] = true;
127
128       } // fi
129
130     } // rof
131
132   } // rof
133   out->SetPoints( points );
134   out->SetLines( lines );
135   return( 1 );
136 }
137
138 // -------------------------------------------------------------------------
139 template< class _TSkeleton >
140 int fpa::VTK::Image::ImageSkeletonToPolyData< _TSkeleton >::
141 RequestInformation(
142   vtkInformation* information,
143   vtkInformationVector** input,
144   vtkInformationVector* output
145   )
146 {
147   vtkInformation* info = output->GetInformationObject( 0 );
148   /* TODO
149      info->Set(
150      vtkStreamingDemandDrivenPipeline::MAXIMUM_NUMBER_OF_PIECES( ), -1
151      );
152   */
153
154   if( this->m_Skeleton != NULL )
155   {
156     /* TODO
157        typename C::TScalar len = this->m_RGC->GetTotalLength( );
158        typename C::TScalar s0 = this->m_RGC->Gets0( );
159        typename C::TPoint p0 = this->m_RGC->Axis( s0 );
160        typename C::TPoint p1 = this->m_RGC->Axis( s0 + len );
161
162        info->Set(
163        vtkStreamingDemandDrivenPipeline::WHOLE_BOUNDING_BOX( ),
164        double( p0[ 0 ] ), double( p1[ 0 ] ),
165        double( p0[ 1 ] ), double( p1[ 1 ] ),
166        double( p0[ 2 ] ), double( p1[ 2 ] )
167        );
168     */
169
170   } // fi
171   return( 1 );
172 }
173
174 #endif //  __FPA__VTK__IMAGESKELETONTOPOLYDATA__HXX__
175
176 // eof - $RCSfile$