]> Creatis software - cpPlugins.git/blob - plugins/Widgets/LineWidget.cxx
f73e100ae28645067dad3862eabe5ad5840cd6b0
[cpPlugins.git] / plugins / Widgets / LineWidget.cxx
1 #include <plugins/Widgets/LineWidget.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/DataObjects/Mesh.h>
4 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
5
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>
13
14 // -------------------------------------------------------------------------
15 void cpPluginsWidgets::LineWidget::
16 Clear( )
17 {
18 }
19
20 // -------------------------------------------------------------------------
21 void cpPluginsWidgets::LineWidget::
22 SetEnabled( bool v )
23 {
24 }
25
26 // -------------------------------------------------------------------------
27 cpPluginsWidgets::LineWidget::
28 LineWidget( )
29   : Superclass( )
30 {
31   typedef cpPlugins::BaseObjects::DataObject _TData;
32   typedef cpPlugins::DataObjects::Mesh       _TMesh;
33
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 );
43 }
44
45 // -------------------------------------------------------------------------
46 cpPluginsWidgets::LineWidget::
47 ~LineWidget( )
48 {
49 }
50
51 // -------------------------------------------------------------------------
52 void cpPluginsWidgets::LineWidget::
53 _GenerateData( )
54 {
55   if( this->m_Interactors.size( ) == 0 )
56     this->_Error( "Give at least one valid interactor." );
57
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." );
61 }
62
63 // -------------------------------------------------------------------------
64 cpPluginsWidgets::LineWidget::
65 TValidProps cpPluginsWidgets::LineWidget::
66 _GetValidActors( vtkObject* source )
67 {
68   TValidProps valid_props;
69   auto iIt = this->m_Interactors.begin( );
70   for( ; iIt != this->m_Interactors.end( ); ++iIt )
71   {
72     auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
73     if( r != NULL )
74     {
75       auto props = r->GetViewProps( );
76       if( props != NULL )
77       {
78         props->InitTraversal( );
79         while( vtkProp* prop = props->GetNextProp( ) )
80         {
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 )
85           {
86             auto mapper = image_actor->GetMapper( );
87             if( mapper != NULL )
88               input = mapper->GetInput( );
89           }
90           else if( mesh_actor != NULL )
91           {
92             auto mapper = mesh_actor->GetMapper( );
93             if( mapper != NULL )
94               input = mapper->GetInput( );
95
96           } // fi
97           if( input != NULL )
98             valid_props[ *iIt ].insert( prop );
99
100         } // elihw
101
102       } // fi
103
104     } // fi
105
106   } // rof
107   return( valid_props );
108 }
109
110 // -------------------------------------------------------------------------
111 void cpPluginsWidgets::LineWidget::
112 _GD_Image( vtkImageData* image )
113 {
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." );
117
118   vtkSmartPointer< vtkLineWidget2 > wdg = this->GetVTK< vtkLineWidget2 >( );
119   if( wdg.GetPointer( ) == NULL )
120   {
121     auto rep = vtkSmartPointer< vtkLineRepresentation >::New( );
122
123     /* TODO
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( );
131     */
132
133     wdg = this->_CreateVTK< vtkLineWidget2 >( );
134     wdg->SetInteractor( valid_props.begin( )->first );
135     wdg->SetRepresentation( rep );
136     wdg->EnabledOn( );
137   }
138   else
139     dynamic_cast< vtkLineRepresentation* >( wdg->GetRepresentation( ) )->
140       GetPolyData( this->GetOutputData< vtkPolyData >( "Output" ) );
141 }
142
143 // eof - $RCSfile$