]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/QT/ImageWidget.cxx
yet another refactoring
[cpPlugins.git] / lib / cpExtensions / QT / ImageWidget.cxx
1 #include <cpExtensions/QT/ImageWidget.h>
2 #include <cpExtensions/Interaction/ImageSliceStyle.h>
3 #include <cpExtensions/Visualization/LUTImageActor.h>
4 #include <cpExtensions/Visualization/MeshActor.h>
5 #include <cpExtensions/Visualization/OutlineSource.h>
6 #include <cpExtensions/Visualization/WindowLevelImageActor.h>
7
8 #include <vtkActor.h>
9 #include <vtkCamera.h>
10 #include <vtkImageData.h>
11 #include <vtkImageProperty.h>
12 #include <vtkProperty.h>
13 #include <vtkRenderer.h>
14
15 // -------------------------------------------------------------------------
16 cpExtensions::QT::ImageWidget::
17 ImageWidget( QWidget* parent, Qt::WindowFlags f )
18   : Superclass( parent, f ),
19     m_ImageName( "" ),
20     m_OutlineActor( NULL )
21 {
22   this->m_Style = vtkSmartPointer< TStyle >::New( );
23   this->m_Style->SetCurrentRenderer( this->m_Renderer );
24   this->SetStyle( this->m_Style );
25 }
26
27 // -------------------------------------------------------------------------
28 cpExtensions::QT::ImageWidget::
29 ~ImageWidget( )
30 {
31   this->Clear( );
32   if( this->m_OutlineActor != NULL )
33     delete this->m_OutlineActor;
34   for( auto a = this->m_Actors.begin( ); a != this->m_Actors.end( ); ++a )
35     delete *a;
36   this->m_Actors.clear( );
37 }
38
39 // -------------------------------------------------------------------------
40 void cpExtensions::QT::ImageWidget::
41 Clear( )
42 {
43   this->RemoveViewProps( );
44   this->m_ImageName = "";
45 }
46
47 // -------------------------------------------------------------------------
48 void cpExtensions::QT::ImageWidget::
49 SetImage( vtkImageData* image, const std::string& name, int orientation )
50 {
51   if( name == "" )
52     return;
53   if( this->m_ImageName != "" )
54     this->Clear( );
55   this->m_ImageName = name;
56
57   this->m_WLActor = vtkSmartPointer< TWLActor >::New( );
58   this->m_WLActor->SetImage( image );
59   this->m_WLActor->SetOrientation( orientation );
60
61   this->m_LUTActor = vtkSmartPointer< TLUTActor >::New( );
62   this->m_LUTActor->SetOrientation( orientation );
63
64   this->m_Outline = vtkSmartPointer< TOutline >::New( );
65   this->m_Outline->SetBounds( image->GetBounds( ) );
66   this->m_Outline->Update( );
67
68   if( this->m_OutlineActor != NULL )
69     delete this->m_OutlineActor;
70   this->m_OutlineActor = new TActor( );
71   this->m_OutlineActor->SetMesh( this->m_Outline->GetOutput( ) );
72
73   double cr = double( 0 );
74   double cg = double( 0 );
75   double cb = double( 0 );
76   switch( this->m_WLActor->GetOrientation( ) )
77   {
78   case 0:  cr = double( 1 ); break;
79   case 1:  cg = double( 1 ); break;
80   case 2:  cb = double( 1 ); break;
81   default: cr = double( 1 ); break;
82   } // hctiws
83   this->m_OutlineActor->GetActor( )->GetProperty( )->SetColor( cr, cg, cb );
84
85   this->AddViewProp( this->m_WLActor, this->m_ImageName );
86   this->AddAuxViewProp( this->m_OutlineActor->GetActor( ), this->m_ImageName );
87   this->ResetCamera( );
88 }
89
90 // -------------------------------------------------------------------------
91 void cpExtensions::QT::ImageWidget::
92 Add( vtkDataSet* data, const std::string& name )
93 {
94   auto image = dynamic_cast< vtkImageData* >( data );
95   auto pdata = dynamic_cast< vtkPolyData* >( data );
96   if( image != NULL )
97   {
98     this->m_LUTActor->AddImage( image );
99     this->m_LUTActor->GetProperty( )->SetOpacity( 0.5 );
100     this->AddViewProp( this->m_LUTActor, "__LUT_IMAGE__" );
101     this->Render( );
102   }
103   else if( pdata != NULL )
104   {
105     TActor* actor = new TActor( );
106     actor->SetMesh( pdata );
107     this->m_Actors.push_back( actor );
108     this->AddViewProp( actor->GetActor( ), name );
109     this->Render( );
110
111   } // fi
112 }
113
114 // -------------------------------------------------------------------------
115 void cpExtensions::QT::ImageWidget::
116 ResetCamera( )
117 {
118   if( this->m_WLActor.GetPointer( ) != NULL )
119   {
120     auto image = this->m_WLActor->GetImage( );
121     if( image != NULL )
122     {
123       double bounds[ 6 ];
124       image->GetBounds( bounds );
125
126       // Compute camera properties
127       double center[ 3 ];
128       center[ 0 ] = ( bounds[ 1 ] + bounds[ 0 ] ) / double( 2 );
129       center[ 1 ] = ( bounds[ 3 ] + bounds[ 2 ] ) / double( 2 );
130       center[ 2 ] = ( bounds[ 5 ] + bounds[ 4 ] ) / double( 2 );
131
132       int ori = this->m_WLActor->GetOrientation( );
133       double pos[ 3 ] = { double( 0 ) };
134       pos[ ori ] = double( 1 );
135       pos[ 0 ] += center[ 0 ];
136       pos[ 1 ] += center[ 1 ];
137       pos[ 2 ] += center[ 2 ];
138
139       double up[ 3 ] = { double( 0 ) };
140       if( ori == 0 )
141       {
142         if     ( this->m_Quadrant == 0 ) up[ 2 ] = double( 1 );
143         else if( this->m_Quadrant == 1 ) up[ 2 ] = double( 1 );
144         else if( this->m_Quadrant == 2 ) up[ 2 ] = double( 1 );
145         else if( this->m_Quadrant == 3 ) up[ 2 ] = double( 1 );
146       }
147       else if( ori == 1 )
148       {
149         if     ( this->m_Quadrant == 0 ) up[ 2 ] = double( 1 );
150         else if( this->m_Quadrant == 1 ) up[ 2 ] = double( 1 );
151         else if( this->m_Quadrant == 2 ) up[ 2 ] = double( 1 );
152         else if( this->m_Quadrant == 3 ) up[ 2 ] = double( 1 );
153       }
154       else if( ori == 2 )
155       {
156         if     ( this->m_Quadrant == 0 ) up[ 1 ] = double( -1 );
157         else if( this->m_Quadrant == 1 ) up[ 1 ] = double( -1 );
158         else if( this->m_Quadrant == 2 ) up[ 1 ] = double( -1 );
159         else if( this->m_Quadrant == 3 ) up[ 1 ] = double( -1 );
160         pos[ 2 ] *= double( -1 );
161
162       } // fi
163
164       // Reconfigure camera and return
165       auto camera = this->m_Renderer->GetActiveCamera( );
166       camera->ParallelProjectionOn( );
167       camera->SetFocalPoint( center );
168       camera->SetPosition( pos );
169       camera->SetViewUp( up );
170       this->m_Renderer->ResetCamera( bounds );
171     }
172     else
173       this->Superclass::ResetCamera( );
174   }
175   else
176     this->Superclass::ResetCamera( );
177 }
178
179 // -------------------------------------------------------------------------
180 cpExtensions::QT::ImageWidget::
181 TWLActor* cpExtensions::QT::ImageWidget::
182 GetImageActor( )
183 {
184   return( this->m_WLActor );
185 }
186
187 // -------------------------------------------------------------------------
188 const cpExtensions::QT::ImageWidget::
189 TWLActor* cpExtensions::QT::ImageWidget::
190 GetImageActor( ) const
191 {
192   return( this->m_WLActor );
193 }
194
195 // -------------------------------------------------------------------------
196 void cpExtensions::QT::ImageWidget::
197 SetColor( const std::string& name, double r, double g, double b )
198 {
199   auto props = this->GetViewProps( name );
200   for( auto p = props.begin( ); p != props.end( ); ++p )
201   {
202     auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
203     if( actor != NULL )
204       actor->GetProperty( )->SetColor( r, g, b );
205
206   } // rof
207   this->Render( );
208 }
209
210 // -------------------------------------------------------------------------
211 void cpExtensions::QT::ImageWidget::
212 SetLineWidth( const std::string& name, double w )
213 {
214   auto props = this->GetViewProps( name );
215   for( auto p = props.begin( ); p != props.end( ); ++p )
216   {
217     auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
218     if( actor != NULL )
219       actor->GetProperty( )->SetLineWidth( w );
220
221   } // rof
222   this->Render( );
223 }
224
225 // eof - $RCSfile$