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 cpPluginsWidgets::LineWidget::
38 typedef cpPlugins::BaseObjects::DataObject _TData;
39 typedef cpPlugins::DataObjects::Mesh _TMesh;
41 this->_ConfigureInput< _TData >( "Input", false, false );
42 this->_ConfigureOutput< _TMesh >( "Output" );
43 auto line = vtkSmartPointer< vtkPolyData >::New( );
44 line->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
45 line->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
46 line->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
47 line->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
48 line->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
49 this->GetOutput( "Output" )->SetVTK( line );
52 // -------------------------------------------------------------------------
53 cpPluginsWidgets::LineWidget::
58 // -------------------------------------------------------------------------
59 void cpPluginsWidgets::LineWidget::
62 if( this->m_Interactors.size( ) == 0 )
63 this->_Error( "Give at least one valid interactor." );
65 auto image = this->GetInputData< vtkImageData >( "Input" );
66 if( image != NULL ) this->_GD_Image( image );
67 else this->_Error( "Do not know how to create this widget." );
70 // -------------------------------------------------------------------------
71 cpPluginsWidgets::LineWidget::
72 TValidProps cpPluginsWidgets::LineWidget::
73 _GetValidActors( vtkObject* source )
75 TValidProps valid_props;
76 auto iIt = this->m_Interactors.begin( );
77 for( ; iIt != this->m_Interactors.end( ); ++iIt )
79 auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
82 auto props = r->GetViewProps( );
85 props->InitTraversal( );
86 while( vtkProp* prop = props->GetNextProp( ) )
88 auto image_actor = dynamic_cast< vtkImageSlice* >( prop );
89 auto mesh_actor = dynamic_cast< vtkActor* >( prop );
90 vtkObject* input = NULL;
91 if( image_actor != NULL )
93 auto mapper = image_actor->GetMapper( );
95 input = mapper->GetInput( );
97 else if( mesh_actor != NULL )
99 auto mapper = mesh_actor->GetMapper( );
101 input = mapper->GetInput( );
105 valid_props[ *iIt ].insert( prop );
114 return( valid_props );
117 // -------------------------------------------------------------------------
118 void cpPluginsWidgets::LineWidget::
119 _GD_Image( vtkImageData* image )
121 auto valid_props = this->_GetValidActors( image );
122 if( valid_props.size( ) == 0 )
123 this->_Error( "Given image does not have a valid associated actor." );
125 vtkSmartPointer< vtkLineWidget2 > wdg = this->GetVTK< vtkLineWidget2 >( );
126 if( wdg.GetPointer( ) == NULL )
128 auto vIt = valid_props.begin( );
129 auto actor = dynamic_cast< vtkImageSlice* >( *( vIt->second.begin( ) ) );
132 actor->GetBounds( bnds );
133 double p0[ 3 ], p1[ 3 ];
141 auto rep = vtkSmartPointer< vtkLineRepresentation >::New( );
142 rep->SetPoint1WorldPosition( p0 );
143 rep->SetPoint2WorldPosition( p1 );
144 rep->PlaceWidget( bnds );
146 wdg = this->_CreateVTK< vtkLineWidget2 >( );
147 wdg->SetInteractor( valid_props.begin( )->first );
148 wdg->SetRepresentation( rep );
152 dynamic_cast< vtkLineRepresentation* >( wdg->GetRepresentation( ) )->
153 GetPolyData( this->GetOutputData< vtkPolyData >( "Output" ) );