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