1 #include <Widgets/SplineWidget.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/DataObjects/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 cpPluginsWidgets::SplineWidget::
20 // -------------------------------------------------------------------------
21 void cpPluginsWidgets::SplineWidget::
24 auto wdg = this->GetVTK< vtkSplineWidget >( );
28 wdg->GetInteractor( )->Render( );
33 // -------------------------------------------------------------------------
34 bool cpPluginsWidgets::SplineWidget::
37 auto wdg = this->GetVTK< const vtkSplineWidget >( );
40 vtkSplineWidget* w = const_cast< vtkSplineWidget* >( wdg );
41 return( w->GetEnabled( ) != 0 );
47 // -------------------------------------------------------------------------
48 cpPluginsWidgets::SplineWidget::
53 typedef cpPlugins::BaseObjects::DataObject _TData;
54 typedef cpPlugins::DataObjects::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" );
67 this->m_Parameters.SetBool( "Extend", false );
70 // -------------------------------------------------------------------------
71 cpPluginsWidgets::SplineWidget::
76 // -------------------------------------------------------------------------
77 void cpPluginsWidgets::SplineWidget::
80 typedef cpExtensions::Visualization::WindowLevelImageActor _TActor;
82 auto image = this->GetInputData< vtkImageData >( "Input" );
84 this->_Error( "Invalid input image." );
85 if( this->m_Interactors.size( ) == 0 )
86 this->_Error( "Give at least one interactor." );
88 auto wdg = this->_CreateVTK< vtkSplineWidget >( );
89 if( this->m_Configured )
91 if( this->m_Parameters.GetBool( "Extend" ) )
93 wdg->GetPolyData( this->m_Contour.GetPointer( ) );
95 typedef itk::Vector< double, 3 > _TVector;
96 typedef std::vector< _TVector > _TVectors;
98 unsigned long nPoints = this->m_Contour->GetNumberOfPoints( );
99 unsigned long support = nPoints / 10;
101 for( unsigned long i = 0; i < support; ++i )
103 double p[ 3 ], q[ 3 ];
104 this->m_Contour->GetPoint( i, p );
105 this->m_Contour->GetPoint( nPoints - 1 - i, q );
120 pt.push_back( pp[ 1 ] - pp[ 0 ] );
121 qt.push_back( qp[ 1 ] - qp[ 0 ] );
122 for( unsigned long i = 1; i < support - 1; ++i )
124 pt.push_back( pp[ i + 1 ] - pp[ i - 1 ] );
125 qt.push_back( qp[ i + 1 ] - qp[ i - 1 ] );
128 pt.push_back( pp[ support - 1 ] - pp[ support - 2 ] );
129 qt.push_back( qp[ support - 1 ] - qp[ support - 2 ] );
131 _TVector t0( double( 0 ) ), t1( double( 0 ) );
132 long real0 = 0, real1 = 0;
133 for( unsigned long i = 0; i < support; ++i )
135 double n0 = pt[ i ].GetNorm( );
136 if( n0 > double( 0 ) )
143 double n1 = qt[ i ].GetNorm( );
144 if( n1 > double( 0 ) )
154 t0 /= double( -real0 );
156 t1 /= double( -real1 );
158 t0 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
159 t1 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
161 _TVector p0 = pp[ 0 ] + t0;
162 _TVector p1 = qp[ 0 ] + t1;
164 vtkSmartPointer< vtkPolyData > cnt = this->m_Contour;
165 this->m_Contour = vtkSmartPointer< vtkPolyData >::New( );
166 this->m_Contour->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
167 this->m_Contour->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
168 this->m_Contour->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
169 this->m_Contour->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
170 this->m_Contour->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
171 this->GetOutput( "Output" )->SetVTK( this->m_Contour );
173 this->m_Contour->GetPoints( )->
174 InsertNextPoint( p0[ 0 ], p0[ 1 ], p0[ 2 ] );
175 for( unsigned long i = 0; i < nPoints; ++i )
178 cnt->GetPoints( )->GetPoint( i, p );
179 this->m_Contour->GetPoints( )->InsertNextPoint( p );
182 this->m_Contour->GetPoints( )->
183 InsertNextPoint( p1[ 0 ], p1[ 1 ], p1[ 2 ] );
184 this->m_Contour->Modified( );
187 wdg->GetPolyData( this->m_Contour.GetPointer( ) );
191 auto iIt = this->m_Interactors.begin( );
192 vtkRenderWindowInteractor* iren = NULL;
193 vtkRenderer* ren = NULL;
194 _TActor* actor = NULL;
195 for( ; iIt != this->m_Interactors.end( ); ++iIt )
197 auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
200 auto props = r->GetViewProps( );
203 props->InitTraversal( );
204 while( vtkProp* prop = props->GetNextProp( ) )
206 auto a = dynamic_cast< _TActor* >( prop );
208 if( a->GetImage( ) == image )
223 if( actor == NULL || ren == NULL || iren == NULL )
224 this->_Error( "Invalid actor and/or renderer." );
226 // Widget configuration
227 wdg->SetCurrentRenderer( ren );
228 wdg->SetDefaultRenderer( ren );
229 wdg->SetInputData( image );
230 wdg->SetProp3D( actor );
231 wdg->SetInteractor( iren );
233 image->GetBounds( bnds );
235 bnds[ 0 ], bnds[ 1 ],
236 bnds[ 2 ], bnds[ 3 ],
239 wdg->ProjectToPlaneOn( );
240 wdg->SetProjectionNormalToZAxes( );
241 wdg->SetProjectionPosition(
243 actor->GetBounds( )[ 4 ] +
244 actor->GetBounds( )[ 5 ]
247 wdg->SetHandleSize( 0.005 );
248 wdg->SetNumberOfHandles( 3 );
249 this->m_Configured = true;