1 #include <VTKWidgets/SplineWidget.h>
2 #include <cpInstances/Image.h>
3 #include <cpInstances/Mesh.h>
4 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
8 #include <vtkImageData.h>
9 #include <vtkPolyData.h>
10 #include <vtkRenderer.h>
11 #include <vtkSplineWidget.h>
12 #include <vtkParametricSpline.h>
14 // -------------------------------------------------------------------------
15 void cpPluginsVTKWidgets::SplineWidget::
20 // -------------------------------------------------------------------------
21 void cpPluginsVTKWidgets::SplineWidget::
24 auto wdg = this->GetVTK< vtkSplineWidget >( );
28 wdg->GetInteractor( )->Render( );
33 // -------------------------------------------------------------------------
34 bool cpPluginsVTKWidgets::SplineWidget::
37 auto wdg = this->GetVTK< const vtkSplineWidget >( );
40 vtkSplineWidget* w = const_cast< vtkSplineWidget* >( wdg );
41 return( w->GetEnabled( ) != 0 );
47 // -------------------------------------------------------------------------
48 cpPluginsVTKWidgets::SplineWidget::
53 typedef cpPlugins::BaseObjects::DataObject _TData;
54 typedef cpInstances::Mesh _TMesh;
56 this->_ConfigureInput< _TData >( "Input", false, false );
57 this->_ConfigureOutput< _TMesh >( "Output" );
58 this->m_Contour = vtkSmartPointer< vtkPolyData >::New( );
59 this->m_Contour->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
60 this->m_Contour->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
61 this->m_Contour->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
62 this->m_Contour->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
63 this->m_Contour->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
64 this->GetOutput( "Output" )->SetVTK( this->m_Contour );
66 this->m_Parameters.ConfigureAsBool( "Extend", false );
69 // -------------------------------------------------------------------------
70 cpPluginsVTKWidgets::SplineWidget::
75 // -------------------------------------------------------------------------
76 void cpPluginsVTKWidgets::SplineWidget::
79 typedef cpExtensions::Visualization::WindowLevelImageActor _TActor;
81 auto image = this->GetInputData< vtkImageData >( "Input" );
83 this->_Error( "Invalid input image." );
84 if( this->m_Interactors.size( ) == 0 )
85 this->_Error( "Give at least one interactor." );
87 auto wdg = this->_CreateVTK< vtkSplineWidget >( );
88 if( this->m_Configured )
90 if( this->m_Parameters.GetBool( "Extend" ) )
92 wdg->GetPolyData( this->m_Contour.GetPointer( ) );
94 typedef itk::Vector< double, 3 > _TVector;
95 typedef std::vector< _TVector > _TVectors;
97 unsigned long nPoints = this->m_Contour->GetNumberOfPoints( );
98 unsigned long support = nPoints / 10;
100 for( unsigned long i = 0; i < support; ++i )
102 double p[ 3 ], q[ 3 ];
103 this->m_Contour->GetPoint( i, p );
104 this->m_Contour->GetPoint( nPoints - 1 - i, q );
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 )
123 pt.push_back( pp[ i + 1 ] - pp[ i - 1 ] );
124 qt.push_back( qp[ i + 1 ] - qp[ i - 1 ] );
127 pt.push_back( pp[ support - 1 ] - pp[ support - 2 ] );
128 qt.push_back( qp[ support - 1 ] - qp[ support - 2 ] );
130 _TVector t0( double( 0 ) ), t1( double( 0 ) );
131 long real0 = 0, real1 = 0;
132 for( unsigned long i = 0; i < support; ++i )
134 double n0 = pt[ i ].GetNorm( );
135 if( n0 > double( 0 ) )
142 double n1 = qt[ i ].GetNorm( );
143 if( n1 > double( 0 ) )
153 t0 /= double( -real0 );
155 t1 /= double( -real1 );
157 t0 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
158 t1 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
160 _TVector p0 = pp[ 0 ] + t0;
161 _TVector p1 = qp[ 0 ] + t1;
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 );
172 this->m_Contour->GetPoints( )->
173 InsertNextPoint( p0[ 0 ], p0[ 1 ], p0[ 2 ] );
174 for( unsigned long i = 0; i < nPoints; ++i )
177 cnt->GetPoints( )->GetPoint( i, p );
178 this->m_Contour->GetPoints( )->InsertNextPoint( p );
181 this->m_Contour->GetPoints( )->
182 InsertNextPoint( p1[ 0 ], p1[ 1 ], p1[ 2 ] );
183 this->m_Contour->Modified( );
186 wdg->GetPolyData( this->m_Contour.GetPointer( ) );
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 )
196 auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
199 auto props = r->GetViewProps( );
202 props->InitTraversal( );
203 while( vtkProp* prop = props->GetNextProp( ) )
205 auto a = dynamic_cast< _TActor* >( prop );
207 if( a->GetImage( ) == image )
222 if( actor == NULL || ren == NULL || iren == NULL )
223 this->_Error( "Invalid actor and/or renderer." );
225 // Widget configuration
226 wdg->SetCurrentRenderer( ren );
227 wdg->SetDefaultRenderer( ren );
228 wdg->SetInputData( image );
229 wdg->SetProp3D( actor );
230 wdg->SetInteractor( iren );
232 image->GetBounds( bnds );
234 bnds[ 0 ], bnds[ 1 ],
235 bnds[ 2 ], bnds[ 3 ],
238 wdg->ProjectToPlaneOn( );
239 wdg->SetProjectionNormalToZAxes( );
240 wdg->SetProjectionPosition(
242 actor->GetBounds( )[ 4 ] +
243 actor->GetBounds( )[ 5 ]
246 wdg->SetHandleSize( 0.005 );
247 wdg->SetNumberOfHandles( 3 );
248 this->m_Configured = true;