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>
7 #include <vtkImageData.h>
8 #include <vtkRenderer.h>
9 #include <vtkSplineWidget.h>
10 #include <vtkParametricSpline.h>
12 // -------------------------------------------------------------------------
13 void cpPluginsWidgets::SplineWidget::
18 // -------------------------------------------------------------------------
19 void cpPluginsWidgets::SplineWidget::
22 auto wdg = this->GetVTK< vtkSplineWidget >( );
26 wdg->GetInteractor( )->Render( );
31 // -------------------------------------------------------------------------
32 bool cpPluginsWidgets::SplineWidget::
35 auto wdg = this->GetVTK< const vtkSplineWidget >( );
38 vtkSplineWidget* w = const_cast< vtkSplineWidget* >( wdg );
39 return( w->GetEnabled( ) != 0 );
45 // -------------------------------------------------------------------------
46 cpPluginsWidgets::SplineWidget::
51 typedef cpPlugins::BaseObjects::DataObject _TData;
52 typedef cpPlugins::DataObjects::Mesh _TMesh;
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 );
64 this->m_Parameters.ConfigureAsBool( "Extend" );
65 this->m_Parameters.SetBool( "Extend", false );
68 // -------------------------------------------------------------------------
69 cpPluginsWidgets::SplineWidget::
74 // -------------------------------------------------------------------------
75 void cpPluginsWidgets::SplineWidget::
78 typedef cpExtensions::Visualization::WindowLevelImageActor _TActor;
80 auto image = this->GetInputData< vtkImageData >( "Input" );
82 this->_Error( "Invalid input image." );
83 if( this->m_Interactors.size( ) == 0 )
84 this->_Error( "Give at least one interactor." );
86 auto wdg = this->_CreateVTK< vtkSplineWidget >( );
87 if( this->m_Configured )
89 if( this->m_Parameters.GetBool( "Extend" ) )
91 wdg->GetPolyData( this->m_Contour.GetPointer( ) );
93 typedef itk::Vector< double, 3 > _TVector;
94 typedef std::vector< _TVector > _TVectors;
96 unsigned long nPoints = this->m_Contour->GetNumberOfPoints( );
97 unsigned long support = nPoints / 10;
99 for( unsigned long i = 0; i < support; ++i )
101 double p[ 3 ], q[ 3 ];
102 this->m_Contour->GetPoint( i, p );
103 this->m_Contour->GetPoint( nPoints - 1 - i, q );
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 )
122 pt.push_back( pp[ i + 1 ] - pp[ i - 1 ] );
123 qt.push_back( qp[ i + 1 ] - qp[ i - 1 ] );
126 pt.push_back( pp[ support - 1 ] - pp[ support - 2 ] );
127 qt.push_back( qp[ support - 1 ] - qp[ support - 2 ] );
129 _TVector t0( double( 0 ) ), t1( double( 0 ) );
130 long real0 = 0, real1 = 0;
131 for( unsigned long i = 0; i < support; ++i )
133 double n0 = pt[ i ].GetNorm( );
134 if( n0 > double( 0 ) )
141 double n1 = qt[ i ].GetNorm( );
142 if( n1 > double( 0 ) )
152 t0 /= double( -real0 );
154 t1 /= double( -real1 );
156 t0 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
157 t1 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
159 _TVector p0 = pp[ 0 ] + t0;
160 _TVector p1 = qp[ 0 ] + t1;
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 );
171 this->m_Contour->GetPoints( )->
172 InsertNextPoint( p0[ 0 ], p0[ 1 ], p0[ 2 ] );
173 for( unsigned long i = 0; i < nPoints; ++i )
176 cnt->GetPoints( )->GetPoint( i, p );
177 this->m_Contour->GetPoints( )->InsertNextPoint( p );
180 this->m_Contour->GetPoints( )->
181 InsertNextPoint( p1[ 0 ], p1[ 1 ], p1[ 2 ] );
182 this->m_Contour->Modified( );
185 wdg->GetPolyData( this->m_Contour.GetPointer( ) );
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 )
195 auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
198 auto props = r->GetViewProps( );
201 props->InitTraversal( );
202 while( vtkProp* prop = props->GetNextProp( ) )
204 auto a = dynamic_cast< _TActor* >( prop );
206 if( a->GetImage( ) == image )
221 if( actor == NULL || ren == NULL || iren == NULL )
222 this->_Error( "Invalid actor and/or renderer." );
224 // Widget configuration
225 wdg->SetCurrentRenderer( ren );
226 wdg->SetDefaultRenderer( ren );
227 wdg->SetInputData( image );
228 wdg->SetProp3D( actor );
229 wdg->SetInteractor( iren );
231 image->GetBounds( bnds );
233 bnds[ 0 ], bnds[ 1 ],
234 bnds[ 2 ], bnds[ 3 ],
237 wdg->ProjectToPlaneOn( );
238 wdg->SetProjectionNormalToZAxes( );
239 wdg->SetProjectionPosition(
241 actor->GetBounds( )[ 4 ] +
242 actor->GetBounds( )[ 5 ]
245 wdg->SetHandleSize( 0.005 );
246 wdg->SetNumberOfHandles( 3 );
247 this->m_Configured = true;