]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/LayerImageBase.cxx
#2651 creaMaracasVisu Feature New Normal - Color Layer: Transparent Base, Refresh
[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         _image = image;
168         double spc[3];
169         _image->GetSpacing(spc);
170         int ext[6];
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 = image;
182 }
183
184 //----------------------------------------------------------------------------
185 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
186 {
187         if (_baseView==NULL)
188    {
189                 _baseView = baseview;
190                 ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
191                 _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
192                 colorlayerobserver->SetLayerImageBase(this);
193
194         } // if _baseView
195 }
196
197 //----------------------------------------------------------------------------
198 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
199 {
200         return _baseView;
201 }
202
203 //----------------------------------------------------------------------------
204 void LayerImageBase::Refresh()
205 {
206     if (_baseView!=NULL)
207     {
208         _baseView->Refresh();
209     }
210 }
211
212
213 //----------------------------------------------------------------------------
214 vtkLookupTable* LayerImageBase::GetThresholdTable()
215 {
216         return _thresholdTable;
217 }
218
219 //----------------------------------------------------------------------------
220 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
221 {
222         int ext[6];
223         _image->GetWholeExtent(ext);
224
225         if (x<0)
226         {
227                 x=0;
228         }
229
230         if (y<0)
231         {
232                 y=0;
233         }
234
235         if (z<0)
236         {
237                 z=0;
238         }
239
240         if ( x > (ext[1]-ext[0]) )
241         {
242                 z=ext[1]-ext[0];
243         }
244
245         if ( y > (ext[3]-ext[2]) )
246         {
247                 z=ext[3]-ext[2];
248         }
249
250         if ( z > (ext[5]-ext[4]) )
251         {
252                 z=ext[5]-ext[4];
253         }
254
255
256 }
257
258 //----------------------------------------------------------------------------
259 void LayerImageBase::onThreshold()
260 {
261 printf("EED LayerImageBase::onThreshold Start \n");
262
263         if ((_image!=NULL) && (_baseView!=NULL))
264         {
265 printf("EED LayerImageBase::onThreshold 1 \n");
266                 double spc[3];
267                 GetImage()-> GetSpacing(spc);
268                 double x=GetX();
269                 double y=GetY();
270                 double z=GetZ();
271                 CleanXYZ(x,y,z);
272
273                 x = x*spc[0];
274                 y = y*spc[1];
275                 z = z*spc[2];
276
277                 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
278                 int directionViewer=0;
279                 if (camera->GetParallelProjection()==1)
280                 {
281                         wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
282                         directionViewer =  wxvtk2dbasevie->GetDirection();
283                 } // ParallelProjection
284
285
286                 if (!GetActorPresent())
287                 {
288 printf("EED LayerImageBase::onThreshold 2 \n");
289                         if (_thresholdTable==NULL)
290                         {
291                                 //Lookup Table
292                                 _thresholdTable = vtkLookupTable::New();
293                         } // _thresholdTable
294
295                         if (_thresholdMapper==NULL)
296                         {
297                                 _thresholdMapper = vtkImageMapToColors::New( );
298                         }
299
300                         if (_thresholdActor==NULL)
301                         {
302                                 _thresholdActor = vtkImageActor::New( );
303                                 _scalarbarActor = vtkScalarBarActor::New();
304                                 _thresholdActor->SetOpacity( 0.6 );
305                                 _thresholdActor->InterpolateOn(  );
306                                 if (directionViewer==0)
307                                 {
308                                         _thresholdActor->SetPosition( 900-1,0,0 );
309                                         _thresholdActor->RotateY( -90 );
310                                 }
311                                 if (directionViewer==1)
312                                 {
313                                         _thresholdActor->SetPosition( 0,-900-1, 0 );
314                                         _thresholdActor->RotateX( 90 );
315                                 }
316                                 if (directionViewer==2)
317                                 {
318                                         _thresholdActor->SetPosition( 0,0, -900-1 );
319                                 }
320                         } // _thresholdActor
321                         _baseView->GetRenderer()->AddActor( _thresholdActor );
322                         _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
323                         _actorPresent = true;
324                 }  // !GetActorPresent()
325
326                 ConfigLookupTable();  // virtual method
327                 _imageReslicer->SetInput( GetImage() );
328                 _imageReslicer->SetInformationInput( GetImage() );
329
330 printf("EED LayerImageBase::onThreshold  directionViewer=%d\n", directionViewer );
331
332                 if (directionViewer==0)
333                 {
334                         _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0,  -1,0,0 );
335                         _imageReslicer->SetResliceAxesOrigin(x,0,0);
336                 }
337
338                 if (directionViewer==1)
339                 {
340                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1,  0,-1,0 );
341                         _imageReslicer->SetResliceAxesOrigin(0,y,0);
342                 }
343
344                 if (directionViewer==2)
345                 {
346                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
347                         _imageReslicer->SetResliceAxesOrigin(0,0,z);
348                 }
349
350
351                 _imageReslicer->SetOutputDimensionality(2);
352 //              _imageReslicer->SetInterpolationModeToLinear();
353                 _imageReslicer->SetInterpolationModeToNearestNeighbor();
354
355                 vtkImageData *img = _imageReslicer->GetOutput();
356 //              img->Update();
357 //              img->UpdateInformation();
358
359 //              _thresholdTable->Update();
360                 _thresholdMapper->SetInput( img );
361                 _thresholdMapper->SetLookupTable( _thresholdTable );
362                 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
363                 
364 //              _scalarbarActor->SetLookupTable( _thresholdTable );
365                 _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
366                 _scalarbarActor->SetTitle("Value");
367                 _scalarbarActor->SetNumberOfLabels(4);
368                 int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
369                 _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
370
371 //              _scalarbarActor->SetTextPad(4);  ??
372
373                 } // _image
374 }
375
376
377
378 //----------------------------------------------------------------------------
379 void LayerImageBase::onThresholdChange()
380 {
381         if (_actorPresent)
382         {
383                 onThreshold();
384         }
385 }
386
387 //----------------------------------------------------------------------------
388 void LayerImageBase::onThresholdInterpolation(bool interpolate)
389 {
390         if (_thresholdActor!=NULL)
391         {
392                 if (interpolate)
393                 {
394                         _thresholdActor->InterpolateOn( );
395                 }
396                 else
397                 {
398                         _thresholdActor->InterpolateOff( );
399                 }
400         }
401 }
402
403 //----------------------------------------------------------------------------
404 void LayerImageBase::onThresholdChangeOpacity (int opacity)
405 {
406         if (_actorPresent)
407         {
408                 _thresholdActor->SetOpacity(opacity*0.1);
409         }
410 }
411
412 //----------------------------------------------------------------------------
413 void LayerImageBase::onThresholdRemove()
414 {
415         if (_actorPresent)
416         {
417                 wxVtkBaseView * baseView = _baseView;
418                 baseView->GetRenderer()->RemoveActor( _thresholdActor );
419                 baseView->GetRenderer()->RemoveActor( _scalarbarActor );
420
421                 _actorPresent = false;
422         }
423 }
424
425 //----------------------------------------------------------------------------
426 vtkLookupTable *LayerImageBase::GetvtkLookupTable()
427 {
428     return _thresholdTable;
429 }
430
431 //----------------------------------------------------------------------------
432 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
433 {
434     return _imageReslicer;
435 }
436
437
438
439 // EOF
440