]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/LayerImageBase.cxx
#3546 bug ColorLayer with images all 0
[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 "creawxVTKRenderWindowInteractor.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
196 //EED 2017-01-01 Migration VTK7
197 #if VTK_MAJOR_VERSION <= 5
198         _image->Update();
199         _image->GetWholeExtent(ext);
200 #else
201         _image->GetExtent(ext);
202 #endif
203
204         dimensionOriginalLayer[0] = ext[1]-ext[0]+1;
205         dimensionOriginalLayer[1] = ext[3]-ext[2]+1;
206         dimensionOriginalLayer[2] = ext[5]-ext[4]+1;
207         SetDimensionOriginalLayer(dimensionOriginalLayer);
208         double  spcOriginalLayer[3];
209         _image->GetSpacing(spcOriginalLayer);
210         SetSpcOriginalLayer(spcOriginalLayer);
211         if (GetwxVtkBaseView()!=NULL)
212         {
213                 vtkImageData    *imagebase              =       GetwxVtkBaseView()->GetVtkBaseData()->GetImageData();
214                 if (imagebase!=NULL) 
215                 {       
216
217 //EED 2017-01-01 Migration VTK7
218 #if VTK_MAJOR_VERSION <= 5
219                         imagebase->GetWholeExtent(ext);
220 #else
221                         imagebase->GetExtent(ext);
222 #endif
223
224                         dimBase[0]=ext[1]-ext[0]+1;                             
225                         dimBase[1]=ext[3]-ext[2]+1;                             
226                         dimBase[2]=ext[5]-ext[4]+1;                             
227                         SetDimensionBase(dimBase);
228                         imagebase->GetSpacing(spcBase);
229                         SetSpcBase(spcBase);
230
231                         if ((_X<0) || (_X>dimBase[0])) { SetX( dimBase[0]/2 ); } 
232                         if ((_Y<0) || (_Y>dimBase[1])) { SetY( dimBase[1]/2 ); } 
233                         if ((_Z<0) || (_Z>dimBase[2])) { SetZ( dimBase[2]/2 ); } 
234
235
236                 } // if imagebase
237         } // if wxvtkbaseview
238         _image->GetScalarRange( _range );
239 //      _thresholdTable = vtkLookupTable::New();
240
241
242 //EED 2017-01-01 Migration VTK7
243 #if VTK_MAJOR_VERSION <= 5
244         _imageChangeInformation->SetInput(_image);
245 #else
246         _imageChangeInformation->SetInputData(_image);
247         _imageChangeInformation->Update();
248 #endif
249
250 }
251
252 //------------------------------------------------------------------------------
253 void LayerImageBase::SetSpcOriginalLayer(double spc[3])
254 {
255         _spcOriginalLayer[0] = spc[0];
256         _spcOriginalLayer[1] = spc[1];
257         _spcOriginalLayer[2] = spc[2];
258 }
259
260 //------------------------------------------------------------------------------
261 void LayerImageBase::SetNewSpacingLayer(double spc[3])
262 {
263         _newSpcLayer[0] = spc[0];
264         _newSpcLayer[1] = spc[1];
265         _newSpcLayer[2] = spc[2];
266         _imageChangeInformation->SetOutputSpacing( _newSpcLayer );  
267     _imageChangeInformation->Update();
268 }
269
270
271 //------------------------------------------------------------------------------
272 void LayerImageBase::SetSpcBase(double spc[3])
273 {
274         _spcBase[0] = spc[0];
275         _spcBase[1] = spc[1];
276         _spcBase[2] = spc[2];
277 }
278
279 //------------------------------------------------------------------------------
280 void LayerImageBase::SetDimensionOriginalLayer(int dim[3])
281 {
282         _dimOriginalLayer[0] = dim[0];
283         _dimOriginalLayer[1] = dim[1];
284         _dimOriginalLayer[2] = dim[2];
285 }
286
287 //------------------------------------------------------------------------------
288 void LayerImageBase::SetDimensionBase(int dim[3])
289 {
290         _dimBase[0] = dim[0];
291         _dimBase[1] = dim[1];
292         _dimBase[2] = dim[2];
293 }
294
295
296
297 //------------------------------------------------------------------------------
298 void LayerImageBase::GetSpcOriginalLayer(double *spc)
299 {
300         spc[0] = _spcOriginalLayer[0];
301         spc[1] = _spcOriginalLayer[1];
302         spc[2] = _spcOriginalLayer[2];
303 }
304
305 //------------------------------------------------------------------------------
306 void LayerImageBase::GetSpcBase(double *spc)
307 {
308         spc[0] = _spcBase[0];
309         spc[1] = _spcBase[1];
310         spc[2] = _spcBase[2];
311 }
312
313 //------------------------------------------------------------------------------
314 void LayerImageBase::GetDimensionOriginalLayer(int *dim)
315 {
316         dim[0] = _dimOriginalLayer[0];
317         dim[1] = _dimOriginalLayer[1];
318         dim[2] = _dimOriginalLayer[2];
319 }
320
321 //------------------------------------------------------------------------------
322 void LayerImageBase::GetDimensionBase(int *dim)
323 {
324         dim[0] = _dimBase[0];
325         dim[1] = _dimBase[1];
326         dim[2] = _dimBase[2];
327 }
328
329 //------------------------------------------------------------------------------
330 void LayerImageBase::SetColorBarPosition(std::vector<int> &colorbarposition)
331 {
332    _colorBarPosition=colorbarposition;
333    UpdateColorBarPosition();
334 }
335
336 //------------------------------------------------------------------------------
337 void LayerImageBase::UpdateColorBarPosition()
338 {
339         if ( _scalarbarActor!=NULL)
340         {
341                 if (_colorBarPosition.size()==2)
342                 { 
343                         _scalarbarActor->SetDisplayPosition( _colorBarPosition[0] , _colorBarPosition[1] );
344                 } else {
345                         _scalarbarActor->SetDisplayPosition(0,0);
346                 }
347         } // if   
348 }//
349
350 //------------------------------------------------------------------------------
351 void LayerImageBase::SetRangeForColorBar(std::vector<double> &range)
352 {
353    _rangeForColorBar=range;
354 }
355
356
357 //------------------------------------------------------------------------------
358 void LayerImageBase::GetRangeForColorBar(double &minRange, double &maxRange)
359 {
360         if (_rangeForColorBar.size()==2)
361         {
362                 minRange = _rangeForColorBar[0];        
363                 maxRange = _rangeForColorBar[1];
364         } else {
365                 minRange = _range[0];   
366                 maxRange = _range[1];
367         }
368 }
369
370
371
372 //----------------------------------------------------------------------------
373 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
374 {
375         if (_baseView==NULL)
376    {
377                 _baseView = baseview;
378                 ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
379                 _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
380                 colorlayerobserver->SetLayerImageBase(this);
381         } // if _baseView
382 }
383
384 //----------------------------------------------------------------------------
385 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
386 {
387         return _baseView;
388 }
389
390 //----------------------------------------------------------------------------
391 void LayerImageBase::Refresh()
392 {
393     if (_baseView!=NULL)
394     {
395         _baseView->Refresh();
396     }
397 }
398
399
400 //----------------------------------------------------------------------------
401 vtkLookupTable* LayerImageBase::GetThresholdTable()
402 {
403         return _thresholdTable;
404 }
405
406 //----------------------------------------------------------------------------
407 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
408 {
409         if (x<0)
410         {
411                 x=0;
412         }
413         if (y<0)
414         {
415                 y=0;
416         }
417         if (z<0)
418         {
419                 z=0;
420         }
421         double maxSize;
422         maxSize=_dimOriginalLayer[0]*_newSpcLayer[0];
423         if ( x > maxSize  )
424         {
425                 x=maxSize;
426         }
427         maxSize=_dimOriginalLayer[1]*_newSpcLayer[1];
428         if ( y > maxSize  )
429         {
430                 y = maxSize;
431         }
432         maxSize=_dimOriginalLayer[2]*_newSpcLayer[2];
433         if ( z > maxSize  )
434         {
435                 z = maxSize;
436         }
437 }
438
439 //----------------------------------------------------------------------------
440 void LayerImageBase::onThreshold()
441 {
442         if ((_image!=NULL) && (_baseView!=NULL))
443         {
444                 double x=GetX();
445                 double y=GetY();
446                 double z=GetZ();
447
448                 x = x*_spcBase[0];
449                 y = y*_spcBase[1];
450                 z = z*_spcBase[2];
451
452                 CleanXYZ(x,y,z);
453
454                 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
455                 int directionViewer=0;
456                 if (camera->GetParallelProjection()==1)
457                 {
458                         wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
459                         directionViewer =  wxvtk2dbasevie->GetDirection();
460                 } // ParallelProjection
461
462                 if (!GetActorPresent())
463                 {
464                         if (_thresholdTable==NULL)
465                         {
466                                 //Lookup Table
467                                 _thresholdTable = vtkLookupTable::New();
468                         } // _thresholdTable
469
470                         if (_thresholdMapper==NULL)
471                         {
472                                 _thresholdMapper = vtkImageMapToColors::New( );
473                         }
474
475                         if (_thresholdActor==NULL)
476                         {
477                                 _thresholdActor = vtkImageActor::New( );
478                                 _scalarbarActor = vtkScalarBarActor::New();
479                                 UpdateColorBarPosition();
480                                 _scalarbarActor->SetHeight(0.4);        //default  0.8
481                                 _scalarbarActor->SetWidth(0.10);    //default  0.17
482                                 _thresholdActor->SetOpacity( 1 );
483                                 _thresholdActor->InterpolateOn(  );
484                                 if (directionViewer==0)
485                                 {
486                                         _thresholdActor->SetPosition( 900-1,0,0 );
487                                         _thresholdActor->RotateY( -90 );
488                                 }
489                                 if (directionViewer==1)
490                                 {
491                                         _thresholdActor->SetPosition( 0,-900-1, 0 );
492                                         _thresholdActor->RotateX( 90 );
493                                 }
494                                 if (directionViewer==2)
495                                 {
496                                         _thresholdActor->SetPosition( 0,0, -900-1 );
497                                 }
498                         } // _thresholdActor
499
500                         _baseView->GetRenderer()->AddActor( _thresholdActor );
501                         _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
502                         _actorPresent = true;
503                 }  // !GetActorPresent()
504
505                 ConfigLookupTable();  // virtual method
506
507 //EED 2017-01-01 Migration VTK7
508 #if VTK_MAJOR_VERSION <= 5
509                 _imageReslicer->SetInput( GetImage() );
510 #else
511                 _imageReslicer->SetInputData( GetImage() );
512 #endif
513
514                 _imageReslicer->SetInformationInput( GetImage() );
515
516                 if (directionViewer==0)
517                 {
518                         _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0,  -1,0,0 );
519                         _imageReslicer->SetResliceAxesOrigin(x,0,0);
520                 }
521
522                 if (directionViewer==1)
523                 {
524                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1,  0,-1,0 );
525                         _imageReslicer->SetResliceAxesOrigin(0,y,0);
526                 }
527
528                 if (directionViewer==2)
529                 {
530                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
531                         _imageReslicer->SetResliceAxesOrigin(0,0,z);
532                 }
533
534                 _imageReslicer->SetOutputDimensionality(2);
535 //              _imageReslicer->SetInterpolationModeToLinear();
536                 _imageReslicer->SetInterpolationModeToNearestNeighbor();
537                 _imageReslicer->Modified();
538
539 //EED 2017-01-01 Migration VTK7
540 #if VTK_MAJOR_VERSION <= 5
541                 // ..
542 #else
543                 _imageReslicer->Update();
544 #endif
545                 vtkImageData *img = _imageReslicer->GetOutput();
546 //              img->Update();
547 //              img->UpdateInformation();
548 //              _thresholdTable->Update();
549
550 //EED 2017-01-01 Migration VTK7
551 #if VTK_MAJOR_VERSION <= 5
552                 _thresholdMapper->SetInput( img );
553 #else
554                 _thresholdMapper->SetInputData( img );
555 #endif
556                 _thresholdMapper->SetLookupTable( _thresholdTable );
557
558
559 //EED 2017-01-01 Migration VTK7
560 #if VTK_MAJOR_VERSION <= 5
561                 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
562 #else
563                 _thresholdMapper->Update();
564                 _thresholdActor->SetInputData( _thresholdMapper->GetOutput() );
565 #endif
566
567                 _thresholdActor->SetOpacity(_opacity);
568                 _scalarbarActor->SetLookupTable( _thresholdTable );
569 //              _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
570                 _scalarbarActor->SetTitle("Value");
571                 _scalarbarActor->SetNumberOfLabels(4);
572                 int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
573                 _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
574 //              _scalarbarActor->SetTextPad(4);  ??
575     } // if _image
576 }
577
578 //----------------------------------------------------------------------------
579 void LayerImageBase::onThresholdChange()
580 {
581         if (_actorPresent)
582         {
583                 onThreshold();
584         }
585 }
586
587 //----------------------------------------------------------------------------
588 void LayerImageBase::onThresholdInterpolation(bool interpolate)
589 {
590         if (_thresholdActor!=NULL)
591         {
592                 if (interpolate)
593                 {
594                         _thresholdActor->InterpolateOn( );
595                 } else {
596                         _thresholdActor->InterpolateOff( );
597                 } // if interpolate
598         } // if _thresholdActor
599 }
600
601 //----------------------------------------------------------------------------
602 void LayerImageBase::onThresholdChangeOpacity (int opacity)
603 {
604         if (_actorPresent)
605         {
606                 _thresholdActor->SetOpacity(opacity/100.0);
607         }
608 // EED 2017-12-17
609         _opacity = (double)opacity/100.0;
610 }
611
612 //----------------------------------------------------------------------------
613 void LayerImageBase::onThresholdRemove()
614 {
615         if (_actorPresent)
616         {
617                 wxVtkBaseView * baseView = _baseView;
618                 baseView->GetRenderer()->RemoveActor( _thresholdActor );
619                 baseView->GetRenderer()->RemoveActor( _scalarbarActor );
620                 _actorPresent = false;
621         }  // if _actorPresent
622 }
623
624 //----------------------------------------------------------------------------
625 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
626 {
627     return _imageReslicer;
628 }
629
630 //----------------------------------------------------------------------------
631 void LayerImageBase::GetImageScalarRange()
632 {
633         double min,max;
634         GetRangeForColorBar(min,max);
635 //      _image->GetScalarRange(_range);
636         _range[0] = min;
637         _range[1] = max;
638 }
639
640 vtkScalarsToColors* LayerImageBase::GetLookupTable()
641 {
642         return _thresholdTable;
643 }
644
645 // EOF
646