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