1 #include <VTKWidgets/LineWidget.h>
2 #include <cpInstances/DataObjects/Image.h>
3 #include <cpInstances/DataObjects/Mesh.h>
4 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
6 #include <vtkLineWidget2.h>
7 #include <vtkImageData.h>
8 #include <vtkImageMapper3D.h>
9 #include <vtkInteractorObserver.h>
10 #include <vtkLineRepresentation.h>
11 #include <vtkMapper.h>
12 #include <vtkPolyData.h>
13 #include <vtkRenderer.h>
15 // -------------------------------------------------------------------------
16 void cpPluginsVTKWidgets::LineWidget::
21 // -------------------------------------------------------------------------
22 void cpPluginsVTKWidgets::LineWidget::
25 auto wdg = this->GetVTK< vtkLineWidget2 >( );
29 wdg->GetInteractor( )->Render( );
34 // -------------------------------------------------------------------------
35 bool cpPluginsVTKWidgets::LineWidget::
38 auto wdg = this->GetVTK< const vtkLineWidget2 >( );
41 vtkLineWidget2* w = const_cast< vtkLineWidget2* >( wdg );
42 return( w->GetEnabled( ) != 0 );
48 // -------------------------------------------------------------------------
49 cpPluginsVTKWidgets::LineWidget::
53 typedef cpPlugins::Pipeline::DataObject _TData;
54 typedef cpInstances::DataObjects::Mesh _TMesh;
56 this->_ConfigureInput< _TData >( "Input", false, false );
57 this->_ConfigureOutput< _TMesh >( "Output" );
60 auto line = this->_CreateVTK< vtkPolyData >( );
61 line->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
62 line->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
63 line->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
64 line->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
65 line->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
66 this->GetOutput( "Output" )->SetVTK( line );
69 // -------------------------------------------------------------------------
70 cpPluginsVTKWidgets::LineWidget::
75 // -------------------------------------------------------------------------
76 void cpPluginsVTKWidgets::LineWidget::
79 if( this->m_Interactors.size( ) == 0 )
80 this->_Error( "Give at least one valid interactor." );
82 auto image = this->GetInputData< vtkImageData >( "Input" );
83 if( image != NULL ) this->_GD_Image( image );
84 else this->_Error( "Do not know how to create this widget." );
87 // -------------------------------------------------------------------------
88 cpPluginsVTKWidgets::LineWidget::
89 TValidProps cpPluginsVTKWidgets::LineWidget::
90 _GetValidActors( vtkObject* source )
92 TValidProps valid_props;
93 auto iIt = this->m_Interactors.begin( );
94 for( ; iIt != this->m_Interactors.end( ); ++iIt )
96 auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
99 auto props = r->GetViewProps( );
102 props->InitTraversal( );
103 while( vtkProp* prop = props->GetNextProp( ) )
105 auto image_actor = dynamic_cast< vtkImageSlice* >( prop );
106 auto mesh_actor = dynamic_cast< vtkActor* >( prop );
107 vtkObject* input = NULL;
108 if( image_actor != NULL )
110 auto mapper = image_actor->GetMapper( );
112 input = mapper->GetInput( );
114 else if( mesh_actor != NULL )
116 auto mapper = mesh_actor->GetMapper( );
118 input = mapper->GetInput( );
122 valid_props[ *iIt ].insert( prop );
131 return( valid_props );
134 // -------------------------------------------------------------------------
135 void cpPluginsVTKWidgets::LineWidget::
136 _GD_Image( vtkImageData* image )
138 auto valid_props = this->_GetValidActors( image );
139 if( valid_props.size( ) == 0 )
140 this->_Error( "Given image does not have a valid associated actor." );
142 vtkSmartPointer< vtkLineWidget2 > wdg = this->GetVTK< vtkLineWidget2 >( );
143 if( wdg.GetPointer( ) == NULL )
145 auto vIt = valid_props.begin( );
146 auto actor = dynamic_cast< vtkImageSlice* >( *( vIt->second.begin( ) ) );
149 actor->GetBounds( bnds );
150 double p0[ 3 ], p1[ 3 ];
158 auto rep = vtkSmartPointer< vtkLineRepresentation >::New( );
159 rep->SetPoint1WorldPosition( p0 );
160 rep->SetPoint2WorldPosition( p1 );
161 rep->PlaceWidget( bnds );
163 wdg = this->_CreateVTK< vtkLineWidget2 >( );
164 wdg->SetInteractor( valid_props.begin( )->first );
165 wdg->SetRepresentation( rep );
169 dynamic_cast< vtkLineRepresentation* >( wdg->GetRepresentation( ) )->
170 GetPolyData( this->GetOutputData< vtkPolyData >( "Output" ) );