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);
246 _imageChangeInformation->Update();
251 //------------------------------------------------------------------------------
252 void LayerImageBase::SetSpcOriginalLayer(double spc[3])
254 _spcOriginalLayer[0] = spc[0];
255 _spcOriginalLayer[1] = spc[1];
256 _spcOriginalLayer[2] = spc[2];
259 //------------------------------------------------------------------------------
260 void LayerImageBase::SetNewSpacingLayer(double spc[3])
262 _newSpcLayer[0] = spc[0];
263 _newSpcLayer[1] = spc[1];
264 _newSpcLayer[2] = spc[2];
265 _imageChangeInformation->SetOutputSpacing( _newSpcLayer );
270 //------------------------------------------------------------------------------
271 void LayerImageBase::SetSpcBase(double spc[3])
273 _spcBase[0] = spc[0];
274 _spcBase[1] = spc[1];
275 _spcBase[2] = spc[2];
278 //------------------------------------------------------------------------------
279 void LayerImageBase::SetDimensionOriginalLayer(int dim[3])
281 _dimOriginalLayer[0] = dim[0];
282 _dimOriginalLayer[1] = dim[1];
283 _dimOriginalLayer[2] = dim[2];
286 //------------------------------------------------------------------------------
287 void LayerImageBase::SetDimensionBase(int dim[3])
289 _dimBase[0] = dim[0];
290 _dimBase[1] = dim[1];
291 _dimBase[2] = dim[2];
296 //------------------------------------------------------------------------------
297 void LayerImageBase::GetSpcOriginalLayer(double *spc)
299 spc[0] = _spcOriginalLayer[0];
300 spc[1] = _spcOriginalLayer[1];
301 spc[2] = _spcOriginalLayer[2];
304 //------------------------------------------------------------------------------
305 void LayerImageBase::GetSpcBase(double *spc)
307 spc[0] = _spcBase[0];
308 spc[1] = _spcBase[1];
309 spc[2] = _spcBase[2];
312 //------------------------------------------------------------------------------
313 void LayerImageBase::GetDimensionOriginalLayer(int *dim)
315 dim[0] = _dimOriginalLayer[0];
316 dim[1] = _dimOriginalLayer[1];
317 dim[2] = _dimOriginalLayer[2];
320 //------------------------------------------------------------------------------
321 void LayerImageBase::GetDimensionBase(int *dim)
323 dim[0] = _dimBase[0];
324 dim[1] = _dimBase[1];
325 dim[2] = _dimBase[2];
329 //------------------------------------------------------------------------------
330 void LayerImageBase::SetColorBarPosition(std::vector<int> &colorbarposition)
332 _colorBarPosition=colorbarposition;
335 //------------------------------------------------------------------------------
336 void LayerImageBase::SetRangeForColorBar(std::vector<double> &range)
338 _rangeForColorBar=range;
342 //------------------------------------------------------------------------------
343 void LayerImageBase::GetRangeForColorBar(double &minRange, double &maxRange)
345 if (_rangeForColorBar.size()==2)
347 minRange = _rangeForColorBar[0];
348 maxRange = _rangeForColorBar[1];
350 minRange = _range[0];
351 maxRange = _range[1];
357 //----------------------------------------------------------------------------
358 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
362 _baseView = baseview;
363 ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
364 _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
365 colorlayerobserver->SetLayerImageBase(this);
369 //----------------------------------------------------------------------------
370 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
375 //----------------------------------------------------------------------------
376 void LayerImageBase::Refresh()
380 _baseView->Refresh();
385 //----------------------------------------------------------------------------
386 vtkLookupTable* LayerImageBase::GetThresholdTable()
388 return _thresholdTable;
391 //----------------------------------------------------------------------------
392 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
407 maxSize=_dimOriginalLayer[0]*_newSpcLayer[0];
412 maxSize=_dimOriginalLayer[1]*_newSpcLayer[1];
417 maxSize=_dimOriginalLayer[2]*_newSpcLayer[2];
425 //----------------------------------------------------------------------------
426 void LayerImageBase::onThreshold()
428 if ((_image!=NULL) && (_baseView!=NULL))
440 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
441 int directionViewer=0;
442 if (camera->GetParallelProjection()==1)
444 wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
445 directionViewer = wxvtk2dbasevie->GetDirection();
446 } // ParallelProjection
449 if (!GetActorPresent())
451 if (_thresholdTable==NULL)
454 _thresholdTable = vtkLookupTable::New();
457 if (_thresholdMapper==NULL)
459 _thresholdMapper = vtkImageMapToColors::New( );
462 if (_thresholdActor==NULL)
464 _thresholdActor = vtkImageActor::New( );
465 _scalarbarActor = vtkScalarBarActor::New();
466 if (_colorBarPosition.size()==2)
468 _scalarbarActor->SetDisplayPosition(_colorBarPosition[0],_colorBarPosition[1]);
470 _scalarbarActor->SetDisplayPosition(0,0);
473 _thresholdActor->SetOpacity( 1 );
474 _thresholdActor->InterpolateOn( );
476 if (directionViewer==0)
478 _thresholdActor->SetPosition( 900-1,0,0 );
479 _thresholdActor->RotateY( -90 );
481 if (directionViewer==1)
483 _thresholdActor->SetPosition( 0,-900-1, 0 );
484 _thresholdActor->RotateX( 90 );
486 if (directionViewer==2)
488 _thresholdActor->SetPosition( 0,0, -900-1 );
491 _baseView->GetRenderer()->AddActor( _thresholdActor );
492 _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
493 _actorPresent = true;
494 } // !GetActorPresent()
496 ConfigLookupTable(); // virtual method
498 //EED 2017-01-01 Migration VTK7
499 #if VTK_MAJOR_VERSION <= 5
500 _imageReslicer->SetInput( GetImage() );
502 _imageReslicer->SetInputData( GetImage() );
505 _imageReslicer->SetInformationInput( GetImage() );
507 if (directionViewer==0)
509 _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0, -1,0,0 );
510 _imageReslicer->SetResliceAxesOrigin(x,0,0);
513 if (directionViewer==1)
515 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1, 0,-1,0 );
516 _imageReslicer->SetResliceAxesOrigin(0,y,0);
519 if (directionViewer==2)
521 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
522 _imageReslicer->SetResliceAxesOrigin(0,0,z);
525 _imageReslicer->SetOutputDimensionality(2);
526 // _imageReslicer->SetInterpolationModeToLinear();
527 _imageReslicer->SetInterpolationModeToNearestNeighbor();
528 _imageReslicer->Modified();
530 //EED 2017-01-01 Migration VTK7
531 #if VTK_MAJOR_VERSION <= 5
534 _imageReslicer->Update();
536 vtkImageData *img = _imageReslicer->GetOutput();
538 // img->UpdateInformation();
539 // _thresholdTable->Update();
541 //EED 2017-01-01 Migration VTK7
542 #if VTK_MAJOR_VERSION <= 5
543 _thresholdMapper->SetInput( img );
545 _thresholdMapper->SetInputData( img );
548 _thresholdMapper->SetLookupTable( _thresholdTable );
551 //EED 2017-01-01 Migration VTK7
552 #if VTK_MAJOR_VERSION <= 5
553 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
555 _thresholdMapper->Update();
556 _thresholdActor->SetInputData( _thresholdMapper->GetOutput() );
559 _scalarbarActor->SetLookupTable( _thresholdTable );
560 // _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
561 _scalarbarActor->SetTitle("Value");
562 _scalarbarActor->SetNumberOfLabels(4);
563 int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
564 _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
565 // _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;
615 } // if _actorPresent
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);