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