]> 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       double area = 0.0;
98       for( unsigned long i = 0; i < nPoints; ++i )
99       {
100         double p[ 3 ], q[ 3 ];
101         this->m_Contour->GetPoint( i, p );
102         this->m_Contour->GetPoint( ( i + 1 ) % nPoints, q );
103
104         area += ( p[ 0 ] * q[ 1 ] ) - ( q[ 0 ] - p[ 1 ] );
105
106       } // rof
107       std::cout << "Area: " << area << std::endl;
108
109       unsigned long support = nPoints / 10;
110       _TVectors pp, qp;
111       for( unsigned long i = 0; i < support; ++i )
112       {
113         double p[ 3 ], q[ 3 ];
114         this->m_Contour->GetPoint( i, p );
115         this->m_Contour->GetPoint( nPoints - 1 - i, q );
116
117         _TVector pv, qv;
118         pv[ 0 ] = p[ 0 ];
119         pv[ 1 ] = p[ 1 ];
120         pv[ 2 ] = p[ 2 ];
121         qv[ 0 ] = q[ 0 ];
122         qv[ 1 ] = q[ 1 ];
123         qv[ 2 ] = q[ 2 ];
124         pp.push_back( pv );
125         qp.push_back( qv );
126
127       } // rof
128
129       _TVectors pt, qt;
130       pt.push_back( pp[ 1 ] - pp[ 0 ] );
131       qt.push_back( qp[ 1 ] - qp[ 0 ] );
132       for( unsigned long i = 1; i < support - 1; ++i )
133       {
134         pt.push_back( pp[ i + 1 ] - pp[ i - 1 ] );
135         qt.push_back( qp[ i + 1 ] - qp[ i - 1 ] );
136
137       } // rof
138       pt.push_back( pp[ support - 1 ] - pp[ support - 2 ] );
139       qt.push_back( qp[ support - 1 ] - qp[ support - 2 ] );
140
141       _TVector t0( double( 0 ) ), t1( double( 0 ) );
142       long real0 = 0, real1 = 0;
143       for( unsigned long i = 0; i < support; ++i )
144       {
145         double n0 = pt[ i ].GetNorm( );
146         if( n0 > double( 0 ) )
147         {
148           t0 += pt[ i ] / n0;
149           real0++;
150
151         } // fi
152
153         double n1 = qt[ i ].GetNorm( );
154         if( n1 > double( 0 ) )
155         {
156           t1 += qt[ i ] / n1;
157           real1++;
158
159         } // fi
160
161       } // rof
162
163       if( real0 > 0 )
164         t0 /= double( -real0 );
165       if( real1 > 0 )
166         t1 /= double( -real1 );
167
168       t0 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
169       t1 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
170
171       _TVector p0 = pp[ 0 ] + t0;
172       _TVector p1 = qp[ 0 ] + t1;
173
174       vtkSmartPointer< vtkPolyData > cnt = this->m_Contour;
175       this->m_Contour = vtkSmartPointer< vtkPolyData >::New( );
176       this->m_Contour->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
177       this->m_Contour->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
178       this->m_Contour->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
179       this->m_Contour->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
180       this->m_Contour->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
181       this->GetOutput( "Output" )->SetVTK( this->m_Contour );
182
183       this->m_Contour->GetPoints( )->
184         InsertNextPoint( p0[ 0 ], p0[ 1 ], p0[ 2 ] );
185       for( unsigned long i = 0; i < nPoints; ++i )
186       {
187         double p[ 3 ];
188         cnt->GetPoints( )->GetPoint( i, p );
189         this->m_Contour->GetPoints( )->InsertNextPoint( p );
190
191       } // rof
192       this->m_Contour->GetPoints( )->
193         InsertNextPoint( p1[ 0 ], p1[ 1 ], p1[ 2 ] );
194       this->m_Contour->Modified( );
195     }
196     else
197       wdg->GetPolyData( this->m_Contour.GetPointer( ) );
198   }
199   else
200   {
201     auto iIt = this->m_Interactors.begin( );
202     vtkRenderWindowInteractor* iren = NULL;
203     vtkRenderer* ren = NULL;
204     _TActor* actor = NULL;
205     for( ; iIt != this->m_Interactors.end( ); ++iIt )
206     {
207       auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
208       if( r != NULL )
209       {
210         auto props = r->GetViewProps( );
211         if( props != NULL )
212         {
213           props->InitTraversal( );
214           while( vtkProp* prop = props->GetNextProp( ) )
215           {
216             auto a = dynamic_cast< _TActor* >( prop );
217             if( a != NULL )
218               if( a->GetImage( ) == image )
219               {
220                 iren = *iIt;
221                 actor = a;
222                 ren = r;
223
224               } // fi
225
226           } // elihw
227
228         } // fi
229
230       } // fi
231
232     } // rof
233     if( actor == NULL || ren == NULL || iren == NULL )
234       this->_Error( "Invalid actor and/or renderer." );
235
236     // Widget configuration
237     wdg->SetCurrentRenderer( ren );
238     wdg->SetDefaultRenderer( ren );
239     wdg->SetInputData( image );
240     wdg->SetProp3D( actor );
241     wdg->SetInteractor( iren );
242     double bnds[ 6 ];
243     image->GetBounds( bnds );
244     wdg->PlaceWidget(
245       bnds[ 0 ], bnds[ 1 ],
246       bnds[ 2 ], bnds[ 3 ],
247       bnds[ 4 ], bnds[ 5 ]
248       );
249     wdg->ProjectToPlaneOn( );
250     wdg->SetProjectionNormalToZAxes( );
251     wdg->SetProjectionPosition(
252       (
253         actor->GetBounds( )[ 4 ] +
254         actor->GetBounds( )[ 5 ]
255         ) / double( 2 )
256       );
257     wdg->SetHandleSize( 0.005 );
258     wdg->SetNumberOfHandles( 3 );
259     this->m_Configured = true;
260
261   } // fi
262 }
263
264 // eof - $RCSfile$