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