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 "creawxVTKRenderWindowInteractor.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 //EED 2017-01-01 Migration VTK7
197 #if VTK_MAJOR_VERSION <= 5
199 _image->GetWholeExtent(ext);
201 _image->GetExtent(ext);
204 dimensionOriginalLayer[0] = ext[1]-ext[0]+1;
205 dimensionOriginalLayer[1] = ext[3]-ext[2]+1;
206 dimensionOriginalLayer[2] = ext[5]-ext[4]+1;
207 SetDimensionOriginalLayer(dimensionOriginalLayer);
208 double spcOriginalLayer[3];
209 _image->GetSpacing(spcOriginalLayer);
210 SetSpcOriginalLayer(spcOriginalLayer);
211 if (GetwxVtkBaseView()!=NULL)
213 vtkImageData *imagebase = GetwxVtkBaseView()->GetVtkBaseData()->GetImageData();
217 //EED 2017-01-01 Migration VTK7
218 #if VTK_MAJOR_VERSION <= 5
219 imagebase->GetWholeExtent(ext);
221 imagebase->GetExtent(ext);
224 dimBase[0]=ext[1]-ext[0]+1;
225 dimBase[1]=ext[3]-ext[2]+1;
226 dimBase[2]=ext[5]-ext[4]+1;
227 SetDimensionBase(dimBase);
228 imagebase->GetSpacing(spcBase);
231 if ((_X<0) || (_X>dimBase[0])) { SetX( dimBase[0]/2 ); }
232 if ((_Y<0) || (_Y>dimBase[1])) { SetY( dimBase[1]/2 ); }
233 if ((_Z<0) || (_Z>dimBase[2])) { SetZ( dimBase[2]/2 ); }
237 } // if wxvtkbaseview
238 _image->GetScalarRange( _range );
239 _thresholdTable = vtkLookupTable::New();
242 //EED 2017-01-01 Migration VTK7
243 #if VTK_MAJOR_VERSION <= 5
244 _imageChangeInformation->SetInput(_image);
246 _imageChangeInformation->SetInputData(_image);
247 _imageChangeInformation->Update();
252 //------------------------------------------------------------------------------
253 void LayerImageBase::SetSpcOriginalLayer(double spc[3])
255 _spcOriginalLayer[0] = spc[0];
256 _spcOriginalLayer[1] = spc[1];
257 _spcOriginalLayer[2] = spc[2];
260 //------------------------------------------------------------------------------
261 void LayerImageBase::SetNewSpacingLayer(double spc[3])
263 _newSpcLayer[0] = spc[0];
264 _newSpcLayer[1] = spc[1];
265 _newSpcLayer[2] = spc[2];
266 _imageChangeInformation->SetOutputSpacing( _newSpcLayer );
271 //------------------------------------------------------------------------------
272 void LayerImageBase::SetSpcBase(double spc[3])
274 _spcBase[0] = spc[0];
275 _spcBase[1] = spc[1];
276 _spcBase[2] = spc[2];
279 //------------------------------------------------------------------------------
280 void LayerImageBase::SetDimensionOriginalLayer(int dim[3])
282 _dimOriginalLayer[0] = dim[0];
283 _dimOriginalLayer[1] = dim[1];
284 _dimOriginalLayer[2] = dim[2];
287 //------------------------------------------------------------------------------
288 void LayerImageBase::SetDimensionBase(int dim[3])
290 _dimBase[0] = dim[0];
291 _dimBase[1] = dim[1];
292 _dimBase[2] = dim[2];
297 //------------------------------------------------------------------------------
298 void LayerImageBase::GetSpcOriginalLayer(double *spc)
300 spc[0] = _spcOriginalLayer[0];
301 spc[1] = _spcOriginalLayer[1];
302 spc[2] = _spcOriginalLayer[2];
305 //------------------------------------------------------------------------------
306 void LayerImageBase::GetSpcBase(double *spc)
308 spc[0] = _spcBase[0];
309 spc[1] = _spcBase[1];
310 spc[2] = _spcBase[2];
313 //------------------------------------------------------------------------------
314 void LayerImageBase::GetDimensionOriginalLayer(int *dim)
316 dim[0] = _dimOriginalLayer[0];
317 dim[1] = _dimOriginalLayer[1];
318 dim[2] = _dimOriginalLayer[2];
321 //------------------------------------------------------------------------------
322 void LayerImageBase::GetDimensionBase(int *dim)
324 dim[0] = _dimBase[0];
325 dim[1] = _dimBase[1];
326 dim[2] = _dimBase[2];
330 //------------------------------------------------------------------------------
331 void LayerImageBase::SetColorBarPosition(std::vector<int> &colorbarposition)
333 _colorBarPosition=colorbarposition;
336 //------------------------------------------------------------------------------
337 void LayerImageBase::SetRangeForColorBar(std::vector<double> &range)
339 _rangeForColorBar=range;
343 //------------------------------------------------------------------------------
344 void LayerImageBase::GetRangeForColorBar(double &minRange, double &maxRange)
346 if (_rangeForColorBar.size()==2)
348 minRange = _rangeForColorBar[0];
349 maxRange = _rangeForColorBar[1];
351 minRange = _range[0];
352 maxRange = _range[1];
358 //----------------------------------------------------------------------------
359 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
363 _baseView = baseview;
364 ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
365 _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
366 colorlayerobserver->SetLayerImageBase(this);
370 //----------------------------------------------------------------------------
371 wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
376 //----------------------------------------------------------------------------
377 void LayerImageBase::Refresh()
381 _baseView->Refresh();
386 //----------------------------------------------------------------------------
387 vtkLookupTable* LayerImageBase::GetThresholdTable()
389 return _thresholdTable;
392 //----------------------------------------------------------------------------
393 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
408 maxSize=_dimOriginalLayer[0]*_newSpcLayer[0];
413 maxSize=_dimOriginalLayer[1]*_newSpcLayer[1];
418 maxSize=_dimOriginalLayer[2]*_newSpcLayer[2];
426 //----------------------------------------------------------------------------
427 void LayerImageBase::onThreshold()
429 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);
473 _scalarbarActor->SetHeight(0.4); //default 0.8
474 _scalarbarActor->SetWidth(0.08); //default 0.17
476 _thresholdActor->SetOpacity( 1 );
477 _thresholdActor->InterpolateOn( );
479 if (directionViewer==0)
481 _thresholdActor->SetPosition( 900-1,0,0 );
482 _thresholdActor->RotateY( -90 );
484 if (directionViewer==1)
486 _thresholdActor->SetPosition( 0,-900-1, 0 );
487 _thresholdActor->RotateX( 90 );
489 if (directionViewer==2)
491 _thresholdActor->SetPosition( 0,0, -900-1 );
494 _baseView->GetRenderer()->AddActor( _thresholdActor );
495 _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
496 _actorPresent = true;
497 } // !GetActorPresent()
499 ConfigLookupTable(); // virtual method
501 //EED 2017-01-01 Migration VTK7
502 #if VTK_MAJOR_VERSION <= 5
503 _imageReslicer->SetInput( GetImage() );
505 _imageReslicer->SetInputData( GetImage() );
508 _imageReslicer->SetInformationInput( GetImage() );
510 if (directionViewer==0)
512 _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0, -1,0,0 );
513 _imageReslicer->SetResliceAxesOrigin(x,0,0);
516 if (directionViewer==1)
518 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1, 0,-1,0 );
519 _imageReslicer->SetResliceAxesOrigin(0,y,0);
522 if (directionViewer==2)
524 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
525 _imageReslicer->SetResliceAxesOrigin(0,0,z);
528 _imageReslicer->SetOutputDimensionality(2);
529 // _imageReslicer->SetInterpolationModeToLinear();
530 _imageReslicer->SetInterpolationModeToNearestNeighbor();
531 _imageReslicer->Modified();
533 //EED 2017-01-01 Migration VTK7
534 #if VTK_MAJOR_VERSION <= 5
537 _imageReslicer->Update();
539 vtkImageData *img = _imageReslicer->GetOutput();
541 // img->UpdateInformation();
542 // _thresholdTable->Update();
544 //EED 2017-01-01 Migration VTK7
545 #if VTK_MAJOR_VERSION <= 5
546 _thresholdMapper->SetInput( img );
548 _thresholdMapper->SetInputData( img );
551 _thresholdMapper->SetLookupTable( _thresholdTable );
554 //EED 2017-01-01 Migration VTK7
555 #if VTK_MAJOR_VERSION <= 5
556 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
558 _thresholdMapper->Update();
559 _thresholdActor->SetInputData( _thresholdMapper->GetOutput() );
562 _thresholdActor->SetOpacity(_opacity);
563 _scalarbarActor->SetLookupTable( _thresholdTable );
564 // _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
565 _scalarbarActor->SetTitle("Value");
566 _scalarbarActor->SetNumberOfLabels(4);
567 int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
568 _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
569 // _scalarbarActor->SetTextPad(4); ??
575 //----------------------------------------------------------------------------
576 void LayerImageBase::onThresholdChange()
584 //----------------------------------------------------------------------------
585 void LayerImageBase::onThresholdInterpolation(bool interpolate)
587 if (_thresholdActor!=NULL)
591 _thresholdActor->InterpolateOn( );
595 _thresholdActor->InterpolateOff( );
600 //----------------------------------------------------------------------------
601 void LayerImageBase::onThresholdChangeOpacity (int opacity)
605 _thresholdActor->SetOpacity(opacity/100.0);
608 _opacity = (double)opacity/100.0;
611 //----------------------------------------------------------------------------
612 void LayerImageBase::onThresholdRemove()
616 wxVtkBaseView * baseView = _baseView;
617 baseView->GetRenderer()->RemoveActor( _thresholdActor );
618 baseView->GetRenderer()->RemoveActor( _scalarbarActor );
620 _actorPresent = false;
621 } // if _actorPresent
624 //----------------------------------------------------------------------------
625 vtkLookupTable *LayerImageBase::GetvtkLookupTable()
627 return _thresholdTable;
630 //----------------------------------------------------------------------------
631 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
633 return _imageReslicer;
636 //----------------------------------------------------------------------------
637 void LayerImageBase::GetImageScalarRange()
640 GetRangeForColorBar(min,max);
641 // _image->GetScalarRange(_range);