]> Creatis software - cpPlugins.git/blob - plugins/Widgets/SplineWidget.cxx
...
[cpPlugins.git] / plugins / Widgets / SplineWidget.cxx
1 #include <plugins/Widgets/SplineWidget.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/DataObjects/Mesh.h>
4 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
5 #include <cpPlugins_BaseObjects.h>
6
7 #include <vtkImageData.h>
8 #include <vtkRenderer.h>
9 #include <vtkSplineWidget.h>
10 #include <vtkParametricSpline.h>
11
12 // -------------------------------------------------------------------------
13 void cpPluginsWidgets::SplineWidget::
14 Clear( )
15 {
16 }
17
18 // -------------------------------------------------------------------------
19 void cpPluginsWidgets::SplineWidget::
20 SetEnabled( bool v )
21 {
22   auto wdg = this->GetVTK< vtkSplineWidget >( );
23   if( wdg != NULL )
24   {
25     wdg->SetEnabled( v );
26     wdg->GetInteractor( )->Render( );
27
28   } // fi
29 }
30
31 // -------------------------------------------------------------------------
32 bool cpPluginsWidgets::SplineWidget::
33 GetEnabled( ) const
34 {
35   auto wdg = this->GetVTK< const vtkSplineWidget >( );
36   if( wdg != NULL )
37   {
38     vtkSplineWidget* w = const_cast< vtkSplineWidget* >( wdg );
39     return( w->GetEnabled( ) != 0 );
40   }
41   else
42     return( false );
43 }
44
45 // -------------------------------------------------------------------------
46 cpPluginsWidgets::SplineWidget::
47 SplineWidget( )
48   : Superclass( ),
49     m_Configured( false )
50 {
51   typedef cpPlugins::BaseObjects::DataObject _TData;
52   typedef cpPlugins::DataObjects::Mesh       _TMesh;
53
54   this->_ConfigureInput< _TData >( "Input", false, false );
55   this->_ConfigureOutput< _TMesh >( "Output" );
56   this->m_Contour = vtkSmartPointer< vtkPolyData >::New( );
57   this->m_Contour->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
58   this->m_Contour->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
59   this->m_Contour->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
60   this->m_Contour->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
61   this->m_Contour->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
62   this->GetOutput( "Output" )->SetVTK( this->m_Contour );
63
64   this->m_Parameters.ConfigureAsBool( "Extend" );
65   this->m_Parameters.SetBool( "Extend", false );
66 }
67
68 // -------------------------------------------------------------------------
69 cpPluginsWidgets::SplineWidget::
70 ~SplineWidget( )
71 {
72 }
73
74 // -------------------------------------------------------------------------
75 void cpPluginsWidgets::SplineWidget::
76 _GenerateData( )
77 {
78   typedef cpExtensions::Visualization::WindowLevelImageActor _TActor;
79
80   auto image = this->GetInputData< vtkImageData >( "Input" );
81   if( image == NULL )
82     this->_Error( "Invalid input image." );
83   if( this->m_Interactors.size( ) == 0 )
84     this->_Error( "Give at least one interactor." );
85
86   auto wdg = this->_CreateVTK< vtkSplineWidget >( );
87   if( this->m_Configured )
88   {
89     if( this->m_Parameters.GetBool( "Extend" ) )
90     {
91       wdg->GetPolyData( this->m_Contour.GetPointer( ) );
92
93       typedef itk::Vector< double, 3 > _TVector;
94       typedef std::vector< _TVector > _TVectors;
95
96       unsigned long nPoints = this->m_Contour->GetNumberOfPoints( );
97       unsigned long support = nPoints / 10;
98       _TVectors pp, qp;
99       for( unsigned long i = 0; i < support; ++i )
100       {
101         double p[ 3 ], q[ 3 ];
102         this->m_Contour->GetPoint( i, p );
103         this->m_Contour->GetPoint( nPoints - 1 - i, q );
104
105         _TVector pv, qv;
106         pv[ 0 ] = p[ 0 ];
107         pv[ 1 ] = p[ 1 ];
108         pv[ 2 ] = p[ 2 ];
109         qv[ 0 ] = q[ 0 ];
110         qv[ 1 ] = q[ 1 ];
111         qv[ 2 ] = q[ 2 ];
112         pp.push_back( pv );
113         qp.push_back( qv );
114
115       } // rof
116
117       _TVectors pt, qt;
118       pt.push_back( pp[ 1 ] - pp[ 0 ] );
119       qt.push_back( qp[ 1 ] - qp[ 0 ] );
120       for( unsigned long i = 1; i < support - 1; ++i )
121       {
122         pt.push_back( pp[ i + 1 ] - pp[ i - 1 ] );
123         qt.push_back( qp[ i + 1 ] - qp[ i - 1 ] );
124
125       } // rof
126       pt.push_back( pp[ support - 1 ] - pp[ support - 2 ] );
127       qt.push_back( qp[ support - 1 ] - qp[ support - 2 ] );
128
129       _TVector t0( double( 0 ) ), t1( double( 0 ) );
130       long real0 = 0, real1 = 0;
131       for( unsigned long i = 0; i < support; ++i )
132       {
133         double n0 = pt[ i ].GetNorm( );
134         if( n0 > double( 0 ) )
135         {
136           t0 += pt[ i ] / n0;
137           real0++;
138
139         } // fi
140
141         double n1 = qt[ i ].GetNorm( );
142         if( n1 > double( 0 ) )
143         {
144           t1 += qt[ i ] / n1;
145           real1++;
146
147         } // fi
148
149       } // rof
150
151       if( real0 > 0 )
152         t0 /= double( -real0 );
153       if( real1 > 0 )
154         t1 /= double( -real1 );
155
156       t0 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
157       t1 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
158
159       _TVector p0 = pp[ 0 ] + t0;
160       _TVector p1 = qp[ 0 ] + t1;
161
162       vtkSmartPointer< vtkPolyData > cnt = this->m_Contour;
163       this->m_Contour = vtkSmartPointer< vtkPolyData >::New( );
164       this->m_Contour->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
165       this->m_Contour->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
166       this->m_Contour->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
167       this->m_Contour->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
168       this->m_Contour->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
169       this->GetOutput( "Output" )->SetVTK( this->m_Contour );
170
171       this->m_Contour->GetPoints( )->
172         InsertNextPoint( p0[ 0 ], p0[ 1 ], p0[ 2 ] );
173       for( unsigned long i = 0; i < nPoints; ++i )
174       {
175         double p[ 3 ];
176         cnt->GetPoints( )->GetPoint( i, p );
177         this->m_Contour->GetPoints( )->InsertNextPoint( p );
178
179       } // rof
180       this->m_Contour->GetPoints( )->
181         InsertNextPoint( p1[ 0 ], p1[ 1 ], p1[ 2 ] );
182       this->m_Contour->Modified( );
183     }
184     else
185       wdg->GetPolyData( this->m_Contour.GetPointer( ) );
186   }
187   else
188   {
189     auto iIt = this->m_Interactors.begin( );
190     vtkRenderWindowInteractor* iren = NULL;
191     vtkRenderer* ren = NULL;
192     _TActor* actor = NULL;
193     for( ; iIt != this->m_Interactors.end( ); ++iIt )
194     {
195       auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
196       if( r != NULL )
197       {
198         auto props = r->GetViewProps( );
199         if( props != NULL )
200         {
201           props->InitTraversal( );
202           while( vtkProp* prop = props->GetNextProp( ) )
203           {
204             auto a = dynamic_cast< _TActor* >( prop );
205             if( a != NULL )
206               if( a->GetImage( ) == image )
207               {
208                 iren = *iIt;
209                 actor = a;
210                 ren = r;
211
212               } // fi
213
214           } // elihw
215
216         } // fi
217
218       } // fi
219
220     } // rof
221     if( actor == NULL || ren == NULL || iren == NULL )
222       this->_Error( "Invalid actor and/or renderer." );
223
224     // Widget configuration
225     wdg->SetCurrentRenderer( ren );
226     wdg->SetDefaultRenderer( ren );
227     wdg->SetInputData( image );
228     wdg->SetProp3D( actor );
229     wdg->SetInteractor( iren );
230     double bnds[ 6 ];
231     image->GetBounds( bnds );
232     wdg->PlaceWidget(
233       bnds[ 0 ], bnds[ 1 ],
234       bnds[ 2 ], bnds[ 3 ],
235       bnds[ 4 ], bnds[ 5 ]
236       );
237     wdg->ProjectToPlaneOn( );
238     wdg->SetProjectionNormalToZAxes( );
239     wdg->SetProjectionPosition(
240       (
241         actor->GetBounds( )[ 4 ] +
242         actor->GetBounds( )[ 5 ]
243         ) / double( 2 )
244       );
245     wdg->SetHandleSize( 0.005 );
246     wdg->SetNumberOfHandles( 3 );
247     this->m_Configured = true;
248
249   } // fi
250 }
251
252 // eof - $RCSfile$