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