]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/LayerImageBase.cxx
f2ebd6f981d0099dcfbbb481bcefa969de89ec17
[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         _imageChangeInformation =       vtkImageChangeInformation::New();
108 }
109
110 //----------------------------------------------------------------------------
111 LayerImageBase::~LayerImageBase()
112 {
113 }
114
115 //----------------------------------------------------------------------------
116 void LayerImageBase::SetX(int x)
117 {
118         _X = x;
119 }
120
121 //----------------------------------------------------------------------------
122 void LayerImageBase::SetY(int y)
123 {
124         _Y = y;
125 }
126
127 //----------------------------------------------------------------------------
128 void LayerImageBase::SetZ(int z)
129 {
130         _Z = z;
131 }
132
133 //----------------------------------------------------------------------------
134 int LayerImageBase::GetX()  // virtual
135 {
136         return _X;
137 }
138
139 //----------------------------------------------------------------------------
140 int LayerImageBase::GetY()  // virtual
141 {
142         return _Y;
143 }
144 //----------------------------------------------------------------------------
145 int LayerImageBase::GetZ()  // virtual
146 {
147         return _Z;
148 }
149
150
151 //----------------------------------------------------------------------------
152 vtkImageData* LayerImageBase::GetImage()
153 {
154 //      return _image;
155         vtkImageData *result;
156         if (_image==NULL)
157         {
158                 result = NULL;
159         } else {
160                 result = _imageChangeInformation->GetOutput();
161         }
162         return result;
163 }
164
165 //----------------------------------------------------------------------------
166 bool LayerImageBase::GetActorPresent()
167 {
168         return _actorPresent;
169 }
170
171
172 //----------------------------------------------------------------------------
173 void LayerImageBase::SetImage(vtkImageData* image)
174 {
175         int     ext[6];
176         double  spcBase[3];
177         int     dimensionOriginalLayer[3];
178         int     dimBase[6];
179
180         ext[0]=0;
181         ext[1]=0;
182         ext[2]=0;
183         ext[3]=0;
184         ext[4]=0;
185         ext[5]=0;
186         spcBase[0]=0;
187         spcBase[1]=0;
188         spcBase[2]=0;
189         spcBase[3]=0;
190         spcBase[4]=0;
191         spcBase[5]=0;
192
193         _image = image;
194         _image->Update();
195         _image->GetWholeExtent(ext);
196         dimensionOriginalLayer[0] = ext[1]-ext[0]+1;
197         dimensionOriginalLayer[1] = ext[3]-ext[2]+1;
198         dimensionOriginalLayer[2] = ext[5]-ext[4]+1;
199         SetDimensionOriginalLayer(dimensionOriginalLayer);
200         double  spcOriginalLayer[3];
201         _image->GetSpacing(spcOriginalLayer);
202         SetSpcOriginalLayer(spcOriginalLayer);
203         if (GetwxVtkBaseView()!=NULL)
204         {
205                 vtkImageData    *imagebase              =       GetwxVtkBaseView()->GetVtkBaseData()->GetImageData();
206                 if (imagebase!=NULL) 
207                 {       
208                         imagebase->GetWholeExtent(ext);
209                         dimBase[0]=ext[1]-ext[0]+1;                             
210                         dimBase[1]=ext[3]-ext[2]+1;                             
211                         dimBase[2]=ext[5]-ext[4]+1;                             
212                         SetDimensionBase(dimBase);
213                         imagebase->GetSpacing(spcBase);
214                         SetSpcBase(spcBase);
215                         SetX( dimBase[0]*spcBase[0]/2 );
216                         SetY( dimBase[1]*spcBase[1]/2 );
217                         SetZ( dimBase[2]*spcBase[2]/2 );
218                 } // if imagebase
219         } // if wxvtkbaseview
220         _image->GetScalarRange( _range );
221         _thresholdTable = vtkLookupTable::New();
222
223         _imageChangeInformation->SetInput(_image);
224 }
225
226 //------------------------------------------------------------------------------
227 void LayerImageBase::SetSpcOriginalLayer(double spc[3])
228 {
229         _spcOriginalLayer[0] = spc[0];
230         _spcOriginalLayer[1] = spc[1];
231         _spcOriginalLayer[2] = spc[2];
232 }
233
234 //------------------------------------------------------------------------------
235 void LayerImageBase::SetNewSpacingLayer(double spc[3])
236 {
237         _newSpcLayer[0]=spc[0];
238         _newSpcLayer[1]=spc[1];
239         _newSpcLayer[2]=spc[2];
240         _imageChangeInformation->SetOutputSpacing( _newSpcLayer );  
241
242 }
243
244
245 //------------------------------------------------------------------------------
246 void LayerImageBase::SetSpcBase(double spc[3])
247 {
248         _spcBase[0] = spc[0];
249         _spcBase[1] = spc[1];
250         _spcBase[2] = spc[2];
251 }
252
253 //------------------------------------------------------------------------------
254 void LayerImageBase::SetDimensionOriginalLayer(int dim[3])
255 {
256         _dimOriginalLayer[0] = dim[0];
257         _dimOriginalLayer[1] = dim[1];
258         _dimOriginalLayer[2] = dim[2];
259 }
260
261 //------------------------------------------------------------------------------
262 void LayerImageBase::SetDimensionBase(int dim[3])
263 {
264         _dimBase[0] = dim[0];
265         _dimBase[1] = dim[1];
266         _dimBase[2] = dim[2];
267 }
268
269
270
271 //------------------------------------------------------------------------------
272 void LayerImageBase::GetSpcOriginalLayer(double *spc)
273 {
274         spc[0] = _spcOriginalLayer[0];
275         spc[1] = _spcOriginalLayer[1];
276         spc[2] = _spcOriginalLayer[2];
277 }
278
279 //------------------------------------------------------------------------------
280 void LayerImageBase::GetSpcBase(double *spc)
281 {
282         spc[0] = _spcBase[0];
283         spc[1] = _spcBase[1];
284         spc[2] = _spcBase[2];
285 }
286
287 //------------------------------------------------------------------------------
288 void LayerImageBase::GetDimensionOriginalLayer(int *dim)
289 {
290         dim[0] = _dimOriginalLayer[0];
291         dim[1] = _dimOriginalLayer[1];
292         dim[2] = _dimOriginalLayer[2];
293 }
294
295 //------------------------------------------------------------------------------
296 void LayerImageBase::GetDimensionBase(int *dim)
297 {
298         dim[0] = _dimBase[0];
299         dim[1] = _dimBase[1];
300         dim[2] = _dimBase[2];
301 }
302
303
304 //------------------------------------------------------------------------------
305 void LayerImageBase::SetColorBarPosition(std::vector<int> &colorbarposition)
306 {
307    _colorBarPosition=colorbarposition;
308 }
309
310 //------------------------------------------------------------------------------
311 void LayerImageBase::SetRangeForColorBar(std::vector<double> &range)
312 {
313    _rangeForColorBar=range;
314 }
315
316
317 //------------------------------------------------------------------------------
318 void LayerImageBase::GetRangeForColorBar(double &minRange, double &maxRange)
319 {
320         if (_rangeForColorBar.size()==2)
321         {
322                 minRange = _rangeForColorBar[0];        
323                 maxRange = _rangeForColorBar[1];
324         } else {
325                 minRange = _range[0];   
326                 maxRange = _range[1];
327         }
328 }
329
330
331
332 //----------------------------------------------------------------------------
333 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
334 {
335         if (_baseView==NULL)
336    {
337                 _baseView = baseview;
338                 ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
339                 _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
340                 colorlayerobserver->SetLayerImageBase(this);
341         } // if _baseView
342 }
343
344 //----------------------------------------------------------------------------
345 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
346 {
347         return _baseView;
348 }
349
350 //----------------------------------------------------------------------------
351 void LayerImageBase::Refresh()
352 {
353     if (_baseView!=NULL)
354     {
355         _baseView->Refresh();
356     }
357 }
358
359
360 //----------------------------------------------------------------------------
361 vtkLookupTable* LayerImageBase::GetThresholdTable()
362 {
363         return _thresholdTable;
364 }
365
366 //----------------------------------------------------------------------------
367 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
368 {
369         if (x<0)
370         {
371                 x=0;
372         }
373         if (y<0)
374         {
375                 y=0;
376         }
377         if (z<0)
378         {
379                 z=0;
380         }
381         double maxSize;
382         maxSize=_dimOriginalLayer[0]*_newSpcLayer[0];
383         if ( x > maxSize  )
384         {
385                 x=maxSize;
386         }
387         maxSize=_dimOriginalLayer[1]*_newSpcLayer[1];
388         if ( y > maxSize  )
389         {
390                 y = maxSize;
391         }
392         maxSize=_dimOriginalLayer[2]*_newSpcLayer[2];
393         if ( z > maxSize  )
394         {
395                 z = maxSize;
396         }
397         
398 }
399
400 //----------------------------------------------------------------------------
401 void LayerImageBase::onThreshold()
402 {
403         if ((_image!=NULL) && (_baseView!=NULL))
404         {
405                 double x=GetX();
406                 double y=GetY();
407                 double z=GetZ();
408
409
410                 x = x*_spcBase[0];
411                 y = y*_spcBase[1];
412                 z = z*_spcBase[2];
413
414
415                 CleanXYZ(x,y,z);
416
417                 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
418                 int directionViewer=0;
419                 if (camera->GetParallelProjection()==1)
420                 {
421                         wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
422                         directionViewer =  wxvtk2dbasevie->GetDirection();
423                 } // ParallelProjection
424
425
426                 if (!GetActorPresent())
427                 {
428                         if (_thresholdTable==NULL)
429                         {
430                                 //Lookup Table
431                                 _thresholdTable = vtkLookupTable::New();
432                         } // _thresholdTable
433
434                         if (_thresholdMapper==NULL)
435                         {
436                                 _thresholdMapper = vtkImageMapToColors::New( );
437                         }
438
439                         if (_thresholdActor==NULL)
440                         {
441                                 _thresholdActor = vtkImageActor::New( );
442                                 _scalarbarActor = vtkScalarBarActor::New();
443                                 if (_colorBarPosition.size()==2)
444                                 { 
445                                         _scalarbarActor->SetDisplayPosition(_colorBarPosition[0],_colorBarPosition[1]);
446                                 } else {
447                                         _scalarbarActor->SetDisplayPosition(0,0);
448                                 }
449
450                                 _thresholdActor->SetOpacity( 0 );
451                                 _thresholdActor->InterpolateOn(  );
452                                 
453                                 if (directionViewer==0)
454                                 {
455                                         _thresholdActor->SetPosition( 900-1,0,0 );
456                                         _thresholdActor->RotateY( -90 );
457                                 }
458                                 if (directionViewer==1)
459                                 {
460                                         _thresholdActor->SetPosition( 0,-900-1, 0 );
461                                         _thresholdActor->RotateX( 90 );
462                                 }
463                                 if (directionViewer==2)
464                                 {
465                                         _thresholdActor->SetPosition( 0,0, -900-1 );
466                                 }
467                         } // _thresholdActor
468                         _baseView->GetRenderer()->AddActor( _thresholdActor );
469                         _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
470                         _actorPresent = true;
471                 }  // !GetActorPresent()
472
473                 ConfigLookupTable();  // virtual method
474                 _imageReslicer->SetInput( GetImage() );
475                 _imageReslicer->SetInformationInput( GetImage() );
476
477                 if (directionViewer==0)
478                 {
479                         _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0,  -1,0,0 );
480                         _imageReslicer->SetResliceAxesOrigin(x,0,0);
481                 }
482
483                 if (directionViewer==1)
484                 {
485                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1,  0,-1,0 );
486                         _imageReslicer->SetResliceAxesOrigin(0,y,0);
487                 }
488
489                 if (directionViewer==2)
490                 {
491                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
492                         _imageReslicer->SetResliceAxesOrigin(0,0,z);
493                 }
494
495                 _imageReslicer->SetOutputDimensionality(2);
496 //              _imageReslicer->SetInterpolationModeToLinear();
497                 _imageReslicer->SetInterpolationModeToNearestNeighbor();
498                 _imageReslicer->Modified();
499
500
501                 vtkImageData *img = _imageReslicer->GetOutput();
502 //              img->Update();
503 //              img->UpdateInformation();
504
505 //              _thresholdTable->Update();
506                 _thresholdMapper->SetInput( img );
507                 _thresholdMapper->SetLookupTable( _thresholdTable );
508                 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
509                 
510                 _scalarbarActor->SetLookupTable( _thresholdTable );
511 //              _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
512                 _scalarbarActor->SetTitle("Value");
513                 _scalarbarActor->SetNumberOfLabels(4);
514                 int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
515                 _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
516
517 //              _scalarbarActor->SetTextPad(4);  ??
518
519
520                 } // _image
521 }
522
523
524
525 //----------------------------------------------------------------------------
526 void LayerImageBase::onThresholdChange()
527 {
528         if (_actorPresent)
529         {
530                 onThreshold();
531         }
532 }
533
534 //----------------------------------------------------------------------------
535 void LayerImageBase::onThresholdInterpolation(bool interpolate)
536 {
537         if (_thresholdActor!=NULL)
538         {
539                 if (interpolate)
540                 {
541                         _thresholdActor->InterpolateOn( );
542                 }
543                 else
544                 {
545                         _thresholdActor->InterpolateOff( );
546                 }
547         }
548 }
549
550 //----------------------------------------------------------------------------
551 void LayerImageBase::onThresholdChangeOpacity (int opacity)
552 {
553         if (_actorPresent)
554         {
555                 _thresholdActor->SetOpacity(opacity/100.0);
556         }
557 }
558
559 //----------------------------------------------------------------------------
560 void LayerImageBase::onThresholdRemove()
561 {
562         if (_actorPresent)
563         {
564                 wxVtkBaseView * baseView = _baseView;
565                 baseView->GetRenderer()->RemoveActor( _thresholdActor );
566                 baseView->GetRenderer()->RemoveActor( _scalarbarActor );
567
568                 _actorPresent = false;
569         }
570 }
571
572 //----------------------------------------------------------------------------
573 vtkLookupTable *LayerImageBase::GetvtkLookupTable()
574 {
575     return _thresholdTable;
576 }
577
578 //----------------------------------------------------------------------------
579 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
580 {
581     return _imageReslicer;
582 }
583
584 //----------------------------------------------------------------------------
585 void LayerImageBase::GetImageScalarRange()
586 {
587         double min,max;
588         GetRangeForColorBar(min,max);
589 //      _image->GetScalarRange(_range);
590         _range[0]=min;
591         _range[1]=max;
592 }
593
594
595
596 // EOF
597