1 #include <VTKWidgets/LineWidget.h>
2 #include <cpInstances/Image.h>
3 #include <cpInstances/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::BaseObjects::DataObject _TData;
54 typedef cpInstances::Mesh _TMesh;
56 this->_ConfigureInput< _TData >( "Input", false, false );
57 this->_ConfigureOutput< _TMesh >( "Output" );
58 auto line = vtkSmartPointer< vtkPolyData >::New( );
59 line->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
60 line->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
61 line->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
62 line->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
63 line->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
64 this->GetOutput( "Output" )->SetVTK( line );
67 // -------------------------------------------------------------------------
68 cpPluginsVTKWidgets::LineWidget::
73 // -------------------------------------------------------------------------
74 void cpPluginsVTKWidgets::LineWidget::
77 if( this->m_Interactors.size( ) == 0 )
78 this->_Error( "Give at least one valid interactor." );
80 auto image = this->GetInputData< vtkImageData >( "Input" );
81 if( image != NULL ) this->_GD_Image( image );
82 else this->_Error( "Do not know how to create this widget." );
85 // -------------------------------------------------------------------------
86 cpPluginsVTKWidgets::LineWidget::
87 TValidProps cpPluginsVTKWidgets::LineWidget::
88 _GetValidActors( vtkObject* source )
90 TValidProps valid_props;
91 auto iIt = this->m_Interactors.begin( );
92 for( ; iIt != this->m_Interactors.end( ); ++iIt )
94 auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
97 auto props = r->GetViewProps( );
100 props->InitTraversal( );
101 while( vtkProp* prop = props->GetNextProp( ) )
103 auto image_actor = dynamic_cast< vtkImageSlice* >( prop );
104 auto mesh_actor = dynamic_cast< vtkActor* >( prop );
105 vtkObject* input = NULL;
106 if( image_actor != NULL )
108 auto mapper = image_actor->GetMapper( );
110 input = mapper->GetInput( );
112 else if( mesh_actor != NULL )
114 auto mapper = mesh_actor->GetMapper( );
116 input = mapper->GetInput( );
120 valid_props[ *iIt ].insert( prop );
129 return( valid_props );
132 // -------------------------------------------------------------------------
133 void cpPluginsVTKWidgets::LineWidget::
134 _GD_Image( vtkImageData* image )
136 auto valid_props = this->_GetValidActors( image );
137 if( valid_props.size( ) == 0 )
138 this->_Error( "Given image does not have a valid associated actor." );
140 vtkSmartPointer< vtkLineWidget2 > wdg = this->GetVTK< vtkLineWidget2 >( );
141 if( wdg.GetPointer( ) == NULL )
143 auto vIt = valid_props.begin( );
144 auto actor = dynamic_cast< vtkImageSlice* >( *( vIt->second.begin( ) ) );
147 actor->GetBounds( bnds );
148 double p0[ 3 ], p1[ 3 ];
156 auto rep = vtkSmartPointer< vtkLineRepresentation >::New( );
157 rep->SetPoint1WorldPosition( p0 );
158 rep->SetPoint2WorldPosition( p1 );
159 rep->PlaceWidget( bnds );
161 wdg = this->_CreateVTK< vtkLineWidget2 >( );
162 wdg->SetInteractor( valid_props.begin( )->first );
163 wdg->SetRepresentation( rep );
167 dynamic_cast< vtkLineRepresentation* >( wdg->GetRepresentation( ) )->
168 GetPolyData( this->GetOutputData< vtkPolyData >( "Output" ) );