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