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