]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Visualization/MPRActors.cxx
sphere widget done, finally
[cpPlugins.git] / lib / cpExtensions / Visualization / MPRActors.cxx
index 2f7ce682a97e80585589e6a01a14873b52b3636a..b04194484b71dcbaf4b3d2eb670cf308b480f82a 100644 (file)
@@ -1,12 +1,12 @@
 #include <cpExtensions/Visualization/MPRActors.h>
 
+#include <vtkAlgorithmOutput.h>
 #include <vtkImageData.h>
-#include <vtkInformation.h>
 #include <vtkOutlineSource.h>
-#include <vtkPolyDataMapper.h>
 #include <vtkProperty.h>
 #include <vtkRenderer.h>
-#include <vtkWindowLevelLookupTable.h>
+#include <vtkRendererCollection.h>
+#include <vtkRenderWindow.h>
 
 // -------------------------------------------------------------------------
 cpExtensions::Visualization::MPRActors*
@@ -28,259 +28,107 @@ GetSliceActors( const int& i ) const
 }
 
 // -------------------------------------------------------------------------
-void cpExtensions::Visualization::MPRActors::
-SetInputConnection( vtkAlgorithmOutput* aout )
-{
-  this->ImageMapToColors->SetInputConnection( aout );
-  this->SetLookupTableToWindowLevel( );
-  this->_UpdateSlices( );
-}
-
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::MPRActors::
-SetInputData( vtkImageData* image )
-{
-  this->ImageMapToColors->SetInputData( image );
-  this->SetLookupTableToWindowLevel( );
-  this->_UpdateSlices( );
-}
-
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::MPRActors::
-PushDataInto(
-  vtkRenderer* x, vtkRenderer* y, vtkRenderer* z, vtkRenderer* w
-  )
+int cpExtensions::Visualization::MPRActors::
+AddInputConnection( vtkAlgorithmOutput* aout )
 {
-  vtkRenderer* rends[] = { x, y, z };
-  for( int i = 0; i < 3; ++i )
+  for( unsigned int i = 0; i < 2; ++i )
   {
-    if( rends[ i ] != NULL )
+    for( unsigned int j = 0; j < 3; ++j )
     {
-      rends[ i ]->AddActor( this->Slices[ 0 ][ i ]->GetImageActor( ) );
-      rends[ i ]->AddActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
-      for( int j = 0; j < 3; ++j )
-        rends[ i ]->AddActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
+      this->Slices[ i ][ j ]->SetInputConnection( aout );
+      this->Slices[ i ][ j ]->SetAxis( j );
 
-    } // fi
-    if( w != NULL )
-    {
-      w->AddActor( this->Slices[ 1 ][ i ]->GetImageActor( ) );
-      w->AddActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
-
-    } // fi
+    } // rof
 
   } // rof
-  if( w != NULL )
-    w->AddActor( this->ImageOutlineActor );
+  this->_CreateBoundingBox( );
+  return( 0 );
 }
 
 // -------------------------------------------------------------------------
-void cpExtensions::Visualization::MPRActors::
-PopDataFrom( vtkRenderer* x, vtkRenderer* y, vtkRenderer* z, vtkRenderer* w )
+int cpExtensions::Visualization::MPRActors::
+AddInputData( vtkImageData* new_image )
 {
-  vtkRenderer* rends[] = { x, y, z };
-  for( int i = 0; i < 3; ++i )
+  for( unsigned int i = 0; i < 2; ++i )
   {
-    if( rends[ i ] != NULL )
+    for( unsigned int j = 0; j < 3; ++j )
     {
-      rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetImageActor( ) );
-      rends[ i ]->RemoveActor( this->Slices[ 0 ][ i ]->GetTextActor( ) );
-      for( int j = 0; j < 3; ++j )
-        rends[ i ]->RemoveActor( this->Slices[ 0 ][ j ]->GetPlaneActor( ) );
-      if( w != NULL )
-      {
-        w->RemoveActor( this->Slices[ 1 ][ i ]->GetImageActor( ) );
-        w->RemoveActor( this->Slices[ 1 ][ i ]->GetPlaneActor( ) );
-
-      } // fi
+      this->Slices[ i ][ j ]->SetInputImage( new_image );
+      this->Slices[ i ][ j ]->SetAxis( j );
 
-    } // fi
+    } // rof
 
   } // rof
-
-  if( w != NULL )
-    w->RemoveActor( this->ImageOutlineActor );
+  this->_CreateBoundingBox( );
+  return( 0 );
 }
 
 // -------------------------------------------------------------------------
-vtkScalarsToColors* cpExtensions::Visualization::MPRActors::
-GetLookupTable( ) const
+unsigned int cpExtensions::Visualization::MPRActors::
+GetNumberOfImages( ) const
 {
-  return( this->ImageMapToColors->GetLookupTable( ) );
+  return( this->Slices[ 0 ][ 0 ]->GetNumberOfImages( ) );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::MPRActors::
-SetLookupTable( vtkScalarsToColors* lut )
-{
-  this->ImageMapToColors->SetLookupTable( lut );
-  this->ImageMapToColors->Update( );
-  this->Modified( );
-}
-
-// -------------------------------------------------------------------------
-vtkWindowLevelLookupTable* cpExtensions::Visualization::MPRActors::
-GetLookupTableAsWindowLevel( ) const
+PushActorsInto(
+  vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
+  vtkRenderWindow* w
+  )
 {
-  return(
-    dynamic_cast< vtkWindowLevelLookupTable* >( this->GetLookupTable( ) )
-    );
+  this->Slices[ 0 ][ 0 ]->PushActorsInto( x, true );
+  this->Slices[ 0 ][ 1 ]->PushActorsInto( y, true );
+  this->Slices[ 0 ][ 2 ]->PushActorsInto( z, true );
+  this->Slices[ 1 ][ 0 ]->PushActorsInto( w, false );
+  this->Slices[ 1 ][ 1 ]->PushActorsInto( w, false );
+  this->Slices[ 1 ][ 2 ]->PushActorsInto( w, false );
+
+  vtkRenderer* wren =
+    ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
+  if( wren != NULL )
+    wren->AddActor( this->ImageOutlineActor );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::MPRActors::
-SetLookupTableToWindowLevel( )
-{
-  // Check if the input has been configured
-  vtkImageData* image = this->_InputImage( );
-  if( image == NULL )
-    return;
-
-  double r[ 2 ];
-  image->GetScalarRange( r );
-
-  vtkSmartPointer< vtkWindowLevelLookupTable > lut =
-    vtkSmartPointer< vtkWindowLevelLookupTable >::New( );
-  lut->SetScaleToLinear( );
-  lut->SetTableRange( r );
-  lut->Build( );
-
-  this->SetLookupTable( lut );
-}
-
-// -------------------------------------------------------------------------
-double cpExtensions::Visualization::MPRActors::
-GetMinWindow( ) const
-{
-  return( double( 0 ) );
-}
-
-// -------------------------------------------------------------------------
-double cpExtensions::Visualization::MPRActors::
-GetMaxWindow( ) const
-{
-  // Check if the input has been configured
-  vtkImageData* image = this->_InputImage( );
-  if( image == NULL )
-    return( double( 0 ) );
-
-  double r[ 2 ];
-  image->GetScalarRange( r );
-  return( r[ 1 ] - r[ 0 ] );
-}
-
-// -------------------------------------------------------------------------
-double cpExtensions::Visualization::MPRActors::
-GetMinLevel( ) const
+PopActorsFrom(
+  vtkRenderWindow* x, vtkRenderWindow* y, vtkRenderWindow* z,
+  vtkRenderWindow* w
+  )
 {
-  // Check if the input has been configured
-  vtkImageData* image = this->_InputImage( );
-  if( image == NULL )
-    return( double( 0 ) );
-
-  double r[ 2 ];
-  image->GetScalarRange( r );
-  return( r[ 0 ] );
+  this->Slices[ 0 ][ 0 ]->PopActorsFrom( x );
+  this->Slices[ 0 ][ 1 ]->PopActorsFrom( y );
+  this->Slices[ 0 ][ 2 ]->PopActorsFrom( z );
+  this->Slices[ 1 ][ 0 ]->PopActorsFrom( w );
+  this->Slices[ 1 ][ 1 ]->PopActorsFrom( w );
+  this->Slices[ 1 ][ 2 ]->PopActorsFrom( w );
+
+  vtkRenderer* wren =
+    ( w != NULL )? w->GetRenderers( )->GetFirstRenderer( ): NULL;
+  if( wren != NULL )
+    wren->RemoveActor( this->ImageOutlineActor );
+
+  this->Slices[ 0 ][ 0 ]->Clear( );
+  this->Slices[ 0 ][ 1 ]->Clear( );
+  this->Slices[ 0 ][ 2 ]->Clear( );
+  this->Slices[ 1 ][ 0 ]->Clear( );
+  this->Slices[ 1 ][ 1 ]->Clear( );
+  this->Slices[ 1 ][ 2 ]->Clear( );
 }
 
 // -------------------------------------------------------------------------
 double cpExtensions::Visualization::MPRActors::
-GetMaxLevel( ) const
+GetWindow( )
 {
-  // Check if the input has been configured
-  vtkImageData* image = this->_InputImage( );
-  if( image == NULL )
-    return( double( 0 ) );
-
-  double r[ 2 ];
-  image->GetScalarRange( r );
-  return( r[ 1 ] );
+  return( this->Slices[ 0 ][ 0 ]->GetWindow( ) );
 }
 
 // -------------------------------------------------------------------------
 double cpExtensions::Visualization::MPRActors::
-GetWindow( ) const
+GetLevel( )
 {
-  vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
-  if( lut != NULL )
-    return( lut->GetWindow( ) );
-  else
-    return( double( 0 ) );
-}
-
-// -------------------------------------------------------------------------
-double cpExtensions::Visualization::MPRActors::
-GetLevel( ) const
-{
-  vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
-  if( lut != NULL )
-    return( lut->GetLevel( ) );
-  else
-    return( double( 0 ) );
-}
-
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::MPRActors::
-SetWindow( const double& w )
-{
-  vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
-  if( lut != NULL )
-  {
-    lut->SetWindow( w );
-    lut->Build( );
-    this->ImageMapToColors->Modified( );
-    this->Modified( );
-
-  } // fi
-}
-
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::MPRActors::
-SetLevel( const double& l )
-{
-  vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
-  if( lut != NULL )
-  {
-    lut->SetLevel( l );
-    lut->Build( );
-    this->ImageMapToColors->Modified( );
-    this->Modified( );
-
-  } // fi
-}
-
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::MPRActors::
-SetWindowLevel( const double& w, const double& l )
-{
-  vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
-  if( lut != NULL )
-  {
-    lut->SetWindow( l );
-    lut->SetLevel( l );
-    lut->Build( );
-    this->ImageMapToColors->Modified( );
-    this->Modified( );
-
-  } // fi
-}
-
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::MPRActors::
-ResetWindowLevel( )
-{
-  vtkImageData* image = this->_InputImage( );
-  vtkWindowLevelLookupTable* lut = this->GetLookupTableAsWindowLevel( );
-  if( image != NULL && lut != NULL )
-  {
-    double r[ 2 ];
-    image->GetScalarRange( r );
-    lut->SetTableRange( r );
-    lut->Build( );
-    this->ImageMapToColors->Modified( );
-    this->Modified( );
-
-  } // fi
+  return( this->Slices[ 0 ][ 0 ]->GetLevel( ) );
 }
 
 // -------------------------------------------------------------------------
@@ -308,7 +156,8 @@ GetSlice( const int& axis ) const
 void cpExtensions::Visualization::MPRActors::
 SetSlice( const int& axis, const int& slice )
 {
-  vtkImageData* image = this->_InputImage( );
+  vtkImageData* image =
+    dynamic_cast< vtkImageData* >( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
   if( image == NULL )
     return;
 
@@ -333,7 +182,8 @@ SetSlice( const int& axis, const int& slice )
 void cpExtensions::Visualization::MPRActors::
 SetSlice( const int& axis, const double& slice )
 {
-  vtkImageData* image = this->_InputImage( );
+  vtkImageData* image =
+    dynamic_cast< vtkImageData* >( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
   if( image == NULL )
     return;
 
@@ -350,16 +200,11 @@ SetSlice( const int& axis, const double& slice )
 void cpExtensions::Visualization::MPRActors::
 ResetSlices( )
 {
-  // TODO
-}
-
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::MPRActors::
-GetImageBounds( double bounds[ 6 ] ) const
-{
-  vtkImageData* image = this->_InputImage( );
-  if( image != NULL )
-    image->GetBounds( bounds );
+  for( unsigned int i = 0; i < 2; ++i )
+    for( unsigned int j = 0; j < 3; ++j )
+      this->Slices[ i ][ j ]->SetSliceNumber(
+        this->Slices[ i ][ j ]->GetSliceNumberMinValue( )
+        );
 }
 
 // -------------------------------------------------------------------------
@@ -367,14 +212,26 @@ cpExtensions::Visualization::MPRActors::
 MPRActors( )
   : Superclass( )
 {
-  this->ImageMapToColors = vtkSmartPointer< vtkImageMapToColors >::New( );
   this->ImageOutlineActor = vtkSmartPointer< vtkActor >::New( );
-  this->Slices[ 0 ][ 0 ] = vtkSmartPointer< _TSlice >::New( );
-  this->Slices[ 0 ][ 1 ] = vtkSmartPointer< _TSlice >::New( );
-  this->Slices[ 0 ][ 2 ] = vtkSmartPointer< _TSlice >::New( );
-  this->Slices[ 1 ][ 0 ] = vtkSmartPointer< _TSlice >::New( );
-  this->Slices[ 1 ][ 1 ] = vtkSmartPointer< _TSlice >::New( );
-  this->Slices[ 1 ][ 2 ] = vtkSmartPointer< _TSlice >::New( );
+  for( unsigned int i = 0; i < 2; ++i )
+    for( unsigned int j = 0; j < 3; ++j )
+      this->Slices[ i ][ j ] = vtkSmartPointer< ImageSliceActors >::New( );
+
+  this->Slices[ 0 ][ 0 ]->AssociateSlice( this->Slices[ 1 ][ 0 ] );
+  this->Slices[ 0 ][ 1 ]->AssociateSlice( this->Slices[ 1 ][ 1 ] );
+  this->Slices[ 0 ][ 2 ]->AssociateSlice( this->Slices[ 1 ][ 2 ] );
+
+  for( unsigned int i = 0; i < 3; ++ i )
+  {
+    this->Slices[ 0 ][ i ]->AddRenderCommand(
+      Self::_RenderCommand, this
+      );
+    this->Slices[ 0 ][ i ]->AddWindowLevelCommand(
+      Self::_WindowLevelCommand, this
+      );
+    this->Slices[ 0 ][ i ]->AddSlicesCommand( Self::_SlicesCommand, this );
+
+  } // rof
 }
 
 // -------------------------------------------------------------------------
@@ -383,39 +240,14 @@ cpExtensions::Visualization::MPRActors::
 {
 }
 
-// -------------------------------------------------------------------------
-vtkImageData* cpExtensions::Visualization::MPRActors::
-_InputImage( ) const
-{
-  vtkAlgorithm* algo = this->ImageMapToColors->GetInputAlgorithm( );
-  vtkInformation* info = algo->GetOutputInformation( 0 );
-  return(
-    vtkImageData::SafeDownCast( info->Get( vtkDataObject::DATA_OBJECT( ) ) )
-    );
-}
-
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::MPRActors::
-_UpdateSlices( )
+_CreateBoundingBox( )
 {
-  // Check if the input has been configured
-  vtkImageData* image = this->_InputImage( );
+  vtkImageData* image =
+    dynamic_cast< vtkImageData* >( this->Slices[ 0 ][ 0 ]->GetInputImage( ) );
   if( image == NULL )
     return;
-  this->ImageMapToColors->Update( );
-
-  for( int j = 0; j < 2; ++j )
-  {
-    for( int i = 0; i < 3; ++i )
-    {
-      this->Slices[ j ][ i ]->SetInputConnection(
-        this->ImageMapToColors->GetOutputPort( ), i
-        );
-      this->Slices[ j ][ i ]->UpdateText( );
-
-    } // rof
-
-  } // rof
 
   // Create 3D outline
   double bb[ 6 ];
@@ -432,80 +264,66 @@ _UpdateSlices( )
   this->ImageOutlineActor->GetProperty( )->SetColor( 1, 1, 1 );
   this->ImageOutlineActor->GetProperty( )->SetLineWidth( 1 );
 
-  this->ImageOutlineActorIndex = this->GetNumberOfItems( );
   this->AddItem( this->ImageOutlineActor );
+}
 
-  // Cursor radius
-  /*
-    double spac[ 3 ];
-    image->GetSpacing( spac );
-    double radius = spac[ 0 ];
-    radius = ( spac[ 1 ] < radius )? spac[ 1 ]: radius;
-    radius = ( spac[ 2 ] < radius )? spac[ 2 ]: radius;
-    radius *= double( 6 );
-    this->Cursor->SetRadius( radius );
-    this->CursorMapper->Modified( );
-    this->CursorActor->Modified( );
-
-    // Plane actors
-    for( int a = 0; a < 3; ++a )
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::MPRActors::
+_SlicesCommand( double* pos, int axis, void* data )
+{
+  MPRActors* actors = reinterpret_cast< MPRActors* >( data );
+  if( actors == NULL )
+    return;
+  for( unsigned int j = 0; j < 3; ++j )
+  {
+    if( actors->Slices[ 0 ][ j ]->GetAxis( ) != axis )
     {
-    // Configure actors
-    this->Planes[ a ].Configure( this->ImageToWindowLevel->GetOutputPort( ), a );
-    this->Planes[ a ].ConfigureRegion( this->Region->GetOutputPort( ) );
-    this->Planes[ a ].UpdateText( this->GetWindow( ), this->GetLevel( ) );
-
-    // Add them to renderer
-    vtkRenderer* ren = this->Interactors[ a ]->GetRenderWindow( )->
-    GetRenderers( )->GetFirstRenderer( );
-    if( ren == NULL )
-    vtkErrorMacro( "At least one interactor doesn't have a valid renderer" );
-    ren->AddActor( this->Planes[ a ].ImageActor );
-    ren->AddActor( this->Planes[ a ].TextActor );
-
-    for( int i = 0; i < 3; ++i )
-    this->Interactors[ a ]->GetRenderWindow( )->
-    GetRenderers( )->GetFirstRenderer( )->
-    AddActor( this->Planes[ i ].PlaneActor );
+      actors->Slices[ 0 ][ j ]->SetSlice( pos );
+      actors->Slices[ 1 ][ j ]->SetSlice( pos );
+      actors->Slices[ 0 ][ j ]->Render( 1e-3 );
+      actors->Slices[ 1 ][ j ]->Render( 1e-3 );
 
-    } // rof
-  */
-  // Keep track into collection
-  /*
-    this->XPlaneIndex = this->GetNumberOfItems( );
-    this->AddItem( this->Planes[ 0 ].ImageActor.GetPointer( ) );
-    this->XTextIndex = this->GetNumberOfItems( );
-    this->AddItem( this->Planes[ 0 ].TextActor.GetPointer( ) );
-    this->XBoundsIndex = this->GetNumberOfItems( );
-    this->AddItem( this->Planes[ 0 ].PlaneActor.GetPointer( ) );
-
-    this->YPlaneIndex = this->GetNumberOfItems( );
-    this->AddItem( this->Planes[ 1 ].ImageActor.GetPointer( ) );
-    this->YTextIndex = this->GetNumberOfItems( );
-    this->AddItem( this->Planes[ 1 ].TextActor.GetPointer( ) );
-    this->YBoundsIndex = this->GetNumberOfItems( );
-    this->AddItem( this->Planes[ 1 ].PlaneActor.GetPointer( ) );
-
-    this->ZPlaneIndex = this->GetNumberOfItems( );
-    this->AddItem( this->Planes[ 2 ].ImageActor.GetPointer( ) );
-    this->ZTextIndex = this->GetNumberOfItems( );
-    this->AddItem( this->Planes[ 2 ].TextActor.GetPointer( ) );
-    this->ZBoundsIndex = this->GetNumberOfItems( );
-    this->AddItem( this->Planes[ 2 ].PlaneActor.GetPointer( ) );
-  */
-  // Initialize slice visualization
-  // this->ResetSlices( );
-
-  /*
-    #error CONTOUR_WIDGET <- ACA VOY
-    static vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep =
-    vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
-    static vtkSmartPointer<vtkContourWidget> contourWidget =
-    vtkSmartPointer<vtkContourWidget>::New();
-    contourWidget->SetInteractor( zi );
-    contourWidget->SetRepresentation( contourRep );
-    contourWidget->On( );
-  */
+    } // fi
+
+  } // rof
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::MPRActors::
+_WindowLevelCommand( double window, double level, void* data )
+{
+  MPRActors* actors = reinterpret_cast< MPRActors* >( data );
+  if( actors == NULL )
+    return;
+
+  actors->Slices[ 0 ][ 0 ]->SetWindowLevel( window, level );
+  actors->Slices[ 0 ][ 1 ]->SetWindowLevel( window, level );
+  actors->Slices[ 0 ][ 2 ]->SetWindowLevel( window, level );
+  actors->Slices[ 1 ][ 0 ]->SetWindowLevel( window, level );
+  actors->Slices[ 1 ][ 1 ]->SetWindowLevel( window, level );
+  actors->Slices[ 1 ][ 2 ]->SetWindowLevel( window, level );
+
+  actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
+  actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
+  actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
+  actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
+  actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
+  actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );
+}
+
+// -------------------------------------------------------------------------
+void cpExtensions::Visualization::MPRActors::
+_RenderCommand( void* data )
+{
+  MPRActors* actors = reinterpret_cast< MPRActors* >( data );
+  if( actors == NULL )
+    return;
+  actors->Slices[ 0 ][ 0 ]->Render( 1e-3 );
+  actors->Slices[ 0 ][ 1 ]->Render( 1e-3 );
+  actors->Slices[ 0 ][ 2 ]->Render( 1e-3 );
+  actors->Slices[ 1 ][ 0 ]->Render( 1e-3 );
+  actors->Slices[ 1 ][ 1 ]->Render( 1e-3 );
+  actors->Slices[ 1 ][ 2 ]->Render( 1e-3 );
 }
 
 // eof - $RCSfile$