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