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