]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/QT/ImageWidget.cxx
yet another refactoring
[cpPlugins.git] / lib / cpExtensions / QT / ImageWidget.cxx
diff --git a/lib/cpExtensions/QT/ImageWidget.cxx b/lib/cpExtensions/QT/ImageWidget.cxx
new file mode 100644 (file)
index 0000000..bac5f17
--- /dev/null
@@ -0,0 +1,225 @@
+#include <cpExtensions/QT/ImageWidget.h>
+#include <cpExtensions/Interaction/ImageSliceStyle.h>
+#include <cpExtensions/Visualization/LUTImageActor.h>
+#include <cpExtensions/Visualization/MeshActor.h>
+#include <cpExtensions/Visualization/OutlineSource.h>
+#include <cpExtensions/Visualization/WindowLevelImageActor.h>
+
+#include <vtkActor.h>
+#include <vtkCamera.h>
+#include <vtkImageData.h>
+#include <vtkImageProperty.h>
+#include <vtkProperty.h>
+#include <vtkRenderer.h>
+
+// -------------------------------------------------------------------------
+cpExtensions::QT::ImageWidget::
+ImageWidget( QWidget* parent, Qt::WindowFlags f )
+  : Superclass( parent, f ),
+    m_ImageName( "" ),
+    m_OutlineActor( NULL )
+{
+  this->m_Style = vtkSmartPointer< TStyle >::New( );
+  this->m_Style->SetCurrentRenderer( this->m_Renderer );
+  this->SetStyle( this->m_Style );
+}
+
+// -------------------------------------------------------------------------
+cpExtensions::QT::ImageWidget::
+~ImageWidget( )
+{
+  this->Clear( );
+  if( this->m_OutlineActor != NULL )
+    delete this->m_OutlineActor;
+  for( auto a = this->m_Actors.begin( ); a != this->m_Actors.end( ); ++a )
+    delete *a;
+  this->m_Actors.clear( );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::QT::ImageWidget::
+Clear( )
+{
+  this->RemoveViewProps( );
+  this->m_ImageName = "";
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::QT::ImageWidget::
+SetImage( vtkImageData* image, const std::string& name, int orientation )
+{
+  if( name == "" )
+    return;
+  if( this->m_ImageName != "" )
+    this->Clear( );
+  this->m_ImageName = name;
+
+  this->m_WLActor = vtkSmartPointer< TWLActor >::New( );
+  this->m_WLActor->SetImage( image );
+  this->m_WLActor->SetOrientation( orientation );
+
+  this->m_LUTActor = vtkSmartPointer< TLUTActor >::New( );
+  this->m_LUTActor->SetOrientation( orientation );
+
+  this->m_Outline = vtkSmartPointer< TOutline >::New( );
+  this->m_Outline->SetBounds( image->GetBounds( ) );
+  this->m_Outline->Update( );
+
+  if( this->m_OutlineActor != NULL )
+    delete this->m_OutlineActor;
+  this->m_OutlineActor = new TActor( );
+  this->m_OutlineActor->SetMesh( this->m_Outline->GetOutput( ) );
+
+  double cr = double( 0 );
+  double cg = double( 0 );
+  double cb = double( 0 );
+  switch( this->m_WLActor->GetOrientation( ) )
+  {
+  case 0:  cr = double( 1 ); break;
+  case 1:  cg = double( 1 ); break;
+  case 2:  cb = double( 1 ); break;
+  default: cr = double( 1 ); break;
+  } // hctiws
+  this->m_OutlineActor->GetActor( )->GetProperty( )->SetColor( cr, cg, cb );
+
+  this->AddViewProp( this->m_WLActor, this->m_ImageName );
+  this->AddAuxViewProp( this->m_OutlineActor->GetActor( ), this->m_ImageName );
+  this->ResetCamera( );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::QT::ImageWidget::
+Add( vtkDataSet* data, const std::string& name )
+{
+  auto image = dynamic_cast< vtkImageData* >( data );
+  auto pdata = dynamic_cast< vtkPolyData* >( data );
+  if( image != NULL )
+  {
+    this->m_LUTActor->AddImage( image );
+    this->m_LUTActor->GetProperty( )->SetOpacity( 0.5 );
+    this->AddViewProp( this->m_LUTActor, "__LUT_IMAGE__" );
+    this->Render( );
+  }
+  else if( pdata != NULL )
+  {
+    TActor* actor = new TActor( );
+    actor->SetMesh( pdata );
+    this->m_Actors.push_back( actor );
+    this->AddViewProp( actor->GetActor( ), name );
+    this->Render( );
+
+  } // fi
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::QT::ImageWidget::
+ResetCamera( )
+{
+  if( this->m_WLActor.GetPointer( ) != NULL )
+  {
+    auto image = this->m_WLActor->GetImage( );
+    if( image != NULL )
+    {
+      double bounds[ 6 ];
+      image->GetBounds( bounds );
+
+      // Compute camera properties
+      double center[ 3 ];
+      center[ 0 ] = ( bounds[ 1 ] + bounds[ 0 ] ) / double( 2 );
+      center[ 1 ] = ( bounds[ 3 ] + bounds[ 2 ] ) / double( 2 );
+      center[ 2 ] = ( bounds[ 5 ] + bounds[ 4 ] ) / double( 2 );
+
+      int ori = this->m_WLActor->GetOrientation( );
+      double pos[ 3 ] = { double( 0 ) };
+      pos[ ori ] = double( 1 );
+      pos[ 0 ] += center[ 0 ];
+      pos[ 1 ] += center[ 1 ];
+      pos[ 2 ] += center[ 2 ];
+
+      double up[ 3 ] = { double( 0 ) };
+      if( ori == 0 )
+      {
+        if     ( this->m_Quadrant == 0 ) up[ 2 ] = double( 1 );
+        else if( this->m_Quadrant == 1 ) up[ 2 ] = double( 1 );
+        else if( this->m_Quadrant == 2 ) up[ 2 ] = double( 1 );
+        else if( this->m_Quadrant == 3 ) up[ 2 ] = double( 1 );
+      }
+      else if( ori == 1 )
+      {
+        if     ( this->m_Quadrant == 0 ) up[ 2 ] = double( 1 );
+        else if( this->m_Quadrant == 1 ) up[ 2 ] = double( 1 );
+        else if( this->m_Quadrant == 2 ) up[ 2 ] = double( 1 );
+        else if( this->m_Quadrant == 3 ) up[ 2 ] = double( 1 );
+      }
+      else if( ori == 2 )
+      {
+        if     ( this->m_Quadrant == 0 ) up[ 1 ] = double( -1 );
+        else if( this->m_Quadrant == 1 ) up[ 1 ] = double( -1 );
+        else if( this->m_Quadrant == 2 ) up[ 1 ] = double( -1 );
+        else if( this->m_Quadrant == 3 ) up[ 1 ] = double( -1 );
+        pos[ 2 ] *= double( -1 );
+
+      } // fi
+
+      // Reconfigure camera and return
+      auto camera = this->m_Renderer->GetActiveCamera( );
+      camera->ParallelProjectionOn( );
+      camera->SetFocalPoint( center );
+      camera->SetPosition( pos );
+      camera->SetViewUp( up );
+      this->m_Renderer->ResetCamera( bounds );
+    }
+    else
+      this->Superclass::ResetCamera( );
+  }
+  else
+    this->Superclass::ResetCamera( );
+}
+
+// -------------------------------------------------------------------------
+cpExtensions::QT::ImageWidget::
+TWLActor* cpExtensions::QT::ImageWidget::
+GetImageActor( )
+{
+  return( this->m_WLActor );
+}
+
+// -------------------------------------------------------------------------
+const cpExtensions::QT::ImageWidget::
+TWLActor* cpExtensions::QT::ImageWidget::
+GetImageActor( ) const
+{
+  return( this->m_WLActor );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::QT::ImageWidget::
+SetColor( const std::string& name, double r, double g, double b )
+{
+  auto props = this->GetViewProps( name );
+  for( auto p = props.begin( ); p != props.end( ); ++p )
+  {
+    auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
+    if( actor != NULL )
+      actor->GetProperty( )->SetColor( r, g, b );
+
+  } // rof
+  this->Render( );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::QT::ImageWidget::
+SetLineWidth( const std::string& name, double w )
+{
+  auto props = this->GetViewProps( name );
+  for( auto p = props.begin( ); p != props.end( ); ++p )
+  {
+    auto actor = dynamic_cast< vtkActor* >( p->GetPointer( ) );
+    if( actor != NULL )
+      actor->GetProperty( )->SetLineWidth( w );
+
+  } // rof
+  this->Render( );
+}
+
+// eof - $RCSfile$