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 printf("EED LayerImageBase::GetThresholdTable\n");
390 return _thresholdTable;
393 //----------------------------------------------------------------------------
394 void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
409 maxSize=_dimOriginalLayer[0]*_newSpcLayer[0];
414 maxSize=_dimOriginalLayer[1]*_newSpcLayer[1];
419 maxSize=_dimOriginalLayer[2]*_newSpcLayer[2];
427 //----------------------------------------------------------------------------
428 void LayerImageBase::onThreshold()
430 if ((_image!=NULL) && (_baseView!=NULL))
442 vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
443 int directionViewer=0;
444 if (camera->GetParallelProjection()==1)
446 wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
447 directionViewer = wxvtk2dbasevie->GetDirection();
448 } // ParallelProjection
451 if (!GetActorPresent())
453 if (_thresholdTable==NULL)
456 _thresholdTable = vtkLookupTable::New();
459 if (_thresholdMapper==NULL)
461 _thresholdMapper = vtkImageMapToColors::New( );
464 if (_thresholdActor==NULL)
466 _thresholdActor = vtkImageActor::New( );
467 _scalarbarActor = vtkScalarBarActor::New();
468 if (_colorBarPosition.size()==2)
470 _scalarbarActor->SetDisplayPosition(_colorBarPosition[0],_colorBarPosition[1]);
472 _scalarbarActor->SetDisplayPosition(0,0);
474 _scalarbarActor->SetHeight(0.4); //default 0.8
475 _scalarbarActor->SetWidth(0.08); //default 0.17
477 _thresholdActor->SetOpacity( 1 );
478 _thresholdActor->InterpolateOn( );
480 if (directionViewer==0)
482 _thresholdActor->SetPosition( 900-1,0,0 );
483 _thresholdActor->RotateY( -90 );
485 if (directionViewer==1)
487 _thresholdActor->SetPosition( 0,-900-1, 0 );
488 _thresholdActor->RotateX( 90 );
490 if (directionViewer==2)
492 _thresholdActor->SetPosition( 0,0, -900-1 );
495 _baseView->GetRenderer()->AddActor( _thresholdActor );
496 _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
497 _actorPresent = true;
498 } // !GetActorPresent()
500 ConfigLookupTable(); // virtual method
502 //EED 2017-01-01 Migration VTK7
503 #if VTK_MAJOR_VERSION <= 5
504 _imageReslicer->SetInput( GetImage() );
506 _imageReslicer->SetInputData( GetImage() );
509 _imageReslicer->SetInformationInput( GetImage() );
511 if (directionViewer==0)
513 _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0, -1,0,0 );
514 _imageReslicer->SetResliceAxesOrigin(x,0,0);
517 if (directionViewer==1)
519 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1, 0,-1,0 );
520 _imageReslicer->SetResliceAxesOrigin(0,y,0);
523 if (directionViewer==2)
525 _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
526 _imageReslicer->SetResliceAxesOrigin(0,0,z);
529 _imageReslicer->SetOutputDimensionality(2);
530 // _imageReslicer->SetInterpolationModeToLinear();
531 _imageReslicer->SetInterpolationModeToNearestNeighbor();
532 _imageReslicer->Modified();
534 //EED 2017-01-01 Migration VTK7
535 #if VTK_MAJOR_VERSION <= 5
538 _imageReslicer->Update();
540 vtkImageData *img = _imageReslicer->GetOutput();
542 // img->UpdateInformation();
543 // _thresholdTable->Update();
545 //EED 2017-01-01 Migration VTK7
546 #if VTK_MAJOR_VERSION <= 5
547 _thresholdMapper->SetInput( img );
549 _thresholdMapper->SetInputData( img );
552 _thresholdMapper->SetLookupTable( _thresholdTable );
555 //EED 2017-01-01 Migration VTK7
556 #if VTK_MAJOR_VERSION <= 5
557 _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
559 _thresholdMapper->Update();
560 _thresholdActor->SetInputData( _thresholdMapper->GetOutput() );
563 _thresholdActor->SetOpacity(_opacity);
564 _scalarbarActor->SetLookupTable( _thresholdTable );
565 // _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
566 _scalarbarActor->SetTitle("Value");
567 _scalarbarActor->SetNumberOfLabels(4);
568 int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
569 _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
570 // _scalarbarActor->SetTextPad(4); ??
576 //----------------------------------------------------------------------------
577 void LayerImageBase::onThresholdChange()
585 //----------------------------------------------------------------------------
586 void LayerImageBase::onThresholdInterpolation(bool interpolate)
588 if (_thresholdActor!=NULL)
592 _thresholdActor->InterpolateOn( );
596 _thresholdActor->InterpolateOff( );
601 //----------------------------------------------------------------------------
602 void LayerImageBase::onThresholdChangeOpacity (int opacity)
606 _thresholdActor->SetOpacity(opacity/100.0);
609 _opacity = (double)opacity/100.0;
612 //----------------------------------------------------------------------------
613 void LayerImageBase::onThresholdRemove()
617 wxVtkBaseView * baseView = _baseView;
618 baseView->GetRenderer()->RemoveActor( _thresholdActor );
619 baseView->GetRenderer()->RemoveActor( _scalarbarActor );
620 _actorPresent = false;
621 } // if _actorPresent
624 //----------------------------------------------------------------------------
625 vtkImageReslice *LayerImageBase::GetvtkImageReslice()
627 return _imageReslicer;
630 //----------------------------------------------------------------------------
631 void LayerImageBase::GetImageScalarRange()
634 GetRangeForColorBar(min,max);
635 // _image->GetScalarRange(_range);
640 vtkScalarsToColors* LayerImageBase::GetLookupTable()
642 return _thresholdTable;