]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/LayerImageBase.cxx
1e402d3b35785d86341c215e7f797ca9285c2487
[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         _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 #endif
247
248 }
249
250 //------------------------------------------------------------------------------
251 void LayerImageBase::SetSpcOriginalLayer(double spc[3])
252 {
253         _spcOriginalLayer[0] = spc[0];
254         _spcOriginalLayer[1] = spc[1];
255         _spcOriginalLayer[2] = spc[2];
256 }
257
258 //------------------------------------------------------------------------------
259 void LayerImageBase::SetNewSpacingLayer(double spc[3])
260 {
261         _newSpcLayer[0]=spc[0];
262         _newSpcLayer[1]=spc[1];
263         _newSpcLayer[2]=spc[2];
264         _imageChangeInformation->SetOutputSpacing( _newSpcLayer );  
265
266 }
267
268
269 //------------------------------------------------------------------------------
270 void LayerImageBase::SetSpcBase(double spc[3])
271 {
272         _spcBase[0] = spc[0];
273         _spcBase[1] = spc[1];
274         _spcBase[2] = spc[2];
275 }
276
277 //------------------------------------------------------------------------------
278 void LayerImageBase::SetDimensionOriginalLayer(int dim[3])
279 {
280         _dimOriginalLayer[0] = dim[0];
281         _dimOriginalLayer[1] = dim[1];
282         _dimOriginalLayer[2] = dim[2];
283 }
284
285 //------------------------------------------------------------------------------
286 void LayerImageBase::SetDimensionBase(int dim[3])
287 {
288         _dimBase[0] = dim[0];
289         _dimBase[1] = dim[1];
290         _dimBase[2] = dim[2];
291 }
292
293
294
295 //------------------------------------------------------------------------------
296 void LayerImageBase::GetSpcOriginalLayer(double *spc)
297 {
298         spc[0] = _spcOriginalLayer[0];
299         spc[1] = _spcOriginalLayer[1];
300         spc[2] = _spcOriginalLayer[2];
301 }
302
303 //------------------------------------------------------------------------------
304 void LayerImageBase::GetSpcBase(double *spc)
305 {
306         spc[0] = _spcBase[0];
307         spc[1] = _spcBase[1];
308         spc[2] = _spcBase[2];
309 }
310
311 //------------------------------------------------------------------------------
312 void LayerImageBase::GetDimensionOriginalLayer(int *dim)
313 {
314         dim[0] = _dimOriginalLayer[0];
315         dim[1] = _dimOriginalLayer[1];
316         dim[2] = _dimOriginalLayer[2];
317 }
318
319 //------------------------------------------------------------------------------
320 void LayerImageBase::GetDimensionBase(int *dim)
321 {
322         dim[0] = _dimBase[0];
323         dim[1] = _dimBase[1];
324         dim[2] = _dimBase[2];
325 }
326
327
328 //------------------------------------------------------------------------------
329 void LayerImageBase::SetColorBarPosition(std::vector<int> &colorbarposition)
330 {
331    _colorBarPosition=colorbarposition;
332 }
333
334 //------------------------------------------------------------------------------
335 void LayerImageBase::SetRangeForColorBar(std::vector<double> &range)
336 {
337    _rangeForColorBar=range;
338 }
339
340
341 //------------------------------------------------------------------------------
342 void LayerImageBase::GetRangeForColorBar(double &minRange, double &maxRange)
343 {
344         if (_rangeForColorBar.size()==2)
345         {
346                 minRange = _rangeForColorBar[0];        
347                 maxRange = _rangeForColorBar[1];
348         } else {
349                 minRange = _range[0];   
350                 maxRange = _range[1];
351         }
352 }
353
354
355
356 //----------------------------------------------------------------------------
357 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
358 {
359         if (_baseView==NULL)
360    {
361                 _baseView = baseview;
362                 ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
363                 _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
364                 colorlayerobserver->SetLayerImageBase(this);
365         } // if _baseView
366 }
367
368 //----------------------------------------------------------------------------
369 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
370 {
371         return _baseView;
372 }
373
374 //----------------------------------------------------------------------------
375 void LayerImageBase::Refresh()
376 {
377     if (_baseView!=NULL)
378     {
379         _baseView->Refresh();
380     }
381 }
382
383
384 //----------------------------------------------------------------------------
385 vtkLookupTable* LayerImageBase::GetThresholdTable()
386 {
387         return _thresholdTable;
388 }
389
390 //----------------------------------------------------------------------------
391 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
392 {
393         if (x<0)
394         {
395                 x=0;
396         }
397         if (y<0)
398         {
399                 y=0;
400         }
401         if (z<0)
402         {
403                 z=0;
404         }
405         double maxSize;
406         maxSize=_dimOriginalLayer[0]*_newSpcLayer[0];
407         if ( x > maxSize  )
408         {
409                 x=maxSize;
410         }
411         maxSize=_dimOriginalLayer[1]*_newSpcLayer[1];
412         if ( y > maxSize  )
413         {
414                 y = maxSize;
415         }
416         maxSize=_dimOriginalLayer[2]*_newSpcLayer[2];
417         if ( z > maxSize  )
418         {
419                 z = maxSize;
420         }
421         
422 }
423
424 //----------------------------------------------------------------------------
425 void LayerImageBase::onThreshold()
426 {
427         if ((_image!=NULL) && (_baseView!=NULL))
428         {
429                 double x=GetX();
430                 double y=GetY();
431                 double z=GetZ();
432
433
434                 x = x*_spcBase[0];
435                 y = y*_spcBase[1];
436                 z = z*_spcBase[2];
437
438
439                 CleanXYZ(x,y,z);
440
441                 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
442                 int directionViewer=0;
443                 if (camera->GetParallelProjection()==1)
444                 {
445                         wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
446                         directionViewer =  wxvtk2dbasevie->GetDirection();
447                 } // ParallelProjection
448
449
450                 if (!GetActorPresent())
451                 {
452                         if (_thresholdTable==NULL)
453                         {
454                                 //Lookup Table
455                                 _thresholdTable = vtkLookupTable::New();
456                         } // _thresholdTable
457
458                         if (_thresholdMapper==NULL)
459                         {
460                                 _thresholdMapper = vtkImageMapToColors::New( );
461                         }
462
463                         if (_thresholdActor==NULL)
464                         {
465                                 _thresholdActor = vtkImageActor::New( );
466                                 _scalarbarActor = vtkScalarBarActor::New();
467                                 if (_colorBarPosition.size()==2)
468                                 { 
469                                         _scalarbarActor->SetDisplayPosition(_colorBarPosition[0],_colorBarPosition[1]);
470                                 } else {
471                                         _scalarbarActor->SetDisplayPosition(0,0);
472                                 }
473
474                                 _thresholdActor->SetOpacity( 1 );
475                                 _thresholdActor->InterpolateOn(  );
476                                 
477                                 if (directionViewer==0)
478                                 {
479                                         _thresholdActor->SetPosition( 900-1,0,0 );
480                                         _thresholdActor->RotateY( -90 );
481                                 }
482                                 if (directionViewer==1)
483                                 {
484                                         _thresholdActor->SetPosition( 0,-900-1, 0 );
485                                         _thresholdActor->RotateX( 90 );
486                                 }
487                                 if (directionViewer==2)
488                                 {
489                                         _thresholdActor->SetPosition( 0,0, -900-1 );
490                                 }
491                         } // _thresholdActor
492                         _baseView->GetRenderer()->AddActor( _thresholdActor );
493                         _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
494                         _actorPresent = true;
495                 }  // !GetActorPresent()
496
497                 ConfigLookupTable();  // virtual method
498
499 //EED 2017-01-01 Migration VTK7
500 #if VTK_MAJOR_VERSION <= 5
501                 _imageReslicer->SetInput( GetImage() );
502 #else
503                 _imageReslicer->SetInputData( GetImage() );
504 #endif
505
506                 _imageReslicer->SetInformationInput( GetImage() );
507
508                 if (directionViewer==0)
509                 {
510                         _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0,  -1,0,0 );
511                         _imageReslicer->SetResliceAxesOrigin(x,0,0);
512                 }
513
514                 if (directionViewer==1)
515                 {
516                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1,  0,-1,0 );
517                         _imageReslicer->SetResliceAxesOrigin(0,y,0);
518                 }
519
520                 if (directionViewer==2)
521                 {
522                         _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
523                         _imageReslicer->SetResliceAxesOrigin(0,0,z);
524                 }
525
526                 _imageReslicer->SetOutputDimensionality(2);
527 //              _imageReslicer->SetInterpolationModeToLinear();
528                 _imageReslicer->SetInterpolationModeToNearestNeighbor();
529                 _imageReslicer->Modified();
530
531
532                 vtkImageData *img = _imageReslicer->GetOutput();
533 //              img->Update();
534 //              img->UpdateInformation();
535
536 //              _thresholdTable->Update();
537
538 //EED 2017-01-01 Migration VTK7
539 #if VTK_MAJOR_VERSION <= 5
540                 _thresholdMapper->SetInput( img );
541 #else
542                 _thresholdMapper->SetInputData( img );
543 #endif
544
545                 _thresholdMapper->SetLookupTable( _thresholdTable );
546
547
548 //EED 2017-01-01 Migration VTK7
549 #if VTK_MAJOR_VERSION <= 5
550                 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
551 #else
552                 _thresholdActor->SetInputData( _thresholdMapper->GetOutput() );
553 #endif
554
555                 
556                 _scalarbarActor->SetLookupTable( _thresholdTable );
557 //              _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
558                 _scalarbarActor->SetTitle("Value");
559                 _scalarbarActor->SetNumberOfLabels(4);
560                 int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
561                 _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
562
563 //              _scalarbarActor->SetTextPad(4);  ??
564
565
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         }
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
641
642 // EOF
643