]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/ActorImageProperties.cxx
...
[cpPlugins.git] / lib / cpPlugins / ActorImageProperties.cxx
1 #include <cpPlugins/ActorImageProperties.h>
2
3 #ifdef cpPlugins_QT4
4
5 #include <cpPlugins/ui_ActorImageProperties.h>
6
7 #include <vtkImageActor.h>
8 #include <vtkImageData.h>
9 #include <vtkImageMapper3D.h>
10 #include <vtkImageProperty.h>
11
12 // -------------------------------------------------------------------------
13 cpPlugins::ActorImageProperties::
14 ActorImageProperties( QWidget* parent )
15   : cpPlugins::ActorProperties( parent ),
16     m_UI( new Ui::ActorImageProperties )
17 {
18   this->m_UI->setupUi( this );
19 }
20
21 // -------------------------------------------------------------------------
22 cpPlugins::ActorImageProperties::
23 ~ActorImageProperties( )
24 {
25   delete this->m_UI;
26 }
27
28 // -------------------------------------------------------------------------
29 bool cpPlugins::ActorImageProperties::
30 addActor( vtkProp* obj )
31 {
32   auto actor = dynamic_cast< vtkImageActor* >( obj );
33   if( actor != NULL )
34   {
35     this->m_Actors.insert( obj );
36     if( this->m_Actors.size( ) == 1 )
37       this->_updateWidgets( );
38     return( true );
39   }
40   else
41     return( false );
42 }
43
44 // -------------------------------------------------------------------------
45 void cpPlugins::ActorImageProperties::
46 _updateWidgets( )
47 {
48   auto actor =
49     dynamic_cast< vtkImageActor* >( this->m_Actors.begin( )->GetPointer( ) );
50   if( actor == NULL )
51     return;
52
53   // Get properties
54   auto prop = actor->GetProperty( );
55   auto mapper = actor->GetMapper( );
56   auto image = mapper->GetInput( );
57   double r[ 2 ];
58   image->GetScalarRange( r );
59   double win = prop->GetColorWindow( );
60   double lev = prop->GetColorLevel( );
61   double op = prop->GetOpacity( );
62   int interp = prop->GetInterpolationType( );
63
64   this->m_UI->ValueWindow->setMinimum( 0 );
65   this->m_UI->ValueWindow->setMaximum( r[ 1 ] - r[ 0 ] );
66   this->m_UI->ValueLevel->setMinimum( r[ 0 ] );
67   this->m_UI->ValueLevel->setMaximum( r[ 1 ] );
68   this->m_UI->ValueWindow->setValue( win );
69   this->m_UI->ValueLevel->setValue( lev );
70
71   double w = win / ( r[ 1 ] - r[ 0 ] );
72   double l = ( lev - r[ 0 ] ) / ( r[ 1 ] - r[ 0 ] );
73
74   op *= double( this->m_UI->Opacity->maximum( ) );
75   this->m_UI->Opacity->setValue( int( op ) );
76
77   w *= double( this->m_UI->Window->maximum( ) );
78   this->m_UI->Window->setValue( int( w ) );
79
80   l *= double( this->m_UI->Level->maximum( ) );
81   this->m_UI->Level->setValue( int( l ) );
82
83   if( interp == VTK_CUBIC_INTERPOLATION )
84     this->m_UI->Interpolation->setCurrentIndex( 0 );
85   else if( interp == VTK_LINEAR_INTERPOLATION )
86     this->m_UI->Interpolation->setCurrentIndex( 1 );
87   else if( interp == VTK_NEAREST_INTERPOLATION )
88     this->m_UI->Interpolation->setCurrentIndex( 2 );
89
90   // Connect stuff
91   this->connect(
92     this->m_UI->Opacity, SIGNAL( valueChanged( int ) ),
93     this, SLOT( _Opacity( int ) )
94     );
95   this->connect(
96     this->m_UI->Window, SIGNAL( valueChanged( int ) ),
97     this, SLOT( _Window( int ) )
98     );
99   this->connect(
100     this->m_UI->Level, SIGNAL( valueChanged( int ) ),
101     this, SLOT( _Level( int ) )
102     );
103   this->connect(
104     this->m_UI->ValueWindow, SIGNAL( valueChanged( double ) ),
105     this, SLOT( _Window( double ) )
106     );
107   this->connect(
108     this->m_UI->ValueLevel, SIGNAL( valueChanged( double ) ),
109     this, SLOT( _Level( double ) )
110     );
111   this->connect(
112     this->m_UI->Interpolation, SIGNAL( currentIndexChanged( int ) ),
113     this, SLOT( _Interpolation( int ) )
114     );
115 }
116
117 // -------------------------------------------------------------------------
118 void cpPlugins::ActorImageProperties::
119 _Opacity( int v )
120 {
121   double op = double( v ) / double( this->m_UI->Opacity->maximum( ) );
122   auto aIt = this->m_Actors.begin( );
123   for( ; aIt != this->m_Actors.end( ); ++aIt )
124   {
125     auto ma = dynamic_cast< vtkImageActor* >( aIt->GetPointer( ) );
126     ma->GetProperty( )->SetOpacity( op );
127     ma->Modified( );
128
129   } // rof
130   this->_render( );
131 }
132
133 // -------------------------------------------------------------------------
134 void cpPlugins::ActorImageProperties::
135 _Window( int v )
136 {
137   double x = this->m_UI->ValueWindow->minimum( );
138   double y = this->m_UI->ValueWindow->maximum( );
139   double z = double( v ) / double( this->m_UI->Window->maximum( ) );
140   double w = ( ( y - x ) * z ) + x;
141   bool prev = this->m_UI->ValueWindow->blockSignals( true );
142   this->m_UI->ValueWindow->setValue( w );
143   this->m_UI->ValueWindow->blockSignals( prev );
144   auto aIt = this->m_Actors.begin( );
145   for( ; aIt != this->m_Actors.end( ); ++aIt )
146   {
147     auto ma = dynamic_cast< vtkImageActor* >( aIt->GetPointer( ) );
148     ma->GetProperty( )->SetColorWindow( w );
149     ma->Modified( );
150
151   } // rof
152   this->_render( );
153 }
154
155 // -------------------------------------------------------------------------
156 void cpPlugins::ActorImageProperties::
157 _Level( int v )
158 {
159   double x = this->m_UI->ValueLevel->minimum( );
160   double y = this->m_UI->ValueLevel->maximum( );
161   double z = double( v ) / double( this->m_UI->Level->maximum( ) );
162   double l = ( ( y - x ) * z ) + x;
163   bool prev = this->m_UI->ValueLevel->blockSignals( true );
164   this->m_UI->ValueLevel->setValue( l );
165   this->m_UI->ValueLevel->blockSignals( prev );
166   auto aIt = this->m_Actors.begin( );
167   for( ; aIt != this->m_Actors.end( ); ++aIt )
168   {
169     auto ma = dynamic_cast< vtkImageActor* >( aIt->GetPointer( ) );
170     ma->GetProperty( )->SetColorLevel( l );
171     ma->Modified( );
172
173   } // rof
174   this->_render( );
175 }
176
177 // -------------------------------------------------------------------------
178 void cpPlugins::ActorImageProperties::
179 _Window( double v )
180 {
181   auto aIt = this->m_Actors.begin( );
182   for( ; aIt != this->m_Actors.end( ); ++aIt )
183   {
184     auto ma = dynamic_cast< vtkImageActor* >( aIt->GetPointer( ) );
185     ma->GetProperty( )->SetColorWindow( v );
186     ma->Modified( );
187
188   } // rof
189   this->_render( );
190
191   double x = this->m_UI->ValueWindow->minimum( );
192   double y = this->m_UI->ValueWindow->maximum( );
193   double z = ( v - x ) / ( y - x );
194   double w = double( z ) * double( this->m_UI->Window->maximum( ) );
195   bool prev = this->m_UI->Window->blockSignals( true );
196   this->m_UI->Window->setValue( int( w ) );
197   this->m_UI->Window->blockSignals( prev );
198 }
199
200 // -------------------------------------------------------------------------
201 void cpPlugins::ActorImageProperties::
202 _Level( double v )
203 {
204   auto aIt = this->m_Actors.begin( );
205   for( ; aIt != this->m_Actors.end( ); ++aIt )
206   {
207     auto ma = dynamic_cast< vtkImageActor* >( aIt->GetPointer( ) );
208     ma->GetProperty( )->SetColorLevel( v );
209     ma->Modified( );
210
211   } // rof
212   this->_render( );
213
214   double x = this->m_UI->ValueLevel->minimum( );
215   double y = this->m_UI->ValueLevel->maximum( );
216   double z = ( v - x ) / ( y - x );
217   double l = double( z ) * double( this->m_UI->Level->maximum( ) );
218   bool prev = this->m_UI->Level->blockSignals( true );
219   this->m_UI->Level->setValue( int( l ) );
220   this->m_UI->Level->blockSignals( prev );
221 }
222
223 // -------------------------------------------------------------------------
224 void cpPlugins::ActorImageProperties::
225 _Interpolation( int v )
226 {
227   int interp = VTK_NEAREST_INTERPOLATION;
228   if     ( v == 0 ) interp = VTK_CUBIC_INTERPOLATION;
229   else if( v == 1 ) interp = VTK_LINEAR_INTERPOLATION;
230   else if( v == 2 ) interp = VTK_NEAREST_INTERPOLATION;
231
232   auto aIt = this->m_Actors.begin( );
233   for( ; aIt != this->m_Actors.end( ); ++aIt )
234   {
235     auto ma = dynamic_cast< vtkImageActor* >( aIt->GetPointer( ) );
236     ma->GetProperty( )->SetInterpolationType( interp );
237     ma->Modified( );
238
239   } // rof
240   this->_render( );
241 }
242
243 #endif // cpPlugins_QT4
244
245 // eof - $RCSfile$