]> Creatis software - cpPlugins.git/blob - plugins/Widgets/LineWidget.cxx
...
[cpPlugins.git] / plugins / Widgets / LineWidget.cxx
1 #include <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 <vtkPolyData.h>
13 #include <vtkRenderer.h>
14
15 // -------------------------------------------------------------------------
16 void cpPluginsWidgets::LineWidget::
17 Clear( )
18 {
19 }
20
21 // -------------------------------------------------------------------------
22 void cpPluginsWidgets::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 cpPluginsWidgets::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 cpPluginsWidgets::LineWidget::
50 LineWidget( )
51   : Superclass( )
52 {
53   typedef cpPlugins::BaseObjects::DataObject _TData;
54   typedef cpPlugins::DataObjects::Mesh       _TMesh;
55
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 );
65 }
66
67 // -------------------------------------------------------------------------
68 cpPluginsWidgets::LineWidget::
69 ~LineWidget( )
70 {
71 }
72
73 // -------------------------------------------------------------------------
74 void cpPluginsWidgets::LineWidget::
75 _GenerateData( )
76 {
77   if( this->m_Interactors.size( ) == 0 )
78     this->_Error( "Give at least one valid interactor." );
79
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." );
83 }
84
85 // -------------------------------------------------------------------------
86 cpPluginsWidgets::LineWidget::
87 TValidProps cpPluginsWidgets::LineWidget::
88 _GetValidActors( vtkObject* source )
89 {
90   TValidProps valid_props;
91   auto iIt = this->m_Interactors.begin( );
92   for( ; iIt != this->m_Interactors.end( ); ++iIt )
93   {
94     auto r = ( *iIt )->GetInteractorStyle( )->GetCurrentRenderer( );
95     if( r != NULL )
96     {
97       auto props = r->GetViewProps( );
98       if( props != NULL )
99       {
100         props->InitTraversal( );
101         while( vtkProp* prop = props->GetNextProp( ) )
102         {
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 )
107           {
108             auto mapper = image_actor->GetMapper( );
109             if( mapper != NULL )
110               input = mapper->GetInput( );
111           }
112           else if( mesh_actor != NULL )
113           {
114             auto mapper = mesh_actor->GetMapper( );
115             if( mapper != NULL )
116               input = mapper->GetInput( );
117
118           } // fi
119           if( input != NULL )
120             valid_props[ *iIt ].insert( prop );
121
122         } // elihw
123
124       } // fi
125
126     } // fi
127
128   } // rof
129   return( valid_props );
130 }
131
132 // -------------------------------------------------------------------------
133 void cpPluginsWidgets::LineWidget::
134 _GD_Image( vtkImageData* image )
135 {
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." );
139
140   vtkSmartPointer< vtkLineWidget2 > wdg = this->GetVTK< vtkLineWidget2 >( );
141   if( wdg.GetPointer( ) == NULL )
142   {
143     auto vIt = valid_props.begin( );
144     auto actor = dynamic_cast< vtkImageSlice* >( *( vIt->second.begin( ) ) );
145
146     double bnds[ 6 ];
147     actor->GetBounds( bnds );
148     double p0[ 3 ], p1[ 3 ];
149     p0[ 0 ] = bnds[ 0 ];
150     p0[ 1 ] = bnds[ 2 ];
151     p0[ 2 ] = bnds[ 4 ];
152     p1[ 0 ] = bnds[ 1 ];
153     p1[ 1 ] = bnds[ 3 ];
154     p1[ 2 ] = bnds[ 5 ];
155
156     auto rep = vtkSmartPointer< vtkLineRepresentation >::New( );
157     rep->SetPoint1WorldPosition( p0 );
158     rep->SetPoint2WorldPosition( p1 );
159     rep->PlaceWidget( bnds );
160
161     wdg = this->_CreateVTK< vtkLineWidget2 >( );
162     wdg->SetInteractor( valid_props.begin( )->first );
163     wdg->SetRepresentation( rep );
164     wdg->EnabledOn( );
165   }
166   else
167     dynamic_cast< vtkLineRepresentation* >( wdg->GetRepresentation( ) )->
168       GetPolyData( this->GetOutputData< vtkPolyData >( "Output" ) );
169 }
170
171 // eof - $RCSfile$