]> Creatis software - cpPlugins.git/blobdiff - lib/cpExtensions/Visualization/ImageSliceActors.cxx
...
[cpPlugins.git] / lib / cpExtensions / Visualization / ImageSliceActors.cxx
index 52be1f1ddeb099e82990b032f4e002038356c40b..6d67a66c62ee64f05e8bbaf8c020b5840c4dc463 100644 (file)
 #include <cpExtensions/Visualization/ImageSliceActors.h>
+#include <cpExtensions/Visualization/ImageOutlineActor.h>
+#include <cpExtensions/Visualization/LUTImageActor.h>
+#include <cpExtensions/Visualization/WindowLevelImageActor.h>
 
-#include <vtkAlgorithmOutput.h>
-#include <vtkCellArray.h>
 #include <vtkImageData.h>
-#include <vtkInformation.h>
 #include <vtkPlane.h>
-#include <vtkPoints.h>
-#include <vtkProperty.h>
-#include <vtkStreamingDemandDrivenPipeline.h>
-#include <vtkTextProperty.h>
 
 // -------------------------------------------------------------------------
-cpExtensions::Visualization::ImageSliceActors*
 cpExtensions::Visualization::ImageSliceActors::
+Self* cpExtensions::Visualization::ImageSliceActors::
 New( )
 {
   return( new Self( ) );
 }
 
 // -------------------------------------------------------------------------
-void cpExtensions::Visualization::ImageSliceActors::
-AddInputConnection( vtkAlgorithmOutput* aout, int axis )
-{
-  unsigned int nImages = this->SliceMappers.size( );
-
-  vtkSmartPointer< vtkImageSliceMapper > mapper =
-    vtkSmartPointer< vtkImageSliceMapper >::New( );
-  this->SliceMappers.push_back( mapper );
-  mapper->SetInputConnection( aout );
-  mapper->SetOrientation(
-    ( nImages == 0 )? axis: this->SliceMappers[ 0 ]->GetOrientation( )
-    );
-  mapper->Update( );
-  
-  vtkSmartPointer< vtkImageActor > actor =
-    vtkSmartPointer< vtkImageActor >::New( );
-  this->ImageActors.push_back( actor );
-  actor->SetMapper( mapper );
-  actor->Modified( );
-
-  if( nImages == 0 )
-  {
-    this->AddItem( this->TextActor );
-    this->AddItem( this->PlaneActor );
-
-  } // fi
-  this->AddItem( actor );
-  this->Modified( );
-}
-
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::ImageSliceActors::
-AddInputData( vtkImageData* data, int axis )
-{
-  unsigned int nImages = this->SliceMappers.size( );
-
-  vtkSmartPointer< vtkImageSliceMapper > mapper =
-    vtkSmartPointer< vtkImageSliceMapper >::New( );
-  this->SliceMappers.push_back( mapper );
-  mapper->SetInputData( data );
-  mapper->SetOrientation(
-    ( nImages == 0 )? axis: this->SliceMappers[ 0 ]->GetOrientation( )
-    );
-  mapper->Update( );
-
-  vtkSmartPointer< vtkImageActor > actor =
-    vtkSmartPointer< vtkImageActor >::New( );
-  this->ImageActors.push_back( actor );
-  actor->SetMapper( mapper );
-  actor->Modified( );
-
-  if( nImages == 0 )
-  {
-    this->AddItem( this->TextActor );
-    this->AddItem( this->PlaneActor );
-
-  } // fi
-  this->AddItem( actor );
-  this->Modified( );
-}
-
-// -------------------------------------------------------------------------
-void cpExtensions::Visualization::ImageSliceActors::
-Clear( )
+vtkImageData* cpExtensions::Visualization::ImageSliceActors::
+GetImage( )
 {
-  // Unbind from container
-  this->RemoveAllItems( );
-
-  // Delete all images
-  this->SliceMappers.clear( );
-  this->ImageActors.clear( );
-
-  // Reconfigure unique objects
-  this->PlaneSource = vtkSmartPointer< vtkPolyData >::New( );
-  this->PlaneMapper = vtkSmartPointer< vtkPolyDataMapper >::New( );
-  this->TextActor   = vtkSmartPointer< vtkTextActor >::New( );
-  this->PlaneActor  = vtkSmartPointer< vtkActor >::New( );
-  this->TextBuffer[ 0 ] = '\0';
-
-  // Unique objects configuration
-  vtkSmartPointer< vtkPoints > plane_points =
-    vtkSmartPointer< vtkPoints >::New( );
-  vtkSmartPointer< vtkCellArray > plane_lines =
-    vtkSmartPointer< vtkCellArray >::New( );
-
-  plane_points->InsertNextPoint( 0, 0, 0 );
-  plane_points->InsertNextPoint( 0, 1, 0 );
-  plane_points->InsertNextPoint( 1, 1, 0 );
-  plane_points->InsertNextPoint( 1, 0, 0 );
-  plane_lines->InsertNextCell( 5 );
-  plane_lines->InsertCellPoint( 0 );
-  plane_lines->InsertCellPoint( 1 );
-  plane_lines->InsertCellPoint( 2 );
-  plane_lines->InsertCellPoint( 3 );
-  plane_lines->InsertCellPoint( 0 );
-  this->PlaneSource->SetPoints( plane_points );
-  this->PlaneSource->SetLines( plane_lines );
-
-  this->PlaneMapper->SetInputData( this->PlaneSource );
-  this->PlaneActor->SetMapper( this->PlaneMapper );
-
-  this->TextActor->SetTextScaleModeToNone( );
-  vtkTextProperty* textprop = this->TextActor->GetTextProperty( );
-  textprop->SetColor( 1, 1, 1 );
-  textprop->SetFontFamilyToCourier( );
-  textprop->SetFontSize( 18 );
-  textprop->BoldOff( );
-  textprop->ItalicOff( );
-  textprop->ShadowOff( );
-  textprop->SetJustificationToLeft( );
-  textprop->SetVerticalJustificationToBottom( );
-  vtkCoordinate* coord = this->TextActor->GetPositionCoordinate( );
-  coord->SetCoordinateSystemToNormalizedViewport( );
-  coord->SetValue( 0.01, 0.01 );
-}
-
-// -------------------------------------------------------------------------
-unsigned int cpExtensions::Visualization::ImageSliceActors::
-GetNumberOfImageActors( ) const
-{
-  return( this->ImageActors.size( ) );
-}
-
-// -------------------------------------------------------------------------
-vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
-GetImageActor( unsigned int id )
-{
-  if( id < this->ImageActors.size( ) )
-    return( this->ImageActors[ id ] );
+  if( this->m_WindowLevelImageActor.GetPointer( ) != NULL )
+    return( this->m_WindowLevelImageActor->GetImage( ) );
   else
     return( NULL );
 }
 
 // -------------------------------------------------------------------------
-const vtkImageActor* cpExtensions::Visualization::ImageSliceActors::
-GetImageActor( unsigned int id ) const
+const vtkImageData* cpExtensions::Visualization::ImageSliceActors::
+GetImage( ) const
 {
-  if( id < this->ImageActors.size( ) )
-    return( this->ImageActors[ id ] );
+  if( this->m_WindowLevelImageActor.GetPointer( ) != NULL )
+    return( this->m_WindowLevelImageActor->GetImage( ) );
   else
     return( NULL );
 }
 
 // -------------------------------------------------------------------------
-vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
-GetTextActor( )
+cpExtensions::Visualization::
+WindowLevelImageActor* cpExtensions::Visualization::ImageSliceActors::
+GetWindowLevelImageActor( )
 {
-  return( this->TextActor );
+  return( this->m_WindowLevelImageActor.GetPointer( ) );
 }
 
 // -------------------------------------------------------------------------
-const vtkTextActor* cpExtensions::Visualization::ImageSliceActors::
-GetTextActor( ) const
+const cpExtensions::Visualization::
+WindowLevelImageActor* cpExtensions::Visualization::ImageSliceActors::
+GetWindowLevelImageActor( ) const
 {
-  return( this->TextActor );
+  return( this->m_WindowLevelImageActor.GetPointer( ) );
 }
 
 // -------------------------------------------------------------------------
-vtkActor* cpExtensions::Visualization::ImageSliceActors::
-GetPlaneActor( )
+cpExtensions::Visualization::
+LUTImageActor* cpExtensions::Visualization::ImageSliceActors::
+GetLUTImageActor( )
 {
-  return( this->PlaneActor );
+  return( this->m_LUTImageActor.GetPointer( ) );
 }
 
 // -------------------------------------------------------------------------
-const vtkActor* cpExtensions::Visualization::ImageSliceActors::
-GetPlaneActor( ) const
+const cpExtensions::Visualization::
+LUTImageActor* cpExtensions::Visualization::ImageSliceActors::
+GetLUTImageActor( ) const
 {
-  return( this->PlaneActor );
+  return( this->m_LUTImageActor.GetPointer( ) );
 }
 
 // -------------------------------------------------------------------------
-double* cpExtensions::Visualization::ImageSliceActors::
-GetDisplayBounds( ) const
+cpExtensions::Visualization::
+ImageOutlineActor* cpExtensions::Visualization::ImageSliceActors::
+GetImageOutlineActor( )
 {
-  if( this->ImageActors.size( ) > 0 )
-    return( this->ImageActors[ 0 ]->GetDisplayBounds( ) );
-  else
-    return( NULL );
+  return( this->m_ImageOutlineActor.GetPointer( ) );
 }
 
 // -------------------------------------------------------------------------
-void cpExtensions::Visualization::ImageSliceActors::
-GetDisplayBounds( double bounds[ 6 ] ) const
+const cpExtensions::Visualization::
+ImageOutlineActor* cpExtensions::Visualization::ImageSliceActors::
+GetImageOutlineActor( ) const
 {
-  if( this->ImageActors.size( ) > 0 )
-    this->ImageActors[ 0 ]->GetDisplayBounds( bounds );
+  return( this->m_ImageOutlineActor.GetPointer( ) );
 }
 
 // -------------------------------------------------------------------------
 int cpExtensions::Visualization::ImageSliceActors::
-GetAxis( ) const
+GetOrientation( ) const
 {
-  if( this->SliceMappers.size( ) > 0 )
-    return( this->SliceMappers[ 0 ]->GetOrientation( ) );
+  if( this->m_WindowLevelImageActor.GetPointer( ) != NULL )
+    return( this->m_WindowLevelImageActor->GetOrientation( ) );
   else
-    return( -1 );
+    return( 0 );
 }
 
 // -------------------------------------------------------------------------
-int cpExtensions::Visualization::ImageSliceActors::
-GetSliceNumber( ) const
+void cpExtensions::Visualization::ImageSliceActors::
+SetImage( vtkImageData* image, int orientation )
 {
-  if( this->SliceMappers.size( ) > 0 )
-    return( this->SliceMappers[ 0 ]->GetSliceNumber( ) );
-  else
-    return( -1 );
-}
+  this->m_WindowLevelImageActor =
+    vtkSmartPointer< WindowLevelImageActor >::New( );
+  this->m_WindowLevelImageActor->SetImage( image );
+  this->m_WindowLevelImageActor->SetOrientation( orientation );
+  this->m_ImageOutlineActor = vtkSmartPointer< ImageOutlineActor >::New( );
+  this->m_LUTImageActor = NULL;
 
-// -------------------------------------------------------------------------
-int cpExtensions::Visualization::ImageSliceActors::
-GetSliceNumberMinValue( ) const
-{
-  if( this->SliceMappers.size( ) > 0 )
-    return( this->SliceMappers[ 0 ]->GetSliceNumberMinValue( ) );
-  else
-    return( -1 );
-}
+  // Put an initial slice
+  int ext[ 6 ];
+  image->GetExtent( ext );
+  this->SetSliceNumber(
+    ext[ this->m_WindowLevelImageActor->GetOrientation( ) << 1 ]
+    );
 
-// -------------------------------------------------------------------------
-int cpExtensions::Visualization::ImageSliceActors::
-GetSliceNumberMaxValue( ) const
-{
-  if( this->SliceMappers.size( ) > 0 )
-    return( this->SliceMappers[ 0 ]->GetSliceNumberMaxValue( ) );
-  else
-    return( -1 );
+  // Update collection
+  this->RemoveAllItems( );
+  this->AddItem( this->m_ImageOutlineActor );
+  this->AddItem( this->m_WindowLevelImageActor );
 }
 
 // -------------------------------------------------------------------------
-void cpExtensions::Visualization::ImageSliceActors::
-SetSliceNumber( const int& slice )
+unsigned int cpExtensions::Visualization::ImageSliceActors::
+AddLUTImage( vtkImageData* image )
 {
-  unsigned int nImages = this->SliceMappers.size( );
-  if( nImages == 0 )
-    return;
-
-  // Change visualization extent
-  for( unsigned int i = 0; i < nImages; ++i )
+  if( this->GetImage( ) != NULL )
   {
-    this->SliceMappers[ i ]->SetSliceNumber( slice );
-    this->SliceMappers[ i ]->Modified( );
-    this->ImageActors[ i ]->Modified( );
-    this->SliceMappers[ i ]->Update( );
-
-  } // rof
-
-  // Compute plane
-  vtkAlgorithm* algo = this->SliceMappers[ 0 ]->GetInputAlgorithm( );
-  vtkInformation* info = algo->GetOutputInformation( 0 );
-  int ext[ 6 ];
-  double ori[ 3 ], spac[ 3 ], pos[ 3 ];
-  info->Get( vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT( ), ext );
-  info->Get( vtkDataObject::ORIGIN( ), ori );
-  info->Get( vtkDataObject::SPACING( ), spac );
-  this->SliceMappers[ 0 ]->GetSlicePlane( )->GetOrigin( pos );
-
-  // Prevent obscuring voxels by offsetting the plane geometry
-  double xbnds[ ] =
-    {
-      ori[ 0 ] + ( spac[ 0 ] * double( ext[ 0 ] ) ),
-      ori[ 0 ] + ( spac[ 0 ] * double( ext[ 1 ] ) )
-    };
-  double ybnds[ ] =
+    if( this->m_LUTImageActor.GetPointer( ) == NULL )
     {
-      ori[ 1 ] + ( spac[ 1 ] * double( ext[ 2 ] ) ),
-      ori[ 1 ] + ( spac[ 1 ] * double( ext[ 3 ] ) )
-    };
-  double zbnds[ ] =
-    {
-      ori[ 2 ] + ( spac[ 2 ] * double( ext[ 4 ] ) ),
-      ori[ 2 ] + ( spac[ 2 ] * double( ext[ 5 ] ) )
-    };
-
-  if( spac[ 0 ] < double( 0 ) )
-  {
-    double t = xbnds[ 0 ];
-    xbnds[ 0 ] = xbnds[ 1 ];
-    xbnds[ 1 ] = t;
+      this->m_LUTImageActor = vtkSmartPointer< LUTImageActor >::New( );
+      this->m_LUTImageActor->SetOrientation( this->GetOrientation( ) );
+      this->AddItem( this->m_LUTImageActor );
 
-  } // fi
-  if( spac[ 1 ] < double( 0 ) )
-  {
-    double t = ybnds[ 0 ];
-    ybnds[ 0 ] = ybnds[ 1 ];
-    ybnds[ 1 ] = t;
-
-  } // fi
-  if( spac[ 2 ] < double( 0 ) )
-  {
-    double t = zbnds[ 0 ];
-    zbnds[ 0 ] = zbnds[ 1 ];
-    zbnds[ 1 ] = t;
-
-  } // fi
-
-  int axis = this->SliceMappers[ 0 ]->GetOrientation( );
-  this->PlaneActor->GetProperty( )->SetRepresentationToWireframe( );
-  this->PlaneActor->GetProperty( )->SetLineWidth( 2 );
-  vtkPoints* plane_points = this->PlaneSource->GetPoints( );
-  if( axis == 0 ) // YZ, x-normal
-  {
-    plane_points->SetPoint( 0, pos[ 0 ], ybnds[ 0 ], zbnds[ 0 ] );
-    plane_points->SetPoint( 1, pos[ 0 ], ybnds[ 1 ], zbnds[ 0 ] );
-    plane_points->SetPoint( 2, pos[ 0 ], ybnds[ 1 ], zbnds[ 1 ] );
-    plane_points->SetPoint( 3, pos[ 0 ], ybnds[ 0 ], zbnds[ 1 ] );
-    this->PlaneActor->GetProperty( )->SetColor( 1, 0, 0 );
-  }
-  else if( axis == 1 ) // ZX, y-normal
-  {
-    plane_points->SetPoint( 0, xbnds[ 0 ], pos[ 1 ], zbnds[ 0 ] );
-    plane_points->SetPoint( 1, xbnds[ 0 ], pos[ 1 ], zbnds[ 1 ] );
-    plane_points->SetPoint( 2, xbnds[ 1 ], pos[ 1 ], zbnds[ 1 ] );
-    plane_points->SetPoint( 3, xbnds[ 1 ], pos[ 1 ], zbnds[ 0 ] );
-    this->PlaneActor->GetProperty( )->SetColor( 0, 1, 0 );
+    } // fi
+    this->m_LUTImageActor->AddImage( image );
+    return( this->m_LUTImageActor->GetNumberOfImages( ) );
   }
-  else // XY, z-normal
-  {
-    plane_points->SetPoint( 0, xbnds[ 0 ], ybnds[ 0 ], pos[ 2 ] );
-    plane_points->SetPoint( 1, xbnds[ 1 ], ybnds[ 0 ], pos[ 2 ] );
-    plane_points->SetPoint( 2, xbnds[ 1 ], ybnds[ 1 ], pos[ 2 ] );
-    plane_points->SetPoint( 3, xbnds[ 0 ], ybnds[ 1 ], pos[ 2 ] );
-    this->PlaneActor->GetProperty( )->SetColor( 0, 0, 1 );
+  else
+    return( 0 );
+}
 
-  } // fi
-  this->PlaneSource->Modified( );
-  this->PlaneMapper->Modified( );
-  this->PlaneActor->Modified( );
-  this->Modified( );
+// -------------------------------------------------------------------------
+int cpExtensions::Visualization::ImageSliceActors::
+GetSliceNumber( ) const
+{
+  return( this->m_WindowLevelImageActor->GetSliceNumber( ) );
 }
 
 // -------------------------------------------------------------------------
 void cpExtensions::Visualization::ImageSliceActors::
-UpdateText( )
+SetSliceNumber( int slice )
 {
-  if( this->SliceMappers.size( ) > 0 )
-  {
-    char axis;
-    int axId = this->SliceMappers[ 0 ]->GetOrientation( );
-    if     ( axId == 0 ) axis = 'X';
-    else if( axId == 1 ) axis = 'Y';
-    else if( axId == 2 ) axis = 'Z';
+  this->m_WindowLevelImageActor->SetSliceNumber( slice );
+  if( this->m_LUTImageActor.GetPointer( ) != NULL )
+    this->m_LUTImageActor->SetSliceNumber( slice );
 
-    std::sprintf(
-      this->TextBuffer, "Axis: %c (%d)",
-      axis, this->SliceMappers[ 0 ]->GetSliceNumber( )
-      );
-  }
-  else
-    this->TextBuffer[ 0 ] = '\0';
-  this->TextActor->SetInput( this->TextBuffer );
-  this->TextActor->Modified( );
-  this->Modified( );
+  double bounds[ 6 ];
+  this->m_WindowLevelImageActor->GetImage( )->GetBounds( bounds );
+  int o = this->m_WindowLevelImageActor->GetOrientation( );
+  double p = this->m_WindowLevelImageActor->GetSlicePlane( )->GetOrigin( )[ o ];
+  this->m_ImageOutlineActor->SetBounds( o, p, bounds );
 }
 
 // -------------------------------------------------------------------------
@@ -379,7 +163,6 @@ cpExtensions::Visualization::ImageSliceActors::
 ImageSliceActors( )
   : Superclass( )
 {
-  this->Clear( );
 }
 
 // -------------------------------------------------------------------------