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