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::
26 // -------------------------------------------------------------------------
27 cpPluginsWidgets::LineWidget::
31 typedef cpPlugins::BaseObjects::DataObject _TData;
32 typedef cpPlugins::DataObjects::Mesh _TMesh;
34 this->_ConfigureInput< _TData >( "Input", false, false );
35 this->_ConfigureOutput< _TMesh >( "Output" );
36 auto line = vtkSmartPointer< vtkPolyData >::New( );
37 line->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
38 line->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
39 line->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
40 line->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
41 line->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
42 this->GetOutput( "Output" )->SetVTK( line );
45 // -------------------------------------------------------------------------
46 cpPluginsWidgets::LineWidget::
51 // -------------------------------------------------------------------------
52 void cpPluginsWidgets::LineWidget::
55 if( this->m_Interactors.size( ) == 0 )
56 this->_Error( "Give at least one valid interactor." );
58 auto image = this->GetInputData< vtkImageData >( "Input" );
59 if( image != NULL ) this->_GD_Image( image );
60 else this->_Error( "Do not know how to create this widget." );
63 // -------------------------------------------------------------------------
64 cpPluginsWidgets::LineWidget::
65 TValidProps cpPluginsWidgets::LineWidget::
66 _GetValidActors( vtkObject* source )
68 TValidProps valid_props;
69 auto iIt = this->m_Interactors.begin( );
70 for( ; iIt != this->m_Interactors.end( ); ++iIt )
72 auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
75 auto props = r->GetViewProps( );
78 props->InitTraversal( );
79 while( vtkProp* prop = props->GetNextProp( ) )
81 auto image_actor = dynamic_cast< vtkImageSlice* >( prop );
82 auto mesh_actor = dynamic_cast< vtkActor* >( prop );
83 vtkObject* input = NULL;
84 if( image_actor != NULL )
86 auto mapper = image_actor->GetMapper( );
88 input = mapper->GetInput( );
90 else if( mesh_actor != NULL )
92 auto mapper = mesh_actor->GetMapper( );
94 input = mapper->GetInput( );
98 valid_props[ *iIt ].insert( prop );
107 return( valid_props );
110 // -------------------------------------------------------------------------
111 void cpPluginsWidgets::LineWidget::
112 _GD_Image( vtkImageData* image )
114 auto valid_props = this->_GetValidActors( image );
115 if( valid_props.size( ) == 0 )
116 this->_Error( "Given image does not have a valid associated actor." );
118 vtkSmartPointer< vtkLineWidget2 > wdg = this->GetVTK< vtkLineWidget2 >( );
119 if( wdg.GetPointer( ) == NULL )
121 auto rep = vtkSmartPointer< vtkLineRepresentation >::New( );
124 p[ 0 ] = 0.0; p[ 1 ] = -1.0; p[ 2 ] = 0.0;
125 rep->SetPoint1WorldPosition( p );
126 p[ 0 ] = 0.0; p[ 1 ] = 1.0; p[ 2 ] = 0.0;
127 rep->SetPoint2WorldPosition( p );
128 rep->PlaceWidget( pl3d_block0->GetBounds( ) );
129 rep->GetPolyData( seeds );
130 rep->DistanceAnnotationVisibilityOn( );
133 wdg = this->_CreateVTK< vtkLineWidget2 >( );
134 wdg->SetInteractor( valid_props.begin( )->first );
135 wdg->SetRepresentation( rep );
139 dynamic_cast< vtkLineRepresentation* >( wdg->GetRepresentation( ) )->
140 GetPolyData( this->GetOutputData< vtkPolyData >( "Output" ) );