]> Creatis software - creaMaracasVisu.git/blobdiff - lib/maracasVisuLib/src/interface/wxWindows/widgets/ThresholdImageView/LayerImageBase.cxx
#2855 creaMaracasVisu Bug New Normal - ColorLayer box spacing size not identified
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / ThresholdImageView / LayerImageBase.cxx
index a4946f0ccbc07659f62e56a62ad2cda49801cb6c..8f1fc5e04406beae38226215da1528ddab6cd2fc 100644 (file)
+/*# ---------------------------------------------------------------------
+#
+# Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
+#                        pour la Sant�)
+# Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
+# Previous Authors : Laurent Guigues, Jean-Pierre Roux
+# CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
+#
+#  This software is governed by the CeCILL-B license under French law and
+#  abiding by the rules of distribution of free software. You can  use,
+#  modify and/ or redistribute the software under the terms of the CeCILL-B
+#  license as circulated by CEA, CNRS and INRIA at the following URL
+#  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
+#  or in the file LICENSE.txt.
+#
+#  As a counterpart to the access to the source code and  rights to copy,
+#  modify and redistribute granted by the license, users are provided only
+#  with a limited warranty  and the software's author,  the holder of the
+#  economic rights,  and the successive licensors  have only  limited
+#  liability.
+#
+#  The fact that you are presently reading this means that you have had
+#  knowledge of the CeCILL-B license and that you accept its terms.
+# ------------------------------------------------------------------------ */
 
 /**
- *  \file 
- *  \brief Class bbtk::ThresholdImageView . 
+ *  \file
+ *  \brief Class bbtk::ThresholdImageView .
  */
 
 #include "LayerImageBase.h"
 
+#include "wxMPRBaseData.h"
+#include "wxVtk2DBaseView.h"
+#include "wxVTKRenderWindowInteractor.h"
+#include "vtkTextProperty.h"
+
+
+//---------------------------------------------------------------------------------------------
+//---------------------------------------------------------------------------------------------
+//---------------------------------------------------------------------------------------------
+
+// Callback for the interaction
+class ColorLayerObserver : public vtkCommand{
+  public:
+       virtual char const *GetClassName() const { return "ColorLayerObserver";}
+
+       static ColorLayerObserver *New()        {      return new ColorLayerObserver;   }
+
+       ColorLayerObserver() 
+       {
+               _layerimagebase = NULL;
+       }
+
+       virtual void Execute(vtkObject *wdg, unsigned long event, void* calldata)       
+       { 
+               if (_layerimagebase!= NULL)
+               {
+
+                       wxVtkBaseView           *wxvtkbaseview = _layerimagebase->GetwxVtkBaseView();
+                       if (wxvtkbaseview!=NULL)
+                       {
+
+                               vtkMPRBaseData  *vtkmprbasedata =       (vtkMPRBaseData*)wxvtkbaseview->GetVtkBaseData();
+                               if (vtkmprbasedata!=NULL)
+                               {
+                                       _layerimagebase->SetX( vtkmprbasedata->GetX() );
+                                       _layerimagebase->SetY( vtkmprbasedata->GetY() );
+                                       _layerimagebase->SetZ( vtkmprbasedata->GetZ() );
+                       _layerimagebase->onThresholdChange();
+                               } // if basedata
+                               
+                       } // if wxvtkbaseview
+               } //if layerimagebase
+       }
+
+       void SetLayerImageBase(LayerImageBase* layerimagebase)
+       {
+       _layerimagebase =       layerimagebase;
+       }
+
+private:
+       LayerImageBase* _layerimagebase;
+};
+
+
+
+//---------------------------------------------------------------------------------------------
+//---------------------------------------------------------------------------------------------
+//---------------------------------------------------------------------------------------------
+
+
+
 LayerImageBase::LayerImageBase()
 {
        _actorPresent                   =       false;
+       _X                                              =       0;
+       _Y                                              =       0;
        _Z                                              =       0;
        _thresholdTable                 =       NULL;
        _thresholdMapper                =       NULL;
+       _scalarbarActor                 =       NULL;
        _thresholdActor                 =       NULL;
        _image                                  =       NULL;
+       _baseView                               =       NULL;
        _imageReslicer                  =       vtkImageReslice::New();
+       _imageChangeInformation =       vtkImageChangeInformation::New();
+}
+
+//----------------------------------------------------------------------------
+LayerImageBase::~LayerImageBase()
+{
+}
 
+//----------------------------------------------------------------------------
+void LayerImageBase::SetX(int x)
+{
+       _X = x;
 }
 
 //----------------------------------------------------------------------------
-  LayerImageBase::~LayerImageBase()
-  {
-  }
+void LayerImageBase::SetY(int y)
+{
+       _Y = y;
+}
 
 //----------------------------------------------------------------------------
 void LayerImageBase::SetZ(int z)
@@ -30,15 +131,35 @@ void LayerImageBase::SetZ(int z)
 }
 
 //----------------------------------------------------------------------------
-int LayerImageBase::GetZ()
+int LayerImageBase::GetX()  // virtual
+{
+       return _X;
+}
+
+//----------------------------------------------------------------------------
+int LayerImageBase::GetY()  // virtual
+{
+       return _Y;
+}
+//----------------------------------------------------------------------------
+int LayerImageBase::GetZ()  // virtual
 {
        return _Z;
 }
 
+
 //----------------------------------------------------------------------------
 vtkImageData* LayerImageBase::GetImage()
 {
-       return _image;
+//     return _image;
+       vtkImageData *result;
+       if (_image==NULL)
+       {
+               result = NULL;
+       } else {
+               result = _imageChangeInformation->GetOutput();
+       }
+       return result;
 }
 
 //----------------------------------------------------------------------------
@@ -48,17 +169,157 @@ bool LayerImageBase::GetActorPresent()
 }
 
 
-
 //----------------------------------------------------------------------------
 void LayerImageBase::SetImage(vtkImageData* image)
 {
+       int     ext[6];
+       int     dimensionOriginalLayer[3];
        _image = image;
+       _image->GetWholeExtent(ext);
+       dimensionOriginalLayer[0] = ext[1]-ext[0]+1;
+       dimensionOriginalLayer[1] = ext[3]-ext[2]+1;
+       dimensionOriginalLayer[2] = ext[5]-ext[4]+1;
+       SetDimensionOriginalLayer(dimensionOriginalLayer);
+       double  spcOriginalLayer[3];
+       _image->GetSpacing(spcOriginalLayer);
+       SetSpcOriginalLayer(spcOriginalLayer);
+       if (GetwxVtkBaseView()!=NULL)
+       {
+               vtkImageData    *imagebase              =       GetwxVtkBaseView()->GetVtkBaseData()->GetImageData();
+               if (imagebase!=NULL) 
+               {       
+                       int dimBase[6];
+                       imagebase->GetWholeExtent(ext);
+                       dimBase[0]=ext[1]-ext[0]+1;                             
+                       dimBase[1]=ext[3]-ext[2]+1;                             
+                       dimBase[2]=ext[5]-ext[4]+1;                             
+                       SetDimensionBase(dimBase);
+                       double  spcBase[3];
+                       imagebase->GetSpacing(spcBase);
+                       SetSpcBase(spcBase);
+                       SetX( dimBase[0]*spcBase[0]/2 );
+                       SetY( dimBase[1]*spcBase[1]/2 );
+                       SetZ( dimBase[2]*spcBase[2]/2 );
+               } // if imagebase
+       } // if wxvtkbaseview
+       _image->Update();
+       _image->GetScalarRange( _range );
+       _thresholdTable = vtkLookupTable::New();
+
+       _imageChangeInformation->SetInput(_image);
+}
+
+//------------------------------------------------------------------------------
+void LayerImageBase::SetSpcOriginalLayer(double spc[3])
+{
+       _spcOriginalLayer[0] = spc[0];
+       _spcOriginalLayer[1] = spc[1];
+       _spcOriginalLayer[2] = spc[2];
+}
+
+//------------------------------------------------------------------------------
+void LayerImageBase::SetNewSpacingLayer(double spc[3])
+{
+       _newSpcLayer[0]=spc[0];
+       _newSpcLayer[1]=spc[1];
+       _newSpcLayer[2]=spc[2];
+       _imageChangeInformation->SetOutputSpacing( _newSpcLayer );  
+
+}
+
+
+//------------------------------------------------------------------------------
+void LayerImageBase::SetSpcBase(double spc[3])
+{
+       _spcBase[0] = spc[0];
+       _spcBase[1] = spc[1];
+       _spcBase[2] = spc[2];
+}
+
+//------------------------------------------------------------------------------
+void LayerImageBase::SetDimensionOriginalLayer(int dim[3])
+{
+       _dimOriginalLayer[0] = dim[0];
+       _dimOriginalLayer[1] = dim[1];
+       _dimOriginalLayer[2] = dim[2];
+}
+
+//------------------------------------------------------------------------------
+void LayerImageBase::SetDimensionBase(int dim[3])
+{
+       _dimBase[0] = dim[0];
+       _dimBase[1] = dim[1];
+       _dimBase[2] = dim[2];
 }
 
+
+
+//------------------------------------------------------------------------------
+void LayerImageBase::GetSpcOriginalLayer(double *spc)
+{
+       spc[0] = _spcOriginalLayer[0];
+       spc[1] = _spcOriginalLayer[1];
+       spc[2] = _spcOriginalLayer[2];
+}
+
+//------------------------------------------------------------------------------
+void LayerImageBase::GetSpcBase(double *spc)
+{
+       spc[0] = _spcBase[0];
+       spc[1] = _spcBase[1];
+       spc[2] = _spcBase[2];
+}
+
+//------------------------------------------------------------------------------
+void LayerImageBase::GetDimensionOriginalLayer(int *dim)
+{
+       dim[0] = _dimOriginalLayer[0];
+       dim[1] = _dimOriginalLayer[1];
+       dim[2] = _dimOriginalLayer[2];
+}
+
+//------------------------------------------------------------------------------
+void LayerImageBase::GetDimensionBase(int *dim)
+{
+       dim[0] = _dimBase[0];
+       dim[1] = _dimBase[1];
+       dim[2] = _dimBase[2];
+}
+
+
+//------------------------------------------------------------------------------
+void LayerImageBase::SetRangeForColorBar(std::vector<double> &range)
+{
+   _rangeForColorBar=range;
+}
+
+
+//------------------------------------------------------------------------------
+void LayerImageBase::GetRangeForColorBar(double &minRange, double &maxRange)
+{
+       if (_rangeForColorBar.size()==2)
+       {
+               minRange = _rangeForColorBar[0];        
+               maxRange = _rangeForColorBar[1];
+       } else {
+               minRange = _range[0];   
+               maxRange = _range[1];
+       }
+}
+
+
+
 //----------------------------------------------------------------------------
 void LayerImageBase::SetwxVtkBaseView(wxVtkBaseView *baseview)
 {
-       _baseView = baseview;
+       if (_baseView==NULL)
+   {
+               _baseView = baseview;
+               ColorLayerObserver *colorlayerobserver = ColorLayerObserver::New();
+               _baseView->GetVtkBaseData()->AddObserver( 1, colorlayerobserver );
+               colorlayerobserver->SetLayerImageBase(this);
+
+       } // if _baseView
 }
 
 //----------------------------------------------------------------------------
@@ -67,6 +328,14 @@ wxVtkBaseView *LayerImageBase::GetwxVtkBaseView()
        return _baseView;
 }
 
+//----------------------------------------------------------------------------
+void LayerImageBase::Refresh()
+{
+    if (_baseView!=NULL)
+    {
+        _baseView->Refresh();
+    }
+}
 
 
 //----------------------------------------------------------------------------
@@ -75,54 +344,176 @@ vtkLookupTable* LayerImageBase::GetThresholdTable()
        return _thresholdTable;
 }
 
+//----------------------------------------------------------------------------
+void LayerImageBase::CleanXYZ(double &x, double &y, double &z)
+{
+
+       if (x<0)
+       {
+               x=0;
+       }
+
+       if (y<0)
+       {
+               y=0;
+       }
+
+       if (z<0)
+       {
+               z=0;
+       }
+
+       double maxSize;
+
+       maxSize=_dimOriginalLayer[0]*_newSpcLayer[0];
+       if ( x > maxSize  )
+       {
+               x=maxSize;
+       }
+
+       maxSize=_dimOriginalLayer[1]*_newSpcLayer[1];
+       if ( y > maxSize  )
+       {
+               y = maxSize;
+       }
+
+       maxSize=_dimOriginalLayer[2]*_newSpcLayer[2];
+       if ( z > maxSize  )
+       {
+               z = maxSize;
+       }
+
+       
+/*
+
+       if ( x > (ext[1]-ext[0]) )
+       {
+               z=ext[1]-ext[0];
+       }
+
+       if ( y > (ext[3]-ext[2]) )
+       {
+               z=ext[3]-ext[2];
+       }
+
+       if ( z > (ext[5]-ext[4]) )
+       {
+               z=ext[5]-ext[4];
+       }
+*/
 
+}
 
 //----------------------------------------------------------------------------
 void LayerImageBase::onThreshold()
 {
-       int     z = GetZ();
-       
-       if (!GetActorPresent())
-       {                               
-               if (_thresholdTable==NULL)
+       if ((_image!=NULL) && (_baseView!=NULL))
+       {
+               double x=GetX();
+               double y=GetY();
+               double z=GetZ();
+
+               x = x*_spcBase[0];
+               y = y*_spcBase[1];
+               z = z*_spcBase[2];
+
+               CleanXYZ(x,y,z);
+
+               vtkCamera *camera = _baseView->GetRenderer()->GetActiveCamera();
+               int directionViewer=0;
+               if (camera->GetParallelProjection()==1)
                {
-                       //Lookup Table
-                       _thresholdTable = vtkLookupTable::New();
-               } // _thresholdTable
-               
-               if (_thresholdMapper==NULL)
+                       wxVtk2DBaseView *wxvtk2dbasevie = (wxVtk2DBaseView *)_baseView;
+                       directionViewer =  wxvtk2dbasevie->GetDirection();
+               } // ParallelProjection
+
+               if (!GetActorPresent())
+               {
+                       if (_thresholdTable==NULL)
+                       {
+                               //Lookup Table
+                               _thresholdTable = vtkLookupTable::New();
+                       } // _thresholdTable
+
+                       if (_thresholdMapper==NULL)
+                       {
+                               _thresholdMapper = vtkImageMapToColors::New( );
+                       }
+
+                       if (_thresholdActor==NULL)
+                       {
+                               _thresholdActor = vtkImageActor::New( );
+                               _scalarbarActor = vtkScalarBarActor::New();
+                               _thresholdActor->SetOpacity( 0.6 );
+                               _thresholdActor->InterpolateOn(  );
+                               if (directionViewer==0)
+                               {
+                                       _thresholdActor->SetPosition( 900-1,0,0 );
+                                       _thresholdActor->RotateY( -90 );
+                               }
+                               if (directionViewer==1)
+                               {
+                                       _thresholdActor->SetPosition( 0,-900-1, 0 );
+                                       _thresholdActor->RotateX( 90 );
+                               }
+                               if (directionViewer==2)
+                               {
+                                       _thresholdActor->SetPosition( 0,0, -900-1 );
+                               }
+                       } // _thresholdActor
+                       _baseView->GetRenderer()->AddActor( _thresholdActor );
+                       _baseView->GetRenderer()->AddActor2D( _scalarbarActor );
+                       _actorPresent = true;
+               }  // !GetActorPresent()
+
+               ConfigLookupTable();  // virtual method
+               _imageReslicer->SetInput( GetImage() );
+               _imageReslicer->SetInformationInput( GetImage() );
+
+               if (directionViewer==0)
                {
-                       _thresholdMapper = vtkImageMapToColors::New( );
+                       _imageReslicer->SetResliceAxesDirectionCosines( 0,0,1, 0,1,0,  -1,0,0 );
+                       _imageReslicer->SetResliceAxesOrigin(x,0,0);
                }
-               
-               if (_thresholdActor==NULL)
+
+               if (directionViewer==1)
                {
-                       _thresholdActor = vtkImageActor::New( );
-                       _thresholdActor->SetOpacity( 0.6 );
-                       _thresholdActor->InterpolateOn(  );
-                       _thresholdActor->SetPosition( 0,0, 900-1 );
-               } // _thresholdActor
-               
+                       _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0 ,0,0,1,  0,-1,0 );
+                       _imageReslicer->SetResliceAxesOrigin(0,y,0);
+               }
+
+               if (directionViewer==2)
+               {
+                       _imageReslicer->SetResliceAxesDirectionCosines( 1,0,0, 0,1,0 ,0,0,1 );
+                       _imageReslicer->SetResliceAxesOrigin(0,0,z);
+               }
+
+               _imageReslicer->SetOutputDimensionality(2);
+//             _imageReslicer->SetInterpolationModeToLinear();
+               _imageReslicer->SetInterpolationModeToNearestNeighbor();
+               _imageReslicer->Modified();
+
+
+               vtkImageData *img = _imageReslicer->GetOutput();
+//             img->Update();
+//             img->UpdateInformation();
+
+//             _thresholdTable->Update();
+               _thresholdMapper->SetInput( img );
+               _thresholdMapper->SetLookupTable( _thresholdTable );
+               _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
                
-               _baseView->GetRenderer()->AddActor( _thresholdActor );
-               _actorPresent = true;           
-       }  // !GetActorPresent()
+//             _scalarbarActor->SetLookupTable( _thresholdTable );
+               _scalarbarActor->SetLookupTable( _thresholdMapper->GetLookupTable() );
+               _scalarbarActor->SetTitle("Value");
+               _scalarbarActor->SetNumberOfLabels(4);
+               int fontsize = _scalarbarActor->GetLabelTextProperty()->GetFontSize();
+               _scalarbarActor->GetLabelTextProperty()->SetFontSize(fontsize/2);
 
-       ConfigLookupTable();  // virtual method
-       _imageReslicer->SetInput( GetImage() );
-       _imageReslicer->SetInformationInput( GetImage() );                      
-       _imageReslicer->SetResliceAxesDirectionCosines(1,0,0, 0,1,0 ,0,0,1);
-       _imageReslicer->SetOutputDimensionality(2);
-       _imageReslicer->SetInterpolationModeToLinear();
-       _imageReslicer->SetResliceAxesOrigin(0,0,z);
+//             _scalarbarActor->SetTextPad(4);  ??
 
-       vtkImageData *img = _imageReslicer->GetOutput();
-       img->Update();
-       img->UpdateInformation();
 
-       _thresholdMapper->SetInput( img );
-       _thresholdMapper->SetLookupTable( _thresholdTable );
-       _thresholdActor->SetInput( _thresholdMapper->GetOutput() );
+               } // _image
 }
 
 
@@ -148,7 +539,7 @@ void LayerImageBase::onThresholdInterpolation(bool interpolate)
                else
                {
                        _thresholdActor->InterpolateOff( );
-               }               
+               }
        }
 }
 
@@ -168,10 +559,33 @@ void LayerImageBase::onThresholdRemove()
        {
                wxVtkBaseView * baseView = _baseView;
                baseView->GetRenderer()->RemoveActor( _thresholdActor );
+               baseView->GetRenderer()->RemoveActor( _scalarbarActor );
+
                _actorPresent = false;
-       }       
+       }
 }
 
+//----------------------------------------------------------------------------
+vtkLookupTable *LayerImageBase::GetvtkLookupTable()
+{
+    return _thresholdTable;
+}
+
+//----------------------------------------------------------------------------
+vtkImageReslice *LayerImageBase::GetvtkImageReslice()
+{
+    return _imageReslicer;
+}
+
+//----------------------------------------------------------------------------
+void LayerImageBase::GetImageScalarRange()
+{
+       double min,max;
+       GetRangeForColorBar(min,max);
+//     _image->GetScalarRange(_range);
+       _range[0]=min;
+       _range[1]=max;
+}