]> Creatis software - cpPlugins.git/blob - plugins/VTKWidgets/LineWidget.cxx
yet another refactoring
[cpPlugins.git] / plugins / VTKWidgets / LineWidget.cxx
1 #include <VTKWidgets/LineWidget.h>
2 #include <cpInstances/DataObjects/Image.h>
3 #include <cpInstances/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 <vtkPolyData.h>
13 #include <vtkRenderer.h>
14
15 // -------------------------------------------------------------------------
16 void cpPluginsVTKWidgets::LineWidget::
17 Clear( )
18 {
19 }
20
21 // -------------------------------------------------------------------------
22 void cpPluginsVTKWidgets::LineWidget::
23 SetEnabled( bool v )
24 {
25   auto wdg = this->GetVTK< vtkLineWidget2 >( );
26   if( wdg != NULL )
27   {
28     wdg->SetEnabled( v );
29     wdg->GetInteractor( )->Render( );
30
31   } // fi
32 }
33
34 // -------------------------------------------------------------------------
35 bool cpPluginsVTKWidgets::LineWidget::
36 GetEnabled( ) const
37 {
38   auto wdg = this->GetVTK< const vtkLineWidget2 >( );
39   if( wdg != NULL )
40   {
41     vtkLineWidget2* w = const_cast< vtkLineWidget2* >( wdg );
42     return( w->GetEnabled( ) != 0 );
43   }
44   else
45     return( false );
46 }
47
48 // -------------------------------------------------------------------------
49 cpPluginsVTKWidgets::LineWidget::
50 LineWidget( )
51   : Superclass( )
52 {
53   typedef cpPlugins::Pipeline::DataObject _TData;
54   typedef cpInstances::DataObjects::Mesh       _TMesh;
55
56   this->_ConfigureInput< _TData >( "Input", false, false );
57   this->_ConfigureOutput< _TMesh >( "Output" );
58
59   // Create output data
60   auto line = this->_CreateVTK< vtkPolyData >( );
61   line->SetPoints( vtkSmartPointer< vtkPoints >::New( ) );
62   line->SetVerts( vtkSmartPointer< vtkCellArray >::New( ) );
63   line->SetLines( vtkSmartPointer< vtkCellArray >::New( ) );
64   line->SetPolys( vtkSmartPointer< vtkCellArray >::New( ) );
65   line->SetStrips( vtkSmartPointer< vtkCellArray >::New( ) );
66   this->GetOutput( "Output" )->SetVTK( line );
67 }
68
69 // -------------------------------------------------------------------------
70 cpPluginsVTKWidgets::LineWidget::
71 ~LineWidget( )
72 {
73 }
74
75 // -------------------------------------------------------------------------
76 void cpPluginsVTKWidgets::LineWidget::
77 _GenerateData( )
78 {
79   if( this->m_Interactors.size( ) == 0 )
80     this->_Error( "Give at least one valid interactor." );
81
82   auto image = this->GetInputData< vtkImageData >( "Input" );
83   if( image != NULL ) this->_GD_Image( image );
84   else this->_Error( "Do not know how to create this widget." );
85 }
86
87 // -------------------------------------------------------------------------
88 cpPluginsVTKWidgets::LineWidget::
89 TValidProps cpPluginsVTKWidgets::LineWidget::
90 _GetValidActors( vtkObject* source )
91 {
92   TValidProps valid_props;
93   auto iIt = this->m_Interactors.begin( );
94   for( ; iIt != this->m_Interactors.end( ); ++iIt )
95   {
96     auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
97     if( r != NULL )
98     {
99       auto props = r->GetViewProps( );
100       if( props != NULL )
101       {
102         props->InitTraversal( );
103         while( vtkProp* prop = props->GetNextProp( ) )
104         {
105           auto image_actor = dynamic_cast< vtkImageSlice* >( prop );
106           auto mesh_actor = dynamic_cast< vtkActor* >( prop );
107           vtkObject* input = NULL;
108           if( image_actor != NULL )
109           {
110             auto mapper = image_actor->GetMapper( );
111             if( mapper != NULL )
112               input = mapper->GetInput( );
113           }
114           else if( mesh_actor != NULL )
115           {
116             auto mapper = mesh_actor->GetMapper( );
117             if( mapper != NULL )
118               input = mapper->GetInput( );
119
120           } // fi
121           if( input != NULL )
122             valid_props[ *iIt ].insert( prop );
123
124         } // elihw
125
126       } // fi
127
128     } // fi
129
130   } // rof
131   return( valid_props );
132 }
133
134 // -------------------------------------------------------------------------
135 void cpPluginsVTKWidgets::LineWidget::
136 _GD_Image( vtkImageData* image )
137 {
138   auto valid_props = this->_GetValidActors( image );
139   if( valid_props.size( ) == 0 )
140     this->_Error( "Given image does not have a valid associated actor." );
141
142   vtkSmartPointer< vtkLineWidget2 > wdg = this->GetVTK< vtkLineWidget2 >( );
143   if( wdg.GetPointer( ) == NULL )
144   {
145     auto vIt = valid_props.begin( );
146     auto actor = dynamic_cast< vtkImageSlice* >( *( vIt->second.begin( ) ) );
147
148     double bnds[ 6 ];
149     actor->GetBounds( bnds );
150     double p0[ 3 ], p1[ 3 ];
151     p0[ 0 ] = bnds[ 0 ];
152     p0[ 1 ] = bnds[ 2 ];
153     p0[ 2 ] = bnds[ 4 ];
154     p1[ 0 ] = bnds[ 1 ];
155     p1[ 1 ] = bnds[ 3 ];
156     p1[ 2 ] = bnds[ 5 ];
157
158     auto rep = vtkSmartPointer< vtkLineRepresentation >::New( );
159     rep->SetPoint1WorldPosition( p0 );
160     rep->SetPoint2WorldPosition( p1 );
161     rep->PlaceWidget( bnds );
162
163     wdg = this->_CreateVTK< vtkLineWidget2 >( );
164     wdg->SetInteractor( valid_props.begin( )->first );
165     wdg->SetRepresentation( rep );
166     wdg->EnabledOn( );
167   }
168   else
169     dynamic_cast< vtkLineRepresentation* >( wdg->GetRepresentation( ) )->
170       GetPolyData( this->GetOutputData< vtkPolyData >( "Output" ) );
171 }
172
173 // eof - $RCSfile$