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();
107 _imageChangeInformation = vtkImageChangeInformation::New();
110 //----------------------------------------------------------------------------
111 LayerImageBase::~LayerImageBase()
115 //----------------------------------------------------------------------------
116 void LayerImageBase::SetX(int x)
121 //----------------------------------------------------------------------------
122 void LayerImageBase::SetY(int y)
127 //----------------------------------------------------------------------------
128 void LayerImageBase::SetZ(int z)
133 //----------------------------------------------------------------------------
134 int LayerImageBase::GetX() // virtual
139 //----------------------------------------------------------------------------
140 int LayerImageBase::GetY() // virtual
144 //----------------------------------------------------------------------------
145 int LayerImageBase::GetZ() // virtual
151 //----------------------------------------------------------------------------
152 vtkImageData* LayerImageBase::GetImage()
155 vtkImageData *result;
160 result = _imageChangeInformation->GetOutput();
165 //----------------------------------------------------------------------------
166 bool LayerImageBase::GetActorPresent()
168 return _actorPresent;
172 //----------------------------------------------------------------------------
173 void LayerImageBase::SetImage(vtkImageData* image)
177 int dimensionOriginalLayer[3];
195 //EED 2017-01-01 Migration VTK7
196 #if VTK_MAJOR_VERSION <= 5
198 _image->GetWholeExtent(ext);
200 _image->GetExtent(ext);
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)
212 vtkImageData *imagebase = GetwxVtkBaseView()->GetVtkBaseData()->GetImageData();
216 //EED 2017-01-01 Migration VTK7
217 #if VTK_MAJOR_VERSION <= 5
218 imagebase->GetWholeExtent(ext);
220 imagebase->GetExtent(ext);
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);
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 ); }
236 } // if wxvtkbaseview
237 _image->GetScalarRange( _range );
238 _thresholdTable = vtkLookupTable::New();
241 //EED 2017-01-01 Migration VTK7
242 #if VTK_MAJOR_VERSION <= 5
243 _imageChangeInformation->SetInput(_image);
245 _imageChangeInformation->SetInputData(_image);
250 //------------------------------------------------------------------------------
251 void LayerImageBase::SetSpcOriginalLayer(double spc[3])
253 _spcOriginalLayer[0] = spc[0];
254 _spcOriginalLayer[1] = spc[1];
255 _spcOriginalLayer[2] = spc[2];
258 //------------------------------------------------------------------------------
259 void LayerImageBase::SetNewSpacingLayer(double spc[3])
261 _newSpcLayer[0]=spc[0];
262 _newSpcLayer[1]=spc[1];
263 _newSpcLayer[2]=spc[2];
264 _imageChangeInformation->SetOutputSpacing( _newSpcLayer );
269 //------------------------------------------------------------------------------
270 void LayerImageBase::SetSpcBase(double spc[3])
272 _spcBase[0] = spc[0];
273 _spcBase[1] = spc[1];
274 _spcBase[2] = spc[2];
277 //------------------------------------------------------------------------------
278 void LayerImageBase::SetDimensionOriginalLayer(int dim[3])
280 _dimOriginalLayer[0] = dim[0];
281 _dimOriginalLayer[1] = dim[1];
282 _dimOriginalLayer[2] = dim[2];
285 //------------------------------------------------------------------------------
286 void LayerImageBase::SetDimensionBase(int dim[3])
288 _dimBase[0] = dim[0];
289 _dimBase[1] = dim[1];
290 _dimBase[2] = dim[2];
295 //------------------------------------------------------------------------------
296 void LayerImageBase::GetSpcOriginalLayer(double *spc)
298 spc[0] = _spcOriginalLayer[0];
299 spc[1] = _spcOriginalLayer[1];
300 spc[2] = _spcOriginalLayer[2];
303 //------------------------------------------------------------------------------
304 void LayerImageBase::GetSpcBase(double *spc)
306 spc[0] = _spcBase[0];
307 spc[1] = _spcBase[1];
308 spc[2] = _spcBase[2];
311 //------------------------------------------------------------------------------
312 void LayerImageBase::GetDimensionOriginalLayer(int *dim)
314 dim[0] = _dimOriginalLayer[0];
315 dim[1] = _dimOriginalLayer[1];
316 dim[2] = _dimOriginalLayer[2];
319 //------------------------------------------------------------------------------
320 void LayerImageBase::GetDimensionBase(int *dim)
322 dim[0] = _dimBase[0];
323 dim[1] = _dimBase[1];
324 dim[2] = _dimBase[2];
328 //------------------------------------------------------------------------------
329 void LayerImageBase::SetColorBarPosition(std::vector<int> &colorbarposition)
331 _colorBarPosition=colorbarposition;
334 //------------------------------------------------------------------------------
335 void LayerImageBase::SetRangeForColorBar(std::vector<double> &range)
337 _rangeForColorBar=range;
341 //------------------------------------------------------------------------------
342 void LayerImageBase::GetRangeForColorBar(double &minRange, double &maxRange)
344 if (_rangeForColorBar.size()==2)
346 minRange = _rangeForColorBar[0];
347 maxRange = _rangeForColorBar[1];
349 minRange = _range[0];
350 maxRange = _range[1];
356 //----------------------------------------------------------------------------
357 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
361 _baseView = baseview;
362 ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
363 _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
364 colorlayerobserver->SetLayerImageBase(this);
368 //----------------------------------------------------------------------------
369 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
374 //----------------------------------------------------------------------------
375 void LayerImageBase::Refresh()
379 _baseView->Refresh();
384 //----------------------------------------------------------------------------
385 vtkLookupTable* LayerImageBase::GetThresholdTable()
387 return _thresholdTable;
390 //----------------------------------------------------------------------------
391 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
406 maxSize=_dimOriginalLayer[0]*_newSpcLayer[0];
411 maxSize=_dimOriginalLayer[1]*_newSpcLayer[1];
416 maxSize=_dimOriginalLayer[2]*_newSpcLayer[2];
424 //----------------------------------------------------------------------------
425 void LayerImageBase::onThreshold()
427 if ((_image!=NULL) && (_baseView!=NULL))
441 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
442 int directionViewer=0;
443 if (camera->GetParallelProjection()==1)
445 wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
446 directionViewer = wxvtk2dbasevie->GetDirection();
447 } // ParallelProjection
450 if (!GetActorPresent())
452 if (_thresholdTable==NULL)
455 _thresholdTable = vtkLookupTable::New();
458 if (_thresholdMapper==NULL)
460 _thresholdMapper = vtkImageMapToColors::New( );
463 if (_thresholdActor==NULL)
465 _thresholdActor = vtkImageActor::New( );
466 _scalarbarActor = vtkScalarBarActor::New();
467 if (_colorBarPosition.size()==2)
469 _scalarbarActor->SetDisplayPosition(_colorBarPosition[0],_colorBarPosition[1]);
471 _scalarbarActor->SetDisplayPosition(0,0);
474 _thresholdActor->SetOpacity( 1 );
475 _thresholdActor->InterpolateOn( );
477 if (directionViewer==0)
479 _thresholdActor->SetPosition( 900-1,0,0 );
480 _thresholdActor->RotateY( -90 );
482 if (directionViewer==1)
484 _thresholdActor->SetPosition( 0,-900-1, 0 );
485 _thresholdActor->RotateX( 90 );
487 if (directionViewer==2)
489 _thresholdActor->SetPosition( 0,0, -900-1 );
492 _baseView->GetRenderer()->AddActor( _thresholdActor );
493 _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
494 _actorPresent = true;
495 } // !GetActorPresent()
497 ConfigLookupTable(); // virtual method
499 //EED 2017-01-01 Migration VTK7
500 #if VTK_MAJOR_VERSION <= 5
501 _imageReslicer->SetInput( GetImage() );
503 _imageReslicer->SetInputData( GetImage() );
506 _imageReslicer->SetInformationInput( GetImage() );
508 if (directionViewer==0)
510 _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0, -1,0,0 );
511 _imageReslicer->SetResliceAxesOrigin(x,0,0);
514 if (directionViewer==1)
516 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1, 0,-1,0 );
517 _imageReslicer->SetResliceAxesOrigin(0,y,0);
520 if (directionViewer==2)
522 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
523 _imageReslicer->SetResliceAxesOrigin(0,0,z);
526 _imageReslicer->SetOutputDimensionality(2);
527 // _imageReslicer->SetInterpolationModeToLinear();
528 _imageReslicer->SetInterpolationModeToNearestNeighbor();
529 _imageReslicer->Modified();
532 vtkImageData *img = _imageReslicer->GetOutput();
534 // img->UpdateInformation();
536 // _thresholdTable->Update();
538 //EED 2017-01-01 Migration VTK7
539 #if VTK_MAJOR_VERSION <= 5
540 _thresholdMapper->SetInput( img );
542 _thresholdMapper->SetInputData( img );
545 _thresholdMapper->SetLookupTable( _thresholdTable );
548 //EED 2017-01-01 Migration VTK7
549 #if VTK_MAJOR_VERSION <= 5
550 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
552 _thresholdActor->SetInputData( _thresholdMapper->GetOutput() );
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);
563 // _scalarbarActor->SetTextPad(4); ??
571 //----------------------------------------------------------------------------
572 void LayerImageBase::onThresholdChange()
580 //----------------------------------------------------------------------------
581 void LayerImageBase::onThresholdInterpolation(bool interpolate)
583 if (_thresholdActor!=NULL)
587 _thresholdActor->InterpolateOn( );
591 _thresholdActor->InterpolateOff( );
596 //----------------------------------------------------------------------------
597 void LayerImageBase::onThresholdChangeOpacity (int opacity)
601 _thresholdActor->SetOpacity(opacity/100.0);
605 //----------------------------------------------------------------------------
606 void LayerImageBase::onThresholdRemove()
610 wxVtkBaseView * baseView = _baseView;
611 baseView->GetRenderer()->RemoveActor( _thresholdActor );
612 baseView->GetRenderer()->RemoveActor( _scalarbarActor );
614 _actorPresent = false;
618 //----------------------------------------------------------------------------
619 vtkLookupTable *LayerImageBase::GetvtkLookupTable()
621 return _thresholdTable;
624 //----------------------------------------------------------------------------
625 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
627 return _imageReslicer;
630 //----------------------------------------------------------------------------
631 void LayerImageBase::GetImageScalarRange()
634 GetRangeForColorBar(min,max);
635 // _image->GetScalarRange(_range);