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( );
98 for( unsigned long i = 0; i < nPoints; ++i )
100 double p[ 3 ], q[ 3 ];
101 this->m_Contour->GetPoint( i, p );
102 this->m_Contour->GetPoint( ( i + 1 ) % nPoints, q );
104 area += ( p[ 0 ] * q[ 1 ] ) - ( q[ 0 ] - p[ 1 ] );
107 std::cout << "Area: " << area << std::endl;
109 unsigned long support = nPoints / 10;
111 for( unsigned long i = 0; i < support; ++i )
113 double p[ 3 ], q[ 3 ];
114 this->m_Contour->GetPoint( i, p );
115 this->m_Contour->GetPoint( nPoints - 1 - i, q );
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 )
134 pt.push_back( pp[ i + 1 ] - pp[ i - 1 ] );
135 qt.push_back( qp[ i + 1 ] - qp[ i - 1 ] );
138 pt.push_back( pp[ support - 1 ] - pp[ support - 2 ] );
139 qt.push_back( qp[ support - 1 ] - qp[ support - 2 ] );
141 _TVector t0( double( 0 ) ), t1( double( 0 ) );
142 long real0 = 0, real1 = 0;
143 for( unsigned long i = 0; i < support; ++i )
145 double n0 = pt[ i ].GetNorm( );
146 if( n0 > double( 0 ) )
153 double n1 = qt[ i ].GetNorm( );
154 if( n1 > double( 0 ) )
164 t0 /= double( -real0 );
166 t1 /= double( -real1 );
168 t0 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
169 t1 *= ( pp[ 0 ] - qp[ 0 ] ).GetNorm( );
171 _TVector p0 = pp[ 0 ] + t0;
172 _TVector p1 = qp[ 0 ] + t1;
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 );
183 this->m_Contour->GetPoints( )->
184 InsertNextPoint( p0[ 0 ], p0[ 1 ], p0[ 2 ] );
185 for( unsigned long i = 0; i < nPoints; ++i )
188 cnt->GetPoints( )->GetPoint( i, p );
189 this->m_Contour->GetPoints( )->InsertNextPoint( p );
192 this->m_Contour->GetPoints( )->
193 InsertNextPoint( p1[ 0 ], p1[ 1 ], p1[ 2 ] );
194 this->m_Contour->Modified( );
197 wdg->GetPolyData( this->m_Contour.GetPointer( ) );
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 )
207 auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
210 auto props = r->GetViewProps( );
213 props->InitTraversal( );
214 while( vtkProp* prop = props->GetNextProp( ) )
216 auto a = dynamic_cast< _TActor* >( prop );
218 if( a->GetImage( ) == image )
233 if( actor == NULL || ren == NULL || iren == NULL )
234 this->_Error( "Invalid actor and/or renderer." );
236 // Widget configuration
237 wdg->SetCurrentRenderer( ren );
238 wdg->SetDefaultRenderer( ren );
239 wdg->SetInputData( image );
240 wdg->SetProp3D( actor );
241 wdg->SetInteractor( iren );
243 image->GetBounds( bnds );
245 bnds[ 0 ], bnds[ 1 ],
246 bnds[ 2 ], bnds[ 3 ],
249 wdg->ProjectToPlaneOn( );
250 wdg->SetProjectionNormalToZAxes( );
251 wdg->SetProjectionPosition(
253 actor->GetBounds( )[ 4 ] +
254 actor->GetBounds( )[ 5 ]
257 wdg->SetHandleSize( 0.005 );
258 wdg->SetNumberOfHandles( 3 );
259 this->m_Configured = true;