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