]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/LayerImageBase.cxx
Merge branch 'master' of ssh://git.creatis.insa-lyon.fr/creaMaracasVisu
[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
37
38
39
40 //---------------------------------------------------------------------------------------------
41 //---------------------------------------------------------------------------------------------
42 //---------------------------------------------------------------------------------------------
43
44 // Callback for the interaction
45 class ColorLayerObserver : public vtkCommand{
46   public:
47         virtual char const *GetClassName() const { return "ColorLayerObserver";}
48
49         static ColorLayerObserver *New()        {      return new ColorLayerObserver;   }
50
51         ColorLayerObserver() 
52         {
53                 _layerimagebase = NULL;
54         }
55
56         virtual void Execute(vtkObject *wdg, unsigned long event, void* calldata)       
57         { 
58                 if (_layerimagebase!= NULL)
59                 {
60
61                         wxVtkBaseView           *wxvtkbaseview = _layerimagebase->GetwxVtkBaseView();
62                         if (wxvtkbaseview!=NULL)
63                         {
64
65                                 vtkMPRBaseData  *vtkmprbasedata =       (vtkMPRBaseData*)wxvtkbaseview->GetVtkBaseData();
66                                 if (vtkmprbasedata!=NULL)
67                                 {
68                                         _layerimagebase->SetX( vtkmprbasedata->GetX() );
69                                         _layerimagebase->SetY( vtkmprbasedata->GetY() );
70                                         _layerimagebase->SetZ( vtkmprbasedata->GetZ() );
71                         _layerimagebase->onThresholdChange();
72                                 } // if basedata
73                                 
74                         } // if wxvtkbaseview
75                 } //if layerimagebase
76         }
77
78         void SetLayerImageBase(LayerImageBase* layerimagebase)
79         {
80         _layerimagebase =       layerimagebase;
81         }
82
83 private:
84         LayerImageBase* _layerimagebase;
85 };
86
87
88 //---------------------------------------------------------------------------------------------
89 //---------------------------------------------------------------------------------------------
90 //---------------------------------------------------------------------------------------------
91
92 ColorLayerImageBasevtkInteractor::ColorLayerImageBasevtkInteractor(LayerImageBase* layerImageBase)
93 {
94     _state                              =       false;
95     _stateKey                   =       false;
96     _layerImageBase     =       layerImageBase;
97         
98 }
99
100 //---------------------------------------------------------------------------------------------
101 ColorLayerImageBasevtkInteractor::~ColorLayerImageBasevtkInteractor()
102 {
103 }
104
105 //---------------------------------------------------------------------------------------------
106 bool ColorLayerImageBasevtkInteractor::OnChar() 
107 {
108         return true;
109 }
110
111
112 //---------------------------------------------------------------------------------------------
113 bool ColorLayerImageBasevtkInteractor::OnMouseMove()   // vitual
114 {
115         printf("EED ColorLayerImageBasevtkInteractor::OnMouseMove start \n");
116
117         vtkRenderWindowInteractor *interactor = _vtkInteractorStyleBaseView->GetInteractor();
118
119         // For example in ManualPaint Action
120         // This is not working, because the order of insertion events mecanism (_vtkInteractorStyleBaseView)
121         // Layer first Paint after.
122         // the only way to make it works is to take the axes an paint.  this force a first SetParent_refresh_waiting()
123 //      if ( this->_vtkInteractorStyleBaseView->GetParent_refresh_waiting() == true )
124
125         if ( interactor->GetControlKey()==1 )  // EED This if for ManaulPaint Interaction compatibility
126         {
127                 printf("EED ColorLayerImageBasevtkInteractor::OnMouseMove need Refresh \n" );
128                 _layerImageBase->onThreshold();
129                 _layerImageBase->GetvtkImageReslice()->Modified();
130         } else {
131                 printf("EED ColorLayerImageBasevtkInteractor::OnMouseMove NOT need Refresh \n" );
132         }       
133
134         
135         if (  (_state==true) &&  (_layerImageBase->GetActorPresent())  )
136         {
137                 wxVtk2DBaseView *wxvtk2Dbaseview = (wxVtk2DBaseView*) _vtkInteractorStyleBaseView->GetWxVtk2DBaseView();
138                 wxVTKRenderWindowInteractor *wxVTKiren = wxvtk2Dbaseview->GetWxVTKRenderWindowInteractor();
139                 int px, py;
140                 wxVTKiren->GetEventPosition(px, py);
141
142 //        int     typeView = 1;
143                 double X = (double) px;
144                 double Y = (double) py;
145                 double Z = wxvtk2Dbaseview->GetActualSlice();
146 //              wxvtk2Dbaseview->TransFromCoordScreenToWorld(X, Y, Z,true, typeView);
147                 wxvtk2Dbaseview->TransFromCoordScreenToWorld(X, Y, Z, true);
148
149 //              _manualPaintControler->PaintImage((int) X, (int) Y, (int) Z);
150
151                 _layerImageBase->SetX( (int)X );
152                 _layerImageBase->SetY( (int)Y );
153                 _layerImageBase->SetZ( (int)Z );
154                 _layerImageBase->onThreshold();
155 //              _layerImageBase->Refresh();
156
157                 this->_vtkInteractorStyleBaseView->SetParent_refresh_waiting();
158
159                 if  (_vtkInteractorStyleBaseView->GetRefresh_waiting()==true)
160                 {
161                         _layerImageBase->GetvtkImageReslice()->Modified();
162                 }
163         printf("EED ColorLayerImageBasevtkInteractor::YESSSSSS \n");
164         }
165         return true;
166 }
167
168 //---------------------------------------------------------------------------------------------
169 bool  ColorLayerImageBasevtkInteractor::OnRightButtonDown() // virtual
170 {
171         printf("EED ColorLayerImageBasevtkInteractor::OnRightButtonDown \n");
172         _state=true;
173    return true;
174 }
175
176
177 //---------------------------------------------------------------------------------------------
178 bool  ColorLayerImageBasevtkInteractor::OnRightButtonUp()  // virtual
179 {
180         printf("EED ColorLayerImageBasevtkInteractor::OnRightButtonUp \n");
181         _state=false;
182         return true;
183 }
184
185
186 //---------------------------------------------------------------------------------------------
187 //---------------------------------------------------------------------------------------------
188 //---------------------------------------------------------------------------------------------
189
190
191
192 LayerImageBase::LayerImageBase()
193 {
194         _actorPresent                   =       false;
195         _X                                                      =       0;
196         _Y                                                      =       0;
197         _Z                                                      =       0;
198         _thresholdTable         =       NULL;
199         _thresholdMapper                =       NULL;
200         _scalarbarActor         =       NULL;
201         _thresholdActor         =       NULL;
202         _image                                  =       NULL;
203         _baseView                               =       NULL;
204         _imageReslicer                  =       vtkImageReslice::New();
205 }
206
207 //----------------------------------------------------------------------------
208 LayerImageBase::~LayerImageBase()
209 {
210 }
211
212 //----------------------------------------------------------------------------
213 void LayerImageBase::SetX(int x)
214 {
215         _X = x;
216 }
217
218 //----------------------------------------------------------------------------
219 void LayerImageBase::SetY(int y)
220 {
221         _Y = y;
222 }
223
224 //----------------------------------------------------------------------------
225 void LayerImageBase::SetZ(int z)
226 {
227         _Z = z;
228 }
229
230 //----------------------------------------------------------------------------
231 int LayerImageBase::GetX()  // virtual
232 {
233         return _X;
234 }
235
236 //----------------------------------------------------------------------------
237 int LayerImageBase::GetY()  // virtual
238 {
239         return _Y;
240 }
241 //----------------------------------------------------------------------------
242 int LayerImageBase::GetZ()  // virtual
243 {
244         return _Z;
245 }
246
247
248 //----------------------------------------------------------------------------
249 vtkImageData* LayerImageBase::GetImage()
250 {
251         return _image;
252 }
253
254 //----------------------------------------------------------------------------
255 bool LayerImageBase::GetActorPresent()
256 {
257         return _actorPresent;
258 }
259
260
261
262 //----------------------------------------------------------------------------
263 void LayerImageBase::SetImage(vtkImageData* image)
264 {
265         _image = image;
266 }
267
268 //----------------------------------------------------------------------------
269 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
270 {
271         if (_baseView==NULL)
272    {
273                 _baseView = baseview;
274 // Old EED 2014 11 13
275 //      vtkInteractorStyleBaseView *isbv    = (vtkInteractorStyleBaseView*)(_baseView->GetInteractorStyleBaseView());
276 //      isbv->AddInteractorStyleMaracas( new ColorLayerImageBasevtkInteractor(this) );
277
278                 ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
279                 _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
280                 colorlayerobserver->SetLayerImageBase(this);
281
282         } // if _baseView
283 }
284
285 //----------------------------------------------------------------------------
286 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
287 {
288         return _baseView;
289 }
290
291 //----------------------------------------------------------------------------
292 void LayerImageBase::Refresh()
293 {
294     if (_baseView!=NULL)
295     {
296         _baseView->Refresh();
297     }
298 }
299
300
301 //----------------------------------------------------------------------------
302 vtkLookupTable* LayerImageBase::GetThresholdTable()
303 {
304         return _thresholdTable;
305 }
306
307 //----------------------------------------------------------------------------
308 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
309 {
310         int ext[6];
311         _image->GetWholeExtent(ext);
312
313         if (x<0)
314         {
315                 x=0;
316         }
317
318         if (y<0)
319         {
320                 y=0;
321         }
322
323         if (z<0)
324         {
325                 z=0;
326         }
327
328         if ( x > (ext[1]-ext[0]) )
329         {
330                 z=ext[1]-ext[0];
331         }
332
333         if ( y > (ext[3]-ext[2]) )
334         {
335                 z=ext[3]-ext[2];
336         }
337
338         if ( z > (ext[5]-ext[4]) )
339         {
340                 z=ext[5]-ext[4];
341         }
342
343
344 }
345
346 //----------------------------------------------------------------------------
347 void LayerImageBase::onThreshold()
348 {
349         if ((_image!=NULL) && (_baseView!=NULL))
350         {
351                 double spc[3];
352                 GetImage()-> GetSpacing(spc);
353                 double x=GetX();
354                 double y=GetY();
355                 double z=GetZ();
356                 CleanXYZ(x,y,z);
357
358                 x = x*spc[0];
359                 y = y*spc[1];
360                 z = z*spc[2];
361
362                 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
363                 int directionViewer=0;
364                 if (camera->GetParallelProjection()==1)
365                 {
366                         wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
367                         directionViewer =  wxvtk2dbasevie->GetDirection();
368                 } // ParallelProjection
369
370
371                 if (!GetActorPresent())
372                 {
373                         if (_thresholdTable==NULL)
374                         {
375                                 //Lookup Table
376                                 _thresholdTable = vtkLookupTable::New();
377                         } // _thresholdTable
378
379                         if (_thresholdMapper==NULL)
380                         {
381                                 _thresholdMapper = vtkImageMapToColors::New( );
382                         }
383
384                         if (_thresholdActor==NULL)
385                         {
386                                 _thresholdActor = vtkImageActor::New( );
387                                 _scalarbarActor = vtkScalarBarActor::New();
388                                 _thresholdActor->SetOpacity( 0.6 );
389                                 _thresholdActor->InterpolateOn(  );
390                                 if (directionViewer==0)
391                                 {
392                                         _thresholdActor->SetPosition( 900-1,0,0 );
393                                         _thresholdActor->RotateY( -90 );
394                                 }
395                                 if (directionViewer==1)
396                                 {
397                                         _thresholdActor->SetPosition( 0,-900-1, 0 );
398                                         _thresholdActor->RotateX( 90 );
399                                 }
400                                 if (directionViewer==2)
401                                 {
402                                         _thresholdActor->SetPosition( 0,0, -900-1 );
403                                 }
404                         } // _thresholdActor
405                         _baseView->GetRenderer()->AddActor( _thresholdActor );
406                         _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
407                         _actorPresent = true;
408                 }  // !GetActorPresent()
409
410                 ConfigLookupTable();  // virtual method
411                 _imageReslicer->SetInput( GetImage() );
412                 _imageReslicer->SetInformationInput( GetImage() );
413
414                 if (directionViewer==0)
415                 {
416                         _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0,  -1,0,0 );
417                         _imageReslicer->SetResliceAxesOrigin(x,0,0);
418                 }
419
420                 if (directionViewer==1)
421                 {
422                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1,  0,-1,0 );
423                         _imageReslicer->SetResliceAxesOrigin(0,y,0);
424                 }
425
426                 if (directionViewer==2)
427                 {
428                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
429                         _imageReslicer->SetResliceAxesOrigin(0,0,z);
430                 }
431
432
433                 _imageReslicer->SetOutputDimensionality(2);
434 //              _imageReslicer->SetInterpolationModeToLinear();
435                 _imageReslicer->SetInterpolationModeToNearestNeighbor();
436
437                 vtkImageData *img = _imageReslicer->GetOutput();
438 //              img->Update();
439 //              img->UpdateInformation();
440
441 //              _thresholdTable->Update();
442                 _thresholdMapper->SetInput( img );
443                 _thresholdMapper->SetLookupTable( _thresholdTable );
444                 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
445                 
446 //              _scalarbarActor->SetLookupTable( _thresholdTable );
447                 _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
448
449                 _scalarbarActor->SetTitle("Value");
450                 _scalarbarActor->SetNumberOfLabels(5);
451 //              _scalarbarActor->SetTextPad(4);  ??
452
453                 } // _image
454 }
455
456
457
458 //----------------------------------------------------------------------------
459 void LayerImageBase::onThresholdChange()
460 {
461         if (_actorPresent)
462         {
463                 onThreshold();
464         }
465 }
466
467 //----------------------------------------------------------------------------
468 void LayerImageBase::onThresholdInterpolation(bool interpolate)
469 {
470         if (_thresholdActor!=NULL)
471         {
472                 if (interpolate)
473                 {
474                         _thresholdActor->InterpolateOn( );
475                 }
476                 else
477                 {
478                         _thresholdActor->InterpolateOff( );
479                 }
480         }
481 }
482
483 //----------------------------------------------------------------------------
484 void LayerImageBase::onThresholdChangeOpacity (int opacity)
485 {
486         if (_actorPresent)
487         {
488                 _thresholdActor->SetOpacity(opacity*0.1);
489         }
490 }
491
492 //----------------------------------------------------------------------------
493 void LayerImageBase::onThresholdRemove()
494 {
495         if (_actorPresent)
496         {
497                 wxVtkBaseView * baseView = _baseView;
498                 baseView->GetRenderer()->RemoveActor( _thresholdActor );
499                 baseView->GetRenderer()->RemoveActor( _scalarbarActor );
500
501                 _actorPresent = false;
502         }
503 }
504
505 //----------------------------------------------------------------------------
506 vtkLookupTable *LayerImageBase::GetvtkLookupTable()
507 {
508     return _thresholdTable;
509 }
510
511 //----------------------------------------------------------------------------
512 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
513 {
514     return _imageReslicer;
515 }
516
517
518
519 // EOF
520