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