1 #include <plugins/Widgets/LineWidget.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/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 <vtkRenderer.h>
14 // -------------------------------------------------------------------------
15 void cpPluginsWidgets::LineWidget::
20 // -------------------------------------------------------------------------
21 void cpPluginsWidgets::LineWidget::
24 auto wdg = this->GetVTK< vtkLineWidget2 >( );
28 wdg->GetInteractor( )->Render( );
33 // -------------------------------------------------------------------------
34 bool cpPluginsWidgets::LineWidget::
37 auto wdg = this->GetVTK< const vtkLineWidget2 >( );
40 vtkLineWidget2* w = const_cast< vtkLineWidget2* >( wdg );
41 return( w->GetEnabled( ) != 0 );
47 // -------------------------------------------------------------------------
48 cpPluginsWidgets::LineWidget::
52 typedef cpPlugins::BaseObjects::DataObject _TData;
53 typedef cpPlugins::DataObjects::Mesh _TMesh;
55 this->_ConfigureInput< _TData >( "Input", false, false );
56 this->_ConfigureOutput< _TMesh >( "Output" );
57 auto line = vtkSmartPointer< vtkPolyData >::New( );
58 line->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
59 line->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
60 line->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
61 line->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
62 line->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
63 this->GetOutput( "Output" )->SetVTK( line );
66 // -------------------------------------------------------------------------
67 cpPluginsWidgets::LineWidget::
72 // -------------------------------------------------------------------------
73 void cpPluginsWidgets::LineWidget::
76 if( this->m_Interactors.size( ) == 0 )
77 this->_Error( "Give at least one valid interactor." );
79 auto image = this->GetInputData< vtkImageData >( "Input" );
80 if( image != NULL ) this->_GD_Image( image );
81 else this->_Error( "Do not know how to create this widget." );
84 // -------------------------------------------------------------------------
85 cpPluginsWidgets::LineWidget::
86 TValidProps cpPluginsWidgets::LineWidget::
87 _GetValidActors( vtkObject* source )
89 TValidProps valid_props;
90 auto iIt = this->m_Interactors.begin( );
91 for( ; iIt != this->m_Interactors.end( ); ++iIt )
93 auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
96 auto props = r->GetViewProps( );
99 props->InitTraversal( );
100 while( vtkProp* prop = props->GetNextProp( ) )
102 auto image_actor = dynamic_cast< vtkImageSlice* >( prop );
103 auto mesh_actor = dynamic_cast< vtkActor* >( prop );
104 vtkObject* input = NULL;
105 if( image_actor != NULL )
107 auto mapper = image_actor->GetMapper( );
109 input = mapper->GetInput( );
111 else if( mesh_actor != NULL )
113 auto mapper = mesh_actor->GetMapper( );
115 input = mapper->GetInput( );
119 valid_props[ *iIt ].insert( prop );
128 return( valid_props );
131 // -------------------------------------------------------------------------
132 void cpPluginsWidgets::LineWidget::
133 _GD_Image( vtkImageData* image )
135 auto valid_props = this->_GetValidActors( image );
136 if( valid_props.size( ) == 0 )
137 this->_Error( "Given image does not have a valid associated actor." );
139 vtkSmartPointer< vtkLineWidget2 > wdg = this->GetVTK< vtkLineWidget2 >( );
140 if( wdg.GetPointer( ) == NULL )
142 auto vIt = valid_props.begin( );
143 auto actor = dynamic_cast< vtkImageSlice* >( *( vIt->second.begin( ) ) );
146 actor->GetBounds( bnds );
147 double p0[ 3 ], p1[ 3 ];
155 auto rep = vtkSmartPointer< vtkLineRepresentation >::New( );
156 rep->SetPoint1WorldPosition( p0 );
157 rep->SetPoint2WorldPosition( p1 );
158 rep->PlaceWidget( bnds );
160 wdg = this->_CreateVTK< vtkLineWidget2 >( );
161 wdg->SetInteractor( valid_props.begin( )->first );
162 wdg->SetRepresentation( rep );
166 dynamic_cast< vtkLineRepresentation* >( wdg->GetRepresentation( ) )->
167 GetPolyData( this->GetOutputData< vtkPolyData >( "Output" ) );