1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
28 * \brief Class bbtk::ThresholdImageView .
31 #include "LayerImageBase.h"
33 #include "wxMPRBaseData.h"
34 #include "wxVtk2DBaseView.h"
35 #include "wxVTKRenderWindowInteractor.h"
36 #include "vtkTextProperty.h"
39 //---------------------------------------------------------------------------------------------
40 //---------------------------------------------------------------------------------------------
41 //---------------------------------------------------------------------------------------------
43 // Callback for the interaction
44 class ColorLayerObserver : public vtkCommand{
46 virtual char const *GetClassName() const { return "ColorLayerObserver";}
48 static ColorLayerObserver *New() { return new ColorLayerObserver; }
52 _layerimagebase = NULL;
55 virtual void Execute(vtkObject *wdg, unsigned long event, void* calldata)
57 if (_layerimagebase!= NULL)
60 wxVtkBaseView *wxvtkbaseview = _layerimagebase->GetwxVtkBaseView();
61 if (wxvtkbaseview!=NULL)
64 vtkMPRBaseData *vtkmprbasedata = (vtkMPRBaseData*)wxvtkbaseview->GetVtkBaseData();
65 if (vtkmprbasedata!=NULL)
67 _layerimagebase->SetX( vtkmprbasedata->GetX() );
68 _layerimagebase->SetY( vtkmprbasedata->GetY() );
69 _layerimagebase->SetZ( vtkmprbasedata->GetZ() );
70 _layerimagebase->onThresholdChange();
77 void SetLayerImageBase(LayerImageBase* layerimagebase)
79 _layerimagebase = layerimagebase;
83 LayerImageBase* _layerimagebase;
88 //---------------------------------------------------------------------------------------------
89 //---------------------------------------------------------------------------------------------
90 //---------------------------------------------------------------------------------------------
94 LayerImageBase::LayerImageBase()
96 _actorPresent = false;
100 _thresholdTable = NULL;
101 _thresholdMapper = NULL;
102 _scalarbarActor = NULL;
103 _thresholdActor = NULL;
106 _imageReslicer = vtkImageReslice::New();
109 //----------------------------------------------------------------------------
110 LayerImageBase::~LayerImageBase()
114 //----------------------------------------------------------------------------
115 void LayerImageBase::SetX(int x)
120 //----------------------------------------------------------------------------
121 void LayerImageBase::SetY(int y)
126 //----------------------------------------------------------------------------
127 void LayerImageBase::SetZ(int z)
132 //----------------------------------------------------------------------------
133 int LayerImageBase::GetX() // virtual
138 //----------------------------------------------------------------------------
139 int LayerImageBase::GetY() // virtual
143 //----------------------------------------------------------------------------
144 int LayerImageBase::GetZ() // virtual
150 //----------------------------------------------------------------------------
151 vtkImageData* LayerImageBase::GetImage()
156 //----------------------------------------------------------------------------
157 bool LayerImageBase::GetActorPresent()
159 return _actorPresent;
164 //----------------------------------------------------------------------------
165 void LayerImageBase::SetImage(vtkImageData* image)
170 _image->GetSpacing(spc);
171 _image->GetWholeExtent(ext);
172 int sx=ext[1]-ext[0]+1;
173 int sy=ext[3]-ext[2]+1;
174 int sz=ext[5]-ext[4]+1;
175 int ssx=(sx/2)*spc[0];
176 int ssy=(sy/2)*spc[1];
177 int ssz=(sz/2)*spc[2];
182 _image->GetScalarRange( _range );
185 void LayerImageBase::SetRangeForColorBar(std::vector<double> &range)
187 _rangeForColorBar=range;
190 void LayerImageBase::GetRangeForColorBar(double &minRange, double &maxRange)
192 if (_rangeForColorBar.size()==2)
194 minRange = _rangeForColorBar[0];
195 maxRange = _rangeForColorBar[1];
197 minRange = _range[0];
198 maxRange = _range[1];
204 //----------------------------------------------------------------------------
205 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
209 _baseView = baseview;
210 ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
211 _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
212 colorlayerobserver->SetLayerImageBase(this);
217 //----------------------------------------------------------------------------
218 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
223 //----------------------------------------------------------------------------
224 void LayerImageBase::Refresh()
228 _baseView->Refresh();
233 //----------------------------------------------------------------------------
234 vtkLookupTable* LayerImageBase::GetThresholdTable()
236 return _thresholdTable;
239 //----------------------------------------------------------------------------
240 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
243 _image->GetWholeExtent(ext);
260 if ( x > (ext[1]-ext[0]) )
265 if ( y > (ext[3]-ext[2]) )
270 if ( z > (ext[5]-ext[4]) )
278 //----------------------------------------------------------------------------
279 void LayerImageBase::onThreshold()
281 if ((_image!=NULL) && (_baseView!=NULL))
284 GetImage()-> GetSpacing(spc);
294 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
295 int directionViewer=0;
296 if (camera->GetParallelProjection()==1)
298 wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
299 directionViewer = wxvtk2dbasevie->GetDirection();
300 } // ParallelProjection
303 if (!GetActorPresent())
305 if (_thresholdTable==NULL)
308 _thresholdTable = vtkLookupTable::New();
311 if (_thresholdMapper==NULL)
313 _thresholdMapper = vtkImageMapToColors::New( );
316 if (_thresholdActor==NULL)
318 _thresholdActor = vtkImageActor::New( );
319 _scalarbarActor = vtkScalarBarActor::New();
320 _thresholdActor->SetOpacity( 0.6 );
321 _thresholdActor->InterpolateOn( );
322 if (directionViewer==0)
324 _thresholdActor->SetPosition( 900-1,0,0 );
325 _thresholdActor->RotateY( -90 );
327 if (directionViewer==1)
329 _thresholdActor->SetPosition( 0,-900-1, 0 );
330 _thresholdActor->RotateX( 90 );
332 if (directionViewer==2)
334 _thresholdActor->SetPosition( 0,0, -900-1 );
337 _baseView->GetRenderer()->AddActor( _thresholdActor );
338 _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
339 _actorPresent = true;
340 } // !GetActorPresent()
342 ConfigLookupTable(); // virtual method
343 _imageReslicer->SetInput( GetImage() );
344 _imageReslicer->SetInformationInput( GetImage() );
346 if (directionViewer==0)
348 _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0, -1,0,0 );
349 _imageReslicer->SetResliceAxesOrigin(x,0,0);
352 if (directionViewer==1)
354 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1, 0,-1,0 );
355 _imageReslicer->SetResliceAxesOrigin(0,y,0);
358 if (directionViewer==2)
360 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
361 _imageReslicer->SetResliceAxesOrigin(0,0,z);
365 _imageReslicer->SetOutputDimensionality(2);
366 // _imageReslicer->SetInterpolationModeToLinear();
367 _imageReslicer->SetInterpolationModeToNearestNeighbor();
369 vtkImageData *img = _imageReslicer->GetOutput();
371 // img->UpdateInformation();
373 // _thresholdTable->Update();
374 _thresholdMapper->SetInput( img );
375 _thresholdMapper->SetLookupTable( _thresholdTable );
376 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
378 // _scalarbarActor->SetLookupTable( _thresholdTable );
379 _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
380 _scalarbarActor->SetTitle("Value");
381 _scalarbarActor->SetNumberOfLabels(4);
382 int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
383 _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
385 // _scalarbarActor->SetTextPad(4); ??
392 //----------------------------------------------------------------------------
393 void LayerImageBase::onThresholdChange()
401 //----------------------------------------------------------------------------
402 void LayerImageBase::onThresholdInterpolation(bool interpolate)
404 if (_thresholdActor!=NULL)
408 _thresholdActor->InterpolateOn( );
412 _thresholdActor->InterpolateOff( );
417 //----------------------------------------------------------------------------
418 void LayerImageBase::onThresholdChangeOpacity (int opacity)
422 _thresholdActor->SetOpacity(opacity*0.1);
426 //----------------------------------------------------------------------------
427 void LayerImageBase::onThresholdRemove()
431 wxVtkBaseView * baseView = _baseView;
432 baseView->GetRenderer()->RemoveActor( _thresholdActor );
433 baseView->GetRenderer()->RemoveActor( _scalarbarActor );
435 _actorPresent = false;
439 //----------------------------------------------------------------------------
440 vtkLookupTable *LayerImageBase::GetvtkLookupTable()
442 return _thresholdTable;
445 //----------------------------------------------------------------------------
446 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
448 return _imageReslicer;