]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/ThresholdImageView.cxx
no message
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / ThresholdImageView / ThresholdImageView.cxx
1
2 /**
3  *  \file 
4  *  \brief Class bbtk::ThresholdImageView . 
5  */
6
7
8 #include "ThresholdImageView.h"
9
10
11
12   //=========================================================================
13         ThresholdImageView::ThresholdImageView( )
14   {
15           _actorPresent                         =       false;
16           _Z                                            =       0;
17           _minValue                                     =       0;
18           _maxValue                                     =       1000;
19           _image                                        =       NULL;
20           _imageReslicer                        =       NULL;
21           _thresholdTable                       =       NULL;
22           _thresholdMapper                      =       NULL;
23           _thresholdActor                       =       NULL;
24           _baseColorR                           =       1;
25           _baseColorG                           =       0;
26           _baseColorB                           =       0;
27   }
28
29   //=========================================================================
30   ThresholdImageView::~ThresholdImageView()
31   {
32   }
33   //=========================================================================
34
35
36 //----------------------------------------------------------------------------
37 void ThresholdImageView::SetImage(vtkImageData* image)
38 {
39         printf("ThresholdImageView::SetImage %p\n", image);
40         _image = image;
41 }
42
43 //----------------------------------------------------------------------------
44 void ThresholdImageView::SetwxVtkBaseView(wxVtkBaseView *baseview)
45 {
46         _baseView = baseview;
47 }
48
49 //----------------------------------------------------------------------------
50 wxVtkBaseView *ThresholdImageView::GetwxVtkBaseView()
51 {
52         return _baseView;
53 }
54
55 //----------------------------------------------------------------------------
56 void ThresholdImageView::SetZ(int z)
57 {
58         _Z = z;
59 }
60
61 //----------------------------------------------------------------------------
62 void ThresholdImageView::SetminMaxValue(int min, int max)
63 {
64         _minValue = min;
65         _maxValue = max;
66 }
67
68 //----------------------------------------------------------------------------
69 void ThresholdImageView::onThreshold()
70 {
71         int     z = _Z;
72         double range[2];
73         
74         vtkImageData *img = _image;
75         img->GetScalarRange(range);
76         if (range[1]==0)
77         {
78                 range[1]=255;
79         }
80         
81         
82         int minTot = floor (range[0]);
83         int maxTot = ceil (range[1]);
84         
85         
86         int minVal = floor (_minValue);
87         int maxVal = floor (_maxValue);
88         
89         
90         if (!_actorPresent)
91         {
92                 if (_imageReslicer==NULL)
93                 {
94                         _imageReslicer = vtkImageReslice::New();
95                         _imageReslicer->SetInput( img );
96                         _imageReslicer->SetInformationInput(img);                       
97                         _imageReslicer->SetResliceAxesDirectionCosines(1,0,0, 0,1,0 ,0,0,1);
98                         _imageReslicer->SetOutputDimensionality(2);
99                         _imageReslicer->SetInterpolationModeToLinear();
100                 }
101                 
102                 _imageReslicer->SetResliceAxesOrigin(0,0,z);
103                 
104                 img = _imageReslicer->GetOutput();
105                 img->Update();
106                 img->UpdateInformation();
107                 
108                 wxVtkBaseView *baseView = _baseView;
109                 
110                 if (_thresholdTable==NULL)
111                 {
112                         //Lookup Table
113                         _thresholdTable = vtkLookupTable::New();
114                         _thresholdTable->SetNumberOfTableValues(maxTot+1);
115                         _thresholdTable->SetTableRange(range); 
116                         _thresholdTable->SetAlphaRange(0, 1);
117                         _thresholdTable->SetValueRange(0, 1);
118                         _thresholdTable->SetSaturationRange(0, 0); 
119                         _thresholdTable->SetRampToLinear( );
120                 }
121                 
122                 //Assign a fake color for the upper image, and set the white as transparent
123                 int i;
124                 for(i = minTot; i <= maxTot; i++)
125                 {
126                         if( i >= minVal && i <= maxVal )
127                         {
128                                 _thresholdTable->SetTableValue(i,_baseColorR,_baseColorG,_baseColorB, 1);
129                         }
130                         else if( i >= minTot && i < minVal )
131                         {
132                                 _thresholdTable->SetTableValue(i, 1.0, 1.0, 1.0, 0); //transparent
133                         }
134                         else if( i > maxVal && i < maxTot )
135                         {
136                                 _thresholdTable->SetTableValue(i, 1.0, 1.0, 1.0, 0); //transparent
137                         }
138                         else
139                         {
140                                 _thresholdTable->SetTableValue(i, 1.0, 1.0, 1.0, 0); //transparent
141                         }
142                 }
143                 _thresholdTable->Build( );
144                 
145                 if (_thresholdMapper==NULL)
146                 {
147                         _thresholdMapper = vtkImageMapToColors::New( );
148                 }
149                 
150                 _thresholdMapper->SetLookupTable( _thresholdTable );
151                 _thresholdMapper->SetInput( img );
152                 
153                 if (_thresholdActor==NULL)
154                 {
155                         _thresholdActor = vtkImageActor::New( );
156                         _thresholdActor->SetOpacity( 0.6 );
157                         _thresholdActor->InterpolateOn(  );
158                         _thresholdActor->SetPosition( 0,0, 900-1 );
159                 }
160                 
161                 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
162                 
163                 baseView->GetRenderer()->AddActor( _thresholdActor );
164                 _actorPresent = true;
165         }
166         
167         else
168         {
169                 _imageReslicer->SetResliceAxesOrigin(0,0,z);
170                 img = _imageReslicer->GetOutput();
171                 img->Update();
172                 img->UpdateInformation();
173                 
174                 //Assign a fake color for the upper image, and set the white as transparent
175                 int i;
176                 for(i = minTot; i <= maxTot; i++)
177                 {
178                         if( i >= minVal && i <= maxVal )
179                         {
180                                 _thresholdTable->SetTableValue(i,_baseColorR,_baseColorG,_baseColorB, 1);
181                         }
182                         else if( i >= minTot && i < minVal )
183                         {
184                                 _thresholdTable->SetTableValue(i, 1.0, 1.0, 1.0, 0); //transparent
185                         }
186                         else if( i > maxVal && i < maxTot )
187                         {
188                                 _thresholdTable->SetTableValue(i, 1.0, 1.0, 1.0, 0); //transparent
189                         }
190                         else
191                         {
192                                 _thresholdTable->SetTableValue(i, 1.0, 1.0, 1.0, 0); //transparent
193                         }
194                 }
195                 
196                 _thresholdTable->Build( );
197                 _thresholdMapper->SetLookupTable( _thresholdTable );
198                 _thresholdMapper->SetInput( img );
199                 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
200         }
201 }
202
203
204 //----------------------------------------------------------------------------
205 void ThresholdImageView::onThresholdChange()
206 {
207         if (_actorPresent)
208         {
209                 onThreshold();
210         }
211 }
212
213 //----------------------------------------------------------------------------
214 void ThresholdImageView::onThresholdInterpolation(bool interpolate)
215 {
216         if (_thresholdActor!=NULL)
217         {
218                 if (interpolate)
219                 {
220                         _thresholdActor->InterpolateOn( );
221                 }
222                 
223                 else
224                 {
225                         _thresholdActor->InterpolateOff( );
226                 }
227                 
228 //EED           _theViewPanel->RefreshInterface();
229         }
230 }
231
232 //----------------------------------------------------------------------------
233 void ThresholdImageView::onThresholdChangeOpacity (int opacity)
234 {
235         if (_actorPresent)
236         {
237                 _thresholdActor->SetOpacity(opacity*0.1);
238         }
239         
240 //EED   _theViewPanel->RefreshInterface();
241 }
242
243 //----------------------------------------------------------------------------
244 void ThresholdImageView::onThresholdRemove()
245 {
246         if (_actorPresent)
247         {
248                 wxVtkBaseView * baseView = _baseView;
249                 baseView->GetRenderer()->RemoveActor( _thresholdActor );
250                 _actorPresent = false;
251         }
252         
253 //EED   _theViewPanel->RefreshInterface();
254 }
255
256
257 //----------------------------------------------------------------------------
258 void ThresholdImageView::SetBaseColor(double r, double g, double b)
259 {
260         _baseColorR = r;
261         _baseColorG = g;
262         _baseColorB = b;
263 }
264
265
266 // EOF
267