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;
101 _thresholdTable = NULL;
102 _thresholdMapper = NULL;
103 _scalarbarActor = NULL;
104 _thresholdActor = NULL;
107 _imageReslicer = vtkImageReslice::New();
108 _imageChangeInformation = vtkImageChangeInformation::New();
111 //----------------------------------------------------------------------------
112 LayerImageBase::~LayerImageBase()
116 //----------------------------------------------------------------------------
117 void LayerImageBase::SetX(int x)
122 //----------------------------------------------------------------------------
123 void LayerImageBase::SetY(int y)
128 //----------------------------------------------------------------------------
129 void LayerImageBase::SetZ(int z)
134 //----------------------------------------------------------------------------
135 int LayerImageBase::GetX() // virtual
140 //----------------------------------------------------------------------------
141 int LayerImageBase::GetY() // virtual
145 //----------------------------------------------------------------------------
146 int LayerImageBase::GetZ() // virtual
152 //----------------------------------------------------------------------------
153 vtkImageData* LayerImageBase::GetImage()
156 vtkImageData *result;
161 result = _imageChangeInformation->GetOutput();
166 //----------------------------------------------------------------------------
167 bool LayerImageBase::GetActorPresent()
169 return _actorPresent;
173 //----------------------------------------------------------------------------
174 void LayerImageBase::SetImage(vtkImageData* image)
178 int dimensionOriginalLayer[3];
196 _image->GetWholeExtent(ext);
197 dimensionOriginalLayer[0] = ext[1]-ext[0]+1;
198 dimensionOriginalLayer[1] = ext[3]-ext[2]+1;
199 dimensionOriginalLayer[2] = ext[5]-ext[4]+1;
200 SetDimensionOriginalLayer(dimensionOriginalLayer);
201 double spcOriginalLayer[3];
202 _image->GetSpacing(spcOriginalLayer);
203 SetSpcOriginalLayer(spcOriginalLayer);
204 if (GetwxVtkBaseView()!=NULL)
206 vtkImageData *imagebase = GetwxVtkBaseView()->GetVtkBaseData()->GetImageData();
209 imagebase->GetWholeExtent(ext);
210 dimBase[0]=ext[1]-ext[0]+1;
211 dimBase[1]=ext[3]-ext[2]+1;
212 dimBase[2]=ext[5]-ext[4]+1;
213 SetDimensionBase(dimBase);
214 imagebase->GetSpacing(spcBase);
217 if ((_X<0) || (_X>dimBase[0])) { SetX( dimBase[0]/2 ); }
218 if ((_Y<0) || (_Y>dimBase[1])) { SetY( dimBase[1]/2 ); }
219 if ((_Z<0) || (_Z>dimBase[2])) { SetZ( dimBase[2]/2 ); }
223 } // if wxvtkbaseview
224 _image->GetScalarRange( _range );
225 _thresholdTable = vtkLookupTable::New();
227 _imageChangeInformation->SetInput(_image);
230 //------------------------------------------------------------------------------
231 void LayerImageBase::SetSpcOriginalLayer(double spc[3])
233 _spcOriginalLayer[0] = spc[0];
234 _spcOriginalLayer[1] = spc[1];
235 _spcOriginalLayer[2] = spc[2];
238 //------------------------------------------------------------------------------
239 void LayerImageBase::SetNewSpacingLayer(double spc[3])
241 _newSpcLayer[0]=spc[0];
242 _newSpcLayer[1]=spc[1];
243 _newSpcLayer[2]=spc[2];
244 _imageChangeInformation->SetOutputSpacing( _newSpcLayer );
249 //------------------------------------------------------------------------------
250 void LayerImageBase::SetSpcBase(double spc[3])
252 _spcBase[0] = spc[0];
253 _spcBase[1] = spc[1];
254 _spcBase[2] = spc[2];
257 //------------------------------------------------------------------------------
258 void LayerImageBase::SetDimensionOriginalLayer(int dim[3])
260 _dimOriginalLayer[0] = dim[0];
261 _dimOriginalLayer[1] = dim[1];
262 _dimOriginalLayer[2] = dim[2];
265 //------------------------------------------------------------------------------
266 void LayerImageBase::SetDimensionBase(int dim[3])
268 _dimBase[0] = dim[0];
269 _dimBase[1] = dim[1];
270 _dimBase[2] = dim[2];
275 //------------------------------------------------------------------------------
276 void LayerImageBase::GetSpcOriginalLayer(double *spc)
278 spc[0] = _spcOriginalLayer[0];
279 spc[1] = _spcOriginalLayer[1];
280 spc[2] = _spcOriginalLayer[2];
283 //------------------------------------------------------------------------------
284 void LayerImageBase::GetSpcBase(double *spc)
286 spc[0] = _spcBase[0];
287 spc[1] = _spcBase[1];
288 spc[2] = _spcBase[2];
291 //------------------------------------------------------------------------------
292 void LayerImageBase::GetDimensionOriginalLayer(int *dim)
294 dim[0] = _dimOriginalLayer[0];
295 dim[1] = _dimOriginalLayer[1];
296 dim[2] = _dimOriginalLayer[2];
299 //------------------------------------------------------------------------------
300 void LayerImageBase::GetDimensionBase(int *dim)
302 dim[0] = _dimBase[0];
303 dim[1] = _dimBase[1];
304 dim[2] = _dimBase[2];
308 //------------------------------------------------------------------------------
309 void LayerImageBase::SetColorBarPosition(std::vector<int> &colorbarposition)
311 _colorBarPosition=colorbarposition;
314 //------------------------------------------------------------------------------
315 void LayerImageBase::SetRangeForColorBar(std::vector<double> &range)
317 _rangeForColorBar=range;
321 //------------------------------------------------------------------------------
322 void LayerImageBase::GetRangeForColorBar(double &minRange, double &maxRange)
324 if (_rangeForColorBar.size()==2)
326 minRange = _rangeForColorBar[0];
327 maxRange = _rangeForColorBar[1];
329 minRange = _range[0];
330 maxRange = _range[1];
336 //----------------------------------------------------------------------------
337 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
341 _baseView = baseview;
342 ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
343 _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
344 colorlayerobserver->SetLayerImageBase(this);
348 //----------------------------------------------------------------------------
349 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
354 //----------------------------------------------------------------------------
355 void LayerImageBase::Refresh()
359 _baseView->Refresh();
364 //----------------------------------------------------------------------------
365 vtkLookupTable* LayerImageBase::GetThresholdTable()
367 return _thresholdTable;
370 //----------------------------------------------------------------------------
371 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
386 maxSize=_dimOriginalLayer[0]*_newSpcLayer[0];
391 maxSize=_dimOriginalLayer[1]*_newSpcLayer[1];
396 maxSize=_dimOriginalLayer[2]*_newSpcLayer[2];
404 //----------------------------------------------------------------------------
405 void LayerImageBase::onThreshold()
407 if ((_image!=NULL) && (_baseView!=NULL))
419 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
420 int directionViewer=0;
421 if (camera->GetParallelProjection()==1)
423 wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
424 directionViewer = wxvtk2dbasevie->GetDirection();
425 } // ParallelProjection
428 if (!GetActorPresent())
430 if (_thresholdTable==NULL)
433 _thresholdTable = vtkLookupTable::New();
436 if (_thresholdMapper==NULL)
438 _thresholdMapper = vtkImageMapToColors::New( );
441 if (_thresholdActor==NULL)
443 _thresholdActor = vtkImageActor::New( );
444 _scalarbarActor = vtkScalarBarActor::New();
445 if (_colorBarPosition.size()==2)
447 _scalarbarActor->SetDisplayPosition(_colorBarPosition[0],_colorBarPosition[1]);
449 _scalarbarActor->SetDisplayPosition(0,0);
452 _thresholdActor->SetOpacity( 1 );
453 _thresholdActor->InterpolateOn( );
455 if (directionViewer==0)
457 _thresholdActor->SetPosition( 900-1,0,0 );
458 _thresholdActor->RotateY( -90 );
460 if (directionViewer==1)
462 _thresholdActor->SetPosition( 0,-900-1, 0 );
463 _thresholdActor->RotateX( 90 );
465 if (directionViewer==2)
467 _thresholdActor->SetPosition( 0,0, -900-1 );
470 _baseView->GetRenderer()->AddActor( _thresholdActor );
471 _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
472 _actorPresent = true;
473 } // !GetActorPresent()
475 ConfigLookupTable(); // virtual method
476 _imageReslicer->SetInput( GetImage() );
477 _imageReslicer->SetInformationInput( GetImage() );
479 if (directionViewer==0)
481 _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0, -1,0,0 );
482 _imageReslicer->SetResliceAxesOrigin(x,0,0);
485 if (directionViewer==1)
487 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1, 0,-1,0 );
488 _imageReslicer->SetResliceAxesOrigin(0,y,0);
491 if (directionViewer==2)
493 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
494 _imageReslicer->SetResliceAxesOrigin(0,0,z);
497 _imageReslicer->SetOutputDimensionality(2);
498 // _imageReslicer->SetInterpolationModeToLinear();
499 _imageReslicer->SetInterpolationModeToNearestNeighbor();
500 _imageReslicer->Modified();
503 vtkImageData *img = _imageReslicer->GetOutput();
505 // img->UpdateInformation();
507 // _thresholdTable->Update();
508 _thresholdMapper->SetInput( img );
509 _thresholdMapper->SetLookupTable( _thresholdTable );
510 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
511 _thresholdActor->SetOpacity(_opacity);
513 _scalarbarActor->SetLookupTable( _thresholdTable );
514 // _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
515 _scalarbarActor->SetTitle("Value");
516 _scalarbarActor->SetNumberOfLabels(4);
517 int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
518 _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
520 // _scalarbarActor->SetTextPad(4); ??
528 //----------------------------------------------------------------------------
529 void LayerImageBase::onThresholdChange()
537 //----------------------------------------------------------------------------
538 void LayerImageBase::onThresholdInterpolation(bool interpolate)
540 if (_thresholdActor!=NULL)
544 _thresholdActor->InterpolateOn( );
548 _thresholdActor->InterpolateOff( );
553 //----------------------------------------------------------------------------
554 void LayerImageBase::onThresholdChangeOpacity (int opacity)
558 _thresholdActor->SetOpacity(opacity/100.0);
561 _opacity = (double)opacity/100.0;
564 //----------------------------------------------------------------------------
565 void LayerImageBase::onThresholdRemove()
569 wxVtkBaseView * baseView = _baseView;
570 baseView->GetRenderer()->RemoveActor( _thresholdActor );
571 baseView->GetRenderer()->RemoveActor( _scalarbarActor );
573 _actorPresent = false;
577 //----------------------------------------------------------------------------
578 vtkLookupTable *LayerImageBase::GetvtkLookupTable()
580 return _thresholdTable;
583 //----------------------------------------------------------------------------
584 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
586 return _imageReslicer;
589 //----------------------------------------------------------------------------
590 void LayerImageBase::GetImageScalarRange()
593 GetRangeForColorBar(min,max);
594 // _image->GetScalarRange(_range);