]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/LayerImageBase.cxx
#2440 creaMaracasVisu Feature New Normal - Add Active option to the ColorLayer Box
[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         if (_baseView==NULL)
221         {
222           _baseView = baseview;
223           vtkInteractorStyleBaseView *isbv    = (vtkInteractorStyleBaseView*)(_baseView->GetInteractorStyleBaseView());
224           isbv->AddInteractorStyleMaracas( new ColorLayerImageBasevtkInteractor(this) );
225         } // if _baseView
226 }
227
228 //----------------------------------------------------------------------------
229 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
230 {
231         return _baseView;
232 }
233
234 //----------------------------------------------------------------------------
235 void LayerImageBase::Refresh()
236 {
237     if (_baseView!=NULL)
238     {
239         _baseView->Refresh();
240     }
241 }
242
243
244 //----------------------------------------------------------------------------
245 vtkLookupTable* LayerImageBase::GetThresholdTable()
246 {
247         return _thresholdTable;
248 }
249
250 //----------------------------------------------------------------------------
251 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
252 {
253         int ext[6];
254         _image->GetWholeExtent(ext);
255
256         if (x<0)
257         {
258                 x=0;
259         }
260
261         if (y<0)
262         {
263                 y=0;
264         }
265
266         if (z<0)
267         {
268                 z=0;
269         }
270
271         if ( x > (ext[1]-ext[0]) )
272         {
273                 z=ext[1]-ext[0];
274         }
275
276         if ( y > (ext[3]-ext[2]) )
277         {
278                 z=ext[3]-ext[2];
279         }
280
281         if ( z > (ext[5]-ext[4]) )
282         {
283                 z=ext[5]-ext[4];
284         }
285
286
287 }
288
289 //----------------------------------------------------------------------------
290 void LayerImageBase::onThreshold()
291 {
292         if ((_image!=NULL) && (_baseView!=NULL))
293         {
294                 double spc[3];
295                 GetImage()-> GetSpacing(spc);
296                 double x=GetX();
297                 double y=GetY();
298                 double z=GetZ();
299                 CleanXYZ(x,y,z);
300
301                 x = x*spc[0];
302                 y = y*spc[1];
303                 z = z*spc[2];
304
305                 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
306                 int directionViewer=0;
307                 if (camera->GetParallelProjection()==1)
308                 {
309                         wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
310                         directionViewer =  wxvtk2dbasevie->GetDirection();
311                 } // ParallelProjection
312
313
314                 if (!GetActorPresent())
315                 {
316                         if (_thresholdTable==NULL)
317                         {
318                                 //Lookup Table
319                                 _thresholdTable = vtkLookupTable::New();
320                         } // _thresholdTable
321
322                         if (_thresholdMapper==NULL)
323                         {
324                                 _thresholdMapper = vtkImageMapToColors::New( );
325                         }
326
327                         if (_thresholdActor==NULL)
328                         {
329                                 _thresholdActor = vtkImageActor::New( );
330                                 _thresholdActor->SetOpacity( 0.6 );
331                                 _thresholdActor->InterpolateOn(  );
332                                 if (directionViewer==0)
333                                 {
334                                         _thresholdActor->SetPosition( 900-1,0,0 );
335                                         _thresholdActor->RotateY( -90 );
336                                 }
337                                 if (directionViewer==1)
338                                 {
339                                         _thresholdActor->SetPosition( 0,-900-1, 0 );
340                                         _thresholdActor->RotateX( 90 );
341                                 }
342                                 if (directionViewer==2)
343                                 {
344                                         _thresholdActor->SetPosition( 0,0, -900-1 );
345                                 }
346                         } // _thresholdActor
347                         _baseView->GetRenderer()->AddActor( _thresholdActor );
348                         _actorPresent = true;
349                 }  // !GetActorPresent()
350
351                 ConfigLookupTable();  // virtual method
352                 _imageReslicer->SetInput( GetImage() );
353                 _imageReslicer->SetInformationInput( GetImage() );
354
355                 if (directionViewer==0)
356                 {
357                         _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0,  -1,0,0 );
358                         _imageReslicer->SetResliceAxesOrigin(x,0,0);
359                 }
360
361                 if (directionViewer==1)
362                 {
363                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1,  0,-1,0 );
364                         _imageReslicer->SetResliceAxesOrigin(0,y,0);
365                 }
366
367                 if (directionViewer==2)
368                 {
369                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
370                         _imageReslicer->SetResliceAxesOrigin(0,0,z);
371                 }
372
373
374                 _imageReslicer->SetOutputDimensionality(2);
375 //              _imageReslicer->SetInterpolationModeToLinear();
376                 _imageReslicer->SetInterpolationModeToNearestNeighbor();
377
378                 vtkImageData *img = _imageReslicer->GetOutput();
379 //              img->Update();
380 //              img->UpdateInformation();
381
382                 _thresholdMapper->SetInput( img );
383                 _thresholdMapper->SetLookupTable( _thresholdTable );
384                 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
385                 
386                 } // _image
387 }
388
389
390
391 //----------------------------------------------------------------------------
392 void LayerImageBase::onThresholdChange()
393 {
394         if (_actorPresent)
395         {
396                 onThreshold();
397         }
398 }
399
400 //----------------------------------------------------------------------------
401 void LayerImageBase::onThresholdInterpolation(bool interpolate)
402 {
403         if (_thresholdActor!=NULL)
404         {
405                 if (interpolate)
406                 {
407                         _thresholdActor->InterpolateOn( );
408                 }
409                 else
410                 {
411                         _thresholdActor->InterpolateOff( );
412                 }
413         }
414 }
415
416 //----------------------------------------------------------------------------
417 void LayerImageBase::onThresholdChangeOpacity (int opacity)
418 {
419         if (_actorPresent)
420         {
421                 _thresholdActor->SetOpacity(opacity*0.1);
422         }
423 }
424
425 //----------------------------------------------------------------------------
426 void LayerImageBase::onThresholdRemove()
427 {
428         if (_actorPresent)
429         {
430                 wxVtkBaseView * baseView = _baseView;
431                 baseView->GetRenderer()->RemoveActor( _thresholdActor );
432                 _actorPresent = false;
433         }
434 }
435
436 //----------------------------------------------------------------------------
437 vtkLookupTable *LayerImageBase::GetvtkLookupTable()
438 {
439     return _thresholdTable;
440 }
441
442 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
443 {
444     return _imageReslicer;
445 }
446
447
448
449 // EOF
450