]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/LayerImageBase.cxx
925dc8f84157ab708b9b93d0fb7de3255b327952
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / ThresholdImageView / LayerImageBase.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
8 #
9 #  This software is governed by the CeCILL-B license under French law and
10 #  abiding by the rules of distribution of free software. You can  use,
11 #  modify and/ or redistribute the software under the terms of the CeCILL-B
12 #  license as circulated by CEA, CNRS and INRIA at the following URL
13 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 #  or in the file LICENSE.txt.
15 #
16 #  As a counterpart to the access to the source code and  rights to copy,
17 #  modify and redistribute granted by the license, users are provided only
18 #  with a limited warranty  and the software's author,  the holder of the
19 #  economic rights,  and the successive licensors  have only  limited
20 #  liability.
21 #
22 #  The fact that you are presently reading this means that you have had
23 #  knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
25
26 /**
27  *  \file
28  *  \brief Class bbtk::ThresholdImageView .
29  */
30
31 #include "LayerImageBase.h"
32
33 #include "wxMPRBaseData.h"
34 #include "wxVtk2DBaseView.h"
35 #include "wxVTKRenderWindowInteractor.h"
36 #include "vtkTextProperty.h"
37
38
39 //---------------------------------------------------------------------------------------------
40 //---------------------------------------------------------------------------------------------
41 //---------------------------------------------------------------------------------------------
42
43 // Callback for the interaction
44 class ColorLayerObserver : public vtkCommand{
45   public:
46         virtual char const *GetClassName() const { return "ColorLayerObserver";}
47
48         static ColorLayerObserver *New()        {      return new ColorLayerObserver;   }
49
50         ColorLayerObserver() 
51         {
52                 _layerimagebase = NULL;
53         }
54
55         virtual void Execute(vtkObject *wdg, unsigned long event, void* calldata)       
56         { 
57                 if (_layerimagebase!= NULL)
58                 {
59
60                         wxVtkBaseView           *wxvtkbaseview = _layerimagebase->GetwxVtkBaseView();
61                         if (wxvtkbaseview!=NULL)
62                         {
63
64                                 vtkMPRBaseData  *vtkmprbasedata =       (vtkMPRBaseData*)wxvtkbaseview->GetVtkBaseData();
65                                 if (vtkmprbasedata!=NULL)
66                                 {
67                                         _layerimagebase->SetX( vtkmprbasedata->GetX() );
68                                         _layerimagebase->SetY( vtkmprbasedata->GetY() );
69                                         _layerimagebase->SetZ( vtkmprbasedata->GetZ() );
70                         _layerimagebase->onThresholdChange();
71                                 } // if basedata
72                                 
73                         } // if wxvtkbaseview
74                 } //if layerimagebase
75         }
76
77         void SetLayerImageBase(LayerImageBase* layerimagebase)
78         {
79         _layerimagebase =       layerimagebase;
80         }
81
82 private:
83         LayerImageBase* _layerimagebase;
84 };
85
86
87
88 //---------------------------------------------------------------------------------------------
89 //---------------------------------------------------------------------------------------------
90 //---------------------------------------------------------------------------------------------
91
92
93
94 LayerImageBase::LayerImageBase()
95 {
96         _actorPresent           =       false;
97         _X                                      =       0;
98         _Y                                      =       0;
99         _Z                                      =       0;
100         _thresholdTable         =       NULL;
101         _thresholdMapper        =       NULL;
102         _scalarbarActor         =       NULL;
103         _thresholdActor         =       NULL;
104         _image                          =       NULL;
105         _baseView                       =       NULL;
106         _imageReslicer          =       vtkImageReslice::New();
107 }
108
109 //----------------------------------------------------------------------------
110 LayerImageBase::~LayerImageBase()
111 {
112 }
113
114 //----------------------------------------------------------------------------
115 void LayerImageBase::SetX(int x)
116 {
117         _X = x;
118 }
119
120 //----------------------------------------------------------------------------
121 void LayerImageBase::SetY(int y)
122 {
123         _Y = y;
124 }
125
126 //----------------------------------------------------------------------------
127 void LayerImageBase::SetZ(int z)
128 {
129         _Z = z;
130 }
131
132 //----------------------------------------------------------------------------
133 int LayerImageBase::GetX()  // virtual
134 {
135         return _X;
136 }
137
138 //----------------------------------------------------------------------------
139 int LayerImageBase::GetY()  // virtual
140 {
141         return _Y;
142 }
143 //----------------------------------------------------------------------------
144 int LayerImageBase::GetZ()  // virtual
145 {
146         return _Z;
147 }
148
149
150 //----------------------------------------------------------------------------
151 vtkImageData* LayerImageBase::GetImage()
152 {
153         return _image;
154 }
155
156 //----------------------------------------------------------------------------
157 bool LayerImageBase::GetActorPresent()
158 {
159         return _actorPresent;
160 }
161
162
163
164 //----------------------------------------------------------------------------
165 void LayerImageBase::SetImage(vtkImageData* image)
166 {
167         double spc[3];
168         int ext[6];
169         _image = image;
170         _image->GetSpacing(spc);
171         _image->GetWholeExtent(ext);
172         int sx=ext[1]-ext[0]+1;
173         int sy=ext[3]-ext[2]+1;
174         int sz=ext[5]-ext[4]+1;
175         int ssx=(sx/2)*spc[0];
176         int ssy=(sy/2)*spc[1];
177         int ssz=(sz/2)*spc[2];
178         SetX( ssx );
179         SetY( ssy );
180         SetZ( ssz );
181         _image->Update();
182         _image->GetScalarRange( _range );
183         _thresholdTable = vtkLookupTable::New();
184 }
185
186 void LayerImageBase::SetRangeForColorBar(std::vector<double> &range)
187 {
188    _rangeForColorBar=range;
189 }
190
191 void LayerImageBase::GetRangeForColorBar(double &minRange, double &maxRange)
192 {
193         if (_rangeForColorBar.size()==2)
194         {
195                 minRange = _rangeForColorBar[0];        
196                 maxRange = _rangeForColorBar[1];
197         } else {
198                 minRange = _range[0];   
199                 maxRange = _range[1];
200         }
201 }
202
203
204
205 //----------------------------------------------------------------------------
206 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
207 {
208         if (_baseView==NULL)
209    {
210                 _baseView = baseview;
211                 ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
212                 _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
213                 colorlayerobserver->SetLayerImageBase(this);
214
215         } // if _baseView
216 }
217
218 //----------------------------------------------------------------------------
219 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
220 {
221         return _baseView;
222 }
223
224 //----------------------------------------------------------------------------
225 void LayerImageBase::Refresh()
226 {
227     if (_baseView!=NULL)
228     {
229         _baseView->Refresh();
230     }
231 }
232
233
234 //----------------------------------------------------------------------------
235 vtkLookupTable* LayerImageBase::GetThresholdTable()
236 {
237         return _thresholdTable;
238 }
239
240 //----------------------------------------------------------------------------
241 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
242 {
243         int ext[6];
244         _image->GetWholeExtent(ext);
245
246         if (x<0)
247         {
248                 x=0;
249         }
250
251         if (y<0)
252         {
253                 y=0;
254         }
255
256         if (z<0)
257         {
258                 z=0;
259         }
260
261         if ( x > (ext[1]-ext[0]) )
262         {
263                 z=ext[1]-ext[0];
264         }
265
266         if ( y > (ext[3]-ext[2]) )
267         {
268                 z=ext[3]-ext[2];
269         }
270
271         if ( z > (ext[5]-ext[4]) )
272         {
273                 z=ext[5]-ext[4];
274         }
275
276
277 }
278
279 //----------------------------------------------------------------------------
280 void LayerImageBase::onThreshold()
281 {
282         if ((_image!=NULL) && (_baseView!=NULL))
283         {
284                 double spc[3];
285                 GetImage()-> GetSpacing(spc);
286                 double x=GetX();
287                 double y=GetY();
288                 double z=GetZ();
289                 CleanXYZ(x,y,z);
290
291                 x = x*spc[0];
292                 y = y*spc[1];
293                 z = z*spc[2];
294
295                 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
296                 int directionViewer=0;
297                 if (camera->GetParallelProjection()==1)
298                 {
299                         wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
300                         directionViewer =  wxvtk2dbasevie->GetDirection();
301                 } // ParallelProjection
302
303
304                 if (!GetActorPresent())
305                 {
306                         if (_thresholdTable==NULL)
307                         {
308                                 //Lookup Table
309                                 _thresholdTable = vtkLookupTable::New();
310                         } // _thresholdTable
311
312                         if (_thresholdMapper==NULL)
313                         {
314                                 _thresholdMapper = vtkImageMapToColors::New( );
315                         }
316
317                         if (_thresholdActor==NULL)
318                         {
319                                 _thresholdActor = vtkImageActor::New( );
320                                 _scalarbarActor = vtkScalarBarActor::New();
321                                 _thresholdActor->SetOpacity( 0.6 );
322                                 _thresholdActor->InterpolateOn(  );
323                                 if (directionViewer==0)
324                                 {
325                                         _thresholdActor->SetPosition( 900-1,0,0 );
326                                         _thresholdActor->RotateY( -90 );
327                                 }
328                                 if (directionViewer==1)
329                                 {
330                                         _thresholdActor->SetPosition( 0,-900-1, 0 );
331                                         _thresholdActor->RotateX( 90 );
332                                 }
333                                 if (directionViewer==2)
334                                 {
335                                         _thresholdActor->SetPosition( 0,0, -900-1 );
336                                 }
337                         } // _thresholdActor
338                         _baseView->GetRenderer()->AddActor( _thresholdActor );
339                         _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
340                         _actorPresent = true;
341                 }  // !GetActorPresent()
342
343                 ConfigLookupTable();  // virtual method
344                 _imageReslicer->SetInput( GetImage() );
345                 _imageReslicer->SetInformationInput( GetImage() );
346
347                 if (directionViewer==0)
348                 {
349                         _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0,  -1,0,0 );
350                         _imageReslicer->SetResliceAxesOrigin(x,0,0);
351                 }
352
353                 if (directionViewer==1)
354                 {
355                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1,  0,-1,0 );
356                         _imageReslicer->SetResliceAxesOrigin(0,y,0);
357                 }
358
359                 if (directionViewer==2)
360                 {
361                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
362                         _imageReslicer->SetResliceAxesOrigin(0,0,z);
363                 }
364
365
366                 _imageReslicer->SetOutputDimensionality(2);
367 //              _imageReslicer->SetInterpolationModeToLinear();
368                 _imageReslicer->SetInterpolationModeToNearestNeighbor();
369
370                 vtkImageData *img = _imageReslicer->GetOutput();
371 //              img->Update();
372 //              img->UpdateInformation();
373
374 //              _thresholdTable->Update();
375                 _thresholdMapper->SetInput( img );
376                 _thresholdMapper->SetLookupTable( _thresholdTable );
377                 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
378                 
379 //              _scalarbarActor->SetLookupTable( _thresholdTable );
380                 _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
381                 _scalarbarActor->SetTitle("Value");
382                 _scalarbarActor->SetNumberOfLabels(4);
383                 int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
384                 _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
385
386 //              _scalarbarActor->SetTextPad(4);  ??
387
388                 } // _image
389 }
390
391
392
393 //----------------------------------------------------------------------------
394 void LayerImageBase::onThresholdChange()
395 {
396         if (_actorPresent)
397         {
398                 onThreshold();
399         }
400 }
401
402 //----------------------------------------------------------------------------
403 void LayerImageBase::onThresholdInterpolation(bool interpolate)
404 {
405         if (_thresholdActor!=NULL)
406         {
407                 if (interpolate)
408                 {
409                         _thresholdActor->InterpolateOn( );
410                 }
411                 else
412                 {
413                         _thresholdActor->InterpolateOff( );
414                 }
415         }
416 }
417
418 //----------------------------------------------------------------------------
419 void LayerImageBase::onThresholdChangeOpacity (int opacity)
420 {
421         if (_actorPresent)
422         {
423                 _thresholdActor->SetOpacity(opacity*0.1);
424         }
425 }
426
427 //----------------------------------------------------------------------------
428 void LayerImageBase::onThresholdRemove()
429 {
430         if (_actorPresent)
431         {
432                 wxVtkBaseView * baseView = _baseView;
433                 baseView->GetRenderer()->RemoveActor( _thresholdActor );
434                 baseView->GetRenderer()->RemoveActor( _scalarbarActor );
435
436                 _actorPresent = false;
437         }
438 }
439
440 //----------------------------------------------------------------------------
441 vtkLookupTable *LayerImageBase::GetvtkLookupTable()
442 {
443     return _thresholdTable;
444 }
445
446 //----------------------------------------------------------------------------
447 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
448 {
449     return _imageReslicer;
450 }
451
452 //----------------------------------------------------------------------------
453 void LayerImageBase::GetImageScalarRange()
454 {
455         double min,max;
456         GetRangeForColorBar(min,max);
457 //      _image->GetScalarRange(_range);
458         _range[0]=min;
459         _range[1]=max;
460 }
461
462
463
464 // EOF
465