]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/LayerImageBase.cxx
#3547 Bug color layer Z position
[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                 x = x*_spcBase[0];
448                 y = y*_spcBase[1];
449                 z = z*_spcBase[2];
450                 CleanXYZ(x,y,z);
451                 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
452                 int directionViewer=0;
453                 if (camera->GetParallelProjection()==1)
454                 {
455                         wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
456                         directionViewer =  wxvtk2dbasevie->GetDirection();
457                 } // ParallelProjection
458
459                 if (!GetActorPresent())
460                 {
461                         if (_thresholdTable==NULL)
462                         {
463                                 //Lookup Table
464                                 _thresholdTable = vtkLookupTable::New();
465                         } // _thresholdTable
466
467                         if (_thresholdMapper==NULL)
468                         {
469                                 _thresholdMapper = vtkImageMapToColors::New( );
470                         }
471
472                         if (_thresholdActor==NULL)
473                         {
474                                 _thresholdActor = vtkImageActor::New( );
475                                 _scalarbarActor = vtkScalarBarActor::New();
476                                 UpdateColorBarPosition();
477                                 _scalarbarActor->SetHeight(0.4);        //default  0.8
478                                 _scalarbarActor->SetWidth(0.10);    //default  0.17
479                                 _thresholdActor->SetOpacity( 1 );
480                                 _thresholdActor->InterpolateOn(  );
481                                 if (directionViewer==0)
482                                 {
483                                         _thresholdActor->SetPosition( 900-1,0,0 );
484                                         _thresholdActor->RotateY( -90 );
485                                 }
486                                 if (directionViewer==1)
487                                 {
488                                         _thresholdActor->SetPosition( 0,-900-1, 0 );
489                                         _thresholdActor->RotateX( 90 );
490                                 }
491                                 if (directionViewer==2)
492                                 {
493                                         _thresholdActor->SetPosition( 0,0, -900+100 );
494                                 }
495                         } // _thresholdActor
496
497                         _baseView->GetRenderer()->AddActor( _thresholdActor );
498                         _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
499                         _actorPresent = true;
500                 }  // !GetActorPresent()
501
502                 ConfigLookupTable();  // virtual method
503
504 //EED 2017-01-01 Migration VTK7
505 #if VTK_MAJOR_VERSION <= 5
506                 _imageReslicer->SetInput( GetImage() );
507 #else
508                 _imageReslicer->SetInputData( GetImage() );
509 #endif
510
511                 _imageReslicer->SetInformationInput( GetImage() );
512
513                 if (directionViewer==0)
514                 {
515                         _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0,  -1,0,0 );
516                         _imageReslicer->SetResliceAxesOrigin(x,0,0);
517                 }
518
519                 if (directionViewer==1)
520                 {
521                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1,  0,-1,0 );
522                         _imageReslicer->SetResliceAxesOrigin(0,y,0);
523                 }
524
525                 if (directionViewer==2)
526                 {
527                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
528                         _imageReslicer->SetResliceAxesOrigin(0,0,z);
529                 }
530
531                 _imageReslicer->SetOutputDimensionality(2);
532 //              _imageReslicer->SetInterpolationModeToLinear();
533                 _imageReslicer->SetInterpolationModeToNearestNeighbor();
534                 _imageReslicer->Modified();
535
536 //EED 2017-01-01 Migration VTK7
537 #if VTK_MAJOR_VERSION <= 5
538                 // ..
539 #else
540                 _imageReslicer->Update();
541 #endif
542                 vtkImageData *img = _imageReslicer->GetOutput();
543 //              img->Update();
544 //              img->UpdateInformation();
545 //              _thresholdTable->Update();
546
547 //EED 2017-01-01 Migration VTK7
548 #if VTK_MAJOR_VERSION <= 5
549                 _thresholdMapper->SetInput( img );
550 #else
551                 _thresholdMapper->SetInputData( img );
552 #endif
553                 _thresholdMapper->SetLookupTable( _thresholdTable );
554
555
556 //EED 2017-01-01 Migration VTK7
557 #if VTK_MAJOR_VERSION <= 5
558                 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
559 #else
560                 _thresholdMapper->Update();
561                 _thresholdActor->SetInputData( _thresholdMapper->GetOutput() );
562 #endif
563
564                 _thresholdActor->SetOpacity(_opacity);
565                 _scalarbarActor->SetLookupTable( _thresholdTable );
566 //              _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
567                 _scalarbarActor->SetTitle("Value");
568                 _scalarbarActor->SetNumberOfLabels(4);
569                 int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
570                 _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
571 //              _scalarbarActor->SetTextPad(4);  ??
572     } // if _image
573 }
574
575 //----------------------------------------------------------------------------
576 void LayerImageBase::onThresholdChange()
577 {
578         if (_actorPresent)
579         {
580                 onThreshold();
581         }
582 }
583
584 //----------------------------------------------------------------------------
585 void LayerImageBase::onThresholdInterpolation(bool interpolate)
586 {
587         if (_thresholdActor!=NULL)
588         {
589                 if (interpolate)
590                 {
591                         _thresholdActor->InterpolateOn( );
592                 } else {
593                         _thresholdActor->InterpolateOff( );
594                 } // if interpolate
595         } // if _thresholdActor
596 }
597
598 //----------------------------------------------------------------------------
599 void LayerImageBase::onThresholdChangeOpacity (int opacity)
600 {
601         if (_actorPresent)
602         {
603                 _thresholdActor->SetOpacity(opacity/100.0);
604         }
605 // EED 2017-12-17
606         _opacity = (double)opacity/100.0;
607 }
608
609 //----------------------------------------------------------------------------
610 void LayerImageBase::onThresholdRemove()
611 {
612         if (_actorPresent)
613         {
614                 wxVtkBaseView * baseView = _baseView;
615                 baseView->GetRenderer()->RemoveActor( _thresholdActor );
616                 baseView->GetRenderer()->RemoveActor( _scalarbarActor );
617                 _actorPresent = false;
618         }  // if _actorPresent
619 }
620
621 //----------------------------------------------------------------------------
622 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
623 {
624     return _imageReslicer;
625 }
626
627 //----------------------------------------------------------------------------
628 void LayerImageBase::GetImageScalarRange()
629 {
630         double min,max;
631         GetRangeForColorBar(min,max);
632 //      _image->GetScalarRange(_range);
633         _range[0] = min;
634         _range[1] = max;
635 }
636
637 vtkScalarsToColors* LayerImageBase::GetLookupTable()
638 {
639         return _thresholdTable;
640 }
641
642 // EOF
643